[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 |
|
---|
[863] | 8 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 9 |
|
---|
[1328] | 10 |
|
---|
| 11 | Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
[191] | 12 | {
|
---|
[1328] | 13 | Init(a, b, c);
|
---|
[191] | 14 | }
|
---|
[860] | 15 |
|
---|
[1328] | 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 |
|
---|
[1344] | 31 | int Triangle3::CastRay(const Ray &ray, float &t, const float nearestT, Vector3 &normal) const
|
---|
[1328] | 32 | {
|
---|
[1344] | 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 |
|
---|
[1328] | 49 | // get triangle edge vectors and plane normal
|
---|
[1344] | 50 | const Vector3 u = mVertices[0] - mVertices[1];
|
---|
| 51 | const Vector3 v = mVertices[2] - mVertices[1];
|
---|
[1328] | 52 |
|
---|
[1344] | 53 | normal = Normalize(CrossProd(v, u)); // cross product
|
---|
[1328] | 54 |
|
---|
[1344] | 55 | const Vector3 dir = ray.GetDir(); // ray direction vector
|
---|
| 56 | const Vector3 w0 = ray.GetLoc() - mVertices[1];
|
---|
[1328] | 57 |
|
---|
| 58 | // params to calc ray-plane intersect
|
---|
[1344] | 59 | const float a = -DotProd(normal, w0);
|
---|
| 60 | const float b = DotProd(normal, dir);
|
---|
[1328] | 61 |
|
---|
[1344] | 62 | // check for division by zero
|
---|
[1328] | 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 |
|
---|
[1344] | 76 | // distance from origin of ray to plane
|
---|
[1328] | 77 | t = a / b;
|
---|
| 78 |
|
---|
| 79 | if (t < 0.0) // ray goes away from triangle
|
---|
| 80 | {
|
---|
[1344] | 81 | return Ray::NO_INTERSECTION; // => no intersect
|
---|
[1328] | 82 | }
|
---|
[1344] | 83 | // already found nearer intersection
|
---|
| 84 | else if ((ray.GetType() == Ray::LOCAL_RAY) && (t >= nearestT))
|
---|
| 85 | {
|
---|
| 86 | return Ray::NO_INTERSECTION;
|
---|
| 87 | }
|
---|
[1328] | 88 |
|
---|
[1344] | 89 | ///////////////////////////////////////////////
|
---|
| 90 | //-- found intersection point
|
---|
| 91 | //-- check if it is inside triangle
|
---|
| 92 |
|
---|
| 93 | const Vector3 pt = ray.GetLoc() + t * dir;
|
---|
[1328] | 94 |
|
---|
[1344] | 95 | const Vector3 w = pt - mVertices[1];
|
---|
[1328] | 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 |
|
---|
[1344] | 108 | if ((s < 0.0) || (s > 1.0)) // pt is outside triangle
|
---|
| 109 | {
|
---|
[1328] | 110 | return Ray::NO_INTERSECTION;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | const float s2 = (uv * wu - uu * wv) / D;
|
---|
| 114 |
|
---|
[1344] | 115 | if ((s2 < 0.0) || ((s + s2) > 1.0)) // pt is outside triangle
|
---|
| 116 | {
|
---|
[1328] | 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];
|
---|
[1344] | 140 | const Vector3 v2 = mVertices[2] - mVertices[1];
|
---|
[1328] | 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 | {
|
---|
[1344] | 154 | Vector3 v1 = mVertices[0] - mVertices[1], v2=mVertices[2] - mVertices[1];
|
---|
[1328] | 155 | return 0.5f * Magnitude(CrossProd(v2, v1));
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
[860] | 159 | } |
---|