28 #ifndef _ODE_COLLISION_TRIMESH_INTERNAL_H_
29 #define _ODE_COLLISION_TRIMESH_INTERNAL_H_
35 #include "collision_kernel.h"
36 #include "collision_trimesh_colliders.h"
37 #include <ode/collision_trimesh.h>
40 #define BAN_OPCODE_AUTOLINK
42 using namespace Opcode;
43 #endif // dTRIMESH_OPCODE
46 #include <GIMPACT/gimpact.h>
57 #if !dTRIMESH_OPCODE_USE_OLD_TRIMESH_TRIMESH_COLLIDER
62 MAXCONTACT_X_NODE = 4,
63 CONTACTS_HASHSIZE = 256
72 struct CONTACT_KEY_HASH_NODE
74 CONTACT_KEY m_keyarray[MAXCONTACT_X_NODE];
78 struct CONTACT_KEY_HASH_TABLE
81 CONTACT_KEY_HASH_NODE &operator[](
unsigned int index) {
return m_storage[index]; }
84 CONTACT_KEY_HASH_NODE m_storage[CONTACTS_HASHSIZE];
87 #endif // !dTRIMESH_OPCODE_USE_OLD_TRIMESH_TRIMESH_COLLIDER
88 #endif // dTRIMESH_OPCODE
96 bool ResizeAndResetVertexUSEDFlags(
unsigned VertexCount)
99 size_t VertexNewElements = (VertexCount + 7) / 8;
100 if (VertexNewElements <= m_VertexUseElements || ReallocVertexUSEDFlags(VertexNewElements)) {
101 memset(m_VertexUseBits, 0, VertexNewElements);
107 bool GetVertexUSEDFlag(
unsigned VertexIndex)
const {
return (m_VertexUseBits[VertexIndex / 8] & (1 << (VertexIndex % 8))) != 0; }
108 void SetVertexUSEDFlag(
unsigned VertexIndex) { m_VertexUseBits[VertexIndex / 8] |= (1 << (VertexIndex % 8)); }
111 bool ReallocVertexUSEDFlags(
size_t VertexNewElements)
114 uint8 *VertexNewBits = (uint8 *)dRealloc(m_VertexUseBits, m_VertexUseElements *
sizeof(m_VertexUseBits[0]), VertexNewElements *
sizeof(m_VertexUseBits[0]));
116 m_VertexUseBits = VertexNewBits;
117 m_VertexUseElements = VertexNewElements;
123 void FreeVertexUSEDFlags()
125 dFree(m_VertexUseBits, m_VertexUseElements *
sizeof(m_VertexUseBits[0]));
126 m_VertexUseBits = NULL;
127 m_VertexUseElements = 0;
131 uint8 *m_VertexUseBits;
132 size_t m_VertexUseElements;
142 #endif // dTRIMESH_OPCODE
147 void InitOPCODECaches();
153 #if !dTRIMESH_OPCODE_USE_OLD_TRIMESH_TRIMESH_COLLIDER
154 CONTACT_KEY_HASH_TABLE _hashcontactset;
161 SphereCollider _SphereCollider;
162 OBBCollider _OBBCollider;
163 RayCollider _RayCollider;
164 AABBTreeCollider _AABBTreeCollider;
169 CollisionFaces Faces;
170 SphereCache defaultSphereCache;
171 OBBCache defaultBoxCache;
172 LSSCache defaultCapsuleCache;
177 #endif // dTRIMESH_OPCODE
184 EODETLSKIND tkTLSKind = (EODETLSKIND)uiTLSKind;
185 return COdeTls::GetTrimeshCollidersCache(tkTLSKind);
189 #else // dTLS_ENABLED
195 return &g_ccTrimeshCollidersCache;
199 #endif // dTLS_ENABLED
232 void Build(
const void* Vertices,
int VertexStide,
int VertexCount,
233 const void* Indices,
int IndexCount,
int TriStride,
239 dVector3 AABBExtents;
244 #endif // dTRIMESH_OPCODE
247 const char* m_Vertices;
250 const char* m_Indices;
266 void Build(
const void* Vertices,
int VertexStride,
int VertexCount,
267 const void* Indices,
int IndexCount,
int TriStride,
273 dIASSERT(VertexStride);
275 dIASSERT(IndexCount);
276 m_Vertices=(
const char *)Vertices;
277 m_VertexStride = VertexStride;
278 m_VertexCount = VertexCount;
279 m_Indices = (
const char *)Indices;
280 m_TriangleCount = IndexCount/3;
281 m_TriStride = TriStride;
285 inline void GetVertex(
unsigned int i, dVector3 Out)
289 const float * fverts = (
const float * )(m_Vertices + m_VertexStride*i);
297 const double * dverts = (
const double * )(m_Vertices + m_VertexStride*i);
298 Out[0] = (float)dverts[0];
299 Out[1] = (float)dverts[1];
300 Out[2] = (float)dverts[2];
306 inline void GetTriIndices(
unsigned int itriangle,
unsigned int triindices[3])
308 const unsigned int * ind = (
const unsigned int * )(m_Indices + m_TriStride*itriangle);
309 triindices[0] = ind[0];
310 triindices[1] = ind[1];
311 triindices[2] = ind[2];
313 #endif // dTRIMESH_GIMPACT
318 dTriCallback* Callback;
319 dTriArrayCallback* ArrayCallback;
320 dTriRayCallback* RayCallback;
321 dTriTriMergeCallback* TriMergeCallback;
336 bool controlGeometry(
int controlClass,
int controlCode,
void *dataValue,
int *dataSize);
338 int AABBTest(
dxGeom* g, dReal aabb[6]);
344 MERGE_NORMALS__SPHERE_DEFAULT = DONT_MERGE_CONTACTS
346 bool controlGeometry_SetMergeSphereContacts(
int dataValue);
347 bool controlGeometry_GetMergeSphereContacts(
int &returnValue);
350 dxContactMergeOptions SphereContactsMergeOption;
356 struct SphereTC :
public SphereCache{
361 struct BoxTC :
public OBBCache{
366 struct CapsuleTC :
public LSSCache{
370 #endif // dTRIMESH_OPCODE
373 GIM_TRIMESH m_collision_trimesh;
374 GBUFFER_MANAGER_DATA m_buffer_managers[G_BUFFER_MANAGER__MAX];
375 #endif // dTRIMESH_GIMPACT
379 #include "collision_kernel.h"
382 dIASSERT(Index >= 0 && Index < (Flags & NUMC_MASK));
383 return ((
dContactGeom*)(((
char*)Contacts) + (Index * Stride)));
389 inline unsigned FetchTriangleCount(
dxTriMesh* TriMesh)
391 return TriMesh->Data->Mesh.GetNbTriangles();
394 inline void FetchTriangle(
dxTriMesh* TriMesh,
int Index,
const dVector3 Position,
const dMatrix3 Rotation, dVector3 Out[3]){
397 TriMesh->Data->Mesh.GetTriangle(VP, Index, VC);
398 for (
int i = 0; i < 3; i++){
400 v[0] = VP.Vertex[i]->x;
401 v[1] = VP.Vertex[i]->y;
402 v[2] = VP.Vertex[i]->z;
405 dMultiply0_331(Out[i], Rotation, v);
406 Out[i][0] += Position[0];
407 Out[i][1] += Position[1];
408 Out[i][2] += Position[2];
413 inline void FetchTransformedTriangle(
dxTriMesh* TriMesh,
int Index, dVector3 Out[3]){
416 FetchTriangle(TriMesh, Index, Position, Rotation, Out);
419 inline Matrix4x4& MakeMatrix(
const dVector3 Position,
const dMatrix3 Rotation, Matrix4x4& Out){
420 Out.m[0][0] = (float) Rotation[0];
421 Out.m[1][0] = (float) Rotation[1];
422 Out.m[2][0] = (float) Rotation[2];
424 Out.m[0][1] = (float) Rotation[4];
425 Out.m[1][1] = (float) Rotation[5];
426 Out.m[2][1] = (float) Rotation[6];
428 Out.m[0][2] = (float) Rotation[8];
429 Out.m[1][2] = (float) Rotation[9];
430 Out.m[2][2] = (float) Rotation[10];
432 Out.m[3][0] = (float) Position[0];
433 Out.m[3][1] = (float) Position[1];
434 Out.m[3][2] = (float) Position[2];
444 inline Matrix4x4& MakeMatrix(
dxGeom* g, Matrix4x4& Out){
447 return MakeMatrix(Position, Rotation, Out);
449 #endif // dTRIMESH_OPCODE
459 #define dVECTOR3_VEC3F_COPY(b,a) { \
466 inline void gim_trimesh_get_triangle_verticesODE(GIM_TRIMESH * trimesh, GUINT32 triangle_index, dVector3 v1, dVector3 v2, dVector3 v3) {
467 vec3f src1, src2, src3;
468 gim_trimesh_get_triangle_vertices(trimesh, triangle_index, src1, src2, src3);
470 dVECTOR3_VEC3F_COPY(v1, src1);
471 dVECTOR3_VEC3F_COPY(v2, src2);
472 dVECTOR3_VEC3F_COPY(v3, src3);
478 #define gim_trimesh_get_triangle_vertices gim_trimesh_get_triangle_verticesODE
480 inline int gim_trimesh_ray_closest_collisionODE( GIM_TRIMESH *mesh, dVector3 origin, dVector3 dir, GREAL tmax, GIM_TRIANGLE_RAY_CONTACT_DATA *contact ) {
481 vec3f dir_vec3f = { dir[ 0 ], dir[ 1 ], dir[ 2 ] };
482 vec3f origin_vec3f = { origin[ 0 ], origin[ 1 ], origin[ 2 ] };
484 return gim_trimesh_ray_closest_collision( mesh, origin_vec3f, dir_vec3f, tmax, contact );
487 inline int gim_trimesh_ray_collisionODE( GIM_TRIMESH *mesh, dVector3 origin, dVector3 dir, GREAL tmax, GIM_TRIANGLE_RAY_CONTACT_DATA *contact ) {
488 vec3f dir_vec3f = { dir[ 0 ], dir[ 1 ], dir[ 2 ] };
489 vec3f origin_vec3f = { origin[ 0 ], origin[ 1 ], origin[ 2 ] };
491 return gim_trimesh_ray_collision( mesh, origin_vec3f, dir_vec3f, tmax, contact );
494 #define gim_trimesh_sphere_collisionODE( mesh, Position, Radius, contact ) { \
495 vec3f pos_vec3f = { Position[ 0 ], Position[ 1 ], Position[ 2 ] }; \
496 gim_trimesh_sphere_collision( mesh, pos_vec3f, Radius, contact ); \
499 #define gim_trimesh_plane_collisionODE( mesh, plane, contact ) { \
500 vec4f plane_vec4f = { plane[ 0 ], plane[ 1 ], plane[ 2 ], plane[ 3 ] }; \
501 gim_trimesh_plane_collision( mesh, plane_vec4f, contact ); \
504 #define GIM_AABB_COPY( src, dst ) { \
505 dst[ 0 ]= (src) -> minX; \
506 dst[ 1 ]= (src) -> maxX; \
507 dst[ 2 ]= (src) -> minY; \
508 dst[ 3 ]= (src) -> maxY; \
509 dst[ 4 ]= (src) -> minZ; \
510 dst[ 5 ]= (src) -> maxZ; \
516 #define gim_trimesh_ray_closest_collisionODE gim_trimesh_ray_closest_collision
517 #define gim_trimesh_ray_collisionODE gim_trimesh_ray_collision
518 #define gim_trimesh_sphere_collisionODE gim_trimesh_sphere_collision
519 #define gim_trimesh_plane_collisionODE gim_trimesh_plane_collision
521 #define GIM_AABB_COPY( src, dst ) memcpy( dst, src, 6 * sizeof( GREAL ) )
525 inline unsigned FetchTriangleCount(
dxTriMesh* TriMesh)
527 return gim_trimesh_get_triangle_count(&TriMesh->m_collision_trimesh);
530 inline void FetchTransformedTriangle(
dxTriMesh* TriMesh,
int Index, dVector3 Out[3]){
531 gim_trimesh_locks_work_data(&TriMesh->m_collision_trimesh);
532 gim_trimesh_get_triangle_vertices(&TriMesh->m_collision_trimesh, (GUINT32)Index, Out[0], Out[1], Out[2]);
533 gim_trimesh_unlocks_work_data(&TriMesh->m_collision_trimesh);
536 inline void MakeMatrix(
const dVector3 Position,
const dMatrix3 Rotation, mat4f m)
538 m[0][0] = (float) Rotation[0];
539 m[0][1] = (float) Rotation[1];
540 m[0][2] = (float) Rotation[2];
542 m[1][0] = (float) Rotation[4];
543 m[1][1] = (float) Rotation[5];
544 m[1][2] = (float) Rotation[6];
546 m[2][0] = (float) Rotation[8];
547 m[2][1] = (float) Rotation[9];
548 m[2][2] = (float) Rotation[10];
550 m[0][3] = (float) Position[0];
551 m[1][3] = (float) Position[1];
552 m[2][3] = (float) Position[2];
556 inline void MakeMatrix(
dxGeom* g, mat4f Out){
559 MakeMatrix(Position, Rotation, Out);
561 #endif // dTRIMESH_GIMPACT
564 inline void Decompose(
const dMatrix3 Matrix, dVector3 Right, dVector3 Up, dVector3 Direction){
565 Right[0] = Matrix[0 * 4 + 0];
566 Right[1] = Matrix[1 * 4 + 0];
567 Right[2] = Matrix[2 * 4 + 0];
568 Right[3] = REAL(0.0);
569 Up[0] = Matrix[0 * 4 + 1];
570 Up[1] = Matrix[1 * 4 + 1];
571 Up[2] = Matrix[2 * 4 + 1];
573 Direction[0] = Matrix[0 * 4 + 2];
574 Direction[1] = Matrix[1 * 4 + 2];
575 Direction[2] = Matrix[2 * 4 + 2];
576 Direction[3] = REAL(0.0);
580 inline void Decompose(
const dMatrix3 Matrix, dVector3 Vectors[3]){
581 Decompose(Matrix, Vectors[0], Vectors[1], Vectors[2]);
585 inline void GetPointFromBarycentric(
const dVector3 dv[3], dReal u, dReal v, dVector3 Out){
586 dReal w = REAL(1.0) - u - v;
588 Out[0] = (dv[0][0] * w) + (dv[1][0] * u) + (dv[2][0] * v);
589 Out[1] = (dv[0][1] * w) + (dv[1][1] * u) + (dv[2][1] * v);
590 Out[2] = (dv[0][2] * w) + (dv[1][2] * u) + (dv[2][2] * v);
591 Out[3] = (dv[0][3] * w) + (dv[1][3] * u) + (dv[2][3] * v);
595 inline bool Callback(
dxTriMesh* TriMesh,
dxGeom* Object,
int TriIndex){
596 if (TriMesh->Callback != NULL){
597 return (TriMesh->Callback(TriMesh, Object, TriIndex)!=0);
603 template<
class T>
const T& dcMAX(
const T& x,
const T& y){
604 return x > y ? x : y;
607 template<
class T>
const T& dcMIN(
const T& x,
const T& y){
608 return x < y ? x : y;
611 dReal SqrDistancePointTri(
const dVector3 p,
const dVector3 triOrigin,
612 const dVector3 triEdge1,
const dVector3 triEdge2,
613 dReal* pfSParam = 0, dReal* pfTParam = 0 );
615 dReal SqrDistanceSegments(
const dVector3 seg1Origin,
const dVector3 seg1Direction,
616 const dVector3 seg2Origin,
const dVector3 seg2Direction,
617 dReal* pfSegP0 = 0, dReal* pfSegP1 = 0 );
619 dReal SqrDistanceSegTri(
const dVector3 segOrigin,
const dVector3 segEnd,
620 const dVector3 triOrigin,
621 const dVector3 triEdge1,
const dVector3 triEdge2,
622 dReal* t = 0, dReal* u = 0, dReal* v = 0 );
625 void Vector3Subtract(
const dVector3 left,
const dVector3 right, dVector3 result )
627 result[0] = left[0] - right[0];
628 result[1] = left[1] - right[1];
629 result[2] = left[2] - right[2];
630 result[3] = REAL(0.0);
634 void Vector3Add(
const dVector3 left,
const dVector3 right, dVector3 result )
636 result[0] = left[0] + right[0];
637 result[1] = left[1] + right[1];
638 result[2] = left[2] + right[2];
639 result[3] = REAL(0.0);
643 void Vector3Negate(
const dVector3 in, dVector3 out )
652 void Vector3Copy(
const dVector3 in, dVector3 out )
661 void Vector3Multiply(
const dVector3 in, dReal scalar, dVector3 out )
663 out[0] = in[0] * scalar;
664 out[1] = in[1] * scalar;
665 out[2] = in[2] * scalar;
670 void TransformVector3(
const dVector3 in,
671 const dMatrix3 orientation,
const dVector3 position,
674 dMultiply0_331( out, orientation, in );
675 out[0] += position[0];
676 out[1] += position[1];
677 out[2] += position[2];
700 bool IntersectCapsuleTri(
const dVector3 segOrigin,
const dVector3 segEnd,
701 const dReal radius,
const dVector3 triOrigin,
702 const dVector3 triEdge0,
const dVector3 triEdge1,
703 dReal* dist, dReal* t, dReal* u, dReal* v )
705 dReal sqrDist = SqrDistanceSegTri( segOrigin, segEnd, triOrigin, triEdge0, triEdge1,
711 return ( sqrDist <= (radius * radius) );
715 #endif //_ODE_COLLISION_TRIMESH_INTERNAL_H_
Definition: ode/src/objects.h:57
ODE_API const dReal * dGeomGetRotation(dGeomID geom)
Get the rotation matrix of a placeable geom.
Definition: collision_trimesh_internal.h:316
Definition: collision_kernel.h:96
Definition: collision_kernel.h:202
Definition: collision_trimesh_internal.h:205
ODE_API const dReal * dGeomGetPosition(dGeomID geom)
Get the position vector of a placeable geom.
Definition: collision_trimesh_internal.h:136
Definition: collision_trimesh_internal.h:90