C:/cmcintos/defOrgs/examples/DefOrgViewer/Source/BYUToMeta.cxx

00001 #include "BYUToMeta.h"
00002 
00003 // Convert BYU surface files to .meta files. Assumes 1 object per file and a 3D triangular mesh.
00004 int BYUToMeta( std::string BYUFileName, std::string metaFileName )
00005 {
00006         int SUCCESS = 1;
00007         int FAILURE = -1;
00008 
00009         std::ifstream inFile( BYUFileName.c_str() );
00010         if( !inFile.good() )
00011         { 
00012                 std::cerr << "Error opening input file '" << BYUFileName << "'." << std::endl;
00013                 return FAILURE; 
00014         }
00015 
00016         std::ofstream outFile( metaFileName.c_str() );
00017         //std::ofstream outFile( "C:\\Documents and Settings\\arova\\Desktop\\defOrgs_working_copy\\examples\\DefOrgViewer\\Build\\data\\OFTest" );
00018         if( !outFile.good() )
00019         { 
00020                 std::cerr << "Error creating output file '" << metaFileName << "'." << std::endl;
00021                 return FAILURE; 
00022         }
00023 
00024         // BYU format first line is PART_NUM, VERTEX_NUM, POLY_NUM
00025         int partNum;
00026         inFile >> partNum;
00027         // assume PART_NUM will always be 1:
00028         if( partNum != 1 )
00029         {
00030                 std::cerr << "Format error, PART_NUM (the first number in the file) is " << partNum 
00031                         << ". This function expects PART_NUM to be 1." << std::endl;
00032                 return FAILURE;
00033         }
00034         
00035         int nVertices, nTriangles; 
00036         inFile >> nVertices >> nTriangles;
00037 
00038         // write meta header:
00039         outFile << "ObjectType = Scene" << std::endl
00040                 << "NDims = 3" << std::endl
00041                 << "NObjects = 1" << std::endl
00042                 << "ObjectType = Mesh" << std::endl
00043                 << "NDims = 3" << std::endl
00044                 << "ID = 0" << std::endl
00045                 << "TransformMatrix = 1 0 0 0 1 0 0 0 1" << std::endl
00046                 << "Offset = 0 0 0" << std::endl
00047                 << "CenterOfRotation = 0 0 0" << std::endl
00048                 << "ElementSpacing = 1 1 1" << std::endl
00049                 << "PointType = MET_FLOAT" << std::endl
00050                 << "PointDataType = MET_FLOAT" << std::endl
00051                 << "CellDataType = MET_FLOAT" << std::endl
00052                 << "NCellTypes = 1" << std::endl
00053                 << "PointDim = ID x y ..." << std::endl
00054                 << "NPoints = " << nVertices << std::endl
00055                 << "Points = " << std::endl;
00056 
00057         // read/write vertex data:
00058         double vertex;
00059         for( int i=0; i<nVertices; i++ )
00060         {
00061                 outFile << i << " "; // meta file wants "line numbers" before each set of points
00062                 for( unsigned int j=0; j<3; j++ )
00063                 {
00064                         inFile >> vertex;
00065                         outFile << vertex <<  " ";
00066                 }
00067                 outFile << std::endl;
00068         }
00069 
00070         // write more info required by meta format:
00071         outFile << "CellType = TRI" << std::endl
00072                 << "NCells = " << nTriangles << std::endl
00073                 << "Cells = " << std::endl;
00074 
00075         // read and write corrected triangle data:
00076         double tIndex;
00077         //for( int i=0; i<nTriangles; i++ )
00078         int i = -1;
00079         while( inFile.peek() != EOF ) // TODO: change back to 'for' when Matlab BYU file code is fixed to output proper numbers
00080         {
00081                 i++; // TODO: take this out when changing back to a for loop
00082                 outFile << i << " "; // meta file wants "line numbers" before each set of indices
00083                 for( unsigned int j=0; j<2; j++ )
00084                 {
00085                         inFile >> tIndex;
00086                         tIndex -= 1;
00087                         outFile << tIndex << " ";
00088                 }
00089                 inFile >> tIndex;
00090                 tIndex = abs( tIndex );
00091                 tIndex -= 1;
00092                 outFile << tIndex << std::endl;
00093         }
00094 
00095         // clean up:
00096         inFile.close();
00097         outFile.close();
00098 
00099         return SUCCESS;
00100 }

Generated on Wed Jul 19 13:05:14 2006 for IDO by  doxygen 1.4.7