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