TAPs 0.7.7.3
TAPsModelDefBasedOnFEMHex_CD.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsModelDefBasedOnFEMHex_CD.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (04/15/2010)
00009 UPDATE          (12/05/2010)
00010 ******************************************************************************/
00011 #include "TAPsModelDefBasedOnFEMHex_CD.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs__FEM
00017 //=============================================================================
00018 //-----------------------------------------------------------------------------
00019 template <typename T>
00020 ModelDefBasedOnFEMHex_CD<T>::ModelDefBasedOnFEMHex_CD ()
00021     : ModelDefBasedOnFEM_CD()
00022 {}
00023 //-----------------------------------------------------------------------------
00024 //template <typename T>
00025 //ModelDefBasedOnFEMHex_CD<T>::ModelDefBasedOnFEMHex_CD ( ModelDefBasedOnFEMHex_CD<T> const &orig )
00026 //  :
00027 //{}
00028 //-----------------------------------------------------------------------------
00029 template <typename T>
00030 ModelDefBasedOnFEMHex_CD<T>::~ModelDefBasedOnFEMHex_CD ()
00031 {}
00032 //-----------------------------------------------------------------------------
00033 template <typename T>
00034 std::string ModelDefBasedOnFEMHex_CD<T>::StrInfo () const
00035 {
00036     std::ostringstream ss;
00037     ss << "ModelDefBasedOnFEMHex_CD<" << typeid(T).name() << ">";
00038     ss << "\n";
00039     return ss.str();
00040 }
00041 //-----------------------------------------------------------------------------
00042 //=============================================================================
00043 // Assignment Operator
00044 //-----------------------------------------------------------------------------
00045 //template <typename T>
00046 //inline ModelDefBasedOnFEMHex_CD<T> & ModelDefBasedOnFEMHex_CD<T>::operator= ( ModelDefBasedOnFEMHex_CD<T> const &orig )
00047 //{ 
00048 //    return *this;
00049 //}
00050 //-----------------------------------------------------------------------------
00051 //=============================================================================
00052 
00053 //-----------------------------------------------------------------------------
00054 template <typename T>
00055 bool ModelDefBasedOnFEMHex_CD<T>::Init (
00056     OpenGL::OpenGLHalfEdgeTrigonalModel<T> * pHEModel,  
00057     FEM::MeshHexahedra<T> * pFEMMesh                    
00058 )
00059 {
00060     ModelDefBasedOnFEM_CD<T>::Init( pHEModel );
00061     m_pFEMMesh = pFEMMesh;
00062     return true;
00063 }
00064 
00065 //-----------------------------------------------------------------------------
00066 template <typename T>
00067 bool ModelDefBasedOnFEMHex_CD<T>::CDRwith (
00068     MultiBoundingVolume<T> * const pMBVObj      
00069 )
00070 {
00071     bool bResult = CORE_CDwith( pMBVObj );
00072     if ( bResult ) {
00073         CORE_CR();
00074     }
00075     return bResult;
00076 }
00077 
00078 //-----------------------------------------------------------------------------
00079 template <typename T>
00080 bool ModelDefBasedOnFEMHex_CD<T>::CDRwith (
00081     MultiBoundingVolume<T> * const pMBVObj,     
00082     TransformationSupport<T> * const pTransform 
00083 )
00084 {
00085     TransformationSupport<T> trx = pMBVObj->GetTransform();
00086     pMBVObj->GetTransform().RefToMatrixTransform() = pTransform->RefToMatrixTransform() * pMBVObj->GetTransform().RefToMatrixTransform();
00087     bool bResult = CORE_CDwith( pMBVObj );
00088     if ( bResult ) {
00089         CORE_CR();
00090     }
00091     pMBVObj->GetTransform() = trx;
00092     return bResult;
00093 }
00094 
00095 //-----------------------------------------------------------------------------
00096 template <typename T>
00097 bool ModelDefBasedOnFEMHex_CD<T>::CORE_CDwith (
00098     MultiBoundingVolume<T> * const pMBVObj      
00099 )
00100 {
00101     // [07/07/2010]
00102     // ClearCollidedParts() has been moved out.
00103     // Without resetting collided parts, 
00104     // this collision detection fn will not affect previous collision tests.
00105     // So that we can do collision test of this object with more than one MBV object.
00106     // But ClearCollidedParts() has to be explicitly called 
00107     // to clear the collided parts before each main collision detection test.
00108     //
00109     return ModelDefBasedOnFEM_CD<T>::CORE_CDwith( pMBVObj, &m_ListOfCDObjs );
00110 }
00111 
00112 //-----------------------------------------------------------------------------
00113 template <typename T>
00114 void ModelDefBasedOnFEMHex_CD<T>::CORE_CR ()
00115 {
00116     for ( unsigned int i = 0; i < m_ListOfCDObjs.size(); ++i ) {
00117         GenCDForcesOnFEMMeshByCDObj( m_ListOfCDObjs[i] );
00118     }
00119 }
00120 
00121 //-----------------------------------------------------------------------------
00122 template <typename T>
00123 void ModelDefBasedOnFEMHex_CD<T>::ClearCollidedParts ()
00124 {
00125     m_pFEMMesh->ClearAllCDLoads();
00126     //for ( unsigned int i = 0; i < m_ListOfCDObjs.size(); ++i ) {
00127     //  ClearFEMMeshAdjustmentFromCDObj( m_ListOfCDObjs[i] );
00128     //}
00129     m_ListOfCDObjs.clear();
00130 }
00131 
00132 //-----------------------------------------------------------------------------
00133 template <typename T>
00134 void ModelDefBasedOnFEMHex_CD<T>::GenCDForcesOnFEMMeshByCDObj ( CDObj<T> & cdObj )
00135 {
00136     // Set collision (i.e. point) force
00137     cdObj.PtForce.SetID( cdObj.pHEVertex->GetID() );
00138     cdObj.PtForce.RefToPosition() = cdObj.pHEVertex->GetBarycentricPtr()->RetGenBaryCoords();
00139     cdObj.PtForce.RefToForce() = cdObj.PosAdj * m_tCDForceScale;
00140     // Add the collision force to the FEM mesh
00141     // The force referred in cdObj is in FEM local coordinates.
00142     // Therefore no coordinates conversion is needed.
00143     m_pFEMMesh->AddCDForceInLocalCoordinates( &cdObj.PtForce );
00144 }
00145 
00146 //-----------------------------------------------------------------------------
00147 template <typename T>
00148 void ModelDefBasedOnFEMHex_CD<T>::GenGrabbedForcesOnFEMMeshByCDObj (
00149     CDObj<T> & cdObj,                       
00150     Vector3<T> const & grabbingObjPos_LS    
00151 )
00152 {
00153     // Set collision (i.e. point) force
00154     cdObj.PtForce.SetID( cdObj.pHEVertex->GetID() );
00155     cdObj.PtForce.RefToPosition() = cdObj.pHEVertex->GetBarycentricPtr()->RetGenBaryCoords();
00156 
00157     //cdObj.PtForce.RefToForce() = cdObj.PosAdj * -m_tGrabbedForceScale;
00158     Vector3<T> dv = grabbingObjPos_LS - cdObj.pHEVertex->GetPosition();
00159     cdObj.PtForce.RefToForce() = dv * m_tGrabbedForceScale;
00160     cdObj.PosAdj = grabbingObjPos_LS;
00161 
00162     // Add the collision force to the FEM mesh
00163     // The force referred in cdObj is in FEM local coordinates.
00164     // Therefore no coordinates conversion is needed.
00165     m_pFEMMesh->AddCDForceInLocalCoordinates( &cdObj.PtForce );
00166 }
00167 
00168 //-----------------------------------------------------------------------------
00169 template <typename T>
00170 void ModelDefBasedOnFEMHex_CD<T>::AdjustSurfaceMeshByCDObjs ()
00171 {
00172     for ( unsigned int i = 0; i < m_ListOfCDObjs.size(); ++i ) {
00173         AdjustSurfaceMeshVertexPositionByCDObj( m_ListOfCDObjs[i] );
00174     }
00175 
00176     /*
00177     HEVertex<T> * vertex = m_pHEModel->GetVertexList()->Head();
00178     while ( vertex != NULL ) {
00179         if ( vertex->Counter1 > 0 ) {
00180             Vector3<T> pos = (vertex->GetPosition() + vertex->GetPosition_2()) / T(2);
00181             vertex->GetProtectedPosition_2() = vertex->GetPosition();
00182             vertex->GetProtectedPosition() = pos;
00183             --vertex->Counter1;
00184         }
00185         vertex = vertex->Next();
00186     }
00187     //*/
00188 }
00189 
00190 //-----------------------------------------------------------------------------
00191 template <typename T>
00192 void ModelDefBasedOnFEMHex_CD<T>::AdjustSurfaceMeshVertexPositionByCDObj ( CDObj<T> & cdObj )
00193 {
00194     // Move the vertex position
00195     //cdObj.pHEVertex->GetProtectedPosition() = cdObj.PosAdj;
00196     cdObj.pHEVertex->GetProtectedPosition() += cdObj.PosAdj;
00197 
00198     /*
00199     cdObj.pHEVertex->GetProtectedPosition_2() = cdObj.pHEVertex->GetProtectedPosition();
00200     cdObj.pHEVertex->GetProtectedPosition() += cdObj.PosAdj;
00201     cdObj.pHEVertex->Counter1 = 1000;
00202     //*/
00203 }
00204 
00205 //-----------------------------------------------------------------------------
00206 template <typename T>
00207 void ModelDefBasedOnFEMHex_CD<T>::AdjustSurfaceMeshVertexPositionDueToGrabbedVertex ( CDObj<T> & cdObj )
00208 {
00209     // Move the vertex position
00210     cdObj.pHEVertex->GetProtectedPosition() = cdObj.PosAdj;
00211 }
00212 
00214 //template <typename T>
00215 //void ModelDefBasedOnFEMHex_CD<T>::ClearFEMMeshAdjustmentFromCDObj ( CDObj<T> & cdObj )
00216 //{
00217 //  cdObj.pHEVertex->SetMarkedStatus( false );
00218 //}
00219 
00220 
00221 
00222 
00223 //=============================================================================
00224 // Grabbing
00225 //-----------------------------------------------------------------------------
00226 template <typename T>
00227 void ModelDefBasedOnFEMHex_CD<T>::MoveGrabbedPart ( int grabbedPartID )
00228 {
00229     // Check against invalid range
00230     if ( grabbedPartID < 0 || grabbedPartID >= MAX_NUMBER_OF_GRABBED_PARTS )    return;
00231 
00232     // Move the grabbed part
00233     if ( m_ListOfGrabbedParts_IsGrabbedStatus[ grabbedPartID ] ) {
00234         Matrix4x4<T> invTrx = m_pHEModel->GetTransform().GetInverseMatrixTransform();
00235         for ( std::vector< GrabbedPartData >::iterator it = m_ListOfGrabbedParts.begin(); it != m_ListOfGrabbedParts.end(); ++it ) {
00236             if ( grabbedPartID == it->ID ) {
00237                 Vector3<T> grabbingPos = invTrx * *it->pGrabbingPosition;
00238                 for ( std::vector< CDObj<T> >::iterator data = it->GrabbedVertices.begin(); data != it->GrabbedVertices.end(); ++data ) {
00239                     GenGrabbedForcesOnFEMMeshByCDObj( *data, grabbingPos );
00240                 }
00241                 break;
00242             }
00243         }
00244     }
00245 }
00246 //-----------------------------------------------------------------------------
00247 template <typename T>
00248 void ModelDefBasedOnFEMHex_CD<T>::MoveAllGrabbedParts ()
00249 {
00250     Matrix4x4<T> invTrx = m_pHEModel->GetTransform().GetInverseMatrixTransform();
00251     for ( std::vector< GrabbedPartData >::iterator it = m_ListOfGrabbedParts.begin(); it != m_ListOfGrabbedParts.end(); ++it ) {
00252         Vector3<T> grabbingPos = invTrx * *it->pGrabbingPosition;
00253         for ( std::vector< CDObj<T> >::iterator data = it->GrabbedVertices.begin(); data != it->GrabbedVertices.end(); ++data ) {
00254             GenGrabbedForcesOnFEMMeshByCDObj( *data, grabbingPos );
00255         }
00256     }
00257 }
00258 //-----------------------------------------------------------------------------
00259 template <typename T>
00260 void ModelDefBasedOnFEMHex_CD<T>::AdjustSurfaceMeshDueToGrabbedParts ()
00261 {
00262     for ( std::vector< GrabbedPartData >::iterator it = m_ListOfGrabbedParts.begin(); it != m_ListOfGrabbedParts.end(); ++it ) {
00263         for ( std::vector< CDObj<T> >::iterator data = it->GrabbedVertices.begin(); data != it->GrabbedVertices.end(); ++data ) {
00264             AdjustSurfaceMeshVertexPositionDueToGrabbedVertex( *data );
00265         }
00266     }
00267 }
00268 //-----------------------------------------------------------------------------
00269 // Grabbing
00270 //=============================================================================
00271 
00272 
00273 
00274 
00275 //=============================================================================
00276 #if defined(__gl_h_) || defined(__GL_H__)
00277 //-----------------------------------------------------------------------------
00278 //-----------------------------------------------------------------------------
00279 #endif // #if defined(__gl_h_) || defined(__GL_H__)
00280 //=============================================================================
00281 
00282 //=============================================================================
00283 END_NAMESPACE_TAPs__FEM
00284 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00285 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines