TAPs 0.7.7.3
TAPsBVHNode.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsBVHNode.hpp
00003 
00004 SUKITTI PUNAK   (01/17/2006)
00005 UPDATE          (10/08/2010)
00006 ******************************************************************************/
00007 #ifndef TAPs_BVH_NODE_HPP
00008 #define TAPs_BVH_NODE_HPP
00009 
00017 #include "../Core/TAPsLib.hpp"
00018 
00019 #include "../GeometricDataStructure/HalfEdge/TAPsHalfEdgeDataStructure.hpp" // DEBUG
00020 #include "../GeometricDataStructure/TAPsFace.hpp"   // DEBUG
00021 //#include <list>   // DEBUG
00022 
00024 template <typename T> class BVHTree;
00025 
00026 //=============================================================================
00027 // Enable collision detection on/off status
00028 //-----------------------------------------------------------------------------
00034 //#define TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00035 //-----------------------------------------------------------------------------
00036 #ifdef  TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00037 #endif//TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00038 //=============================================================================
00039 
00040 BEGIN_NAMESPACE_TAPs
00041 //=============================================================================
00042 template <typename T>
00043 class BVHNode {
00044 //=============================================================================
00045 // Data Members
00046     friend class BVHTree<T>;
00047 protected:
00048     //--------------------------------------------------------------------
00049     // Data
00050     Enum::CD        m_eType;        
00051     //bool          m_bIsLeaf;      //!< flag for a leaf node
00052     int             m_iID;          
00053     BVHNode<T> *    m_pParent;      
00054     BVHNode<T> **   m_ppChild;      
00055     int             m_iN;           
00056     //--------------------------------------------------------------------
00057     Vector3<T>  m_vCenter;
00058     //--------------------------------------------------------------------
00059     //          OBB         AABB        Sphere
00060     //--------------------------------------------------------------------
00061     Vector3<T>  m_vU;   //
00062     Vector3<T>  m_vV;   
00063     Vector3<T>  m_vW;   
00064     //--------------------------------------------------------------------
00065     // Using "union" Doesn't Work!
00066     //      OBB                 AABB                    Sphere
00067     //union {   Vector3<T> m_vU;    Vector3<T> m_vMin;  };
00068     //union {   Vector3<T> m_vV;    Vector3<T> m_vMax;  };
00069     //union {   Vector3<T> m_vW;                        T m_tRadius;    };
00070     // Sphere uses the x component of m_vW
00071     //--------------------------------------------------------------------
00072     // Working but with unused space
00073     //          Sphere
00074     //T     m_tRadius;
00075     //--------------------------------------------------------------------
00076     //          AABB
00077     //Vector3<T>    m_vMin, m_vMax;
00078     //--------------------------------------------------------------------
00079     //          OBB
00080     //Vector3<T>    m_vU, m_vV, m_vW;
00081     //--------------------------------------------------------------------
00082 #ifdef  TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00083     bool    m_bOnForColDet;     
00084 #endif//TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00085 
00086 public:
00087     //std::list< HEFace<T> * > listHEFace;  // DEBUG
00088 //=============================================================================
00089 // Member Functions
00090 public:
00091 
00092     #ifdef  TAPs_DEBUG_COLLISION_DETECTION
00093     bool    flag;
00094     #endif//TAPs_DEBUG_COLLISION_DETECTION
00095 
00096     //-------------------------------------------------------------------------
00097     // Output Operator <<
00098     friend std::ostream & operator<< ( std::ostream &output, BVHNode<T> const &node )
00099     {
00100         return output << node.StrInfo();
00101 
00102         for ( int i = 0; i < node.m_iN; ++i ) {
00103             output << "\n\tChild Ptr#" << node.m_iN << ": " << node.m_ppChild[i];
00104         }
00105         //-----------------------------------------------------------
00106         // Output the node info
00107         output << "\nThe node type is ";
00108         switch ( node.m_eType ) {
00109             case Enum::UNDEFINED_NODE:
00110                 output << "UNDEFINED_NODE.";
00111                 break;
00112             case Enum::BVH_NODE_BINARY_SPHERE:
00113                 output  << "BVH_NODE_BINARY_SPHERE." 
00114                         << "  With Center at (" << node.m_vCenter[0] << ", " 
00115                         << node.m_vCenter[1] << ", " << node.m_vCenter[2] 
00116                         <<   ") and radius of " << node.m_vW[0];
00117                 break;
00118             default:
00119                 output << "\nUnrecognized Node!";
00120                 break;
00121         }
00122         output  << "\n";
00123         return output;
00124     }
00125     //-------------------------------------------------------------------------
00127     BVHNode ( Enum::CD type, 
00128               int id                 = -1, 
00129               BVHNode<T> * parent    = NULL, 
00130               BVHNode<T> ** children = NULL );
00132     BVHNode ( int numOfChildren, 
00133               Enum::CD type, 
00134               int id                 = -1, 
00135               BVHNode<T> * parent    = NULL, 
00136               BVHNode<T> ** children = NULL );
00137 protected:
00139     BVHNode ( Enum::CD type, 
00140               int id, 
00141               BVHNode<T> * parent, 
00142               Vector3<T> const & center, 
00143               Vector3<T> const & U, 
00144               Vector3<T> const & V, 
00145               Vector3<T> const & W );
00146     //-------------------------------------------------------------------------
00149     BVHNode ( int id, 
00150               Enum::CD type, 
00151               BVHNode<T> * parent = NULL );
00152     //-------------------------------------------------------------------------
00154 public:
00155     virtual ~BVHNode ();
00156     //-------------------------------------------------------------------------
00158     virtual std::string StrInfo () const;
00159 protected:
00160     //-------------------------------------------------------------------------
00162     void InitChildren ( Enum::CD type,     BVHNode<T> ** children = NULL );
00164     void InitChildren ( int numOfChildren, BVHNode<T> ** children = NULL );
00165 public:
00166     //-------------------------------------------------------------------------
00167     // Get/Set Fn(s)
00168     //---------------------------------------------------------------
00169 
00170 #ifdef  TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00171     bool GetOnStatus () const   { return m_bOnForColDet; }
00172     void SetOnStatus ( bool b ) { m_bOnForColDet = b; }
00173     void ToggleOnStatus ()      { m_bOnForColDet = !m_bOnForColDet; }
00174 #endif//TAPs_ENABLE_COLLISION_DETECTION_ON_OFF_STATUS
00175 
00176 public:
00177     inline int  GetType () const { return m_eType; }
00178     virtual inline bool IsLeaf () const     { return 0 == m_iN; }
00179     virtual inline bool IsNotLeaf () const  { return 0 != m_iN; }
00180 protected:
00181     inline void SetType ( Enum::CD type ) { m_eType = type; }
00182 public:
00183     inline int  GetID () const { return m_iID; }
00184 protected:
00185     inline void SetID ( int id ) { m_iID = id; }
00186     //---------------------------------------------------------------
00187     // Get/Set for Parent Pointer
00188 public:
00189     inline BVHNode<T> const * const Parent () const { return m_pParent; }
00190 protected:
00191     inline BVHNode<T> * Parent () { return m_pParent; }
00192     inline void Parent ( BVHNode<T> * parent )  { m_pParent = parent; }
00193     //---------------------------------------------------------------
00194     // Get/Set for Child Pointers
00195 public:
00196     inline int GetNumOfChildren () const    { return m_iN; }
00197     inline virtual BVHNode<T> const * const Child ( int i ) const
00198         { assert ( 0 <= i && i < m_iN );    return m_ppChild[i]; }
00199         /*{
00200             if ( 0 <= i && i < m_iN )   return m_ppChild[i];
00201             else                        return NULL;
00202         }//*/
00203 //protected:
00204     inline virtual BVHNode<T> * Child ( int i )
00205         { assert ( 0 <= i && i < m_iN );    return m_ppChild[i]; }
00206         /*{
00207             if ( 0 <= i && i < m_iN )   return m_ppChild[i];
00208             else                        return NULL;
00209         }//*/
00210     inline void Child  ( int i, BVHNode<T> * pChild )
00211         { assert ( 0 <= i && i < m_iN );    m_ppChild[i] = pChild; }
00212     //---------------------------------------------------------------
00213     // Get/Set Sphere Bounding Volume
00214 public:
00215     inline Vector3<T> const & GetCenter () const { return m_vCenter; }
00216 //protected:
00217     inline Vector3<T> & GetCenter () { return m_vCenter; }
00218     inline void SetCenter ( Vector3<T> const & center ) { m_vCenter = center; }
00219     inline void SetCenter ( T x, T y, T z ) { m_vCenter.SetXYZ( x, y, z ); }
00220 public:
00221     // m_vW[0] is m_tRadius
00222     inline T GetRadius () const {return m_vW[0]; }
00223 //protected:
00224     // m_vW[0] is m_tRadius
00225     inline void SetRadius ( T radius ) { m_vW[0] = radius; }
00226     //---------------------------------------------------------------
00227     // Get/Set OBB Bounding Volume
00228 public:
00229     inline Vector3<T> const & Get1stPrincipleComponent () const
00230         { return m_vU; }
00231     inline Vector3<T> const & Get2ndPrincipleComponent () const
00232         { return m_vV; }
00233     inline Vector3<T> const & Get3rdPrincipleComponent () const
00234         { return m_vW; }
00235     inline void GetPrincipleComponents ( 
00236         Vector3<T> & U, Vector3<T> & V, Vector3<T> & W ) const
00237         { U = m_vU; V = m_vV; W = m_vW; }
00238 protected:
00239     inline void Set1stPrincipleComponent ( Vector3<T> const & v )
00240         { m_vU = v; }
00241     inline void Set2ndPrincipleComponent ( Vector3<T> const & v )
00242         { m_vV = v; }
00243     inline void Set3rdPrincipleComponent ( Vector3<T> const & v )
00244         { m_vW = v; }
00245     inline void SetPrincipleComponents ( 
00246         Vector3<T> const & U, Vector3<T> const & V, Vector3<T> const & W )
00247         { m_vU = U; m_vV = V; m_vW = W; }
00248     //---------------------------------------------------------------
00249     // Get/Set ABB Bounding Volume
00250 public:
00251     inline Vector3<T> const & GetMinPt () const     { return m_vV; }
00252     inline Vector3<T> const & GetMaxPt () const     { return m_vW; }
00253 protected:
00254     inline void SetMinPt ( Vector3<T> const & v )   { m_vV = v; }
00255     inline void SetMaxPt ( Vector3<T> const & v )   { m_vW = v; }
00256     //---------------------------------------------------------------
00257     // Get Primitive(s)
00258 public:
00259     inline virtual HEFace<T> *  GetAPrimitiveHalfEdgeFace () const
00260         { return NULL; }
00261     inline virtual Face<T> *    GetAPrimitiveFace () const
00262         { return NULL; }
00263     //-------------------------------------------------------------------------
00264     // Node Operations
00265 public:
00266     //-------------------------------------------------------------------------
00268     virtual void Update ();
00269     //-------------------------------------------------------------------------
00272 //  virtual T TestOverlapWith ( BVHNode<T> const * const that ) const;
00273     //-------------------------------------------------------------------------
00276 //  virtual T TestOverlapWithNoPrimitiveTests ( BVHNode<T> const * const that ) const;
00277     //-------------------------------------------------------------------------
00278 public:
00279     //=========================================================================
00280     // Sphere-Sphere Intersection Test (BinarySphereTree vs BinarySphereTree)
00281     //-------------------------------------------------------------------------
00297     inline virtual T TestOverlapSphereWithSphere ( 
00298                         BVHNode<T> const * const that       
00299                     ) const;
00300     inline virtual T TestOverlapSphereWithSphere ( 
00301                         BVHNode<T> const * const that,      
00302                         Matrix4x4<T> const & thatTransform  
00303                     ) const;
00304     inline virtual T TestOverlapSphereWithSphere ( 
00305                         Matrix4x4<T> const & thisTransform, 
00306                         BVHNode<T> const * const that       
00307                     ) const;
00308     inline virtual T TestOverlapSphereWithSphere ( 
00309                         Matrix4x4<T> const & thisTransform, 
00310                         BVHNode<T> const * const that,      
00311                         Matrix4x4<T> const & thatTransform  
00312                     ) const;
00313     //-------------------------------------------------------------------------
00329     inline T TestOverlapSphereWithSphere_NoPrimitiveTests ( 
00330                         BVHNode<T> const * const that       
00331                     ) const;
00332     inline T TestOverlapSphereWithSphere_NoPrimitiveTests ( 
00333                         BVHNode<T> const * const that,      
00334                         Matrix4x4<T> const & thatTransform  
00335                     ) const;
00336     inline T TestOverlapSphereWithSphere_NoPrimitiveTests ( 
00337                         Matrix4x4<T> const & thisTransform, 
00338                         BVHNode<T> const * const that       
00339                     ) const;
00340     inline T TestOverlapSphereWithSphere_NoPrimitiveTests ( 
00341                         Matrix4x4<T> const & thisTransform, 
00342                         BVHNode<T> const * const that,      
00343                         Matrix4x4<T> const & thatTransform  
00344                     ) const;
00345     //-------------------------------------------------------------------------
00346 
00347     //=========================================================================
00348     // Primitive-Primitive Intersection Test
00349     //-------------------------------------------------------------------------
00361     //inline virtual T TestOverlapPrimitiveWithPrimitive ( BVHNode<T> const * const that ) const;
00362     //inline virtual T TestOverlapPrimitiveWithPrimitive ( BVHNode<T> const * const that, 
00363     //                                      Matrix4x4<T> const & thatTransform ) const;
00364     //inline virtual T TestOverlapPrimitiveWithPrimitive ( Matrix4x4<T> const & thisTransform, 
00365     //                                      BVHNode<T> const * const that ) const;
00366     //inline virtual T TestOverlapPrimitiveWithPrimitive ( Matrix4x4<T> const & thisTransform, 
00367     //                                      BVHNode<T> const * const that, 
00368     //                                      Matrix4x4<T> const & thatTransform ) const;
00369     //-------------------------------------------------------------------------
00370 
00371     //=========================================================================
00372     // Sphere-BVSphere Intersection Test (BinarySphereTree vs Sphere BV)
00373     //-------------------------------------------------------------------------
00389     inline T TestOverlapSphereWithBVSphere_NoPrimitiveTests ( 
00390                         Vector3<T> const & centerOfBVSphere,    
00391                         T radiusOfBVSphere                      
00392                     ) const;
00393     inline T TestOverlapSphereWithBVSphere_NoPrimitiveTests ( 
00394                         Matrix4x4<T> const & transformA,        
00395                         Vector3<T> const & centerOfBVSphere,    
00396                         T radiusOfBVSphere                      
00397                     ) const;
00398     //-------------------------------------------------------------------------
00414     inline virtual T TestOverlapSphereWithBVSphere ( 
00415                         Vector3<T> const & centerOfBVSphere,    
00416                         T radiusOfBVSphere                      
00417                     ) const
00418     {
00419         // Same as testing TestOverlapSphereWithBVSphere_NoPrimitiveTests(), since it is not a leaf node.
00420         // Must be overrided by its subclass for primitive-primitive test.
00421         return TestOverlapSphereWithBVSphere_NoPrimitiveTests( centerOfBVSphere, radiusOfBVSphere );
00422     }
00423     inline virtual T TestOverlapSphereWithBVSphere ( 
00424                         Matrix4x4<T> const & transformA,        
00425                         Vector3<T> const & centerOfBVSphere,    
00426                         T radiusOfBVSphere                      
00427                     ) const
00428     {
00429         // Same as testing TestOverlapSphereWithBVSphere_NoPrimitiveTests(), since it is not a leaf node.
00430         // Must be overrided by its subclass for primitive-primitive test.
00431         return TestOverlapSphereWithBVSphere_NoPrimitiveTests( transformA, centerOfBVSphere, radiusOfBVSphere );
00432     }
00433     //-------------------------------------------------------------------------
00434 
00435     //=========================================================================
00436     // Sphere-BVCylinder Intersection Test (BinarySphereTree vs Cylinder BV)
00437     //-------------------------------------------------------------------------
00451     inline bool TestOverlapSphereWithBVCylinder_AtOrigin_NoPrimitiveTests ( 
00452                         T radiusOfBVCylinder,                   
00453                         T heightOfBVCylinder                    
00454                     ) const;
00455     inline bool TestOverlapSphereWithBVCylinder_AtOrigin_NoPrimitiveTests ( 
00456                         Matrix4x4<T> const & transformA,        
00457                         T radiusOfBVCylinder,                   
00458                         T heightOfBVCylinder                    
00459                     ) const;
00460     //-------------------------------------------------------------------------
00474     inline virtual bool TestOverlapSphereWithBVCylinder_AtOrigin ( 
00475                         T radiusOfBVCylinder,                   
00476                         T heightOfBVCylinder                    
00477                     ) const
00478     {
00479         // Same as testing TestOverlapSphereWithBVSphere_NoPrimitiveTests(), since it is not a leaf node.
00480         // Must be overrided by its subclass for primitive-primitive test.
00481         return TestOverlapSphereWithBVCylinder_AtOrigin_NoPrimitiveTests( radiusOfBVCylinder, heightOfBVCylinder );
00482     }
00483     inline virtual bool TestOverlapSphereWithBVCylinder_AtOrigin ( 
00484                         Matrix4x4<T> const & transformA,        
00485                         T radiusOfBVCylinder,                   
00486                         T heightOfBVCylinder                    
00487                     ) const
00488     {
00489         // Same as testing TestOverlapSphereWithBVSphere_NoPrimitiveTests(), since it is not a leaf node.
00490         // Must be overrided by its subclass for primitive-primitive test.
00491         return TestOverlapSphereWithBVCylinder_AtOrigin_NoPrimitiveTests( transformA, radiusOfBVCylinder, heightOfBVCylinder );
00492     }
00493     //-------------------------------------------------------------------------
00494 
00495     //-------------------------------------------------------------------------
00496 #if defined(__gl_h_) || defined(__GL_H__)
00497 public:
00498     // DrawByOpenGL
00499     virtual void Draw () const  { DrawByOpenGL(); }
00500     virtual void DrawByOpenGL () const;
00501     virtual void DrawByOpenGL ( int currentLevel, int startLevel, int endLevel ) const;
00502     virtual void DrawByOpenGLOnlyEndLevel () const;
00503     //---------------------------------------------------------------
00504 //protected:
00505 public: // DEBUG
00506     virtual void DrawBV ( GLenum drawStyle = GLU_LINE ) const;
00507     virtual void DrawPrim ( GLenum drawStyle = GLU_LINE ) const {}; // DEBUG
00508 protected:
00509     virtual void DrawSphereBV ( GLenum drawStyle = GLU_LINE ) const;
00510     virtual void DrawAABBBV ( GLenum drawStyle = GLU_LINE ) const;
00511     virtual void DrawOBBBV ( GLenum drawStyle = GLU_LINE ) const;
00512     //---------------------------------------------------------------
00513     static GLuint   m_uiDisplayList;        // for OpenGL (sphere) drawing
00514 #endif
00515     //-------------------------------------------------------------------------
00516 }; // END CLASS BVHNode
00517 //=============================================================================
00518 END_NAMESPACE_TAPs
00519 //-----------------------------------------------------------------------------
00520 // Include definition if TAPs_USE_EXPORT is not defined
00521 //#if !defined( TAPs_USE_EXPORT )
00522     #include "TAPsBVHNode.cpp"
00523 //#endif
00524 //-----------------------------------------------------------------------------
00525 #endif
00526 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00527 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines