Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_HybridModel.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 hybrid models.
21  *      \file           OPC_HybridModel.h
22  *      \author         Pierre Terdiman
23  *      \date           May, 18, 2003
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_HYBRIDMODEL_H__
30 #define __OPC_HYBRIDMODEL_H__
31
32         //! Leaf descriptor
33         struct LeafTriangles
34         {
35                 udword                  Data;           //!< Packed data
36
37                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38                 /**
39                  *      Gets number of triangles in the leaf.
40                  *      \return         number of triangles N, with 0 < N <= 16
41                  */
42                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43                 inline_ udword  GetNbTriangles()                                const   { return (Data & 15)+1;                                                                                 }
44
45                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46                 /**
47                  *      Gets triangle index for this leaf. Indexed model's array of indices retrieved with HybridModel::GetIndices()
48                  *      \return         triangle index
49                  */
50                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51                 inline_ udword  GetTriangleIndex()                              const   { return Data>>4;                                                                                               }
52                 inline_ void    SetData(udword nb, udword index)                { ASSERT(nb>0 && nb<=16);       nb--;   Data = (index<<4)|(nb&15);      }
53         };
54
55         class OPCODE_API HybridModel : public BaseModel
56         {
57                 public:
58                 // Constructor/Destructor
59                                                                                                         HybridModel();
60                 virtual                                                                         ~HybridModel();
61
62                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63                 /**
64                  *      Builds a collision model.
65                  *      \param          create          [in] model creation structure
66                  *      \return         true if success
67                  */
68                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69                 override(BaseModel)     bool                                    Build(const OPCODECREATE& create);
70
71                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72                 /**
73                  *      Gets the number of bytes used by the tree.
74                  *      \return         amount of bytes used
75                  */
76                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77                 override(BaseModel)     udword                                  GetUsedBytes()          const;
78
79                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80                 /**
81                  *      Refits the collision model. This can be used to handle dynamic meshes. Usage is:
82                  *      1. modify your mesh vertices (keep the topology constant!)
83                  *      2. refit the tree (call this method)
84                  *      \return         true if success
85                  */
86                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87                 override(BaseModel)     bool                                    Refit();
88
89                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90                 /**
91                  *      Gets array of triangles.
92                  *      \return         array of triangles
93                  */
94                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
95                 inline_                         const LeafTriangles*    GetLeafTriangles()      const   { return mTriangles;    }
96
97                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98                 /**
99                  *      Gets array of indices.
100                  *      \return         array of indices
101                  */
102                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103                 inline_                         const udword*                   GetIndices()            const   { return mIndices;              }
104
105                 private:
106                                                         udword                                  mNbLeaves;              //!< Number of leaf nodes in the model
107                                                         LeafTriangles*                  mTriangles;             //!< Array of mNbLeaves leaf descriptors
108                                                         udword                                  mNbPrimitives;  //!< Number of primitives in the model
109                                                         udword*                                 mIndices;               //!< Array of primitive indices
110
111                 // Internal methods
112                                                         void                                    Release();
113         };
114
115 #endif // __OPC_HYBRIDMODEL_H__