Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef _GMTL_PLANEOPS_H_
00007 #define _GMTL_PLANEOPS_H_
00008
00009 #include <gmtl/Defines.h>
00010 #include <gmtl/Plane.h>
00011 #include <gmtl/Math.h>
00012
00013 namespace gmtl
00014 {
00015
00029 template< class DATA_TYPE >
00030 DATA_TYPE distance( const Plane<DATA_TYPE>& plane, const Point<DATA_TYPE, 3>& pt )
00031 {
00032 return ( dot(plane.mNorm, static_cast< Vec<DATA_TYPE, 3> >(pt)) - plane.mOffset );
00033 }
00034
00045 template< class DATA_TYPE >
00046 PlaneSide whichSide( const Plane<DATA_TYPE>& plane,
00047 const Point<DATA_TYPE, 3>& pt )
00048 {
00049 DATA_TYPE dist = distance( plane, pt );
00050
00051 if ( dist < DATA_TYPE(0) )
00052 return NEG_SIDE;
00053 else if ( dist > DATA_TYPE(0) )
00054 return POS_SIDE;
00055 else
00056 return ON_PLANE;
00057 }
00058
00070 template< class DATA_TYPE >
00071 PlaneSide whichSide( const Plane<DATA_TYPE>& plane,
00072 const Point<DATA_TYPE, 3>& pt,
00073 const DATA_TYPE& eps )
00074 {
00075 DATA_TYPE dist = distance( plane, pt );
00076
00077 if ( dist < eps )
00078 return NEG_SIDE;
00079 else if ( dist > eps )
00080 return POS_SIDE;
00081 else
00082 return ON_PLANE;
00083 }
00084
00095 template< class DATA_TYPE >
00096 DATA_TYPE findNearestPt( const Plane<DATA_TYPE>& plane,
00097 const Point<DATA_TYPE, 3>& pt,
00098 Point<DATA_TYPE, 3>& result )
00099 {
00100
00101
00102 gmtlASSERT( isNormalized(plane.mNorm) );
00103 DATA_TYPE dist_to_plane(0);
00104 dist_to_plane = plane.mOffset + dot( plane.mNorm, static_cast< Vec<DATA_TYPE, 3> >(pt) );
00105 result = pt - (plane.mNorm * dist_to_plane);
00106 return dist_to_plane;
00107 }
00108
00112 template< class DATA_TYPE, unsigned SIZE>
00113 void reflect( Point<DATA_TYPE, SIZE>& result,
00114 const Plane<DATA_TYPE>& plane,
00115 const Point<DATA_TYPE, SIZE>& point )
00116 {
00117 gmtl::Point<DATA_TYPE, SIZE> point_on_plane;
00118 findNearestPt( plane, point, point_on_plane );
00119 gmtl::Vec<DATA_TYPE, SIZE> dir = point_on_plane - point;
00120 result = point + (dir * DATA_TYPE(2.0f));
00121 }
00138 template< class DATA_TYPE >
00139 inline bool operator==( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2 )
00140 {
00141 return ( (p1.mNorm == p2.mNorm) && (p1.mOffset == p2.mOffset) );
00142 }
00143
00153 template< class DATA_TYPE >
00154 inline bool operator!=( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2 )
00155 {
00156 return (! (p1 == p2));
00157 }
00158
00170 template< class DATA_TYPE >
00171 inline bool isEqual( const Plane<DATA_TYPE>& p1, const Plane<DATA_TYPE>& p2,
00172 const DATA_TYPE& eps )
00173 {
00174 gmtlASSERT( eps >= 0 );
00175 return ( (isEqual(p1.mNorm, p2.mNorm, eps)) &&
00176 (Math::isEqual(p1.mOffset, p2.mOffset, eps)) );
00177 }
00181 }
00182
00183 #endif
00184