1 | #include "Triangle3.h"
|
---|
2 | #include "Ray.h"
|
---|
3 | #include "AxisAlignedBox3.h"
|
---|
4 | #include "Containers.h"
|
---|
5 | #include "Polygon3.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 |
|
---|
11 | Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
12 | {
|
---|
13 | Init(a, b, c);
|
---|
14 | }
|
---|
15 |
|
---|
16 |
|
---|
17 | void Triangle3::Init(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
18 | {
|
---|
19 | mVertices[0] = a;
|
---|
20 | mVertices[1] = b;
|
---|
21 | mVertices[2] = c;
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | float Triangle3::GetSpatialAngle(const Vector3 &point) const
|
---|
26 | {
|
---|
27 | return 0.0f;
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | int Triangle3::CastRay(const Ray &ray, float &t, const float nearestT, Vector3 &normal) const
|
---|
32 | {
|
---|
33 | #if 0
|
---|
34 | VertexContainer vertices;
|
---|
35 | vertices.push_back(mVertices[0]);
|
---|
36 | vertices.push_back(mVertices[1]);
|
---|
37 | vertices.push_back(mVertices[2]);
|
---|
38 |
|
---|
39 | Polygon3 poly(vertices);
|
---|
40 |
|
---|
41 | int dummy = poly.CastRay(ray, t, nearestT);
|
---|
42 |
|
---|
43 | cout << "polyversion code: " << dummy << " t: " << t << " nearestT: " << nearestT << endl;
|
---|
44 | return dummy;
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | //////////////
|
---|
48 | // specialised triangle ray casting version
|
---|
49 | // using ray-plane intersection
|
---|
50 |
|
---|
51 | // get triangle edge vectors and plane normal
|
---|
52 | const Vector3 u = mVertices[0] - mVertices[1];
|
---|
53 | const Vector3 v = mVertices[2] - mVertices[1];
|
---|
54 |
|
---|
55 | // cross product
|
---|
56 | normal = Normalize(CrossProd(v, u));
|
---|
57 |
|
---|
58 | // ray direction vector
|
---|
59 | const Vector3 dir = ray.GetDir();
|
---|
60 | const Vector3 w0 = ray.GetLoc() - mVertices[1];
|
---|
61 |
|
---|
62 | // params to calc ray-plane intersect
|
---|
63 | const float a = -DotProd(normal, w0);
|
---|
64 | const float b = DotProd(normal, dir);
|
---|
65 |
|
---|
66 | // check for division by zero
|
---|
67 | if (fabs(b) < Limits::Small)
|
---|
68 | {
|
---|
69 | // ray is parallel to triangle plane
|
---|
70 | if (a == 0)
|
---|
71 | {
|
---|
72 | // ray lies in triangle plane
|
---|
73 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | // ray disjoint from plane
|
---|
78 | return Ray::NO_INTERSECTION;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // distance from origin of ray to plane
|
---|
83 | t = a / b;
|
---|
84 |
|
---|
85 | if (t < 0.0) // ray goes away from triangle
|
---|
86 | {
|
---|
87 | return Ray::NO_INTERSECTION; // => no intersect
|
---|
88 | }
|
---|
89 | // already found nearer intersection
|
---|
90 | else if ((ray.GetType() == Ray::LOCAL_RAY) && (t >= nearestT))
|
---|
91 | {
|
---|
92 | return Ray::NO_INTERSECTION;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /////////////////
|
---|
96 | //-- found intersection point
|
---|
97 | //-- check if it is inside triangle
|
---|
98 |
|
---|
99 | const Vector3 pt = ray.GetLoc() + t * dir;
|
---|
100 | #if GTP_DEBUG
|
---|
101 | if (!pt.CheckValidity())
|
---|
102 | {
|
---|
103 | cout << "tr: " << *this << endl;
|
---|
104 | cout << "v: " << pt << " t: " << t << " a: " << a << " b: " << b << " n: " << normal << endl;
|
---|
105 | }
|
---|
106 | #endif
|
---|
107 | const Vector3 w = pt - mVertices[1];
|
---|
108 |
|
---|
109 | const float uu = DotProd(u, u);
|
---|
110 | const float uv = DotProd(u, v);
|
---|
111 | const float vv = DotProd(v, v);
|
---|
112 |
|
---|
113 | const float wu = DotProd(w, u);
|
---|
114 | const float wv = DotProd(w, v);
|
---|
115 | const float D = uv * uv - uu * vv;
|
---|
116 |
|
---|
117 | // get and test parametric coords
|
---|
118 | const float s = (uv * wv - vv * wu) / D;
|
---|
119 |
|
---|
120 | if ((s < 0.0) || (s > 1.0)) // pt is outside triangle
|
---|
121 | {
|
---|
122 | return Ray::NO_INTERSECTION;
|
---|
123 | }
|
---|
124 |
|
---|
125 | const float s2 = (uv * wu - uu * wv) / D;
|
---|
126 |
|
---|
127 | if ((s2 < 0.0) || ((s + s2) > 1.0)) // pt is outside triangle
|
---|
128 | {
|
---|
129 | return Ray::NO_INTERSECTION;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return Ray::INTERSECTION; // I is in T
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | AxisAlignedBox3 Triangle3::GetBoundingBox() const
|
---|
137 | {
|
---|
138 | AxisAlignedBox3 box;
|
---|
139 | box.Initialize();
|
---|
140 |
|
---|
141 | box.Include(mVertices[0]);
|
---|
142 | box.Include(mVertices[1]);
|
---|
143 | box.Include(mVertices[2]);
|
---|
144 |
|
---|
145 | return box;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | Vector3 Triangle3::GetNormal() const
|
---|
150 | {
|
---|
151 | const Vector3 v1 = mVertices[0] - mVertices[1];
|
---|
152 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
153 |
|
---|
154 | return Normalize(CrossProd(v2, v1));
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | Vector3 Triangle3::GetCenter() const
|
---|
159 | {
|
---|
160 | return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | float Triangle3::GetArea() const
|
---|
165 | {
|
---|
166 | Vector3 v1 = mVertices[0] - mVertices[1], v2=mVertices[2] - mVertices[1];
|
---|
167 | return 0.5f * Magnitude(CrossProd(v2, v1));
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | bool Triangle3::CheckValidity() const
|
---|
172 | {
|
---|
173 | return !(
|
---|
174 | EpsilonEqualV3(mVertices[0], mVertices[1]) ||
|
---|
175 | EpsilonEqualV3(mVertices[0], mVertices[2]) ||
|
---|
176 | EpsilonEqualV3(mVertices[1], mVertices[2])
|
---|
177 | );
|
---|
178 | }
|
---|
179 |
|
---|
180 | }
|
---|