PLYReader.hh 9.52 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

/*===========================================================================*\
Mike Kremer's avatar
Mike Kremer committed
43
 *                                                                           *
44
45
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 *                                                                           *
\*===========================================================================*/


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


#ifndef __PLYREADER_HH__
#define __PLYREADER_HH__


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


64
65

#include <iosfwd>
Jan Möbius's avatar
Jan Möbius committed
66
#include <string>
67
#include <cstdio>
Matthias Möller's avatar
Matthias Möller committed
68
#include <vector>
Jan Möbius's avatar
Jan Möbius committed
69
70
71
72

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


//== 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
94
95
    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
96
97
98

*/

99
class OPENMESHDLLEXPORT _PLYReader_ : public BaseReader
Jan Möbius's avatar
Jan Möbius committed
100
101
102
103
104
105
106
107
108
109
{
public:

  _PLYReader_();

  std::string get_description() const { return "PLY polygon file format"; }
  std::string get_extensions()  const { return "ply"; }
  std::string get_magic()       const { return "PLY"; }

  bool read(const std::string& _filename,
110
111
        BaseImporter& _bi,
        Options& _opt);
Jan Möbius's avatar
Jan Möbius committed
112

113
  bool read(std::istream& _is,
114
115
            BaseImporter& _bi,
            Options& _opt);
116

Jan Möbius's avatar
Jan Möbius committed
117
118
119
  bool can_u_read(const std::string& _filename) const;

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

private:

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

135
136
  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
137
138
139

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

140
  void readValue(ValueType _type , std::istream& _in, float& _value) const;
141
  void readValue(ValueType _type , std::istream& _in, double& _value) const;
142
  void readValue(ValueType _type , std::istream& _in, unsigned int& _value) const;
143
144
  void readValue(ValueType _type , std::istream& _in, unsigned short& _value) const;
  void readValue(ValueType _type , std::istream& _in, unsigned char& _value) const;
145
  void readValue(ValueType _type , std::istream& _in, int& _value) const;
146
147
  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
148

149
150
151
  void readInteger(ValueType _type, std::istream& _in, int& _value) const;
  void readInteger(ValueType _type, std::istream& _in, unsigned int& _value) const;

152
  /// Read unsupported properties in PLY file
153
154
155
156
157
158
  void consume_input(std::istream& _in, int _count) const {
	  _in.read(reinterpret_cast<char*>(&buff[0]), _count);
  }

  mutable unsigned char buff[8];

159
  /// Available per file options for reading
Jan Möbius's avatar
Jan Möbius committed
160
  mutable Options options_;
161
162

  /// Options that the user wants to read
Jan Möbius's avatar
Jan Möbius committed
163
164
165
166
167
168
169
  mutable Options userOptions_;

  mutable unsigned int vertexCount_;
  mutable unsigned int faceCount_;

  mutable uint vertexDimension_;

Matthias Möller's avatar
Matthias Möller committed
170
  enum Property {
Jan Möbius's avatar
Jan Möbius committed
171
172
    XCOORD,YCOORD,ZCOORD,
    TEXX,TEXY,
Mike Kremer's avatar
Mike Kremer committed
173
    COLORRED,COLORGREEN,COLORBLUE,COLORALPHA,
Matthias Möller's avatar
Matthias Möller committed
174
    XNORM,YNORM,ZNORM, CUSTOM_PROP, VERTEX_INDICES,
Mike Kremer's avatar
Mike Kremer committed
175
    UNSUPPORTED
Jan Möbius's avatar
Jan Möbius committed
176
177
  };

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

181
  // Number of vertex properties
Matthias Möller's avatar
Matthias Möller committed
182
  struct PropertyInfo
183
  {
Matthias Möller's avatar
Matthias Möller committed
184
    Property       property;
185
186
    ValueType      value;
    std::string    name;//for custom properties
Matthias Möller's avatar
Matthias Möller committed
187
188
189
190
    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){}
191
  };
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

  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
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  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
228
229
230
231
232
233
234
235
};


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


/// Declare the single entity of the PLY reader
extern _PLYReader_  __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
236
OPENMESHDLLEXPORT _PLYReader_&  PLYReader();
Jan Möbius's avatar
Jan Möbius committed
237
238
239
240
241
242
243
244


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