00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cassert>
00019
00020 #include "ioerror.h"
00021 #include "netgenreader.h"
00022
00023 using namespace std;
00024
00025 NetgenReader::~NetgenReader() {
00026 }
00027
00028 NetgenReader::NetgenReader(istream* istr, MeshBuilder* builder)
00029 : _istr(istr), _builder(builder) {}
00030
00031 void NetgenReader::read() {
00032 const string file_name = "<input stream>";
00033 istream& istr = *_istr;
00034
00035 int line = 0;
00036
00037
00038 int nof_node;
00039 istr >> nof_node;
00040 line ++;
00041 if (istr.fail())
00042 throw FileIOError(file_name, line, "error reading number of nodes");
00043 _builder->init_coord(nof_node);
00044
00045
00046 double x, y, z;
00047 for (int i = 0; i < nof_node; i ++) {
00048 istr >> x >> y >> z;
00049 line ++;
00050 if (istr.fail())
00051 throw FileIOError(file_name, line, "error reading node coordinates");
00052 _builder->set_coord(i, x, y, z);
00053 }
00054 _builder->finalize_coord();
00055
00056
00057 int nof_tet;
00058 istr >> nof_tet;
00059 line ++;
00060 if (istr.fail())
00061 throw FileIOError(file_name, line, "error reading number of elements");
00062 _builder->init_tet(nof_tet);
00063
00064
00065 int node_ids[4], node_id, mat;
00066 for (int t = 0; t < nof_tet; t ++) {
00067 istr >> mat;
00068 for (int i = 0; i < 4; i ++) {
00069 istr >> node_id;
00070 node_id --;
00071 if (node_id < 0 || node_id >= nof_node)
00072 throw FileIOError(file_name, line, "node number out of range");
00073 node_ids[i] = node_id;
00074 }
00075 line ++;
00076 if (istr.fail())
00077 throw FileIOError(file_name, line, "error reading node numbers");
00078 _builder->set_tet(t, node_ids[0], node_ids[1], node_ids[2], node_ids[3], mat);
00079 }
00080 _builder->finalize_tet();
00081
00082
00083 int nof_bc_face;
00084 istr >> nof_bc_face;
00085 line ++;
00086 if (istr.fail())
00087 throw FileIOError(file_name, line, "error reading number of boundary faces");
00088 _builder->init_bc(nof_bc_face);
00089
00090
00091 int nof_bc[4] = {0, 0, 0, 0};
00092 int face_node[3];
00093 for (int i = 0; i < nof_bc_face; i ++) {
00094 int bc_id;
00095 istr >> bc_id;
00096 nof_bc[bc_id] += 1;
00097 for (int j = 0; j < 3; j ++) {
00098 istr >> node_id;
00099 node_id --;
00100 if (node_id < 0 || node_id >= nof_node)
00101 throw FileIOError(file_name, line, "node number out of range");
00102 face_node[j] = node_id;
00103 }
00104 line ++;
00105 if (istr.fail())
00106 throw FileIOError(file_name, line, "error reading boundary data");
00107 _builder->set_bc(face_node[0], face_node[1], face_node[2], bc_id);
00108 }
00109
00110
00111 int nof_sym;
00112 for (nof_sym = 3; nof_sym >= 0 && nof_bc[nof_sym] == 0; nof_sym --);
00113
00114 assert(nof_sym >= 0);
00115 for (int i = 1; i < nof_sym; i ++)
00116 if (nof_bc[i] == 0)
00117 throw FileIOError(file_name, "empty symmetry plane detected");
00118 _builder->finalize_bc(nof_sym);
00119 }