OPC_MeshInterface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains a mesh interface.
  11. * \file OPC_MeshInterface.h
  12. * \author Pierre Terdiman
  13. * \date November, 27, 2002
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_MESHINTERFACE_H__
  19. #define __OPC_MESHINTERFACE_H__
  20. struct VertexPointers
  21. {
  22. const Point* Vertex[3];
  23. bool BackfaceCulling(const Point& source)
  24. {
  25. const Point& p0 = *Vertex[0];
  26. const Point& p1 = *Vertex[1];
  27. const Point& p2 = *Vertex[2];
  28. // Compute normal direction
  29. Point Normal = (p2 - p1)^(p0 - p1);
  30. // Backface culling
  31. return (Normal | (source - p0)) >= 0.0f;
  32. }
  33. };
  34. typedef Point ConversionArea[3];
  35. #ifdef OPC_USE_CALLBACKS
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. /**
  38. * User-callback, called by OPCODE to request vertices from the app.
  39. * \param triangle_index [in] face index for which the system is requesting the vertices
  40. * \param triangle [out] triangle's vertices (must be provided by the user)
  41. * \param user_data [in] user-defined data from SetCallback()
  42. */
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. typedef void (*RequestCallback) (udword triangle_index, VertexPointers& triangle, void* user_data);
  45. #endif
  46. class OPCODE_API MeshInterface
  47. {
  48. public:
  49. // Constructor / Destructor
  50. MeshInterface();
  51. ~MeshInterface();
  52. // Common settings
  53. inline_ udword GetNbTriangles() const { return mNbTris; }
  54. inline_ udword GetNbVertices() const { return mNbVerts; }
  55. inline_ void SetNbTriangles(udword nb) { mNbTris = nb; }
  56. inline_ void SetNbVertices(udword nb) { mNbVerts = nb; }
  57. #ifdef OPC_USE_CALLBACKS
  58. // Callback settings
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. /**
  61. * Callback control: setups object callback. Must provide triangle-vertices for a given triangle index.
  62. * \param callback [in] user-defined callback
  63. * \param user_data [in] user-defined data
  64. * \return true if success
  65. */
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. bool SetCallback(RequestCallback callback, void* user_data);
  68. inline_ void* GetUserData() const { return mUserData; }
  69. inline_ RequestCallback GetCallback() const { return mObjCallback; }
  70. #else
  71. // Pointers settings
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. /**
  74. * Pointers control: setups object pointers. Must provide access to faces and vertices for a given object.
  75. * \param tris [in] pointer to triangles
  76. * \param verts [in] pointer to vertices
  77. * \return true if success
  78. */
  79. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. bool SetPointers(const IndexedTriangle* tris, const Point* verts);
  81. inline_ const IndexedTriangle* GetTris() const { return mTris; }
  82. inline_ const Point* GetVerts() const { return mVerts; }
  83. #ifdef OPC_USE_STRIDE
  84. // Strides settings
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. /**
  87. * Strides control
  88. * \param tri_stride [in] size of a triangle in bytes. The first sizeof(IndexedTriangle) bytes are used to get vertex indices.
  89. * \param vertex_stride [in] size of a vertex in bytes. The first sizeof(Point) bytes are used to get vertex position.
  90. * \return true if success
  91. */
  92. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. bool SetStrides(udword tri_stride=sizeof(IndexedTriangle), udword vertex_stride=sizeof(Point));
  94. inline_ udword GetTriStride() const { return mTriStride; }
  95. inline_ udword GetVertexStride() const { return mVertexStride; }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. /**
  98. * Single/Double control
  99. * \param value [in] Indicates if mesh data is provided as array of \c single values. If \c false, data is expected to contain \c double elements.
  100. */
  101. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. inline_ void SetSingle(bool value) { mFetchTriangle = (value ? &MeshInterface::FetchTriangleFromSingles : &MeshInterface::FetchTriangleFromDoubles); }
  103. #endif
  104. #endif
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. /**
  107. * Fetches a triangle given a triangle index.
  108. * \param vp [out] required triangle's vertex pointers
  109. * \param index [in] triangle index
  110. * \param vc [in,out] storage required for data conversion (pass local variable with same scope as \a vp, as \a vp may point to this memory on return)
  111. */
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. inline_ void GetTriangle(VertexPointers& vp, udword index, ConversionArea vc) const
  114. {
  115. #ifdef OPC_USE_CALLBACKS
  116. (mObjCallback)(index, vp, mUserData);
  117. #else
  118. #ifdef OPC_USE_STRIDE
  119. // Since there was conditional statement "if (Single)" which was unpredictable for compiler
  120. // and required both branches to be always generated what made inlining a questionable
  121. // benefit, I consider it better to introduce a forced call
  122. // but get rig of branching and dead code injection.
  123. ((*this).*mFetchTriangle)(vp, index, vc);
  124. #else
  125. const IndexedTriangle* T = &mTris[index];
  126. vp.Vertex[0] = &mVerts[T->mVRef[0]];
  127. vp.Vertex[1] = &mVerts[T->mVRef[1]];
  128. vp.Vertex[2] = &mVerts[T->mVRef[2]];
  129. #endif
  130. #endif
  131. }
  132. private:
  133. #ifndef OPC_USE_CALLBACKS
  134. #ifdef OPC_USE_STRIDE
  135. void FetchTriangleFromSingles(VertexPointers& vp, udword index, ConversionArea vc) const;
  136. void FetchTriangleFromDoubles(VertexPointers& vp, udword index, ConversionArea vc) const;
  137. #endif
  138. #endif
  139. public:
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. /**
  142. * Remaps client's mesh according to a permutation.
  143. * \param nb_indices [in] number of indices in the permutation (will be checked against number of triangles)
  144. * \param permutation [in] list of triangle indices
  145. * \return true if success
  146. */
  147. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  148. bool RemapClient(udword nb_indices, const dTriIndex* permutation) const;
  149. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  150. /**
  151. * Checks the mesh interface is valid, i.e. things have been setup correctly.
  152. * \return true if valid
  153. */
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. bool IsValid() const;
  156. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. /**
  158. * Checks the mesh itself is valid.
  159. * Currently we only look for degenerate faces.
  160. * \return number of degenerate faces
  161. */
  162. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. udword CheckTopology() const;
  164. private:
  165. udword mNbTris; //!< Number of triangles in the input model
  166. udword mNbVerts; //!< Number of vertices in the input model
  167. #ifdef OPC_USE_CALLBACKS
  168. // User callback
  169. void* mUserData; //!< User-defined data sent to callback
  170. RequestCallback mObjCallback; //!< Object callback
  171. #else
  172. // User pointers
  173. #ifdef OPC_USE_STRIDE
  174. udword mTriStride; //!< Possible triangle stride in bytes [Opcode 1.3]
  175. udword mVertexStride; //!< Possible vertex stride in bytes [Opcode 1.3]
  176. typedef void (MeshInterface:: *TriangleFetchProc)(VertexPointers& vp, udword index, ConversionArea vc) const;
  177. TriangleFetchProc mFetchTriangle;
  178. #endif
  179. const IndexedTriangle* mTris; //!< Array of indexed triangles
  180. const Point* mVerts; //!< Array of vertices
  181. #endif
  182. };
  183. #endif //__OPC_MESHINTERFACE_H__