Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Plane.h

Go to the documentation of this file.
00001 
00037 #ifndef _AMIRE_COMMON_PLANE_H
00038 #define _AMIRE_COMMON_PLANE_H
00039 
00040 #include <amire/common/Defines.h>
00041 #include <amire/common/Vector.h>
00042 
00043 namespace amire {
00044   namespace common {
00048     template <class type> class TPlane : public virtual osg::Referenced {
00049     private:
00050       TVector3D<type> position;
00051       TVector3D<type> normal;
00052       type distanceToZero;
00053       type normalLengthDiv;
00054 
00055     public:
00059       inline TPlane(const TVector3D<type> &p1, const TVector3D<type> &p2, const TVector3D<type> &p3) : position(p1), normal(p2) {
00060         Vector v(p3);
00061         v.sub(p1);
00062         normal.sub(p1);
00063         normal.cross(v);
00064         distanceToZero = position.dot(normal);
00065         normalLengthDiv = Real(1.0) / normal.length();
00066       }
00067 
00071       inline TPlane(const TVector3D<type> &pnormal, type distanceToZero) : position(pnormal), normal(pnormal) {
00072         this->distanceToZero = distanceToZero;
00073         position.scale(distanceToZero);
00074         normalLengthDiv = Real(1.0) / normal.length();
00075       }
00076 
00080       inline TPlane(type x, type y, type z, type nx , type ny, type nz) : Referenced(), position(x, y, z), normal(nx, ny, nz) {
00081         distanceToZero = position.dot(normal);
00082         normalLengthDiv = Real(1.0) / normal.length();
00083       }
00084 
00088       inline TPlane(const TVector3D<type> &pposition, const TVector3D<type> &pnormal) : Referenced(), position(pposition), normal(pnormal) {
00089         distanceToZero = position.dot(normal);
00090         normalLengthDiv = Real(1.0) / normal.length();
00091       }
00092 
00096       inline Real distance(const TVector3D<type> &point) {
00097         return normalLengthDiv * (point.dot(normal) - distanceToZero);
00098       }
00099 
00103       inline bool intersectsLine(const TVector3D<type> &position, const TVector3D<type> &direction, type &distance) {
00104         type div = direction.dot(normal);
00105         if (div == 0.0) {
00106           return false;
00107         }
00108         distance = (distanceToZero - position.dot( normal) ) / div;
00109         return true;
00110       }
00111 
00115       inline bool intersectsLine(const TVector3D<type> &position, const TVector3D<type> &direction, TVector3D<type> &intersectionPoint) {
00116         type distance;
00117         if (intersectsLine(position, direction, distance)) {
00118           intersectionPoint = direction;
00119           intersectionPoint.scale(distance);
00120           intersectionPoint.add(position);
00121           return true;
00122         }
00123         return false;
00124       }
00125     };
00126 
00130     typedef TPlane<Real> Plane;
00131   }
00132 }
00133 
00134 #endif

Generated on Tue May 20 17:23:53 2003 for AMIRE-Framework by doxygen1.3