MOAB: Mesh Oriented datABase  (version 5.4.1)
FileRead.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004 #include <string>
00005 #include <sstream>
00006 
00007 #include "moab/Core.hpp"
00008 #include "moab/ReadUtilIface.hpp"
00009 
00010 using namespace std;
00011 using namespace moab;
00012 
00013 int comment( string& line )
00014 {
00015     // if a line starts with '#' is a comment
00016     // eat white space characters
00017     size_t found = line.find_first_not_of( " \t" );
00018     if( found == string::npos ) return 1;  // empty line
00019     if( '#' == line[found] ) return 1;     // a comment indeed
00020     return 0;                              // a line with some data in it, then
00021 }
00022 ErrorCode ReadTriangleOutput( Interface* mb, string fileBase )
00023 {
00024 
00025     //
00026     // get the read interface from moab
00027     ReadUtilIface* iface;
00028     ErrorCode rval = mb->query_interface( iface );
00029     //
00030     if( MB_SUCCESS != rval )
00031     {
00032         cout << "Can't get interface.\n";
00033         return MB_FAILURE;
00034     }
00035     // Triangle default <name>.node
00036     string nodeFileName = fileBase + ".node";
00037     ifstream nodeFile( nodeFileName.c_str() );
00038     if( !nodeFile.is_open() )
00039     {
00040         cout << "can't open node file .\n";
00041         return MB_FILE_DOES_NOT_EXIST;
00042     }
00043     cout << "reading nodes from file " << nodeFileName.c_str() << endl;
00044 
00045     string eleFileName = fileBase + ".ele";
00046     ifstream eleFile( eleFileName.c_str() );
00047     if( !eleFile.is_open() )
00048     {
00049         cout << "can't open element file .\n";
00050         return MB_FILE_DOES_NOT_EXIST;
00051     }
00052     cout << "reading elements from file " << eleFileName.c_str() << endl;
00053 
00054     string line;
00055 
00056     // ignore comment lines that start with #
00057 
00058     int num_nodes = 0, num_triangles = 0;
00059     while( num_nodes == 0 )
00060     {
00061         getline( nodeFile, line );
00062         if( comment( line ) ) continue;
00063         stringstream tks( line );
00064         tks >> num_nodes;  // ignore the rest of the first line
00065                            // maybe will read attributes some other time
00066         cout << "num nodes:" << num_nodes << endl;
00067     }
00068 
00069     //  allocate a block of vertex handles and read xyz’s into them
00070     //  we know the size of the node arrays, and this call will allocate
00071     //   needed arrays, coordinate arrays
00072     //   also, it will return a starting handle for the node sequence
00073     vector< double* > arrays;
00074     EntityHandle startv;
00075     rval = iface->get_node_coords( 2, num_nodes, 0, startv, arrays );
00076     for( int i = 0; i < num_nodes; i++ )
00077     {
00078         getline( nodeFile, line );
00079         if( comment( line ) )
00080         {
00081             i--;  // read one more line
00082             continue;
00083         }
00084         stringstream tokens( line );
00085         int nodeId;
00086         tokens >> nodeId >> arrays[0][i] >> arrays[1][i];
00087     }
00088 
00089     // now read the element data from a different file
00090     // first, find out how many elements are out there
00091     // first line with data should have it
00092     while( num_triangles == 0 )
00093     {
00094         getline( eleFile, line );
00095         if( comment( line ) ) continue;
00096         stringstream tks( line );
00097         tks >> num_triangles;  // ignore the rest of the line
00098         cout << "num triangles:" << num_triangles << endl;
00099     }
00100 
00101     EntityHandle starte;
00102     EntityHandle* starth;  // the connectivity array that will get populated
00103                            // with triangle data
00104     // allocate block of triangle handles and read connectivity into them
00105     rval = iface->get_element_connect( num_triangles, 3, MBTRI, 0, starte, starth );
00106 
00107     for( int j = 0; j < num_triangles; j++ )
00108     {
00109         getline( eleFile, line );
00110         if( comment( line ) )
00111         {
00112             j--;  // read one more line
00113             continue;
00114         }
00115         stringstream tokens( line );
00116         int eleId;
00117         unsigned int node;
00118         tokens >> eleId;
00119         for( int k = 0; k < 3; k++ )
00120         {
00121             tokens >> node;
00122             // vertex handles start at startv
00123             starth[3 * j + k] = startv + node - 1;
00124         }
00125     }
00126 
00127     mb->release_interface( iface );
00128     //
00129     return MB_SUCCESS;
00130 }
00131 
00132 // .
00133 //  Read Triangle output files
00134 //  Assume that the format is <filename>.node and <filename>.ele
00135 //   see  http://www.cs.cmu.edu/~quake/triangle.html for details
00136 //
00137 int main( int argc, char** argv )
00138 {
00139     if( 3 != argc )
00140     {
00141         cout << "Usage: " << argv[0] << " <filename>  <outFile> " << endl;
00142         cout << "       <filename>  is the base file name; *.ele and *.node file are read; outFile "
00143                 "is a file with an extension recognized by MOAB "
00144              << endl;
00145         return 0;
00146     }
00147 
00148     string filename = argv[1];
00149     char* outfile   = argv[2];
00150 
00151     // get MOAB instance and read the file
00152     Core* mb = new Core();
00153 
00154     ErrorCode rval = ReadTriangleOutput( mb, filename );
00155 
00156     if( rval == MB_SUCCESS )
00157     {
00158         cout << "Writing output file " << outfile << endl;
00159         mb->write_file( outfile );
00160     }
00161 
00162     delete mb;
00163 
00164     return 0;
00165 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines