1 // This code is in the public domain -- castanyo@yahoo.es 2 3 #ifndef NV_MATH_PLANE_H 4 #define NV_MATH_PLANE_H 5 6 #include <nvmath/nvmath.h> 7 #include <nvmath/Vector.h> 8 9 namespace nv 10 { 11 class Matrix; 12 13 14 class NVMATH_CLASS Plane 15 { 16 public: 17 typedef Plane const & Arg; 18 19 Plane(); 20 Plane(float x, float y, float z, float w); 21 Plane(Vector4::Arg v); 22 Plane(Vector3::Arg v, float d); 23 Plane(Vector3::Arg normal, Vector3::Arg point); 24 25 const Plane & operator=(Plane::Arg v); 26 27 Vector3 vector() const; 28 scalar offset() const; 29 30 const Vector4 & asVector() const; 31 Vector4 & asVector(); 32 33 void operator*=(scalar s); 34 35 private: 36 Vector4 p; 37 }; 38 Plane()39 inline Plane::Plane() {} Plane(float x,float y,float z,float w)40 inline Plane::Plane(float x, float y, float z, float w) : p(x, y, z, w) {} Plane(Vector4::Arg v)41 inline Plane::Plane(Vector4::Arg v) : p(v) {} Plane(Vector3::Arg v,float d)42 inline Plane::Plane(Vector3::Arg v, float d) : p(v, d) {} Plane(Vector3::Arg normal,Vector3::Arg point)43 inline Plane::Plane(Vector3::Arg normal, Vector3::Arg point) : p(normal, dot(normal, point)) {} 44 45 inline const Plane & Plane::operator=(Plane::Arg v) { p = v.p; return *this; } 46 vector()47 inline Vector3 Plane::vector() const { return p.xyz(); } offset()48 inline scalar Plane::offset() const { return p.w(); } 49 asVector()50 inline const Vector4 & Plane::asVector() const { return p; } asVector()51 inline Vector4 & Plane::asVector() { return p; } 52 53 // Normalize plane. 54 inline Plane normalize(Plane::Arg plane, float epsilon = NV_EPSILON) 55 { 56 const float len = length(plane.vector()); 57 nvDebugCheck(!isZero(len, epsilon)); 58 const float inv = 1.0f / len; 59 return Plane(plane.asVector() * inv); 60 } 61 62 // Get the distance from the given point to this plane. distance(Plane::Arg plane,Vector3::Arg point)63 inline float distance(Plane::Arg plane, Vector3::Arg point) 64 { 65 return dot(plane.vector(), point) - plane.offset(); 66 } 67 68 inline void Plane::operator*=(scalar s) 69 { 70 scale(p, s); 71 } 72 73 Plane transformPlane(const Matrix&, Plane::Arg); 74 75 } // nv namespace 76 77 #endif // NV_MATH_PLANE_H 78