53 #include <OpenMesh/Core/IO/reader/OFFReader.hh> 54 #include <OpenMesh/Core/IO/IOManager.hh> 55 #include <OpenMesh/Core/Utils/color_cast.hh> 64 #if defined(OM_CC_MIPS) 67 #elif defined(_STLPORT_VERSION) && (_STLPORT_VERSION==0x460) 96 _OFFReader_::_OFFReader_()
109 std::ifstream ifile(_filename.c_str(), (options_.is_binary() ? std::ios::binary | std::ios::in
112 if (!ifile.is_open() || !ifile.good())
114 omerr() <<
"[OFFReader] : cannot not open file " 123 bool result =
read(ifile, _bi, _opt);
137 omerr() <<
"[OMReader] : cannot not use stream " 153 if (options_.vertex_has_color() && userOptions_.vertex_has_color()) _opt +=
Options::VertexColor;
154 if (options_.face_has_color() && userOptions_.face_has_color()) _opt +=
Options::FaceColor;
158 if ( options_.is_binary() && userOptions_.color_has_alpha() )
161 return (options_.is_binary() ?
162 read_binary(_in, _bi, _opt, swap) :
163 read_ascii(_in, _bi, _opt));
176 unsigned int i, j, k, l, idx;
177 unsigned int nV, nF, dummy;
184 BaseImporter::VHandles vhandles;
186 std::stringstream stream;
191 std::getline(_in,header);
198 _bi.reserve(nV, 3*nV, nF);
201 for (i=0; i<nV && !_in.eof(); ++i)
204 _in >> v[0]; _in >> v[1]; _in >> v[2];
206 vh = _bi.add_vertex(v);
209 if ( options_.vertex_has_normal() ){
211 _in >> n[0]; _in >> n[1]; _in >> n[2];
213 if ( userOptions_.vertex_has_normal() )
214 _bi.set_normal(vh, n);
219 std::getline(_in,line);
221 int colorType = getColorType(line, options_.vertex_has_texcoord() );
227 if ( options_.vertex_has_color() ){
231 case 1 : stream >> trash;
break;
232 case 2 : stream >> trash; stream >> trash;
break;
234 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
235 if ( userOptions_.vertex_has_color() )
236 _bi.set_color( vh,
Vec3uc( c3 ) );
239 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
240 if ( userOptions_.vertex_has_color() )
241 _bi.set_color( vh,
Vec4uc( c4 ) );
244 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
245 if ( userOptions_.vertex_has_color() ) {
246 _bi.set_color( vh, c3f );
251 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
252 if ( userOptions_.vertex_has_color() ) {
253 _bi.set_color( vh, c4f );
259 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
264 if ( options_.vertex_has_texcoord() ){
265 stream >> t[0]; stream >> t[1];
266 if ( userOptions_.vertex_has_texcoord() )
267 _bi.set_texcoord(vh, t);
302 if ( options_.face_has_color() ){
306 std::getline(_in,line);
308 int colorType = getColorType(line,
false );
315 case 1 : stream >> trash;
break;
316 case 2 : stream >> trash; stream >> trash;
break;
318 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
319 if ( userOptions_.face_has_color() )
320 _bi.set_color( fh,
Vec3uc( c3 ) );
323 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
324 if ( userOptions_.face_has_color() )
325 _bi.set_color( fh,
Vec4uc( c4 ) );
328 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
329 if ( userOptions_.face_has_color() ) {
330 _bi.set_color( fh, c3f );
335 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
336 if ( userOptions_.face_has_color() ) {
337 _bi.set_color( fh, c4f );
343 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
356 int _OFFReader_::getColorType(std::string& _line,
bool _texCoordsAvailable)
const 369 if ( _line.size() < 1 )
373 while (_line.size() > 0 && std::isspace(_line[0]))
374 _line = _line.substr(1);
375 while (_line.size() > 0 && std::isspace(_line[ _line.length()-1 ]))
376 _line = _line.substr(0, _line.length()-1);
382 found=_line.find_first_of(
" ");
383 while (found!=std::string::npos){
385 found=_line.find_first_of(
" ",found+1);
388 if (!_line.empty()) count++;
390 if (_texCoordsAvailable) count -= 2;
392 if (count == 3 || count == 4){
394 found = _line.find(
" ");
395 std::string c1 = _line.substr (0,found);
397 if (c1.find(
".") != std::string::npos){
407 void _OFFReader_::readValue(std::istream& _in,
float& _value)
const{
410 restore( _in , tmp,
false );
414 void _OFFReader_::readValue(std::istream& _in,
int& _value)
const{
417 restore( _in , tmp,
false );
421 void _OFFReader_::readValue(std::istream& _in,
unsigned int& _value)
const{
424 restore( _in , tmp,
false );
431 unsigned int i, j, k, l, idx;
432 unsigned int nV, nF, dummy;
439 BaseImporter::VHandles vhandles;
444 std::getline(_in,header);
449 readValue(_in, dummy);
451 _bi.reserve(nV, 3*nV, nF);
454 for (i=0; i<nV && !_in.eof(); ++i)
457 readValue(_in, v[0]);
458 readValue(_in, v[1]);
459 readValue(_in, v[2]);
461 vh = _bi.add_vertex(v);
463 if ( options_.vertex_has_normal() ) {
464 readValue(_in, n[0]);
465 readValue(_in, n[1]);
466 readValue(_in, n[2]);
468 if ( userOptions_.vertex_has_normal() )
469 _bi.set_normal(vh, n);
472 if ( options_.vertex_has_color() ) {
473 if ( userOptions_.color_is_float() ) {
476 if ( options_.color_has_alpha() ){
477 readValue(_in, cAf[0]);
478 readValue(_in, cAf[1]);
479 readValue(_in, cAf[2]);
480 readValue(_in, cAf[3]);
482 if ( userOptions_.vertex_has_color() )
483 _bi.set_color( vh, cAf );
487 readValue(_in, cf[0]);
488 readValue(_in, cf[1]);
489 readValue(_in, cf[2]);
491 if ( userOptions_.vertex_has_color() )
492 _bi.set_color( vh, cf );
496 if ( options_.color_has_alpha() ){
497 readValue(_in, cA[0]);
498 readValue(_in, cA[1]);
499 readValue(_in, cA[2]);
500 readValue(_in, cA[3]);
502 if ( userOptions_.vertex_has_color() )
503 _bi.set_color( vh,
Vec4uc( cA ) );
506 readValue(_in, c[0]);
507 readValue(_in, c[1]);
508 readValue(_in, c[2]);
510 if ( userOptions_.vertex_has_color() )
511 _bi.set_color( vh,
Vec3uc( c ) );
516 if ( options_.vertex_has_texcoord()) {
517 readValue(_in, t[0]);
518 readValue(_in, t[1]);
520 if ( userOptions_.vertex_has_texcoord() )
521 _bi.set_texcoord(vh, t);
554 if ( _opt.face_has_color() ) {
555 if ( userOptions_.color_is_float() ) {
558 if ( options_.color_has_alpha() ){
559 readValue(_in, cAf[0]);
560 readValue(_in, cAf[1]);
561 readValue(_in, cAf[2]);
562 readValue(_in, cAf[3]);
564 if ( userOptions_.face_has_color() )
565 _bi.set_color( fh , cAf );
568 readValue(_in, cf[0]);
569 readValue(_in, cf[1]);
570 readValue(_in, cf[2]);
572 if ( userOptions_.face_has_color() )
573 _bi.set_color( fh, cf );
577 if ( options_.color_has_alpha() ){
578 readValue(_in, cA[0]);
579 readValue(_in, cA[1]);
580 readValue(_in, cA[2]);
581 readValue(_in, cA[3]);
583 if ( userOptions_.face_has_color() )
584 _bi.set_color( fh ,
Vec4uc( cA ) );
587 readValue(_in, c[0]);
588 readValue(_in, c[1]);
589 readValue(_in, c[2]);
591 if ( userOptions_.face_has_color() )
592 _bi.set_color( fh,
Vec3uc( c ) );
614 std::ifstream ifs(_filename.c_str());
633 char line[LINE_LEN], *p;
634 _is.getline(line, LINE_LEN);
637 std::streamsize remainingChars = _is.gcount();
639 bool vertexDimensionTooHigh =
false;
643 if ( ( remainingChars > 1 ) && ( p[0] ==
'S' && p[1] ==
'T') )
646 if ( ( remainingChars > 0 ) && ( p[0] ==
'C') )
650 if ( ( remainingChars > 0 ) && ( p[0] ==
'N') )
653 if ( ( remainingChars > 0 ) && (p[0] ==
'4' ) )
654 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
656 if ( ( remainingChars > 0 ) && ( p[0] ==
'n') )
657 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
659 if ( ( remainingChars < 3 ) || (!(p[0] ==
'O' && p[1] ==
'F' && p[2] ==
'F') ) )
665 if ( remainingChars >= 4 )
670 if ( ( remainingChars >= 6 ) && ( strncmp(p,
"BINARY", 6) == 0 ) )
674 if (vertexDimensionTooHigh)
Swap byte order in binary mode.
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...
Handle for a face entity.
Has (r) / store (w) vertex colors.
void clear(void)
Clear all bits.
Has (r) / store (w) face colors.
Has (r) / store (w) float values for colors (currently only implemented for PLY and OFF files) ...
Handle for a vertex entity.
Has (r) / store (w) alpha values for colors.
bool register_module(BaseReader *_bl)
bool read(const std::string &_filename, BaseImporter &_bi, Options &_opt) override
void cleanup(void)
Restore state after default constructor.
Set options for reader/writer modules.
bool can_u_read(const std::string &_filename) const override
Returns true if writer can parse _filename (checks extension). _filename can also provide an extensio...
Has (r) / store (w) vertex normals.
_IOManager_ & IOManager()
_OFFReader_ __OFFReaderInstance
Declare the single entity of the OFF reader.
Has (r) / store (w) texture coordinates.