TAPs 0.7.7.3
TAPsBVHTree.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsBVHTree.hpp
00003 ******************************************************************************/
00004 // See comment below
00005 /******************************************************************************
00006 SUKITTI PUNAK   (01/17/2006)
00007 UPDATE          (10/12/2010)
00008 ******************************************************************************/
00009 #ifndef TAPs_BVH_TREE_HPP
00010 #define TAPs_BVH_TREE_HPP
00011 
00034 #include "../Core/TAPsLib.hpp"
00035 #include <set>
00036 #include "TApsBVHNodeLeaf.hpp"
00037 #include "TAPsBVHNodeLeafHalfEdgeAPrim.hpp"
00038 #include "../Support/TAPsInteractionPointGroup.hpp"
00039 #include "TAPsBVHNodeLeafALinkFormedByTwoPointMasses.hpp"
00040 #include "../Support/TAPsTransformationSupport.hpp"
00041 
00042 #include "TAPsMultiBoundingVolume.hpp"
00043 
00044 BEGIN_NAMESPACE_TAPs
00045 //=============================================================================
00046 
00047 
00051 template <typename T>
00052 class BVsAndNodesList {
00053 public:
00054     BVsAndNodesList ( BoundingVolume<T> * pBV, std::vector< BVHNode<T> * > nodeList )
00055         : BV( pBV ), NodeList( nodeList )
00056     {}
00057     BoundingVolume<T> *         BV;         
00058     std::vector< BVHNode<T> * > NodeList;   
00059 };
00060 
00061 
00065 template <typename T>
00066 class BVHTree {
00067 //=============================================================================
00068 // Data Members
00069 protected:
00070     Enum::CD        m_eType;        
00071     int             m_iTotalNodes;  
00072     //-------------------------------------------------------------------------
00073     // Root Node
00074     BVHNode<T> *    m_pRoot;
00075     // Collided Nodes
00076     std::vector< BVHNode<T> * > m_svCollidedNode;       
00077     std::vector< BVHNode<T> * > m_svCollidedNodeThat;   
00078     std::vector< BVsAndNodesList<T> > m_svCollidedBVAndNodeList;    
00079 
00080     std::set< BVHNode<T> * >    m_svLeafNodes;      
00081 
00082     //-------------------------------------------------------------------------
00083     // Transformation Support
00084     TransformationSupport<T> &  m_Transform;    
00085     //Matrix4x4<T>  m_FwdTransformMatrix[2];    //!< forward transformations for collision detection
00086     //Matrix4x4<T>  m_InvTransformMatrix[2];    //!< inverse transformations for collision detection
00087 
00114     //-------------------------------------------------------------------------
00115 //=============================================================================
00116 // Member Functions
00117 public:
00118 
00119     #ifdef  TAPs_DEBUG_COLLISION_DETECTION
00120     void ClearFlags ();
00121     void ClearFlags ( BVHNode<T> * const node );
00122     #endif//TAPs_DEBUG_COLLISION_DETECTION
00123 
00124     //-------------------------------------------------------------------------
00126     friend std::ostream & operator<< ( std::ostream &output, BVHTree<T> const &obj )
00127     {
00128         output  << "BVHTree<" << typeid(T).name() << ">"
00129                 << " with " << obj.GetNumOfNodes() << " nodes"
00130                 << " (" << obj.CountLeafNodes() << " are Leaf nodes)"
00131                 << " and the root ptr is " << obj.m_pRoot 
00132                 << "\n";
00133         return output;
00134     }
00135 
00137     std::string DisplayAllNodesAsString () const
00138     {
00139         std::string str;
00140         DisplayAllNodesAsString( m_pRoot, str );
00141         return str;
00142     }
00143 
00144     //-------------------------------------------------------------------------
00146     BVHTree (   TransformationSupport<T> & transform, 
00147                 Enum::CD type = Enum::UNDEFINED_TREE, 
00148                 int numOfNodes = 0, 
00149                 BVHNode<T> * rootNode = NULL );
00150 
00151     // Constructor
00152     //BVHTree ( TransformationSupport<T> & transform,   //!< I/O: Transform
00153     //          HEFaceList<T> * heFaceList )            //!< I/P: half-edge face list
00154     //{}
00155 
00157     virtual ~BVHTree ();
00158     //-------------------------------------------------------------------------
00159     // Get/Set Fn(s)
00160     inline Enum::CD GetType () const    { return m_eType; }
00161     inline int  GetNumOfNodes () const  { return m_iTotalNodes; }
00162     inline void SetNumOfNodes ( int n ) { m_iTotalNodes = n; }
00163     inline BVHNode<T> *             Root ()         { return m_pRoot; }
00164     inline BVHNode<T> const * const Root () const   { return m_pRoot; }
00165     inline void Root ( BVHNode<T> * root )          { m_pRoot = root; }
00166     //int CountNumOfNodes () const = 0;
00167     //-------------------------------------------------------------------------
00168     // Get Transformation Support Fn(s)
00169     TransformationSupport<T> const &    GetTransform() const
00170         { return m_Transform; }
00171     TransformationSupport<T> &          GetTransform()
00172         { return m_Transform; }
00173     //-------------------------------------------------------------------------
00174     // Tree Operations
00175     //virtual CheckSelfIntersection ...
00176     inline int CountNodes () const;
00177     inline int CountLeafNodes () const;
00178 protected:
00179     int CountNodes ( BVHNode<T> const * const node ) const;
00180     int CountLeafNodes ( BVHNode<T> const * const node ) const;
00181 
00183     void FindListOfLeafNodes ( BVHNode<T> * node, std::set< BVHNode<T> * > & leafNodeSet );
00184 
00186     std::set< BVHNode<T> * > & GetListOfLeafNodes ()
00187     { return m_svLeafNodes; }
00188 
00190     void FindListOfLeafNodes ()
00191     { FindListOfLeafNodes( m_pRoot, m_svLeafNodes ); }
00192 
00193     // Display all nodes as string (recursively)
00194     void DisplayAllNodesAsString ( BVHNode<T> * node, std::string & str, int level = 0 ) const;
00195 
00196 public:
00198     virtual void Update ();
00199 
00201     virtual void Update ( std::set< BVHNode<T> * > & nodeSet );
00202 
00203     //=========================================================================
00204     // PURE VIRTUAL FNs -------------------------------------------------------
00205     //-------------------------------------------------------------------------
00211     virtual bool TestOverlapWith ( BVHTree<T> const * const that ) = 0;
00212 
00218     virtual bool TestOverlapWithTillLeafNodes ( BVHTree<T> const * const that ) = 0;
00219 
00224     virtual bool TestOverlapWith ( BVHNode<T> const * const node ) = 0;
00225 
00230     virtual bool TestOverlapWithTillLeafNodes ( BVHNode<T> const * const node ) = 0;
00231 
00237     virtual bool TestOverlapWith ( MultiBoundingVolume<T> const * const pMBV ) = 0;
00238 
00244     virtual bool TestOverlapWithTillLeafNodes ( MultiBoundingVolume<T> const * const pMBV ) = 0;
00245 
00250     virtual bool TestOverlapWith ( BoundingVolume<T> const * const pBV ) = 0;
00251 
00256     virtual bool TestOverlapWithTillLeafNodes ( BoundingVolume<T> const * const pBV ) = 0;
00257 
00262     virtual bool TestIntersectionWithLineSegmentTillLeafNodes ( Vector3<T> const * const ptA, Vector3<T> const * const ptB ) = 0;
00263 
00269     virtual bool TestIntersectionWithCircleTillLeafNodes ( Vector3<T> const * const circleNormalPlane, Vector3<T> const * const circleCenter, T circleRadius ) = 0;
00270 
00278     virtual bool TestOverlapWith (
00279         InteractionPointGroup<T> * const    pIPG,           
00280         std::vector< PointForce<T> > & ListOfPointForces    
00281     ) = 0;
00282 
00290     virtual bool TestOverlapWithTillLeafNodes (
00291         InteractionPointGroup<T> * const    pIPG,           
00292         std::vector< PointForce<T> > & ListOfPointForces    
00293     ) = 0;
00294 
00295     //-------------------------------------------------------------------------
00296     // PURE VIRTUAL FNs -------------------------------------------------------
00297     //=========================================================================
00298 
00299     //-------------------------------------------------------------------------
00300     // Collided Nodes
00301     virtual std::vector< BVHNode<T> * > & GetListOfCollidedNodes ()     { return m_svCollidedNode; }
00302     virtual std::vector< BVHNode<T> * > & GetListOfCollidedNodesThat () { return m_svCollidedNodeThat; }
00303     virtual std::vector< BVsAndNodesList<T> > & GetListOfCollidedBoundingVolumesAndNodes () { return m_svCollidedBVAndNodeList; }
00304     //-------------------------------------------------------------------------
00305 
00306 protected:
00307 
00311     //void CalAndSaveTransformations ( BoundingVolume<T> const * const pBV );
00312     //void CalAndSaveTransformations ( BoundingVolume<T> const * const pBV, Matrix4x4<T> const & transformB );
00313     //void CalAndSaveTransformations ( Matrix4x4<T> const & transformA, BoundingVolume<T> const * const pBV );
00314     //void CalAndSaveTransformations ( Matrix4x4<T> const & transformA, BoundingVolume<T> const * const pBV, Matrix4x4<T> const & transformB );
00315 
00316     /*
00317     //=========================================================================
00318     // Test Overlap with a MultiBoundingVolume
00319     //-------------------------------------------------------------------------
00326     virtual bool TestOverlap_vs_MBV ( 
00327                     BVHNode<T> const * const nodeA, 
00328                     Matrix4x4<T> const & transformA, 
00329                     MultiBoundingVolume<T> const * const pMBV, 
00330                     Matrix4x4<T> const & transformB )           = 0;
00331     //-------------------------------------------------------------------------
00335     virtual bool TestOverlap_vs_MBV_TillLeafNodes ( 
00336                     BVHNode<T> const * const nodeA, 
00337                     Matrix4x4<T> const & transformA, 
00338                     MultiBoundingVolume<T> const * const pMBV, 
00339                     Matrix4x4<T> const & transformB )           = 0;
00340     //-------------------------------------------------------------------------
00341     //=========================================================================
00342     //*/
00343 
00344     //*
00345     //=========================================================================
00346     // Test Overlap with a BoundingVolume
00347     //-------------------------------------------------------------------------
00354     virtual bool TestOverlap_vs_BV ( 
00355                     BVHNode<T> const * const nodeA, 
00356                     Matrix4x4<T> const & transformA, 
00357                     BoundingVolume<T> const * const pBV, 
00358                     Matrix4x4<T> const & transformB )       = 0;
00359     //-------------------------------------------------------------------------
00363     virtual bool TestOverlap_vs_BV_TillLeafNodes ( 
00364                     BVHNode<T> const * const nodeA, 
00365                     Matrix4x4<T> const & transformA, 
00366                     BoundingVolume<T> const * const pBV, 
00367                     Matrix4x4<T> const & transformB )       = 0;
00368     //-------------------------------------------------------------------------
00369     //=========================================================================
00370     //*/
00371 
00372     //=========================================================================
00373     // Test Overlap with Another BVHTree_BinarySphere
00374     //-------------------------------------------------------------------------
00381     virtual bool TestOverlap_vs_BinarySphere ( 
00382                     BVHNode<T> const * const nodeA, 
00383                     Matrix4x4<T> const & transformA, 
00384                     BVHNode<T> const * const nodeB, 
00385                     Matrix4x4<T> const & transformB )   = 0;
00386     //-------------------------------------------------------------------------
00390     virtual bool TestOverlap_vs_BinarySphere_TillLeafNodes ( 
00391                     BVHNode<T> const * const nodeA, 
00392                     Matrix4x4<T> const & transformA, 
00393                     BVHNode<T> const * const nodeB, 
00394                     Matrix4x4<T> const & transformB )   = 0;
00395     //-------------------------------------------------------------------------
00396     //=========================================================================
00397 
00398     /*
00399     //=========================================================================
00400     // Test Overlap with Another BVHTree_BinaryAABB
00401     //-------------------------------------------------------------------------
00408     virtual bool TestOverlap_vs_BinaryAABB ( 
00409                     BVHNode<T> const * const nodeA, 
00410                     Matrix4x4<T> const & transformA, 
00411                     BVHNode<T> const * const nodeB, 
00412                     Matrix4x4<T> const & transformB )   = 0;
00413     //-------------------------------------------------------------------------
00417     virtual bool TestOverlap_vs_BinaryAABB_TillLeafNodes ( 
00418                     BVHNode<T> const * const nodeA, 
00419                     Matrix4x4<T> const & transformA, 
00420                     BVHNode<T> const * const nodeB, 
00421                     Matrix4x4<T> const & transformB )   = 0;
00422     //-------------------------------------------------------------------------
00423     //=========================================================================
00424     //*/
00425 
00426     /*
00427     //=========================================================================
00428     // Test Overlap with Another BVHTree_BinaryOBB
00429     //-------------------------------------------------------------------------
00436     virtual bool TestOverlap_vs_BinaryOBB ( 
00437                     BVHNode<T> const * const nodeA, 
00438                     Matrix4x4<T> const & transformA, 
00439                     BVHNode<T> const * const nodeB, 
00440                     Matrix4x4<T> const & transformB )   = 0;
00441     //-------------------------------------------------------------------------
00445     virtual bool TestOverlap_vs_BinaryOBB_TillLeafNodes ( 
00446                     BVHNode<T> const * const nodeA, 
00447                     Matrix4x4<T> const & transformA, 
00448                     BVHNode<T> const * const nodeB, 
00449                     Matrix4x4<T> const & transformB )   = 0;
00450     //-------------------------------------------------------------------------
00451     //=========================================================================
00452     //*/
00453 
00454 protected:
00455     //=========================================================================
00456     // BINARY ORIENTED BOUNDING BOX (0BB) HIERARCHY TREE
00457     //-------------------------------------------------------------------------
00458     // Create Binary OBB Tree
00459     static BVHTree<T> * BOBBBuild (
00460       TransformationSupport<T> & transform, // I/O: Transform
00461       const std::list< HEFace<T> * > & heFaceList,  // I/P: half-edge face list
00462       BVHTree<T> * cdtree );                        // O/P: pointer to BVHTree
00463     //-------------------------------------------------------------------------
00464     // Create Binary Child OBBs
00465     static void BOBBBuildChildren ( BVHNode<T> * pParentNode, 
00466                     const std::list< HEFace<T> * > & heFaceList, 
00467                     Vector3<T> const & principleVector );
00468     //-------------------------------------------------------------------------
00469     // Find the group that this face belongs to
00470     static int BOBBDetermineFaceGroup ( 
00471                     HEFace<T> * face, 
00472                     const Vector3<T> & center, 
00473                     const Vector3<T> & direction );
00474     static void BOBBDivideToFaceGroups ( 
00475                     const std::list< HEFace<T> * > & faces, // input
00476                     std::list< HEFace<T> * > & group1,      // output group1
00477                     std::list< HEFace<T> * > & group2,      // output group2
00478                     const Vector3<T> & center, 
00479                     const Vector3<T> & direction );
00480     //-------------------------------------------------------------------------
00481     // Create a new OBB bounding volume node from the face list
00482     static BVHNode<T> * CreateBVHNodeBOBB ( 
00483                     BVHNode<T> * pParentNode, 
00484                     const std::list< HEFace<T> * > & heFaceList );
00485     //-------------------------------------------------------------------------
00486     //=========================================================================
00487 
00488     //=========================================================================
00489     // BINARY AXIS-ALIGNED BOUNDING BOX (AABB) HIERARCHY TREE
00490     //-------------------------------------------------------------------------
00491     // Create Binary AABB Tree
00492     static BVHTree<T> * BAABBBuild (
00493       TransformationSupport<T> & transform, // I/O: Transform
00494       const std::list< HEFace<T> * > & heFaceList,  // I/P: half-edge face list
00495       BVHTree<T> * cdtree );                        // O/P: pointer to BVHTree
00496     //-------------------------------------------------------------------------
00497     // Create Binary Child AABBs
00498     static void BAABBBuildChildren (    BVHNode<T> * pParentNode, 
00499                     const std::list< HEFace<T> * > & heFaceList, 
00500                     Vector3<T> const & principleVector );
00501     //-------------------------------------------------------------------------
00502     // Find the group that this face belongs to
00503     static int BAABBDetermineFaceGroup ( 
00504                     HEFace<T> * face, 
00505                     const Vector3<T> & center, 
00506                     const Vector3<T> & direction );
00507     static void BAABBDivideToFaceGroups ( 
00508                     const std::list< HEFace<T> * > & faces, // input
00509                     std::list< HEFace<T> * > & group1,      // output group1
00510                     std::list< HEFace<T> * > & group2,      // output group2
00511                     const Vector3<T> & center, 
00512                     const Vector3<T> & direction );
00513     //-------------------------------------------------------------------------
00514     // Create a new AABB bounding volume node from the face list
00515     static BVHNode<T> * CreateBVHNodeBAABB ( 
00516                     BVHNode<T> * pParentNode, 
00517                     const std::list< HEFace<T> * > & heFaceList );
00518     //-------------------------------------------------------------------------
00519     //=========================================================================
00520 
00521     //=========================================================================
00522     // This fn is used for dividing the face list into two groups according to
00523     // the longest principle component
00524     // Used by BINARY OBB    BOUNDING VOLUME HIERARCHY TREE
00525     //         BINARY SPHERE BOUNDING VOLUME HIERARCHY TREE
00526     //         BINARY AABB   BOUNDING VOLUME HIERARCHY TREE
00527     //-------------------------------------------------------------------------
00528     // Find OOB (Oriented Bounding Box) (see OBB of Gottschalk, et al.)
00529     static void FindOBBOfFaceList ( 
00530             const std::list< HEFace<T> * > & heFaceList,    // I/P: face list
00531             Vector3<T> & center,    // I/P: center
00532             Vector3<T> & U,         // O/P: 1st principle component
00533             Vector3<T> & V,         // O/P: 2nd principle component
00534             Vector3<T> & W );       // O/P: 3rd principle component
00535     //-------------------------------------------------------------------------
00536     //=========================================================================
00537 
00538     //=========================================================================
00539     // QUAD SPHERE BOUNDING VOLUME HIERARCHY TREE
00540     //-------------------------------------------------------------------------
00541     //-------------------------------------------------------------------------
00542     //=========================================================================
00543 
00544     //=========================================================================
00545     // OCTANT SPHERE BOUNDING VOLUME HIERARCHY TREE
00546     //-------------------------------------------------------------------------
00547     //-------------------------------------------------------------------------
00548     //=========================================================================
00549 
00550     //=========================================================================
00551 #if defined(__gl_h_) || defined(__GL_H__)
00552 public:
00553     // DrawByOpenGL
00554     virtual void Draw () const  { DrawByOpenGL(); }
00555     virtual void DrawByOpenGL () const;
00556     virtual void DrawByOpenGL ( int currentLevel, int startLevel, int endLevel ) const;
00557     virtual void DrawByOpenGLOnlyEndLevel () const;
00558     virtual void DrawByOpenGLCollidedNodes () const;
00559     virtual void DrawByOpenGLCollidedNodesThat ( TransformationSupport<T> const & transformSupport ) const;
00560 #endif
00561     //-------------------------------------------------------------------------
00562 }; // END CLASS BVHTree
00563 //=============================================================================
00564 END_NAMESPACE_TAPs
00565 //-----------------------------------------------------------------------------
00566 // Include definition if TAPs_USE_EXPORT is not defined
00567 //#if !defined( TAPs_USE_EXPORT )
00568     #include "TAPsBVHTree.cpp"
00569 //#endif
00570 //-----------------------------------------------------------------------------
00571 #endif
00572 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00573 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines