meshing/IO.cpp
///Loads from the GeomView Object File Format (OFF)
Loads from the GeomView Object File Format (OFF)
Loads from the GeomView Object File Format ( OFF )
bool LoadOFF(std::istream& in,TriMesh& tri)
{
  string tag;
  in>>tag;
  if(tag != "OFF") {
        LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: not a proper OFF file\n");
    return false;
  }
  int mode = 0; 
  int numFaces = 0;
  int vertIndex=0,faceIndex=0;
  int lineno = 0;
  string line;
  while(in) {
    lineno++;
    getline(in,line);
    if(in.bad()) {
      if(faceIndex == numFaces) return true;
            LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: error reading line "<<lineno);
      return false;
    }
    if(line.length() == 0) continue;
    if(line[0] == '#') continue; 
    if(mode == 0) {
      stringstream ss(line);
      int nv,nf,ne;
      ss>>nv>>nf>>ne;
      if(ss.bad()) {
        LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: unable to read first line\n");
        in.setstate(ios::badbit);
        return false;
      }
      tri.verts.resize(nv);
      tri.tris.resize(0);
      numFaces = nf;
      mode = 1;
      if((int)tri.verts.size() == vertIndex) mode=2;
    }
    else if(mode == 1) {
      stringstream ss(line);
      ss >> tri.verts[vertIndex];
      if(ss.bad()) {
                LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: unable to read vertex from line "<<lineno<<" = "<<line);
        in.setstate(ios::badbit);
        return false;
      }
      vertIndex++;
      if((int)tri.verts.size() == vertIndex) mode=2;
      if(faceIndex == numFaces) return true;
    }
    else if(mode == 2) {
      stringstream ss(line);
      int nv;
      vector<int> face;
      ss >> nv;
      if(ss.bad()) {
                LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: unable to load number of vertices on face line "<<lineno<<" = "<<line);
        in.setstate(ios::badbit);
        return false;
      }
      for(int i=0;i<nv;i++) {
        int v;
        ss>>v;
        if(v < 0 || v >= int(tri.verts.size())) {
                    LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: invalid vertex reference on line "<<lineno<<" = "<<line);
          return false;
        }
        face.push_back(v);
      }
      if(ss.bad()) {
                LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: incorrect number of vertices on line "<<lineno<<" = "<<line);
        in.setstate(ios::badbit);
        return false;
      }
      for(int i=1;i+1<nv;i++) {
        tri.tris.push_back(IntTriple(face[0],face[i],face[i+1]));
      }
      faceIndex++;
      if(faceIndex == numFaces) return true;
    }
  }
  if(faceIndex == numFaces) return true;
    LOG4CXX_ERROR(KrisLibrary::logger(),"LoadOFF: Unexpected end of file on line "<<lineno<<", face "<<faceIndex<<" / "<<numFaces);
  return false;
}
bool LoadOFF ( std :: istream & in , TriMesh & tri ) { string tag ; in >> tag ; if ( tag != " " ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " \n " ) ; return false ; } int mode = 0 ; int numFaces = 0 ; int vertIndex = 0 , faceIndex = 0 ; int lineno = 0 ; string line ; while ( in ) { lineno ++ ; getline ( in , line ) ; if ( in . bad ( ) ) { if ( faceIndex == numFaces ) return true ; LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno ) ; return false ; } if ( line . length ( ) == 0 ) continue ; if ( line [ 0 ] == ' ' ) continue ; if ( mode == 0 ) { stringstream ss ( line ) ; int nv , nf , ne ; ss >> nv >> nf >> ne ; if ( ss . bad ( ) ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " \n " ) ; in . setstate ( ios :: badbit ) ; return false ; } tri . verts . resize ( nv ) ; tri . tris . resize ( 0 ) ; numFaces = nf ; mode = 1 ; if ( ( int ) tri . verts . size ( ) == vertIndex ) mode = 2 ; } else if ( mode == 1 ) { stringstream ss ( line ) ; ss >> tri . verts [ vertIndex ] ; if ( ss . bad ( ) ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno << " " << line ) ; in . setstate ( ios :: badbit ) ; return false ; } vertIndex ++ ; if ( ( int ) tri . verts . size ( ) == vertIndex ) mode = 2 ; if ( faceIndex == numFaces ) return true ; } else if ( mode == 2 ) { stringstream ss ( line ) ; int nv ; vector < int > face ; ss >> nv ; if ( ss . bad ( ) ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno << " " << line ) ; in . setstate ( ios :: badbit ) ; return false ; } for ( int i = 0 ; i < nv ; i ++ ) { int v ; ss >> v ; if ( v < 0 || v >= int ( tri . verts . size ( ) ) ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno << " " << line ) ; return false ; } face . push_back ( v ) ; } if ( ss . bad ( ) ) { LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno << " " << line ) ; in . setstate ( ios :: badbit ) ; return false ; } for ( int i = 1 ; i + 1 < nv ; i ++ ) { tri . tris . push_back ( IntTriple ( face [ 0 ] , face [ i ] , face [ i + 1 ] ) ) ; } faceIndex ++ ; if ( faceIndex == numFaces ) return true ; } } if ( faceIndex == numFaces ) return true ; LOG4CXX_ERROR ( KrisLibrary :: logger ( ) , " " << lineno << " " << faceIndex << " " << numFaces ) ; return false ; }
Loads from the GeomView Object File Format (OFF)
Loads from the GeomView Object File Format ( OFF )
//0: waiting for sizes, 1: reading verts, 2: reading tris //comment line //ignore RGBA colors //ignore RGBA colors //triangulate faces, assume convex