AI_Plane.h

Go to the documentation of this file.
00001 #ifndef AI_PLANE_H
00002 #define AI_PLANE_H
00003 //------------------------------------------------------------------------------
00012 #include "AI_Vector.h"
00013 #include "AI_Line.h"
00014 
00015 class AI_Plane 
00016 {
00017 public:
00019     AI_Plane();
00021     AI_Plane(float A, float B, float C, float D);
00023     AI_Plane(const AI_Plane& p);
00025     AI_Plane(const AI_Vector3& v0, const AI_Vector3& v1, const AI_Vector3& v2);
00027     void set(float A, float B, float C, float D);
00029     void set(const AI_Vector3& v0, const AI_Vector3& v1, const AI_Vector3& v2);
00031     float distance(const AI_Vector3& v) const;
00033     AI_Vector3 normal() const;
00035     bool intersect(const AI_Line3& l, float& t) const;
00037     bool intersect(const AI_Plane& p, AI_Line3& l) const;
00038 
00039     float a , b, c, d;
00040 };
00041 
00042 //------------------------------------------------------------------------------
00045 inline
00046 AI_Plane::AI_Plane() :
00047     a(0.0f),
00048     b(0.0f),
00049     c(0.0f),
00050     d(1.0f)
00051 {
00052     // empty
00053 }
00054 
00055 //------------------------------------------------------------------------------
00058 inline
00059 AI_Plane::AI_Plane(float A, float B, float C, float D) :
00060     a(A),
00061     b(B),
00062     c(C),
00063     d(D)
00064 {
00065     // empty
00066 }
00067 
00068 //------------------------------------------------------------------------------
00071 inline
00072 AI_Plane::AI_Plane(const AI_Plane& rhs) :
00073     a(rhs.a),
00074     b(rhs.b),
00075     c(rhs.c),
00076     d(rhs.d)
00077 {
00078     // empty
00079 }
00080 
00081 //------------------------------------------------------------------------------
00084 inline
00085 void
00086 AI_Plane::set(float A, float B, float C, float D)
00087 {
00088     this->a = A;
00089     this->b = B;
00090     this->c = C;
00091     this->d = D;
00092 }
00093 
00094 //------------------------------------------------------------------------------
00098 inline
00099 void
00100 AI_Plane::set(const AI_Vector3& v0, const AI_Vector3& v1, const AI_Vector3& v2)
00101 {
00102     AI_Vector3 cross((v2 - v0) * (v1 - v0));
00103     cross.norm();
00104     this->a = cross.x;
00105     this->b = cross.y;
00106     this->c = cross.z;
00107     this->d = -(a * v0.x + b * v0.y + c * v0.z);
00108 }
00109 
00110 //------------------------------------------------------------------------------
00113 inline
00114 AI_Plane::AI_Plane(const AI_Vector3& v0, const AI_Vector3& v1, const AI_Vector3& v2)
00115 {
00116     this->set(v0, v1, v2);
00117 }
00118 
00119 //------------------------------------------------------------------------------
00124 inline
00125 float
00126 AI_Plane::distance(const AI_Vector3& v) const
00127 {
00128     return this->a * v.x + this->b * v.y + this->c * v.z + this->d;
00129 }
00130 
00131 //------------------------------------------------------------------------------
00135 inline
00136 AI_Vector3 
00137 AI_Plane::normal() const
00138 {
00139     return AI_Vector3(this->a, this->b, this->c);
00140 }
00141 
00142 //------------------------------------------------------------------------------
00147 inline
00148 bool
00149 AI_Plane::intersect(const AI_Line3& l, float& t) const
00150 {
00151     float f0 = this->a * l.b.x + this->b * l.b.y + this->c * l.b.z + this->d;
00152     float f1 = this->a * -l.m.x + this->b * -l.m.y + this->c * -l.m.z;
00153     if ((f1 < -0.0001f) || (f1 > 0.0001f))
00154     {
00155         t = f0 / f1;
00156         return true;
00157     }
00158     else
00159     {
00160         return false;
00161     }
00162 }
00163 
00164 //------------------------------------------------------------------------------
00168 inline
00169 bool
00170 AI_Plane::intersect(const AI_Plane& p, AI_Line3& l) const
00171 {
00172     AI_Vector3 n0 = this->normal();
00173     AI_Vector3 n1 = p.normal();
00174     float n00 = n0 % n0;
00175     float n01 = n0 % n1;
00176     float n11 = n1 % n1;
00177     float det = n00 * n11 - n01 * n01;
00178     const float tol = 1e-06f;
00179     if (fabs(det) < tol) 
00180     {
00181         return false;
00182     }
00183     else 
00184     {
00185         float inv_det = 1.0f/det;
00186         float c0 = (n11 * this->d - n01 * p.d)    * inv_det;
00187         float c1 = (n00 * p.d - n01 * this->d)* inv_det;
00188         l.m = n0 * n1;
00189         l.b = n0 * c0 + n1 * c1;
00190         return true;
00191     }
00192 }
00193 
00194 //------------------------------------------------------------------------------
00195 #endif