58 #include <OpenMesh/Core/IO/reader/OFFReader.hh> 59 #include <OpenMesh/Core/IO/IOManager.hh> 60 #include <OpenMesh/Core/Utils/color_cast.hh> 69 #if defined(OM_CC_MIPS) 72 #elif defined(_STLPORT_VERSION) && (_STLPORT_VERSION==0x460) 101 _OFFReader_::_OFFReader_()
114 std::ifstream ifile(_filename.c_str(), (options_.is_binary() ? std::ios::binary | std::ios::in
117 if (!ifile.is_open() || !ifile.good())
119 omerr() <<
"[OFFReader] : cannot not open file " 128 bool result =
read(ifile, _bi, _opt);
142 omerr() <<
"[OMReader] : cannot not use stream " 158 if (options_.vertex_has_color() && userOptions_.vertex_has_color()) _opt +=
Options::VertexColor;
159 if (options_.face_has_color() && userOptions_.face_has_color()) _opt +=
Options::FaceColor;
163 if ( options_.is_binary() && userOptions_.color_has_alpha() )
166 return (options_.is_binary() ?
167 read_binary(_in, _bi, _opt, swap) :
168 read_ascii(_in, _bi, _opt));
181 unsigned int i, j, k, l, idx;
182 unsigned int nV, nF, dummy;
189 BaseImporter::VHandles vhandles;
191 std::stringstream stream;
196 std::getline(_in,header);
203 _bi.reserve(nV, 3*nV, nF);
206 for (i=0; i<nV && !_in.eof(); ++i)
209 _in >> v[0]; _in >> v[1]; _in >> v[2];
211 vh = _bi.add_vertex(v);
214 if ( options_.vertex_has_normal() ){
216 _in >> n[0]; _in >> n[1]; _in >> n[2];
218 if ( userOptions_.vertex_has_normal() )
219 _bi.set_normal(vh, n);
224 std::getline(_in,line);
226 int colorType = getColorType(line, options_.vertex_has_texcoord() );
232 if ( options_.vertex_has_color() ){
236 case 1 : stream >> trash;
break;
237 case 2 : stream >> trash; stream >> trash;
break;
239 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
240 if ( userOptions_.vertex_has_color() )
241 _bi.set_color( vh,
Vec3uc( c3 ) );
244 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
245 if ( userOptions_.vertex_has_color() )
246 _bi.set_color( vh,
Vec4uc( c4 ) );
249 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
250 if ( userOptions_.vertex_has_color() ) {
251 _bi.set_color( vh, c3f );
256 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
257 if ( userOptions_.vertex_has_color() ) {
258 _bi.set_color( vh, c4f );
264 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
269 if ( options_.vertex_has_texcoord() ){
270 stream >> t[0]; stream >> t[1];
271 if ( userOptions_.vertex_has_texcoord() )
272 _bi.set_texcoord(vh, t);
307 if ( options_.face_has_color() ){
311 std::getline(_in,line);
313 int colorType = getColorType(line,
false );
320 case 1 : stream >> trash;
break;
321 case 2 : stream >> trash; stream >> trash;
break;
323 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
324 if ( userOptions_.face_has_color() )
325 _bi.set_color( fh,
Vec3uc( c3 ) );
328 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
329 if ( userOptions_.face_has_color() )
330 _bi.set_color( fh,
Vec4uc( c4 ) );
333 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
334 if ( userOptions_.face_has_color() ) {
335 _bi.set_color( fh, c3f );
340 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
341 if ( userOptions_.face_has_color() ) {
342 _bi.set_color( fh, c4f );
348 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
361 int _OFFReader_::getColorType(std::string& _line,
bool _texCoordsAvailable)
const 374 if ( _line.size() < 1 )
378 while (_line.size() > 0 && std::isspace(_line[0]))
379 _line = _line.substr(1);
380 while (_line.size() > 0 && std::isspace(_line[ _line.length()-1 ]))
381 _line = _line.substr(0, _line.length()-1);
387 found=_line.find_first_of(
" ");
388 while (found!=std::string::npos){
390 found=_line.find_first_of(
" ",found+1);
393 if (!_line.empty()) count++;
395 if (_texCoordsAvailable) count -= 2;
397 if (count == 3 || count == 4){
399 found = _line.find(
" ");
400 std::string c1 = _line.substr (0,found);
402 if (c1.find(
".") != std::string::npos){
412 void _OFFReader_::readValue(std::istream& _in,
float& _value)
const{
415 restore( _in , tmp,
false );
419 void _OFFReader_::readValue(std::istream& _in,
int& _value)
const{
422 restore( _in , tmp,
false );
426 void _OFFReader_::readValue(std::istream& _in,
unsigned int& _value)
const{
429 restore( _in , tmp,
false );
436 unsigned int i, j, k, l, idx;
437 unsigned int nV, nF, dummy;
444 BaseImporter::VHandles vhandles;
449 std::getline(_in,header);
454 readValue(_in, dummy);
456 _bi.reserve(nV, 3*nV, nF);
459 for (i=0; i<nV && !_in.eof(); ++i)
462 readValue(_in, v[0]);
463 readValue(_in, v[1]);
464 readValue(_in, v[2]);
466 vh = _bi.add_vertex(v);
468 if ( options_.vertex_has_normal() ) {
469 readValue(_in, n[0]);
470 readValue(_in, n[1]);
471 readValue(_in, n[2]);
473 if ( userOptions_.vertex_has_normal() )
474 _bi.set_normal(vh, n);
477 if ( options_.vertex_has_color() ) {
478 if ( userOptions_.color_is_float() ) {
481 if ( options_.color_has_alpha() ){
482 readValue(_in, cAf[0]);
483 readValue(_in, cAf[1]);
484 readValue(_in, cAf[2]);
485 readValue(_in, cAf[3]);
487 if ( userOptions_.vertex_has_color() )
488 _bi.set_color( vh, cAf );
492 readValue(_in, cf[0]);
493 readValue(_in, cf[1]);
494 readValue(_in, cf[2]);
496 if ( userOptions_.vertex_has_color() )
497 _bi.set_color( vh, cf );
501 if ( options_.color_has_alpha() ){
502 readValue(_in, cA[0]);
503 readValue(_in, cA[1]);
504 readValue(_in, cA[2]);
505 readValue(_in, cA[3]);
507 if ( userOptions_.vertex_has_color() )
508 _bi.set_color( vh,
Vec4uc( cA ) );
511 readValue(_in, c[0]);
512 readValue(_in, c[1]);
513 readValue(_in, c[2]);
515 if ( userOptions_.vertex_has_color() )
516 _bi.set_color( vh,
Vec3uc( c ) );
521 if ( options_.vertex_has_texcoord()) {
522 readValue(_in, t[0]);
523 readValue(_in, t[1]);
525 if ( userOptions_.vertex_has_texcoord() )
526 _bi.set_texcoord(vh, t);
559 if ( _opt.face_has_color() ) {
560 if ( userOptions_.color_is_float() ) {
563 if ( options_.color_has_alpha() ){
564 readValue(_in, cAf[0]);
565 readValue(_in, cAf[1]);
566 readValue(_in, cAf[2]);
567 readValue(_in, cAf[3]);
569 if ( userOptions_.face_has_color() )
570 _bi.set_color( fh , cAf );
573 readValue(_in, cf[0]);
574 readValue(_in, cf[1]);
575 readValue(_in, cf[2]);
577 if ( userOptions_.face_has_color() )
578 _bi.set_color( fh, cf );
582 if ( options_.color_has_alpha() ){
583 readValue(_in, cA[0]);
584 readValue(_in, cA[1]);
585 readValue(_in, cA[2]);
586 readValue(_in, cA[3]);
588 if ( userOptions_.face_has_color() )
589 _bi.set_color( fh ,
Vec4uc( cA ) );
592 readValue(_in, c[0]);
593 readValue(_in, c[1]);
594 readValue(_in, c[2]);
596 if ( userOptions_.face_has_color() )
597 _bi.set_color( fh,
Vec3uc( c ) );
619 std::ifstream ifs(_filename.c_str());
638 char line[LINE_LEN], *p;
639 _is.getline(line, LINE_LEN);
642 std::streamsize remainingChars = _is.gcount();
644 bool vertexDimensionTooHigh =
false;
648 if ( ( remainingChars > 1 ) && ( p[0] ==
'S' && p[1] ==
'T') )
651 if ( ( remainingChars > 0 ) && ( p[0] ==
'C') )
655 if ( ( remainingChars > 0 ) && ( p[0] ==
'N') )
658 if ( ( remainingChars > 0 ) && (p[0] ==
'4' ) )
659 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
661 if ( ( remainingChars > 0 ) && ( p[0] ==
'n') )
662 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
664 if ( ( remainingChars < 3 ) || (!(p[0] ==
'O' && p[1] ==
'F' && p[2] ==
'F') ) )
670 if ( remainingChars >= 4 )
675 if ( ( remainingChars >= 6 ) && ( strncmp(p,
"BINARY", 6) == 0 ) )
679 if (vertexDimensionTooHigh)
virtual bool can_u_read(const std::string &_filename) const
Returns true if writer can parse _filename (checks extension). _filename can also provide an extensio...
Has (r) / store (w) texture coordinates.
Has (r) / store (w) alpha values for colors.
Has (r) / store (w) vertex normals.
_OFFReader_ __OFFReaderInstance
Declare the single entity of the OFF reader.
void clear(void)
Clear all bits.
bool read(const std::string &_filename, BaseImporter &_bi, Options &_opt)
bool can_u_read(const std::string &_filename) const
Returns true if writer can parse _filename (checks extension). _filename can also provide an extensio...
Has (r) / store (w) float values for colors (currently only implemented for PLY and OFF files) ...
Set options for reader/writer modules.
Handle for a vertex entity.
bool register_module(BaseReader *_bl)
Swap byte order in binary mode.
Handle for a face entity.
Has (r) / store (w) face colors.
void cleanup(void)
Restore state after default constructor.
Has (r) / store (w) vertex colors.
_IOManager_ & IOManager()