1 /********************************************************************************
2 * ReactPhysics3D physics library, http://www.reactphysics3d.com                 *
3 * Copyright (c) 2010-2020 Daniel Chappuis                                       *
4 *********************************************************************************
5 *                                                                               *
6 * This software is provided 'as-is', without any express or implied warranty.   *
7 * In no event will the authors be held liable for any damages arising from the  *
8 * use of this software.                                                         *
9 *                                                                               *
10 * Permission is granted to anyone to use this software for any purpose,         *
11 * including commercial applications, and to alter it and redistribute it        *
12 * freely, subject to the following restrictions:                                *
13 *                                                                               *
14 * 1. The origin of this software must not be misrepresented; you must not claim *
15 *    that you wrote the original software. If you use this software in a        *
16 *    product, an acknowledgment in the product documentation would be           *
17 *    appreciated but is not required.                                           *
18 *                                                                               *
19 * 2. Altered source versions must be plainly marked as such, and must not be    *
20 *    misrepresented as being the original software.                             *
21 *                                                                               *
22 * 3. This notice may not be removed or altered from any source distribution.    *
23 *                                                                               *
24 ********************************************************************************/
25 
26 #ifndef REACTPHYSICS3D_POLYHEDRON_MESH_H
27 #define REACTPHYSICS3D_POLYHEDRON_MESH_H
28 
29 // Libraries
30 #include <reactphysics3d/mathematics/mathematics.h>
31 #include "HalfEdgeStructure.h"
32 
33 namespace reactphysics3d {
34 
35 // Declarations
36 class DefaultAllocator;
37 class PolygonVertexArray;
38 
39 // Class PolyhedronMesh
40 /**
41  * This class describes a polyhedron mesh made of faces and vertices.
42  * The faces do not have to be triangles.
43  */
44 class PolyhedronMesh {
45 
46     private:
47 
48         // -------------------- Attributes -------------------- //
49 
50         /// Reference to the memory allocator
51         MemoryAllocator& mMemoryAllocator;
52 
53         /// Pointer the the polygon vertex array with vertices and faces
54         /// of the mesh
55         PolygonVertexArray* mPolygonVertexArray;
56 
57         /// Half-edge structure of the mesh
58         HalfEdgeStructure mHalfEdgeStructure;
59 
60         /// Array with the face normals
61         Vector3* mFacesNormals;
62 
63         /// Centroid of the polyhedron
64         Vector3 mCentroid;
65 
66         // -------------------- Methods -------------------- //
67 
68         /// Constructor
69         PolyhedronMesh(PolygonVertexArray* polygonVertexArray, MemoryAllocator& allocator);
70 
71         /// Create the half-edge structure of the mesh
72         void createHalfEdgeStructure();
73 
74         /// Compute the faces normals
75         void computeFacesNormals();
76 
77         /// Compute the centroid of the polyhedron
78         void computeCentroid() ;
79 
80         /// Compute and return the area of a face
81         decimal getFaceArea(uint faceIndex) const;
82 
83     public:
84 
85         // -------------------- Methods -------------------- //
86 
87         /// Destructor
88         ~PolyhedronMesh();
89 
90         /// Return the number of vertices
91         uint getNbVertices() const;
92 
93         /// Return a vertex
94         Vector3 getVertex(uint index) const;
95 
96         /// Return the number of faces
97         uint getNbFaces() const;
98 
99         /// Return a face normal
100         Vector3 getFaceNormal(uint faceIndex) const;
101 
102         /// Return the half-edge structure of the mesh
103         const HalfEdgeStructure& getHalfEdgeStructure() const;
104 
105         /// Return the centroid of the polyhedron
106         Vector3 getCentroid() const;
107 
108         /// Compute and return the volume of the polyhedron
109         decimal getVolume() const;
110 
111         // ---------- Friendship ---------- //
112 
113         friend class PhysicsCommon;
114 };
115 
116 // Return the number of vertices
117 /**
118  * @return The number of vertices in the mesh
119  */
getNbVertices()120 inline uint PolyhedronMesh::getNbVertices() const {
121     return mHalfEdgeStructure.getNbVertices();
122 }
123 
124 // Return the number of faces
125 /**
126  * @return The number of faces in the mesh
127  */
getNbFaces()128 inline uint PolyhedronMesh::getNbFaces() const {
129    return mHalfEdgeStructure.getNbFaces();
130 }
131 
132 // Return a face normal
133 /**
134  * @param faceIndex The index of a given face of the mesh
135  * @return The normal vector of a given face of the mesh
136  */
getFaceNormal(uint faceIndex)137 inline Vector3 PolyhedronMesh::getFaceNormal(uint faceIndex) const {
138     assert(faceIndex < mHalfEdgeStructure.getNbFaces());
139     return mFacesNormals[faceIndex];
140 }
141 
142 // Return the half-edge structure of the mesh
143 /**
144  * @return The Half-Edge structure of the mesh
145  */
getHalfEdgeStructure()146 inline const HalfEdgeStructure& PolyhedronMesh::getHalfEdgeStructure() const {
147     return mHalfEdgeStructure;
148 }
149 
150 // Return the centroid of the polyhedron
151 /**
152  * @return The centroid of the mesh
153  */
getCentroid()154 inline Vector3 PolyhedronMesh::getCentroid() const {
155     return mCentroid;
156 }
157 
158 }
159 
160 #endif
161 
162