1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "meshkit/Mesh.hpp"

using namespace Jaal;

//##############################################################################
void skip_comments(FILE *f)<--- The function 'skip_comments' is never used.
{
    // Skip comments in an ASCII file (lines beginning with #)
    int c;
    bool in_comment = false;
    while (1) {
         c = fgetc(f);
         if (c == EOF) return;
         if (in_comment) {
              if (c == '\n') in_comment = false;
         } else if (c == '#') {
            in_comment = true;
         } else if (!isspace(c)) {
            break;
         }
    }
    ungetc(c, f);
}
//##############################################################################

int MeshImporter ::off_file(const string &fname)
{
  if( mesh == NULL ) mesh = new Mesh;

  ifstream infile( fname.c_str(), ios::in);
  if( infile.fail() ) {
      cout << "Warning: Cann't open file " << fname << endl;
      return 1;
  }

  //  The codelet is borrowed from TriMesh Software
  vector<int> facevtx;
  double  x, y, z;

  Vertex* vertex;<--- The scope of the variable 'vertex' can be reduced.
  NodeSequence vnodes, connect(3);
  string str;

  infile >> str;
  assert( str == "OFF");

  size_t  numNodes, numFaces, numEdges;
  infile >> numNodes >> numFaces >> numEdges;

  mesh->reserve( numNodes, 0);
  mesh->reserve( numFaces, 2);

  Point3D p3d;
  for( size_t i = 0; i < numNodes; i++) {
       infile >> x >> y >> z;
       p3d[0] = x;
       p3d[1] = y;
       p3d[2] = z;
       vertex = Vertex::newObject();
       vertex->setXYZCoords(p3d);
       vertex->setID(i);
       mesh->addNode( vertex );
  } 

  for( size_t i = 0; i < numFaces; i++) 
  {
       infile >> numNodes;
       facevtx.resize(numNodes);
       connect.resize(numNodes);
       for( size_t j = 0; j < numNodes; j++) 
            infile >> facevtx[j];

       switch( numNodes )
       {
          case 3:
              connect[0] = mesh->getNodeAt(facevtx[0]);
              connect[1] = mesh->getNodeAt(facevtx[1]);
              connect[2] = mesh->getNodeAt(facevtx[2]);
              break;
          case 4:
              connect[0] = mesh->getNodeAt(facevtx[0]);
              connect[1] = mesh->getNodeAt(facevtx[1]);
              connect[2] = mesh->getNodeAt(facevtx[2]);
              connect[3] = mesh->getNodeAt(facevtx[3]);
              break;
          default:
              for( size_t j = 0; j < numNodes; j++) 
                   connect[j] = mesh->getNodeAt(facevtx[j]);
              break; 
       }

       Face *face = new Face;

       int err = face->setNodes( connect );
       if( !err ) 
          mesh->addFace(face);
       else {
          cout << "Fatal error:  Bad element " << endl;
          for( size_t j = 0; j < numNodes; j++) 
              cout << facevtx[j] << " ";
          exit(0);
          delete face;<--- Statements following return, break, continue, goto or throw will never be executed.
       }
   }  

   return 0;
}

//##############################################################################

int 
MeshExporter ::off_file(Mesh *mesh, const string &s)
{
    mesh->prune();
    mesh->enumerate(0);

    string filename = s;
    ofstream ofile(filename.c_str(), ios::out);
    if( ofile.fail() ) 
        return 1;

    size_t numnodes = mesh->getSize(0);
    size_t numfaces = mesh->getSize(2);

    size_t nn = numnodes;
    ofile << "OFF" << endl;

    ofile << nn << " " << numfaces << " 0  " << endl;

    for (size_t i = 0; i < numnodes; i++)
    {
        Vertex *v = mesh->getNodeAt(i);
        assert( v->isActive() );
        const Point3D &p3d = v->getXYZCoords();
        ofile << p3d[0] << " " << p3d[1] << " " << p3d[2] << endl;
    }

    NodeSequence oldConnect, newConnect;
    for (size_t i = 0; i < numfaces; i++)
    {
        Face *face = mesh->getFaceAt(i);
        if( face->isActive()) {
        if (face->getSize(0) == 4)
        {
            oldConnect = face->getNodes();
            Face::quad_tessalate(oldConnect, newConnect); // Because of OpenGL
        }
        else
            newConnect = face->getNodes();

        int nnodes = newConnect.size();
        ofile << nnodes << " ";
        for (int j = 0; j < nnodes; j++)
        {
            size_t vid = newConnect[j]->getID();
            if (vid >= numnodes)
            {
                assert(!newConnect[j]->isRemoved());
                cout << face->getStatus() << endl;
                cout << "Vertex indexing out of range " << vid << " Total : " << numnodes << endl;
                exit(0);
            }
            ofile << vid << " ";
        }
        ofile << endl;
        }
    }

   return 0;
}