newspeoplefor developersdocumentationdownloads

plane.h

Go to the documentation of this file.
00001 #ifndef N_PLANE_H
00002 #define N_PLANE_H
00003 //------------------------------------------------------------------------------
00012 #include "mathlib/vector.h"
00013 #include "mathlib/line.h"
00014 
00015 //------------------------------------------------------------------------------
00016 class plane
00017 {
00018 public:
00020     plane();
00022     plane(float A, float B, float C, float D);
00024     plane(const plane& p);
00026     plane(const vector3& v0, const vector3& v1, const vector3& v2);
00028     void set(float A, float B, float C, float D);
00030     void set(const vector3& v0, const vector3& v1, const vector3& v2);
00032     float distance(const vector3& v) const;
00034     vector3 normal() const;
00036     bool intersect(const line3& l, float& t) const;
00038     bool intersect(const plane& p, line3& l) const;
00039 
00040     float a , b, c, d;
00041 };
00042 
00043 //------------------------------------------------------------------------------
00046 inline
00047 plane::plane() :
00048     a(0.0f),
00049     b(0.0f),
00050     c(0.0f),
00051     d(1.0f)
00052 {
00053     // empty
00054 }
00055 
00056 //------------------------------------------------------------------------------
00059 inline
00060 plane::plane(float A, float B, float C, float D) :
00061     a(A),
00062     b(B),
00063     c(C),
00064     d(D)
00065 {
00066     // empty
00067 }
00068 
00069 //------------------------------------------------------------------------------
00072 inline
00073 plane::plane(const plane& rhs) :
00074     a(rhs.a),
00075     b(rhs.b),
00076     c(rhs.c),
00077     d(rhs.d)
00078 {
00079     // empty
00080 }
00081 
00082 //------------------------------------------------------------------------------
00085 inline
00086 void
00087 plane::set(float A, float B, float C, float D)
00088 {
00089     this->a = A;
00090     this->b = B;
00091     this->c = C;
00092     this->d = D;
00093 }
00094 
00095 //------------------------------------------------------------------------------
00099 inline
00100 void
00101 plane::set(const vector3& v0, const vector3& v1, const vector3& v2)
00102 {
00103     vector3 cross((v2 - v0) * (v1 - v0));
00104     cross.norm();
00105     this->a = cross.x;
00106     this->b = cross.y;
00107     this->c = cross.z;
00108     this->d = -(a * v0.x + b * v0.y + c * v0.z);
00109 }
00110 
00111 //------------------------------------------------------------------------------
00114 inline
00115 plane::plane(const vector3& v0, const vector3& v1, const vector3& v2)
00116 {
00117     this->set(v0, v1, v2);
00118 }
00119 
00120 //------------------------------------------------------------------------------
00125 inline
00126 float
00127 plane::distance(const vector3& v) const
00128 {
00129     return this->a * v.x + this->b * v.y + this->c * v.z + this->d;
00130 }
00131 
00132 //------------------------------------------------------------------------------
00136 inline
00137 vector3
00138 plane::normal() const
00139 {
00140     return vector3(this->a, this->b, this->c);
00141 }
00142 
00143 //------------------------------------------------------------------------------
00160 inline
00161 bool
00162 plane::intersect(const line3& l, float& t) const
00163 {
00164     float f0 = this->a * l.b.x + this->b * l.b.y + this->c * l.b.z + this->d;
00165     float f1 = this->a * -l.m.x + this->b * -l.m.y + this->c * -l.m.z;
00166     if ((f1 < -0.0001f) || (f1 > 0.0001f))
00167     {
00168         t = f0 / f1;
00169         return true;
00170     }
00171     else
00172     {
00173         return false;
00174     }
00175 }
00176 
00177 //------------------------------------------------------------------------------
00181 inline
00182 bool
00183 plane::intersect(const plane& p, line3& l) const
00184 {
00185     vector3 n0 = this->normal();
00186     vector3 n1 = p.normal();
00187     float n00 = n0 % n0;
00188     float n01 = n0 % n1;
00189     float n11 = n1 % n1;
00190     float det = n00 * n11 - n01 * n01;
00191     const float tol = 1e-06f;
00192     if (fabs(det) < tol)
00193     {
00194         return false;
00195     }
00196     else
00197     {
00198         float inv_det = 1.0f/det;
00199         float c0 = (n11 * this->d - n01 * p.d)    * inv_det;
00200         float c1 = (n00 * p.d - n01 * this->d)* inv_det;
00201         l.m = n0 * n1;
00202         l.b = n0 * c0 + n1 * c1;
00203         return true;
00204     }
00205 }
00206 
00207 //------------------------------------------------------------------------------
00208 #endif

Copyright © 1999-2005 by the contributing authors. Ideas, requests, problems: Send feedback.