collision_trimesh.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. /*
  23. * TriMesh code by Erwin de Vries.
  24. *
  25. * Trimesh data.
  26. * This is where the actual vertexdata (pointers), and BV tree is stored.
  27. * Vertices should be single precision!
  28. * This should be more sophisticated, so that the user can easyly implement
  29. * another collision library, but this is a lot of work, and also costs some
  30. * performance because some data has to be copied.
  31. */
  32. #ifndef _ODE_COLLISION_TRIMESH_H_
  33. #define _ODE_COLLISION_TRIMESH_H_
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /*
  38. * Data storage for triangle meshes.
  39. */
  40. struct dxTriMeshData;
  41. typedef struct dxTriMeshData* dTriMeshDataID;
  42. /*
  43. * These dont make much sense now, but they will later when we add more
  44. * features.
  45. */
  46. ODE_API dTriMeshDataID dGeomTriMeshDataCreate(void);
  47. ODE_API void dGeomTriMeshDataDestroy(dTriMeshDataID g);
  48. enum { TRIMESH_FACE_NORMALS };
  49. ODE_API void dGeomTriMeshDataSet(dTriMeshDataID g, int data_id, void* in_data);
  50. ODE_API void* dGeomTriMeshDataGet(dTriMeshDataID g, int data_id);
  51. /**
  52. * We need to set the last transform after each time step for
  53. * accurate collision response. These functions get and set that transform.
  54. * It is stored per geom instance, rather than per dTriMeshDataID.
  55. */
  56. ODE_API void dGeomTriMeshSetLastTransform( dGeomID g, dMatrix4 last_trans );
  57. ODE_API dReal* dGeomTriMeshGetLastTransform( dGeomID g );
  58. /*
  59. * Build a TriMesh data object with single precision vertex data.
  60. */
  61. ODE_API void dGeomTriMeshDataBuildSingle(dTriMeshDataID g,
  62. const void* Vertices, int VertexStride, int VertexCount,
  63. const void* Indices, int IndexCount, int TriStride);
  64. /* same again with a normals array (used as trimesh-trimesh optimization) */
  65. ODE_API void dGeomTriMeshDataBuildSingle1(dTriMeshDataID g,
  66. const void* Vertices, int VertexStride, int VertexCount,
  67. const void* Indices, int IndexCount, int TriStride,
  68. const void* Normals);
  69. /*
  70. * Build a TriMesh data object with double precision vertex data.
  71. */
  72. ODE_API void dGeomTriMeshDataBuildDouble(dTriMeshDataID g,
  73. const void* Vertices, int VertexStride, int VertexCount,
  74. const void* Indices, int IndexCount, int TriStride);
  75. /* same again with a normals array (used as trimesh-trimesh optimization) */
  76. ODE_API void dGeomTriMeshDataBuildDouble1(dTriMeshDataID g,
  77. const void* Vertices, int VertexStride, int VertexCount,
  78. const void* Indices, int IndexCount, int TriStride,
  79. const void* Normals);
  80. /*
  81. * Simple build. Single/double precision based on dSINGLE/dDOUBLE!
  82. */
  83. ODE_API void dGeomTriMeshDataBuildSimple(dTriMeshDataID g,
  84. const dReal* Vertices, int VertexCount,
  85. const dTriIndex* Indices, int IndexCount);
  86. /* same again with a normals array (used as trimesh-trimesh optimization) */
  87. ODE_API void dGeomTriMeshDataBuildSimple1(dTriMeshDataID g,
  88. const dReal* Vertices, int VertexCount,
  89. const dTriIndex* Indices, int IndexCount,
  90. const int* Normals);
  91. /* Preprocess the trimesh data to remove mark unnecessary edges and vertices */
  92. ODE_API void dGeomTriMeshDataPreprocess(dTriMeshDataID g);
  93. /* Get and set the internal preprocessed trimesh data buffer, for loading and saving */
  94. ODE_API void dGeomTriMeshDataGetBuffer(dTriMeshDataID g, unsigned char** buf, int* bufLen);
  95. ODE_API void dGeomTriMeshDataSetBuffer(dTriMeshDataID g, unsigned char* buf);
  96. /*
  97. * Per triangle callback. Allows the user to say if he wants a collision with
  98. * a particular triangle.
  99. */
  100. typedef int dTriCallback(dGeomID TriMesh, dGeomID RefObject, int TriangleIndex);
  101. ODE_API void dGeomTriMeshSetCallback(dGeomID g, dTriCallback* Callback);
  102. ODE_API dTriCallback* dGeomTriMeshGetCallback(dGeomID g);
  103. /*
  104. * Per object callback. Allows the user to get the list of triangles in 1
  105. * shot. Maybe we should remove this one.
  106. */
  107. typedef void dTriArrayCallback(dGeomID TriMesh, dGeomID RefObject, const int* TriIndices, int TriCount);
  108. ODE_API void dGeomTriMeshSetArrayCallback(dGeomID g, dTriArrayCallback* ArrayCallback);
  109. ODE_API dTriArrayCallback* dGeomTriMeshGetArrayCallback(dGeomID g);
  110. /*
  111. * Ray callback.
  112. * Allows the user to say if a ray collides with a triangle on barycentric
  113. * coords. The user can for example sample a texture with alpha transparency
  114. * to determine if a collision should occur.
  115. */
  116. typedef int dTriRayCallback(dGeomID TriMesh, dGeomID Ray, int TriangleIndex, dReal u, dReal v);
  117. ODE_API void dGeomTriMeshSetRayCallback(dGeomID g, dTriRayCallback* Callback);
  118. ODE_API dTriRayCallback* dGeomTriMeshGetRayCallback(dGeomID g);
  119. /*
  120. * Triangle merging callback.
  121. * Allows the user to generate a fake triangle index for a new contact generated
  122. * from merging of two other contacts. That index could later be used by the
  123. * user to determine attributes of original triangles used as sources for a
  124. * merged contact.
  125. */
  126. typedef int dTriTriMergeCallback(dGeomID TriMesh, int FirstTriangleIndex, int SecondTriangleIndex);
  127. ODE_API void dGeomTriMeshSetTriMergeCallback(dGeomID g, dTriTriMergeCallback* Callback);
  128. ODE_API dTriTriMergeCallback* dGeomTriMeshGetTriMergeCallback(dGeomID g);
  129. /*
  130. * Trimesh class
  131. * Construction. Callbacks are optional.
  132. */
  133. ODE_API dGeomID dCreateTriMesh(dSpaceID space, dTriMeshDataID Data, dTriCallback* Callback, dTriArrayCallback* ArrayCallback, dTriRayCallback* RayCallback);
  134. ODE_API void dGeomTriMeshSetData(dGeomID g, dTriMeshDataID Data);
  135. ODE_API dTriMeshDataID dGeomTriMeshGetData(dGeomID g);
  136. /* enable/disable/check temporal coherence*/
  137. ODE_API void dGeomTriMeshEnableTC(dGeomID g, int geomClass, int enable);
  138. ODE_API int dGeomTriMeshIsTCEnabled(dGeomID g, int geomClass);
  139. /*
  140. * Clears the internal temporal coherence caches. When a geom has its
  141. * collision checked with a trimesh once, data is stored inside the trimesh.
  142. * With large worlds with lots of seperate objects this list could get huge.
  143. * We should be able to do this automagically.
  144. */
  145. ODE_API void dGeomTriMeshClearTCCache(dGeomID g);
  146. /*
  147. * returns the TriMeshDataID
  148. */
  149. ODE_API dTriMeshDataID dGeomTriMeshGetTriMeshDataID(dGeomID g);
  150. /*
  151. * Gets a triangle.
  152. */
  153. ODE_API void dGeomTriMeshGetTriangle(dGeomID g, int Index, dVector3* v0, dVector3* v1, dVector3* v2);
  154. /*
  155. * Gets the point on the requested triangle and the given barycentric
  156. * coordinates.
  157. */
  158. ODE_API void dGeomTriMeshGetPoint(dGeomID g, int Index, dReal u, dReal v, dVector3 Out);
  159. /*
  160. This is how the strided data works:
  161. struct StridedVertex{
  162. dVector3 Vertex;
  163. // Userdata
  164. };
  165. int VertexStride = sizeof(StridedVertex);
  166. struct StridedTri{
  167. int Indices[3];
  168. // Userdata
  169. };
  170. int TriStride = sizeof(StridedTri);
  171. */
  172. ODE_API int dGeomTriMeshGetTriangleCount (dGeomID g);
  173. ODE_API void dGeomTriMeshDataUpdate(dTriMeshDataID g);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif /* _ODE_COLLISION_TRIMESH_H_ */