PLYReader.cc 36.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5 6
 *                           www.openmesh.org                                *
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
7
 *---------------------------------------------------------------------------*
8
 *  This file is part of OpenMesh.                                           *
Jan Möbius's avatar
Jan Möbius committed
9
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
10
 *  OpenMesh is free software: you can redistribute it and/or modify         *
11 12 13 14
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
Jan Möbius's avatar
Jan Möbius committed
15
 *                                                                           *
16 17 18 19 20 21 22
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
24 25 26 27
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
Jan Möbius's avatar
Jan Möbius committed
28
 *                                                                           *
29 30 31 32
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
33
 \*===========================================================================*/
34 35

/*===========================================================================*\
Mike Kremer's avatar
Mike Kremer committed
36
 *                                                                           *
37 38
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
39
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
40
 \*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

#define LINE_LEN 4096

//== INCLUDES =================================================================

// OpenMesh
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/IO/reader/PLYReader.hh>
#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>
#include <OpenMesh/Core/IO/SR_store.hh>

//STL
#include <fstream>
#include <memory>

#ifndef WIN32
#include <string.h>
#endif

//=== NAMESPACES ==============================================================


namespace OpenMesh {
namespace IO {

//=============================================================================

//=== INSTANCIATE =============================================================


Mike Kremer's avatar
Mike Kremer committed
74 75
_PLYReader_ __PLYReaderInstance;
_PLYReader_& PLYReader() {
76
    return __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
77
}
Jan Möbius's avatar
Jan Möbius committed
78 79 80 81

//=== IMPLEMENTATION ==========================================================


Mike Kremer's avatar
Mike Kremer committed
82
_PLYReader_::_PLYReader_() {
83
    IOManager().register_module(this);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    // Store sizes in byte of each property type
    scalar_size_[ValueTypeINT8] = 1;
    scalar_size_[ValueTypeUINT8] = 1;
    scalar_size_[ValueTypeINT16] = 2;
    scalar_size_[ValueTypeUINT16] = 2;
    scalar_size_[ValueTypeINT32] = 4;
    scalar_size_[ValueTypeUINT32] = 4;
    scalar_size_[ValueTypeFLOAT32] = 4;
    scalar_size_[ValueTypeFLOAT64] = 8;

    scalar_size_[ValueTypeCHAR] = 1;
    scalar_size_[ValueTypeUCHAR] = 1;
    scalar_size_[ValueTypeSHORT] = 2;
    scalar_size_[ValueTypeUSHORT] = 2;
    scalar_size_[ValueTypeINT] = 4;
    scalar_size_[ValueTypeUINT] = 4;
    scalar_size_[ValueTypeFLOAT] = 4;
    scalar_size_[ValueTypeDOUBLE] = 8;
Jan Möbius's avatar
Jan Möbius committed
103 104 105 106 107
}

//-----------------------------------------------------------------------------


Mike Kremer's avatar
Mike Kremer committed
108
bool _PLYReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) {
109

110
    std::fstream in(_filename.c_str(), (std::ios_base::binary | std::ios_base::in) );
Jan Möbius's avatar
Jan Möbius committed
111

112 113 114 115
    if (!in.is_open() || !in.good()) {
        omerr() << "[PLYReader] : cannot not open file " << _filename << std::endl;
        return false;
    }
Jan Möbius's avatar
Jan Möbius committed
116

117
    bool result = read(in, _bi, _opt);
Jan Möbius's avatar
Jan Möbius committed
118

119 120
    in.close();
    return result;
Jan Möbius's avatar
Jan Möbius committed
121 122 123 124 125
}

//-----------------------------------------------------------------------------


126 127 128 129 130 131 132
bool _PLYReader_::read(std::istream& _in, BaseImporter& _bi, Options& _opt) {

    if (!_in.good()) {
        omerr() << "[PLYReader] : cannot not use stream" << std::endl;
        return false;
    }

133 134
    // filter relevant options for reading
    bool swap = _opt.check(Options::Swap);
Jan Möbius's avatar
Jan Möbius committed
135

136
    userOptions_ = _opt;
Jan Möbius's avatar
Jan Möbius committed
137

138 139
    // build options to be returned
    _opt.clear();
Jan Möbius's avatar
Jan Möbius committed
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    if (options_.vertex_has_normal() && userOptions_.vertex_has_normal()) {
        _opt += Options::VertexNormal;
    }
    if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord()) {
        _opt += Options::VertexTexCoord;
    }
    if (options_.vertex_has_color() && userOptions_.vertex_has_color()) {
        _opt += Options::VertexColor;
    }
    if (options_.face_has_color() && userOptions_.face_has_color()) {
        _opt += Options::FaceColor;
    }
    if (options_.is_binary()) {
        _opt += Options::Binary;
    }
156 157 158
    if (options_.color_is_float()) {
        _opt += Options::ColorFloat;
    }
159 160 161
    if (options_.check(Options::Custom) && userOptions_.check(Options::Custom)) {
        _opt += Options::Custom;
    }
Jan Möbius's avatar
Jan Möbius committed
162

163 164 165
    //    //force user-choice for the alpha value when reading binary
    //    if ( options_.is_binary() && userOptions_.color_has_alpha() )
    //      options_ += Options::ColorAlpha;
Jan Möbius's avatar
Jan Möbius committed
166

167
    return (options_.is_binary() ? read_binary(_in, _bi, swap, _opt) : read_ascii(_in, _bi, _opt));
Jan Möbius's avatar
Jan Möbius committed
168 169 170

}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
template<typename T>
void assignCustomProperty(std::istream& _in, BaseImporter& _bi, VertexHandle _vh, const std::string& _propName)
{
  OpenMesh::VPropHandleT<T> prop;
  if (!_bi.kernel()->get_property_handle(prop,_propName))
    _bi.kernel()->add_property(prop,_propName);
  T in;
  _in >> in;
  _bi.kernel()->property(prop,_vh) = in;
}


void _PLYReader_::readCustomProperty(std::istream& _in, BaseImporter& _bi, VertexHandle _vh, const std::string& _propName, const ValueType _valueType) const
{
  switch (_valueType)
  {
  case ValueTypeINT8:
  case ValueTypeCHAR:
      assignCustomProperty<char>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeINT16:
  case ValueTypeSHORT:
      assignCustomProperty<short>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeUINT16:
  case ValueTypeUSHORT:
      assignCustomProperty<unsigned short>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeINT32:
  case ValueTypeINT:
      assignCustomProperty<int>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeUINT32:
  case ValueTypeUINT:
      assignCustomProperty<unsigned int>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeFLOAT32:
  case ValueTypeFLOAT:
      assignCustomProperty<float>(_in,_bi,_vh,_propName);
      break;
  case ValueTypeFLOAT64:
  case ValueTypeDOUBLE:
      assignCustomProperty<double>(_in,_bi,_vh,_propName);
      break;
  default:
      std::cerr << "unsupported type" << std::endl;
      break;
  }
}
220 221


Jan Möbius's avatar
Jan Möbius committed
222 223
//-----------------------------------------------------------------------------

224
bool _PLYReader_::read_ascii(std::istream& _in, BaseImporter& _bi, const Options& _opt) const {
Mike Kremer's avatar
Mike Kremer committed
225

226 227 228 229 230 231 232 233
    // Reparse the header
    if (!can_u_read(_in)) {
        omerr() << "[PLYReader] : Unable to parse header\n";
        return false;
    }

    unsigned int i, j, k, l, idx;
    unsigned int nV;
Jan Möbius's avatar
Jan Möbius committed
234
    OpenMesh::Vec3f v, n;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    std::string trash;
    OpenMesh::Vec2f t;
    OpenMesh::Vec4i c;
    float tmp;
    BaseImporter::VHandles vhandles;
    VertexHandle vh;

    _bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);

    if (vertexDimension_ != 3) {
        omerr() << "[PLYReader] : Only vertex dimension 3 is supported." << std::endl;
        return false;
    }

    // read vertices:
    for (i = 0; i < vertexCount_ && !_in.eof(); ++i) {
251 252
        vh = _bi.add_vertex();

253 254 255 256
        v[0] = 0.0;
        v[1] = 0.0;
        v[2] = 0.0;

Jan Möbius's avatar
Jan Möbius committed
257 258 259 260
        n[0] = 0.0;
        n[1] = 0.0;
        n[2] = 0.0;

261 262 263 264 265 266 267 268 269
        t[0] = 0.0;
        t[1] = 0.0;

        c[0] = 0;
        c[1] = 0;
        c[2] = 0;
        c[3] = 255;

        for (uint propertyIndex = 0; propertyIndex < vertexPropertyCount_; ++propertyIndex) {
270
            switch (vertexPropertyMap_[propertyIndex].property) {
271 272 273 274 275 276 277 278 279
            case XCOORD:
                _in >> v[0];
                break;
            case YCOORD:
                _in >> v[1];
                break;
            case ZCOORD:
                _in >> v[2];
                break;
Jan Möbius's avatar
Jan Möbius committed
280 281 282 283 284 285 286 287 288
            case XNORM:
                _in >> n[0];
                break;
            case YNORM:
                _in >> n[1];
                break;
            case ZNORM:
                _in >> n[2];
                break;
289 290 291 292 293 294 295
            case TEXX:
                _in >> t[0];
                break;
            case TEXY:
                _in >> t[1];
                break;
            case COLORRED:
296 297
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
298 299 300 301 302 303
                    _in >> tmp;
                    c[0] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
                    _in >> c[0];
                break;
            case COLORGREEN:
304 305
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
306 307 308 309 310 311
                    _in >> tmp;
                    c[1] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
                    _in >> c[1];
                break;
            case COLORBLUE:
312 313
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
314 315 316 317 318 319
                    _in >> tmp;
                    c[2] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
                    _in >> c[2];
                break;
            case COLORALPHA:
320 321
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
322 323 324 325 326
                    _in >> tmp;
                    c[3] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
                    _in >> c[3];
                break;
327 328 329 330 331 332
            case CUSTOM_PROP:
                if (_opt.check(Options::Custom))
                  readCustomProperty(_in, _bi, vh, vertexPropertyMap_[propertyIndex].name, vertexPropertyMap_[propertyIndex].value);
                else
                  _in >> trash;
                break;
333 334 335 336 337 338
            default:
                _in >> trash;
                break;
            }
        }

339
        _bi.set_point(vh, v);
340 341 342 343 344 345
        if (_opt.vertex_has_normal())
          _bi.set_normal(vh, n);
        if (_opt.vertex_has_texcoord())
          _bi.set_texcoord(vh, t);
        if (_opt.vertex_has_color())
          _bi.set_color(vh, Vec4uc(c));
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    }

    // faces
    // #N <v1> <v2> .. <v(n-1)> [color spec]
    for (i = 0; i < faceCount_; ++i) {
        // nV = number of Vertices for current face
        _in >> nV;

        if (nV == 3) {
            vhandles.resize(3);
            _in >> j;
            _in >> k;
            _in >> l;

            vhandles[0] = VertexHandle(j);
            vhandles[1] = VertexHandle(k);
            vhandles[2] = VertexHandle(l);
        } else {
            vhandles.clear();
            for (j = 0; j < nV; ++j) {
                _in >> idx;
                vhandles.push_back(VertexHandle(idx));
            }
        }

Jan Möbius's avatar
Jan Möbius committed
371
        _bi.add_face(vhandles);
372 373 374 375 376

    }

    // File was successfully parsed.
    return true;
Jan Möbius's avatar
Jan Möbius committed
377 378 379 380
}

//-----------------------------------------------------------------------------

381
bool _PLYReader_::read_binary(std::istream& _in, BaseImporter& _bi, bool /*_swap*/, const Options& _opt) const {
Mike Kremer's avatar
Mike Kremer committed
382

383 384 385 386 387 388 389 390
    // Reparse the header
    if (!can_u_read(_in)) {
        omerr() << "[PLYReader] : Unable to parse header\n";
        return false;
    }

    unsigned int i, j, k, l, idx;
    unsigned int nV;
391 392
    OpenMesh::Vec3f        v, n;  // Vertex
    OpenMesh::Vec2f        t;  // TexCoords
393
    BaseImporter::VHandles vhandles;
394 395 396
    VertexHandle           vh;
    OpenMesh::Vec4i        c;  // Color
    float                  tmp;
397 398 399 400 401 402 403 404 405

    _bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);

    // read vertices:
    for (i = 0; i < vertexCount_ && !_in.eof(); ++i) {
        v[0] = 0.0;
        v[1] = 0.0;
        v[2] = 0.0;

Jan Möbius's avatar
Jan Möbius committed
406 407 408 409
        n[0] = 0.0;
        n[1] = 0.0;
        n[2] = 0.0;

410 411 412 413 414 415 416 417 418
        t[0] = 0.0;
        t[1] = 0.0;

        c[0] = 0;
        c[1] = 0;
        c[2] = 0;
        c[3] = 255;

        for (uint propertyIndex = 0; propertyIndex < vertexPropertyCount_; ++propertyIndex) {
419
            switch (vertexPropertyMap_[propertyIndex].property) {
420
            case XCOORD:
421
                readValue(vertexPropertyMap_[propertyIndex].value, _in, v[0]);
422 423
                break;
            case YCOORD:
424
                readValue(vertexPropertyMap_[propertyIndex].value, _in, v[1]);
425 426
                break;
            case ZCOORD:
427
                readValue(vertexPropertyMap_[propertyIndex].value, _in, v[2]);
428
                break;
Jan Möbius's avatar
Jan Möbius committed
429
            case XNORM:
430
                readValue(vertexPropertyMap_[propertyIndex].value, _in, n[0]);
Jan Möbius's avatar
Jan Möbius committed
431 432
                break;
            case YNORM:
433
                readValue(vertexPropertyMap_[propertyIndex].value, _in, n[1]);
Jan Möbius's avatar
Jan Möbius committed
434 435
                break;
            case ZNORM:
436
                readValue(vertexPropertyMap_[propertyIndex].value, _in, n[2]);
Jan Möbius's avatar
Jan Möbius committed
437
                break;
438
            case TEXX:
439
                readValue(vertexPropertyMap_[propertyIndex].value, _in, t[0]);
440 441
                break;
            case TEXY:
442
                readValue(vertexPropertyMap_[propertyIndex].value, _in, t[1]);
443 444
                break;
            case COLORRED:
445 446 447
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
                    readValue(vertexPropertyMap_[propertyIndex].value, _in, tmp);
448 449 450

                    c[0] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
451
                    readInteger(vertexPropertyMap_[propertyIndex].value, _in, c[0]);
452

453 454
                break;
            case COLORGREEN:
455 456 457
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
                    readValue(vertexPropertyMap_[propertyIndex].value, _in, tmp);
458 459
                    c[1] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
460
                    readInteger(vertexPropertyMap_[propertyIndex].value, _in, c[1]);
461

462 463
                break;
            case COLORBLUE:
464 465 466
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
                    readValue(vertexPropertyMap_[propertyIndex].value, _in, tmp);
467 468
                    c[2] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
469
                    readInteger(vertexPropertyMap_[propertyIndex].value, _in, c[2]);
470

471 472
                break;
            case COLORALPHA:
473 474 475
                if (vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT32 ||
                    vertexPropertyMap_[propertyIndex].value == ValueTypeFLOAT) {
                    readValue(vertexPropertyMap_[propertyIndex].value, _in, tmp);
476 477
                    c[3] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
                } else
478
                    readInteger(vertexPropertyMap_[propertyIndex].value, _in, c[3]);
479

480 481
                break;
            default:
482
                // Read unsupported property
483
                consume_input(_in, scalar_size_[vertexPropertyMap_[propertyIndex].value]);
484 485 486 487 488 489
                break;
            }

        }

        vh = _bi.add_vertex(v);
490 491 492 493 494 495
        if (_opt.vertex_has_normal())
          _bi.set_normal(vh, n);
        if (_opt.vertex_has_texcoord())
          _bi.set_texcoord(vh, t);
        if (_opt.vertex_has_color())
          _bi.set_color(vh, Vec4uc(c));
496 497 498 499 500 501 502 503
    }

    for (i = 0; i < faceCount_; ++i) {
        // Read number of vertices for the current face
        readValue(faceIndexType_, _in, nV);

        if (nV == 3) {
            vhandles.resize(3);
504 505 506
            readInteger(faceEntryType_, _in, j);
            readInteger(faceEntryType_, _in, k);
            readInteger(faceEntryType_, _in, l);
507 508 509 510 511 512 513

            vhandles[0] = VertexHandle(j);
            vhandles[1] = VertexHandle(k);
            vhandles[2] = VertexHandle(l);
        } else {
            vhandles.clear();
            for (j = 0; j < nV; ++j) {
514
                readInteger(faceEntryType_, _in, idx);
515 516 517 518
                vhandles.push_back(VertexHandle(idx));
            }
        }

Jan Möbius's avatar
Jan Möbius committed
519
        _bi.add_face(vhandles);
520 521 522
    }

    return true;
Jan Möbius's avatar
Jan Möbius committed
523 524 525
}


526 527 528
//-----------------------------------------------------------------------------


529
void _PLYReader_::readValue(ValueType _type, std::istream& _in, float& _value) const {
530

531 532 533 534 535 536 537 538 539 540 541 542
    switch (_type) {
    case ValueTypeFLOAT32:
    case ValueTypeFLOAT:
        float32_t tmp;
        restore(_in, tmp, options_.check(Options::MSB));
        _value = tmp;
        break;
    default:
        _value = 0.0;
        std::cerr << "unsupported conversion type to float: " << _type << std::endl;
        break;
    }
543 544
}

545 546 547 548 549 550 551

//-----------------------------------------------------------------------------


void _PLYReader_::readValue(ValueType _type, std::istream& _in, double& _value) const {

    switch (_type) {
552

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        case ValueTypeFLOAT64:

        case ValueTypeDOUBLE:

            float64_t tmp;
            restore(_in, tmp, options_.check(Options::MSB));
            _value = tmp;

            break;

    default:

        _value = 0.0;
        std::cerr << "unsupported conversion type to double: " << _type << std::endl;

        break;
    }
}


//-----------------------------------------------------------------------------


576
void _PLYReader_::readValue(ValueType _type, std::istream& _in, unsigned int& _value) const {
577

578 579
    uint32_t tmp_uint32_t;
    uint16_t tmp_uint16_t;
580 581 582
    uint8_t tmp_uchar;

    switch (_type) {
583 584 585 586

        case ValueTypeUINT:

        case ValueTypeUINT32:
587

588 589 590
            restore(_in, tmp_uint32_t, options_.check(Options::MSB));
            _value = tmp_uint32_t;

591
        break;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

        case ValueTypeUSHORT:

        case ValueTypeUINT16:

            restore(_in, tmp_uint16_t, options_.check(Options::MSB));
            _value = tmp_uint16_t;

            break;

        case ValueTypeUCHAR:

        case ValueTypeUINT8:

            restore(_in, tmp_uchar, options_.check(Options::MSB));
            _value = tmp_uchar;

            break;

        default:

            _value = 0;
            std::cerr << "unsupported conversion type to unsigned int: " << _type << std::endl;

            break;
617
    }
618 619
}

620 621 622 623

//-----------------------------------------------------------------------------


624
void _PLYReader_::readValue(ValueType _type, std::istream& _in, int& _value) const {
625

626
    int32_t tmp_int32_t;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    int16_t tmp_int16_t;
    int8_t tmp_char;

    switch (_type) {

        case ValueTypeINT:

        case ValueTypeINT32:

            restore(_in, tmp_int32_t, options_.check(Options::MSB));
            _value = tmp_int32_t;

            break;

        case ValueTypeSHORT:

        case ValueTypeINT16:

            restore(_in, tmp_int16_t, options_.check(Options::MSB));
            _value = tmp_int16_t;

            break;

        case ValueTypeCHAR:

        case ValueTypeINT8:

            restore(_in, tmp_char, options_.check(Options::MSB));
            _value = tmp_char;

            break;

        default:

            _value = 0;
            std::cerr << "unsupported conversion type to int: " << _type << std::endl;

            break;
    }
}


//-----------------------------------------------------------------------------


void _PLYReader_::readInteger(ValueType _type, std::istream& _in, int& _value) const {

    int32_t tmp_int32_t;
    uint32_t tmp_uint32_t;
    int8_t tmp_char;
677 678 679
    uint8_t tmp_uchar;

    switch (_type) {
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

        case ValueTypeINT:

        case ValueTypeINT32:

            restore(_in, tmp_int32_t, options_.check(Options::MSB));
            _value = tmp_int32_t;

            break;

        case ValueTypeUINT:

        case ValueTypeUINT32:

            restore(_in, tmp_uint32_t, options_.check(Options::MSB));
            _value = tmp_uint32_t;
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
            break;

        case ValueTypeCHAR:

        case ValueTypeINT8:

            restore(_in, tmp_char, options_.check(Options::MSB));
            _value = tmp_char;

            break;

        case ValueTypeUCHAR:

        case ValueTypeUINT8:

            restore(_in, tmp_uchar, options_.check(Options::MSB));
            _value = tmp_uchar;

            break;

        default:

            _value = 0;
            std::cerr << "unsupported conversion type to int: " << _type << std::endl;

            break;
723
    }
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

//-----------------------------------------------------------------------------


void _PLYReader_::readInteger(ValueType _type, std::istream& _in, unsigned int& _value) const {

    int32_t tmp_int32_t;
    uint32_t tmp_uint32_t;
    int8_t tmp_char;
    uint8_t tmp_uchar;

    switch (_type) {

        case ValueTypeUINT:

        case ValueTypeUINT32:

            restore(_in, tmp_uint32_t, options_.check(Options::MSB));
            _value = tmp_uint32_t;

            break;

        case ValueTypeINT:

        case ValueTypeINT32:

            restore(_in, tmp_int32_t, options_.check(Options::MSB));
            _value = tmp_int32_t;

            break;

        case ValueTypeUCHAR:

        case ValueTypeUINT8:

            restore(_in, tmp_uchar, options_.check(Options::MSB));
            _value = tmp_uchar;

            break;

        case ValueTypeCHAR:

        case ValueTypeINT8:

            restore(_in, tmp_char, options_.check(Options::MSB));
            _value = tmp_char;

            break;

        default:

            _value = 0;
            std::cerr << "unsupported conversion type to unsigned int: " << _type << std::endl;

            break;
    }
}


785 786
//------------------------------------------------------------------------------

787

Mike Kremer's avatar
Mike Kremer committed
788
bool _PLYReader_::can_u_read(const std::string& _filename) const {
789

790 791 792 793 794 795 796 797 798 799 800
    // !!! Assuming BaseReader::can_u_parse( std::string& )
    // does not call BaseReader::read_magic()!!!

    if (BaseReader::can_u_read(_filename)) {
        std::ifstream ifs(_filename.c_str());
        if (ifs.is_open() && can_u_read(ifs)) {
            ifs.close();
            return true;
        }
    }
    return false;
Jan Möbius's avatar
Jan Möbius committed
801 802
}

803 804


Jan Möbius's avatar
Jan Möbius committed
805 806
//-----------------------------------------------------------------------------

Mike Kremer's avatar
Mike Kremer committed
807
std::string get_property_name(std::string _string1, std::string _string2) {
808

809 810 811 812
    if (_string1 == "float32" || _string1 == "float64" || _string1 == "float" || _string1 == "double" ||
        _string1 == "int8" || _string1 == "uint8" || _string1 == "char" || _string1 == "uchar" ||
        _string1 == "int32" || _string1 == "uint32" || _string1 == "int" || _string1 == "uint" ||
        _string1 == "int16" || _string1 == "uint16" || _string1 == "short" || _string1 == "ushort")
813
        return _string2;
Mike Kremer's avatar
Mike Kremer committed
814

815 816 817 818
    if (_string2 == "float32" || _string2 == "float64" || _string2 == "float" || _string2 == "double" ||
        _string2 == "int8" || _string2 == "uint8" || _string2 == "char" || _string2 == "uchar" ||
        _string2 == "int32" || _string2 == "uint32" || _string2 == "int" || _string2 == "uint" ||
        _string2 == "int16" || _string2 == "uint16" || _string2 == "short" || _string2 == "ushort")
819
        return _string1;
Mike Kremer's avatar
Mike Kremer committed
820

821

822 823
    std::cerr << "Unsupported entry type" << std::endl;
    return "Unsupported";
Jan Möbius's avatar
Jan Möbius committed
824 825 826 827
}

//-----------------------------------------------------------------------------

Mike Kremer's avatar
Mike Kremer committed
828
_PLYReader_::ValueType get_property_type(std::string _string1, std::string _string2) {
829

830
    if (_string1 == "float32" || _string2 == "float32")
831

832
        return _PLYReader_::ValueTypeFLOAT32;
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    else if (_string1 == "float64" || _string2 == "float64")

        return _PLYReader_::ValueTypeFLOAT64;

    else if (_string1 == "float" || _string2 == "float")

        return _PLYReader_::ValueTypeFLOAT;

    else if (_string1 == "double" || _string2 == "double")

        return _PLYReader_::ValueTypeDOUBLE;

    else if (_string1 == "int8" || _string2 == "int8")

        return _PLYReader_::ValueTypeINT8;

    else if (_string1 == "uint8" || _string2 == "uint8")

852
        return _PLYReader_::ValueTypeUINT8;
853 854 855 856 857

    else if (_string1 == "char" || _string2 == "char")

        return _PLYReader_::ValueTypeCHAR;

858
    else if (_string1 == "uchar" || _string2 == "uchar")
859

860
        return _PLYReader_::ValueTypeUCHAR;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

    else if (_string1 == "int32" || _string2 == "int32")

        return _PLYReader_::ValueTypeINT32;

    else if (_string1 == "uint32" || _string2 == "uint32")

        return _PLYReader_::ValueTypeUINT32;

    else if (_string1 == "int" || _string2 == "int")

        return _PLYReader_::ValueTypeINT;

    else if (_string1 == "uint" || _string2 == "uint")

        return _PLYReader_::ValueTypeUINT;

    else if (_string1 == "int16" || _string2 == "int16")

        return _PLYReader_::ValueTypeINT16;

    else if (_string1 == "uint16" || _string2 == "uint16")

        return _PLYReader_::ValueTypeUINT16;

    else if (_string1 == "short" || _string2 == "short")

        return _PLYReader_::ValueTypeSHORT;

    else if (_string1 == "ushort" || _string2 == "ushort")

        return _PLYReader_::ValueTypeUSHORT;
893 894

    return _PLYReader_::Unsupported;
Jan Möbius's avatar
Jan Möbius committed
895 896
}

897

Jan Möbius's avatar
Jan Möbius committed
898 899
//-----------------------------------------------------------------------------

Mike Kremer's avatar
Mike Kremer committed
900
bool _PLYReader_::can_u_read(std::istream& _is) const {
901

902 903 904 905 906 907 908 909 910 911
    // Clear per file options
    options_.cleanup();

    // clear vertex property map, will be recreated
    vertexPropertyMap_.clear();
    vertexPropertyCount_ = 0;

    // read 1st line
    std::string line;
    std::getline(_is, line);
912
    trim(line);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

    //Check if this file is really a ply format
    if (line != "PLY" && line != "ply")
        return false;

    vertexCount_ = 0;
    faceCount_ = 0;
    vertexDimension_ = 0;

    std::string keyword;
    std::string fileType;
    std::string elementName = "";
    std::string propertyName;
    std::string listIndexType;
    std::string listEntryType;
    float version;

    _is >> keyword;
    _is >> fileType;
    _is >> version;

    if (_is.bad()) {
        omerr() << "Defect PLY header detected" << std::endl;
        return false;
    }

    if (fileType == "ascii") {
        options_ -= Options::Binary;
    } else if (fileType == "binary_little_endian") {
        options_ += Options::Binary;
        options_ += Options::LSB;
944 945 946
        //if (Endian::local() == Endian::MSB)

        //  options_ += Options::Swap;
947 948 949
    } else if (fileType == "binary_big_endian") {
        options_ += Options::Binary;
        options_ += Options::MSB;
950 951 952
        //if (Endian::local() == Endian::LSB)

        //  options_ += Options::Swap;
953 954 955 956 957
    } else {
        omerr() << "Unsupported PLY format: " << fileType << std::endl;
        return false;
    }

Matthias Möller's avatar
Matthias Möller committed
958
    std::streamoff streamPos = _is.tellg();
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    _is >> keyword;
    while (keyword != "end_header") {

        if (keyword == "comment") {
            std::getline(_is, line);
        } else if (keyword == "element") {
            _is >> elementName;
            if (elementName == "vertex") {
                _is >> vertexCount_;
            } else if (elementName == "face") {
                _is >> faceCount_;
            } else {
                omerr() << "PLY header unsupported element type: " << elementName << std::endl;
            }
        } else if (keyword == "property") {
            std::string tmp1;
            std::string tmp2;

            // Read first keyword, as it might be a list
            _is >> tmp1;

            if (tmp1 == "list") {
                if (elementName == "vertex") {
                    omerr() << "List type not supported for vertices!" << std::endl;
                } else if (elementName == "face") {
                    _is >> listIndexType;
                    _is >> listEntryType;
                    _is >> propertyName;

                    if (listIndexType == "uint8") {
                        faceIndexType_ = ValueTypeUINT8;
                    } else if (listIndexType == "uchar") {
                        faceIndexType_ = ValueTypeUCHAR;
                    } else {
                        omerr() << "Unsupported Index type for face list: " << listIndexType << std::endl;
994
                        return false;
995 996 997 998 999 1000
                    }

                    if (listEntryType == "int32") {
                        faceEntryType_ = ValueTypeINT32;
                    } else if (listEntryType == "int") {
                        faceEntryType_ = ValueTypeINT;
1001 1002 1003 1004
                    } else if (listEntryType == "uint32") {
                        faceEntryType_ = ValueTypeUINT32;
                    } else if (listEntryType == "uint") {
                        faceEntryType_ = ValueTypeUINT;
1005 1006
                    } else {
                        omerr() << "Unsupported Entry type for face list: " << listEntryType << std::endl;
1007
                        return false;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                    }

                }
            } else {
                // as this is not a list property, read second value of property
                _is >> tmp2;

                if (elementName == "vertex") {
                    // Extract name and type of property
                    // As the order seems to be different in some files, autodetect it.
                    ValueType valueType = get_property_type(tmp1, tmp2);
                    propertyName = get_property_name(tmp1, tmp2);

                    if (propertyName == "x") {
1022 1023 1024
                      VertexPropertyInfo entry(XCOORD, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      vertexDimension_++;
1025
                    } else if (propertyName == "y") {
1026 1027 1028
                      VertexPropertyInfo entry(YCOORD, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      vertexDimension_++;
1029
                    } else if (propertyName == "z") {
1030 1031 1032
                      VertexPropertyInfo entry(ZCOORD, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      vertexDimension_++;
Jan Möbius's avatar
Jan Möbius committed
1033
                    } else if (propertyName == "nx") {
1034 1035 1036
                      VertexPropertyInfo entry(XNORM, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexNormal;
Jan Möbius's avatar
Jan Möbius committed
1037
                    } else if (propertyName == "ny") {
1038 1039 1040
                      VertexPropertyInfo entry(YNORM, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexNormal;
Jan Möbius's avatar
Jan Möbius committed
1041
                    } else if (propertyName == "nz") {
1042 1043 1044
                      VertexPropertyInfo entry(ZNORM, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexNormal;
1045
                    } else if (propertyName == "u" || propertyName == "s") {
1046 1047 1048
                      VertexPropertyInfo entry(TEXX, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexTexCoord;
1049
                    } else if (propertyName == "v" || propertyName == "t") {
1050 1051 1052
                      VertexPropertyInfo entry(TEXY, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexTexCoord;
1053
                    } else if (propertyName == "red") {
1054 1055 1056 1057 1058
                      VertexPropertyInfo entry(COLORRED, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1059
                    } else if (propertyName == "green") {
1060 1061 1062 1063 1064
                      VertexPropertyInfo entry(COLORGREEN, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1065
                    } else if (propertyName == "blue") {
1066 1067 1068 1069 1070
                      VertexPropertyInfo entry(COLORBLUE, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1071
                    } else if (propertyName == "diffuse_red") {
1072
                      VertexPropertyInfo entry(COLORRED, valueType);
1073 1074
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
1075 1076
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1077
                    } else if (propertyName == "diffuse_green") {
1078
                      VertexPropertyInfo entry(COLORGREEN, valueType);
1079 1080
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
1081 1082
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1083
                    } else if (propertyName == "diffuse_blue") {
1084
                      VertexPropertyInfo entry(COLORBLUE, valueType);
1085
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
1086
                      options_ += Options::VertexColor;
1087 1088
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1089
                    } else if (propertyName == "alpha") {
1090 1091 1092 1093 1094 1095
                      VertexPropertyInfo entry(COLORALPHA, valueType);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
                      options_ += Options::VertexColor;
                      options_ += Options::ColorAlpha;
                      if (valueType == ValueTypeFLOAT || valueType == ValueTypeFLOAT32)
                        options_ += Options::ColorFloat;
1096
                    } else {
1097 1098 1099 1100 1101
                      VertexProperty prop = (!options_.is_binary()) ? CUSTOM_PROP : UNSUPPORTED; // loading vertex properties is not yet supported by the binary loader
                      if (prop != UNSUPPORTED)
                        options_ += Options::Custom;
                      VertexPropertyInfo entry(prop, valueType, propertyName);
                      vertexPropertyMap_[vertexPropertyCount_] = entry;
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
                    }

                    vertexPropertyCount_++;

                } else if (elementName == "face") {
                    omerr() << "Properties not supported for faces " << std::endl;
                }

            }

        } else {
            omlog() << "Unsupported keyword : " << keyword << std::endl;
        }

        streamPos = _is.tellg();
        _is >> keyword;
        if (_is.bad()) {
            omerr() << "Error while reading PLY file header" << std::endl;
            return false;
        }
    }

    // As the binary data is directy after the end_header keyword
    // and the stream removes too many bytes, seek back to the right position
    if (options_.is_binary()) {
        _is.seekg(streamPos + 12);
    }

    return true;
Jan Möbius's avatar
Jan Möbius committed
1131 1132 1133 1134 1135 1136
}

//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================