|
30 | 30 | #include <cassert>
|
31 | 31 | #include <reactphysics3d/containers/Array.h>
|
32 | 32 | #include <reactphysics3d/memory/MemoryAllocator.h>
|
| 33 | +#include <reactphysics3d/collision/broadphase/DynamicAABBTree.h> |
33 | 34 |
|
34 | 35 | namespace reactphysics3d {
|
35 | 36 |
|
36 | 37 | // Declarations
|
37 | 38 | class TriangleVertexArray;
|
| 39 | +struct Error; |
38 | 40 |
|
39 | 41 | // Class TriangleMesh
|
40 | 42 | /**
|
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). |
46 | 46 | */
|
47 | 47 | class TriangleMesh {
|
48 | 48 |
|
49 | 49 | protected:
|
50 | 50 |
|
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; |
53 | 62 |
|
54 | 63 | /// Constructor
|
55 | 64 | TriangleMesh(reactphysics3d::MemoryAllocator& allocator);
|
56 | 65 |
|
| 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 | + |
57 | 91 | public:
|
58 | 92 |
|
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; |
61 | 98 |
|
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; |
64 | 101 |
|
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; |
67 | 105 |
|
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; |
70 | 108 |
|
| 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; |
71 | 118 |
|
72 | 119 | // ---------- Friendship ---------- //
|
73 | 120 |
|
74 | 121 | friend class PhysicsCommon;
|
| 122 | + friend class ConcaveMeshShape; |
75 | 123 | };
|
76 | 124 |
|
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(); |
83 | 128 | }
|
84 | 129 |
|
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; |
93 | 133 | }
|
94 | 134 |
|
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]; |
101 | 175 | }
|
102 | 176 |
|
103 | 177 | }
|
|
0 commit comments