PLYReader.hh 9.16 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
Jan Möbius's avatar
Jan Möbius committed
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17 18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
Jan Möbius's avatar
Jan Möbius committed
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20 21 22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24 25 26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28 29 30 31 32 33 34 35 36 37 38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41

42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


//=============================================================================
//
//  Implements a reader module for OFF files
//
//=============================================================================


#ifndef __PLYREADER_HH__
#define __PLYREADER_HH__


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


59 60

#include <iosfwd>
Jan Möbius's avatar
Jan Möbius committed
61
#include <string>
62
#include <cstdio>
Matthias Möller's avatar
Matthias Möller committed
63
#include <vector>
Jan Möbius's avatar
Jan Möbius committed
64 65 66 67

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/SingletonT.hh>
#include <OpenMesh/Core/IO/reader/BaseReader.hh>
68
#include <OpenMesh/Core/Utils/GenProg.hh>
Jan Möbius's avatar
Jan Möbius committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


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


namespace OpenMesh {
namespace IO {


//== FORWARDS =================================================================


class BaseImporter;


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


/**
    Implementation of the PLY format reader. This class is singleton'ed by
Matthias Möller's avatar
Matthias Möller committed
89 90
    SingletonT to OFFReader. It can read custom properties, accessible via the name
    of the custom properties. List properties has the type std::vector<Type>.
Jan Möbius's avatar
Jan Möbius committed
91 92 93

*/

94
class OPENMESHDLLEXPORT _PLYReader_ : public BaseReader
Jan Möbius's avatar
Jan Möbius committed
95 96 97 98 99
{
public:

  _PLYReader_();

Jan Möbius's avatar
Jan Möbius committed
100 101 102
  std::string get_description() const override { return "PLY polygon file format"; }
  std::string get_extensions()  const override { return "ply"; }
  std::string get_magic()       const override { return "PLY"; }
Jan Möbius's avatar
Jan Möbius committed
103 104

  bool read(const std::string& _filename,
105
        BaseImporter& _bi,
Jan Möbius's avatar
Jan Möbius committed
106
        Options& _opt) override;
Jan Möbius's avatar
Jan Möbius committed
107

108
  bool read(std::istream& _is,
109
            BaseImporter& _bi,
Jan Möbius's avatar
Jan Möbius committed
110
            Options& _opt) override;
111

Jan Möbius's avatar
Jan Möbius committed
112
  bool can_u_read(const std::string& _filename) const override;
Jan Möbius's avatar
Jan Möbius committed
113 114

  enum ValueType {
115 116 117 118 119 120 121
    Unsupported,
    ValueTypeINT8, ValueTypeCHAR,
    ValueTypeUINT8, ValueTypeUCHAR,
    ValueTypeINT16, ValueTypeSHORT,
    ValueTypeUINT16, ValueTypeUSHORT,
    ValueTypeINT32, ValueTypeINT,
    ValueTypeUINT32, ValueTypeUINT,
Jan Möbius's avatar
Jan Möbius committed
122
    ValueTypeFLOAT32, ValueTypeFLOAT,
123
    ValueTypeFLOAT64, ValueTypeDOUBLE
Jan Möbius's avatar
Jan Möbius committed
124 125 126 127 128 129
  };

private:

  bool can_u_read(std::istream& _is) const;

130 131
  bool read_ascii(std::istream& _in, BaseImporter& _bi, const Options& _opt) const;
  bool read_binary(std::istream& _in, BaseImporter& _bi, bool swap, const Options& _opt) const;
Jan Möbius's avatar
Jan Möbius committed
132 133 134

  float readToFloatValue(ValueType _type , std::fstream& _in) const;

135
  void readValue(ValueType _type , std::istream& _in, float& _value) const;
136
  void readValue(ValueType _type , std::istream& _in, double& _value) const;
137
  void readValue(ValueType _type , std::istream& _in, unsigned int& _value) const;
138 139
  void readValue(ValueType _type , std::istream& _in, unsigned short& _value) const;
  void readValue(ValueType _type , std::istream& _in, unsigned char& _value) const;
140
  void readValue(ValueType _type , std::istream& _in, int& _value) const;
141 142
  void readValue(ValueType _type , std::istream& _in, short& _value) const;
  void readValue(ValueType _type , std::istream& _in, signed char& _value) const;
Jan Möbius's avatar
Jan Möbius committed
143

144 145 146
  void readInteger(ValueType _type, std::istream& _in, int& _value) const;
  void readInteger(ValueType _type, std::istream& _in, unsigned int& _value) const;

147
  /// Read unsupported properties in PLY file
148 149 150 151 152 153
  void consume_input(std::istream& _in, int _count) const {
	  _in.read(reinterpret_cast<char*>(&buff[0]), _count);
  }

  mutable unsigned char buff[8];

154
  /// Available per file options for reading
Jan Möbius's avatar
Jan Möbius committed
155
  mutable Options options_;
156 157

  /// Options that the user wants to read
Jan Möbius's avatar
Jan Möbius committed
158 159 160 161 162 163 164
  mutable Options userOptions_;

  mutable unsigned int vertexCount_;
  mutable unsigned int faceCount_;

  mutable uint vertexDimension_;

Matthias Möller's avatar
Matthias Möller committed
165
  enum Property {
Jan Möbius's avatar
Jan Möbius committed
166 167
    XCOORD,YCOORD,ZCOORD,
    TEXX,TEXY,
Mike Kremer's avatar
Mike Kremer committed
168
    COLORRED,COLORGREEN,COLORBLUE,COLORALPHA,
Matthias Möller's avatar
Matthias Möller committed
169
    XNORM,YNORM,ZNORM, CUSTOM_PROP, VERTEX_INDICES,
Mike Kremer's avatar
Mike Kremer committed
170
    UNSUPPORTED
Jan Möbius's avatar
Jan Möbius committed
171 172
  };

173 174
  /// Stores sizes of property types
  mutable std::map<ValueType, int> scalar_size_;
Jan Möbius's avatar
Jan Möbius committed
175

176
  // Number of vertex properties
Matthias Möller's avatar
Matthias Möller committed
177
  struct PropertyInfo
178
  {
Matthias Möller's avatar
Matthias Möller committed
179
    Property       property;
180 181
    ValueType      value;
    std::string    name;//for custom properties
Matthias Möller's avatar
Matthias Möller committed
182 183 184 185
    ValueType      listIndexType;//if type is unsupported, the poerty is not a list. otherwise, it the index type
    PropertyInfo():property(UNSUPPORTED),value(Unsupported),name(""),listIndexType(Unsupported){}
    PropertyInfo(Property _p, ValueType _v):property(_p),value(_v),name(""),listIndexType(Unsupported){}
    PropertyInfo(Property _p, ValueType _v, const std::string& _n):property(_p),value(_v),name(_n),listIndexType(Unsupported){}
186
  };
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  enum Element {
	  VERTEX,
	  FACE,
	  UNKNOWN
  };

  // Information on the elements
  struct ElementInfo
  {
    Element element_;
    std::string name_;
    unsigned int count_;
    std::vector< PropertyInfo > properties_;
  };

  mutable std::vector< ElementInfo > elements_;
Jan Möbius's avatar
Jan Möbius committed
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  template<typename T>
  inline void read(_PLYReader_::ValueType _type, std::istream& _in, T& _value, OpenMesh::GenProg::TrueType /*_binary*/) const
  {
    readValue(_type, _in, _value);
  }

  template<typename T>
  inline void read(_PLYReader_::ValueType _type, std::istream& _in, T& _value, OpenMesh::GenProg::FalseType /*_binary*/) const
  {
    _in >> _value;
  }

  //read and assign custom properties with the given type. Also creates property, if not exist
  template<bool binary, typename T, typename Handle>
  void readCreateCustomProperty(std::istream& _in, BaseImporter& _bi, Handle _h, const std::string& _propName, const ValueType _valueType, const ValueType _listType) const;

  template<bool binary, typename Handle>
  void readCustomProperty(std::istream& _in, BaseImporter& _bi, Handle _h, const std::string& _propName, const _PLYReader_::ValueType _valueType, const _PLYReader_::ValueType _listIndexType) const;
Jan Möbius's avatar
Jan Möbius committed
223 224 225 226 227 228 229 230
};


//== TYPE DEFINITION ==========================================================


/// Declare the single entity of the PLY reader
extern _PLYReader_  __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
231
OPENMESHDLLEXPORT _PLYReader_&  PLYReader();
Jan Möbius's avatar
Jan Möbius committed
232 233 234 235 236 237 238 239


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================