PLYReader.hh 8.08 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
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department for Computer Graphics and Multimedia                 *
 *                          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
39
40
 * 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.              *
 *                                                                           * 
/*===========================================================================*\
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
64
65
66
67
68
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
96
97
98
 *                                                                           *
\*===========================================================================*/


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


#ifndef __PLYREADER_HH__
#define __PLYREADER_HH__


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


#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>

#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
    SingletonT to OFFReader.

*/

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

  float readToFloatValue(ValueType _type , std::fstream& _in) const;
139
  void readCustomProperty(std::istream& _in, BaseImporter& _bi, VertexHandle _vh, const std::string& _propName, const ValueType _valueType) const;
Jan Möbius's avatar
Jan Möbius committed
140

141
  void readValue(ValueType _type , std::istream& _in, float& _value) const;
142
  void readValue(ValueType _type, std::istream& _in, double& _value) const;
143
144
  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
145

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

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

  mutable unsigned char buff[8];

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

  /// Options that the user wants to read
Jan Möbius's avatar
Jan Möbius committed
160
161
162
163
164
165
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_;

  mutable ValueType faceIndexType_;
  mutable ValueType faceEntryType_;

  enum VertexProperty {
    XCOORD,YCOORD,ZCOORD,
    TEXX,TEXY,
Mike Kremer's avatar
Mike Kremer committed
174
    COLORRED,COLORGREEN,COLORBLUE,COLORALPHA,
175
    XNORM,YNORM,ZNORM, CUSTOM_PROP,
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
Jan Möbius's avatar
Jan Möbius committed
183
  mutable unsigned int vertexPropertyCount_;
184
185
186
187
188
189
190
191
192
193
  struct VertexPropertyInfo
  {
    VertexProperty property;
    ValueType      value;
    std::string    name;//for custom properties
    VertexPropertyInfo():property(UNSUPPORTED),value(Unsupported),name(""){}
    VertexPropertyInfo(VertexProperty _p, ValueType _v):property(_p),value(_v),name(""){}
    VertexPropertyInfo(VertexProperty _p, ValueType _v, const std::string& _n):property(_p),value(_v),name(_n){}
  };
  mutable std::map< int , VertexPropertyInfo > vertexPropertyMap_;
Jan Möbius's avatar
Jan Möbius committed
194
195
196
197
198
199
200
201
202

};


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


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


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