00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00014
00015 #ifndef plane_H
00016 #define plane_H
00017
00018 #include <math/vec3.hpp>
00019
00020 namespace math {
00021 template<typename T> class plane {
00022 public:
00023 enum
00024 {
00025
00026 POINT_ON_PLANE=0,
00027 POINT_IN_FRONT_OF_PLANE=1,
00028 POINT_BEHIND_PLANE=2
00029 };
00030
00034 plane(): n(vec3<double>(0.0f, 0.0f, 0.0f)), d(0.0f) {}
00035 plane(vec3<double> newNormal, float newIntercept): n(newNormal), d(newIntercept) {}
00036 plane(const plane & rhs) {
00037 n=rhs.n;
00038 d=rhs.d;
00039 }
00041 ~plane() {}
00042 void setFromPoints(const math::vec3<double> & p0, const math::vec3<double> & p1, const math::vec3<double> & p2)
00043 {
00044 n=(p1-p0).cross(p2-p0);
00045
00046 n.Normalize();
00047
00048 CalculateIntercept(p0);
00049 }
00050 void normalize() {
00051 float nLength=n.magnitude();
00052 n/=nLength;
00053 d/=nLength;
00054 }
00055 bool Intersect3(math::plane<T> const & p2, math::plane<T> const & p3, math::vec3<double> & result)
00056 {
00057 float denominator=n.dot((p2.n).cross(p3.n));
00058
00059 if(denominator==0.0f)
00060 return false;
00061
00062 math::vec3<double> temp1, temp2, temp3;
00063 temp1=(p2.n.cross(p3.n))*d;
00064 temp2=(p3.n.cross(n))*p2.d;
00065 temp3=(n.cross(p2.n))*p3.d;
00066
00067 result=(temp1+temp2+temp3)/(-denominator);
00068
00069 return true;
00070 }
00071
00072 float GetDistance(const math::vec3<double> & point) const
00073 {
00074 return point.dot(n) + d;
00075 }
00076
00077 int ClassifyPoint(const math::vec3<double> & point) const
00078 {
00079 float distance = GetDistance(point);
00080
00081 if(distance>EPSILON)
00082 return POINT_IN_FRONT_OF_PLANE;
00083
00084 if(distance<-EPSILON)
00085 return POINT_BEHIND_PLANE;
00086
00087 return POINT_ON_PLANE;
00088 }
00089
00090 math::plane<T> lerp(math::plane<T> const & p2, float factor) {
00091 math::plane<T> result;
00092
00093 result.n = n*(1.0f-factor) + p2.n*factor;
00094
00095 result.n.Normalize();
00096
00097 result.d = d * (1.0f-factor) + p2.d * factor;
00098
00099 return result;
00100 }
00101 bool operator==(math::plane<T> const & rhs) const {
00102 if(n==rhs.n && d == rhs.d)
00103 return true;
00104
00105 return false;
00106 }
00107
00108 void SetNormal(const vec3<double> & rhs) {
00109 n=rhs;
00110 }
00111 void SetIntercept(float newIntercept) {
00112 d=newIntercept;
00113 }
00114 void SetFromPoints(const vec3<double> & p0, const vec3<double> & p1, const vec3<double> & p2);
00115
00116 void CalculateIntercept(const vec3<double> & pointOnPlane) { d=-n.dot(pointOnPlane); }
00117
00118 vec3<double> getNormal() {
00119 return n;
00120 }
00121 float getIntercept() {
00122 return d;
00123 }
00124
00125
00126
00127
00128 bool operator!=(const plane & rhs) const
00129 { return!((*this)==rhs); }
00130
00131
00132 plane operator-(void) const {return plane(-n, -d);}
00133 plane operator+(void) const {return (*this);}
00134
00135
00136 vec3<double> n;
00137 float d;
00138 };
00139 }
00140
00141
00142 #endif //plane_H