[191] | 1 | #include "Triangle3.h"
|
---|
[1328] | 2 | #include "Ray.h"
|
---|
| 3 | #include "AxisAlignedBox3.h"
|
---|
[1344] | 4 | #include "Containers.h"
|
---|
| 5 | #include "Polygon3.h"
|
---|
[191] | 6 |
|
---|
[1328] | 7 |
|
---|
[2176] | 8 | using namespace std;
|
---|
| 9 |
|
---|
[863] | 10 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 11 |
|
---|
[1328] | 12 |
|
---|
| 13 | Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
[191] | 14 | {
|
---|
[1328] | 15 | Init(a, b, c);
|
---|
[191] | 16 | }
|
---|
[860] | 17 |
|
---|
[1328] | 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 | float Triangle3::GetSpatialAngle(const Vector3 &point) const
|
---|
| 28 | {
|
---|
| 29 | return 0.0f;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 |
|
---|
[1867] | 33 | int
|
---|
| 34 | Triangle3::CastRay(const Ray &ray,
|
---|
| 35 | float &t,
|
---|
| 36 | const float nearestT,
|
---|
| 37 | Vector3 &normal) const
|
---|
[1328] | 38 | {
|
---|
[1420] | 39 | //////////////
|
---|
| 40 | // specialised triangle ray casting version
|
---|
[1586] | 41 | // using ray-plane intersection
|
---|
[1344] | 42 |
|
---|
[1328] | 43 | // get triangle edge vectors and plane normal
|
---|
[1344] | 44 | const Vector3 u = mVertices[0] - mVertices[1];
|
---|
| 45 | const Vector3 v = mVertices[2] - mVertices[1];
|
---|
[1328] | 46 |
|
---|
[1420] | 47 | // cross product
|
---|
| 48 | normal = Normalize(CrossProd(v, u));
|
---|
[1328] | 49 |
|
---|
[1420] | 50 | // ray direction vector
|
---|
| 51 | const Vector3 dir = ray.GetDir();
|
---|
[1344] | 52 | const Vector3 w0 = ray.GetLoc() - mVertices[1];
|
---|
[1328] | 53 |
|
---|
| 54 | // params to calc ray-plane intersect
|
---|
[1344] | 55 | const float a = -DotProd(normal, w0);
|
---|
| 56 | const float b = DotProd(normal, dir);
|
---|
[1328] | 57 |
|
---|
[1344] | 58 | // check for division by zero
|
---|
[1328] | 59 | if (fabs(b) < Limits::Small)
|
---|
| 60 | {
|
---|
| 61 | // ray is parallel to triangle plane
|
---|
[1420] | 62 | if (a == 0)
|
---|
[1328] | 63 | {
|
---|
[1420] | 64 | // ray lies in triangle plane
|
---|
[1328] | 65 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
| 66 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
[1420] | 69 | // ray disjoint from plane
|
---|
| 70 | return Ray::NO_INTERSECTION;
|
---|
[1328] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1344] | 74 | // distance from origin of ray to plane
|
---|
[1328] | 75 | t = a / b;
|
---|
| 76 |
|
---|
[1867] | 77 | if (t <= Limits::Small) // ray goes away from triangle
|
---|
[1328] | 78 | {
|
---|
[1867] | 79 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
[1328] | 80 | }
|
---|
[1344] | 81 | // already found nearer intersection
|
---|
| 82 | else if ((ray.GetType() == Ray::LOCAL_RAY) && (t >= nearestT))
|
---|
| 83 | {
|
---|
| 84 | return Ray::NO_INTERSECTION;
|
---|
| 85 | }
|
---|
[1328] | 86 |
|
---|
[1420] | 87 | /////////////////
|
---|
[1344] | 88 | //-- found intersection point
|
---|
| 89 | //-- check if it is inside triangle
|
---|
| 90 |
|
---|
| 91 | const Vector3 pt = ray.GetLoc() + t * dir;
|
---|
[1715] | 92 | #if GTP_DEBUG
|
---|
[1420] | 93 | if (!pt.CheckValidity())
|
---|
| 94 | {
|
---|
| 95 | cout << "tr: " << *this << endl;
|
---|
| 96 | cout << "v: " << pt << " t: " << t << " a: " << a << " b: " << b << " n: " << normal << endl;
|
---|
| 97 | }
|
---|
[1421] | 98 | #endif
|
---|
[1344] | 99 | const Vector3 w = pt - mVertices[1];
|
---|
[1328] | 100 |
|
---|
| 101 | const float uu = DotProd(u, u);
|
---|
| 102 | const float uv = DotProd(u, v);
|
---|
| 103 | const float vv = DotProd(v, v);
|
---|
| 104 |
|
---|
| 105 | const float wu = DotProd(w, u);
|
---|
| 106 | const float wv = DotProd(w, v);
|
---|
| 107 |
|
---|
[1867] | 108 |
|
---|
| 109 | const float D = uv * uv - uu * vv;
|
---|
| 110 |
|
---|
[1328] | 111 | // get and test parametric coords
|
---|
| 112 | const float s = (uv * wv - vv * wu) / D;
|
---|
| 113 |
|
---|
[1867] | 114 | if ((s < -Limits::Small) || (s > 1.0f + Limits::Small)) // pt is outside triangle
|
---|
[1344] | 115 | {
|
---|
[1328] | 116 | return Ray::NO_INTERSECTION;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | const float s2 = (uv * wu - uu * wv) / D;
|
---|
| 120 |
|
---|
[1867] | 121 | if ((s2 < -Limits::Small) || ((s + s2) > 1.0f + Limits::Small)) // pt is outside triangle
|
---|
[1344] | 122 | {
|
---|
[1328] | 123 | return Ray::NO_INTERSECTION;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | return Ray::INTERSECTION; // I is in T
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | AxisAlignedBox3 Triangle3::GetBoundingBox() const
|
---|
| 131 | {
|
---|
| 132 | AxisAlignedBox3 box;
|
---|
| 133 | box.Initialize();
|
---|
| 134 |
|
---|
| 135 | box.Include(mVertices[0]);
|
---|
| 136 | box.Include(mVertices[1]);
|
---|
| 137 | box.Include(mVertices[2]);
|
---|
| 138 |
|
---|
| 139 | return box;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | Vector3 Triangle3::GetNormal() const
|
---|
| 144 | {
|
---|
| 145 | const Vector3 v1 = mVertices[0] - mVertices[1];
|
---|
[1344] | 146 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
[1328] | 147 |
|
---|
| 148 | return Normalize(CrossProd(v2, v1));
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | Vector3 Triangle3::GetCenter() const
|
---|
| 153 | {
|
---|
| 154 | return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | float Triangle3::GetArea() const
|
---|
| 159 | {
|
---|
[1344] | 160 | Vector3 v1 = mVertices[0] - mVertices[1], v2=mVertices[2] - mVertices[1];
|
---|
[1328] | 161 | return 0.5f * Magnitude(CrossProd(v2, v1));
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
[1420] | 165 | bool Triangle3::CheckValidity() const
|
---|
| 166 | {
|
---|
| 167 | return !(
|
---|
| 168 | EpsilonEqualV3(mVertices[0], mVertices[1]) ||
|
---|
| 169 | EpsilonEqualV3(mVertices[0], mVertices[2]) ||
|
---|
| 170 | EpsilonEqualV3(mVertices[1], mVertices[2])
|
---|
| 171 | );
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[1932] | 174 |
|
---|
[1933] | 175 | bool Triangle3::GetPlaneIntersection(const Plane3 &plane,
|
---|
| 176 | Vector3 &intersectA,
|
---|
| 177 | Vector3 &intersectB) const
|
---|
[1932] | 178 | {
|
---|
| 179 | int side[3];
|
---|
| 180 |
|
---|
| 181 | // compute distance from plane
|
---|
| 182 | for (int i = 0; i < 3; ++ i)
|
---|
| 183 | {
|
---|
| 184 | side[i] = plane.Side(mVertices[i], Limits::Small);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /////
|
---|
| 188 | // no intersection => early exit
|
---|
| 189 | if (((side[0] > 0) && (side[1] > 0) && (side[2] > 0)) ||
|
---|
| 190 | ((side[0] < 0) && (side[1] < 0) && (side[2] < 0)))
|
---|
| 191 | {
|
---|
| 192 | return false;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /////////////
|
---|
| 196 | // at least 2 triangle vertices lie in plane => early exit
|
---|
| 197 |
|
---|
| 198 | for (int i = 0; i < 3; ++ i)
|
---|
| 199 | {
|
---|
| 200 | if (!side[i] && !side[(i + 1) % 3])
|
---|
| 201 | {
|
---|
| 202 | intersectA = mVertices[i];
|
---|
| 203 | intersectB = mVertices[(i + 1) % 3];
|
---|
[1933] | 204 |
|
---|
[1932] | 205 | return true;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | bool foundA = false;
|
---|
| 210 |
|
---|
| 211 | // compute intersection points
|
---|
| 212 | for (int i = 0; i < 3; ++ i)
|
---|
| 213 | {
|
---|
| 214 | const int i_2 = (i + 1) % 3;
|
---|
| 215 |
|
---|
| 216 | // intersection found
|
---|
[1933] | 217 | if ((side[i] >= 0) && (side[i_2] <= 0) ||
|
---|
| 218 | (side[i] <= 0) && (side[i_2] >= 0))
|
---|
[1932] | 219 | {
|
---|
| 220 | const float t = plane.FindT(mVertices[i], mVertices[i_2]);
|
---|
| 221 |
|
---|
| 222 | if (!foundA)
|
---|
| 223 | {
|
---|
| 224 | intersectA = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
[1933] | 225 |
|
---|
[1932] | 226 | foundA = true;
|
---|
| 227 | }
|
---|
| 228 | else
|
---|
| 229 | {
|
---|
| 230 | intersectB = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
[1933] | 231 |
|
---|
[1932] | 232 | return true;
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | cout << "warning! wrong triangle - plane intersection" << endl;
|
---|
| 238 | return false; // something went wrong!
|
---|
[1715] | 239 | }
|
---|
[1932] | 240 |
|
---|
| 241 |
|
---|
| 242 | }
|
---|