MeshKit  1.0
xml_io.cpp
Go to the documentation of this file.
00001 #include "meshkit/Mesh.hpp"
00002 #include <iomanip>
00003 
00004 using namespace Jaal;
00005 
00006 int MeshImporter ::xml_file(const string &fname)
00007 {
00008 /*
00009   if( mesh == NULL ) mesh = new Mesh;
00010 
00011   ifstream infile( fname.c_str(), ios::in);
00012   if( infile.fail() ) {
00013       cout << "Warning: Cann't open file " << fname << endl;
00014       return 1;
00015   }
00016 
00017   //  The codelet is borrowed from TriMesh Software
00018   vector<int> facevtx;
00019   double  x, y, z;
00020 
00021   Vertex* vertex;
00022   NodeSequence vnodes, connect(3);
00023   string str;
00024 
00025   infile >> str;
00026   assert( str == "OFF");
00027 
00028   size_t  numNodes, numFaces, numEdges;
00029   infile >> numNodes >> numFaces >> numEdges;
00030 
00031   mesh->reserve( numNodes, 0);
00032   mesh->reserve( numFaces, 2);
00033 
00034   Point3D p3d;
00035   for( size_t i = 0; i < numNodes; i++) {
00036        infile >> x >> y >> z;
00037        p3d[0] = x;
00038        p3d[1] = y;
00039        p3d[2] = z;
00040        vertex = Vertex::newObject();
00041        vertex->setXYZCoords(p3d);
00042        mesh->addNode( vertex );
00043   } 
00044 
00045   for( size_t i = 0; i < numFaces; i++) 
00046   {
00047        infile >> numNodes;
00048        facevtx.resize(numNodes);
00049        connect.resize(numNodes);
00050        for( size_t j = 0; j < numNodes; j++) 
00051             infile >> facevtx[j];
00052 
00053        switch( numNodes )
00054        {
00055           case 3:
00056               connect[0] = mesh->getNodeAt(facevtx[0]);
00057               connect[1] = mesh->getNodeAt(facevtx[1]);
00058               connect[2] = mesh->getNodeAt(facevtx[2]);
00059               break;
00060           case 4:
00061               connect[0] = mesh->getNodeAt(facevtx[0]);
00062               connect[1] = mesh->getNodeAt(facevtx[1]);
00063               connect[2] = mesh->getNodeAt(facevtx[2]);
00064               connect[3] = mesh->getNodeAt(facevtx[3]);
00065               break;
00066           default:
00067               for( size_t j = 0; j < numNodes; j++) 
00068                    connect[j] = mesh->getNodeAt(facevtx[j]);
00069               break; 
00070        }
00071 
00072        Face *face = new Face;
00073 
00074        int err = face->setNodes( connect );
00075        if( !err ) 
00076           mesh->addFace(face);
00077        else {
00078           cout << "Fatal error:  Bad element " << endl;
00079           for( size_t j = 0; j < numNodes; j++) 
00080               cout << facevtx[j] << " ";
00081           exit(0);
00082           delete face;
00083        }
00084    }  
00085 
00086    int gid;
00087    infile >> str;
00088    if( str == "#NODE_GROUP")  {
00089        numNodes = mesh->getSize(0);
00090        for( size_t i = 0; i < numNodes; i++) {
00091             infile >> gid;
00092             Vertex *v = mesh->getNodeAt(i);
00093            v->setGroupID( gid );
00094 //          v->setTag( gid );
00095        }
00096        infile >> str;
00097    }
00098 
00099    if( str == "#FACE_GROUP")  {
00100        numFaces = mesh->getSize(2);
00101        for( size_t i = 0; i < numFaces; i++) {
00102             infile >> gid;
00103             Face *f = mesh->getFaceAt(i);
00104 //            f->setTag( gid );
00105             f->setGroupID( gid );
00106        }
00107    }
00108 */
00109    
00110    return 0;
00111 }
00112 
00113 //##############################################################################
00114 
00115 int 
00116 MeshExporter ::xml_file(Mesh *mesh, const string &s)
00117 {
00118     size_t ncount;
00119     mesh->prune();
00120     mesh->enumerate(0);
00121 
00122     string filename = s;
00123     ofstream ofile(filename.c_str(), ios::out);
00124     if( ofile.fail() ) 
00125         return 1;
00126 
00127     size_t numnodes = mesh->getSize(0);
00128     size_t numfaces = mesh->getSize(2);
00129 
00130     ofile << "<Mesh> " << endl;
00131     ofile << "<NumNodes> " <<  numnodes << " </NumNodes>" << endl;
00132     ofile << "<NumFaces> " <<  numfaces << " </NumFaces>" << endl;
00133     ofile << "<NodeCoordinates>" << endl;
00134     for (size_t i = 0; i < numnodes; i++)
00135     {
00136         Vertex *v = mesh->getNodeAt(i);
00137         assert( v->isActive() );
00138         const Point3D &p3d = v->getXYZCoords();
00139         ofile << p3d[0] << " " << p3d[1] << " " << p3d[2] << endl;
00140     }
00141     ofile << "</NodeCoordinates>" << endl;
00142 
00143     // Write all edges ( mesh edges and feature edges ) 
00144     ncount = 0;
00145     if(ncount > 1 ) {
00146     ofile << "<Edges> " << endl;
00147     ofile << "<Count> " << ncount << " </Count" << endl;
00148     ofile << "</Edges> " << endl;
00149     }
00150 
00151     ncount = 0;
00152     if(ncount > 0) {
00153     ofile << "<FeatureEdges> " << endl;
00154     ofile << "<Count> " << ncount << " </Count" << endl;
00155     ofile << "</FeatureEdges> " << endl;
00156     }
00157 
00158     ofile << "<Faces>" << endl;
00159 
00160     // Write down all triangle faces 
00161     ncount = 0;
00162     for (size_t i = 0; i < numfaces; i++)
00163     {
00164         Face *face = mesh->getFaceAt(i);
00165         if( face->isActive() && face->getSize(0) == 3 ) ncount++;
00166     }
00167 
00168     if( ncount > 0) {
00169     ofile << "<Triangles> " << endl;
00170     ofile << "<Count> " <<  ncount <<  " </Count> " <<  endl;
00171     for (size_t i = 0; i < numfaces; i++)
00172     {
00173         Face *face = mesh->getFaceAt(i);
00174         if( face->isActive() && face->getSize(0) == 3 ) {
00175             for( int j = 0; j < 3; j++) 
00176                 ofile << face->getNodeAt(j)->getID() << " ";
00177            ofile << endl;
00178         }
00179     }
00180     ofile << "</Triangles> " << endl;
00181     }
00182 
00183     ncount = 0;
00184     for (size_t i = 0; i < numfaces; i++)
00185     {
00186         Face *face = mesh->getFaceAt(i);
00187         if( face->isActive() && face->getSize(0) == 4 ) ncount++;
00188 
00189     }
00190 
00191     // Write down all quad  faces 
00192     if( ncount > 0) {
00193     ofile << "<Quads> " << endl;
00194     ofile << "<Count> " <<  ncount <<  " </Count> " <<  endl;
00195     for (size_t i = 0; i < numfaces; i++)
00196     {
00197         Face *face = mesh->getFaceAt(i);
00198         if( face->isActive() && face->getSize(0) == 4 ) {
00199             for( int j = 0; j < 4; j++) 
00200                 ofile << face->getNodeAt(j)->getID() << " ";
00201            ofile << endl;
00202         }
00203     }
00204     ofile << "</Quads> " << endl;
00205     }
00206 
00207     ncount = 0;
00208     for (size_t i = 0; i < numfaces; i++)
00209     {
00210         Face *face = mesh->getFaceAt(i);
00211         if( face->isActive() && face->getSize(0) > 4 ) ncount++;
00212     }
00213 
00214     // Write down all polygonal faces 
00215     if( ncount > 0) {
00216     ofile << "<Polygons> " << endl;
00217     ofile << "<Count> " <<  ncount <<  " </Count> " <<  endl;
00218     for (size_t i = 0; i < numfaces; i++)
00219     {
00220         Face *face = mesh->getFaceAt(i);
00221         if( face->isActive() && face->getSize(0) > 4 ) {
00222             int nn = face->getSize(0);    
00223             ofile << nn << " ";
00224             for( int j = 0; j < nn; j++) 
00225                 ofile << face->getNodeAt(j)->getID() << " ";
00226            ofile << endl;
00227         }
00228     }
00229     ofile << "</Polygons> " << endl;
00230     }
00231 
00232     ofile << "</Faces>" << endl;
00233 
00234     ncount = 0;
00235     if( ncount > 0) {
00236     ofile << "<Partition>" << endl;
00237     ofile << "<Count> " <<  ncount << " </Count" <<  endl;
00238 
00239     if( ncount > 1) {
00240     ofile << "<InterfaceEdges>" << endl;
00241     ofile << "</InterfaceEdges>" << endl;
00242 
00243     ofile << "<NodesPartID>" << endl;
00244     ofile << "</NodesPartID>" << endl;
00245 
00246     ofile << "<FacesPartID>" << endl;
00247     ofile << "</FacesPartID>" << endl;
00248     } 
00249 
00250     ofile << "</Partition>" << endl;
00251     }
00252 
00253     ofile << "<Attributes>" << endl;
00254 
00255     ofile << "<NodeAttributes>" << endl;
00256     ofile << "</NodeAttributes>" << endl;
00257 
00258     ofile << "<EdgeAttributes>" << endl;
00259     ofile << "</EdgeAttributes>" << endl;
00260 
00261     ofile << "<FaceAttributes>" << endl;
00262     ofile << "</FaceAttributes>" << endl;
00263  
00264     ofile << "</Attributes>" << endl;
00265 
00266 /*
00267     size_t nn = numnodes;
00268     ofile << "OFF" << endl;
00269 
00270     NodeSequence oldConnect, newConnect;
00271     for (size_t i = 0; i < numfaces; i++)
00272     {
00273         Face *face = mesh->getFaceAt(i);
00274         if( face->isActive()) {
00275         if (face->getSize(0) == 4)
00276         {
00277             oldConnect = face->getNodes();
00278             Face::quad_tessalate(oldConnect, newConnect); // Because of OpenGL
00279         }
00280         else
00281             newConnect = face->getNodes();
00282 
00283         int nnodes = newConnect.size();
00284         ofile << nnodes << " ";
00285         for (int j = 0; j < nnodes; j++)
00286         {
00287             size_t vid = newConnect[j]->getID();
00288             if (vid >= numnodes)
00289             {
00290                 assert(!newConnect[j]->isRemoved());
00291                 cout << face->getStatus() << endl;
00292                 cout << "Vertex indexing out of range " << vid << " Total : " << numnodes << endl;
00293                 exit(0);
00294             }
00295             ofile << vid << " ";
00296         }
00297         ofile << endl;
00298         }
00299     }
00300 
00301    ofile << "#NODE_GROUP ";
00302    size_t numNodes = mesh->getSize(0);
00303    for( size_t i = 0; i < numNodes; i++) {
00304        Vertex *v = mesh->getNodeAt(i);
00305 //     ofile << v->getTag() << " ";
00306        ofile << v->getGroupID() << " ";
00307    }
00308    ofile << endl;
00309 
00310    ofile << "#FACE_GROUP ";
00311    size_t numFaces = mesh->getSize(2);
00312    for( size_t i = 0; i < numFaces; i++) {
00313         Face *f = mesh->getFaceAt(i);
00314 //      ofile << f->getTag() << " ";
00315         ofile << f->getGroupID() << " ";
00316    }
00317 */
00318    ofile << "</Mesh>" << endl;
00319    
00320    return 0;
00321 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines