Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_Common.h
1 /*
2  *      OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose, 
10 including commercial applications, and to alter it and redistribute it freely, 
11 subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
16 */
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20  *      Contains common classes & defs used in OPCODE.
21  *      \file           OPC_Common.h
22  *      \author         Pierre Terdiman
23  *      \date           March, 20, 2001
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_COMMON_H__
30 #define __OPC_COMMON_H__
31
32 // [GOTTFRIED]: Just a small change for readability.
33 #ifdef OPC_CPU_COMPARE
34         #define GREATER(x, y)   AIR(x) > IR(y)
35 #else
36         #define GREATER(x, y)   fabsf(x) > (y)
37 #endif
38
39         class OPCODE_API CollisionAABB
40         {
41                 public:
42                 //! Constructor
43                 inline_                         CollisionAABB()                                         {}
44                 //! Constructor
45                 inline_                         CollisionAABB(const AABB& b)            { b.GetCenter(mCenter); b.GetExtents(mExtents); }
46                 //! Destructor
47                 inline_                         ~CollisionAABB()                                        {}
48
49                 //! Get min point of the box
50                 inline_ void            GetMin(Point& min)              const           { min = mCenter - mExtents;                                     }
51                 //! Get max point of the box
52                 inline_ void            GetMax(Point& max)              const           { max = mCenter + mExtents;                                     }
53
54                 //! Get component of the box's min point along a given axis
55                 inline_ float           GetMin(udword axis)             const           { return mCenter[axis] - mExtents[axis];        }
56                 //! Get component of the box's max point along a given axis
57                 inline_ float           GetMax(udword axis)             const           { return mCenter[axis] + mExtents[axis];        }
58
59                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60                 /**
61                  *      Setups an AABB from min & max vectors.
62                  *      \param          min                     [in] the min point
63                  *      \param          max                     [in] the max point
64                  */
65                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66                 inline_ void            SetMinMax(const Point& min, const Point& max)           { mCenter = (max + min)*0.5f; mExtents = (max - min)*0.5f;              }
67
68                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69                 /**
70                  *      Checks a box is inside another box.
71                  *      \param          box             [in] the other box
72                  *      \return         true if current box is inside input box
73                  */
74                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75                 inline_ BOOL            IsInside(const CollisionAABB& box) const
76                                                         {
77                                                                 if(box.GetMin(0)>GetMin(0))     return FALSE;
78                                                                 if(box.GetMin(1)>GetMin(1))     return FALSE;
79                                                                 if(box.GetMin(2)>GetMin(2))     return FALSE;
80                                                                 if(box.GetMax(0)<GetMax(0))     return FALSE;
81                                                                 if(box.GetMax(1)<GetMax(1))     return FALSE;
82                                                                 if(box.GetMax(2)<GetMax(2))     return FALSE;
83                                                                 return TRUE;
84                                                         }
85
86                                 Point           mCenter;                                //!< Box center
87                                 Point           mExtents;                               //!< Box extents
88         };
89
90         class OPCODE_API QuantizedAABB
91         {
92                 public:
93                 //! Constructor
94                 inline_                         QuantizedAABB()                 {}
95                 //! Destructor
96                 inline_                         ~QuantizedAABB()                {}
97
98                                 sword           mCenter[3];                             //!< Quantized center
99                                 uword           mExtents[3];                    //!< Quantized extents
100         };
101
102         //! Quickly rotates & translates a vector
103         inline_ void TransformPoint(Point& dest, const Point& source, const Matrix3x3& rot, const Point& trans)
104         {
105                 dest.x = trans.x + source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];
106                 dest.y = trans.y + source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];
107                 dest.z = trans.z + source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];
108         }
109
110 #endif //__OPC_COMMON_H__