TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConvexMesh.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
6 #include <reactphysics3d/collision/PolygonVertexArray.h>
7 #include <reactphysics3d/collision/PolyhedronMesh.h>
8 
9 #include <tdme/tdme.h>
10 #include <tdme/engine/fwd-tdme.h>
14 #include <tdme/math/Math.h>
15 #include <tdme/math/Vector3.h>
17 
18 using std::unique_ptr;
19 using std::vector;
20 
25 using tdme::math::Math;
30 
31 /**
32  * Convex mesh physics primitive
33  * @author Andreas Drewke
34  */
36  : public BoundingVolume
37 {
38 private:
39  static constexpr float VERTEX_COMPARE_EPSILON { Math::EPSILON };
40 
41  vector<Vector3> vertices;
42  vector<int> facesVerticesCount;
43  vector<int> indices;
44 
45  reactphysics3d::PolyhedronMesh* polyhedronMesh { nullptr };
46  unique_ptr<reactphysics3d::PolygonVertexArray> polygonVertexArray;
47  unique_ptr<ByteBuffer> verticesByteBuffer;
48  unique_ptr<ByteBuffer> indicesByteBuffer;
49  vector<reactphysics3d::PolygonVertexArray::PolygonFace> faces;
50 
51  // forbid class copy
53 
54  /**
55  * Public constructor
56  * @param vertices vertices
57  * @param facesVerticesCount faces vertices count
58  * @param indices indices
59  * @param scale scale
60  */
61  ConvexMesh(const vector<Vector3>& vertices, const vector<int>& facesVerticesCount, const vector<int>& indices, const Vector3& scale = Vector3(1.0f, 1.0f, 1.0f));
62 
63  /**
64  * Checks if vertex lives on triangle plane
65  * @param triangle triangle
66  * @param vertex vertex
67  * @return if vertex lives on triangle plane
68  */
69  inline bool isVertexOnTrianglePlane(const Triangle& triangle, const Vector3& vertex) {
70  for (const auto& triangleVertex: triangle.getVertices()) {
71  if (triangleVertex.equals(vertex, VERTEX_COMPARE_EPSILON) == true) return true;
72  }
73  // see: http://www.ambrsoft.com/TrigoCalc/Plan3D/PointsCoplanar.htm
74  Vector3 v1;
75  Vector3 v2;
76  Vector3 v3;
77  v1.set(triangle.getVertices()[1]).sub(triangle.getVertices()[0]).normalize();
78  v2.set(triangle.getVertices()[2]).sub(triangle.getVertices()[0]).normalize();
79  v3.set(vertex).sub(triangle.getVertices()[0]);
80  auto v1Dotv2v3Cross = Vector3::computeDotProduct(v1, Vector3::computeCrossProduct(v2, v3).normalize());
81  return Math::abs(v1Dotv2v3Cross) < 0.001;
82  }
83 
84  /**
85  * Checks if 2 triangles are adjacent
86  * @param triangle1 triangle 1
87  * @param triangle2 triangle 2
88  * @return if triangles are adjacent
89  */
90  inline bool areTrianglesAdjacent(const Triangle& triangle1, const Triangle& triangle2) {
91  auto equalVertices = 0;
92  for (const auto& triangle1Vertex: triangle1.getVertices()) {
93  for (const auto& triangle2Vertex: triangle2.getVertices()) {
94  if (triangle1Vertex.equals(triangle2Vertex, VERTEX_COMPARE_EPSILON)) equalVertices++;
95  }
96  }
97  return equalVertices > 0;
98  }
99 
100  /**
101  * Create convex mesh
102  * Note: it also translates center into origin
103  * @param vertices vertices
104  * @param facesVerticesCount faces vertices count
105  * @param indices indices
106  * @param scale scale
107  */
108  void createConvexMesh(const vector<Vector3>& vertices, const vector<int>& facesVerticesCount, const vector<int>& indices, const Vector3& scale);
109 
110  // overridden methods
111  void destroyCollisionShape() override;
112  void createCollisionShape(World* world) override;
113 
114 public:
115  /**
116  * Public constructor
117  */
118  ConvexMesh();
119 
120  /**
121  * Public constructor
122  * @param model model
123  * @param scale scale
124  */
125  ConvexMesh(ObjectModel* model, const Vector3& scale = Vector3(1.0f, 1.0f, 1.0f));
126 
127  /**
128  * Public destructor
129  */
130  ~ConvexMesh();
131 
132  // overridden methods
133  void setScale(const Vector3& scale) override;
134  BoundingVolume* clone() const override;
135 
136  /**
137  * @return vertices
138  */
139  const vector<Vector3>& getVertices();
140 
141 };
Dynamic physics world class.
Definition: World.h:38
Convex mesh physics primitive.
Definition: ConvexMesh.h:37
unique_ptr< reactphysics3d::PolygonVertexArray > polygonVertexArray
Definition: ConvexMesh.h:46
static constexpr float VERTEX_COMPARE_EPSILON
Definition: ConvexMesh.h:39
reactphysics3d::PolyhedronMesh * polyhedronMesh
Definition: ConvexMesh.h:45
bool isVertexOnTrianglePlane(const Triangle &triangle, const Vector3 &vertex)
Checks if vertex lives on triangle plane.
Definition: ConvexMesh.h:69
void createConvexMesh(const vector< Vector3 > &vertices, const vector< int > &facesVerticesCount, const vector< int > &indices, const Vector3 &scale)
Create convex mesh Note: it also translates center into origin.
Definition: ConvexMesh.cpp:69
void destroyCollisionShape() override
Destroy collision shape.
Definition: ConvexMesh.cpp:383
void setScale(const Vector3 &scale) override
Set local scale.
Definition: ConvexMesh.cpp:351
const vector< Vector3 > & getVertices()
Definition: ConvexMesh.cpp:441
void createCollisionShape(World *world) override
Create collision shap.
Definition: ConvexMesh.cpp:395
BoundingVolume * clone() const override
Clones this bounding volume.
Definition: ConvexMesh.cpp:436
unique_ptr< ByteBuffer > verticesByteBuffer
Definition: ConvexMesh.h:47
vector< reactphysics3d::PolygonVertexArray::PolygonFace > faces
Definition: ConvexMesh.h:49
unique_ptr< ByteBuffer > indicesByteBuffer
Definition: ConvexMesh.h:48
bool areTrianglesAdjacent(const Triangle &triangle1, const Triangle &triangle2)
Checks if 2 triangles are adjacent.
Definition: ConvexMesh.h:90
Triangle entity, this is not directly connectable with physics engine.
Definition: Triangle.h:18
const vector< Vector3 > & getVertices() const
Definition: Triangle.h:37
Standard math functions.
Definition: Math.h:19
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector3 & normalize()
Normalizes this vector3.
Definition: Vector3.h:239
Byte buffer class.
Definition: ByteBuffer.h:27
Float buffer class.
Definition: FloatBuffer.h:18
Integer buffer class.
Definition: IntBuffer.h:14
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6