TAPs 0.7.7.3
TAPsColDetFns_NEW.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsColDetFns_NEW.cpp
00003 SUKITTI PUNAK   (04/14/2010)
00004 UPDATE          (09/03/2010)
00005 ******************************************************************************/
00006 #include "TAPsColDetFns_NEW.hpp"
00007 // Using Inclusion Model (i.e. definitions are included in declarations)
00008 //                       (this name.cpp is included in name.hpp)
00009 // Each friend is defined directly inside its declaration.
00010 
00011 BEGIN_NAMESPACE_TAPs__CD__Fn
00012 //=============================================================================
00013 //-----------------------------------------------------------------------------
00014 
00015 // CDR for Elastic Rod with HalfEdgeModel<T>
00016 //-----------------------------------------------------------------------------
00017 #if ( defined TAPs_MODEL_ELASTIC_ROD_HPP && defined TAPs_HALF_EDGE_MODEL_HPP )
00018 template <typename T>
00019 bool CDR_ElasticRod_with_HalfEdgeModel ( 
00020     TAPs::ModelElasticRod<T> * const pERObj,        
00021     TAPs::OpenGL::HalfEdgeModel<T> * const  pObj,   
00022     TransformationSupport<T> * const pTransform     
00023 )
00024 {
00025     bool bResult = false;
00026 
00027     TransformationSupport<T> transform;
00028     if ( pTransform ) {
00029         transform.ReturnMatrixTransform() *= pTransform->GetMatrixTransform();
00030     }
00031     transform.ReturnMatrixTransform() *= pObj->GetTransform().GetMatrixTransform();
00032 
00033     // Collision Detection
00034     TransformationSupport<T> oldTrx( pObj->GetTransform() );    // store the old transformation
00035     pObj->GetTransform() = transform;                           // set to the new transformation
00036     bResult = pERObj->CDUnit().GetBVHTree()->TestOverlapWithTillLeafNodes( pObj->GetBVHTree() );    // collision detection automatically uses the new transformation
00037     pObj->GetTransform() = oldTrx;                              // restore the old transformation
00038     if ( !bResult ) return bResult;
00039 
00040     // Collision Response
00041     bResult = false;
00042     T Kc = pERObj->RefToParameters().K_CDRwTriangle;
00043 
00044     // The list of collided BV leaf nodes
00045     std::vector< BVHNode<T> * > & ER_BVNodes  = pERObj->CDUnit().GetBVHTree()->GetListOfCollidedNodes();
00046     std::vector< BVHNode<T> * > & Obj_BVNodes = pERObj->CDUnit().GetBVHTree()->GetListOfCollidedNodesThat();
00047     std::vector< HEVertex<T> * const > vertexList;
00048     int noOfVertices;
00049     Matrix4x4<T> & TrxMat = transform.ReturnMatrixTransform();
00050 
00051     for ( int i = 0; i < static_cast<int>( ER_BVNodes.size() ); ++i ) {
00052         BVHNodeLeafElasticRodNode<T> * NodeER = dynamic_cast< BVHNodeLeafElasticRodNode<T> * >( ER_BVNodes[i] );
00053         HEFace<T> * Triangle = Obj_BVNodes[i]->GetAPrimitiveHalfEdgeFace();
00054         vertexList = Triangle->GetPtrsToVerticesAndNumberOfVertices( noOfVertices );
00055         Vector3<T> cylA( NodeER->GetPtrToPrimitive_1()->Centerline[pERObj->GetCurrentIndex()].GetPosition() );
00056         Vector3<T> cylB( NodeER->GetPtrToPrimitive_2()->Centerline[pERObj->GetCurrentIndex()].GetPosition() );
00057         Vector3<T> posA( (TrxMat * Vector4<T>(vertexList[0]->GetPosition())).GetVector3() );
00058         Vector3<T> posB( (TrxMat * Vector4<T>(vertexList[1]->GetPosition())).GetVector3() );
00059         Vector3<T> posC( (TrxMat * Vector4<T>(vertexList[2]->GetPosition())).GetVector3() );
00060         if ( TAPs::CGMath<T>::FindIntersectionCylinderTriangle(
00061                 cylA, cylB,
00062                 pERObj->RefToParameters().Radius + pERObj->RefToParameters().Offset_CD_Triangle,
00063                 posA, posB, posC
00064         ) ) {
00065             Vector3<T> triCentroid( ( posA + posB + posC ) / 3.0 );
00066             Vector3<T> N( Triangle->GetNormal() );
00067             N.Normalized();
00068             Vector3<T> projPt1, projPt2;
00069             TAPs::CGMath<T>::FindProjectedPointOnAPlane( cylA, posB, N, projPt1 );
00070             TAPs::CGMath<T>::FindProjectedPointOnAPlane( cylB, posB, N, projPt2 );
00071             Vector3<T> V1( cylA - projPt1 );
00072             Vector3<T> V2( cylB - projPt2 );
00073             //Vector3<T> V1( cylA - triCentroid );
00074             //Vector3<T> V2( cylB - triCentroid );
00075             T value = V1 * N;
00076             if ( value < 0.0 ) {
00077                 if ( NodeER->GetPtrToPrimitive_1()->SimFlags.CheckSimulationConstraints( Enum::AddOn::IGNORE_CD_FORCE ) == false ) {
00078                     NodeER->GetPtrToPrimitive_1()->ExternalForce -= value*Kc * N;   // adjusted by force
00079                     //NodeER->GetPtrToPrimitive_1()->Centerline[pERObj->GetCurrentIndex()].SetPosition( cylA - value * N ); // adjusted by position
00080                 }
00081             }
00082             value = V2 * N;
00083             if ( value < 0.0 ) {
00084                 if ( NodeER->GetPtrToPrimitive_2()->SimFlags.CheckSimulationConstraints( Enum::AddOn::IGNORE_CD_FORCE ) == false ) {
00085                     NodeER->GetPtrToPrimitive_2()->ExternalForce -= value*Kc * N;   // adjusted by force
00086                     //NodeER->GetPtrToPrimitive_2()->Centerline[pERObj->GetCurrentIndex()].SetPosition( cylB - value * N ); // adjusted by position
00087                 }
00088             }
00089             bResult = true;
00090         }
00091     }
00092     return bResult;
00093 }
00094 #endif//#if ( defined TAPs_MODEL_ELASTIC_ROD_HPP && defined TAPs_HALF_EDGE_MODEL_HPP )
00095 //-----------------------------------------------------------------------------
00096 
00097     
00098     
00099     
00100 // CDR for Elastic Rod with Deformable Model Based On FEM
00101 //-----------------------------------------------------------------------------
00102 #if ( defined TAPs_MODEL_ELASTIC_ROD_HPP && defined TAPs_MODEL_DEF_BASED_ON_FEM_HPP )
00103 template <typename T, typename DATA>
00104 bool CDR_ElasticRod_with_ModelDefBasedOnFEM ( 
00105     TAPs::ModelElasticRod<T> * const pERObj,                        
00106     TAPs::ModelDefBasedOnFEM<T,DATA> * const    pDefModelFEMObj,    
00107     TransformationSupport<T> * const pTransform                     
00108 )
00109 {
00110     bool bResult;
00111 
00113     //ModelElasticRod<T>::SetOfElasticRodNodes::iterator it = pERObj->GetListOfNodes().begin();
00114     //while ( it != pERObj->GetListOfNodes().end() ) {
00115     //  it->IsCollide = false;
00116     //  ++it;
00117     //}
00118 
00119     TransformationSupport<T> transform;
00120     if ( pTransform ) {
00121         transform.RefToMatrixTransform() *= pTransform->GetMatrixTransform();
00122     }
00123     transform.RefToMatrixTransform() *= pDefModelFEMObj->PtrToBVHTreeOfSurfaceMesh()->GetTransform().GetMatrixTransform();
00124 
00125     // Collision Detection
00126     TransformationSupport<T> oldTrx( pDefModelFEMObj->PtrToBVHTreeOfSurfaceMesh()->GetTransform() );    // store the old transformation
00127     pDefModelFEMObj->PtrToBVHTreeOfSurfaceMesh()->GetTransform() = transform;                           // set to the new transformation
00128     bResult = pERObj->CDUnit().GetBVHTree()->TestOverlapWithTillLeafNodes( pDefModelFEMObj->PtrToBVHTreeOfSurfaceMesh() );  // collision detection automatically uses the new transformation
00129     pDefModelFEMObj->PtrToBVHTreeOfSurfaceMesh()->GetTransform() = oldTrx;                              // restore the old transformation
00130     if ( !bResult ) return bResult;
00131 
00132     // Collision Response
00133     bResult = false;
00134     T Kc = pERObj->RefToParameters().K_CDRwTriangle;
00135 
00136     // The list of collided BV leaf nodes
00137     std::vector< BVHNode<T> * > & ER_BVNodes  = pERObj->CDUnit().GetBVHTree()->GetListOfCollidedNodes();
00138     std::vector< BVHNode<T> * > & Obj_BVNodes = pERObj->CDUnit().GetBVHTree()->GetListOfCollidedNodesThat();
00139     std::vector< HEVertex<T> * const > vertexList;
00140     int noOfVertices;
00141     Matrix4x4<T> & TrxMat = transform.RefToMatrixTransform();
00142 
00143     for ( int i = 0; i < static_cast<int>( ER_BVNodes.size() ); ++i ) {
00144         BVHNodeLeafElasticRodNode<T> * NodeER = dynamic_cast< BVHNodeLeafElasticRodNode<T> * >( ER_BVNodes[i] );
00145         HEFace<T> * Triangle = Obj_BVNodes[i]->GetAPrimitiveHalfEdgeFace();
00146         vertexList = Triangle->GetPtrsToVerticesAndNumberOfVertices( noOfVertices );
00147         Vector3<T> cylA( NodeER->GetPtrToPrimitive_1()->Centerline[pERObj->GetCurrentIndex()].GetPosition() );
00148         Vector3<T> cylB( NodeER->GetPtrToPrimitive_2()->Centerline[pERObj->GetCurrentIndex()].GetPosition() );
00149         Vector3<T> posA( (TrxMat * Vector4<T>(vertexList[0]->GetPosition())).GetVector3() );
00150         Vector3<T> posB( (TrxMat * Vector4<T>(vertexList[1]->GetPosition())).GetVector3() );
00151         Vector3<T> posC( (TrxMat * Vector4<T>(vertexList[2]->GetPosition())).GetVector3() );
00152         if ( TAPs::CGMath<T>::FindIntersectionCylinderTriangle(
00153                 cylA, cylB,
00154                 pERObj->RefToParameters().Radius + pERObj->RefToParameters().Offset_CD_Triangle,
00155                 posA, posB, posC
00156         ) ) {
00157             Vector3<T> triCentroid( ( posA + posB + posC ) / 3.0 );
00158             Vector3<T> N( Triangle->GetNormal() );
00159             N.Normalized();
00160             Vector3<T> projPt1, projPt2;
00161             TAPs::CGMath<T>::FindProjectedPointOnAPlane( cylA, posB, N, projPt1 );
00162             TAPs::CGMath<T>::FindProjectedPointOnAPlane( cylB, posB, N, projPt2 );
00163             Vector3<T> V1( cylA - projPt1 );
00164             Vector3<T> V2( cylB - projPt2 );
00165             //Vector3<T> V1( cylA - triCentroid );
00166             //Vector3<T> V2( cylB - triCentroid );
00167             T value = V1 * N;
00168             if ( value < 0.0 ) {
00169                 if ( NodeER->GetPtrToPrimitive_1()->SimFlags.CheckSimulationConstraints( Enum::AddOn::IGNORE_CD_FORCE ) == false ) {
00170                     NodeER->GetPtrToPrimitive_1()->ExternalForce -= value*Kc * N;   // adjusted by force
00171                     //NodeER->GetPtrToPrimitive_1()->Centerline[pERObj->GetCurrentIndex()].SetPosition( cylA - value * N ); // adjusted by position
00172                 }
00173             }
00174             value = V2 * N;
00175             if ( value < 0.0 ) {
00176                 if ( NodeER->GetPtrToPrimitive_2()->SimFlags.CheckSimulationConstraints( Enum::AddOn::IGNORE_CD_FORCE ) == false ) {
00177                     NodeER->GetPtrToPrimitive_2()->ExternalForce -= value*Kc * N;   // adjusted by force
00178                     //NodeER->GetPtrToPrimitive_2()->Centerline[pERObj->GetCurrentIndex()].SetPosition( cylB - value * N ); // adjusted by position
00179                 }
00180             }
00181             bResult = true;
00182 
00184             //NodeER->GetPtrToPrimitive_1()->IsCollide = true;
00185             //NodeER->GetPtrToPrimitive_2()->IsCollide = true;
00186         }
00187     }
00188     return bResult;
00189 }
00190 #endif//#if ( defined TAPs_MODEL_ELASTIC_ROD_HPP && defined TAPs_MODEL_DEF_BASED_ON_FEM_HPP )
00191 //-----------------------------------------------------------------------------
00192 
00193 
00194 
00195 
00196 //-----------------------------------------------------------------------------
00197 //=============================================================================
00198 END_NAMESPACE_TAPs__CD__Fn
00199 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00200 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines