1 | #include "Triangle3.h"
|
---|
2 | #include "AxisAlignedBox3.h"
|
---|
3 | #include "common.h"
|
---|
4 | #include "Plane3.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 |
|
---|
10 | namespace CHCDemo {
|
---|
11 |
|
---|
12 |
|
---|
13 | Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
14 | {
|
---|
15 | Init(a, b, c);
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | void Triangle3::Init(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
20 | {
|
---|
21 | mVertices[0] = a;
|
---|
22 | mVertices[1] = b;
|
---|
23 | mVertices[2] = c;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | AxisAlignedBox3 Triangle3::GetBoundingBox() const
|
---|
28 | {
|
---|
29 | AxisAlignedBox3 box;
|
---|
30 | box.Initialize();
|
---|
31 |
|
---|
32 | box.Include(mVertices[0]);
|
---|
33 | box.Include(mVertices[1]);
|
---|
34 | box.Include(mVertices[2]);
|
---|
35 |
|
---|
36 | box.EnlargeToMinSize();
|
---|
37 |
|
---|
38 | return box;
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | Vector3 Triangle3::GetNormal() const
|
---|
43 | {
|
---|
44 | const Vector3 v1 = mVertices[0] - mVertices[1];
|
---|
45 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
46 |
|
---|
47 | return Normalize(CrossProd(v2, v1));
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | Vector3 Triangle3::GetCenter() const
|
---|
52 | {
|
---|
53 | return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | float Triangle3::GetArea() const
|
---|
58 | {
|
---|
59 | Vector3 v1 = mVertices[0] - mVertices[1], v2=mVertices[2] - mVertices[1];
|
---|
60 | return 0.5f * Magnitude(CrossProd(v2, v1));
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | bool Triangle3::CheckValidity() const
|
---|
65 | {
|
---|
66 | return !(
|
---|
67 | EpsilonEqualV3(mVertices[0], mVertices[1]) ||
|
---|
68 | EpsilonEqualV3(mVertices[0], mVertices[2]) ||
|
---|
69 | EpsilonEqualV3(mVertices[1], mVertices[2])
|
---|
70 | );
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | bool Triangle3::GetPlaneIntersection(const Plane3 &plane,
|
---|
75 | Vector3 &intersectA,
|
---|
76 | Vector3 &intersectB) const
|
---|
77 | {
|
---|
78 | int side[3];
|
---|
79 |
|
---|
80 | // compute distance from plane
|
---|
81 | for (int i = 0; i < 3; ++ i)
|
---|
82 | side[i] = plane.Side(mVertices[i], Limits::Small);
|
---|
83 |
|
---|
84 | /////
|
---|
85 | // no intersection => early exit
|
---|
86 | if (((side[0] > 0) && (side[1] > 0) && (side[2] > 0)) ||
|
---|
87 | ((side[0] < 0) && (side[1] < 0) && (side[2] < 0)))
|
---|
88 | {
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /////////////
|
---|
93 | // at least 2 triangle vertices lie in plane => early exit
|
---|
94 |
|
---|
95 | for (int i = 0; i < 3; ++ i)
|
---|
96 | {
|
---|
97 | if (!side[i] && !side[(i + 1) % 3])
|
---|
98 | {
|
---|
99 | intersectA = mVertices[i];
|
---|
100 | intersectB = mVertices[(i + 1) % 3];
|
---|
101 |
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool foundA = false;
|
---|
107 |
|
---|
108 | // compute intersection points
|
---|
109 | for (int i = 0; i < 3; ++ i)
|
---|
110 | {
|
---|
111 | const int i_2 = (i + 1) % 3;
|
---|
112 |
|
---|
113 | // intersection found
|
---|
114 | if ((side[i] >= 0) && (side[i_2] <= 0) ||
|
---|
115 | (side[i] <= 0) && (side[i_2] >= 0))
|
---|
116 | {
|
---|
117 | const float t = plane.FindT(mVertices[i], mVertices[i_2]);
|
---|
118 |
|
---|
119 | if (!foundA)
|
---|
120 | {
|
---|
121 | intersectA = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
122 | foundA = true;
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | intersectB = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | cout << "warning! wrong triangle - plane intersection" << endl;
|
---|
133 | return false; // something went wrong!
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | }
|
---|