MeshKit  1.0
vtk_io.cpp
Go to the documentation of this file.
00001 #include "meshkit/Mesh.hpp"
00002 
00003 #include <iomanip>
00004 
00005 using namespace Jaal;
00006 
00007 int MeshExporter :: vtk_file(Mesh *mesh, const string &fname)
00008 {
00009     ofstream ofile(fname.c_str(), ios::out);
00010     if( ofile.fail() ) return 1;
00011 
00012     size_t numNodes = mesh->getSize(0);
00013 
00014     ofile << "# vtk DataFile Version 2.0" << endl;
00015     ofile << " Jaal Mesh Generator " << endl;
00016 
00017     ofile << "ASCII " << endl;
00018 
00019     ofile << "DATASET UNSTRUCTURED_GRID " << endl;
00020     ofile << "POINTS " << numNodes << " float " << endl;
00021     ofile << std::setiosflags(ios::fixed);
00022 
00023     for( size_t i = 0; i < numNodes; i++) {
00024         Vertex *vertex = mesh->getNodeAt(i);
00025         const Point3D &xyz = vertex->getXYZCoords();
00026         ofile << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl;
00027     }
00028 
00029     size_t numFaces = mesh->getSize(2);
00030     size_t numConn = 0;
00031 
00032     for( size_t i = 0; i < numFaces; i++) {
00033         Face *face = mesh->getFaceAt(i);
00034         numConn += face->getSize(0);
00035     }
00036 
00037     ofile << "CELLS " << numFaces << "  " << numFaces + numConn << endl;
00038 
00039     for( size_t i = 0; i < numFaces; i++) {
00040         Face *face = mesh->getFaceAt(i);
00041         int nsize = face->getSize(0);
00042         ofile << nsize << " ";
00043         for(int i = 0; i < nsize; i++) {
00044             Vertex *v = face->getNodeAt(i);
00045             ofile << v->getID() << " ";
00046         }
00047         ofile << endl;
00048     }
00049 
00050     ofile << "CELL_TYPES " << numFaces << endl;
00051     for( size_t i = 0; i < numFaces; i++) {
00052         Face *face = mesh->getFaceAt(i);
00053         int nsize = face->getSize(0);
00054         int type  = 7;
00055         if( nsize == 3 ) type = 5;
00056         if( nsize == 4 ) type = 9;
00057         ofile << type << " ";
00058     }
00059     return 0;
00060 }
00061 
00062 int MeshImporter ::vtk_file( const string &fname)
00063 {
00064     cout << " VTK File " << endl;
00065     ifstream infile( fname.c_str(), ios::in);
00066     if( infile.fail() )  {
00067         cout << "Warning: cann't open node file " << fname << endl;
00068         return 1;
00069     }
00070 
00071     size_t numnodes, numfaces;
00072     double x, y, z = 0.0;
00073     Point3D xyz;
00074 
00075     int n0, n1, n2, n3, numelemnodes;
00076     NodeSequence connect;
00077     Face *newface;
00078 
00079     string str;
00080     while( !infile.eof() ) {
00081         infile >> str;
00082 
00083         if( str == "POINTS") {
00084             infile >> numnodes >> str;
00085             mesh->reserve( numnodes, 0);
00086             for( size_t i = 0; i < numnodes; i++)  {
00087                 infile >> x >> y  >> z;
00088                 xyz[0] = x;
00089                 xyz[1] = y;
00090                 xyz[2] = z;
00091                 Vertex *v = Vertex::newObject();
00092                 v->setID(i);
00093                 v->setXYZCoords(xyz);
00094                 mesh->addNode(v);
00095             }
00096         }
00097 
00098         if( str == "CELLS") {
00099             cout << " READ CELLS " << endl;
00100             infile >> numfaces >> str;
00101             mesh->reserve( numfaces, 2);
00102 
00103             for( size_t i = 0; i < numfaces; i++) {
00104                 infile >> numelemnodes;
00105                 //                cout <<  numelemnodes << endl;
00106 
00107                 switch( numelemnodes ) {
00108                 case 3:
00109                     connect.resize(3);
00110                     infile >> n0 >> n1 >> n2;
00111                     connect[0] = mesh->getNodeAt(n0);
00112                     connect[1] = mesh->getNodeAt(n1);
00113                     connect[2] = mesh->getNodeAt(n2);
00114                     newface = Face::newObject();
00115                     newface->setNodes(connect);
00116                     mesh->addFace( newface );
00117                     break;
00118                 case 4:
00119                     connect.resize(4);
00120                     infile >> n0 >> n1 >> n2 >> n3;
00121                     connect[0] = mesh->getNodeAt(n0);
00122                     connect[1] = mesh->getNodeAt(n1);
00123                     connect[2] = mesh->getNodeAt(n2);
00124                     connect[3] = mesh->getNodeAt(n3);
00125                     newface = Face::newObject();
00126                     newface->setNodes(connect);
00127                     mesh->addFace( newface );
00128                     break;
00129                 default:
00130                     for( int i = 0; i < numelemnodes; i++)
00131                         infile >> n0;
00132                 }
00133             }
00134         }
00135     }
00136 
00137     return 0;
00138 }
00139 
00140 
00141 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines