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