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 | /*VertexContainer vertices;
|
---|
34 | vertices.push_back(mVertices[0]);
|
---|
35 | vertices.push_back(mVertices[1]);
|
---|
36 | vertices.push_back(mVertices[2]);
|
---|
37 |
|
---|
38 | Polygon3 poly(vertices);
|
---|
39 |
|
---|
40 | int dummy = poly.CastRay(ray, t, nearestT);
|
---|
41 |
|
---|
42 | cout << "polyversion code: " << dummy << " t: " << t << " nearestT: " << nearestT << endl;
|
---|
43 | return dummy;*/
|
---|
44 | /////////////////////////////////////////////
|
---|
45 | // specialised ray casting version
|
---|
46 |
|
---|
47 | //-- calc ray-plane intersection
|
---|
48 |
|
---|
49 | // get triangle edge vectors and plane normal
|
---|
50 | const Vector3 u = mVertices[0] - mVertices[1];
|
---|
51 | const Vector3 v = mVertices[2] - mVertices[1];
|
---|
52 |
|
---|
53 | normal = Normalize(CrossProd(v, u)); // cross product
|
---|
54 |
|
---|
55 | const Vector3 dir = ray.GetDir(); // ray direction vector
|
---|
56 | const Vector3 w0 = ray.GetLoc() - mVertices[1];
|
---|
57 |
|
---|
58 | // params to calc ray-plane intersect
|
---|
59 | const float a = -DotProd(normal, w0);
|
---|
60 | const float b = DotProd(normal, dir);
|
---|
61 |
|
---|
62 | // check for division by zero
|
---|
63 | if (fabs(b) < Limits::Small)
|
---|
64 | {
|
---|
65 | // ray is parallel to triangle plane
|
---|
66 | if (a == 0) // ray lies in triangle plane
|
---|
67 | {
|
---|
68 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | return Ray::NO_INTERSECTION; // ray disjoint from plane
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | // distance from origin of ray to plane
|
---|
77 | t = a / b;
|
---|
78 |
|
---|
79 | if (t < 0.0) // ray goes away from triangle
|
---|
80 | {
|
---|
81 | return Ray::NO_INTERSECTION; // => no intersect
|
---|
82 | }
|
---|
83 | // already found nearer intersection
|
---|
84 | else if ((ray.GetType() == Ray::LOCAL_RAY) && (t >= nearestT))
|
---|
85 | {
|
---|
86 | return Ray::NO_INTERSECTION;
|
---|
87 | }
|
---|
88 |
|
---|
89 | ///////////////////////////////////////////////
|
---|
90 | //-- found intersection point
|
---|
91 | //-- check if it is inside triangle
|
---|
92 |
|
---|
93 | const Vector3 pt = ray.GetLoc() + t * dir;
|
---|
94 |
|
---|
95 | const Vector3 w = pt - mVertices[1];
|
---|
96 |
|
---|
97 | const float uu = DotProd(u, u);
|
---|
98 | const float uv = DotProd(u, v);
|
---|
99 | const float vv = DotProd(v, v);
|
---|
100 |
|
---|
101 | const float wu = DotProd(w, u);
|
---|
102 | const float wv = DotProd(w, v);
|
---|
103 | const float D = uv * uv - uu * vv;
|
---|
104 |
|
---|
105 | // get and test parametric coords
|
---|
106 | const float s = (uv * wv - vv * wu) / D;
|
---|
107 |
|
---|
108 | if ((s < 0.0) || (s > 1.0)) // pt is outside triangle
|
---|
109 | {
|
---|
110 | return Ray::NO_INTERSECTION;
|
---|
111 | }
|
---|
112 |
|
---|
113 | const float s2 = (uv * wu - uu * wv) / D;
|
---|
114 |
|
---|
115 | if ((s2 < 0.0) || ((s + s2) > 1.0)) // pt is outside triangle
|
---|
116 | {
|
---|
117 | return Ray::NO_INTERSECTION;
|
---|
118 | }
|
---|
119 |
|
---|
120 | return Ray::INTERSECTION; // I is in T
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | AxisAlignedBox3 Triangle3::GetBoundingBox() const
|
---|
125 | {
|
---|
126 | AxisAlignedBox3 box;
|
---|
127 | box.Initialize();
|
---|
128 |
|
---|
129 | box.Include(mVertices[0]);
|
---|
130 | box.Include(mVertices[1]);
|
---|
131 | box.Include(mVertices[2]);
|
---|
132 |
|
---|
133 | return box;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | Vector3 Triangle3::GetNormal() const
|
---|
138 | {
|
---|
139 | const Vector3 v1 = mVertices[0] - mVertices[1];
|
---|
140 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
141 |
|
---|
142 | return Normalize(CrossProd(v2, v1));
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | Vector3 Triangle3::GetCenter() const
|
---|
147 | {
|
---|
148 | return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | float Triangle3::GetArea() const
|
---|
153 | {
|
---|
154 | Vector3 v1 = mVertices[0] - mVertices[1], v2=mVertices[2] - mVertices[1];
|
---|
155 | return 0.5f * Magnitude(CrossProd(v2, v1));
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | } |
---|