Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_MeshInterface.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 a mesh interface.
21  *      \file           OPC_MeshInterface.h
22  *      \author         Pierre Terdiman
23  *      \date           November, 27, 2002
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 // Include Guard
29 #ifndef __OPC_MESHINTERFACE_H__
30 #define __OPC_MESHINTERFACE_H__
31
32         struct VertexPointers
33         {
34                 const Point*    Vertex[3];
35
36                 bool BackfaceCulling(const Point& source)
37                 {
38                         const Point& p0 = *Vertex[0];
39                         const Point& p1 = *Vertex[1];
40                         const Point& p2 = *Vertex[2];
41
42                         // Compute normal direction
43                         Point Normal = (p2 - p1)^(p0 - p1);
44
45                         // Backface culling
46                         return (Normal | (source - p0)) >= 0.0f;
47                 }
48         };
49
50 #ifdef OPC_USE_CALLBACKS
51         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52         /**
53          *      User-callback, called by OPCODE to request vertices from the app.
54          *      \param          triangle_index  [in] face index for which the system is requesting the vertices
55          *      \param          triangle                [out] triangle's vertices (must be provided by the user)
56          *      \param          user_data               [in] user-defined data from SetCallback()
57          */
58         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59         typedef void    (*RequestCallback)      (udword triangle_index, VertexPointers& triangle, void* user_data);
60 #endif
61
62         class OPCODE_API MeshInterface
63         {
64                 public:
65                 // Constructor / Destructor
66                                                                                         MeshInterface();
67                                                                                         ~MeshInterface();
68                 // Common settings
69                 inline_                 udword                          GetNbTriangles()        const   { return mNbTris;       }
70                 inline_                 udword                          GetNbVertices()         const   { return mNbVerts;      }
71                 inline_                 void                            SetNbTriangles(udword nb)       { mNbTris = nb;         }
72                 inline_                 void                            SetNbVertices(udword nb)        { mNbVerts = nb;        }
73
74 #ifdef OPC_USE_CALLBACKS
75                 // Callback settings
76
77                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78                 /**
79                  *      Callback control: setups object callback. Must provide triangle-vertices for a given triangle index.
80                  *      \param          callback        [in] user-defined callback
81                  *      \param          user_data       [in] user-defined data
82                  *      \return         true if success
83                  */
84                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85                                                 bool                            SetCallback(RequestCallback callback, void* user_data);
86                 inline_                 void*                           GetUserData()           const   { return mUserData;             }
87                 inline_                 RequestCallback         GetCallback()           const   { return mObjCallback;  }
88 #else
89                 // Pointers settings
90
91                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92                 /**
93                  *      Pointers control: setups object pointers. Must provide access to faces and vertices for a given object.
94                  *      \param          tris    [in] pointer to triangles
95                  *      \param          verts   [in] pointer to vertices
96                  *      \return         true if success
97                  */
98                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99                                                 bool                            SetPointers(const IndexedTriangle* tris, const Point* verts);
100                 inline_ const   IndexedTriangle*        GetTris()                       const   { return mTris;                 }
101                 inline_ const   Point*                          GetVerts()                      const   { return mVerts;                }
102
103         #ifdef OPC_USE_STRIDE
104                 // Strides settings
105
106                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107                 /**
108                  *      Strides control
109                  *      \param          tri_stride              [in] size of a triangle in bytes. The first sizeof(IndexedTriangle) bytes are used to get vertex indices.
110                  *      \param          vertex_stride   [in] size of a vertex in bytes. The first sizeof(Point) bytes are used to get vertex position.
111                  *      \return         true if success
112                  */
113                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114                                                 bool                            SetStrides(udword tri_stride=sizeof(IndexedTriangle), udword vertex_stride=sizeof(Point));
115                 inline_                 udword                          GetTriStride()          const   { return mTriStride;    }
116                 inline_                 udword                          GetVertexStride()       const   { return mVertexStride; }
117         #endif
118 #endif
119
120                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121                 /**
122                  *      Fetches a triangle given a triangle index.
123                  *      \param          vp              [out] required triangle's vertex pointers
124                  *      \param          index   [in] triangle index
125                  */
126                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127                 inline_                 void                            GetTriangle(VertexPointers& vp, udword index)   const
128                                                                                         {
129 #ifdef OPC_USE_CALLBACKS
130                                                                                                 (mObjCallback)(index, vp, mUserData);
131 #else
132         #ifdef OPC_USE_STRIDE
133                                                                                                 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
134                                                                                                 vp.Vertex[0] = (const Point*)(((ubyte*)mVerts) + T->mVRef[0] * mVertexStride);
135                                                                                                 vp.Vertex[1] = (const Point*)(((ubyte*)mVerts) + T->mVRef[1] * mVertexStride);
136                                                                                                 vp.Vertex[2] = (const Point*)(((ubyte*)mVerts) + T->mVRef[2] * mVertexStride);
137         #else
138                                                                                                 const IndexedTriangle* T = &mTris[index];
139                                                                                                 vp.Vertex[0] = &mVerts[T->mVRef[0]];
140                                                                                                 vp.Vertex[1] = &mVerts[T->mVRef[1]];
141                                                                                                 vp.Vertex[2] = &mVerts[T->mVRef[2]];
142         #endif
143 #endif
144                                                                                         }
145
146                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147                 /**
148                  *      Remaps client's mesh according to a permutation.
149                  *      \param          nb_indices      [in] number of indices in the permutation (will be checked against number of triangles)
150                  *      \param          permutation     [in] list of triangle indices
151                  *      \return         true if success
152                  */
153                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154                 inline_                 bool                            RemapClient(udword nb_indices, const udword* permutation)       const;
155
156                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157                 /**
158                  *      Checks the mesh interface is valid, i.e. things have been setup correctly.
159                  *      \return         true if valid
160                  */
161                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162                                                 bool                            IsValid()               const;
163
164                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165                 /**
166                  *      Checks the mesh itself is valid.
167                  *      Currently we only look for degenerate faces.
168                  *      \return         number of degenerate faces
169                  */
170                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
171                                                 udword                          CheckTopology() const;
172                 private:
173
174                                                 udword                          mNbTris;                        //!< Number of triangles in the input model
175                                                 udword                          mNbVerts;                       //!< Number of vertices in the input model
176 #ifdef OPC_USE_CALLBACKS
177                 // User callback
178                                                 void*                           mUserData;                      //!< User-defined data sent to callback
179                                                 RequestCallback         mObjCallback;           //!< Object callback
180 #else
181                 // User pointers
182                                 const   IndexedTriangle*        mTris;                          //!< Array of indexed triangles
183                                 const   Point*                          mVerts;                         //!< Array of vertices
184         #ifdef OPC_USE_STRIDE
185                                                 udword                          mTriStride;                     //!< Possible triangle stride in bytes [Opcode 1.3]
186                                                 udword                          mVertexStride;          //!< Possible vertex stride in bytes [Opcode 1.3]
187         #endif
188 #endif
189         };
190
191 #endif //__OPC_MESHINTERFACE_H__