TAPs 0.7.7.3
TAPsElasticRodCD.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsElasticRodCD.hpp
00003 ******************************************************************************/
00015 /******************************************************************************
00016 SUKITTI PUNAK   (08/21/2009)
00017 UPDATE          (01/04/2011)
00018 ******************************************************************************/
00019 #ifndef TAPs_ELASTIC_ROD__CD_HPP
00020 #define TAPs_ELASTIC_ROD__CD_HPP
00021 
00022 #include "TAPsElasticRodParameters.hpp"
00023 #include "TAPsElasticRodNode.hpp"
00024 
00025 // For collision detection
00026 #include "../../CD/TAPsBVHTree.hpp"
00027 #include "../../CD/TAPsMultiBoundingVolume.hpp" // for collision detection by bounding volumes
00028 #include "../../CD/TAPsBVHNodeLeafElasticRodNode.hpp"
00029 #include "../../Support/TAPsTransformationSupport.hpp"      // Has A
00030 #include "../../OpenGLModel/TAPsHETriMeshOneModelMultiParts.hpp"
00031 //#include "../../OpenGLModel/TAPsHalfEdgeModel.hpp"
00032 //#include "ModelDefBasedOnFEMSupport/TAPsModelDefBasedOnFEM.hpp"
00033 
00034 //#define   TAPs_ADD_IMPLICIT_OBJECTS
00035 
00036 #ifdef  TAPs_ADD_IMPLICIT_OBJECTS
00037 #include "../TAPsListOfImplicitObjects.hpp"
00038 #endif//TAPs_ADD_IMPLICIT_OBJECTS
00039 
00040 // For drawing by OpenGL
00041 #if defined(__gl_h_) || defined(__GL_H__)
00042     #include "../../OpenGL/TAPsOpenGLObj.hpp"
00043 #endif
00044 
00045 BEGIN_NAMESPACE_TAPs
00046 //=============================================================================
00047 template <typename T>
00048 class ElasticRodCD {
00049 
00050     typedef std::vector< ElasticRodNode<T> >    SetOfElasticRodNodes;
00051 
00052     // Member Functions -------------------------------------------------------
00054     friend std::ostream & operator<< ( std::ostream &output, ElasticRodCD<T> const &obj )
00055     {
00056         output << obj.StrInfo();
00057         return output;
00058     }
00059 
00060 public:
00061 
00062 
00063     // For Debugging
00064     Vector3<T> d_ShaftSPt, d_ShaftEPt;
00065     int d_NumOfWrappingLoops;
00066     std::vector< Vector3<T> > d_NodePositions;
00067 
00068 
00069     class Loop {
00070         Loop ( int startPt, int endpt ) : StartPt(startPt), EndPt(endPt) {}
00071 
00073         void CalLoopCenter ( Vector3<T> & center ) const
00074         {
00075             center = m_pNodeList[EndPt];
00076             for ( int i = StartPt; i < EndPt; ++i ) {
00077                 center += m_pNodeList[i];
00078             }
00079             center /= EndPt - StartPt + 1;
00080         }
00081 
00082         int StartPt;    
00083         int EndPt;      
00084     };
00085 
00086 public:
00087     //-------------------------------------------------------------------------
00089     ElasticRodCD ();
00090 
00092     //ElasticRodCD ( ElasticRodCD<T> const &obj );
00093 
00095     virtual ~ElasticRodCD ();
00096 
00098     void CreateCD (
00099         int *                       pIdxCurr,       
00100         int *                       pIdxNext,       
00101         ElasticRodParameters<T> *   pParameters,    
00102         SetOfElasticRodNodes *      pNodeList       
00103     );
00104 
00106     void DeleteCD ();
00107 
00109     void Reset ();
00110 
00112     void BuildBVHTree ();
00113 
00115     void UpdateBVHTree ();
00116 
00118     bool CDRwith (
00119         MultiBoundingVolume<T> * const pMBVObj,     
00120         TransformationSupport<T> * const pTransform 
00121     );
00122 
00124     bool CDRbySegmentsWith (
00125         std::vector<unsigned int> const * pSegments,    
00126         MultiBoundingVolume<T> * const pMBVObj,         
00127         TransformationSupport<T> * const pTransform     
00128     );
00129 
00130 
00135     bool CDRwithMBVforShaftLikeObject_wCheckWrappingLoops (
00136         MultiBoundingVolume<T> * const pMBVObj,         
00137         Vector3<T> * const pShaftStartPt,               
00138         Vector3<T> * const pShaftEndPt,                 
00139         T const *   pShaftRadiusOfInfluence,            
00140         TransformationSupport<T> * const pTransform,    
00141         T * pNumOfWrappingLoops,                        
00142         bool *  isCCW                                   
00143         //, std::vector<Loop> * pWrappingLoops  //!< list of formed wrapping loops
00144     );
00145 
00150     bool CDRbySegmentsWithMBVforShaftLikeObject_wCheckWrappingLoops (
00151         std::vector<unsigned int> const * pSegments,    
00152         MultiBoundingVolume<T> * const pMBVObj,         
00153         Vector3<T> * const pShaftStartPt,               
00154         Vector3<T> * const pShaftEndPt,                 
00155         T const *   pShaftRadiusOfInfluence,            
00156         TransformationSupport<T> * const pTransform,    
00157         T * pNumOfWrappingLoops,                        
00158         bool *  isCCW                                   
00159         //, std::vector<Loop> * pWrappingLoops  //!< list of formed wrapping loops
00160     );
00161 
00163     inline virtual bool CDRwith (
00164         TAPs::OpenGL::HETriMeshOneModelMultiParts<T> * pObj,    
00165         TransformationSupport<T> * const pTransform = NULL      
00166     );
00167 
00168     #ifdef  TAPs_ADD_IMPLICIT_OBJECTS
00169 
00170     bool CDRwithImplicitObject (
00171         ImplicitObject<T> * const pImpObj   
00172     );
00173     #endif//TAPs_ADD_IMPLICIT_OBJECTS
00174 
00176     inline void CDRforSelfIntersections ();
00177 
00178     //-------------------------------------------------------------------------
00180     virtual std::string StrInfo () const;
00181 
00182     //-------------------------------------------------------------------------
00183     // Operations
00184 
00186     BVHTree<T> * GetBVHTree ()  { return m_BVHTree; }
00187 
00188     // Data Members -----------------------------------------------------------
00189 //=============================================================================
00190 protected:
00191     // Member Functions -------------------------------------------------------
00192 
00194     BVHTree<T> * BuildBVHTreeRecursively ( 
00195         int startID,                    
00196         BVHNode<T> ** childNodeList,    
00197         int numOfChildNodes             
00198     );
00199 
00201     void UpdateBVHNodeRecursively ( BVHNode<T> * node );
00203     void CheckSelfIntersectionsNextLevelRecursively ( BVHNode<T> * node1, BVHNode<T> * node2 );
00205     T    CheckSelfIntersectionsRecursively ( BVHNode<T> * node1, BVHNode<T> * node2 );
00206 
00207     #ifdef  TAPs_ADD_IMPLICIT_OBJECTS
00208 
00209     void CollisionResponseWithImplicitObject_Sphere ( ImplicitObject_Sphere<T> * const pImpObj );
00211     void CollisionResponseWithImplicitObject_Torus ( ImplicitObject_Torus<T> * const pImpObj );
00212     #endif//TAPs_ADD_IMPLICIT_OBJECTS
00213 
00214     // Data Members -----------------------------------------------------------
00215 
00216     BVHTree<T> *    m_BVHTree;  
00217 
00218     TransformationSupport<T>    m_DummyTransform;   
00219 
00220     int *                       m_pIdxCurr;     
00221     int *                       m_pIdxNext;     
00222     ElasticRodParameters<T> *   m_pParameters;  
00223     SetOfElasticRodNodes *      m_pNodeList;    
00224 
00225 
00226 
00227 //=============================================================================
00228 #ifdef  TAPs_SIM_WITH_POSITION_BASED_DYNAMICS
00229 //-----------------------------------------------------------------------------
00230 public:
00232     void UpdateBVHTree_PBD ();
00234     inline void CDRforSelfIntersections_PBD ();
00235 protected:
00237     void UpdateBVHNodeRecursively_PBD ( BVHNode<T> * node );
00239     void CheckSelfIntersectionsNextLevelRecursively_PBD ( BVHNode<T> * node1, BVHNode<T> * node2 );
00241     T    CheckSelfIntersectionsRecursively_PBD ( BVHNode<T> * node1, BVHNode<T> * node2 );
00243     void CollisionResponseByUpdatingGeometric_PBD (
00244         Vector3<T> & displacementVector,        
00245         BVHNodeLeafElasticRodNode<T> * Node1,   
00246         BVHNodeLeafElasticRodNode<T> * Node2    
00247     );
00248 //-----------------------------------------------------------------------------
00249 #endif//TAPs_SIM_WITH_POSITION_BASED_DYNAMICS
00250 //=============================================================================
00251 
00252 
00253 
00254 //=============================================================================
00255 #ifdef  TAPs_ADVANCED_SELF_COLLISION_DETECTION
00256 //-----------------------------------------------------------------------------
00257 private:
00258     template <typename T>
00259     class SelfCDExtraData {
00260     public:
00261         SelfCDExtraData ( unsigned int linkID_A, unsigned int linkID_B, T average_radius ) 
00262             : LinkID_A( linkID_A ), LinkID_B( linkID_B ), AverageRadius( average_radius )
00263         {}
00264 
00265         //Vector3<T>    CalLinkCenter_A ()
00266         //{
00267         //  return ( (*m_pNodeList)[LinkID_A+1].Centerline[IdxCurr] + (*m_pNodeList)[LinkID_A].Centerline[IdxCurr] ) * T(0.5);
00268         //}
00269 
00270         //Vector3<T>    CalLinkCenter_B ()
00271         //{
00272         //  return ( (*m_pNodeList)[LinkID_B+1].Centerline[IdxCurr] + (*m_pNodeList)[LinkID_B].Centerline[IdxCurr] ) * T(0.5);
00273         //}
00274 
00275         //Vector3<T>    CalAverageLinkCenter ()
00276         //{
00277         //  return ( LinkCenter_A + LinkCenter_B ) * T(0.5);
00278         //}
00279 
00280         #if defined(__gl_h_) || defined(__GL_H__)
00281         void Draw () const
00282         {
00283             glPushAttrib( GL_ALL_ATTRIB_BITS );
00284             glDisable( GL_LIGHTING );
00285             glPointSize( 2 );
00286             glBegin( GL_POINTS );
00287                 glColor3f( 1, 0, 0 );
00288                 glVertex3fv( LinkCenter_A.GetDataFloat() );
00289                 glColor3f( 0, 1, 0 );
00290                 glVertex3fv( AverageCenter.GetDataFloat() );
00291                 glColor3f( 0, 0, 10 );
00292                 glVertex3fv( LinkCenter_B.GetDataFloat() );
00293             glEnd();
00294             glLineWidth( 1 );
00295             glBegin( GL_LINE_STRIP );
00296                 glColor3f( 1, 0, 0 );
00297                 glVertex3fv( LinkCenter_A.GetDataFloat() );
00298                 glColor3f( 0, 1, 0 );
00299                 glVertex3fv( AverageCenter.GetDataFloat() );
00300                 glColor3f( 0, 0, 1 );
00301                 glVertex3fv( LinkCenter_B.GetDataFloat() );
00302             glEnd();
00303             glPopAttrib();
00304         }
00305         #endif//defined(__gl_h_) || defined(__GL_H__)
00306 
00307         unsigned int    LinkID_A;       
00308         unsigned int    LinkID_B;       
00309 
00310         Vector3<T>  LinkCenter_A;       
00311         Vector3<T>  LinkCenter_B;       
00312         Vector3<T>  AverageCenter;      
00313         T           AverageRadius;      
00314     };
00315 
00316 public:
00317 
00318     std::list< SelfCDExtraData<T> > ListOfSelfCDExtraData;  
00319     bool ** SelfCDExtraDataTrackStatus; 
00320 
00322     inline void UpdateSelfCDExtraData ();
00323 
00325     //inline void PreventMissedCDWithSelfCDExtraData ();
00326 
00328     inline void CheckAndRestoreWithSelfCDExtraData ( unsigned int nodeID );
00329 
00330     //int StartLinkIDofCurrentTrackedKnot;  //! start link id of the current tracked knot for adjusting self CD
00331     //int EndLinkIDofCurrentTrackedKnot;        //! start link id of the current tracked knot for adjusting self CD
00332 
00333 protected:
00335     inline void ResetSelfCDExtraData ();
00336 
00338     inline bool InitSelfCDExtraData ();
00339 
00341     inline void DeleteSelfCDExtraData ();
00342 
00344     inline void AddSelfCDExtraDataForALinkPair ( unsigned int nodeID_1, unsigned int nodeID_2 );
00345 
00346 //-----------------------------------------------------------------------------
00347 #endif//TAPs_ADVANCED_SELF_COLLISION_DETECTION
00348 //=============================================================================
00349 
00350 
00351 //=============================================================================
00352 private:
00353     // Member Functions -------------------------------------------------------
00354 //=============================================================================
00355 
00356 //OpenGL
00357 //-----------------------------------------------------------------------------
00358 //#define __GL_H__
00359 //#define   TAPs_USE_GLSL
00360 //-----------------------------------------------------------------------------
00361 #if defined(__gl_h_) || defined(__GL_H__)
00362 public:
00364     inline virtual void Draw () const;
00365     
00367     #ifdef  TAPs_ADVANCED_SELF_COLLISION_DETECTION
00368     inline virtual void DrawExtraSelfCDData () const;
00369     #else //TAPs_ADVANCED_SELF_COLLISION_DETECTION
00370     inline virtual void DrawExtraSelfCDData () const {};
00371     #endif//TAPs_ADVANCED_SELF_COLLISION_DETECTION
00372 
00373     //OpenGL::OpenGLObj Rendering;  //!< OpenGL Object has material color properties
00374 #endif//defined(__gl_h_) || defined(__GL_H__)
00375 //-----------------------------------------------------------------------------
00376 //=============================================================================
00377 }; // CLASS END: ElasticRodCD
00378 //=============================================================================
00379 END_NAMESPACE_TAPs
00380 //-----------------------------------------------------------------------------
00381 #include "TAPsElasticRodCD.cpp"
00382 //-----------------------------------------------------------------------------
00383 #endif
00384 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00385 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines