Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / Ice / IceOBB.h
1 /*
2  *      ICE / 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  *      Contains OBB-related code. (oriented bounding box)
20  *      \file           IceOBB.h
21  *      \author         Pierre Terdiman
22  *      \date           January, 13, 2000
23  */
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 // Include Guard
28 #ifndef __ICEOBB_H__
29 #define __ICEOBB_H__
30
31         // Forward declarations
32         class LSS;
33
34         class ICEMATHS_API OBB
35         {
36                 public:
37                 //! Constructor
38                 inline_                                         OBB()           {}
39                 //! Constructor
40                 inline_                                         OBB(const Point& center, const Point& extents, const Matrix3x3& rot) : mCenter(center), mExtents(extents), mRot(rot)    {}
41                 //! Destructor
42                 inline_                                         ~OBB()          {}
43
44                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45                 /**
46                  *      Setups an empty OBB.
47                  */
48                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49                                 void                            SetEmpty()
50                                                                         {
51                                                                                 mCenter.Zero();
52                                                                                 mExtents.Set(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
53                                                                                 mRot.Identity();
54                                                                         }
55
56                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57                 /**
58                  *      Tests if a point is contained within the OBB.
59                  *      \param          p       [in] the world point to test
60                  *      \return         true if inside the OBB
61                  */
62                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63                                 bool                            ContainsPoint(const Point& p)   const;
64
65                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66                 /**
67                  *      Builds an OBB from an AABB and a world transform.
68                  *      \param          aabb    [in] the aabb
69                  *      \param          mat             [in] the world transform
70                  */
71                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72                                 void                            Create(const AABB& aabb, const Matrix4x4& mat);
73
74                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75                 /**
76                  *      Recomputes the OBB after an arbitrary transform by a 4x4 matrix.
77                  *      \param          mtx             [in] the transform matrix
78                  *      \param          obb             [out] the transformed OBB
79                  */
80                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81                 inline_ void                            Rotate(const Matrix4x4& mtx, OBB& obb)  const
82                                                                         {
83                                                                                 // The extents remain constant
84                                                                                 obb.mExtents = mExtents;
85                                                                                 // The center gets x-formed
86                                                                                 obb.mCenter = mCenter * mtx;
87                                                                                 // Combine rotations
88                                                                                 obb.mRot = mRot * Matrix3x3(mtx);
89                                                                         }
90
91                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92                 /**
93                  *      Checks the OBB is valid.
94                  *      \return         true if the box is valid
95                  */
96                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97                 inline_ BOOL                            IsValid()       const
98                                                                         {
99                                                                                 // Consistency condition for (Center, Extents) boxes: Extents >= 0.0f
100                                                                                 if(mExtents.x < 0.0f)   return FALSE;
101                                                                                 if(mExtents.y < 0.0f)   return FALSE;
102                                                                                 if(mExtents.z < 0.0f)   return FALSE;
103                                                                                 return TRUE;
104                                                                         }
105
106                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107                 /**
108                  *      Computes the obb planes.
109                  *      \param          planes  [out] 6 box planes
110                  *      \return         true if success
111                  */
112                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113                                 bool                            ComputePlanes(Plane* planes)    const;
114
115                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116                 /**
117                  *      Computes the obb points.
118                  *      \param          pts     [out] 8 box points
119                  *      \return         true if success
120                  */
121                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122                                 bool                            ComputePoints(Point* pts)       const;
123
124                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125                 /**
126                  *      Computes vertex normals.
127                  *      \param          pts     [out] 8 box points
128                  *      \return         true if success
129                  */
130                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131                                 bool                            ComputeVertexNormals(Point* pts)        const;
132
133                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
134                 /**
135                  *      Returns edges.
136                  *      \return         24 indices (12 edges) indexing the list returned by ComputePoints()
137                  */
138                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139                                 const udword*           GetEdges()      const;
140
141                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
142                 /**
143                  *      Returns local edge normals.
144                  *      \return         edge normals in local space
145                  */
146                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147                                 const Point*            GetLocalEdgeNormals()   const;
148
149                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150                 /**
151                  *      Returns world edge normal
152                  *      \param          edge_index              [in] 0 <= edge index < 12
153                  *      \param          world_normal    [out] edge normal in world space
154                  */
155                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156                                 void                            ComputeWorldEdgeNormal(udword edge_index, Point& world_normal)  const;
157
158                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159                 /**
160                  *      Computes an LSS surrounding the OBB.
161                  *      \param          lss             [out] the LSS
162                  */
163                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
164                                 void                            ComputeLSS(LSS& lss)    const;
165
166                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
167                 /**
168                  *      Checks the OBB is inside another OBB.
169                  *      \param          box             [in] the other OBB
170                  *      \return         TRUE if we're inside the other box
171                  */
172                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
173                                 BOOL                            IsInside(const OBB& box)        const;
174
175                 inline_ const Point&            GetCenter()             const   { return mCenter;       }
176                 inline_ const Point&            GetExtents()    const   { return mExtents;      }
177                 inline_ const Matrix3x3&        GetRot()                const   { return mRot;          }
178
179                 inline_ void                            GetRotatedExtents(Matrix3x3& extents)   const
180                                                                         {
181                                                                                 extents = mRot;
182                                                                                 extents.Scale(mExtents);
183                                                                         }
184
185                                 Point                           mCenter;                //!< B for Box
186                                 Point                           mExtents;               //!< B for Bounding
187                                 Matrix3x3                       mRot;                   //!< O for Oriented
188
189                                 // Orientation is stored in row-major format,
190                                 // i.e. rows = eigen vectors of the covariance matrix
191         };
192
193 #endif  // __ICEOBB_H__