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