The Parametric Pseudo-Manifold (PPS) Library 1.0
|
00001 00026 #include "reader.h" 00027 00028 #include <iostream> 00029 #include <sstream> 00030 #include <cassert> 00031 00032 00046 namespace offlib { 00047 00048 using std::cout; 00049 using std::endl; 00050 00063 void 00064 Reader::read( 00065 unsigned& nv , 00066 double*& vset , 00067 unsigned& nf , 00068 unsigned*& fset 00069 ) 00070 { 00071 std::filebuf fb ; 00072 00073 assert( fb.open( _fname.c_str() , std::ios::in ) != 0 ) ; 00074 00075 std::istream istr( &fb ) ; 00076 00077 _is = new Lexer( istr ) ; 00078 00079 vset = 0 ; 00080 fset = 0 ; 00081 00082 read_header( nv , nf ) ; 00083 00084 read_vertices( nv , vset ) ; 00085 00086 read_faces( nf , nv , fset ) ; 00087 00088 fb.close(); 00089 } 00090 00091 00101 void 00102 Reader::read_header( 00103 unsigned& nv, 00104 unsigned& nf 00105 ) 00106 { 00107 // 00108 // The first line should start with the string "OFF". 00109 // 00110 std::string stroff; 00111 00112 assert( _is->get_string( stroff ) ) ; 00113 00114 assert( stroff == "OFF" ) ; 00115 00116 // 00117 // Read in the number of vertices of the mesh. 00118 // 00119 int number ; 00120 00121 assert ( _is->get_integer( number ) ) ; 00122 00123 nv = unsigned( number ) ; 00124 00125 assert( nv >= 4 ) ; 00126 00127 // 00128 // Read in the number of faces of the mesh. 00129 // 00130 00131 assert( _is->get_integer( number ) ) ; 00132 00133 nf = unsigned( number ) ; 00134 00135 assert( nf >= 4 ) ; 00136 00137 // 00138 // Read in the number of edges of the surface. 00139 // 00140 00141 assert( _is->get_integer( number ) ) ; 00142 00147 return ; 00148 } 00149 00150 00160 void 00161 Reader::read_vertices( 00162 unsigned nv , 00163 double*& vset 00164 ) 00165 { 00166 // 00167 // Allocate memory for storing the vertices. 00168 // 00169 vset = new double[ 3 * nv ] ; 00170 00171 // 00172 // Read "nv" lines with the vertex coordinates 00173 // 00174 for ( unsigned i = 0 ; i < nv ; i++ ) { 00175 // 00176 // Read in the first coordinate. 00177 // 00178 double x ; 00179 00180 assert( _is->get_double( x ) ) ; 00181 00182 // 00183 // Read in the second coordinate. 00184 // 00185 double y ; 00186 00187 assert( _is->get_double( y ) ) ; 00188 00189 // 00190 // Read in the third coordinate. 00191 // 00192 double z ; 00193 00194 assert( _is->get_double( z ) ) ; 00195 00196 const unsigned j = 3 * i ; 00197 00198 vset[ j ] = x ; 00199 vset[ j + 1 ] = y ; 00200 vset[ j + 2 ] = z ; 00201 } 00202 00203 return ; 00204 } 00205 00206 00217 void 00218 Reader::read_faces( 00219 unsigned nf, 00220 unsigned nv, 00221 unsigned*& fset 00222 ) 00223 { 00224 // 00225 // Allocate memory for storing the vertices. 00226 // 00227 fset = new unsigned[ 3 * nf ] ; 00228 00229 // 00230 // Read "nf" lines of the form "3 v1 v2 v3", where "v1", "v2", and 00231 // "v3" are the vertex indices of the face defined by these 00232 // vertices. 00233 // 00234 for ( unsigned i = 0 ; i < nf ; i++ ) { 00235 // 00236 // Read in the number of vertices of the i-th face. 00237 // 00238 int number ; 00239 00240 assert( _is->get_integer( number ) ) ; 00241 00242 assert( number == 3 ) ; 00243 00244 // 00245 // Read in the identifier of the first face vertex. 00246 // 00247 00248 assert( _is->get_integer( number ) ) ; 00249 00250 unsigned i1 = unsigned( number ) ; 00251 00252 assert( i1 < nv ) ; 00253 00254 // 00255 // Read in the identifier of the second face vertex. 00256 // 00257 00258 assert( _is->get_integer( number ) ) ; 00259 00260 unsigned i2 = unsigned( number ) ; 00261 00262 assert( i2 < nv ) ; 00263 00264 // 00265 // Read in the identifier of the third face vertex. 00266 // 00267 00268 assert( _is->get_integer( number ) ) ; 00269 00270 unsigned i3 = unsigned( number ) ; 00271 00272 assert( i3 < nv ) ; 00273 00274 // 00275 // Store face data. 00276 // 00277 const unsigned j = 3 * i ; 00278 00279 fset[ j ] = i1 ; 00280 fset[ j + 1 ] = i2 ; 00281 fset[ j + 2 ] = i3 ; 00282 } 00283 00284 return ; 00285 } 00286 00287 } 00288 //end of group class.