1 | /** |
---|
2 | @file Plane.h |
---|
3 | |
---|
4 | Plane class |
---|
5 | |
---|
6 | @maintainer Morgan McGuire, matrix@graphics3d.com |
---|
7 | |
---|
8 | @created 2001-06-02 |
---|
9 | @edited 2004-07-18 |
---|
10 | */ |
---|
11 | |
---|
12 | #ifndef G3D_PLANE_H |
---|
13 | #define G3D_PLANE_H |
---|
14 | |
---|
15 | #include "G3D/platform.h" |
---|
16 | #include "G3D/Vector3.h" |
---|
17 | #include "G3D/Vector4.h" |
---|
18 | |
---|
19 | namespace G3D { |
---|
20 | |
---|
21 | /** |
---|
22 | An infinite 2D plane in 3D space. |
---|
23 | */ |
---|
24 | class Plane { |
---|
25 | private: |
---|
26 | |
---|
27 | /** normal.Dot(x,y,z) = distance */ |
---|
28 | Vector3 _normal; |
---|
29 | float _distance; |
---|
30 | |
---|
31 | /** |
---|
32 | Assumes the normal has unit length. |
---|
33 | */ |
---|
34 | Plane(const Vector3& n, float d) : _normal(n), _distance(d) { |
---|
35 | } |
---|
36 | |
---|
37 | public: |
---|
38 | |
---|
39 | Plane() : _normal(Vector3::unitY()), _distance(0) { |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | Constructs a plane from three points. |
---|
44 | */ |
---|
45 | Plane( |
---|
46 | const Vector3& point0, |
---|
47 | const Vector3& point1, |
---|
48 | const Vector3& point2); |
---|
49 | |
---|
50 | /** |
---|
51 | Constructs a plane from three points, where at most two are |
---|
52 | at infinity (w = 0, not xyz = inf). |
---|
53 | */ |
---|
54 | Plane( |
---|
55 | Vector4 point0, |
---|
56 | Vector4 point1, |
---|
57 | Vector4 point2); |
---|
58 | |
---|
59 | /** |
---|
60 | The normal will be unitized. |
---|
61 | */ |
---|
62 | Plane( |
---|
63 | const Vector3& __normal, |
---|
64 | const Vector3& point); |
---|
65 | |
---|
66 | static Plane fromEquation(float a, float b, float c, float d); |
---|
67 | |
---|
68 | virtual ~Plane() {} |
---|
69 | |
---|
70 | /** |
---|
71 | Returns true if point is on the side the normal points to or |
---|
72 | is in the plane. |
---|
73 | */ |
---|
74 | inline bool halfSpaceContains(Vector3 point) const { |
---|
75 | // Clamp to a finite range for testing |
---|
76 | point = point.clamp(Vector3::minFinite(), Vector3::maxFinite()); |
---|
77 | |
---|
78 | // We can get away with putting values *at* the limits of the float32 range into |
---|
79 | // a dot product, since the dot product is carried out on float64. |
---|
80 | return _normal.dot(point) >= _distance; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | Returns true if point is on the side the normal points to or |
---|
85 | is in the plane. |
---|
86 | */ |
---|
87 | inline bool halfSpaceContains(const Vector4& point) const { |
---|
88 | if (point.w == 0) { |
---|
89 | return _normal.dot(point.xyz()) > 0; |
---|
90 | } else { |
---|
91 | return halfSpaceContains(point.xyz() / point.w); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | Returns true if point is on the side the normal points to or |
---|
97 | is in the plane. Only call on finite points. Faster than halfSpaceContains. |
---|
98 | */ |
---|
99 | inline bool halfSpaceContainsFinite(const Vector3& point) const { |
---|
100 | debugAssert(point.isFinite()); |
---|
101 | return _normal.dot(point) >= _distance; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | Returns true if the point is nearly in the plane. |
---|
106 | */ |
---|
107 | inline bool fuzzyContains(const Vector3 &point) const { |
---|
108 | return fuzzyEq(point.dot(_normal), _distance); |
---|
109 | } |
---|
110 | |
---|
111 | inline const Vector3& normal() const { |
---|
112 | return _normal; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | Returns distance from point to plane. Distance is negative if point is behind (not in plane in direction opposite normal) the plane. |
---|
117 | */ |
---|
118 | inline float distance(const Vector3& x) const { |
---|
119 | return (_normal.dot(x) - _distance); |
---|
120 | } |
---|
121 | |
---|
122 | inline Vector3 closestPoint(const Vector3& x) const { |
---|
123 | return x + (_normal * (-distance(x))); |
---|
124 | } |
---|
125 | |
---|
126 | /** Returns normal * distance from origin */ |
---|
127 | Vector3 center() const { |
---|
128 | return _normal * _distance; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | Inverts the facing direction of the plane so the new normal |
---|
133 | is the inverse of the old normal. |
---|
134 | */ |
---|
135 | void flip(); |
---|
136 | |
---|
137 | /** |
---|
138 | Returns the equation in the form: |
---|
139 | |
---|
140 | <CODE>normal.Dot(Vector3(<I>x</I>, <I>y</I>, <I>z</I>)) + d = 0</CODE> |
---|
141 | */ |
---|
142 | void getEquation(Vector3 &normal, double& d) const; |
---|
143 | void getEquation(Vector3 &normal, float& d) const; |
---|
144 | |
---|
145 | /** |
---|
146 | ax + by + cz + d = 0 |
---|
147 | */ |
---|
148 | void getEquation(double& a, double& b, double& c, double& d) const; |
---|
149 | void getEquation(float& a, float& b, float& c, float& d) const; |
---|
150 | |
---|
151 | std::string toString() const; |
---|
152 | }; |
---|
153 | |
---|
154 | } // namespace |
---|
155 | |
---|
156 | #endif |
---|