MeshKit  1.0
off_io.cpp
Go to the documentation of this file.
00001 #include "meshkit/Mesh.hpp"
00002 
00003 using namespace Jaal;
00004 
00005 //##############################################################################
00006 void skip_comments(FILE *f)
00007 {
00008     // Skip comments in an ASCII file (lines beginning with #)
00009     int c;
00010     bool in_comment = false;
00011     while (1) {
00012          c = fgetc(f);
00013          if (c == EOF) return;
00014          if (in_comment) {
00015               if (c == '\n') in_comment = false;
00016          } else if (c == '#') {
00017             in_comment = true;
00018          } else if (!isspace(c)) {
00019             break;
00020          }
00021     }
00022     ungetc(c, f);
00023 }
00024 //##############################################################################
00025 
00026 int MeshImporter ::off_file(const string &fname)
00027 {
00028   if( mesh == NULL ) mesh = new Mesh;
00029 
00030   ifstream infile( fname.c_str(), ios::in);
00031   if( infile.fail() ) {
00032       cout << "Warning: Cann't open file " << fname << endl;
00033       return 1;
00034   }
00035 
00036   //  The codelet is borrowed from TriMesh Software
00037   vector<int> facevtx;
00038   double  x, y, z;
00039 
00040   Vertex* vertex;
00041   NodeSequence vnodes, connect(3);
00042   string str;
00043 
00044   infile >> str;
00045   assert( str == "OFF");
00046 
00047   size_t  numNodes, numFaces, numEdges;
00048   infile >> numNodes >> numFaces >> numEdges;
00049 
00050   mesh->reserve( numNodes, 0);
00051   mesh->reserve( numFaces, 2);
00052 
00053   Point3D p3d;
00054   for( size_t i = 0; i < numNodes; i++) {
00055        infile >> x >> y >> z;
00056        p3d[0] = x;
00057        p3d[1] = y;
00058        p3d[2] = z;
00059        vertex = Vertex::newObject();
00060        vertex->setXYZCoords(p3d);
00061        vertex->setID(i);
00062        mesh->addNode( vertex );
00063   } 
00064 
00065   for( size_t i = 0; i < numFaces; i++) 
00066   {
00067        infile >> numNodes;
00068        facevtx.resize(numNodes);
00069        connect.resize(numNodes);
00070        for( size_t j = 0; j < numNodes; j++) 
00071             infile >> facevtx[j];
00072 
00073        switch( numNodes )
00074        {
00075           case 3:
00076               connect[0] = mesh->getNodeAt(facevtx[0]);
00077               connect[1] = mesh->getNodeAt(facevtx[1]);
00078               connect[2] = mesh->getNodeAt(facevtx[2]);
00079               break;
00080           case 4:
00081               connect[0] = mesh->getNodeAt(facevtx[0]);
00082               connect[1] = mesh->getNodeAt(facevtx[1]);
00083               connect[2] = mesh->getNodeAt(facevtx[2]);
00084               connect[3] = mesh->getNodeAt(facevtx[3]);
00085               break;
00086           default:
00087               for( size_t j = 0; j < numNodes; j++) 
00088                    connect[j] = mesh->getNodeAt(facevtx[j]);
00089               break; 
00090        }
00091 
00092        Face *face = new Face;
00093 
00094        int err = face->setNodes( connect );
00095        if( !err ) 
00096           mesh->addFace(face);
00097        else {
00098           cout << "Fatal error:  Bad element " << endl;
00099           for( size_t j = 0; j < numNodes; j++) 
00100               cout << facevtx[j] << " ";
00101           exit(0);
00102           delete face;
00103        }
00104    }  
00105 
00106    return 0;
00107 }
00108 
00109 //##############################################################################
00110 
00111 int 
00112 MeshExporter ::off_file(Mesh *mesh, const string &s)
00113 {
00114     mesh->prune();
00115     mesh->enumerate(0);
00116 
00117     string filename = s;
00118     ofstream ofile(filename.c_str(), ios::out);
00119     if( ofile.fail() ) 
00120         return 1;
00121 
00122     size_t numnodes = mesh->getSize(0);
00123     size_t numfaces = mesh->getSize(2);
00124 
00125     size_t nn = numnodes;
00126     ofile << "OFF" << endl;
00127 
00128     ofile << nn << " " << numfaces << " 0  " << endl;
00129 
00130     for (size_t i = 0; i < numnodes; i++)
00131     {
00132         Vertex *v = mesh->getNodeAt(i);
00133         assert( v->isActive() );
00134         const Point3D &p3d = v->getXYZCoords();
00135         ofile << p3d[0] << " " << p3d[1] << " " << p3d[2] << endl;
00136     }
00137 
00138     NodeSequence oldConnect, newConnect;
00139     for (size_t i = 0; i < numfaces; i++)
00140     {
00141         Face *face = mesh->getFaceAt(i);
00142         if( face->isActive()) {
00143         if (face->getSize(0) == 4)
00144         {
00145             oldConnect = face->getNodes();
00146             Face::quad_tessalate(oldConnect, newConnect); // Because of OpenGL
00147         }
00148         else
00149             newConnect = face->getNodes();
00150 
00151         int nnodes = newConnect.size();
00152         ofile << nnodes << " ";
00153         for (int j = 0; j < nnodes; j++)
00154         {
00155             size_t vid = newConnect[j]->getID();
00156             if (vid >= numnodes)
00157             {
00158                 assert(!newConnect[j]->isRemoved());
00159                 cout << face->getStatus() << endl;
00160                 cout << "Vertex indexing out of range " << vid << " Total : " << numnodes << endl;
00161                 exit(0);
00162             }
00163             ofile << vid << " ";
00164         }
00165         ofile << endl;
00166         }
00167     }
00168 
00169    return 0;
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines