D:/Zythum/DinoKod/Common/Plane.h

00001 #ifndef __PLANE_H__
00002 #define __PLANE_H__
00003 
00004 #include "Common/CommonDll.h"
00005 #include "Common/Types.h"
00006 
00007 //---------------------------------------------------------------------------------------------------------------------
00008 typedef enum _KPPOINTSIDE
00009 {
00010         KPPS_FRONTSIDE,
00011         KPPS_BACKSIDE,
00012         KPPS_ONPLANE
00013 } KPPOINTSIDE;
00014 
00015 //---------------------------------------------------------------------------------------------------------------------
00016 class COMMON_API KPlane
00017 {
00018 public:
00019         KVector         m_Normal;
00020         float           m_Distance;
00021 
00022         inline KPlane()
00023         {
00024                 // m_Vector est deja initialisé par KVector::KVector()
00025                 m_Distance = 0.0f;
00026         }
00027         
00028         inline KPlane( KVector& Normal, float Distance )
00029         {
00030                 m_Normal        = Normal;
00031                 m_Normal.Normalize();
00032                 m_Distance      = Distance;
00033         }
00034 
00035         inline KPlane( KVector& Origin, KVector& Normal )
00036         {
00037                 m_Normal        = Normal;
00038                 m_Normal.Normalize();
00039                 m_Distance = -KVector::DotProduct( Origin, m_Normal );
00040         }
00041 
00042         inline KPlane( KVector& Vector0, KVector& Vector1, KVector& Vector2 )
00043         {
00044                 m_Normal = KVector::CrossProduct( Vector1 - Vector0, Vector2 - Vector0 );
00045                 m_Normal.Normalize();
00046                 m_Distance = -KVector::DotProduct( Vector0, m_Normal );
00047         }
00048 
00049         inline float Distance( KVector& Vector )
00050         {
00051                 return KVector::DotProduct( m_Normal, Vector ) - m_Distance;
00052         }
00053 
00054         inline void Normalize()
00055         {
00056                 float   Magnitude = m_Normal.Magnitude();
00057 
00058                 m_Normal.x /= Magnitude;
00059                 m_Normal.y /= Magnitude;
00060                 m_Normal.z /= Magnitude;
00061                 m_Distance /= Magnitude;
00062         }
00063 
00064         inline KPPOINTSIDE GetPointSide( KVector& Point, float Epsilon = 0.001f )
00065         {
00066                 float Det = Distance( Point );
00067 
00068                 if( Det < -Epsilon )
00069                         return KPPS_BACKSIDE;
00070 
00071                 if( Det > Epsilon )
00072                         return KPPS_FRONTSIDE;
00073 
00074                 return KPPS_ONPLANE;
00075         }       
00076 
00077         inline bool IsFrontFacingTo( KVector& Direction )
00078         {
00079                 return ( m_Normal.DotProduct( Direction ) <= 0 );
00080         }
00081 
00082         inline double SignedDistanceTo( KVector& Point )
00083         {
00084                 return Point.DotProduct( m_Normal ) + m_Distance;
00085         }
00086 
00087         inline static bool IsBBoxInside( KPlane* pClipPlane, u32 nClipPlanes, KVector pBBox[6] )
00088         {
00089                 bool    bInside;
00090 /*
00091                 for( u32 b = 0; b < 6; b ++ )
00092                 {
00093                         bInside = true;
00094                         for( u32 p = 0; p < nClipPlanes; p ++ )
00095                         {
00096                                 // teste si le point est du bon coté du plan
00097                                 if( pClipPlane[p].Distance( pBBox[b] ) < 0.0f )
00098                                 {
00099                                         // le point est en dehors
00100                                         bInside = false;
00101                                         break;
00102                                 }
00103                         }
00104                 
00105                         if( bInside )
00106                                 return true;
00107                 }
00108                 */
00109 
00110                 for( u32 p = 0; p < nClipPlanes; p ++ )
00111                 {
00112                         bInside = false;
00113                         
00114                         // teste si le point est du bon coté du plan
00115                         for( u32 b = 0; b < 8; b ++ )
00116                         {
00117                                 if( pClipPlane[p].Distance( pBBox[b] ) > 0.0f )
00118                                 {
00119                                         // le point est dedans
00120                                         bInside = true;
00121                                         break;
00122                                 }
00123                         }
00124                         if( !bInside )
00125                                 return false;
00126                 }
00127 
00128                 return true;
00129         }
00130 };
00131 
00132 #endif //       __PLANE_H__

Generated on Sun Mar 25 20:02:11 2007 for Zythum Project by  doxygen 1.5.1-p1