Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_VolumeCollider.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 base volume collider class.
21  *      \file           OPC_VolumeCollider.h
22  *      \author         Pierre Terdiman
23  *      \date           June, 2, 2001
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_VOLUMECOLLIDER_H__
30 #define __OPC_VOLUMECOLLIDER_H__
31
32         struct OPCODE_API VolumeCache
33         {
34                                                         VolumeCache() : Model(null)             {}
35                                                         ~VolumeCache()                                  {}
36
37                 Container                       TouchedPrimitives;      //!< Indices of touched primitives
38                 const BaseModel*        Model;                          //!< Owner
39         };
40
41         class OPCODE_API VolumeCollider : public Collider
42         {
43                 public:
44                 // Constructor / Destructor
45                                                                                         VolumeCollider();
46                 virtual                                                         ~VolumeCollider() = 0;
47
48                 // Collision report
49
50                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51                 /**
52                  *      Gets the number of touched primitives after a collision query.
53                  *      \see            GetContactStatus()
54                  *      \see            GetTouchedPrimitives()
55                  *      \return         the number of touched primitives
56                  */
57                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
58                 inline_                         udword                  GetNbTouchedPrimitives()        const   { return mTouchedPrimitives ? mTouchedPrimitives->GetNbEntries() : 0;   }
59
60                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61                 /**
62                  *      Gets the list of touched primitives after a collision query.
63                  *      \see            GetContactStatus()
64                  *      \see            GetNbTouchedPrimitives()
65                  *      \return         the list of touched primitives (primitive indices)
66                  */
67                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68                 inline_         const   udword*                 GetTouchedPrimitives()          const   { return mTouchedPrimitives ? mTouchedPrimitives->GetEntries() : null;  }
69
70                 // Stats
71
72                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73                 /**
74                  *      Stats: gets the number of Volume-BV overlap tests after a collision query.
75                  *      \see            GetNbVolumePrimTests()
76                  *      \return         the number of Volume-BV tests performed during last query
77                  */
78                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
79                 inline_                         udword                  GetNbVolumeBVTests()            const   { return mNbVolumeBVTests;                                                                                              }
80
81                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82                 /**
83                  *      Stats: gets the number of Volume-Triangle overlap tests after a collision query.
84                  *      \see            GetNbVolumeBVTests()
85                  *      \return         the number of Volume-Triangle tests performed during last query
86                  */
87                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88                 inline_                         udword                  GetNbVolumePrimTests()          const   { return mNbVolumePrimTests;                                                                                    }
89
90                 // Settings
91
92                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93                 /**
94                  *      Validates current settings. You should call this method after all the settings / callbacks have been defined for a collider.
95                  *      \return         null if everything is ok, else a string describing the problem
96                  */
97                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98                 override(Collider)      const char*             ValidateSettings();
99
100                 protected:
101                 // Touched primitives
102                                                         Container*              mTouchedPrimitives;     //!< List of touched primitives
103
104                 // Dequantization coeffs
105                                                         Point                   mCenterCoeff;
106                                                         Point                   mExtentsCoeff;
107                 // Stats
108                                                         udword                  mNbVolumeBVTests;       //!< Number of Volume-BV tests
109                                                         udword                  mNbVolumePrimTests;     //!< Number of Volume-Primitive tests
110                 // Internal methods
111                                                         void                    _Dump(const AABBCollisionNode* node);
112                                                         void                    _Dump(const AABBNoLeafNode* node);
113                                                         void                    _Dump(const AABBQuantizedNode* node);
114                                                         void                    _Dump(const AABBQuantizedNoLeafNode* node);
115
116                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
117                 /**
118                  *      Initializes a query
119                  */
120                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121                 override(Collider) inline_      void    InitQuery()
122                                                                                         {
123                                                                                                 // Reset stats & contact status
124                                                                                                 mNbVolumeBVTests        = 0;
125                                                                                                 mNbVolumePrimTests      = 0;
126                                                                                                 Collider::InitQuery();
127                                                                                         }
128
129                 inline_                         BOOL                    IsCacheValid(VolumeCache& cache)
130                                                                                         {
131                                                                                                 // We're going to do a volume-vs-model query.
132                                                                                                 if(cache.Model!=mCurrentModel)
133                                                                                                 {
134                                                                                                         // Cached list was for another model so we can't keep it
135                                                                                                         // Keep track of new owner and reset cache
136                                                                                                         cache.Model = mCurrentModel;
137                                                                                                         return FALSE;
138                                                                                                 }
139                                                                                                 else
140                                                                                                 {
141                                                                                                         // Same models, no problem
142                                                                                                         return TRUE;
143                                                                                                 }
144                                                                                         }
145         };
146
147 #endif // __OPC_VOLUMECOLLIDER_H__