TAPs 0.7.7.3
TAPsArticulatedBodySetup.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsArticulatedBodySetup.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (03/26/2010)
00009 UPDATE          (05/01/2010)
00010 ******************************************************************************/
00011 #include "TAPsArticulatedBodySetup.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> TextFile ArticulatedBodySetup<T>::g_TextFile;
00019 template <typename T> ModelArticulatedBody<T> * ArticulatedBodySetup<T>::g_pArtBody;
00020 //=============================================================================
00021 //-----------------------------------------------------------------------------
00022 template <typename T>
00023 ModelArticulatedBody<T> * ArticulatedBodySetup<T>::FromFile ( 
00024     std::string setupFile   
00025 ) throw(...)
00026 {
00027     // Try to read the input file
00028     try {
00029         ReadFile( setupFile );
00030     }
00031     catch ( Exception & e ) {
00032         throw e;
00033     }
00034 
00035     // Try to setup objects from the read file
00036     try {
00037         g_pArtBody = new ModelArticulatedBody<T>();
00038         for ( int i = 0; i < g_TextFile.GetNumOfLines(); ++i ) {
00039             //std::cout << "Line#" << i+1 << ":\t" << g_TextFile.GetLine( i ) << "\n";
00040             Interpret( StringManipulator::StripOutComment( g_TextFile.GetLine( i ) ), i );
00041             //std::cout << "[after] Line#" << i+1 << ":\t" << g_TextFile.GetLine( i ) << "\n";
00042         }
00043     }
00044     catch ( Exception & e ) {
00045         throw e;
00046     }
00047     return g_pArtBody;
00048 }
00049 
00050 //-----------------------------------------------------------------------------
00051 template <typename T>
00052 void ArticulatedBodySetup<T>::ReadFile ( std::string const & setupFile ) throw(...)
00053 {
00054     // Try to read the input file
00055     if ( !g_TextFile.Read( setupFile ) ) {
00056         throw Exception( Exception::FILE_CANNOT_BE_OPENED, __FILE__, __LINE__ );
00057     }
00058 }
00059 
00060 //-----------------------------------------------------------------------------
00061 template <typename T>
00062 void ArticulatedBodySetup<T>::Interpret ( std::string const & line, int & lineNo ) throw(...)
00063 {
00064     // Keywords
00065     static std::string NODE = "Node";
00066 
00067     // Temp data
00068     int n;
00069     std::string info;
00070 
00071     // Node
00072     n = line.find( NODE );
00073     if ( n != -1 ) {
00074         info = line.substr( n+NODE.length() );
00075         try {
00076             Node( info, lineNo );
00077         }
00078         catch ( Exception & e ) {
00079             throw e;
00080         }
00081     }
00082 }
00083 
00084 //-----------------------------------------------------------------------------
00085 template <typename T>
00086 void ArticulatedBodySetup<T>::Node ( std::string info, int & lineNo ) throw(...)
00087 {
00088     // Keywords
00089     static std::string MESH_FILE = "MeshFile";
00090     static std::string POS = "Position";
00091     static std::string ROT = "Rotation";
00092     static std::string SCALE = "Scale";
00093 
00094     // Temp data
00095     int n;
00096     std::string nodedata;
00097 
00098     // Find the open brace bracket '{'
00099     n = info.find( '{' );
00100     while ( n == -1 ) {
00101         if ( lineNo >= g_TextFile.GetNumOfLines()-1 ) {
00102             throw Exception( Exception::BAD_FORMAT, __FILE__, __LINE__, "doesn't have '{' for Node" );
00103         }
00104         info = StringManipulator::StripOutComment( g_TextFile.GetLine( ++lineNo ) );
00105         n = info.find( '{' );
00106     }
00107     if ( n+1 < static_cast<int>(info.length()) ) {
00108         info = info.substr( n+1 );
00109     }
00110     else {
00111         info = "";
00112     }
00113 
00114     // Find the end brace bracket '}'
00115     n = info.find( '}' );
00116     while ( n == -1 ) {
00117         nodedata += " ";
00118         nodedata += info;
00119         if ( lineNo >= g_TextFile.GetNumOfLines()-1 ) {
00120             throw Exception( Exception::BAD_FORMAT, __FILE__, __LINE__, "doesn't have '}' for Node" );
00121         }
00122         info = StringManipulator::StripOutComment( g_TextFile.GetLine( ++lineNo ) );
00123         n = info.find( '}' );
00124     }
00125     info = info.substr( 0, n );
00126     nodedata += " ";
00127     nodedata += info;
00128 
00129     // Temp data
00130     std::string key;
00131     std::string meshFile( "" );
00132     Vector3<T> pos;
00133     Vector4<T> rot;
00134     Vector3<T> scale;
00135 
00136     // Process nodedata
00137     while ( nodedata.length() > 0 ) {
00138 
00139         StringManipulator::BreakString( nodedata, key, nodedata, ":;, \t\n" );
00140 
00141         //std::cout << "key: " << key << std::endl;
00142         //std::cout << "nodedata: " << nodedata << "\n";
00143 
00144         if ( key.compare( MESH_FILE ) == 0 ) {
00145 
00146             TAPs::RigidBodyDynamics<Real> R;
00147             R.SetModelFromFile( "../Data/cone1_8.obj", TAPs::Enum::MODEL_TYPE_HALFEDGE );
00148 
00149             // Get the filename
00150             int b = nodedata.find( '"' );
00151             int e = nodedata.find( '"', b+1 );
00152             key = nodedata.substr( b+1, e-b-1 );
00153             nodedata = nodedata.substr( e+1 );
00154             meshFile = key;
00155             //std::cout << "fileName: " << meshFile << "\n";
00156         }
00157         if ( key.compare( POS ) == 0 ) {
00158             StringManipulator::BreakString( nodedata, key, nodedata, ":;, \t\n" );
00159             StringManipulator::ConvertStrToVar( key, pos[0] );
00160             StringManipulator::BreakString( nodedata, key, nodedata, ":;, \t\n" );
00161             StringManipulator::ConvertStrToVar( key, pos[1] );
00162             StringManipulator::BreakString( nodedata, key, nodedata, ":;, \t\n" );
00163             StringManipulator::ConvertStrToVar( key, pos[2] );
00164             std::cout << "pos: " << pos << "\n";
00165         }
00166         //...
00167     }
00168 
00169     // Create the rigid body
00170     if ( meshFile != "" ) {
00171         g_pArtBody->m_ListRigidBodies.push_back( RigidBodyDynamics<T>() );
00172         if ( !g_pArtBody->m_ListRigidBodies.back().SetModelFromFile( meshFile, TAPs::Enum::MODEL_TYPE_POLYGONAL ) ) {
00173             delete g_pArtBody;
00174             g_pArtBody = NULL;
00175             throw Exception( Exception::CANNOT_CREATE_AN_OBJECT, __FILE__, __LINE__ );
00176         }
00177         // Set centroid
00178         g_pArtBody->m_ListRigidBodies.back().SetOriginPoint( pos );
00179         // Set rotation
00180         //...
00181         // set scale
00182         //...
00183     }
00184 }
00185 
00186 //=============================================================================
00187 END_NAMESPACE_TAPs
00188 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00189 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines