collision_trimesh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. typedef enum
  43. {
  44. dMTV__MIN,
  45. dMTV_FIRST = dMTV__MIN,
  46. dMTV_SECOND,
  47. dMTV_THIRD,
  48. dMTV__MAX,
  49. } dMeshTriangleVertex;
  50. /*
  51. * These don't make much sense now, but they will later when we add more
  52. * features.
  53. */
  54. ODE_API dTriMeshDataID dGeomTriMeshDataCreate(void);
  55. ODE_API void dGeomTriMeshDataDestroy(dTriMeshDataID g);
  56. /*
  57. * The values of data_id that can be used with dGeomTriMeshDataSet/dGeomTriMeshDataGet
  58. */
  59. enum
  60. {
  61. dTRIMESHDATA__MIN,
  62. dTRIMESHDATA_FACE_NORMALS = dTRIMESHDATA__MIN,
  63. dTRIMESHDATA_USE_FLAGS,
  64. dTRIMESHDATA__MAX,
  65. #ifndef TRIMESH_FACE_NORMALS // Define this name during the header inclusion if you need it for something else
  66. // Included for backward compatibility -- please use the corrected name above. Sorry.
  67. TRIMESH_FACE_NORMALS = dTRIMESHDATA_FACE_NORMALS,
  68. #endif
  69. };
  70. /*
  71. * The flags of the dTRIMESHDATA_USE_FLAGS data elements
  72. */
  73. enum
  74. {
  75. dMESHDATAUSE_EDGE1 = 0x01,
  76. dMESHDATAUSE_EDGE2 = 0x02,
  77. dMESHDATAUSE_EDGE3 = 0x04,
  78. dMESHDATAUSE_VERTEX1 = 0x08,
  79. dMESHDATAUSE_VERTEX2 = 0x10,
  80. dMESHDATAUSE_VERTEX3 = 0x20,
  81. };
  82. /*
  83. * Set and get the TriMeshData additional data
  84. * Note: The data is NOT COPIED on assignment
  85. */
  86. ODE_API void dGeomTriMeshDataSet(dTriMeshDataID g, int data_id, void *in_data);
  87. ODE_API void *dGeomTriMeshDataGet(dTriMeshDataID g, int data_id);
  88. ODE_API void *dGeomTriMeshDataGet2(dTriMeshDataID g, int data_id, size_t *pout_size/*=NULL*/);
  89. /**
  90. * We need to set the last transform after each time step for
  91. * accurate collision response. These functions get and set that transform.
  92. * It is stored per geom instance, rather than per dTriMeshDataID.
  93. */
  94. ODE_API void dGeomTriMeshSetLastTransform( dGeomID g, const dMatrix4 last_trans );
  95. ODE_API const dReal* dGeomTriMeshGetLastTransform( dGeomID g );
  96. /*
  97. * Build a TriMesh data object with single precision vertex data.
  98. */
  99. ODE_API void dGeomTriMeshDataBuildSingle(dTriMeshDataID g,
  100. const void* Vertices, int VertexStride, int VertexCount,
  101. const void* Indices, int IndexCount, int TriStride);
  102. /* same again with a normals array (used as trimesh-trimesh optimization) */
  103. ODE_API void dGeomTriMeshDataBuildSingle1(dTriMeshDataID g,
  104. const void* Vertices, int VertexStride, int VertexCount,
  105. const void* Indices, int IndexCount, int TriStride,
  106. const void* Normals);
  107. /*
  108. * Build a TriMesh data object with double precision vertex data.
  109. */
  110. ODE_API void dGeomTriMeshDataBuildDouble(dTriMeshDataID g,
  111. const void* Vertices, int VertexStride, int VertexCount,
  112. const void* Indices, int IndexCount, int TriStride);
  113. /* same again with a normals array (used as trimesh-trimesh optimization) */
  114. ODE_API void dGeomTriMeshDataBuildDouble1(dTriMeshDataID g,
  115. const void* Vertices, int VertexStride, int VertexCount,
  116. const void* Indices, int IndexCount, int TriStride,
  117. const void* Normals);
  118. /*
  119. * Simple build. Single/double precision based on dSINGLE/dDOUBLE!
  120. */
  121. ODE_API void dGeomTriMeshDataBuildSimple(dTriMeshDataID g,
  122. const dReal* Vertices, int VertexCount,
  123. const dTriIndex* Indices, int IndexCount);
  124. /* same again with a normals array (used as trimesh-trimesh optimization) */
  125. ODE_API void dGeomTriMeshDataBuildSimple1(dTriMeshDataID g,
  126. const dReal* Vertices, int VertexCount,
  127. const dTriIndex* Indices, int IndexCount,
  128. const int* Normals);
  129. /*
  130. * Data preprocessing build request flags.
  131. */
  132. enum
  133. {
  134. dTRIDATAPREPROCESS_BUILD__MIN,
  135. dTRIDATAPREPROCESS_BUILD_CONCAVE_EDGES = dTRIDATAPREPROCESS_BUILD__MIN, // Used to optimize OPCODE trimesh-capsule collisions; allocates 1 byte per triangle; no extra data associated
  136. dTRIDATAPREPROCESS_BUILD_FACE_ANGLES, // Used to aid trimesh-convex collisions; memory requirements depend on extra data
  137. dTRIDATAPREPROCESS_BUILD__MAX,
  138. };
  139. /*
  140. * Data preprocessing extra values for dTRIDATAPREPROCESS_BUILD_FACE_ANGLES.
  141. */
  142. enum
  143. {
  144. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__MIN,
  145. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA_BYTE_POSITIVE = dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__MIN, // Build angles for convex edges only and store as bytes; allocates 3 bytes per triangle; stores angles (0..180] in 1/254 fractions leaving two values for the flat and all the concaves
  146. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA_BYTE_ALL, // Build angles for all the edges and store in bytes; allocates 3 bytes per triangle; stores angles [-180..0) and (0..180] in 1/127 fractions plus a value for the flat angle
  147. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA_WORD_ALL, // Build angles for all the edges and store in words; allocates 6 bytes per triangle; stores angles [-180..0) and (0..180] in 1/32767 fractions plus a value for the flat angle
  148. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__MAX,
  149. dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__DEFAULT = dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA_BYTE_POSITIVE, // The default value assumed if the extra data is not provided
  150. };
  151. /*
  152. * Pre-process the trimesh data according to the request flags.
  153. *
  154. * buildRequestFlags is a bitmask of 1U << dTRIDATAPREPROCESS_BUILD_...
  155. * It is allowed to call the function multiple times provided the bitmasks are different each time.
  156. *
  157. * requestExtraData is an optional pointer to array of extra parameters per bitmask bits
  158. * (only the elements indexed by positions of raised bits are examined;
  159. * defaults are assumed if the pointer is NULL)
  160. *
  161. * The function returns a boolean status the only failure reason being insufficient memory.
  162. */
  163. ODE_API int dGeomTriMeshDataPreprocess2(dTriMeshDataID g, unsigned int buildRequestFlags, const dintptr *requestExtraData/*=NULL | const dintptr (*)[dTRIDATAPREPROCESS_BUILD__MAX]*/);
  164. /*
  165. * Obsolete. Equivalent to calling dGeomTriMeshDataPreprocess2(g, (1U << dTRIDATAPREPROCESS_BUILD_CONCAVE_EDGES), NULL)
  166. */
  167. ODE_API int dGeomTriMeshDataPreprocess(dTriMeshDataID g);
  168. /*
  169. * Get and set the internal preprocessed trimesh data buffer (see the enumerated type above), for loading and saving
  170. * These functions are deprecated. Use dGeomTriMeshDataSet/dGeomTriMeshDataGet2 with dTRIMESHDATA_USE_FLAGS instead.
  171. */
  172. ODE_API_DEPRECATED ODE_API void dGeomTriMeshDataGetBuffer(dTriMeshDataID g, unsigned char** buf, int* bufLen);
  173. ODE_API_DEPRECATED ODE_API void dGeomTriMeshDataSetBuffer(dTriMeshDataID g, unsigned char* buf);
  174. /*
  175. * Per triangle callback. Allows the user to say if he wants a collision with
  176. * a particular triangle.
  177. */
  178. typedef int dTriCallback(dGeomID TriMesh, dGeomID RefObject, int TriangleIndex);
  179. ODE_API void dGeomTriMeshSetCallback(dGeomID g, dTriCallback* Callback);
  180. ODE_API dTriCallback* dGeomTriMeshGetCallback(dGeomID g);
  181. /*
  182. * Per object callback. Allows the user to get the list of triangles in 1
  183. * shot. Maybe we should remove this one.
  184. */
  185. typedef void dTriArrayCallback(dGeomID TriMesh, dGeomID RefObject, const int* TriIndices, int TriCount);
  186. ODE_API void dGeomTriMeshSetArrayCallback(dGeomID g, dTriArrayCallback* ArrayCallback);
  187. ODE_API dTriArrayCallback* dGeomTriMeshGetArrayCallback(dGeomID g);
  188. /*
  189. * Ray callback.
  190. * Allows the user to say if a ray collides with a triangle on barycentric
  191. * coords. The user can for example sample a texture with alpha transparency
  192. * to determine if a collision should occur.
  193. */
  194. typedef int dTriRayCallback(dGeomID TriMesh, dGeomID Ray, int TriangleIndex, dReal u, dReal v);
  195. ODE_API void dGeomTriMeshSetRayCallback(dGeomID g, dTriRayCallback* Callback);
  196. ODE_API dTriRayCallback* dGeomTriMeshGetRayCallback(dGeomID g);
  197. /*
  198. * Triangle merging callback.
  199. * Allows the user to generate a fake triangle index for a new contact generated
  200. * from merging of two other contacts. That index could later be used by the
  201. * user to determine attributes of original triangles used as sources for a
  202. * merged contact.
  203. */
  204. typedef int dTriTriMergeCallback(dGeomID TriMesh, int FirstTriangleIndex, int SecondTriangleIndex);
  205. ODE_API void dGeomTriMeshSetTriMergeCallback(dGeomID g, dTriTriMergeCallback* Callback);
  206. ODE_API dTriTriMergeCallback* dGeomTriMeshGetTriMergeCallback(dGeomID g);
  207. /*
  208. * Trimesh class
  209. * Construction. Callbacks are optional.
  210. */
  211. ODE_API dGeomID dCreateTriMesh(dSpaceID space, dTriMeshDataID Data, dTriCallback* Callback, dTriArrayCallback* ArrayCallback, dTriRayCallback* RayCallback);
  212. ODE_API void dGeomTriMeshSetData(dGeomID g, dTriMeshDataID Data);
  213. ODE_API dTriMeshDataID dGeomTriMeshGetData(dGeomID g);
  214. /* enable/disable/check temporal coherence*/
  215. ODE_API void dGeomTriMeshEnableTC(dGeomID g, int geomClass, int enable);
  216. ODE_API int dGeomTriMeshIsTCEnabled(dGeomID g, int geomClass);
  217. /*
  218. * Clears the internal temporal coherence caches. When a geom has its
  219. * collision checked with a trimesh once, data is stored inside the trimesh.
  220. * With large worlds with lots of seperate objects this list could get huge.
  221. * We should be able to do this automagically.
  222. */
  223. ODE_API void dGeomTriMeshClearTCCache(dGeomID g);
  224. /*
  225. * returns the TriMeshDataID
  226. */
  227. ODE_API dTriMeshDataID dGeomTriMeshGetTriMeshDataID(dGeomID g);
  228. /*
  229. * Gets a triangle.
  230. */
  231. ODE_API void dGeomTriMeshGetTriangle(dGeomID g, int Index, dVector3* v0, dVector3* v1, dVector3* v2);
  232. /*
  233. * Gets the point on the requested triangle and the given barycentric
  234. * coordinates.
  235. */
  236. ODE_API void dGeomTriMeshGetPoint(dGeomID g, int Index, dReal u, dReal v, dVector3 Out);
  237. /*
  238. This is how the strided data works:
  239. struct StridedVertex{
  240. dVector3 Vertex;
  241. // Userdata
  242. };
  243. int VertexStride = sizeof(StridedVertex);
  244. struct StridedTri{
  245. int Indices[3];
  246. // Userdata
  247. };
  248. int TriStride = sizeof(StridedTri);
  249. */
  250. ODE_API int dGeomTriMeshGetTriangleCount (dGeomID g);
  251. ODE_API void dGeomTriMeshDataUpdate(dTriMeshDataID g);
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif /* _ODE_COLLISION_TRIMESH_H_ */