Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ioPLY.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #if !defined(LoadPLY_INCLUDE_ONCE)
33 #define LoadPLY_INCLUDE_ONCE
34 
35 #include <vlGraphics/Geometry.hpp>
38 
39 namespace vl
40 {
41  class VirtualFile;
42  class TextStream;
43 }
44 
45 namespace vl
46 {
47 //-----------------------------------------------------------------------------
48  VLGRAPHICS_EXPORT ref<ResourceDatabase> loadPLY(VirtualFile* file);
49  VLGRAPHICS_EXPORT ref<ResourceDatabase> loadPLY(const String& path);
50 //---------------------------------------------------------------------------
51 // LoadWriterPLY
52 //---------------------------------------------------------------------------
57  {
59 
60  public:
61  LoadWriterPLY(): ResourceLoadWriter("|ply|", "|ply|") {}
62 
64  {
65  return loadPLY(path);
66  }
67 
69  {
70  return loadPLY(file);
71  }
72 
74  bool writeResource(const String& /*path*/, ResourceDatabase* /*resource*/) const
75  {
76  return false;
77  }
78 
80  bool writeResource(VirtualFile* /*file*/, ResourceDatabase* /*resource*/) const
81  {
82  return false;
83  }
84  };
85 //-----------------------------------------------------------------------------
86 // PlyLoader
87 //-----------------------------------------------------------------------------
92  {
93  public:
94  typedef enum
95  {
104  PlyDouble
105  } EType;
108  {
109  public:
110  const String& name() const { return mName; }
111  void setName(const String& name) { mName = name; }
112  virtual void read(VirtualFile*, bool le) = 0;
113  virtual void read(TextStream* text) = 0;
114  protected:
116  };
119  {
120  public:
121  PlyScalar(): mScalarType(PlyError) { mData.mDouble = 0; }
122  void setScalarType(EType type) { mScalarType = type; }
123  EType scalarType() const { return mScalarType; }
124  virtual void read(VirtualFile* file, bool le);
125  virtual void read(TextStream* text);
126  float getAsFloat() const;
127  int getAsInt() const;
128  protected:
129  union
130  {
131  char mChar;
132  unsigned char mUChar;
133  short mShort;
134  unsigned short mUShort;
135  int mInt;
136  unsigned int mUInt;
137  float mFloat;
138  double mDouble;
139  } mData;
140  EType mScalarType;
141  };
144  {
145  public:
146  PlyScalarList(): mScalarType(PlyError), mCountType(PlyError) {}
147  void setCountType(EType type) { mCountType = type; }
148  EType countType() const { return mCountType; }
149  void setScalarType(EType type) { mScalarType = type; }
150  EType scalarType() const { return mScalarType; }
151  const std::vector<PlyScalar>& scalars() const { return mScalars; }
152  std::vector<PlyScalar>& scalars() { return mScalars; }
153  virtual void read(VirtualFile* file, bool le)
154  {
155  PlyScalar c;
156  c.setScalarType(countType());
157  c.read(file,le);
158  scalars().resize(c.getAsInt());
159  for(unsigned i=0; i<scalars().size(); ++i)
160  {
161  scalars()[i].setScalarType(scalarType());
162  scalars()[i].read(file,le);
163  }
164  }
165  virtual void read(TextStream* text)
166  {
167  PlyScalar c;
168  c.setScalarType(countType());
169  c.read(text);
170  scalars().resize(c.getAsInt());
171  for(unsigned i=0; i<scalars().size(); ++i)
172  {
173  scalars()[i].setScalarType(scalarType());
174  scalars()[i].read(text);
175  }
176  }
177  protected:
178  std::vector<PlyScalar> mScalars;
179  EType mScalarType;
180  EType mCountType;
181  };
183  class PlyElement: public Object
184  {
185  public:
186  PlyElement(): mElemCount(0) {}
187  const String& name() const { return mName; }
188  void setName(const String& name) { mName = name; }
189  const std::vector< ref<PlyPropertyAbstract> >& properties() const { return mProperties; }
190  std::vector< ref<PlyPropertyAbstract> >& properties() { return mProperties; }
191  int elemCount() const { return mElemCount; }
192  void setElemCount(int count) { mElemCount = count; }
193  virtual void read(VirtualFile* file, bool le)
194  {
195  for(unsigned int i=0; i<mProperties.size(); ++i)
196  mProperties[i]->read(file, le);
197  }
198  virtual void read(TextStream* text)
199  {
200  for(unsigned int i=0; i<mProperties.size(); ++i)
201  mProperties[i]->read(text);
202  }
203  fvec3 getVertex() const
204  {
205  fvec3 v;
206  if (mVertex[0]) v.x() = mVertex[0]->getAsFloat();
207  if (mVertex[1]) v.y() = mVertex[1]->getAsFloat();
208  if (mVertex[2]) v.z() = mVertex[2]->getAsFloat();
209  return v;
210  }
211  fvec3 getNormal() const
212  {
213  fvec3 v;
214  if (mNormal[0]) v.x() = mNormal[0]->getAsFloat();
215  if (mNormal[1]) v.y() = mNormal[1]->getAsFloat();
216  if (mNormal[2]) v.z() = mNormal[2]->getAsFloat();
217  return v;
218  }
219  ubvec4 getColor() const
220  {
221  ubvec4 v;
222  if (mColor[0]) v.r() = (unsigned char)mColor[0]->getAsInt();
223  if (mColor[1]) v.g() = (unsigned char)mColor[1]->getAsInt();
224  if (mColor[2]) v.b() = (unsigned char)mColor[2]->getAsInt();
225  if (mColor[3]) v.a() = (unsigned char)mColor[3]->getAsInt();
226  return v;
227  }
228  void analyze();
229  protected:
230  std::vector< ref<PlyPropertyAbstract> > mProperties;
231  std::vector< ref<PlyScalar> > mVertex;
232  std::vector< ref<PlyScalar> > mNormal;
233  std::vector< ref<PlyScalar> > mColor;
236  };
237  public:
239  PlyLoader(): mBinary(false), mLittleEndian(false) {}
241  ref<ResourceDatabase> loadPly(VirtualFile* file);
242  const std::vector< ref<PlyElement> >& elements() const { return mElements; }
243  std::vector< ref<PlyElement> >& elements() { return mElements; }
244  bool binary() const { return mBinary; }
245  bool littleEndian() const { return mLittleEndian; }
246  void readElements(VirtualFile* file);
247  void readElements(TextStream* text);
248  void newElement(PlyElement*el);
249  EType translateType(const String& type);
250  void analyzeHeader();
251  bool readHeader(TextStream* line_reader);
252  protected:
253  std::vector< ref<PlyElement> > mElements;
257  std::vector<unsigned int> mIndices;
259  bool mBinary;
261 };
262 };
263 
264 #endif
265 
unsigned char mUChar
Definition: ioPLY.hpp:132
bool mBinary
Definition: ioPLY.hpp:259
Used by PlyLoader.
Definition: ioPLY.hpp:118
std::vector< ref< PlyScalar > > mNormal
Definition: ioPLY.hpp:232
Used by PlyLoader.
Definition: ioPLY.hpp:183
EType countType() const
Definition: ioPLY.hpp:148
An abstract class representing a file.
Definition: VirtualFile.hpp:60
void setScalarType(EType type)
Definition: ioPLY.hpp:149
std::vector< unsigned int > mIndices
Definition: ioPLY.hpp:257
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const T_Scalar & z() const
Definition: Vector3.hpp:92
void setName(const String &name)
Definition: ioPLY.hpp:188
const T_Scalar & r() const
Definition: Vector4.hpp:112
virtual void read(VirtualFile *file, bool le)
Definition: ioPLY.cpp:65
const std::vector< PlyScalar > & scalars() const
Definition: ioPLY.hpp:151
ref< ArrayFloat3 > mNormals
Definition: ioPLY.hpp:255
The Vector4 class is a template class that implements a generic 4 components vector, see also vl::fvec4, vl::dvec4, vl::uvec4, vl::ivec4, vl::svec4, vl::usvec4, vl::bvec4, vl::ubvec4.
Definition: Vector4.hpp:44
ref< ResourceDatabase > loadResource(const String &path) const
Definition: ioPLY.hpp:63
fvec3 getNormal() const
Definition: ioPLY.hpp:211
int mVertexIndex
Definition: ioPLY.hpp:258
const String & name() const
Definition: ioPLY.hpp:187
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
fvec3 getVertex() const
Definition: ioPLY.hpp:203
The LoadWriterPLY class is a ResourceLoadWriter capable of reading PLY files.
Definition: ioPLY.hpp:56
virtual void read(TextStream *text)
Definition: ioPLY.hpp:165
ubvec4 getColor() const
Definition: ioPLY.hpp:219
const std::vector< ref< PlyElement > > & elements() const
Definition: ioPLY.hpp:242
ref< ResourceDatabase > loadResource(VirtualFile *file) const
Definition: ioPLY.hpp:68
Visualization Library main namespace.
const String & name() const
Definition: ioPLY.hpp:110
void setScalarType(EType type)
Definition: ioPLY.hpp:122
The TextStream class can be used to conveniently read or parse utf8-encoded text files.
Definition: TextStream.hpp:46
EType scalarType() const
Definition: ioPLY.hpp:123
const T_Scalar & g() const
Definition: Vector4.hpp:113
bool writeResource(const String &, ResourceDatabase *) const
Not supported yet.
Definition: ioPLY.hpp:74
bool binary() const
Definition: ioPLY.hpp:244
unsigned int mUInt
Definition: ioPLY.hpp:136
std::vector< ref< PlyElement > > mElements
Definition: ioPLY.hpp:253
The base class for all the reference counted objects.
Definition: Object.hpp:158
Loads a PLY file.
Definition: ioPLY.hpp:91
bool mLittleEndian
Definition: ioPLY.hpp:260
const T_Scalar & y() const
Definition: Vector3.hpp:91
bool littleEndian() const
Definition: ioPLY.hpp:245
void setCountType(EType type)
Definition: ioPLY.hpp:147
std::vector< PlyScalar > & scalars()
Definition: ioPLY.hpp:152
bool writeResource(VirtualFile *, ResourceDatabase *) const
Not supported yet.
Definition: ioPLY.hpp:80
ref< ArrayUByte4 > mColors
Definition: ioPLY.hpp:256
const T_Scalar & b() const
Definition: Vector4.hpp:114
Used by PlyLoader.
Definition: ioPLY.hpp:143
int getAsInt() const
Definition: ioPLY.cpp:116
std::vector< PlyScalar > mScalars
Definition: ioPLY.hpp:178
virtual void read(TextStream *text)
Definition: ioPLY.hpp:198
int elemCount() const
Definition: ioPLY.hpp:191
virtual void read(VirtualFile *file, bool le)
Definition: ioPLY.hpp:153
std::vector< ref< PlyScalar > > mVertex
Definition: ioPLY.hpp:231
PlyLoader()
Constructor.
Definition: ioPLY.hpp:239
std::vector< ref< PlyScalar > > mColor
Definition: ioPLY.hpp:233
const T_Scalar & x() const
Definition: Vector3.hpp:90
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadPLY(VirtualFile *file)
Definition: ioPLY.cpp:58
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
std::vector< ref< PlyPropertyAbstract > > mProperties
Definition: ioPLY.hpp:230
std::vector< ref< PlyElement > > & elements()
Definition: ioPLY.hpp:243
void setName(const String &name)
Definition: ioPLY.hpp:111
The ResourceLoadWriter class is an abstract class used to implement read/write support for one or mor...
void setElemCount(int count)
Definition: ioPLY.hpp:192
const std::vector< ref< PlyPropertyAbstract > > & properties() const
Definition: ioPLY.hpp:189
ref< ArrayFloat3 > mVerts
Definition: ioPLY.hpp:254
unsigned short mUShort
Definition: ioPLY.hpp:134
virtual void read(VirtualFile *file, bool le)
Definition: ioPLY.hpp:193
The ResourceDatabase class contains and manipulates a set of resources.
std::vector< ref< PlyPropertyAbstract > > & properties()
Definition: ioPLY.hpp:190
const T_Scalar & a() const
Definition: Vector4.hpp:115
EType scalarType() const
Definition: ioPLY.hpp:150