Skip to content

Commit eb5ad92

Browse files
Refactor TriangleMesh and ConcaveMeshShape
1 parent 01311b3 commit eb5ad92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+741
-712
lines changed

CHANGELOG.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@
1515
- The PolyhedronMesh class has been renamed to ConvexMesh
1616
- The PhysicsCommon::createPolyhedronMesh() method has been renamed to PhysicsCommon::createConvexMesh()
1717
- The PhysicsCommon::destroyPolyhedronMesh() method has been renamed to PhysicsCommon::destroyConvexMesh()
18-
- The PhysicsCommon::createConvexMesh() nows returns a list of errors that might have happened during the mesh creation
18+
- The PhysicsCommon::createConvexMesh() nows outputs a list of errors that might have happened during the mesh creation
1919
- The PhysicsCommon::createConvexMesh() method now takes a reference to PolygonVertexArray
20-
- The input data (vertices, indices, ...) are now copied into the ConvexMesh, TriangularMesh and HeighField and not shared anymore.
20+
- The input data (vertices, indices, ...) are now copied into the ConvexMesh, TriangularMesh and HeighField and not shared anymore
21+
- The PhysicsCommon::createTriangleMesh() method now directly takes a TriangleVertexArray
22+
- The PhysicsCommon::createTriangleMesh() nows outputs a list of errors that might have happened during the mesh creation
23+
- The signature of the TriangleVertexArray::getTriangleVerticesIndices() method has changed
24+
- The signature of the TriangleVertexArray::getNormal() method has changed
25+
- The getLocalBounds() methods of the collision shapes now returns an AABB
2126

2227
### Removed
2328

29+
- The TriangleMesh does not support adding multiple parts of a mesh anymore.
30+
- The TriangleMesh::addSubpart() method has been removed. The PhysicsCommon::createTriangleMesh() method should be used instead
31+
- The TriangleMesh::getSubpart() method has been removed.
32+
- The TriangleMesh::getNbSubparts() method has been removed.
33+
34+
2435
### Fixed
2536

2637
- Issue [#237](https://github.com/DanielChappuis/reactphysics3d/issues/237) Wrong assert has been removed

include/reactphysics3d/collision/ConvexMesh.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// Libraries
3030
#include <reactphysics3d/mathematics/mathematics.h>
3131
#include <reactphysics3d/containers/Array.h>
32+
#include <reactphysics3d/collision/shapes/AABB.h>
3233
#include "HalfEdgeStructure.h"
3334

3435
namespace reactphysics3d {
@@ -55,7 +56,7 @@ class ConvexMesh {
5556
/// Half-edge structure of the mesh
5657
HalfEdgeStructure mHalfEdgeStructure;
5758

58-
// All the vertices of the mesh
59+
/// All the vertices of the mesh
5960
Array<Vector3> mVertices;
6061

6162
/// Array with the face normals
@@ -64,11 +65,8 @@ class ConvexMesh {
6465
/// Centroid of the mesh
6566
Vector3 mCentroid;
6667

67-
/// Mesh minimum bounds in the three local x, y and z directions
68-
Vector3 mMinBounds;
69-
70-
/// Mesh maximum bounds in the three local x, y and z directions
71-
Vector3 mMaxBounds;
68+
/// Mesh minimum/maximum bounds in the three local x, y and z directions
69+
AABB mBounds;
7270

7371
/// Volume of the mesh
7472
decimal mVolume;
@@ -121,11 +119,8 @@ class ConvexMesh {
121119
/// Return the centroid of the mesh
122120
const Vector3& getCentroid() const;
123121

124-
/// Return the minimum bounds of the mesh in the x,y,z direction
125-
const Vector3& getMinBounds() const;
126-
127-
/// Return the maximum bounds of the mesh in the x,y,z direction
128-
const Vector3& getMaxBounds() const;
122+
/// Return the bounds of the mesh in the x,y,z direction
123+
const AABB& getBounds() const;
129124

130125
/// Compute and return the volume of the mesh
131126
decimal getVolume() const;
@@ -190,22 +185,6 @@ RP3D_FORCE_INLINE const Vector3& ConvexMesh::getCentroid() const {
190185
return mCentroid;
191186
}
192187

193-
// Return the minimum bounds of the mesh in the x,y,z direction
194-
/**
195-
* @return The three mimimum bounds of the mesh in the x,y,z direction
196-
*/
197-
RP3D_FORCE_INLINE const Vector3& ConvexMesh::getMinBounds() const {
198-
return mMinBounds;
199-
}
200-
201-
// Return the maximum bounds of the mesh in the x,y,z direction
202-
/**
203-
* @return The three maximum bounds of the mesh in the x,y,z direction
204-
*/
205-
RP3D_FORCE_INLINE const Vector3& ConvexMesh::getMaxBounds() const {
206-
return mMaxBounds;
207-
}
208-
209188
// Return the volume of the convex mesh
210189
/**
211190
* @return The volume of the mesh
@@ -226,7 +205,7 @@ RP3D_FORCE_INLINE Vector3 ConvexMesh::getLocalInertiaTensor(decimal mass, Vector
226205
// TODO: We should compute a much better inertia tensor here (not using a box)
227206

228207
const decimal factor = (decimal(1.0) / decimal(3.0)) * mass;
229-
const Vector3 realExtent = decimal(0.5) * scale * (getMaxBounds() - getMinBounds());
208+
const Vector3 realExtent = decimal(0.5) * scale * (mBounds.getMax() - mBounds.getMin());
230209
assert(realExtent.x > 0 && realExtent.y > 0 && realExtent.z > 0);
231210
const decimal xSquare = realExtent.x * realExtent.x;
232211
const decimal ySquare = realExtent.y * realExtent.y;

include/reactphysics3d/collision/RaycastInfo.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ struct RaycastInfo {
6060
/// The hit point "p" is such that p = point1 + hitFraction * (point2 - point1)
6161
decimal hitFraction;
6262

63-
/// Mesh subpart index that has been hit (only used for triangles mesh and -1 otherwise)
64-
int meshSubpart;
65-
6663
/// Hit triangle index (only used for triangles mesh and -1 otherwise)
6764
int triangleIndex;
6865

@@ -75,7 +72,7 @@ struct RaycastInfo {
7572
// -------------------- Methods -------------------- //
7673

7774
/// Constructor
78-
RaycastInfo() : meshSubpart(-1), triangleIndex(-1), body(nullptr), collider(nullptr) {
75+
RaycastInfo() : triangleIndex(-1), body(nullptr), collider(nullptr) {
7976

8077
}
8178

include/reactphysics3d/collision/TriangleMesh.h

Lines changed: 109 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,74 +30,148 @@
3030
#include <cassert>
3131
#include <reactphysics3d/containers/Array.h>
3232
#include <reactphysics3d/memory/MemoryAllocator.h>
33+
#include <reactphysics3d/collision/broadphase/DynamicAABBTree.h>
3334

3435
namespace reactphysics3d {
3536

3637
// Declarations
3738
class TriangleVertexArray;
39+
struct Error;
3840

3941
// Class TriangleMesh
4042
/**
41-
* This class represents a mesh made of triangles. A TriangleMesh contains
42-
* one or several parts. Each part is a set of triangles represented in a
43-
* TriangleVertexArray object describing all the triangles vertices of the part.
44-
* A TriangleMesh object can be used to create a ConcaveMeshShape from a triangle
45-
* mesh for instance.
43+
* This class represents a mesh made of triangles.
44+
* A single TriangleMesh object can be used to create one or many ConcaveMeshShape (with
45+
* different scaling for instance).
4646
*/
4747
class TriangleMesh {
4848

4949
protected:
5050

51-
/// All the triangle arrays of the mesh (one triangle array per part)
52-
Array<TriangleVertexArray*> mTriangleArrays;
51+
/// All the vertices of the mesh
52+
Array<Vector3> mVertices;
53+
54+
/// The three vertices indices for each triangle face of the mesh
55+
Array<uint32> mTriangles;
56+
57+
/// The normal vector at each vertex of the mesh
58+
Array<Vector3> mVerticesNormals;
59+
60+
/// Dynamic AABB tree to accelerate collision with the triangles
61+
DynamicAABBTree mDynamicAABBTree;
5362

5463
/// Constructor
5564
TriangleMesh(reactphysics3d::MemoryAllocator& allocator);
5665

66+
/// Copy the vertices into the mesh
67+
bool copyVertices(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
68+
69+
/// Copy or compute the vertices normals
70+
bool copyOrComputeVerticesNormals(const TriangleVertexArray& triangleVertexArray,
71+
std::vector<Error>& errors);
72+
73+
/// Copy the triangles into the mesh
74+
bool copyTriangles(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
75+
76+
/// Insert all the triangles into the dynamic AABB tree
77+
void initBVHTree();
78+
79+
/// Initialize the mesh using a TriangleVertexArray
80+
bool init(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
81+
82+
/// Report all shapes overlapping with the AABB given in parameter.
83+
void reportAllShapesOverlappingWithAABB(const AABB& aabb, Array<int32>& overlappingNodes);
84+
85+
/// Return the integer data of leaf node of the dynamic AABB tree
86+
int32 getDynamicAABBTreeNodeDataInt(int32 nodeID) const;
87+
88+
/// Ray casting method
89+
void raycast(const Ray& ray, DynamicAABBTreeRaycastCallback& callback) const;
90+
5791
public:
5892

59-
/// Destructor
60-
~TriangleMesh();
93+
/// Return the number of vertices in the mesh
94+
uint32 getNbVertices() const;
95+
96+
/// Return the number of triangles faces of the mesh
97+
uint32 getNbTriangles() const;
6198

62-
/// Add a subpart of the mesh
63-
void addSubpart(TriangleVertexArray* triangleVertexArray);
99+
/// Return the bounds of the mesh in the x,y,z direction
100+
const AABB& getBounds() const;
64101

65-
/// Return a pointer to a given subpart (triangle vertex array) of the mesh
66-
TriangleVertexArray* getSubpart(uint32 indexSubpart) const;
102+
/// Return the three vertex indices of a given triangle face
103+
void getTriangleVerticesIndices(uint32 triangleIndex, uint32& outV1Index, uint32& outV2Index,
104+
uint32& outV3Index) const;
67105

68-
/// Return the number of subparts of the mesh
69-
uint32 getNbSubparts() const;
106+
/// Return the coordinates of the three vertices of a given triangle face
107+
void getTriangleVertices(uint32 triangleIndex, Vector3& outV1, Vector3& outV2, Vector3& outV3) const;
70108

109+
/// Return the normals of the three vertices of a given triangle face
110+
void getTriangleVerticesNormals(uint32 triangleIndex, Vector3& outN1,
111+
Vector3& outN2, Vector3& outN3) const;
112+
113+
/// Return the coordinates of a given vertex
114+
const Vector3& getVertex(uint32 vertexIndex) const;
115+
116+
/// Return the normal of a given vertex
117+
const Vector3& getVertexNormal(uint32 vertexIndex) const;
71118

72119
// ---------- Friendship ---------- //
73120

74121
friend class PhysicsCommon;
122+
friend class ConcaveMeshShape;
75123
};
76124

77-
// Add a subpart of the mesh
78-
/**
79-
* @param triangleVertexArray Pointer to the TriangleVertexArray to add into the mesh
80-
*/
81-
RP3D_FORCE_INLINE void TriangleMesh::addSubpart(TriangleVertexArray* triangleVertexArray) {
82-
mTriangleArrays.add(triangleVertexArray );
125+
// Return the number of vertices in the mesh
126+
RP3D_FORCE_INLINE uint32 TriangleMesh::getNbVertices() const {
127+
return mVertices.size();
83128
}
84129

85-
// Return a pointer to a given subpart (triangle vertex array) of the mesh
86-
/**
87-
* @param indexSubpart The index of the sub-part of the mesh
88-
* @return A pointer to the triangle vertex array of a given sub-part of the mesh
89-
*/
90-
RP3D_FORCE_INLINE TriangleVertexArray* TriangleMesh::getSubpart(uint32 indexSubpart) const {
91-
assert(indexSubpart < mTriangleArrays.size());
92-
return mTriangleArrays[indexSubpart];
130+
// Return the number of triangles faces of the mesh
131+
RP3D_FORCE_INLINE uint32 TriangleMesh::getNbTriangles() const {
132+
return mTriangles.size() / 3;
93133
}
94134

95-
// Return the number of sub-parts of the mesh
96-
/**
97-
* @return The number of sub-parts of the mesh
98-
*/
99-
RP3D_FORCE_INLINE uint32 TriangleMesh::getNbSubparts() const {
100-
return static_cast<uint32>(mTriangleArrays.size());
135+
// Return the three vertex indices of a given triangle face
136+
RP3D_FORCE_INLINE void TriangleMesh::getTriangleVerticesIndices(uint32 triangleIndex, uint32& outV1Index,
137+
uint32& outV2Index, uint32& outV3Index) const {
138+
assert(triangleIndex < mTriangles.size() / 3);
139+
140+
outV1Index = mTriangles[triangleIndex * 3];
141+
outV2Index = mTriangles[triangleIndex * 3 + 1];
142+
outV3Index = mTriangles[triangleIndex * 3 + 2];
143+
}
144+
145+
// Return the coordinates of the three vertices of a given triangle face
146+
RP3D_FORCE_INLINE void TriangleMesh::getTriangleVertices(uint32 triangleIndex, Vector3& outV1, Vector3& outV2,
147+
Vector3& outV3) const {
148+
assert(triangleIndex < mTriangles.size() / 3);
149+
150+
outV1 = mVertices[mTriangles[triangleIndex * 3]];
151+
outV2 = mVertices[mTriangles[triangleIndex * 3 + 1]];
152+
outV3 = mVertices[mTriangles[triangleIndex * 3 + 2]];
153+
}
154+
155+
// Return the normals of the three vertices of a given triangle face
156+
RP3D_FORCE_INLINE void TriangleMesh::getTriangleVerticesNormals(uint32 triangleIndex, Vector3& outN1,
157+
Vector3& outN2, Vector3& outN3) const {
158+
assert(triangleIndex < mTriangles.size() / 3);
159+
160+
outN1 = mVerticesNormals[mTriangles[triangleIndex * 3]];
161+
outN2 = mVerticesNormals[mTriangles[triangleIndex * 3 + 1]];
162+
outN3 = mVerticesNormals[mTriangles[triangleIndex * 3 + 2]];
163+
}
164+
165+
// Return the coordinates of a given vertex
166+
RP3D_FORCE_INLINE const Vector3& TriangleMesh::getVertex(uint32 vertexIndex) const {
167+
assert(vertexIndex < mVertices.size());
168+
return mVertices[vertexIndex];
169+
}
170+
171+
// Return the normal of a given vertex
172+
RP3D_FORCE_INLINE const Vector3& TriangleMesh::getVertexNormal(uint32 vertexIndex) const {
173+
assert(vertexIndex < mVertices.size());
174+
return mVerticesNormals[vertexIndex];
101175
}
102176

103177
}

include/reactphysics3d/collision/TriangleVertexArray.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ struct Vector3;
3939
* This class is used to describe the vertices and faces of a triangular mesh.
4040
* A TriangleVertexArray represents a continuous array of vertices and indexes
4141
* of a triangular mesh. When you create a TriangleVertexArray, no data is copied
42-
* into the array. It only stores pointer to the data. The purpose is to allow
43-
* the user to share vertices data between the physics engine and the rendering
42+
* into the array. It only stores pointer to the data.
4443
* part. Therefore, make sure that the data pointed by a TriangleVertexArray
4544
* remains valid during the TriangleVertexArray life.
4645
*/
@@ -97,13 +96,10 @@ class TriangleVertexArray {
9796
IndexDataType mIndexDataType;
9897

9998
/// True if the vertices normals are provided by the user
100-
bool mAreVerticesNormalsProvidedByUser;
99+
bool mHasNormals;
101100

102101
// -------------------- Methods -------------------- //
103102

104-
/// Compute the vertices normals when they are not provided by the user
105-
void computeVerticesNormals();
106-
107103
public:
108104

109105
// -------------------- Methods -------------------- //
@@ -120,9 +116,6 @@ class TriangleVertexArray {
120116
VertexDataType vertexDataType, NormalDataType normalDataType,
121117
IndexDataType indexDataType);
122118

123-
/// Destructor
124-
~TriangleVertexArray();
125-
126119
/// Deleted assignment operator
127120
TriangleVertexArray& operator=(const TriangleVertexArray& triangleVertexArray) = delete;
128121

@@ -135,6 +128,9 @@ class TriangleVertexArray {
135128
/// Return the vertex normal data type
136129
NormalDataType getVertexNormalDataType() const;
137130

131+
/// Return true if the vertices normal have been provided by the user
132+
bool getHasNormals() const;
133+
138134
/// Return the index data type
139135
IndexDataType getIndexDataType() const;
140136

@@ -162,20 +158,15 @@ class TriangleVertexArray {
162158
/// Return the pointer to the start of the indices array
163159
const void* getIndicesStart() const;
164160

165-
/// Return the vertices coordinates of a triangle
166-
void getTriangleVertices(uint32 triangleIndex, Vector3* outTriangleVertices) const;
167-
168-
/// Return the three vertices normals of a triangle
169-
void getTriangleVerticesNormals(uint32 triangleIndex, Vector3* outTriangleVerticesNormals) const;
170-
171-
/// Return the indices of the three vertices of a given triangle in the array
172-
void getTriangleVerticesIndices(uint32 triangleIndex, uint32* outVerticesIndices) const;
161+
/// Return the three vertex indices of a given triangle face
162+
void getTriangleVerticesIndices(uint32 triangleIndex, uint32& outV1Index, uint32& outV2Index,
163+
uint32& outV3Index) const;
173164

174165
/// Return a vertex of the array
175-
void getVertex(uint32 vertexIndex, Vector3* outVertex);
166+
Vector3 getVertex(uint32 vertexIndex) const;
176167

177168
/// Return a vertex normal of the array
178-
void getNormal(uint32 vertexIndex, Vector3* outNormal);
169+
Vector3 getVertexNormal(uint32 vertexIndex) const;
179170
};
180171

181172
// Return the vertex data type
@@ -266,6 +257,14 @@ RP3D_FORCE_INLINE const void* TriangleVertexArray::getIndicesStart() const {
266257
return mIndicesStart;
267258
}
268259

260+
// Return true if the vertices normals have been provided by the user
261+
/**
262+
* @return True if the vertices normals have been provided by the user
263+
*/
264+
RP3D_FORCE_INLINE bool TriangleVertexArray::getHasNormals() const {
265+
return mHasNormals;
266+
}
267+
269268
}
270269

271270
#endif

0 commit comments

Comments
 (0)