Skip to content

Commit e22ba24

Browse files
Working on TriangleMesh
1 parent eb5ad92 commit e22ba24

File tree

23 files changed

+322
-174
lines changed

23 files changed

+322
-174
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ set (REACTPHYSICS3D_HEADERS
145145
"include/reactphysics3d/containers/Deque.h"
146146
"include/reactphysics3d/utils/Profiler.h"
147147
"include/reactphysics3d/utils/Logger.h"
148-
"include/reactphysics3d/utils/Error.h"
148+
"include/reactphysics3d/utils/Message.h"
149149
"include/reactphysics3d/utils/DefaultLogger.h"
150150
"include/reactphysics3d/utils/DebugRenderer.h"
151151
"include/reactphysics3d/utils/quickhull/QuickHull.h"

include/reactphysics3d/collision/ConvexMesh.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace reactphysics3d {
3737
// Declarations
3838
class DefaultAllocator;
3939
class PolygonVertexArray;
40-
struct Error;
40+
struct Message;
4141

4242
// Class ConvexMesh
4343
/**
@@ -77,16 +77,16 @@ class ConvexMesh {
7777
ConvexMesh(MemoryAllocator& allocator);
7878

7979
/// Initialize a mesh and returns errors if any
80-
bool init(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors);
80+
bool init(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors);
8181

8282
/// Copy the vertices into the mesh
83-
bool copyVertices(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors);
83+
bool copyVertices(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors);
8484

8585
/// Create the half-edge structure of the mesh
86-
bool createHalfEdgeStructure(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors);
86+
bool createHalfEdgeStructure(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors);
8787

8888
/// Compute the faces normals
89-
bool computeFacesNormals(std::vector<Error>& errors);
89+
bool computeFacesNormals(std::vector<Message>& errors);
9090

9191
/// Compute and return the face normal (not normalized)
9292
Vector3 computeFaceNormal(uint32 faceIndex) const;

include/reactphysics3d/collision/TriangleMesh.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
#include <reactphysics3d/containers/Array.h>
3232
#include <reactphysics3d/memory/MemoryAllocator.h>
3333
#include <reactphysics3d/collision/broadphase/DynamicAABBTree.h>
34+
#include <reactphysics3d/containers/Map.h>
3435

3536
namespace reactphysics3d {
3637

3738
// Declarations
3839
class TriangleVertexArray;
39-
struct Error;
40+
struct Message;
4041

4142
// Class TriangleMesh
4243
/**
@@ -48,6 +49,9 @@ class TriangleMesh {
4849

4950
protected:
5051

52+
/// Reference to the memory allocator
53+
MemoryAllocator& mAllocator;
54+
5155
/// All the vertices of the mesh
5256
Array<Vector3> mVertices;
5357

@@ -60,24 +64,29 @@ class TriangleMesh {
6064
/// Dynamic AABB tree to accelerate collision with the triangles
6165
DynamicAABBTree mDynamicAABBTree;
6266

67+
/// Epsilon value for this mesh
68+
decimal mEpsilon;
69+
6370
/// Constructor
6471
TriangleMesh(reactphysics3d::MemoryAllocator& allocator);
6572

6673
/// Copy the vertices into the mesh
67-
bool copyVertices(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
74+
bool copyVertices(const TriangleVertexArray& triangleVertexArray, std::vector<Message>& messages);
6875

6976
/// Copy or compute the vertices normals
70-
bool copyOrComputeVerticesNormals(const TriangleVertexArray& triangleVertexArray,
71-
std::vector<Error>& errors);
77+
void computeVerticesNormals();
7278

7379
/// Copy the triangles into the mesh
74-
bool copyTriangles(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
80+
bool copyData(const TriangleVertexArray& triangleVertexArray, std::vector<Message>& errors);
7581

7682
/// Insert all the triangles into the dynamic AABB tree
7783
void initBVHTree();
7884

7985
/// Initialize the mesh using a TriangleVertexArray
80-
bool init(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
86+
bool init(const TriangleVertexArray& triangleVertexArray, std::vector<Message>& messages);
87+
88+
/// Addd a vertex to the mesh
89+
uint32 addVertex(uint32 userVertexIndex, const Vector3& vertex, Map<uint32, uint32>& mapUserVertexIndexToInternal);
8190

8291
/// Report all shapes overlapping with the AABB given in parameter.
8392
void reportAllShapesOverlappingWithAABB(const AABB& aabb, Array<int32>& overlappingNodes);
@@ -160,6 +169,11 @@ RP3D_FORCE_INLINE void TriangleMesh::getTriangleVerticesNormals(uint32 triangleI
160169
outN1 = mVerticesNormals[mTriangles[triangleIndex * 3]];
161170
outN2 = mVerticesNormals[mTriangles[triangleIndex * 3 + 1]];
162171
outN3 = mVerticesNormals[mTriangles[triangleIndex * 3 + 2]];
172+
173+
// TODO : REMOVE
174+
assert(outN1.length() > MACHINE_EPSILON);
175+
assert(outN2.length() > MACHINE_EPSILON);
176+
assert(outN3.length() > MACHINE_EPSILON);
163177
}
164178

165179
// Return the coordinates of a given vertex

include/reactphysics3d/collision/shapes/TriangleShape.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <reactphysics3d/collision/shapes/AABB.h>
3232
#include <reactphysics3d/collision/shapes/ConvexPolyhedronShape.h>
3333

34+
// TODO : REMOVE
35+
#include <iostream>
36+
3437
/// ReactPhysics3D namespace
3538
namespace reactphysics3d {
3639

@@ -311,6 +314,9 @@ RP3D_FORCE_INLINE Vector3 TriangleShape::computeSmoothLocalContactNormalForTrian
311314

312315
assert(mNormal.length() > decimal(0.0));
313316

317+
// TODO : REMOVE
318+
std::cout << "mNormal: " << mNormal.to_string() << std::endl;
319+
314320
// Compute the barycentric coordinates of the point in the triangle
315321
decimal u, v, w;
316322
computeBarycentricCoordinatesInTriangle(mPoints[0], mPoints[1], mPoints[2], localContactPoint, u, v, w);

include/reactphysics3d/engine/PhysicsCommon.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class PhysicsCommon {
204204
HeightFieldShape* createHeightFieldShape(int nbGridColumns, int nbGridRows, decimal minHeight, decimal maxHeight,
205205
const void* heightFieldData, HeightFieldShape::HeightDataType dataType,
206206
int upAxis = 1, decimal integerHeightScale = 1.0f,
207-
const Vector3& scaling = Vector3(1,1,1));
207+
const Vector3& scaling = Vector3(1,1,1));
208208

209209
/// Destroy a height-field shape
210210
void destroyHeightFieldShape(HeightFieldShape* heightFieldShape);
@@ -216,16 +216,16 @@ class PhysicsCommon {
216216
void destroyConcaveMeshShape(ConcaveMeshShape* concaveMeshShape);
217217

218218
/// Create a convex mesh from a PolygonVertexArray describing vertices and faces
219-
ConvexMesh* createConvexMesh(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors);
219+
ConvexMesh* createConvexMesh(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& messages);
220220

221221
/// Create a convex mesh from an array of vertices (automatically computing the convex hull using QuickHull)
222-
ConvexMesh* createConvexMesh(const VertexArray& vertexArray, std::vector<Error>& errors);
222+
ConvexMesh* createConvexMesh(const VertexArray& vertexArray, std::vector<Message>& messages);
223223

224224
/// Destroy a convex mesh
225225
void destroyConvexMesh(ConvexMesh* convexMesh);
226226

227227
/// Create a triangle mesh
228-
TriangleMesh* createTriangleMesh(const TriangleVertexArray& triangleVertexArray, std::vector<Error>& errors);
228+
TriangleMesh* createTriangleMesh(const TriangleVertexArray& triangleVertexArray, std::vector<Message>& messages);
229229

230230
/// Destroy a triangle mesh
231231
void destroyTriangleMesh(TriangleMesh* triangleMesh);

include/reactphysics3d/reactphysics3d.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#include <reactphysics3d/constraint/HingeJoint.h>
6767
#include <reactphysics3d/constraint/FixedJoint.h>
6868
#include <reactphysics3d/containers/Array.h>
69-
#include <reactphysics3d/utils/Error.h>
69+
#include <reactphysics3d/utils/Message.h>
7070

7171
/// Alias to the ReactPhysics3D namespace
7272
namespace rp3d = reactphysics3d;

include/reactphysics3d/utils/Error.h renamed to include/reactphysics3d/utils/Message.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,36 @@
2323
* *
2424
********************************************************************************/
2525

26-
#ifndef REACTPHYSICS3D_ERROR_H
27-
#define REACTPHYSICS3D_ERROR_H
26+
#ifndef REACTPHYSICS3D_MESSAGE_H
27+
#define REACTPHYSICS3D_MESSAGE_H
2828

2929
// Libraries
3030
#include <string>
3131

3232
/// ReactPhysics3D namespace
3333
namespace reactphysics3d {
3434

35-
// Structure Error
35+
// Structure Message
3636
/**
37-
* This structure represent an error that can be returned to the user
37+
* This structure represent a message that can be returned to the user
3838
*/
39-
struct Error {
39+
struct Message {
4040

4141
public:
4242

43-
/// Error level
44-
enum class Level {Error = 1, Warning = 2, Information = 4};
43+
/// Type of message
44+
enum class Type {Error = 1, Warning = 2, Information = 4};
4545

46-
/// Error message
47-
std::string message;
46+
/// Message text
47+
std::string text;
4848

49-
// Level (error, warning, information)
50-
Level level;
49+
// Type (error, warning, information)
50+
Type type;
5151

5252
// -------------------- Methods -------------------- //
5353

5454
/// Constructor
55-
Error(std::string message, Level level = Level::Error) : message(message), level(level) {
55+
Message(std::string text, Type type = Type::Error) : text(text), type(type) {
5656

5757
}
5858
};

include/reactphysics3d/utils/quickhull/QuickHull.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace reactphysics3d {
3939
// Declarations
4040
class ConvexMesh;
4141
class VertexArray;
42-
struct Error;
42+
struct Message;
4343
template<typename T>
4444
class Array;
4545

@@ -75,7 +75,7 @@ class QuickHull {
7575
static bool computeInitialHull(Array<Vector3>& points, QHHalfEdgeStructure& convexHull,
7676
Array<QHHalfEdgeStructure::Face*>& initialFaces,
7777
Array<uint32>& orphanPointsIndices,
78-
MemoryAllocator& allocator, std::vector<Error>& errors);
78+
MemoryAllocator& allocator, std::vector<Message>& errors);
7979

8080
/// Extract the points from the array
8181
static void extractPoints(const VertexArray& vertexArray, Array<Vector3>& outArray);
@@ -170,7 +170,7 @@ class QuickHull {
170170
static bool computeConvexHull(const VertexArray& vertexArray, PolygonVertexArray& outPolygonVertexArray,
171171
Array<float>& outVertices, Array<unsigned int>& outIndices,
172172
Array<PolygonVertexArray::PolygonFace>& outFaces,
173-
MemoryAllocator& allocator, std::vector<Error>& errors);
173+
MemoryAllocator& allocator, std::vector<Message>& errors);
174174

175175

176176

src/collision/ConvexMesh.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <reactphysics3d/collision/PolygonVertexArray.h>
3030
#include <reactphysics3d/utils/DefaultLogger.h>
3131
#include <reactphysics3d/engine/PhysicsCommon.h>
32-
#include <reactphysics3d/utils/Error.h>
32+
#include <reactphysics3d/utils/Message.h>
3333
#include <cstdlib>
3434

3535
using namespace reactphysics3d;
@@ -54,7 +54,7 @@ ConvexMesh* ConvexMesh::create(MemoryAllocator& allocator) {
5454
}
5555

5656
// Initialize a mesh and returns errors if any
57-
bool ConvexMesh::init(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors) {
57+
bool ConvexMesh::init(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors) {
5858

5959
bool isValid = true;
6060

@@ -80,7 +80,7 @@ bool ConvexMesh::init(const PolygonVertexArray& polygonVertexArray, std::vector<
8080
}
8181

8282
// Copy the vertices into the mesh
83-
bool ConvexMesh::copyVertices(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors) {
83+
bool ConvexMesh::copyVertices(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors) {
8484

8585
bool isValid = true;
8686

@@ -110,7 +110,7 @@ bool ConvexMesh::copyVertices(const PolygonVertexArray& polygonVertexArray, std:
110110
}
111111
else {
112112

113-
errors.push_back(Error("The mesh does not have any vertices"));
113+
errors.push_back(Message("The mesh does not have any vertices"));
114114

115115
isValid = false;
116116
}
@@ -120,7 +120,7 @@ bool ConvexMesh::copyVertices(const PolygonVertexArray& polygonVertexArray, std:
120120

121121
// Create the half-edge structure of the mesh
122122
/// This method returns true if the mesh is valid or false otherwise
123-
bool ConvexMesh::createHalfEdgeStructure(const PolygonVertexArray& polygonVertexArray, std::vector<Error>& errors) {
123+
bool ConvexMesh::createHalfEdgeStructure(const PolygonVertexArray& polygonVertexArray, std::vector<Message>& errors) {
124124

125125
bool isValid = true;
126126

@@ -152,7 +152,7 @@ bool ConvexMesh::createHalfEdgeStructure(const PolygonVertexArray& polygonVertex
152152
if (faceVertices.size() < 3) {
153153

154154
// Create a new error
155-
errors.push_back(Error(std::string("The face with index " + std::to_string(f) + " has less than three vertices")));
155+
errors.push_back(Message(std::string("The face with index " + std::to_string(f) + " has less than three vertices")));
156156

157157
isValid = false;
158158

@@ -173,7 +173,7 @@ bool ConvexMesh::createHalfEdgeStructure(const PolygonVertexArray& polygonVertex
173173
"Error when creating a ConvexMesh: input PolygonVertexArray is not valid. Mesh with duplicated vertices is not supported.", __FILE__, __LINE__);
174174

175175
// Create a new error
176-
errors.push_back(Error(std::string("Convex Mesh might have duplicated vertices (invalid Euler formula)")));
176+
errors.push_back(Message(std::string("Convex Mesh might have duplicated vertices (invalid Euler formula)")));
177177

178178
isValid = false;
179179
}
@@ -188,7 +188,7 @@ bool ConvexMesh::createHalfEdgeStructure(const PolygonVertexArray& polygonVertex
188188
}
189189

190190
// Compute the faces normals
191-
bool ConvexMesh::computeFacesNormals(std::vector<Error>& errors) {
191+
bool ConvexMesh::computeFacesNormals(std::vector<Message>& errors) {
192192

193193
bool isValid = true;
194194

@@ -211,7 +211,7 @@ bool ConvexMesh::computeFacesNormals(std::vector<Error>& errors) {
211211
}
212212
else {
213213
isValid = false;
214-
errors.push_back(Error("Face with index " + std::to_string(f) + " has a zero area"));
214+
errors.push_back(Message("Face with index " + std::to_string(f) + " has a zero area"));
215215
}
216216
}
217217
}

0 commit comments

Comments
 (0)