6 @maintainer Morgan McGuire, matrix@graphics3d.com
15 #include "G3D/platform.h"
16 #include "G3D/Vector3.h"
17 #include "G3D/Vector4.h"
22 An infinite 2D plane in 3D space.
27 /** normal.Dot(x,y,z) = distance */
32 Assumes the normal has unit length.
34 Plane(const Vector3
& n
, float d
) : _normal(n
), _distance(d
) {
39 Plane() : _normal(Vector3::unitY()), _distance(0) {
43 Constructs a plane from three points.
46 const Vector3
& point0
,
47 const Vector3
& point1
,
48 const Vector3
& point2
);
51 Constructs a plane from three points, where at most two are
52 at infinity (w = 0, not xyz = inf).
60 The normal will be unitized.
63 const Vector3
& __normal
,
64 const Vector3
& point
);
66 static Plane
fromEquation(float a
, float b
, float c
, float d
);
71 Returns true if point is on the side the normal points to or
74 inline bool halfSpaceContains(Vector3 point
) const {
75 // Clamp to a finite range for testing
76 point
= point
.clamp(Vector3::minFinite(), Vector3::maxFinite());
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
;
84 Returns true if point is on the side the normal points to or
87 inline bool halfSpaceContains(const Vector4
& point
) const {
89 return _normal
.dot(point
.xyz()) > 0;
91 return halfSpaceContains(point
.xyz() / point
.w
);
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.
99 inline bool halfSpaceContainsFinite(const Vector3
& point
) const {
100 debugAssert(point
.isFinite());
101 return _normal
.dot(point
) >= _distance
;
105 Returns true if the point is nearly in the plane.
107 inline bool fuzzyContains(const Vector3
&point
) const {
108 return fuzzyEq(point
.dot(_normal
), _distance
);
111 inline const Vector3
& normal() const {
116 Returns distance from point to plane. Distance is negative if point is behind (not in plane in direction opposite normal) the plane.
118 inline float distance(const Vector3
& x
) const {
119 return (_normal
.dot(x
) - _distance
);
122 inline Vector3
closestPoint(const Vector3
& x
) const {
123 return x
+ (_normal
* (-distance(x
)));
126 /** Returns normal * distance from origin */
127 Vector3
center() const {
128 return _normal
* _distance
;
132 Inverts the facing direction of the plane so the new normal
133 is the inverse of the old normal.
138 Returns the equation in the form:
140 <CODE>normal.Dot(Vector3(<I>x</I>, <I>y</I>, <I>z</I>)) + d = 0</CODE>
142 void getEquation(Vector3
&normal
, double& d
) const;
143 void getEquation(Vector3
&normal
, float& d
) const;
148 void getEquation(double& a
, double& b
, double& c
, double& d
) const;
149 void getEquation(float& a
, float& b
, float& c
, float& d
) const;
151 std::string
toString() const;