PLYReader.hh 8.35 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/SingletonT.hh>
#include <OpenMesh/Core/IO/reader/BaseReader.hh>

#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
96
97
    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
98
99
100

*/

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

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

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

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

private:

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

137
138
  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
139
140

  float readToFloatValue(ValueType _type , std::fstream& _in) const;
Matthias Möller's avatar
Matthias Möller committed
141
142
  template<typename Handle>
  void readCustomProperty(std::istream& _in, BaseImporter& _bi, Handle _h, const std::string& _propName, const ValueType _valueType, const ValueType _listIndexType) const;
Jan Möbius's avatar
Jan Möbius committed
143

144
  void readValue(ValueType _type , std::istream& _in, float& _value) const;
145
  void readValue(ValueType _type, std::istream& _in, double& _value) const;
146
147
  void readValue(ValueType _type , std::istream& _in, unsigned int& _value) const;
  void readValue(ValueType _type , std::istream& _in, int& _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
170
  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
171
  enum Property {
Jan Möbius's avatar
Jan Möbius committed
172
173
    XCOORD,YCOORD,ZCOORD,
    TEXX,TEXY,
Mike Kremer's avatar
Mike Kremer committed
174
    COLORRED,COLORGREEN,COLORBLUE,COLORALPHA,
Matthias Möller's avatar
Matthias Möller committed
175
    XNORM,YNORM,ZNORM, CUSTOM_PROP, VERTEX_INDICES,
Mike Kremer's avatar
Mike Kremer committed
176
    UNSUPPORTED
Jan Möbius's avatar
Jan Möbius committed
177
178
  };

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

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

};


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


/// Declare the single entity of the PLY reader
extern _PLYReader_  __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
204
OPENMESHDLLEXPORT _PLYReader_&  PLYReader();
Jan Möbius's avatar
Jan Möbius committed
205
206
207
208
209
210
211
212


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