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