This example demonstrates how to read Triangle mesh output files (.node and .ele formats) and import them into MOAB. Triangle is a 2D mesh generator that produces .node and .ele files. This example shows how to parse node coordinates and element connectivity from these files and create MOAB entities from the external mesh data.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
{
size_t found = line.find_first_not_of( " \t" );
if( found == string::npos ) return 1;
if( '#' == line[found] ) return 1;
return 0;
}
{
string nodeFileName = fileBase + ".node";
ifstream nodeFile( nodeFileName.c_str() );
if( !nodeFile.is_open() )
{
cout << "can't open node file .\n";
}
cout << "reading nodes from file " << nodeFileName.c_str() << endl;
string eleFileName = fileBase + ".ele";
ifstream eleFile( eleFileName.c_str() );
if( !eleFile.is_open() )
{
cout << "can't open element file .\n";
}
cout << "reading elements from file " << eleFileName.c_str() << endl;
string line;
int num_nodes = 0, num_triangles = 0;
while( num_nodes == 0 )
{
getline( nodeFile, line );
stringstream tks( line );
tks >> num_nodes;
cout << "num nodes:" << num_nodes << endl;
}
vector< double* > arrays;
MB_CHK_SET_ERR(
iface->get_node_coords( 2, num_nodes, 0, startv, arrays ),
"Failed to get node coordinates" );
for( int i = 0; i < num_nodes; i++ )
{
getline( nodeFile, line );
{
i--;
continue;
}
stringstream tokens( line );
int nodeId;
tokens >> nodeId >> arrays[0][i] >> arrays[1][i];
}
while( num_triangles == 0 )
{
getline( eleFile, line );
stringstream tks( line );
tks >> num_triangles;
cout << "num triangles:" << num_triangles << endl;
}
"Failed to get element connectivity" );
for( int j = 0; j < num_triangles; j++ )
{
getline( eleFile, line );
{
j--;
continue;
}
stringstream tokens( line );
int eleId;
unsigned int node;
tokens >> eleId;
for( int k = 0; k < 3; k++ )
{
tokens >> node;
starth[3 * j + k] = startv + node - 1;
}
}
}
int main(
int argc,
char** argv )
{
if( 3 != argc )
{
cout << "Usage: " << argv[0] << " <filename> <outFile> " << endl;
cout << " <filename> is the base file name; *.ele and *.node file are read; outFile "
"is a file with an extension recognized by MOAB "
<< endl;
return 0;
}
string filename = argv[1];
char* outfile = argv[2];
cout << "Writing output file " << outfile << endl;
return 0;
}