The Parametric Pseudo-Manifold (PPS) Library 1.0
offlib::Reader Class Reference

This class represents a file reader for reading in a triangle surface mesh information from an OFF file. More...

#include <reader.h>

Collaboration diagram for offlib::Reader:

List of all members.

Public Member Functions

 Reader (const std::string &fn)
 Creates an instance of this class.
 ~Reader ()
 Destroys an instance of this class.
void read (unsigned &nv, double *&vset, unsigned &nf, unsigned *&fset)
 Reads in the topological and geometric information of the surface mesh described in the input file.

Private Member Functions

void read_header (unsigned &nv, unsigned &nf)
 Reads in the number of vertices and faces of the surface described by the input file.
void read_vertices (unsigned nv, double *&vset)
 Reads in the vertex coordinates of the surface described by the input file.
void read_faces (unsigned nf, unsigned nv, unsigned *&fset)
 Reads in the face vertex identifiers of the surface described by the input file.

Private Attributes

std::string _fname
 The input file name.
Lexer_is
 A lexical analyzer for the input file stream.

Detailed Description

This class represents a file reader for reading in a triangle surface mesh information from an OFF file.

Definition at line 56 of file reader.h.


Constructor & Destructor Documentation

offlib::Reader::Reader ( const std::string &  fn) [inline]

Creates an instance of this class.

Parameters:
fnThe name of an OFF input file.

Definition at line 71 of file reader.h.

References _fname.

                                  : _is( 0 )
    {
      _fname = fn + std::string( ".off" ) ;
    }

Member Function Documentation

void offlib::Reader::read ( unsigned &  nv,
double *&  vset,
unsigned &  nf,
unsigned *&  fset 
)

Reads in the topological and geometric information of the surface mesh described in the input file.

Parameters:
nvA reference to the number of vertices of the surface.
vsetA reference to an array with the vertex coordinates.
nfA reference to the number of faces of the surface.
fsetA reference to an array with the face vertex identifiers.

Definition at line 64 of file reader.cpp.

References _fname, _is, read_faces(), read_header(), and read_vertices().

Referenced by main().

  {
    std::filebuf fb ;

    assert( fb.open( _fname.c_str() , std::ios::in ) != 0 ) ;

    std::istream istr( &fb ) ;

    _is = new Lexer( istr ) ;

    vset = 0 ;
    fset = 0 ;

    read_header( nv , nf ) ;

    read_vertices( nv , vset ) ;

    read_faces( nf , nv , fset ) ;

    fb.close();
  }
void offlib::Reader::read_faces ( unsigned  nf,
unsigned  nv,
unsigned *&  fset 
) [private]

Reads in the face vertex identifiers of the surface described by the input file.

Parameters:
nfThe number of faces of the surface.
nvThe number of vertices of the surface.
fsetA reference to an array of face vertex identifiers.

Definition at line 218 of file reader.cpp.

References _is, and offlib::Lexer::get_integer().

Referenced by read().

  {
    //
    // Allocate memory for storing the vertices.
    //
    fset = new unsigned[ 3 * nf ] ;

    //
    // Read "nf" lines of the form "3 v1 v2 v3", where "v1", "v2", and
    // "v3"  are the  vertex  indices  of the  face  defined by  these
    // vertices.
    //
    for ( unsigned i = 0 ; i < nf ; i++ ) {
      //
      // Read in the number of vertices of the i-th face.
      //
      int number ;

      assert( _is->get_integer( number ) ) ;

      assert( number == 3 ) ;

      //
      // Read in the identifier of the first face vertex.
      //

      assert( _is->get_integer( number ) ) ;

      unsigned i1 = unsigned( number ) ;      

      assert( i1 < nv ) ;

      //
      // Read in the identifier of the second face vertex.
      //

      assert( _is->get_integer( number ) ) ;

      unsigned i2 = unsigned( number ) ;      

      assert( i2 < nv ) ;

      //
      // Read in the identifier of the third face vertex.
      //

      assert( _is->get_integer( number ) ) ;

      unsigned i3 = unsigned( number ) ;      

      assert( i3 < nv ) ;

      //
      // Store face data.
      //
      const unsigned j = 3 * i ;

      fset[ j     ] = i1 ;
      fset[ j + 1 ] = i2 ;
      fset[ j + 2 ] = i3 ;
    }

    return ;
  }
void offlib::Reader::read_header ( unsigned &  nv,
unsigned &  nf 
) [private]

Reads in the number of vertices and faces of the surface described by the input file.

Parameters:
nvA reference to the number of vertices of the surface.
nfA reference to the number of faces of the surface.

We ignore the number of edges.

Definition at line 102 of file reader.cpp.

References _is, offlib::Lexer::get_integer(), and offlib::Lexer::get_string().

Referenced by read().

  {
    //
    // The first line should start with the string "OFF".
    //
    std::string stroff;

    assert( _is->get_string( stroff ) ) ;

    assert( stroff == "OFF" ) ;
   
    //
    // Read in the number of vertices of the mesh.
    //
    int number ;

    assert ( _is->get_integer( number ) ) ;

    nv = unsigned( number ) ;

    assert( nv >= 4 ) ;

    //
    // Read in the number of faces of the mesh.
    //

    assert( _is->get_integer( number ) ) ;

    nf = unsigned( number ) ;

    assert( nf >= 4 ) ;

    //
    // Read in the number of edges of the surface.
    //

    assert( _is->get_integer( number ) ) ;

    return ;
  }
void offlib::Reader::read_vertices ( unsigned  nv,
double *&  vset 
) [private]

Reads in the vertex coordinates of the surface described by the input file.

Parameters:
nvThe number of vertices of the surface.
vsetA reference to an array of vertex coordinates.
nvA reference to the number of vertices of the surface.
vsetA reference to an array of vertex coordinates.

Definition at line 161 of file reader.cpp.

References _is, and offlib::Lexer::get_double().

Referenced by read().

  {
    //
    // Allocate memory for storing the vertices.
    //
    vset = new double[ 3 * nv ] ;

    //
    // Read "nv" lines with the vertex coordinates
    // 
    for ( unsigned i = 0 ; i < nv ; i++ ) {
      //
      // Read in the first coordinate.
      //
      double x ;

      assert( _is->get_double( x ) ) ;

      //
      // Read in the second coordinate.
      //
      double y ;

      assert( _is->get_double( y ) ) ;

      //
      // Read in the third coordinate.
      //
      double z ;

      assert( _is->get_double( z ) ) ;

      const unsigned j = 3 * i ;

      vset[ j     ] = x ;
      vset[ j + 1 ] = y ;
      vset[ j + 2 ] = z ;
    }

    return ;
  }

The documentation for this class was generated from the following files: