MeshKit
1.0
|
00001 #include "meshkit/Mesh.hpp" 00002 00003 using namespace Jaal; 00004 00005 int 00006 MeshExporter::simple_file( Mesh *mesh, const string &s) 00007 { 00008 if (!mesh->isPruned()) 00009 { 00010 mesh->prune(); 00011 mesh->enumerate(0); 00012 mesh->enumerate(2); 00013 } 00014 00015 string filename = s; 00016 ofstream ofile(filename.c_str(), ios::out); 00017 if( ofile.fail() ) { 00018 return 1; 00019 } 00020 00021 if (!mesh->is_consistently_oriented()) 00022 { 00023 cout << "Warning: Mesh is not conistently oriented " << endl; 00024 } 00025 00026 size_t numnodes = mesh->getSize(0); 00027 size_t numfaces = mesh->getSize(2); 00028 00029 size_t nn = numnodes; 00030 00031 ofile << nn << " " << numfaces << endl; 00032 00033 for (size_t i = 0; i < numnodes; i++) 00034 { 00035 Vertex *v = mesh->getNodeAt(i); 00036 const Point3D &p3d = v->getXYZCoords(); 00037 ofile << p3d[0] << " " << p3d[1] << " " << p3d[2] << endl; 00038 } 00039 00040 vector<PNode> oldConnect, newConnect; 00041 for (size_t i = 0; i < numfaces; i++) 00042 { 00043 Face *face = mesh->getFaceAt(i); 00044 face->start_from_concave_corner(); 00045 int nnodes = face->getSize(0); 00046 ofile << nnodes << " "; 00047 for (int j = 0; j < nnodes; j++) 00048 { 00049 Vertex *vtx = face->getNodeAt(j); 00050 size_t vid = vtx->getID(); 00051 if (vid >= numnodes) 00052 { 00053 assert(!face->isRemoved()); 00054 assert(!vtx->isRemoved()); 00055 cout << face->getID() << endl; 00056 cout << face->isRemoved() << endl; 00057 cout << "Vertex indexing out of range " << vid << endl; 00058 exit(0); 00059 } 00060 ofile << vid << " "; 00061 } 00062 ofile << endl; 00063 } 00064 00065 /* 00066 for (size_t i = 0; i < numnodes; i++) 00067 { 00068 Vertex *vertex = mesh->getNodeAt(i); 00069 ofile << vertex->getTag() << " "; 00070 } 00071 ofile << endl; 00072 00073 for (size_t i = 0; i < numfaces; i++) 00074 { 00075 Face *face = mesh->getFaceAt(i); 00076 ofile << face->getTag() << " "; 00077 } 00078 */ 00079 return 0; 00080 } 00081 00083 00084 int MeshImporter::simple_file(const string &fname) 00085 { 00086 ifstream infile( fname.c_str(), ios::in); 00087 if( infile.fail() ) { 00088 cout << "Warning: cann't open node file " << fname << endl; 00089 return 1; 00090 } 00091 00092 size_t numnodes, numfaces; 00093 00094 infile >> numnodes >> numfaces; 00095 00096 mesh->reserve(numnodes,0); 00097 00098 double x, y, z = 0.0; 00099 Point3D xyz; 00100 for( size_t i = 0; i < numnodes; i++) { 00101 infile >> x >> y >> z; 00102 xyz[0] = x; 00103 xyz[1] = y; 00104 xyz[2] = z; 00105 Vertex *v = Vertex::newObject(); 00106 v->setID(i); 00107 v->setXYZCoords(xyz); 00108 mesh->addNode(v); 00109 } 00110 00111 mesh->reserve( numfaces, 2); 00112 00113 int n0, n1, n2, n3, numelemnodes; 00114 NodeSequence connect; 00115 Face *newface; 00116 00117 string restline; 00118 for( size_t i = 0; i < numfaces; i++) { 00119 infile >> numelemnodes; 00120 00121 if( numelemnodes == 3 ) { 00122 connect.resize(3); 00123 infile >> n0 >> n1 >> n2; 00124 connect[0] = mesh->getNodeAt(n0); 00125 connect[1] = mesh->getNodeAt(n1); 00126 connect[2] = mesh->getNodeAt(n2); 00127 newface = Face::newObject(); 00128 newface->setNodes(connect); 00129 newface->setID(i); 00130 mesh->addFace( newface ); 00131 } 00132 00133 if( numelemnodes == 4 ) { 00134 connect.resize(4); 00135 infile >> n0 >> n1 >> n2 >> n3; 00136 connect[0] = mesh->getNodeAt(n0); 00137 connect[1] = mesh->getNodeAt(n1); 00138 connect[2] = mesh->getNodeAt(n2); 00139 connect[3] = mesh->getNodeAt(n3); 00140 newface = Face::newObject(); 00141 newface->setNodes(connect); 00142 newface->setID(i); 00143 mesh->addFace( newface ); 00144 } 00145 } 00146 00147 /* 00148 int itag; 00149 for( size_t i = 0; i < numnodes; i++) { 00150 infile >> itag; 00151 Vertex *v = mesh->getNodeAt(i); 00152 v->setTag(itag); 00153 } 00154 00155 for( size_t i = 0; i < numfaces; i++) { 00156 infile >> itag; 00157 Face *f = mesh->getFaceAt(i); 00158 f->setTag(itag); 00159 } 00160 */ 00161 00162 return 0; 00163 } 00164 00166 00167 int Mesh::readFromFile( const string &fname) 00168 { 00169 MeshImporter mimporter; 00170 Mesh *m = mimporter.load( fname, this); 00171 00172 if( m ) return 0; 00173 00174 return 1; 00175 00176 } 00177