1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
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 *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
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. *
21 *************************************************************************/
24 * TriMesh code by Erwin de Vries.
27 * This is where the actual vertexdata (pointers), and BV tree is stored.
28 * Vertices should be single precision!
29 * This should be more sophisticated, so that the user can easyly implement
30 * another collision library, but this is a lot of work, and also costs some
31 * performance because some data has to be copied.
34 #ifndef _ODE_COLLISION_TRIMESH_H_
35 #define _ODE_COLLISION_TRIMESH_H_
42 * Data storage for triangle meshes.
45 typedef struct dxTriMeshData* dTriMeshDataID;
52 dMTV_FIRST = dMTV__MIN,
58 } dMeshTriangleVertex;
61 * These don't make much sense now, but they will later when we add more
64 ODE_API dTriMeshDataID dGeomTriMeshDataCreate(void);
65 ODE_API void dGeomTriMeshDataDestroy(dTriMeshDataID g);
69 * The values of data_id that can be used with dGeomTriMeshDataSet/dGeomTriMeshDataGet
75 dTRIMESHDATA_FACE_NORMALS = dTRIMESHDATA__MIN,
76 dTRIMESHDATA_USE_FLAGS,
80 #ifndef TRIMESH_FACE_NORMALS // Define this name during the header inclusion if you need it for something else
81 // Included for backward compatibility -- please use the corrected name above. Sorry.
82 TRIMESH_FACE_NORMALS = dTRIMESHDATA_FACE_NORMALS,
87 * The flags of the dTRIMESHDATA_USE_FLAGS data elements
91 dMESHDATAUSE_EDGE1 = 0x01,
92 dMESHDATAUSE_EDGE2 = 0x02,
93 dMESHDATAUSE_EDGE3 = 0x04,
94 dMESHDATAUSE_VERTEX1 = 0x08,
95 dMESHDATAUSE_VERTEX2 = 0x10,
96 dMESHDATAUSE_VERTEX3 = 0x20,
100 * Set and get the TriMeshData additional data
101 * Note: The data is NOT COPIED on assignment
103 ODE_API void dGeomTriMeshDataSet(dTriMeshDataID g, int data_id, void *in_data);
104 ODE_API void *dGeomTriMeshDataGet(dTriMeshDataID g, int data_id);
105 ODE_API void *dGeomTriMeshDataGet2(dTriMeshDataID g, int data_id, dsizeint *pout_size/*=NULL*/);
110 * We need to set the last transform after each time step for
111 * accurate collision response. These functions get and set that transform.
112 * It is stored per geom instance, rather than per dTriMeshDataID.
114 ODE_API void dGeomTriMeshSetLastTransform( dGeomID g, const dMatrix4 last_trans );
115 ODE_API const dReal* dGeomTriMeshGetLastTransform( dGeomID g );
118 * Build a TriMesh data object with single precision vertex data.
120 ODE_API void dGeomTriMeshDataBuildSingle(dTriMeshDataID g,
121 const void* Vertices, int VertexStride, int VertexCount,
122 const void* Indices, int IndexCount, int TriStride);
123 /* same again with a normals array (used as trimesh-trimesh optimization) */
124 ODE_API void dGeomTriMeshDataBuildSingle1(dTriMeshDataID g,
125 const void* Vertices, int VertexStride, int VertexCount,
126 const void* Indices, int IndexCount, int TriStride,
127 const void* Normals);
129 * Build a TriMesh data object with double precision vertex data.
131 ODE_API void dGeomTriMeshDataBuildDouble(dTriMeshDataID g,
132 const void* Vertices, int VertexStride, int VertexCount,
133 const void* Indices, int IndexCount, int TriStride);
134 /* same again with a normals array (used as trimesh-trimesh optimization) */
135 ODE_API void dGeomTriMeshDataBuildDouble1(dTriMeshDataID g,
136 const void* Vertices, int VertexStride, int VertexCount,
137 const void* Indices, int IndexCount, int TriStride,
138 const void* Normals);
141 * Simple build. Single/double precision based on dSINGLE/dDOUBLE!
143 ODE_API void dGeomTriMeshDataBuildSimple(dTriMeshDataID g,
144 const dReal* Vertices, int VertexCount,
145 const dTriIndex* Indices, int IndexCount);
146 /* same again with a normals array (used as trimesh-trimesh optimization) */
147 ODE_API void dGeomTriMeshDataBuildSimple1(dTriMeshDataID g,
148 const dReal* Vertices, int VertexCount,
149 const dTriIndex* Indices, int IndexCount,
154 * Data preprocessing build request flags.
158 dTRIDATAPREPROCESS_BUILD__MIN,
160 dTRIDATAPREPROCESS_BUILD_CONCAVE_EDGES = dTRIDATAPREPROCESS_BUILD__MIN, // Used to optimize OPCODE trimesh-capsule collisions; allocates 1 byte per triangle; no extra data associated
161 dTRIDATAPREPROCESS_BUILD_FACE_ANGLES, // Used to aid trimesh-convex collisions; memory requirements depend on extra data
163 dTRIDATAPREPROCESS_BUILD__MAX,
167 * Data preprocessing extra values for dTRIDATAPREPROCESS_BUILD_FACE_ANGLES.
171 dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__MIN,
173 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
174 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
175 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
177 dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__MAX,
179 dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA__DEFAULT = dTRIDATAPREPROCESS_FACE_ANGLES_EXTRA_BYTE_POSITIVE, // The default value assumed if the extra data is not provided
184 * Pre-process the trimesh data according to the request flags.
186 * buildRequestFlags is a bitmask of 1U << dTRIDATAPREPROCESS_BUILD_...
187 * It is allowed to call the function multiple times provided the bitmasks are different each time.
189 * requestExtraData is an optional pointer to array of extra parameters per bitmask bits
190 * (only the elements indexed by positions of raised bits are examined;
191 * defaults are assumed if the pointer is NULL)
193 * The function returns a boolean status the only failure reason being insufficient memory.
195 ODE_API int dGeomTriMeshDataPreprocess2(dTriMeshDataID g, unsigned int buildRequestFlags, const dintptr *requestExtraData/*=NULL | const dintptr (*)[dTRIDATAPREPROCESS_BUILD__MAX]*/);
198 * Obsolete. Equivalent to calling dGeomTriMeshDataPreprocess2(g, (1U << dTRIDATAPREPROCESS_BUILD_CONCAVE_EDGES), NULL)
200 ODE_API int dGeomTriMeshDataPreprocess(dTriMeshDataID g);
205 * Get and set the internal preprocessed trimesh data buffer (see the enumerated type above), for loading and saving
206 * These functions are deprecated. Use dGeomTriMeshDataSet/dGeomTriMeshDataGet2 with dTRIMESHDATA_USE_FLAGS instead.
208 ODE_API_DEPRECATED ODE_API void dGeomTriMeshDataGetBuffer(dTriMeshDataID g, unsigned char** buf, int* bufLen);
209 ODE_API_DEPRECATED ODE_API void dGeomTriMeshDataSetBuffer(dTriMeshDataID g, unsigned char* buf);
213 * Per triangle callback. Allows the user to say if he wants a collision with
214 * a particular triangle.
216 typedef int dTriCallback(dGeomID TriMesh, dGeomID RefObject, int TriangleIndex);
217 ODE_API void dGeomTriMeshSetCallback(dGeomID g, dTriCallback* Callback);
218 ODE_API dTriCallback* dGeomTriMeshGetCallback(dGeomID g);
221 * Per object callback. Allows the user to get the list of triangles in 1
222 * shot. Maybe we should remove this one.
224 typedef void dTriArrayCallback(dGeomID TriMesh, dGeomID RefObject, const int* TriIndices, int TriCount);
225 ODE_API void dGeomTriMeshSetArrayCallback(dGeomID g, dTriArrayCallback* ArrayCallback);
226 ODE_API dTriArrayCallback* dGeomTriMeshGetArrayCallback(dGeomID g);
230 * Allows the user to say if a ray collides with a triangle on barycentric
231 * coords. The user can for example sample a texture with alpha transparency
232 * to determine if a collision should occur.
234 typedef int dTriRayCallback(dGeomID TriMesh, dGeomID Ray, int TriangleIndex, dReal u, dReal v);
235 ODE_API void dGeomTriMeshSetRayCallback(dGeomID g, dTriRayCallback* Callback);
236 ODE_API dTriRayCallback* dGeomTriMeshGetRayCallback(dGeomID g);
239 * Triangle merging callback.
240 * Allows the user to generate a fake triangle index for a new contact generated
241 * from merging of two other contacts. That index could later be used by the
242 * user to determine attributes of original triangles used as sources for a
245 typedef int dTriTriMergeCallback(dGeomID TriMesh, int FirstTriangleIndex, int SecondTriangleIndex);
246 ODE_API void dGeomTriMeshSetTriMergeCallback(dGeomID g, dTriTriMergeCallback* Callback);
247 ODE_API dTriTriMergeCallback* dGeomTriMeshGetTriMergeCallback(dGeomID g);
251 * Construction. Callbacks are optional.
253 ODE_API dGeomID dCreateTriMesh(dSpaceID space, dTriMeshDataID Data, dTriCallback* Callback, dTriArrayCallback* ArrayCallback, dTriRayCallback* RayCallback);
255 ODE_API void dGeomTriMeshSetData(dGeomID g, dTriMeshDataID Data);
256 ODE_API dTriMeshDataID dGeomTriMeshGetData(dGeomID g);
259 /* enable/disable/check temporal coherence*/
260 ODE_API void dGeomTriMeshEnableTC(dGeomID g, int geomClass, int enable);
261 ODE_API int dGeomTriMeshIsTCEnabled(dGeomID g, int geomClass);
264 * Clears the internal temporal coherence caches. When a geom has its
265 * collision checked with a trimesh once, data is stored inside the trimesh.
266 * With large worlds with lots of seperate objects this list could get huge.
267 * We should be able to do this automagically.
269 ODE_API void dGeomTriMeshClearTCCache(dGeomID g);
273 * returns the TriMeshDataID
275 ODE_API dTriMeshDataID dGeomTriMeshGetTriMeshDataID(dGeomID g);
280 ODE_API void dGeomTriMeshGetTriangle(dGeomID g, int Index, dVector3* v0, dVector3* v1, dVector3* v2);
283 * Gets the point on the requested triangle and the given barycentric
286 ODE_API void dGeomTriMeshGetPoint(dGeomID g, int Index, dReal u, dReal v, dVector3 Out);
290 This is how the strided data works:
292 struct StridedVertex{
296 int VertexStride = sizeof(StridedVertex);
302 int TriStride = sizeof(StridedTri);
307 ODE_API int dGeomTriMeshGetTriangleCount (dGeomID g);
309 ODE_API void dGeomTriMeshDataUpdate(dTriMeshDataID g);
315 #endif /* _ODE_COLLISION_TRIMESH_H_ */