Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / include / g3dlite / G3D / Plane.h
blobf00c15c0988aa82fb75629c90c4712adc78f6c8c
1 /**
2 @file Plane.h
4 Plane class
6 @maintainer Morgan McGuire, matrix@graphics3d.com
8 @created 2001-06-02
9 @edited 2004-07-18
12 #ifndef G3D_PLANE_H
13 #define G3D_PLANE_H
15 #include "G3D/platform.h"
16 #include "G3D/Vector3.h"
17 #include "G3D/Vector4.h"
19 namespace G3D {
21 /**
22 An infinite 2D plane in 3D space.
24 class Plane {
25 private:
27 /** normal.Dot(x,y,z) = distance */
28 Vector3 _normal;
29 float _distance;
31 /**
32 Assumes the normal has unit length.
34 Plane(const Vector3& n, float d) : _normal(n), _distance(d) {
37 public:
39 Plane() : _normal(Vector3::unitY()), _distance(0) {
42 /**
43 Constructs a plane from three points.
45 Plane(
46 const Vector3& point0,
47 const Vector3& point1,
48 const Vector3& point2);
50 /**
51 Constructs a plane from three points, where at most two are
52 at infinity (w = 0, not xyz = inf).
54 Plane(
55 Vector4 point0,
56 Vector4 point1,
57 Vector4 point2);
59 /**
60 The normal will be unitized.
62 Plane(
63 const Vector3& __normal,
64 const Vector3& point);
66 static Plane fromEquation(float a, float b, float c, float d);
68 virtual ~Plane() {}
70 /**
71 Returns true if point is on the side the normal points to or
72 is in the plane.
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;
83 /**
84 Returns true if point is on the side the normal points to or
85 is in the plane.
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);
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.
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 {
112 return _normal;
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.
135 void flip();
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;
146 ax + by + cz + d = 0
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;
154 } // namespace
156 #endif