![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsReadTAPsMBV.cpp 00003 ******************************************************************************/ 00008 /****************************************************************************** 00009 SUKITTI PUNAK (08/25/2006) 00010 UPDATE (09/03/2010) 00011 ******************************************************************************/ 00012 #include "TAPsReadTAPsMBV.hpp" 00013 // Using Inclusion Model (i.e. definitions are included in declarations) 00014 // (this name.cpp is included in name.hpp) 00015 // Each friend is defined directly inside its declaration. 00016 00017 BEGIN_NAMESPACE_TAPs 00018 //============================================================================= 00019 template <typename T> int ReadTAPsMBV<T>::iCounter = 0; 00020 template <typename T> int ReadTAPsMBV<T>::iNumBoundingVolume = 0; 00021 template <typename T> FILE * ReadTAPsMBV<T>::fileIn = NULL; 00022 //============================================================================= 00023 //----------------------------------------------------------------------------- 00024 // Read an input .MBV file 00025 template <typename T> 00026 bool ReadTAPsMBV<T>::ReadFile ( 00027 const char * fileName, MultiBoundingVolume<T> * const prMBV ) 00028 { 00029 //--------------------------------------------------------------- 00030 // If MultiBoundingVolume is not created yet! 00031 if ( !prMBV ) { 00032 std::cout << "The Multi Bounding Volume Object must be created first!" 00033 << std::endl; 00034 return false; 00035 } 00036 //--------------------------------------------------------------- 00037 // Find the format of the file 00038 char *name = new char[ strlen( fileName )+1 ]; 00039 char *format = new char[64]; 00040 strcpy( name, fileName ); 00041 char *token = strtok( name, "." ); 00042 do { 00043 strcpy( format, token ); 00044 token = strtok( '\0', "." ); 00045 } while ( token ); 00046 //--------------------------------------------------------------- 00047 // If not a .MBV extension 00048 if ( strcmp( format, "MBV" ) != 0 ) { 00049 std::cout << "The input file extension must be .MBV!" << std::endl; 00050 return false; 00051 } 00052 //=============================================================== 00053 // Process the file readin. 00054 //--------------------------------------------------------------- 00055 // Open the input file 00056 fileIn = fopen( fileName, "r" ); 00057 if ( !fileIn ) { 00058 std::perror( fileName ); 00059 return false; 00060 } 00061 //--------------------------------------------------------------- 00062 //std::cout << "START READING " << fileName << "\n"; 00063 char line[256]; 00064 char node[128]; 00065 //--------------------------------------------------------------- 00066 iCounter = 0; 00067 while ( !feof(fileIn) ) { 00068 fgets( line, 256, fileIn ); // read a line 00069 sscanf( line, "%s", node ); 00070 //--------------------------------------- 00071 // Skip A Commen Line 00072 if ( node[0] == '#' ) { 00073 continue; 00074 } 00075 //--------------------------------------- 00076 // Skip An Empty Line 00077 if ( strlen( line ) <= 2 ) { 00078 continue; 00079 } 00080 //--------------------------------------- 00081 if ( !strncmp( node, "BOUNDING_VOLUME", 00082 strlen("BOUNDING_VOLUME") ) ) { 00083 //std::cout << "NODE: " << node << "\n"; 00084 ProcessNodeBOUNDING_VOLUME( line, prMBV ); 00085 ++iCounter; 00086 } 00087 else if ( !strncmp( node, "Number_Of_Bounding_Volumes", 00088 strlen("Number_Of_Bounding_Volumes") ) ) { 00089 int size; 00090 sscanf( line, "%s %d", node, &size ); 00091 iNumBoundingVolume = size; 00092 //std::cout << "iNumBoundingVolume: " << iNumBoundingVolume << std::endl; 00093 } 00094 // if ( iNumBoundingVolume == iCounter ) break; 00095 } 00096 //--------------------------------------------------------------- 00097 fclose(fileIn); 00098 //std::cout << "END READING " << fileName << "\n"; 00099 return true; 00100 } 00101 //----------------------------------------------------------------------------- 00102 //============================================================================= 00103 //----------------------------------------------------------------------------- 00104 // Process BOUNDING_VOLUME 00105 template <typename T> 00106 void ReadTAPsMBV<T>::ProcessNodeBOUNDING_VOLUME ( 00107 char * line, MultiBoundingVolume<T> * const prMBV ) 00108 { 00109 //--------------------------------------------------------------- 00110 // For Handling Transformations 00111 std::vector< std::string > vTransformName; 00112 std::vector< T > vTransformValue; 00113 //--------------------------------------------------------------- 00114 // Example: 00115 // BOUNDING_VOLUME { 00116 // ID 0 00117 // NAME "Shaft" 00118 // BV_TYPE BOUNDING_CYLINDER 00119 // RADIUS 5.25 00120 // HEIGHT 300 00121 // CENTER 0 0 0 00122 // } 00123 //--------------------------------------------------------------- 00124 // Temp Data 00125 int tempID = iCounter; 00126 char tempName[128]; 00127 strcpy( tempName, "Unnamed" ); 00128 char tempString[128]; 00129 Enum::CD tempBoundingType = Enum::BOUNDING_SPHERE; 00130 float tempRadius = 0.5; 00131 float tempHeight = 1; 00132 Vector3<T> tempCenter = Vector3<T>(0,0,0); 00133 bool tempStatusApplyTransformation = false; 00134 //--------------------------------------------------------------- 00135 char node[128]; 00136 do { 00137 fgets( line, 256, fileIn ); 00138 sscanf( line, "%s", node ); 00139 //std::cout << "IN: " << line; 00140 //--------------------------------------- 00141 if ( node[0] == '#' ) continue; 00142 //--------------------------------------- 00143 else if ( !strncmp( node, "ID", strlen("ID") ) ) { 00144 sscanf( line, "%s %d", node, &tempID ); 00145 } 00146 else if ( !strncmp( node, "NAME", strlen("NAME") ) ) { 00147 //char * strtok ( char * str, const char * delimiters ); 00148 char * token; 00149 token = strtok (line, "\""); 00150 token = strtok (NULL, "\""); 00151 strcpy( tempName, token ); 00152 //sscanf( line, "%s %s", node, tempName ); 00153 } 00154 else if ( !strncmp( node, "BV_TYPE", strlen("BV_TYPE") ) ) { 00155 sscanf( line, "%s %s", node, tempString ); 00156 //if ( !strncmp( tempString, "BOUNDING_SPHERE", strlen("BOUNDING_SPHERE") ) ) { 00157 // tempBoundingType = Enum::BOUNDING_SPHERE; 00158 //} 00159 if ( !strncmp( tempString, "BOUNDING_BOX", strlen("BOUNDING_BOX") ) ) { 00160 tempBoundingType = Enum::BOUNDING_BOX; 00161 } 00162 else if ( !strncmp( tempString, "BOUNDING_CYLINDER", strlen("BOUNDING_CYLINDER") ) ) { 00163 tempBoundingType = Enum::BOUNDING_CYLINDER; 00164 } 00165 else if ( !strncmp( tempString, "BOUNDING_CONE", strlen("BOUNDING_CONE") ) ) { 00166 tempBoundingType = Enum::BOUNDING_CONE; 00167 } 00168 else if ( !strncmp( tempString, "BOUNDING_HALF_SPHERE", strlen("BOUNDING_HALF_SPHERE") ) ) { 00169 tempBoundingType = Enum::BOUNDING_HALF_SPHERE; 00170 } 00171 else if ( !strncmp( tempString, "BOUNDING_ELLIPSOID", strlen("BOUNDING_ELLIPSOID") ) ) { 00172 tempBoundingType = Enum::BOUNDING_ELLIPSOID; 00173 } 00174 else if ( !strncmp( tempString, "BOUNDING_HALF_ELLIPSOID", strlen("BOUNDING_HALF_ELLIPSOID") ) ) { 00175 tempBoundingType = Enum::BOUNDING_HALF_ELLIPSOID; 00176 } 00177 else if ( !strncmp( tempString, "BOUNDING_CAPSULE", strlen("BOUNDING_CAPSULE") ) ) { 00178 tempBoundingType = Enum::BOUNDING_CAPSULE; 00179 } 00180 else if ( !strncmp( tempString, "BOUNDING_POLYTOPE", strlen("BOUNDING_POLYTOPE") ) ) { 00181 tempBoundingType = Enum::BOUNDING_POLYTOPE; 00182 } 00183 } 00184 else if ( !strncmp( node, "RADIUS", strlen("RADIUS") ) ) { 00185 sscanf( line, "%s %g", node, &tempRadius ); 00186 } 00187 else if ( !strncmp( node, "HEIGHT", strlen("HEIGHT") ) ) { 00188 sscanf( line, "%s %g", node, &tempHeight ); 00189 } 00190 else if ( !strncmp( node, "CENTER", strlen("CENTER") ) ) { 00191 float x, y, z; 00192 sscanf( line, "%s %g %g %g", node, &x, &y, &z ); 00193 tempCenter.SetXYZ( x, y, z ); 00194 } 00195 else if ( !strncmp( node, "TRANSLATION", strlen("TRANSLATION") ) ) { 00196 float x, y, z; 00197 sscanf( line, "%s %g %g %g", node, &x, &y, &z ); 00198 vTransformName.push_back( "TRANSLATION" ); 00199 vTransformValue.push_back( x ); 00200 vTransformValue.push_back( y ); 00201 vTransformValue.push_back( z ); 00202 } 00203 else if ( !strncmp( node, "ROTATION_AXIS_AND_ANGLE", strlen("ROTATION_AXIS_AND_ANGLE") ) ) { 00204 float x, y, z, a; 00205 sscanf( line, "%s %g %g %g %g", node, &x, &y, &z, &a ); 00206 vTransformName.push_back( "ROTATION_AXIS_AND_ANGLE" ); 00207 vTransformValue.push_back( x ); 00208 vTransformValue.push_back( y ); 00209 vTransformValue.push_back( z ); 00210 vTransformValue.push_back( a ); 00211 } 00212 else if ( !strncmp( node, "SCALE", strlen("SCALE") ) ) { 00213 float s; 00214 //float sx, sy, sz; 00215 sscanf( line, "%s %g", node, &s ); 00216 //sscanf( line, "%s %g %g %g", node, &sx, &sy, &sz ); 00217 vTransformName.push_back( "SCALE" ); 00218 vTransformValue.push_back( s ); 00219 //vTransformValue.push_back( sx ); 00220 //vTransformValue.push_back( sy ); 00221 //vTransformValue.push_back( sz ); 00222 } 00223 } while ( node[0] != '}' && !feof(fileIn) ); 00224 //--------------------------------------------------------------- 00225 // Create the bounding volume, set its properties and put it 00226 // into the MultiBoundingVolume 00227 BoundingVolume<T> * tempBoundingVolumePtr = NULL; 00228 switch ( tempBoundingType ) { 00229 case Enum::BOUNDING_SPHERE: 00230 tempBoundingVolumePtr = new BoundingSphere<T>( tempID ); 00231 tempBoundingVolumePtr->SetRadius( tempRadius ); 00232 break; 00233 case Enum::BOUNDING_BOX: 00234 //tempBoundingVolumePtr = new BoundingBox<T>( tempID ); 00235 break; 00236 case Enum::BOUNDING_CYLINDER: 00237 tempBoundingVolumePtr = new BoundingCylinder<T>( tempID ); 00238 tempBoundingVolumePtr->SetRadius( tempRadius ); 00239 tempBoundingVolumePtr->SetHeight( tempHeight ); 00240 break; 00241 case Enum::BOUNDING_HALF_SPHERE: 00242 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00243 break; 00244 case Enum::BOUNDING_CONE: 00245 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00246 break; 00247 case Enum::BOUNDING_ELLIPSOID: 00248 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00249 break; 00250 case Enum::BOUNDING_HALF_ELLIPSOID: 00251 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00252 break; 00253 case Enum::BOUNDING_CAPSULE: 00254 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00255 break; 00256 case Enum::BOUNDING_POLYTOPE: 00257 //tempBoundingVolumePtr = new Bounding<T>( tempID ); 00258 break; 00259 default: 00260 std::cout << "ERROR: Unknown Bounding Type!" << std::endl; 00261 assert( false ); 00262 break; 00263 } 00264 assert( tempBoundingVolumePtr ); 00265 tempBoundingVolumePtr->SetName( tempName ); 00266 tempBoundingVolumePtr->SetCenter( tempCenter ); 00267 TransformationSupport<T> * transform = &(tempBoundingVolumePtr->GetTransform()); 00268 //--------------------------------------------------------------- 00269 // Apply Transformations 00270 int nextPos = 0; 00271 for ( int i = 0; i < static_cast<int>( vTransformName.size() ); ++i ) { 00272 if ( vTransformName[i].compare( "TRANSLATION" ) == 0 ) { 00273 transform->ApplyTranslation( vTransformValue[nextPos], vTransformValue[nextPos+1], vTransformValue[nextPos+2] ); 00274 nextPos += 3; 00275 } 00276 else if ( vTransformName[i].compare( "ROTATION_AXIS_AND_ANGLE" ) == 0 ) { 00277 transform->ApplyRotationAxisAndAngle( 00278 vTransformValue[nextPos], vTransformValue[nextPos+1], vTransformValue[nextPos+2], vTransformValue[nextPos+3] ); 00279 nextPos += 4; 00280 } 00281 else if ( vTransformName[i].compare( "SCALE" ) == 0 ) { 00282 transform->ApplyUniformScale( vTransformValue[nextPos] ); 00283 ++nextPos; 00284 //transform->ApplyScale( vTransformValue[nextPos], vTransformValue[nextPos+1], vTransformValue[nextPos+2] ); 00285 nextPos += 3; 00286 } 00287 } 00288 //--------------------------------------------------------------- 00289 prMBV->GetBoundingVolumeList().push_back( tempBoundingVolumePtr ); 00290 //--------------------------------------------------------------- 00291 //std::cout << "End of Read BOUNDING_VOLUME Node" << std::endl; 00292 } 00293 //----------------------------------------------------------------------------- 00294 //============================================================================= 00295 END_NAMESPACE_TAPs 00296 //----------------------------------------------------------------------------- 00297 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00298 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8