[372] | 1 | #ifndef _Plane3_H__
|
---|
| 2 | #define _Plane3_H__
|
---|
| 3 |
|
---|
| 4 | #include "Vector3.h"
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | /** 3D Plane */
|
---|
| 8 | class Plane3 {
|
---|
| 9 | public:
|
---|
| 10 |
|
---|
| 11 | Vector3 mNormal;
|
---|
| 12 | float mD;
|
---|
| 13 |
|
---|
| 14 | Plane3() {}
|
---|
| 15 |
|
---|
| 16 | Plane3(const Vector3 &a,
|
---|
| 17 | const Vector3 &b,
|
---|
| 18 | const Vector3 &c
|
---|
| 19 | ) {
|
---|
| 20 | Vector3 v1=a-b, v2=c-b;
|
---|
| 21 | mNormal = Normalize(CrossProd(v2,v1));
|
---|
| 22 | mD = -DotProd(b, mNormal);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | Plane3(const Vector3 &normal,
|
---|
| 26 | const Vector3 &point
|
---|
| 27 | ):mNormal(normal)
|
---|
| 28 | {
|
---|
| 29 | mD = -DotProd(normal, point);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[396] | 32 | void ReverseOrientation()
|
---|
| 33 | {
|
---|
| 34 | mNormal *= -1;
|
---|
| 35 | mD *= -1;
|
---|
| 36 | }
|
---|
[372] | 37 |
|
---|
| 38 | float Distance(const Vector3 &v) const {
|
---|
| 39 | return DotProd(v, mNormal) + mD;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | int Side(const Vector3 &v, const float threshold = 1e-6) const {
|
---|
| 43 | return signum(Distance(v), threshold);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | Vector3 FindIntersection(const Vector3 &a,
|
---|
| 47 | const Vector3 &b,
|
---|
| 48 | float *t = NULL,
|
---|
| 49 | bool *coplanar = NULL
|
---|
| 50 | ) const
|
---|
| 51 | {
|
---|
| 52 | const Vector3 v = b - a; // line from A to B
|
---|
| 53 | float dv = DotProd(v, mNormal);
|
---|
| 54 |
|
---|
| 55 | if (signum(dv) == 0)
|
---|
| 56 | {
|
---|
| 57 | if (coplanar) (*coplanar) = true;
|
---|
| 58 | if (t) (*t) = 1;
|
---|
| 59 | return a;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (coplanar) (*coplanar) = false;
|
---|
| 63 | float u = - Distance(a) / dv; // TODO: could be done more efficiently
|
---|
| 64 |
|
---|
| 65 | if (t) (*t) = u;
|
---|
| 66 |
|
---|
| 67 | return a + u * v;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | float FindT(const Vector3 &a,
|
---|
| 71 | const Vector3 &b) const
|
---|
| 72 | {
|
---|
| 73 | const Vector3 v = b - a; // line from A to B
|
---|
| 74 | const float dv = DotProd(v, mNormal);
|
---|
| 75 |
|
---|
| 76 | if (signum(dv) == 0)
|
---|
| 77 | return 1;
|
---|
| 78 |
|
---|
| 79 | return - Distance(a) / dv; // TODO: could be done more efficiently
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | friend bool
|
---|
| 83 | PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result);
|
---|
| 84 |
|
---|
| 85 | friend ostream &operator<<(ostream &s, const Plane3 p) {
|
---|
| 86 | s<<p.mNormal<<" "<<p.mD;
|
---|
| 87 | return s;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | #endif
|
---|