Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_PlanesCollider.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 a planes collider.
21  *      \file           OPC_PlanesCollider.h
22  *      \author         Pierre Terdiman
23  *      \date           January, 1st, 2002
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_PLANESCOLLIDER_H__
30 #define __OPC_PLANESCOLLIDER_H__
31
32         struct OPCODE_API PlanesCache : VolumeCache
33         {
34                                         PlanesCache()
35                                         {
36                                         }
37         };
38
39         class OPCODE_API PlanesCollider : public VolumeCollider
40         {
41                 public:
42                 // Constructor / Destructor
43                                                                                         PlanesCollider();
44                 virtual                                                         ~PlanesCollider();
45
46                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47                 /**
48                  *      Generic collision query for generic OPCODE models. After the call, access the results:
49                  *      - with GetContactStatus()
50                  *      - with GetNbTouchedPrimitives()
51                  *      - with GetTouchedPrimitives()
52                  *
53                  *      \param          cache                   [in/out] a planes cache
54                  *      \param          planes                  [in] list of planes in world space
55                  *      \param          nb_planes               [in] number of planes
56                  *      \param          model                   [in] Opcode model to collide with
57                  *      \param          worldm                  [in] model's world matrix, or null
58                  *      \return         true if success
59                  *      \warning        SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
60                  */
61                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62                                                         bool                    Collide(PlanesCache& cache, const Plane* planes, udword nb_planes, const Model& model, const Matrix4x4* worldm=null);
63
64                 // Mutant box-with-planes collision queries
65                 inline_                         bool                    Collide(PlanesCache& cache, const OBB& box, const Model& model, const Matrix4x4* worldb=null, const Matrix4x4* worldm=null)
66                                                                                         {
67                                                                                                 Plane PL[6];
68
69                                                                                                 if(worldb)
70                                                                                                 {
71                                                                                                         // Create a new OBB in world space
72                                                                                                         OBB WorldBox;
73                                                                                                         box.Rotate(*worldb, WorldBox);
74                                                                                                         // Compute planes from the sides of the box
75                                                                                                         WorldBox.ComputePlanes(PL);
76                                                                                                 }
77                                                                                                 else
78                                                                                                 {
79                                                                                                         // Compute planes from the sides of the box
80                                                                                                         box.ComputePlanes(PL);
81                                                                                                 }
82
83                                                                                                 // Collide with box planes
84                                                                                                 return Collide(cache, PL, 6, model, worldm);
85                                                                                         }
86                 // Settings
87
88                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89                 /**
90                  *      Validates current settings. You should call this method after all the settings and callbacks have been defined for a collider.
91                  *      \return         null if everything is ok, else a string describing the problem
92                  */
93                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94                 override(Collider)      const char*             ValidateSettings();
95
96                 protected:
97                 // Planes in model space
98                                                         udword                  mNbPlanes;
99                                                         Plane*                  mPlanes;
100                 // Leaf description
101                                                         VertexPointers  mVP;
102                 // Internal methods
103                                                         void                    _Collide(const AABBCollisionNode* node, udword clip_mask);
104                                                         void                    _Collide(const AABBNoLeafNode* node, udword clip_mask);
105                                                         void                    _Collide(const AABBQuantizedNode* node, udword clip_mask);
106                                                         void                    _Collide(const AABBQuantizedNoLeafNode* node, udword clip_mask);
107                                                         void                    _CollideNoPrimitiveTest(const AABBCollisionNode* node, udword clip_mask);
108                                                         void                    _CollideNoPrimitiveTest(const AABBNoLeafNode* node, udword clip_mask);
109                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNode* node, udword clip_mask);
110                                                         void                    _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node, udword clip_mask);
111                         // Overlap tests
112                 inline_                         BOOL                    PlanesAABBOverlap(const Point& center, const Point& extents, udword& out_clip_mask, udword in_clip_mask);
113                 inline_                         BOOL                    PlanesTriOverlap(udword in_clip_mask);
114                         // Init methods
115                                                         BOOL                    InitQuery(PlanesCache& cache, const Plane* planes, udword nb_planes, const Matrix4x4* worldm=null);
116         };
117
118         class OPCODE_API HybridPlanesCollider : public PlanesCollider
119         {
120                 public:
121                 // Constructor / Destructor
122                                                                                         HybridPlanesCollider();
123                 virtual                                                         ~HybridPlanesCollider();
124
125                                                         bool                    Collide(PlanesCache& cache, const Plane* planes, udword nb_planes, const HybridModel& model, const Matrix4x4* worldm=null);
126                 protected:
127                                                         Container               mTouchedBoxes;
128         };
129
130 #endif // __OPC_PLANESCOLLIDER_H__