[2750] | 1 | #include "Triangle3.h"
|
---|
| 2 | #include "AxisAlignedBox3.h"
|
---|
[2751] | 3 | #include "common.h"
|
---|
| 4 | #include "Plane3.h"
|
---|
[2750] | 5 |
|
---|
| 6 |
|
---|
| 7 | using namespace std;
|
---|
| 8 |
|
---|
| 9 |
|
---|
[2776] | 10 | namespace CHCDemoEngine {
|
---|
[2751] | 11 |
|
---|
[2750] | 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::GetPlaneIntersection(const Plane3 &plane,
|
---|
| 65 | Vector3 &intersectA,
|
---|
| 66 | Vector3 &intersectB) const
|
---|
| 67 | {
|
---|
| 68 | int side[3];
|
---|
| 69 |
|
---|
| 70 | // compute distance from plane
|
---|
| 71 | for (int i = 0; i < 3; ++ i)
|
---|
| 72 | side[i] = plane.Side(mVertices[i], Limits::Small);
|
---|
| 73 |
|
---|
| 74 | /////
|
---|
| 75 | // no intersection => early exit
|
---|
| 76 | if (((side[0] > 0) && (side[1] > 0) && (side[2] > 0)) ||
|
---|
| 77 | ((side[0] < 0) && (side[1] < 0) && (side[2] < 0)))
|
---|
| 78 | {
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /////////////
|
---|
| 83 | // at least 2 triangle vertices lie in plane => early exit
|
---|
| 84 |
|
---|
| 85 | for (int i = 0; i < 3; ++ i)
|
---|
| 86 | {
|
---|
| 87 | if (!side[i] && !side[(i + 1) % 3])
|
---|
| 88 | {
|
---|
| 89 | intersectA = mVertices[i];
|
---|
| 90 | intersectB = mVertices[(i + 1) % 3];
|
---|
| 91 |
|
---|
| 92 | return true;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | bool foundA = false;
|
---|
| 97 |
|
---|
| 98 | // compute intersection points
|
---|
| 99 | for (int i = 0; i < 3; ++ i)
|
---|
| 100 | {
|
---|
| 101 | const int i_2 = (i + 1) % 3;
|
---|
| 102 |
|
---|
| 103 | // intersection found
|
---|
| 104 | if ((side[i] >= 0) && (side[i_2] <= 0) ||
|
---|
| 105 | (side[i] <= 0) && (side[i_2] >= 0))
|
---|
| 106 | {
|
---|
| 107 | const float t = plane.FindT(mVertices[i], mVertices[i_2]);
|
---|
| 108 |
|
---|
| 109 | if (!foundA)
|
---|
| 110 | {
|
---|
| 111 | intersectA = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
| 112 | foundA = true;
|
---|
| 113 | }
|
---|
| 114 | else
|
---|
| 115 | {
|
---|
| 116 | intersectB = mVertices[i] + t * (mVertices[i_2] - mVertices[i]);
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | cout << "warning! wrong triangle - plane intersection" << endl;
|
---|
| 123 | return false; // something went wrong!
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
[2957] | 127 | bool Triangle3::Valid() const
|
---|
| 128 | {
|
---|
| 129 | const Vector3 a = mVertices[0] - mVertices[1];
|
---|
| 130 | const Vector3 b = mVertices[0] - mVertices[2];
|
---|
| 131 | const Vector3 cross_a_b = CrossProd(a, b);
|
---|
| 132 |
|
---|
| 133 | const float eps = 1e-6f;
|
---|
| 134 |
|
---|
| 135 | if (SqrMagnitude(cross_a_b) <= eps * eps)
|
---|
| 136 | {
|
---|
| 137 | // v0, v1 & v2 lies on a line (area == 0)
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return true;
|
---|
[2750] | 142 | }
|
---|
[2957] | 143 |
|
---|
| 144 |
|
---|
| 145 | }
|
---|