#ifndef __Plane3_H__ #define __Plane3_H__ #include "Vector3.h" namespace CHCDemoEngine { /** Class describing a plane in 3D space. */ class Plane3 { public: Plane3() {} Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c); /** Note: we assume that the normal vector is already normalized. */ Plane3(const Vector3 &normal, const Vector3 &point); inline void ReverseOrientation() { mNormal *= -1; mD *= -1; } /** Get distance to a point. */ inline float Distance(const Vector3 &v) const { return DotProd(v, mNormal) + mD; } enum {BACK_SIDE = -1, INTERSECTS = 0, FRONT_SIDE = 1}; /** Returns 1 if v is on front side, -1 if on back side, 0 if on plane. */ int Side(const Vector3 &v, float threshold = 1e-6) const; /** Finds intersection of line segment between points a and b with plane. @param a start point @param b end point @param t if not NULL, returns parameter value of intersections @param coplanar if not NULL, returns true if plane and line segments are coplanar. */ Vector3 FindIntersection(const Vector3 &a, const Vector3 &b, float *t = NULL, bool *coplanar = NULL) const; /** Finds value of intersection parameter t on line segment from a to b. @returns 0 if coplanar, else parameter t */ float FindT(const Vector3 &a, const Vector3 &b) const; operator const float*() const { return (const float*) this; } float& operator[] (const int inx) { return (&mNormal.x)[inx]; } const float &operator[] (const int inx) const { return *(&mNormal.x + inx); } /** Transforms the plane with the matrix m. */ void Transform(const Matrix4x4 &m); /////////// //-- friends friend bool PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result); friend bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2); friend std::ostream &operator<<(std::ostream &s, const Plane3 &p) { s << p.mNormal << " " << p.mD; return s; } //protected: /////////////// Vector3 mNormal; float mD; }; } #endif