[2751] | 1 | #include "Plane3.h"
|
---|
| 2 | #include "Matrix4x4.h"
|
---|
| 3 |
|
---|
| 4 |
|
---|
[2776] | 5 | namespace CHCDemoEngine
|
---|
[2751] | 6 | {
|
---|
| 7 |
|
---|
| 8 | Plane3::Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
| 9 | {
|
---|
[2755] | 10 | Vector3 v1 = a - b, v2 = c - b;
|
---|
| 11 | mNormal = Normalize(CrossProd(v2, v1));
|
---|
[2751] | 12 | mD = -DotProd(b, mNormal);
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | Plane3::Plane3(const Vector3 &normal, const Vector3 &point):
|
---|
| 17 | mNormal(normal)
|
---|
| 18 | {
|
---|
| 19 | mD = -DotProd(normal, point);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | bool PlaneIntersection(const Plane3 &a,
|
---|
| 24 | const Plane3 &b,
|
---|
| 25 | const Plane3 &c,
|
---|
| 26 | Vector3 &result)
|
---|
| 27 | {
|
---|
| 28 | Vector3 sx(a.mNormal.x, b.mNormal.x, c.mNormal.x),
|
---|
| 29 | sy(a.mNormal.y, b.mNormal.y, c.mNormal.y),
|
---|
[2755] | 30 | sz(a.mNormal.z, b.mNormal.z, c.mNormal.z),
|
---|
| 31 | sd(a.mD, b.mD, c.mD);
|
---|
[2751] | 32 |
|
---|
[2755] | 33 | Matrix4x4 md(a.mNormal, b.mNormal, c.mNormal), mx, my, mz;
|
---|
[2751] | 34 |
|
---|
[2755] | 35 | mx.SetColumns(sd, sy, sz);
|
---|
| 36 | my.SetColumns(sx, sd, sz);
|
---|
| 37 | mz.SetColumns(sx, sy, sd);
|
---|
[2751] | 38 |
|
---|
[2755] | 39 | const float det = md.Det3x3();
|
---|
[2751] | 40 |
|
---|
[2755] | 41 | if (fabs(det) < TRASH)
|
---|
| 42 | return false;
|
---|
| 43 |
|
---|
| 44 | result.SetValue(mx.Det3x3()/det,
|
---|
| 45 | my.Det3x3()/det,
|
---|
| 46 | mz.Det3x3()/det);
|
---|
| 47 |
|
---|
| 48 | return true;
|
---|
[2751] | 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
[2755] | 52 | bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2)
|
---|
| 53 | {
|
---|
| 54 | return
|
---|
| 55 | p1.mNormal.x != p2.mNormal.x ||
|
---|
| 56 | p1.mNormal.y != p2.mNormal.y ||
|
---|
| 57 | p1.mNormal.z != p2.mNormal.z ||
|
---|
| 58 | p1.mD == p2.mD;
|
---|
| 59 | }
|
---|
[2751] | 60 |
|
---|
| 61 |
|
---|
| 62 | Vector3 Plane3::FindIntersection(const Vector3 &a,
|
---|
| 63 | const Vector3 &b,
|
---|
| 64 | float *t,
|
---|
| 65 | bool *coplanar) const
|
---|
| 66 | {
|
---|
| 67 | // line from A to B
|
---|
| 68 | const Vector3 v = b - a;
|
---|
| 69 | float dv = DotProd(v, mNormal);
|
---|
| 70 |
|
---|
| 71 | if (signum(dv) == 0)
|
---|
| 72 | {
|
---|
| 73 | if (coplanar) (*coplanar) = true;
|
---|
| 74 |
|
---|
| 75 | if (t) (*t) = 0;
|
---|
| 76 |
|
---|
| 77 | return a;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | if (coplanar) (*coplanar) = false;
|
---|
| 81 |
|
---|
| 82 | float u = - Distance(a) / dv;
|
---|
| 83 |
|
---|
| 84 | if (t) (*t) = u;
|
---|
| 85 |
|
---|
| 86 | return a + u * v;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | int Plane3::Side(const Vector3 &v, const float threshold) const
|
---|
| 91 | {
|
---|
| 92 | return signum(Distance(v), threshold);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | float Plane3::FindT(const Vector3 &a, const Vector3 &b) const
|
---|
| 97 | {
|
---|
| 98 | const Vector3 v = b - a; // line from A to B
|
---|
| 99 | const float dv = DotProd(v, mNormal);
|
---|
| 100 |
|
---|
| 101 | // does not intersect or coincident
|
---|
| 102 | if (signum(dv) == 0)
|
---|
| 103 | return 0;
|
---|
| 104 |
|
---|
| 105 | return - Distance(a) / dv;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | }
|
---|