[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,
|
---|
[437] | 17 | const Vector3 &b,
|
---|
| 18 | const Vector3 &c
|
---|
[372] | 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,
|
---|
[504] | 26 | const Vector3 &point
|
---|
| 27 | ):mNormal(normal)
|
---|
[372] | 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 |
|
---|
[437] | 46 | /** Finds intersection of line segment between points a and b with plane.
|
---|
| 47 | @param a start point
|
---|
| 48 | @param b end point
|
---|
| 49 | @param t if not NULL, returns parameter value of intersections
|
---|
| 50 | @param coplanar if not NULL, returns true if plane and line segments are coplanar.
|
---|
| 51 | */
|
---|
[372] | 52 | Vector3 FindIntersection(const Vector3 &a,
|
---|
[448] | 53 | const Vector3 &b,
|
---|
| 54 | float *t = NULL,
|
---|
| 55 | bool *coplanar = NULL) const
|
---|
[372] | 56 | {
|
---|
| 57 | const Vector3 v = b - a; // line from A to B
|
---|
| 58 | float dv = DotProd(v, mNormal);
|
---|
| 59 |
|
---|
| 60 | if (signum(dv) == 0)
|
---|
| 61 | {
|
---|
| 62 | if (coplanar) (*coplanar) = true;
|
---|
[437] | 63 | if (t) (*t) = 0;
|
---|
[372] | 64 | return a;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (coplanar) (*coplanar) = false;
|
---|
| 68 | float u = - Distance(a) / dv; // TODO: could be done more efficiently
|
---|
| 69 |
|
---|
| 70 | if (t) (*t) = u;
|
---|
| 71 |
|
---|
| 72 | return a + u * v;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[437] | 75 | /** Finds value of intersection parameter t on line segment from a to b.
|
---|
| 76 | @returns -1 if coplanar, else parameter t
|
---|
| 77 | */
|
---|
[485] | 78 | float FindT(const Vector3 &a, const Vector3 &b) const
|
---|
[372] | 79 | {
|
---|
| 80 | const Vector3 v = b - a; // line from A to B
|
---|
| 81 | const float dv = DotProd(v, mNormal);
|
---|
| 82 |
|
---|
[487] | 83 | // does not intersect or coincident
|
---|
[372] | 84 | if (signum(dv) == 0)
|
---|
[487] | 85 | return 0;
|
---|
[372] | 86 |
|
---|
| 87 | return - Distance(a) / dv; // TODO: could be done more efficiently
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | friend bool
|
---|
| 91 | PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result);
|
---|
| 92 |
|
---|
| 93 | friend ostream &operator<<(ostream &s, const Plane3 p) {
|
---|
| 94 | s<<p.mNormal<<" "<<p.mD;
|
---|
| 95 | return s;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | #endif
|
---|