Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_OBBCollider.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 code for an OBB collider.
21  *      \file           OPC_OBBCollider.h
22  *      \author         Pierre Terdiman
23  *      \date           January, 1st, 2002
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_OBBCOLLIDER_H__
30 #define __OPC_OBBCOLLIDER_H__
31
32         struct OPCODE_API OBBCache : VolumeCache
33         {
34                                         OBBCache() : FatCoeff(1.1f)
35                                         {
36                                                 FatBox.mCenter.Zero();
37                                                 FatBox.mExtents.Zero();
38                                                 FatBox.mRot.Identity();
39                                         }
40
41                 // Cached faces signature
42                 OBB                             FatBox;         //!< Box used when performing the query resulting in cached faces
43                 // User settings
44                 float                   FatCoeff;       //!< extents multiplier used to create a fat box
45         };
46
47         class OPCODE_API OBBCollider : public VolumeCollider
48         {
49                 public:
50                 // Constructor / Destructor
51                                                                                         OBBCollider();
52                 virtual                                                         ~OBBCollider();
53
54                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55                 /**
56                  *      Generic collision query for generic OPCODE models. After the call, access the results:
57                  *      - with GetContactStatus()
58                  *      - with GetNbTouchedPrimitives()
59                  *      - with GetTouchedPrimitives()
60                  *
61                  *      \param          cache                   [in/out] a box cache
62                  *      \param          box                             [in] collision OBB in local space
63                  *      \param          model                   [in] Opcode model to collide with
64                  *      \param          worldb                  [in] OBB's world matrix, or null
65                  *      \param          worldm                  [in] model's world matrix, or null
66                  *      \return         true if success
67                  *      \warning        SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
68                  */
69                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70                                                         bool                    Collide(OBBCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
71
72                 // Settings
73
74                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75                 /**
76                  *      Settings: select between full box-box tests or "SAT-lite" tests (where Class III axes are discarded)
77                  *      \param          flag            [in] true for full tests, false for coarse tests
78                  */
79                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80                 inline_                         void                    SetFullBoxBoxTest(bool flag)    { mFullBoxBoxTest = flag;       }
81
82                 // Settings
83
84                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85                 /**
86                  *      Validates current settings. You should call this method after all the settings and callbacks have been defined for a collider.
87                  *      \return         null if everything is ok, else a string describing the problem
88                  */
89                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90                 override(Collider)      const char*             ValidateSettings();
91
92                 protected:
93                 // Precomputed data
94                                                         Matrix3x3               mAR;                            //!< Absolute rotation matrix
95                                                         Matrix3x3               mRModelToBox;           //!< Rotation from model space to obb space
96                                                         Matrix3x3               mRBoxToModel;           //!< Rotation from obb space to model space
97                                                         Point                   mTModelToBox;           //!< Translation from model space to obb space
98                                                         Point                   mTBoxToModel;           //!< Translation from obb space to model space
99
100                                                         Point                   mBoxExtents;
101                                                         Point                   mB0;                            //!< - mTModelToBox + mBoxExtents
102                                                         Point                   mB1;                            //!< - mTModelToBox - mBoxExtents
103
104                                                         float                   mBBx1;
105                                                         float                   mBBy1;
106                                                         float                   mBBz1;
107
108                                                         float                   mBB_1;
109                                                         float                   mBB_2;
110                                                         float                   mBB_3;
111                                                         float                   mBB_4;
112                                                         float                   mBB_5;
113                                                         float                   mBB_6;
114                                                         float                   mBB_7;
115                                                         float                   mBB_8;
116                                                         float                   mBB_9;
117
118                 // Leaf description
119                                                         Point                   mLeafVerts[3];          //!< Triangle vertices
120                 // Settings
121                                                         bool                    mFullBoxBoxTest;        //!< Perform full BV-BV tests (true) or SAT-lite tests (false)
122                 // Internal methods
123                                                         void                    _Collide(const AABBCollisionNode* node);
124                                                         void                    _Collide(const AABBNoLeafNode* node);
125                                                         void                    _Collide(const AABBQuantizedNode* node);
126                                                         void                    _Collide(const AABBQuantizedNoLeafNode* node);
127                                                         void                    _CollideNoPrimitiveTest(const AABBCollisionNode* node);
128                                                         void                    _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
129                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
130                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
131                         // Overlap tests
132                 inline_                         BOOL                    OBBContainsBox(const Point& bc, const Point& be);
133                 inline_                         BOOL                    BoxBoxOverlap(const Point& extents, const Point& center);
134                 inline_                         BOOL                    TriBoxOverlap();
135                         // Init methods
136                                                         BOOL                    InitQuery(OBBCache& cache, const OBB& box, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
137         };
138
139         class OPCODE_API HybridOBBCollider : public OBBCollider
140         {
141                 public:
142                 // Constructor / Destructor
143                                                                                         HybridOBBCollider();
144                 virtual                                                         ~HybridOBBCollider();
145
146                                                         bool                    Collide(OBBCache& cache, const OBB& box, const HybridModel& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null);
147                 protected:
148                                                         Container               mTouchedBoxes;
149         };
150
151 #endif // __OPC_OBBCOLLIDER_H__