Skip to content

Commit ba531e2

Browse files
Working on ConvexMesh
1 parent dd0111f commit ba531e2

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

include/reactphysics3d/collision/ConvexMesh.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class ConvexMesh {
7070
/// Mesh maximum bounds in the three local x, y and z directions
7171
Vector3 mMaxBounds;
7272

73+
/// Volume of the mesh
74+
decimal mVolume;
75+
7376
// -------------------- Methods -------------------- //
7477

7578
/// Constructor
@@ -90,6 +93,9 @@ class ConvexMesh {
9093
/// Compute and return the face normal (not normalized)
9194
Vector3 computeFaceNormal(uint32 faceIndex) const;
9295

96+
/// Compute the volume of the mesh
97+
void computeVolume();
98+
9399
/// Static factory method to create a convex mesh
94100
static ConvexMesh* create(MemoryAllocator& allocator);
95101

@@ -124,6 +130,9 @@ class ConvexMesh {
124130
/// Compute and return the volume of the mesh
125131
decimal getVolume() const;
126132

133+
/// Return the local inertia tensor of the mesh
134+
Vector3 getLocalInertiaTensor(decimal mass, Vector3 scale) const;
135+
127136
// ---------- Friendship ---------- //
128137

129138
friend class PhysicsCommon;
@@ -197,6 +206,34 @@ RP3D_FORCE_INLINE const Vector3& ConvexMesh::getMaxBounds() const {
197206
return mMaxBounds;
198207
}
199208

209+
// Return the volume of the convex mesh
210+
/**
211+
* @return The volume of the mesh
212+
*/
213+
RP3D_FORCE_INLINE decimal ConvexMesh::getVolume() const {
214+
return mVolume;
215+
}
216+
217+
// Return the local inertia tensor of the mesh
218+
/// The local inertia tensor of the convex mesh is approximated using the inertia tensor
219+
/// of its bounding box.
220+
/**
221+
* @param mass Mass to use to compute the inertia tensor of the collision shape
222+
* @param scale Scaling factor for the mesh
223+
*/
224+
RP3D_FORCE_INLINE Vector3 ConvexMesh::getLocalInertiaTensor(decimal mass, Vector3 scale) const {
225+
226+
// TODO: We should compute a much better inertia tensor here (not using a box)
227+
228+
const decimal factor = (decimal(1.0) / decimal(3.0)) * mass;
229+
const Vector3 realExtent = decimal(0.5) * scale * (getMaxBounds() - getMinBounds());
230+
assert(realExtent.x > 0 && realExtent.y > 0 && realExtent.z > 0);
231+
const decimal xSquare = realExtent.x * realExtent.x;
232+
const decimal ySquare = realExtent.y * realExtent.y;
233+
const decimal zSquare = realExtent.z * realExtent.z;
234+
return Vector3(factor * (ySquare + zSquare), factor * (xSquare + zSquare), factor * (xSquare + ySquare));
235+
}
236+
200237
}
201238

202239
#endif

include/reactphysics3d/collision/shapes/ConvexMeshShape.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,7 @@ RP3D_FORCE_INLINE void ConvexMeshShape::getLocalBounds(Vector3& min, Vector3& ma
172172
* @param mass Mass to use to compute the inertia tensor of the collision shape
173173
*/
174174
RP3D_FORCE_INLINE Vector3 ConvexMeshShape::getLocalInertiaTensor(decimal mass) const {
175-
176-
// TODO: We should compute a much better inertia tensor here (not using a box)
177-
178-
const decimal factor = (decimal(1.0) / decimal(3.0)) * mass;
179-
const Vector3 realExtent = decimal(0.5) * mScale * (mConvexMesh->getMaxBounds() - mConvexMesh->getMinBounds());
180-
assert(realExtent.x > 0 && realExtent.y > 0 && realExtent.z > 0);
181-
const decimal xSquare = realExtent.x * realExtent.x;
182-
const decimal ySquare = realExtent.y * realExtent.y;
183-
const decimal zSquare = realExtent.z * realExtent.z;
184-
return Vector3(factor * (ySquare + zSquare), factor * (xSquare + zSquare), factor * (xSquare + ySquare));
175+
return mConvexMesh->getLocalInertiaTensor(mass, mScale);
185176
}
186177

187178
// Return the number of faces of the mesh
@@ -236,7 +227,7 @@ RP3D_FORCE_INLINE Vector3 ConvexMeshShape::getCentroid() const {
236227

237228
// Compute and return the volume of the collision shape
238229
RP3D_FORCE_INLINE decimal ConvexMeshShape::getVolume() const {
239-
return mConvexMesh->getVolume();
230+
return mConvexMesh->getVolume() * mScale.x * mScale.y * mScale.z;
240231
}
241232

242233
}

src/collision/ConvexMesh.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ using namespace reactphysics3d;
4242
*/
4343
ConvexMesh::ConvexMesh(MemoryAllocator& allocator)
4444
: mMemoryAllocator(allocator), mHalfEdgeStructure(allocator, 6, 8, 24),
45-
mVertices(allocator), mFacesNormals(allocator), mMinBounds(0, 0, 0), mMaxBounds(0, 0, 0) {
45+
mVertices(allocator), mFacesNormals(allocator), mMinBounds(0, 0, 0), mMaxBounds(0, 0, 0), mVolume(0) {
4646

4747
}
4848

@@ -73,6 +73,9 @@ bool ConvexMesh::init(const PolygonVertexArray& polygonVertexArray, std::vector<
7373
// Compute the faces normals
7474
isValid &= computeFacesNormals(errors);
7575

76+
// Compute the volume of the mesh
77+
computeVolume();
78+
7679
return isValid;
7780
}
7881

@@ -206,19 +209,6 @@ bool ConvexMesh::computeFacesNormals(std::vector<Error>& errors) {
206209

207210
if (face.faceVertices.size() >= 3) {
208211

209-
auto test0 = face.faceVertices[0];
210-
auto test1 = face.faceVertices[1];
211-
auto test2 = face.faceVertices[2];
212-
213-
auto v0 = getVertex(face.faceVertices[0]);
214-
auto v1 = getVertex(face.faceVertices[1]);
215-
auto v2 = getVertex(face.faceVertices[2]);
216-
auto v3 = Vector3(0, 0, 0);
217-
if (face.faceVertices.size() > 3) {
218-
219-
v3 = getVertex(face.faceVertices[3]);
220-
}
221-
222212
mFacesNormals.add(computeFaceNormal(f));
223213
decimal normalLength = mFacesNormals[f].length();
224214

@@ -258,9 +248,9 @@ Vector3 ConvexMesh::computeFaceNormal(uint32 faceIndex) const {
258248
return normal;
259249
}
260250

261-
// Compute and return the volume of the convex mesh
251+
// Compute the volume of the convex mesh
262252
/// We use the divergence theorem to compute the volume of the convex mesh using a sum over its faces.
263-
decimal ConvexMesh::getVolume() const {
253+
void ConvexMesh::computeVolume() {
264254

265255
decimal sum = 0.0;
266256

@@ -275,5 +265,5 @@ decimal ConvexMesh::getVolume() const {
275265
sum += faceVertex.dot(faceNormal) * faceArea;
276266
}
277267

278-
return std::abs(sum) / decimal(3.0);
268+
mVolume = std::abs(sum) / decimal(3.0);
279269
}

testbed/common/ConvexMesh.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,3 @@ int ConvexMesh::findVertexIndex(const std::vector<openglframework::Vector3>& ver
263263

264264
return -1;
265265
}
266-

0 commit comments

Comments
 (0)