Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
FileRead.cpp
Go to the documentation of this file.
1 /** @example FileRead.cpp
2  * This example demonstrates how to read Triangle mesh output files (.node and .ele formats)
3  * and import them into MOAB. Triangle is a 2D mesh generator that produces .node and .ele files.
4  * This example shows how to parse node coordinates and element connectivity from these files
5  * and create MOAB entities from the external mesh data.
6  */
7 #include <iostream>
8 #include <fstream>
9 #include <vector>
10 #include <string>
11 #include <sstream>
12 
13 #include "moab/Core.hpp"
14 #include "moab/ReadUtilIface.hpp"
15 
16 using namespace std;
17 using namespace moab;
18 
19 int comment( string& line )
20 {
21  // if a line starts with '#' is a comment
22  // eat white space characters
23  size_t found = line.find_first_not_of( " \t" );
24  if( found == string::npos ) return 1; // empty line
25  if( '#' == line[found] ) return 1; // a comment indeed
26  return 0; // a line with some data in it, then
27 }
29 {
30 
31  //
32  // get the read interface from moab
34  MB_CHK_SET_ERR( mb->query_interface( iface ), "Can't get interface" );
35  // Triangle default <name>.node
36  string nodeFileName = fileBase + ".node";
37  ifstream nodeFile( nodeFileName.c_str() );
38  if( !nodeFile.is_open() )
39  {
40  cout << "can't open node file .\n";
42  }
43  cout << "reading nodes from file " << nodeFileName.c_str() << endl;
44 
45  string eleFileName = fileBase + ".ele";
46  ifstream eleFile( eleFileName.c_str() );
47  if( !eleFile.is_open() )
48  {
49  cout << "can't open element file .\n";
51  }
52  cout << "reading elements from file " << eleFileName.c_str() << endl;
53 
54  string line;
55 
56  // ignore comment lines that start with #
57 
58  int num_nodes = 0, num_triangles = 0;
59  while( num_nodes == 0 )
60  {
61  getline( nodeFile, line );
62  if( comment( line ) ) continue;
63  stringstream tks( line );
64  tks >> num_nodes; // ignore the rest of the first line
65  // maybe will read attributes some other time
66  cout << "num nodes:" << num_nodes << endl;
67  }
68 
69  // allocate a block of vertex handles and read xyz’s into them
70  // we know the size of the node arrays, and this call will allocate
71  // needed arrays, coordinate arrays
72  // also, it will return a starting handle for the node sequence
73  vector< double* > arrays;
74  EntityHandle startv;
75  MB_CHK_SET_ERR( iface->get_node_coords( 2, num_nodes, 0, startv, arrays ), "Failed to get node coordinates" );
76  for( int i = 0; i < num_nodes; i++ )
77  {
78  getline( nodeFile, line );
79  if( comment( line ) )
80  {
81  i--; // read one more line
82  continue;
83  }
84  stringstream tokens( line );
85  int nodeId;
86  tokens >> nodeId >> arrays[0][i] >> arrays[1][i];
87  }
88 
89  // now read the element data from a different file
90  // first, find out how many elements are out there
91  // first line with data should have it
92  while( num_triangles == 0 )
93  {
94  getline( eleFile, line );
95  if( comment( line ) ) continue;
96  stringstream tks( line );
97  tks >> num_triangles; // ignore the rest of the line
98  cout << "num triangles:" << num_triangles << endl;
99  }
100 
101  EntityHandle starte;
102  EntityHandle* starth; // the connectivity array that will get populated
103  // with triangle data
104  // allocate block of triangle handles and read connectivity into them
105  MB_CHK_SET_ERR( iface->get_element_connect( num_triangles, 3, MBTRI, 0, starte, starth ),
106  "Failed to get element connectivity" );
107 
108  for( int j = 0; j < num_triangles; j++ )
109  {
110  getline( eleFile, line );
111  if( comment( line ) )
112  {
113  j--; // read one more line
114  continue;
115  }
116  stringstream tokens( line );
117  int eleId;
118  unsigned int node;
119  tokens >> eleId;
120  for( int k = 0; k < 3; k++ )
121  {
122  tokens >> node;
123  // vertex handles start at startv
124  starth[3 * j + k] = startv + node - 1;
125  }
126  }
127 
129  //
130  return MB_SUCCESS;
131 }
132 
133 // .
134 // Read Triangle output files
135 // Assume that the format is <filename>.node and <filename>.ele
136 // see http://www.cs.cmu.edu/~quake/triangle.html for details
137 //
138 int main( int argc, char** argv )
139 {
140  if( 3 != argc )
141  {
142  cout << "Usage: " << argv[0] << " <filename> <outFile> " << endl;
143  cout << " <filename> is the base file name; *.ele and *.node file are read; outFile "
144  "is a file with an extension recognized by MOAB "
145  << endl;
146  return 0;
147  }
148 
149  string filename = argv[1];
150  char* outfile = argv[2];
151 
152  // get MOAB instance and read the file
153  Core* mb = new Core();
154 
155  MB_CHK_SET_ERR( ReadTriangleOutput( mb, filename ), "Failed to read Triangle output" );
156 
157  cout << "Writing output file " << outfile << endl;
158  MB_CHK_SET_ERR( mb->write_file( outfile ), "Failed to write output file" );
159 
160  delete mb;
161 
162  return 0;
163 }