TDME2  1.9.200
Triangle.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
4 #include <tdme/math/Math.h>
5 #include <tdme/math/Vector3.h>
6 
8 
9 using tdme::math::Math;
11 
12 Triangle::Triangle()
13 {
14  this->vertices.resize(3);
15 }
16 
17 Triangle::Triangle(const Vector3& vertex0, const Vector3& vertex1, const Vector3& vertex2)
18 {
19  this->vertices.resize(3);
20  this->vertices[0].set(vertex0);
21  this->vertices[1].set(vertex1);
22  this->vertices[2].set(vertex2);
23 }
24 
25 void Triangle::computeClosestPointOnBoundingVolume(const Vector3& point, Vector3& closestPoint) const {
26  // see: http://www.gamedev.net/topic/552906-closest-point-on-triangle/
27  Vector3 edge0;
28  Vector3 edge1;
29  Vector3 v0Point;
30  edge0.set(vertices[1]).sub(vertices[0]);
31  edge1.set(vertices[2]).sub(vertices[0]);
32  v0Point.set(vertices[0]).sub(point);
33  auto a = Vector3::computeDotProduct(edge0, edge0);
34  auto b = Vector3::computeDotProduct(edge0, edge1);
35  auto c = Vector3::computeDotProduct(edge1, edge1);
36  auto d = Vector3::computeDotProduct(edge0, v0Point);
37  auto e = Vector3::computeDotProduct(edge1, v0Point);
38  auto det = a * c - b * b;
39  auto s = b * e - c * d;
40  auto t = b * d - a * e;
41  if (s + t < det) {
42  if (s < 0.0f) {
43  if (t < 0.0f) {
44  if (d < 0.0f) {
45  s = Math::clamp(-d / a, 0.0f, 1.0f);
46  t = 0.0f;
47  } else {
48  s = 0.0f;
49  t = Math::clamp(-e / c, 0.0f, 1.0f);
50  }
51  } else {
52  s = 0.0f;
53  t = Math::clamp(-e / c, 0.0f, 1.0f);
54  }
55  } else if (t < 0.0f) {
56  s = Math::clamp(-d / a, 0.0f, 1.0f);
57  t = 0.0f;
58  } else {
59  auto invDet = 1.0f / det;
60  s *= invDet;
61  t *= invDet;
62  }
63  } else {
64  if (s < 0.0f) {
65  auto tmp0 = b + d;
66  auto tmp1 = c + e;
67  if (tmp1 > tmp0) {
68  auto numer = tmp1 - tmp0;
69  auto denom = a - 2 * b + c;
70  s = Math::clamp(numer / denom, 0.0f, 1.0f);
71  t = 1 - s;
72  } else {
73  t = Math::clamp(-e / c, 0.0f, 1.0f);
74  s = 0.0f;
75  }
76  } else if (t < 0.0f) {
77  if (a + d > b + e) {
78  auto numer = c + e - b - d;
79  auto denom = a - 2 * b + c;
80  s = Math::clamp(numer / denom, 0.0f, 1.0f);
81  t = 1 - s;
82  } else {
83  s = Math::clamp(-e / c, 0.0f, 1.0f);
84  t = 0.0f;
85  }
86  } else {
87  auto numer = c + e - b - d;
88  auto denom = a - 2 * b + c;
89  s = Math::clamp(numer / denom, 0.0f, 1.0f);
90  t = 1.0f - s;
91  }
92  }
93  closestPoint.set(vertices[0]).add(edge0.scale(s)).add(edge1.scale(t));
94 }
Triangle entity, this is not directly connectable with physics engine.
Definition: Triangle.h:18
Triangle()
Public constructor.
Definition: Triangle.cpp:12
void computeClosestPointOnBoundingVolume(const Vector3 &point, Vector3 &closestPoint) const
Compute closest point on bounding volume.
Definition: Triangle.cpp:25
vector< Vector3 > vertices
Definition: Triangle.h:49
Standard math functions.
Definition: Math.h:19
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70