TAPs 0.7.7.3
TAPsModelForSurgery.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsModelForSurgery.cpp
00003 
00004 ModelForSurgery class (cpp file).
00005 
00006 SUKITTI PUNAK   (08/06/2008)
00007 UPDATE          (09/03/2010)
00008 ******************************************************************************/
00009 #include "TAPsModelForSurgery.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs
00015 //=============================================================================
00016 template <typename T> int ModelForSurgery<T>::m_iTotal = 0;
00017 
00018 //-----------------------------------------------------------------------------
00019 template <typename T>
00020 ModelForSurgery<T>::ModelForSurgery (
00021         std::string data_location,      
00022         enum ModelType,                 
00023         T Kpointmass,                   
00024         T Kstiffness,                   
00025         T Kdamper,                      
00026         T HomeKstiffness,               
00027         T HomeKdamper,                  
00028         T predefinedTimeStep,           
00029         int numSimSubSteps              
00030 
00031 )
00032     : m_iId( m_iTotal++ ),
00033       m_strName( "" ),
00034       m_strDataLocation( data_location ),
00035       m_tPtMass( Kpointmass ),
00036       m_tKStiffness( Kstiffness ),
00037       m_tKDamper( Kdamper ),
00038       m_tHomeKStiffness( HomeKstiffness ),
00039       m_tHomeKDamper( HomeKdamper ),
00040       m_OperationFlags( SIMULATION_ON | COLLISION_ON | RENDER_ON ),
00041       m_tPredefinedTimeStep( predefinedTimeStep ),
00042       m_iNumSimSubSteps( numSimSubSteps )
00043 {}
00044 
00045 //-----------------------------------------------------------------------------
00046 template <typename T>
00047 ModelForSurgery<T>::~ModelForSurgery ()
00048 {
00049     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00050     while ( pos != m_Parts.end() ) {
00051         delete (*pos);
00052         ++pos;
00053     }
00054     m_Parts.clear();
00055 }
00056 
00057 //-----------------------------------------------------------------------------
00058 template <typename T>
00059 std::string ModelForSurgery<T>::StrInfo () const
00060 {
00061     std::ostringstream ss;
00062     ss << "ModelForSurgery<" << typeid(T).name() << ">";
00063     return ss.str();
00064 }
00065 
00066 //-----------------------------------------------------------------------------
00067 template <typename T>
00068 void ModelForSurgery<T>::ApplyAndResetTransform ()
00069 {
00070     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00071     while ( pos != m_Parts.end() ) {
00072         (*pos)->GetTransform().SetMatrixTransform( GetTransform().RefToMatrixTransform() * (*pos)->GetTransform().RefToMatrixTransform() );
00073         (*pos)->ApplyAndResetTransform();
00074         ++pos;
00075     }
00076     // Set Transformation to Identity Matrix
00077     GetTransform().MakeIdentity();
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 template <typename T>
00082 void ModelForSurgery<T>::SetOperationFlag ( enum Operation flag )
00083 {
00084     m_OperationFlags.SetFlag( flag );
00085     //if ( flag & SIMULATION_ON ) {}
00086     if ( flag & COLLISION_ON ) {
00087     }
00088     //if ( flag & RENDER_ON ) {}
00089 }
00090 
00091 //-----------------------------------------------------------------------------
00092 template <typename T>
00093 void ModelForSurgery<T>::ClearOperationFlag ( enum Operation flag )
00094 {
00095     m_OperationFlags.ClearFlag( flag );
00096     //if ( flag & SIMULATION_ON ) {}
00097     //if ( flag & COLLISION_ON ) {
00098     //  for ( int i = 0; i < static_cast<int>( m_Parts.size() ); ++i ) {
00099     //      for ( int p = 0; p < static_cast<int>( m_Parts[i].GetListOfParts().size() ); ++p ) {
00100     //          m_Parts[i].GetListOfParts()[p];
00101     //      }
00102     //  }
00103     //}
00104     //if ( flag & RENDER_ON ) {}
00105 }
00106 
00107 
00108 //=============================================================================
00109 // For Simulation
00110 //-----------------------------------------------------------------------------
00111 template <typename T>
00112 void ModelForSurgery<T>::AdvanceSimulation ( T tCurrent, T tNext )
00113 {
00114     if ( CheckOperationFlag( SIMULATION_ON ) == false ) return;
00115     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00116     while ( pos != m_Parts.end() ) {
00117         (*pos)->AdvanceSimulation( tCurrent, tNext );
00118         ++pos;
00119     }
00120 }
00121 //-----------------------------------------------------------------------------
00122 template <typename T>
00123 void ModelForSurgery<T>::AdvanceSimulation ( T tCurrent )
00124 {
00125     if ( CheckOperationFlag( SIMULATION_ON ) == false ) return;
00126     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00127     while ( pos != m_Parts.end() ) {
00128         (*pos)->AdvanceSimulation( tCurrent );
00129         ++pos;
00130     }
00131 }
00132 //-----------------------------------------------------------------------------
00133 template <typename T>
00134 void ModelForSurgery<T>::AdvanceSimulation ( Simulation::SimClock<T> & simClock )
00135 {
00136     if ( CheckOperationFlag( SIMULATION_ON ) == false ) return;
00137     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00138     while ( pos != m_Parts.end() ) {
00139         (*pos)->AdvanceSimulation( simClock );
00140         ++pos;
00141     }
00142 }
00143 //-----------------------------------------------------------------------------
00144 
00145 
00146 //=============================================================================
00147 // For Collision Detection
00148 //-----------------------------------------------------------------------------
00149 template <typename T>
00150 bool ModelForSurgery<T>::CDR ( 
00151         TAPs::BoundingVolume<T> const * const pBVObj    
00152         , TAPs::Vector3<T> * totalForce = NULL          
00153         , T gain = 1.0                                  
00154 )
00155 {
00156     if ( CheckOperationFlag( COLLISION_ON ) == false )  return false;
00157 
00158     TAPs::Vector3<T> force( 0, 0, 0 );
00159     bool result = false;
00160 
00161     std::cout << "ModelForSurgery<T>::CDR with an BV object -- NOT TESTED YET!\n";
00162 
00163     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00164 
00165     while ( pos != m_Parts.end() ) {
00166         // Store the transformation of the part
00167         TransformationSupport<T> trx( (*pos)->GetTransform() );
00168         // Apply the model's transformation on the part
00169         (*pos)->GetTransform().SetMatrixTransform( m_Transform.GetMatrixTransform() * trx.GetMatrixTransform() );
00170         // Collision Detection and Response
00171         if ( TAPs::CD::Fn::CDR_PolygonalMesh_vs_BV<T>( *pos, pBVObj, &force, gain ) ) {
00172             if ( totalForce )   *totalForce += force;
00173             result = true;
00174         }
00175         // Restore the transformation of the part
00176         (*pos)->SetTransform( trx );
00177         ++pos;
00178     }
00179 
00180     return result;
00181 }
00182 //-----------------------------------------------------------------------------
00183 template <typename T>
00184 bool ModelForSurgery<T>::CDR (
00185         TAPs::MultiBoundingVolume<T> const * const pMBVObj  
00186         , TAPs::Vector3<T> * totalForce = NULL              
00187         , T gain = 1.0                                      
00188 )
00189 {
00190     if ( CheckOperationFlag( COLLISION_ON ) == false )  return false;
00191 
00192     TAPs::Vector3<T> force( 0, 0, 0 );
00193     bool result = false;
00194 
00195     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00196 
00197     while ( pos != m_Parts.end() ) {
00198         // Store the transformation of the part
00199         TransformationSupport<T> trx( (*pos)->GetTransform() );
00200         // Apply the model's transformation on the part
00201         (*pos)->GetTransform().SetMatrixTransform( m_Transform.GetMatrixTransform() * trx.GetMatrixTransform() );
00202         // Collision Detection and Response
00203         if ( TAPs::CD::Fn::CDR_PolygonalMesh_vs_MBV<T>( *pos, pMBVObj, &force, gain ) ) {
00204             if ( totalForce )   *totalForce += force;
00205             result = true;
00206         }
00207         // Restore the transformation of the part
00208         (*pos)->SetTransform( trx );
00209         ++pos;
00210     }
00211     *totalForce = (m_Transform.GetMatrixTransform().GetInverse() * Vector4<T>( *totalForce )).GetVector3();
00212     totalForce->SetXYZ( 0, 0, 0 );
00213 
00214     return result;
00215 }
00216 //-----------------------------------------------------------------------------
00217 template <typename T>
00218 bool ModelForSurgery<T>::CDRigid (
00219         TAPs::MultiBoundingVolume<T> const * const pMBVObj  
00220         , TAPs::Vector3<T> * totalForce = NULL              
00221         , T gain = 1.0                                      
00222 )
00223 {
00224     if ( CheckOperationFlag( COLLISION_ON ) == false )  return false;
00225 
00226     TAPs::Vector3<T> force( 0, 0, 0 );
00227     bool result = false;
00228 
00229     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00230 
00231     while ( pos != m_Parts.end() ) {
00232         // Store the transformation of the part
00233         TransformationSupport<T> trx( (*pos)->GetTransform() );
00234         // Apply the model's transformation on the part
00235         (*pos)->GetTransform().SetMatrixTransform( m_Transform.GetMatrixTransform() * trx.GetMatrixTransform() );
00236         // Collision Detection and Response
00237         if ( TAPs::CD::Fn::CDRigid_PolygonalMesh_vs_MBV<T>( *pos, pMBVObj, &force, gain ) ) {
00238             if ( totalForce )   *totalForce += force;
00239             result = true;
00240         }
00241         // Restore the transformation of the part
00242         (*pos)->SetTransform( trx );
00243         ++pos;
00244     }
00245     *totalForce = (m_Transform.GetMatrixTransform().GetInverse() * Vector4<T>( *totalForce )).GetVector3();
00246     totalForce->SetXYZ( 0, 0, 0 );
00247 
00248     return result;
00249 }
00250 //-----------------------------------------------------------------------------
00251 template <typename T>
00252 bool ModelForSurgery<T>::CDR ( 
00253         ModelForSurgery<T> * const pModel,  
00254         T deformRatio = 0.5                 
00255 )
00256 {
00257     std::cout << __FILE__ << " " << __LINE__ << " Fn: CDR(...) -- NOT IMPLEMENTED YET!\n";
00258     return false;
00259 }
00260 //-----------------------------------------------------------------------------
00261 
00262 #if defined(__gl_h_) || defined(__GL_H__)
00263 //-----------------------------------------------------------------------------
00264 template <typename T>
00265 void ModelForSurgery<T>::DisplayGL ( OpenGL::Enum::DrawMode DM )
00266 {
00267     if ( CheckOperationFlag( RENDER_ON ) == false ) return;
00268 
00269     std::vector< OpenGL::OpenGLHETriMeshOneModelMultiParts<T> * >::iterator pos = m_Parts.begin();
00270 
00271     glPushMatrix();
00272     GetTransform().TransformByOpenGLForDrawing();
00273     while ( pos != m_Parts.end() ) {
00274         (*pos)->DisplayGL( DM );
00275         ++pos;
00276     }
00277     glPopMatrix();
00278 }
00279 
00280 #else   // OpenGL
00281 #endif  // OpenGL
00282 //-----------------------------------------------------------------------------
00283 //=============================================================================
00284 END_NAMESPACE_TAPs
00285 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00286 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines