My Project: dynamics.mod/NewtonDynamics/source/physics/dgCollisionMesh.h Source File

My Project

dgCollisionMesh.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #ifndef __DGCOLLISION_MESH_H__
23 #define __DGCOLLISION_MESH_H__
24
25
26 #include "dgCollision.h"
27 #include "dgCollisionConvex.h"
28 #include "dgCollisionInstance.h"
29
30
31 #define DG_MAX_COLLIDING_FACES 512
32 #define DG_MAX_COLLIDING_INDICES (DG_MAX_COLLIDING_FACES * (4 * 2 + 3))
33
34
35 class dgCollisionMesh;
36 typedef void (*dgCollisionMeshCollisionCallback) (const dgBody* const bodyWithTreeCollision, const dgBody* const body, dgInt32 faceID,
37 dgInt32 vertexCount, const dgFloat32* const vertex, dgInt32 vertexStrideInBytes);
38
39
40
41 DG_MSC_VECTOR_ALIGMENT
42 class dgPolygonMeshDesc: public dgFastAABBInfo
43 {
44 public:
45 class dgMesh
46 {
47 public:
48 dgInt32 m_globalFaceIndexCount[DG_MAX_COLLIDING_FACES];
49 dgInt32 m_globalFaceIndexStart[DG_MAX_COLLIDING_FACES];
50 dgFloat32 m_globalHitDistance[DG_MAX_COLLIDING_FACES];
51 };
52
53 // colliding box in polygonSoup local space
54 DG_INLINE dgPolygonMeshDesc()
55 :dgFastAABBInfo()
56 ,m_boxDistanceTravelInMeshSpace(dgFloat32 (0.0f))
57 ,m_maxT(dgFloat32 (1.0f))
58 ,m_doContinuesCollisionTest(false)
59 {
60 }
61
62 dgPolygonMeshDesc(dgCollisionParamProxy& proxy, void* const userData);
63
64 DG_INLINE void SetDistanceTravel (const dgVector& distanceInGlobalSpace)
65 {
66 //const dgMatrix& soupMatrix = data.m_polySoupCollision->GetGlobalMatrix();
67 const dgMatrix& soupMatrix = m_polySoupCollision->GetGlobalMatrix();
68 //data.m_boxDistanceTravelInMeshSpace = data.m_polySoupCollision->GetInvScale().CompProduct4(soupMatrix.UnrotateVector(upperBoundVeloc.CompProduct4(data.m_objCollision->GetInvScale())));
69 m_boxDistanceTravelInMeshSpace = m_polySoupCollision->GetInvScale().CompProduct4(soupMatrix.UnrotateVector(distanceInGlobalSpace.CompProduct4(m_objCollision->GetInvScale())));
70 }
71
72
73 DG_INLINE dgInt32 GetFaceIndexCount(dgInt32 indexCount) const
74 {
75 return indexCount * 2 + 3;
76 }
77
78 DG_INLINE const dgInt32* GetAdjacentFaceEdgeNormalArray(const dgInt32* const faceIndexArray, dgInt32 indexCount) const
79 {
80 return &faceIndexArray[indexCount + 2];
81 }
82
83
84 DG_INLINE dgInt32 GetNormalIndex(const dgInt32* const faceIndexArray, dgInt32 indexCount) const
85 {
86 return faceIndexArray[indexCount + 1];
87 }
88
89 DG_INLINE dgInt32 GetFaceId(const dgInt32* const faceIndexArray, dgInt32 indexCount) const
90 {
91 return faceIndexArray[indexCount];
92 }
93
94 DG_INLINE dgFloat32 GetFaceSize(const dgInt32* const faceIndexArray, dgInt32 indexCount) const
95 {
96 dgInt32 size = faceIndexArray[indexCount * 2 + 2];
97 return dgFloat32 ((size >= 1) ? size : dgFloat32 (1.0f));
98 }
99
100 void SortFaceArray ();
101
102 dgVector m_boxDistanceTravelInMeshSpace;
103 dgInt32 m_threadNumber;
104 dgInt32 m_faceCount;
105 dgInt32 m_vertexStrideInBytes;
106 dgFloat32 m_skinThickness;
107 void* m_userData;
108 dgBody *m_objBody;
109 dgBody *m_polySoupBody;
110 dgCollisionInstance* m_objCollision;
111 dgCollisionInstance* m_polySoupCollision;
112 dgFloat32* m_vertex;
113 dgInt32* m_faceIndexCount;
114 dgInt32* m_faceVertexIndex;
115
116 // private data;
117 dgInt32* m_faceIndexStart;
118 dgFloat32* m_hitDistance;
119 const dgCollisionMesh* m_me;
120 dgInt32 m_globalIndexCount;
121 dgFloat32 m_maxT;
122 bool m_doContinuesCollisionTest;
123 dgInt32 m_globalFaceVertexIndex[DG_MAX_COLLIDING_INDICES];
124 dgMesh m_meshData;
125 } DG_GCC_VECTOR_ALIGMENT;
126
127 DG_MSC_VECTOR_ALIGMENT
128 class dgCollisionMeshRayHitDesc
129 {
130 public:
131 dgCollisionMeshRayHitDesc ()
132 :m_matrix (dgGetIdentityMatrix())
133 {
134 }
135
136 dgVector m_localP0;
137 dgVector m_localP1;
138 dgVector m_normal;
139 dgUnsigned64 m_userId;
140 void* m_userData;
141 void* m_altenateUserData;
142 dgMatrix m_matrix;
143 }DG_GCC_VECTOR_ALIGMENT;
144
145
146
147 class dgCollisionMesh: public dgCollision
148 {
149 public:
150
151 DG_MSC_VECTOR_ALIGMENT
152 class dgMeshVertexListIndexList
153 {
154 public:
155 dgInt32* m_indexList;
156 dgInt32* m_userDataList;
157 dgFloat32* m_veterxArray;
158 dgInt32 m_triangleCount;
159 dgInt32 m_maxIndexCount;
160 dgInt32 m_vertexCount;
161 dgInt32 m_vertexStrideInBytes;
162 }DG_GCC_VECTOR_ALIGMENT;
163
164
165 dgCollisionMesh (dgWorld* const world, dgCollisionID type);
166 dgCollisionMesh (dgWorld* const world, dgDeserialize deserialization, void* const userData);
167 virtual ~dgCollisionMesh();
168
169
170
171
172 virtual dgFloat32 GetVolume () const;
173 virtual dgFloat32 GetBoxMinRadius () const;
174 virtual dgFloat32 GetBoxMaxRadius () const;
175 virtual void GetVertexListIndexList (const dgVector& p0, const dgVector& p1, dgMeshVertexListIndexList &data) const = 0;
176
177 virtual void GetCollidingFaces (dgPolygonMeshDesc* const data) const = 0;
178
179 void SetDebugCollisionCallback (dgCollisionMeshCollisionCallback debugCallback);
180 dgCollisionMeshCollisionCallback GetDebugCollisionCallback() const { return m_debugCallback;}
181
182 protected:
183 virtual void SetCollisionBBox (const dgVector& p0, const dgVector& p1);
184
185 private:
186 virtual dgInt32 CalculateSignature () const;
187 virtual dgVector SupportVertex (const dgVector& dir, dgInt32* const vertexIndex) const;
188
189 virtual void CalcAABB (const dgMatrix& matrix, dgVector& p0, dgVector& p1) const;
190 virtual void DebugCollision (const dgMatrix& matrix, dgCollision::OnDebugCollisionMeshCallback callback, void* const userData) const;
191
192 virtual dgVector CalculateVolumeIntegral (const dgMatrix& globalMatrix, const dgVector& plane, const dgCollisionInstance& parentScale) const;
193 virtual dgFloat32 RayCast (const dgVector& localP0, const dgVector& localP1, dgFloat32 maxT, dgContactPoint& contactOut, const dgBody* const body, void* const userData, OnRayPrecastAction preFilter) const = 0;
194 virtual dgFloat32 ConvexRayCast (const dgCollisionInstance* const convexShape, const dgMatrix& origin, const dgVector& veloc, dgFloat32 minT, dgContactPoint& contactOut, const dgBody* const referenceBody, const dgCollisionInstance* const referenceInstance, void* const userData, dgInt32 threadId) const;
195
196 dgInt32 CalculatePlaneIntersection (const dgFloat32* const vertex, const dgInt32* const index, dgInt32 indexCount, dgInt32 strideInFloat, const dgPlane& localPlane, dgVector* const contactsOut) const;
197
198 dgInt32 CalculatePlaneIntersection (const dgVector& normal, const dgVector& point, dgVector* const contactsOut, dgFloat32 normalSign) const;
199
200 virtual void GetCollisionInfo(dgCollisionInfo* const info) const;
201 virtual void Serialize(dgSerialize callback, void* const userData) const;
202
203
204 #ifdef DG_DEBUG_AABB
205 dgVector BoxSupportMapping (const dgVector& dir) const;
206 #endif
207
208 protected:
209 dgCollisionMeshCollisionCallback m_debugCallback;
210
211
212 friend class dgWorld;
213 friend class dgCollisionInstance;
214 };
215
216
217
218 #endif
219
dgPlane
Definition: dgPlane.h:29
dgCollisionMesh::m_debugCallback
dgCollisionMeshCollisionCallback m_debugCallback
Definition: dgCollisionMesh.h:209
dgCollisionMesh::dgMeshVertexListIndexList::m_userDataList
dgInt32 * m_userDataList
Definition: dgCollisionMesh.h:156
dgUnsigned64
unsigned long long dgUnsigned64
Definition: dgTypes.h:223
dgPolygonMeshDesc::m_globalFaceVertexIndex
dgInt32 m_globalFaceVertexIndex[DG_MAX_COLLIDING_INDICES]
Definition: dgCollisionMesh.h:123
dgCollisionMesh::dgMeshVertexListIndexList::m_veterxArray
dgFloat32 * m_veterxArray
Definition: dgCollisionMesh.h:157
dgCollisionMesh::dgMeshVertexListIndexList::m_vertexCount
dgInt32 m_vertexCount
Definition: dgCollisionMesh.h:160
dgPolygonMeshDesc::m_skinThickness
dgFloat32 m_skinThickness
Definition: dgCollisionMesh.h:106
DG_MAX_COLLIDING_INDICES
#define DG_MAX_COLLIDING_INDICES
Definition: dgCollisionMesh.h:32
dgCollisionMeshRayHitDesc::m_userData
void * m_userData
Definition: dgCollisionMesh.h:140
dgCollision::OnDebugCollisionMeshCallback
void(dgApi * OnDebugCollisionMeshCallback)(void *userData, int vertexCount, const dgFloat32 *FaceArray, int faceId)
Definition: dgCollision.h:220
dgPolygonMeshDesc::m_doContinuesCollisionTest
bool m_doContinuesCollisionTest
Definition: dgCollisionMesh.h:122
dgPolygonMeshDesc::dgPolygonMeshDesc
DG_INLINE dgPolygonMeshDesc()
Definition: dgCollisionMesh.h:54
dgCollisionInfo
Definition: dgCollision.h:82
dgMatrix::UnrotateVector
dgVector UnrotateVector(const dgVector &v) const
Definition: dgMatrix.h:206
dgBody
Definition: dgBody.h:44
dgCollisionInstance::GetInvScale
const dgVector & GetInvScale() const
Definition: dgCollisionInstance.h:230
dgPolygonMeshDesc::dgMesh::m_globalFaceIndexStart
dgInt32 m_globalFaceIndexStart[DG_MAX_COLLIDING_FACES]
Definition: dgCollisionMesh.h:49
userData
void *const userData
Definition: dgBroadPhase.h:35
dgCollisionMeshCollisionCallback
void(* dgCollisionMeshCollisionCallback)(const dgBody *const bodyWithTreeCollision, const dgBody *const body, dgInt32 faceID, dgInt32 vertexCount, const dgFloat32 *const vertex, dgInt32 vertexStrideInBytes)
Definition: dgCollisionMesh.h:36
dgCollisionMeshRayHitDesc::m_localP1
dgVector m_localP1
Definition: dgCollisionMesh.h:137
DG_INLINE
#define DG_INLINE
Definition: dgTypes.h:178
dgPolygonMeshDesc::m_faceVertexIndex
dgInt32 * m_faceVertexIndex
Definition: dgCollisionMesh.h:114
dgFloat32
float dgFloat32
Definition: dgTypes.h:233
dgInt32
int dgInt32
Definition: dgTypes.h:218
void
typedef void(dgApi *ConstraintsForceFeeback)(const dgConstraint &me
dgCollisionMeshRayHitDesc::m_normal
dgVector m_normal
Definition: dgCollisionMesh.h:138
dgPolygonMeshDesc
Definition: dgCollisionMesh.h:42
dgVector
Definition: dgVector.h:639
dgPolygonMeshDesc::m_hitDistance
dgFloat32 * m_hitDistance
Definition: dgCollisionMesh.h:118
dgPolygonMeshDesc::m_threadNumber
dgInt32 m_threadNumber
Definition: dgCollisionMesh.h:103
dgCollisionMeshRayHitDesc::m_matrix
dgMatrix m_matrix
Definition: dgCollisionMesh.h:142
dgPolygonMeshDesc::GetAdjacentFaceEdgeNormalArray
DG_INLINE const dgInt32 * GetAdjacentFaceEdgeNormalArray(const dgInt32 *const faceIndexArray, dgInt32 indexCount) const
Definition: dgCollisionMesh.h:78
dgPolygonMeshDesc::m_meshData
dgMesh m_meshData
Definition: dgCollisionMesh.h:124
dgPolygonMeshDesc::m_objBody
dgBody * m_objBody
Definition: dgCollisionMesh.h:108
dgContactPoint
Definition: dgContact.h:78
dgCollisionMesh::dgMeshVertexListIndexList::m_maxIndexCount
dgInt32 m_maxIndexCount
Definition: dgCollisionMesh.h:159
dgPolygonMeshDesc::dgMesh
Definition: dgCollisionMesh.h:45
dgSerialize
void(dgApi * dgSerialize)(void *const userData, const void *const buffer, size_t size)
Definition: dgTypes.h:280
dgPolygonMeshDesc::m_vertex
dgFloat32 * m_vertex
Definition: dgCollisionMesh.h:112
DG_MSC_VECTOR_ALIGMENT
#define DG_MSC_VECTOR_ALIGMENT
Definition: dgTypes.h:194
dgCollisionInstance::GetGlobalMatrix
const dgMatrix & GetGlobalMatrix() const
Definition: dgCollisionInstance.h:241
dgCollisionMesh::dgMeshVertexListIndexList::m_vertexStrideInBytes
dgInt32 m_vertexStrideInBytes
Definition: dgCollisionMesh.h:161
dgCollisionMeshRayHitDesc::dgCollisionMeshRayHitDesc
dgCollisionMeshRayHitDesc()
Definition: dgCollisionMesh.h:131
dgDeserialize
void(dgApi * dgDeserialize)(void *const userData, void *buffer, size_t size)
Definition: dgTypes.h:279
dgCollisionMeshRayHitDesc
Definition: dgCollisionMesh.h:128
dgPolygonMeshDesc::dgMesh::m_globalHitDistance
dgFloat32 m_globalHitDistance[DG_MAX_COLLIDING_FACES]
Definition: dgCollisionMesh.h:50
dgPolygonMeshDesc::m_polySoupBody
dgBody * m_polySoupBody
Definition: dgCollisionMesh.h:109
dgPolygonMeshDesc::SetDistanceTravel
DG_INLINE void SetDistanceTravel(const dgVector &distanceInGlobalSpace)
Definition: dgCollisionMesh.h:64
dgCollisionMeshRayHitDesc::m_userId
dgUnsigned64 m_userId
Definition: dgCollisionMesh.h:139
dgCollisionMesh::dgMeshVertexListIndexList::m_indexList
dgInt32 * m_indexList
Definition: dgCollisionMesh.h:155
dgFastAABBInfo
Definition: dgIntersections.h:245
dgPolygonMeshDesc::m_me
const dgCollisionMesh * m_me
Definition: dgCollisionMesh.h:119
OnRayPrecastAction
dgUnsigned32(dgApi * OnRayPrecastAction)(const dgBody *const body, const dgCollisionInstance *const collision, void *const userData)
Definition: dgBroadPhase.h:36
dgPolygonMeshDesc::SortFaceArray
void SortFaceArray()
Definition: dgCollisionMesh.cpp:128
dgCollisionInstance
Definition: dgCollisionInstance.h:30
dgCollisionMesh
Definition: dgCollisionMesh.h:147
dgPolygonMeshDesc::m_boxDistanceTravelInMeshSpace
dgVector m_boxDistanceTravelInMeshSpace
Definition: dgCollisionMesh.h:102
dgPolygonMeshDesc::m_maxT
dgFloat32 m_maxT
Definition: dgCollisionMesh.h:121
dgPolygonMeshDesc::dgMesh::m_globalFaceIndexCount
dgInt32 m_globalFaceIndexCount[DG_MAX_COLLIDING_FACES]
Definition: dgCollisionMesh.h:48
dgCollisionMesh::GetDebugCollisionCallback
dgCollisionMeshCollisionCallback GetDebugCollisionCallback() const
Definition: dgCollisionMesh.h:180
dgCollisionMesh::dgMeshVertexListIndexList::m_triangleCount
dgInt32 m_triangleCount
Definition: dgCollisionMesh.h:158
dgPolygonMeshDesc::GetFaceSize
DG_INLINE dgFloat32 GetFaceSize(const dgInt32 *const faceIndexArray, dgInt32 indexCount) const
Definition: dgCollisionMesh.h:94
dgPolygonMeshDesc::m_vertexStrideInBytes
dgInt32 m_vertexStrideInBytes
Definition: dgCollisionMesh.h:105
DG_GCC_VECTOR_ALIGMENT
dgPolygonMeshDesc DG_GCC_VECTOR_ALIGMENT
dgVector::CompProduct4
DG_INLINE dgVector CompProduct4(const dgVector &A) const
Definition: dgVector.h:851
dgCollisionMeshRayHitDesc::m_localP0
dgVector m_localP0
Definition: dgCollisionMesh.h:136
dgCollision.h
dgCollisionMesh::dgMeshVertexListIndexList
Definition: dgCollisionMesh.h:152
dgCollision
Definition: dgCollision.h:217
dgPolygonMeshDesc::m_polySoupCollision
dgCollisionInstance * m_polySoupCollision
Definition: dgCollisionMesh.h:111
dgPolygonMeshDesc::m_objCollision
dgCollisionInstance * m_objCollision
Definition: dgCollisionMesh.h:110
dgPolygonMeshDesc::m_faceIndexStart
dgInt32 * m_faceIndexStart
Definition: dgCollisionMesh.h:117
dgCollisionInstance.h
dgMatrix
Definition: dgMatrix.h:40
dgPolygonMeshDesc::m_globalIndexCount
dgInt32 m_globalIndexCount
Definition: dgCollisionMesh.h:120
dgPolygonMeshDesc::GetFaceIndexCount
DG_INLINE dgInt32 GetFaceIndexCount(dgInt32 indexCount) const
Definition: dgCollisionMesh.h:73
dgGetIdentityMatrix
const dgMatrix & dgGetIdentityMatrix()
Definition: dgMatrix.cpp:47
dgWorld
Definition: dgWorld.h:129
dgCollisionMeshRayHitDesc::m_altenateUserData
void * m_altenateUserData
Definition: dgCollisionMesh.h:141
dgPolygonMeshDesc::GetNormalIndex
DG_INLINE dgInt32 GetNormalIndex(const dgInt32 *const faceIndexArray, dgInt32 indexCount) const
Definition: dgCollisionMesh.h:84
dgPolygonMeshDesc::GetFaceId
DG_INLINE dgInt32 GetFaceId(const dgInt32 *const faceIndexArray, dgInt32 indexCount) const
Definition: dgCollisionMesh.h:89
dgPolygonMeshDesc::m_faceCount
dgInt32 m_faceCount
Definition: dgCollisionMesh.h:104
DG_MAX_COLLIDING_FACES
#define DG_MAX_COLLIDING_FACES
Definition: dgCollisionMesh.h:31
dgPolygonMeshDesc::m_faceIndexCount
dgInt32 * m_faceIndexCount
Definition: dgCollisionMesh.h:113
dgCollisionID
dgCollisionID
Definition: dgCollision.h:52
dgCollisionConvex.h
dgPolygonMeshDesc::m_userData
void * m_userData
Definition: dgCollisionMesh.h:107
dgCollisionParamProxy
Definition: dgContact.h:93