ScummVM API documentation
dgCollisionMesh.h
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 #if !defined(__DGCOLLISIONPOLYGONALSOUP_H__)
23 #define __DGCOLLISIONPOLYGONALSOUP_H__
24 
25 
26 #include "dgCollision.h"
27 #include "dgCollisionConvex.h"
28 
29 #define DG_MAX_COLLIDING_FACES (1024 * 2)
30 #define DG_MAX_COLLIDING_VERTEX (DG_MAX_COLLIDING_FACES * 4)
31 
32 
33 class dgCollisionMesh;
34 typedef void (*dgCollisionMeshCollisionCallback)(const dgBody *bodyWithTreeCollision, const dgBody *const body, dgInt32 faceID,
35  dgInt32 vertexCount, const dgFloat32 *vertex, dgInt32 vertexStrideInBytes);
36 
37 
38 DG_MSC_VECTOR_ALIGMENT
40 public:
41  // colliding box in polygonSoup local space
42  dgVector m_boxP0;
43  dgVector m_boxP1;
44  dgInt32 m_threadNumber;
45  dgInt32 m_faceCount;
46  dgInt32 m_vertexStrideInBytes;
47  void *m_userData;
48  dgFloat32 *m_vertex;
49  dgInt32 *m_userAttribute;
50  dgInt32 *m_faceIndexCount;
51  dgInt32 *m_faceVertexIndex;
52  dgBody *m_objBody;
53  dgBody *m_polySoupBody;
54  dgFloat32 *m_faceMaxSize;
55 
56  // private data;
57  const dgCollisionMesh *m_me;
58  dgInt32 *m_faceNormalIndex;
59  dgInt32 *m_faceAdjencentEdgeNormal;
60 
61  dgInt32 m_globalIndexCount;
62  dgInt32 m_globalUserAttribute[DG_MAX_COLLIDING_FACES];
63  dgInt32 m_globalFaceIndexCount[DG_MAX_COLLIDING_FACES];
64  dgInt32 m_globalFaceNormalIndex[DG_MAX_COLLIDING_FACES];
65  dgFloat32 m_globalFaceMaxSize[DG_MAX_COLLIDING_FACES];
66  dgInt32 m_globalFaceVertexIndex[DG_MAX_COLLIDING_VERTEX];
67  dgInt32 m_globalAdjencentEdgeNormal[DG_MAX_COLLIDING_VERTEX];
68 
69 
70 } DG_GCC_VECTOR_ALIGMENT;
71 
72 DG_MSC_VECTOR_ALIGMENT
74 public:
76  : m_matrix(dgGetIdentityMatrix()) {
77  }
78 
79  dgVector m_localP0;
80  dgVector m_localP1;
81  dgVector m_normal;
82  dgUnsigned32 m_userId;
83  void *m_userData;
84  void *m_altenateUserData;
85  dgMatrix m_matrix;
86 } DG_GCC_VECTOR_ALIGMENT;
87 
88 
89 
91 public:
93  public:
94  DG_CLIPPED_FACE_EDGE *m_next;
95  DG_CLIPPED_FACE_EDGE *m_twin;
96  dgInt32 m_incidentNormal;
97  dgInt32 m_incidentVertex;
98  };
99 
100  DG_MSC_VECTOR_ALIGMENT
102  public:
103  dgInt32 *m_indexList;
104  dgInt32 *m_userDataList;
105  dgFloat32 *m_veterxArray;
106  dgInt32 m_triangleCount;
107  dgInt32 m_maxIndexCount;
108  dgInt32 m_vertexCount;
109  dgInt32 m_vertexStrideInBytes;
110  } DG_GCC_VECTOR_ALIGMENT;
111 
112  DG_MSC_VECTOR_ALIGMENT
114  public:
117 
118  virtual dgInt32 CalculateSignature() const;
119  virtual void Serialize(dgSerialize callback, void *const userData) const;
120  virtual void SetCollisionBBox(const dgVector &p0, const dgVector &p1);
121  virtual dgFloat32 RayCast(const dgVector &localP0, const dgVector &localP1, dgContactPoint &contactOut, OnRayPrecastAction preFilter, const dgBody *const body, void *const userData) const;
122  virtual bool OOBBTest(const dgMatrix &matrix, const dgCollisionConvex *const shape, void *const cacheOrder) const;
123 
124  virtual dgVector SupportVertex(const dgVector &dir) const;
125  virtual dgVector SupportVertexSimd(const dgVector &dir) const;
126 
127  virtual dgInt32 CalculatePlaneIntersection(const dgVector &normal, const dgVector &point, dgVector *const contactsOut) const;
128  virtual dgInt32 CalculatePlaneIntersectionSimd(const dgVector &normal, const dgVector &point, dgVector *const contactsOut) const;
129 
130  virtual dgFloat32 GetVolume() const;
131  virtual dgFloat32 GetBoxMinRadius() const;
132  virtual dgFloat32 GetBoxMaxRadius() const;
133  virtual void CalculateInertia(dgVector &inertia, dgVector &origin) const;
134  virtual bool IsEdgeIntersection() const;
135 
136  void BeamClipping(const dgCollisionConvex *const hull, const dgMatrix &matrix, dgFloat32 size);
137  void BeamClippingSimd(const dgCollisionConvex *const hull, const dgMatrix &matrix, dgFloat32 size);
138  dgInt32 QuickTest(const dgCollisionConvex *const hull, const dgMatrix &matrix);
139  dgInt32 QuickTestSimd(const dgCollisionConvex *const hull, const dgMatrix &matrix);
140  dgInt32 QuickTestContinue(const dgCollisionConvex *const hull, const dgMatrix &matrix);
141  dgInt32 QuickTestContinueSimd(const dgCollisionConvex *const hull, const dgMatrix &matrix);
142  dgInt32 ClipContacts(dgInt32 count, dgContactPoint *const contactOut, const dgMatrix &globalMatrix) const;
143 
144  dgVector ClosestDistanceToTriangle(const dgVector &point, const dgVector &p0, const dgVector &p1, const dgVector &p2, bool &isEdge) const;
145  bool PointToPolygonDistance(const dgVector &point, dgFloat32 radius, dgVector &out, bool &isEdge);
146  bool DistanceToOrigen(const dgMatrix &matrix, const dgVector &scale, dgFloat32 radius, dgVector &out, bool &isEdge);
147  dgFloat32 MovingPointToPolygonContact(const dgVector &p, const dgVector &veloc, dgFloat32 radius, dgContactPoint &contact);
148  dgFloat32 MovingSphereToEdgeContact(const dgVector &center, const dgVector &veloc, dgFloat32 radius, const dgVector &v0, const dgVector &v1, dgVector &contactOutOnLine) const;
149 
150  void CalculateNormal();
151  void CalculateNormalSimd();
152 
153  dgVector m_normal;
154  dgVector m_localPoly[64];
155  dgVector m_localPolySimd[64 * 3 / 4];
156  dgInt32 m_clippEdgeNormal[64];
157  dgInt32 m_count;
158  dgInt32 m_paddedCount;
159  dgInt32 m_normalIndex;
160  dgInt32 m_stride;
161  dgInt32 *m_index;
162  dgInt32 *m_adjacentNormalIndex;
163  dgFloat32 *m_vertex;
164  mutable dgInt32 m_isEdgeIntersection;
165 
166  } DG_GCC_VECTOR_ALIGMENT;
167 
168  dgCollisionMesh(dgMemoryAllocator *const allocator, dgCollisionID type);
169  dgCollisionMesh(dgWorld *const world, dgDeserialize deserialization, void *const userData);
170  virtual ~dgCollisionMesh();
171 
172  void SetCollisionCallback(dgCollisionMeshCollisionCallback debugCallback);
173 
174 
175 
176  virtual void *GetUserData() const;
177  virtual dgFloat32 GetVolume() const;
178  virtual dgFloat32 GetBoxMinRadius() const;
179  virtual dgFloat32 GetBoxMaxRadius() const;
180  virtual void CalculateInertia(dgVector &inertia, dgVector &origin) const;
181  virtual void GetVertexListIndexList(const dgVector &p0, const dgVector &p1, dgGetVertexListIndexList &data) const = 0;
182 
183  virtual void GetCollidingFaces(dgPolygonMeshDesc *const data) const = 0;
184  virtual void GetCollidingFacesSimd(dgPolygonMeshDesc *const data) const = 0;
185 
186  dgCollisionMeshCollisionCallback GetDebugCollisionCallback() const {
187  return m_debugCallback;
188  }
189 // dgCollisionMeshUserRayCastCallback GetDebugRayCastCallback() const { return m_userRayCastCallback;}
190 
191 protected:
192  virtual void SetCollisionBBox(const dgVector &p0, const dgVector &p1);
193 
194 private:
195  virtual dgInt32 CalculateSignature() const;
196  dgVector SupportVertex(const dgVector &dir) const;
197  virtual void CalcAABB(const dgMatrix &matrix, dgVector &p0, dgVector &p1) const;
198  virtual void CalcAABBSimd(const dgMatrix &matrix, dgVector &p0, dgVector &p1) const;
199  virtual bool OOBBTest(const dgMatrix &matrix, const dgCollisionConvex *const shape, void *const cacheOrder) const;
200 
201 
202  virtual void DebugCollision(const dgMatrix &matrix, OnDebugCollisionMeshCallback callback, void *const userData) const;
203 
204  virtual dgVector CalculateVolumeIntegral(const dgMatrix &globalMatrix, GetBuoyancyPlane bouyancyPlane, void *const context) const;
205  virtual dgFloat32 RayCast(const dgVector &localP0, const dgVector &localP1, dgContactPoint &contactOut, OnRayPrecastAction preFilter, const dgBody *const body, void *const userData) const = 0;
206  virtual dgFloat32 RayCastSimd(const dgVector &localP0, const dgVector &localP1, dgContactPoint &contactOut, OnRayPrecastAction preFilter, const dgBody *const body, void *const userData) const = 0;
207 
208  dgInt32 CalculatePlaneIntersection(const dgFloat32 *vertex, const dgInt32 *index, dgInt32 indexCount, dgInt32 strideInFloat,
209  const dgPlane &localPlane, dgVector *const contactsOut) const;
210 
211 
212  virtual void GetCollisionInfo(dgCollisionInfo *info) const;
213  virtual void Serialize(dgSerialize callback, void *const userData) const;
214 
215 #ifdef DG_DEBUG_AABB
216  dgVector BoxSupportMapping(const dgVector &dir) const;
217 #endif
218 
219 protected:
220  dgVector m_boxSize;
221  dgVector m_boxOrigin;
222 
223  dgMemoryAllocator *m_allocator;
224  dgCollisionMeshCollisionCallback m_debugCallback;
225 // dgCollisionMeshUserRayCastCallback m_userRayCastCallback;
226  dgCollisionConvexPolygon *m_polygon[DG_MAXIMUN_THREADS];
227 
228  friend class dgWorld;
229 };
230 
231 
232 
233 #endif
234 
Definition: dgPlane.h:29
Definition: dgCollision.h:73
Definition: dgBody.h:85
Definition: dgCollisionMesh.h:39
Definition: dgVector.h:86
Definition: dgCollisionMesh.h:92
Definition: dgContact.h:88
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: dgCollisionMesh.h:73
Definition: dgCollisionMesh.h:101
Definition: dgCollisionMesh.h:90
Definition: dgCollisionConvex.h:39
Definition: dgCollisionMesh.h:113
Definition: dgCollision.h:178
Definition: dgMatrix.h:41
Definition: dgMemory.h:80
Definition: dgWorld.h:118