PLYReader.hh 9.43 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
94
95
96

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

//== 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
97
98
    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
99
100
101

*/

102
class OPENMESHDLLEXPORT _PLYReader_ : public BaseReader
Jan Möbius's avatar
Jan Möbius committed
103
104
105
106
107
108
109
110
111
112
{
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,
113
114
        BaseImporter& _bi,
        Options& _opt);
Jan Möbius's avatar
Jan Möbius committed
115

116
  bool read(std::istream& _is,
117
118
            BaseImporter& _bi,
            Options& _opt);
119

Jan Möbius's avatar
Jan Möbius committed
120
121
122
  bool can_u_read(const std::string& _filename) const;

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

private:

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

138
139
  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
140
141
142

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

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

152
153
154
  void readInteger(ValueType _type, std::istream& _in, int& _value) const;
  void readInteger(ValueType _type, std::istream& _in, unsigned int& _value) const;

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

  mutable unsigned char buff[8];

162
  /// Available per file options for reading
Jan Möbius's avatar
Jan Möbius committed
163
  mutable Options options_;
164
165

  /// Options that the user wants to read
Jan Möbius's avatar
Jan Möbius committed
166
167
168
169
170
171
172
173
  mutable Options userOptions_;

  mutable unsigned int vertexCount_;
  mutable unsigned int faceCount_;

  mutable ValueType vertexType_;
  mutable uint vertexDimension_;

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

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

185
  // Number of vertex properties
Matthias Möller's avatar
Matthias Möller committed
186
  struct PropertyInfo
187
  {
Matthias Möller's avatar
Matthias Möller committed
188
    Property       property;
189
190
    ValueType      value;
    std::string    name;//for custom properties
Matthias Möller's avatar
Matthias Möller committed
191
192
193
194
    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){}
195
  };
Matthias Möller's avatar
Matthias Möller committed
196
197
  mutable std::vector< PropertyInfo > vertexProperties_;
  mutable std::vector< PropertyInfo > faceProperties_;
Jan Möbius's avatar
Jan Möbius committed
198

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  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
217
218
219
220
221
222
223
224
};


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


/// Declare the single entity of the PLY reader
extern _PLYReader_  __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
225
OPENMESHDLLEXPORT _PLYReader_&  PLYReader();
Jan Möbius's avatar
Jan Möbius committed
226
227
228
229
230
231
232
233


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