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 |
|
---|
32 | void ReverseOrientation()
|
---|
33 | {
|
---|
34 | mNormal *= -1;
|
---|
35 | mD *= -1;
|
---|
36 | }
|
---|
37 |
|
---|
38 | float Distance(const Vector3 &v) const {
|
---|
39 | return DotProd(v, mNormal) + mD;
|
---|
40 | }
|
---|
41 |
|
---|
42 | enum {BACK_SIDE = -1, INTERSECTS = 0, FRONT_SIDE = 1};
|
---|
43 |
|
---|
44 |
|
---|
45 | /** Returns 1 if v is on front side, -1 if on back side, 0 if on plane.
|
---|
46 | */
|
---|
47 | int Side(const Vector3 &v, const float threshold = 1e-6) const;
|
---|
48 |
|
---|
49 | /** Finds intersection of line segment between points a and b with plane.
|
---|
50 | @param a start point
|
---|
51 | @param b end point
|
---|
52 | @param t if not NULL, returns parameter value of intersections
|
---|
53 | @param coplanar if not NULL, returns true if plane and line segments are coplanar.
|
---|
54 | */
|
---|
55 | Vector3 FindIntersection(const Vector3 &a,
|
---|
56 | const Vector3 &b,
|
---|
57 | float *t = NULL,
|
---|
58 | bool *coplanar = NULL) const;
|
---|
59 |
|
---|
60 | /** Finds value of intersection parameter t on line segment from a to b.
|
---|
61 | @returns 0 if coplanar, else parameter t
|
---|
62 | */
|
---|
63 | float FindT(const Vector3 &a, const Vector3 &b) const;
|
---|
64 |
|
---|
65 | friend bool
|
---|
66 | PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result);
|
---|
67 |
|
---|
68 | friend ostream &operator<<(ostream &s, const Plane3 p) {
|
---|
69 | s<<p.mNormal<<" "<<p.mD;
|
---|
70 | return s;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | };
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | #endif
|
---|