Skip to content

Commit 1ab72e2

Browse files
Move mMinBounds and mMaxBounds from ConvexMeshShape into ConvexMesh
1 parent 1029e02 commit 1ab72e2

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

include/reactphysics3d/collision/ConvexMesh.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class ConvexMesh {
6464
/// Centroid of the mesh
6565
Vector3 mCentroid;
6666

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;
72+
6773
// -------------------- Methods -------------------- //
6874

6975
/// Constructor
@@ -107,7 +113,13 @@ class ConvexMesh {
107113
const HalfEdgeStructure& getHalfEdgeStructure() const;
108114

109115
/// Return the centroid of the mesh
110-
Vector3 getCentroid() const;
116+
const Vector3& getCentroid() const;
117+
118+
/// Return the minimum bounds of the mesh in the x,y,z direction
119+
const Vector3& getMinBounds() const;
120+
121+
/// Return the maximum bounds of the mesh in the x,y,z direction
122+
const Vector3& getMaxBounds() const;
111123

112124
/// Compute and return the volume of the mesh
113125
decimal getVolume() const;
@@ -165,10 +177,26 @@ RP3D_FORCE_INLINE const HalfEdgeStructure& ConvexMesh::getHalfEdgeStructure() co
165177
/**
166178
* @return The centroid of the mesh
167179
*/
168-
RP3D_FORCE_INLINE Vector3 ConvexMesh::getCentroid() const {
180+
RP3D_FORCE_INLINE const Vector3& ConvexMesh::getCentroid() const {
169181
return mCentroid;
170182
}
171183

184+
// Return the minimum bounds of the mesh in the x,y,z direction
185+
/**
186+
* @return The three mimimum bounds of the mesh in the x,y,z direction
187+
*/
188+
RP3D_FORCE_INLINE const Vector3& ConvexMesh::getMinBounds() const {
189+
return mMinBounds;
190+
}
191+
192+
// Return the maximum bounds of the mesh in the x,y,z direction
193+
/**
194+
* @return The three maximum bounds of the mesh in the x,y,z direction
195+
*/
196+
RP3D_FORCE_INLINE const Vector3& ConvexMesh::getMaxBounds() const {
197+
return mMaxBounds;
198+
}
199+
172200
}
173201

174202
#endif

include/reactphysics3d/collision/shapes/ConvexMeshShape.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,14 @@ class ConvexMeshShape : public ConvexPolyhedronShape {
5454
/// Convex mesh
5555
ConvexMesh* mConvexMesh;
5656

57-
// TODO : Maybe store min/max bounds in the ConvexMesh instead
58-
59-
/// Mesh minimum bounds in the three local x, y and z directions
60-
Vector3 mMinBounds;
61-
62-
/// Mesh maximum bounds in the three local x, y and z directions
63-
Vector3 mMaxBounds;
64-
65-
/// Scale of the mesh
57+
/// Scale to apply to the mesh
6658
Vector3 mScale;
6759

6860
// -------------------- Methods -------------------- //
6961

7062
/// Constructor
7163
ConvexMeshShape(ConvexMesh* convexMesh, MemoryAllocator& allocator, const Vector3& scale = Vector3(1,1,1));
7264

73-
/// Recompute the bounds of the mesh
74-
void recalculateBounds();
75-
7665
/// Return a local support point in a given direction without the object margin.
7766
virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction) const override;
7867

@@ -163,7 +152,6 @@ RP3D_FORCE_INLINE const Vector3& ConvexMeshShape::getScale() const {
163152
/// after changing the scale of a collision shape
164153
RP3D_FORCE_INLINE void ConvexMeshShape::setScale(const Vector3& scale) {
165154
mScale = scale;
166-
recalculateBounds();
167155
notifyColliderAboutChangedSize();
168156
}
169157

@@ -173,8 +161,8 @@ RP3D_FORCE_INLINE void ConvexMeshShape::setScale(const Vector3& scale) {
173161
* @param max The maximum bounds of the shape in local-space coordinates
174162
*/
175163
RP3D_FORCE_INLINE void ConvexMeshShape::getLocalBounds(Vector3& min, Vector3& max) const {
176-
min = mMinBounds;
177-
max = mMaxBounds;
164+
min = mConvexMesh->getMinBounds() * mScale;
165+
max = mConvexMesh->getMaxBounds() * mScale;
178166
}
179167

180168
// Return the local inertia tensor of the collision shape.
@@ -184,8 +172,11 @@ RP3D_FORCE_INLINE void ConvexMeshShape::getLocalBounds(Vector3& min, Vector3& ma
184172
* @param mass Mass to use to compute the inertia tensor of the collision shape
185173
*/
186174
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+
187178
const decimal factor = (decimal(1.0) / decimal(3.0)) * mass;
188-
const Vector3 realExtent = decimal(0.5) * (mMaxBounds - mMinBounds);
179+
const Vector3 realExtent = decimal(0.5) * mScale * (mConvexMesh->getMaxBounds() - mConvexMesh->getMinBounds());
189180
assert(realExtent.x > 0 && realExtent.y > 0 && realExtent.z > 0);
190181
const decimal xSquare = realExtent.x * realExtent.x;
191182
const decimal ySquare = realExtent.y * realExtent.y;

src/collision/ConvexMesh.cpp

Lines changed: 12 additions & 1 deletion
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) {
45+
mVertices(allocator), mFacesNormals(allocator), mMinBounds(0, 0, 0), mMaxBounds(0, 0, 0) {
4646

4747
}
4848

@@ -82,12 +82,23 @@ bool ConvexMesh::copyVertices(const PolygonVertexArray& polygonVertexArray, std:
8282
bool isValid = true;
8383

8484
mCentroid.setToZero();
85+
mMinBounds = polygonVertexArray.getVertex(0);
86+
mMaxBounds = polygonVertexArray.getVertex(0);
8587

8688
for (uint32 i=0 ; i < polygonVertexArray.getNbVertices(); i++) {
8789

8890
const Vector3 vertex = polygonVertexArray.getVertex(i);
8991
mVertices.add(vertex);
9092
mCentroid += vertex;
93+
94+
if (vertex.x > mMaxBounds.x) mMaxBounds.x = vertex.x;
95+
if (vertex.x < mMinBounds.x) mMinBounds.x = vertex.x;
96+
97+
if (vertex.y > mMaxBounds.y) mMaxBounds.y = vertex.y;
98+
if (vertex.y < mMinBounds.y) mMinBounds.y = vertex.y;
99+
100+
if (vertex.z > mMaxBounds.z) mMaxBounds.z = vertex.z;
101+
if (vertex.z < mMinBounds.z) mMinBounds.z = vertex.z;
91102
}
92103

93104
if (getNbVertices() > 0) {

src/collision/shapes/ConvexMeshShape.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ using namespace reactphysics3d;
4141
*/
4242
ConvexMeshShape::ConvexMeshShape(ConvexMesh* convexMesh, MemoryAllocator& allocator, const Vector3& scale)
4343
: ConvexPolyhedronShape(CollisionShapeName::CONVEX_MESH, allocator), mConvexMesh(convexMesh),
44-
mMinBounds(0, 0, 0), mMaxBounds(0, 0, 0), mScale(scale) {
44+
mScale(scale) {
4545

46-
// Recalculate the bounds of the mesh
47-
recalculateBounds();
4846
}
4947

5048
// Return a local support point in a given direction without the object margin.
@@ -79,30 +77,6 @@ Vector3 ConvexMeshShape::getLocalSupportPointWithoutMargin(const Vector3& direct
7977
return mConvexMesh->getVertex(indexMaxDotProduct) * mScale;
8078
}
8179

82-
// Recompute the bounds of the mesh
83-
void ConvexMeshShape::recalculateBounds() {
84-
85-
mMinBounds = mConvexMesh->getVertex(0);
86-
mMaxBounds = mConvexMesh->getVertex(0);
87-
88-
// For each vertex of the mesh
89-
for (uint32 i=1; i<mConvexMesh->getNbVertices(); i++) {
90-
91-
if (mConvexMesh->getVertex(i).x > mMaxBounds.x) mMaxBounds.x = mConvexMesh->getVertex(i).x;
92-
if (mConvexMesh->getVertex(i).x < mMinBounds.x) mMinBounds.x = mConvexMesh->getVertex(i).x;
93-
94-
if (mConvexMesh->getVertex(i).y > mMaxBounds.y) mMaxBounds.y = mConvexMesh->getVertex(i).y;
95-
if (mConvexMesh->getVertex(i).y < mMinBounds.y) mMinBounds.y = mConvexMesh->getVertex(i).y;
96-
97-
if (mConvexMesh->getVertex(i).z > mMaxBounds.z) mMaxBounds.z = mConvexMesh->getVertex(i).z;
98-
if (mConvexMesh->getVertex(i).z < mMinBounds.z) mMinBounds.z = mConvexMesh->getVertex(i).z;
99-
}
100-
101-
// Apply the local scaling factor
102-
mMaxBounds = mMaxBounds * mScale;
103-
mMinBounds = mMinBounds * mScale;
104-
}
105-
10680
// Raycast method with feedback information
10781
/// This method implements the technique in the book "Real-time Collision Detection" by
10882
/// Christer Ericson.

0 commit comments

Comments
 (0)