Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
FileRead.cpp File Reference
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include "moab/Core.hpp"
#include "moab/ReadUtilIface.hpp"
+ Include dependency graph for FileRead.cpp:

Go to the source code of this file.

Functions

int comment (string &line)
 
ErrorCode ReadTriangleOutput (Interface *mb, string fileBase)
 
int main (int argc, char **argv)
 

Function Documentation

◆ comment()

int comment ( string &  line)

Definition at line 13 of file FileRead.cpp.

14 {
15  // if a line starts with '#' is a comment
16  // eat white space characters
17  size_t found = line.find_first_not_of( " \t" );
18  if( found == string::npos ) return 1; // empty line
19  if( '#' == line[found] ) return 1; // a comment indeed
20  return 0; // a line with some data in it, then
21 }

Referenced by ReadTriangleOutput().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 137 of file FileRead.cpp.

138 {
139  if( 3 != argc )
140  {
141  cout << "Usage: " << argv[0] << " <filename> <outFile> " << endl;
142  cout << " <filename> is the base file name; *.ele and *.node file are read; outFile "
143  "is a file with an extension recognized by MOAB "
144  << endl;
145  return 0;
146  }
147 
148  string filename = argv[1];
149  char* outfile = argv[2];
150 
151  // get MOAB instance and read the file
152  Core* mb = new Core();
153 
154  ErrorCode rval = ReadTriangleOutput( mb, filename );
155 
156  if( rval == MB_SUCCESS )
157  {
158  cout << "Writing output file " << outfile << endl;
159  mb->write_file( outfile );
160  }
161 
162  delete mb;
163 
164  return 0;
165 }

References ErrorCode, mb, MB_SUCCESS, ReadTriangleOutput(), and moab::Core::write_file().

◆ ReadTriangleOutput()

ErrorCode ReadTriangleOutput ( Interface mb,
string  fileBase 
)

Definition at line 22 of file FileRead.cpp.

23 {
24 
25  //
26  // get the read interface from moab
28  ErrorCode rval = mb->query_interface( iface );
29  //
30  if( MB_SUCCESS != rval )
31  {
32  cout << "Can't get interface.\n";
33  return MB_FAILURE;
34  }
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  rval = iface->get_node_coords( 2, num_nodes, 0, startv, arrays );
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  rval = iface->get_element_connect( num_triangles, 3, MBTRI, 0, starte, starth );
106 
107  for( int j = 0; j < num_triangles; j++ )
108  {
109  getline( eleFile, line );
110  if( comment( line ) )
111  {
112  j--; // read one more line
113  continue;
114  }
115  stringstream tokens( line );
116  int eleId;
117  unsigned int node;
118  tokens >> eleId;
119  for( int k = 0; k < 3; k++ )
120  {
121  tokens >> node;
122  // vertex handles start at startv
123  starth[3 * j + k] = startv + node - 1;
124  }
125  }
126 
128  //
129  return MB_SUCCESS;
130 }

References comment(), ErrorCode, iface, mb, MB_FILE_DOES_NOT_EXIST, MB_SUCCESS, MBTRI, moab::Interface::query_interface(), and moab::Interface::release_interface().

Referenced by main().