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 "
152 if (options_.vertex_has_color() && userOptions_.vertex_has_color()) _opt +=
Options::VertexColor;
153 if (options_.face_has_color() && userOptions_.face_has_color()) _opt +=
Options::FaceColor;
157 if ( options_.is_binary() && userOptions_.color_has_alpha() )
160 return (options_.is_binary() ?
161 read_binary(_in, _bi, _opt, swap_required) :
162 read_ascii(_in, _bi, _opt));
175 unsigned int i, j, k, l, idx;
176 unsigned int nV, nF, dummy;
183 BaseImporter::VHandles vhandles;
185 std::stringstream stream;
190 std::getline(_in,header);
197 _bi.reserve(nV, 3*nV, nF);
200 for (i=0; i<nV && !_in.eof(); ++i)
203 _in >> v[0]; _in >> v[1]; _in >> v[2];
205 vh = _bi.add_vertex(v);
208 if ( options_.vertex_has_normal() ){
210 _in >> n[0]; _in >> n[1]; _in >> n[2];
212 if ( userOptions_.vertex_has_normal() )
213 _bi.set_normal(vh, n);
218 std::getline(_in,line);
220 int colorType = getColorType(line, options_.vertex_has_texcoord() );
226 if ( options_.vertex_has_color() ){
230 case 1 : stream >> trash;
break;
231 case 2 : stream >> trash; stream >> trash;
break;
233 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
234 if ( userOptions_.vertex_has_color() )
235 _bi.set_color( vh,
Vec3uc( c3 ) );
238 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
239 if ( userOptions_.vertex_has_color() )
240 _bi.set_color( vh,
Vec4uc( c4 ) );
243 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
244 if ( userOptions_.vertex_has_color() ) {
245 _bi.set_color( vh, c3f );
250 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
251 if ( userOptions_.vertex_has_color() ) {
252 _bi.set_color( vh, c4f );
258 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
263 if ( options_.vertex_has_texcoord() ){
264 stream >> t[0]; stream >> t[1];
265 if ( userOptions_.vertex_has_texcoord() )
266 _bi.set_texcoord(vh, t);
284 vhandles[0] = VertexHandle(j);
285 vhandles[1] = VertexHandle(k);
286 vhandles[2] = VertexHandle(l);
294 vhandles.push_back(VertexHandle(idx));
298 FaceHandle fh = _bi.add_face(vhandles);
301 if ( options_.face_has_color() ){
305 std::getline(_in,line);
307 int colorType = getColorType(line,
false );
314 case 1 : stream >> trash;
break;
315 case 2 : stream >> trash; stream >> trash;
break;
317 case 3 : stream >> c3[0]; stream >> c3[1]; stream >> c3[2];
318 if ( userOptions_.face_has_color() )
319 _bi.set_color( fh,
Vec3uc( c3 ) );
322 case 4 : stream >> c4[0]; stream >> c4[1]; stream >> c4[2]; stream >> c4[3];
323 if ( userOptions_.face_has_color() )
324 _bi.set_color( fh,
Vec4uc( c4 ) );
327 case 5 : stream >> c3f[0]; stream >> c3f[1]; stream >> c3f[2];
328 if ( userOptions_.face_has_color() ) {
329 _bi.set_color( fh, c3f );
334 case 6 : stream >> c4f[0]; stream >> c4f[1]; stream >> c4f[2]; stream >> c4f[3];
335 if ( userOptions_.face_has_color() ) {
336 _bi.set_color( fh, c4f );
342 std::cerr <<
"Error in file format (colorType = " << colorType <<
")\n";
355int _OFFReader_::getColorType(std::string& _line,
bool _texCoordsAvailable)
const
368 if ( _line.size() < 1 )
372 while (_line.size() > 0 && std::isspace(_line[0]))
373 _line = _line.substr(1);
374 while (_line.size() > 0 && std::isspace(_line[ _line.length()-1 ]))
375 _line = _line.substr(0, _line.length()-1);
381 found=_line.find_first_of(
" ");
382 while (found!=std::string::npos){
384 found=_line.find_first_of(
" ",found+1);
387 if (!_line.empty()) count++;
389 if (_texCoordsAvailable) count -= 2;
391 if (count == 3 || count == 4){
393 found = _line.find(
" ");
394 std::string c1 = _line.substr (0,found);
396 if (c1.find(
".") != std::string::npos){
406void _OFFReader_::readValue(std::istream& _in,
float& _value)
const{
409 restore( _in , tmp,
false );
413void _OFFReader_::readValue(std::istream& _in,
int& _value)
const{
416 restore( _in , tmp,
false );
420void _OFFReader_::readValue(std::istream& _in,
unsigned int& _value)
const{
423 restore( _in , tmp,
false );
428_OFFReader_::read_binary(std::istream& _in,
BaseImporter& _bi, Options& _opt,
bool )
const
430 unsigned int i, j, k, l, idx;
431 unsigned int nV, nF, dummy;
438 BaseImporter::VHandles vhandles;
443 std::getline(_in,header);
448 readValue(_in, dummy);
450 _bi.reserve(nV, 3*nV, nF);
453 for (i=0; i<nV && !_in.eof(); ++i)
456 readValue(_in, v[0]);
457 readValue(_in, v[1]);
458 readValue(_in, v[2]);
460 vh = _bi.add_vertex(v);
462 if ( options_.vertex_has_normal() ) {
463 readValue(_in, n[0]);
464 readValue(_in, n[1]);
465 readValue(_in, n[2]);
467 if ( userOptions_.vertex_has_normal() )
468 _bi.set_normal(vh, n);
471 if ( options_.vertex_has_color() ) {
472 if ( userOptions_.color_is_float() ) {
475 if ( options_.color_has_alpha() ){
476 readValue(_in, cAf[0]);
477 readValue(_in, cAf[1]);
478 readValue(_in, cAf[2]);
479 readValue(_in, cAf[3]);
481 if ( userOptions_.vertex_has_color() )
482 _bi.set_color( vh, cAf );
486 readValue(_in, cf[0]);
487 readValue(_in, cf[1]);
488 readValue(_in, cf[2]);
490 if ( userOptions_.vertex_has_color() )
491 _bi.set_color( vh, cf );
495 if ( options_.color_has_alpha() ){
496 readValue(_in, cA[0]);
497 readValue(_in, cA[1]);
498 readValue(_in, cA[2]);
499 readValue(_in, cA[3]);
501 if ( userOptions_.vertex_has_color() )
502 _bi.set_color( vh,
Vec4uc( cA ) );
505 readValue(_in, c[0]);
506 readValue(_in, c[1]);
507 readValue(_in, c[2]);
509 if ( userOptions_.vertex_has_color() )
510 _bi.set_color( vh,
Vec3uc( c ) );
515 if ( options_.vertex_has_texcoord()) {
516 readValue(_in, t[0]);
517 readValue(_in, t[1]);
519 if ( userOptions_.vertex_has_texcoord() )
520 _bi.set_texcoord(vh, t);
538 vhandles[0] = VertexHandle(j);
539 vhandles[1] = VertexHandle(k);
540 vhandles[2] = VertexHandle(l);
546 vhandles.push_back(VertexHandle(idx));
550 FaceHandle fh = _bi.add_face(vhandles);
553 if ( _opt.face_has_color() ) {
554 if ( userOptions_.color_is_float() ) {
557 if ( options_.color_has_alpha() ){
558 readValue(_in, cAf[0]);
559 readValue(_in, cAf[1]);
560 readValue(_in, cAf[2]);
561 readValue(_in, cAf[3]);
563 if ( userOptions_.face_has_color() )
564 _bi.set_color( fh , cAf );
567 readValue(_in, cf[0]);
568 readValue(_in, cf[1]);
569 readValue(_in, cf[2]);
571 if ( userOptions_.face_has_color() )
572 _bi.set_color( fh, cf );
576 if ( options_.color_has_alpha() ){
577 readValue(_in, cA[0]);
578 readValue(_in, cA[1]);
579 readValue(_in, cA[2]);
580 readValue(_in, cA[3]);
582 if ( userOptions_.face_has_color() )
583 _bi.set_color( fh ,
Vec4uc( cA ) );
586 readValue(_in, c[0]);
587 readValue(_in, c[1]);
588 readValue(_in, c[2]);
590 if ( userOptions_.face_has_color() )
591 _bi.set_color( fh,
Vec3uc( c ) );
613 std::ifstream ifs(_filename.c_str());
632 char line[LINE_LEN], *p;
633 _is.getline(line, LINE_LEN);
636 std::streamsize remainingChars = _is.gcount();
638 bool vertexDimensionTooHigh =
false;
642 if ( ( remainingChars > 1 ) && ( p[0] ==
'S' && p[1] ==
'T') )
645 if ( ( remainingChars > 0 ) && ( p[0] ==
'C') )
649 if ( ( remainingChars > 0 ) && ( p[0] ==
'N') )
652 if ( ( remainingChars > 0 ) && (p[0] ==
'4' ) )
653 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
655 if ( ( remainingChars > 0 ) && ( p[0] ==
'n') )
656 { vertexDimensionTooHigh =
true; ++p; --remainingChars; }
658 if ( ( remainingChars < 3 ) || (!(p[0] ==
'O' && p[1] ==
'F' && p[2] ==
'F') ) )
664 if ( remainingChars >= 4 )
669 if ( ( remainingChars >= 6 ) && ( strncmp(p,
"BINARY", 6) == 0 ) )
673 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...
Set options for reader/writer modules.
void cleanup(void)
Restore state after default constructor.
void clear(void)
Clear all bits.
@ ColorFloat
Has (r) / store (w) float values for colors (currently only implemented for PLY and OFF files)
@ Swap
Swap byte order in binary mode.
@ FaceColor
Has (r) / store (w) face colors.
@ Binary
Set binary mode for r/w.
@ ColorAlpha
Has (r) / store (w) alpha values for colors.
@ VertexNormal
Has (r) / store (w) vertex normals.
@ VertexTexCoord
Has (r) / store (w) texture coordinates.
@ VertexColor
Has (r) / store (w) vertex colors.
bool register_module(BaseReader *_bl)
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...
bool read(const std::string &_filename, BaseImporter &_bi, Options &_opt) override
_OFFReader_ __OFFReaderInstance
Declare the single entity of the OFF reader.
_IOManager_ & IOManager()
VectorT< unsigned char, 3 > Vec3uc
VectorT< unsigned char, 4 > Vec4uc
Handle for a vertex entity.