Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

math/plane.cpp

Go to the documentation of this file.
00001 #include "plane.h"
00002 
00007 #include "matrix4.h"
00008 #include "bbox.h"
00009 
00010 Plane::Plane()
00011 : _pos(Vector0), _direction(VectorZ), _d(0.0)
00012 {
00013 }
00014 
00015 Plane::Plane(const Vector &pos,const Vector &dir)
00016 : _pos(pos), _direction(dir), _d(0.0)
00017 {
00018     _d = - (_pos*_direction);
00019 }
00020 
00021 const Vector &Plane::direction() const { return _direction; }
00022       Vector &Plane::direction()       { return _direction; }
00023 
00024 double  Plane::d() const { return _d; }
00025 double &Plane::d()       { return _d; }
00026 
00027 bool 
00028 Plane::operator==(const Plane &plane) const
00029 {
00030     return (plane.direction()==_direction && plane.d()==_d );
00031 }
00032    
00033 void
00034 Plane::transform(const Matrix &trans)
00035 {
00036     _pos       = trans * _pos;
00037     _direction = trans * _direction - trans * Vector0;
00038     _d = - (_pos*_direction);
00039 }
00040 
00041 void
00042 Plane::flip()
00043 {
00044     _direction  = _direction * -1;
00045     _d         *= -1;
00046 }
00047     
00048 real 
00049 Plane::dist(const Vector &pos) const
00050 {
00051     return (pos*_direction)+_d; 
00052 }   
00053 
00054 bool 
00055 Plane::inside(const Vector &pos) const
00056 {
00057     return dist(pos)>=0.0;
00058 }
00059 
00060 real 
00061 Plane::dist(const BoundingBox &box) const
00062 {
00063     // Algorithm from Real-Time Rendering book
00064     // Page 311 Plane/Box Intersection Detection
00065 
00066     // vMin and vMax are corners of the axis-aligned
00067     // box in the direction of the plane normal.
00068 
00069     // Returns 0.0 iff plane touches box
00070 
00071     Vector vMin,vMax;
00072 
00073     for (int i=0; i<3; i++)
00074         if (_direction[i]>=0.0)
00075         {
00076             vMin[i] = box.min()[i];
00077             vMax[i] = box.max()[i];
00078         }
00079         else
00080         {   vMin[i] = box.max()[i];
00081             vMax[i] = box.min()[i];
00082         }
00083 
00084     const real vMinDist = dist(vMin);
00085 
00086     if (vMinDist>0.0)
00087         return vMinDist;
00088 
00089     const real vMaxDist = dist(vMax);
00090 
00091     if (vMaxDist>=0.0)
00092         return 0.0;
00093 
00094     return vMaxDist;
00095 }
00096 
00097 real
00098 Plane::intersect(const Vector &p0,const Vector &p1) const
00099 {
00100     const real d0 = dist(p0);
00101     const real d1 = dist(p1);
00102 
00103     return d0/(d0-d1);
00104 }
00105 
00106 Vector
00107 Plane::intersectPosition(const Vector &p0,const Vector &p1) const
00108 {
00109     const real t = intersect(p0,p1);
00110     return p0 + (p1-p0)*t;
00111 }

Generated on Tue Nov 5 11:11:04 2002 for GLT by doxygen1.2.18