Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/plugins/ioPLY.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #if !defined(LoadPLY_INCLUDE_ONCE)
00033 #define LoadPLY_INCLUDE_ONCE
00034 
00035 #include <vlGraphics/Geometry.hpp>
00036 #include <vlCore/ResourceLoadWriter.hpp>
00037 #include <vlCore/ResourceDatabase.hpp>
00038 
00039 namespace vl
00040 {
00041   class VirtualFile;
00042   class TextStream;
00043 }
00044 
00045 namespace vl
00046 {
00047 //-----------------------------------------------------------------------------
00048   ref<ResourceDatabase> loadPLY(VirtualFile* file);
00049   ref<ResourceDatabase> loadPLY(const String& path);
00050 //---------------------------------------------------------------------------
00051 // LoadWriterPLY
00052 //---------------------------------------------------------------------------
00056   class LoadWriterPLY: public ResourceLoadWriter
00057   {
00058     VL_INSTRUMENT_CLASS(vl::LoadWriterPLY, ResourceLoadWriter)
00059 
00060   public:
00061     LoadWriterPLY(): ResourceLoadWriter("|ply|", "|ply|") {}
00062 
00063     ref<ResourceDatabase> loadResource(const String& path) const 
00064     {
00065       return loadPLY(path);
00066     }
00067 
00068     ref<ResourceDatabase> loadResource(VirtualFile* file) const
00069     {
00070       return loadPLY(file);
00071     }
00072 
00074     bool writeResource(const String& /*path*/, ResourceDatabase* /*resource*/) const
00075     {
00076       return false;
00077     }
00078 
00080     bool writeResource(VirtualFile* /*file*/, ResourceDatabase* /*resource*/) const
00081     {
00082       return false;
00083     }
00084   };
00085 //-----------------------------------------------------------------------------
00086 // PlyLoader
00087 //-----------------------------------------------------------------------------
00091   class VLGRAPHICS_EXPORT PlyLoader
00092   {
00093   public:
00094     typedef enum
00095     {
00096       PlyError,
00097       PlyChar,
00098       PlyUChar,
00099       PlyShort,
00100       PlyUShort,
00101       PlyInt,
00102       PlyUInt,
00103       PlyFloat,
00104       PlyDouble
00105     } EType;
00107     class PlyPropertyAbstract: public Object
00108     {
00109     public:
00110       const String& name() const { return mName; }
00111       void setName(const String& name) { mName = name; }
00112       virtual void read(VirtualFile*, bool le) = 0;
00113       virtual void read(TextStream* text) = 0;
00114     protected:
00115       String mName;
00116     };
00118     class PlyScalar: public PlyPropertyAbstract
00119     {
00120     public:
00121       PlyScalar(): mScalarType(PlyError) { mData.mDouble = 0; }
00122       void setScalarType(EType type) { mScalarType = type; }
00123       EType scalarType() const { return mScalarType; }
00124       virtual void read(VirtualFile* file, bool le);
00125       virtual void read(TextStream* text);
00126       float getAsFloat() const;
00127       int getAsInt() const;
00128     protected:
00129       union
00130       {
00131         char mChar;
00132         unsigned char mUChar;
00133         short mShort;
00134         unsigned short mUShort;
00135         int mInt;
00136         unsigned int mUInt;
00137         float mFloat;
00138         double mDouble;
00139       } mData;
00140       EType mScalarType;
00141     };
00143     class PlyScalarList: public PlyPropertyAbstract
00144     {
00145     public:
00146       PlyScalarList(): mScalarType(PlyError), mCountType(PlyError) {}
00147       void setCountType(EType type) { mCountType = type; }
00148       EType countType() const { return mCountType; }
00149       void setScalarType(EType type) { mScalarType = type; }
00150       EType scalarType() const { return mScalarType; }
00151       const std::vector<PlyScalar>& scalars() const { return mScalars; }
00152       std::vector<PlyScalar>& scalars() { return mScalars; }
00153       virtual void read(VirtualFile* file, bool le) 
00154       {
00155         PlyScalar c;
00156         c.setScalarType(countType());
00157         c.read(file,le);
00158         scalars().resize(c.getAsInt());
00159         for(unsigned i=0; i<scalars().size(); ++i)
00160         {
00161           scalars()[i].setScalarType(scalarType());
00162           scalars()[i].read(file,le);
00163         }
00164       }
00165       virtual void read(TextStream* text)
00166       {
00167         PlyScalar c;
00168         c.setScalarType(countType());
00169         c.read(text);
00170         scalars().resize(c.getAsInt());
00171         for(unsigned i=0; i<scalars().size(); ++i)
00172         {
00173           scalars()[i].setScalarType(scalarType());
00174           scalars()[i].read(text);
00175         }
00176       }
00177     protected:
00178       std::vector<PlyScalar> mScalars;
00179       EType mScalarType;
00180       EType mCountType;
00181     };
00183     class PlyElement: public Object
00184     {
00185     public:
00186       PlyElement(): mElemCount(0) {}
00187       const String& name() const { return mName; }
00188       void setName(const String& name) { mName = name; }
00189       const std::vector< ref<PlyPropertyAbstract> >& properties() const { return mProperties; }
00190       std::vector< ref<PlyPropertyAbstract> >& properties() { return mProperties; }
00191       int elemCount() const { return mElemCount; }
00192       void setElemCount(int count) { mElemCount = count; }
00193       virtual void read(VirtualFile* file, bool le)
00194       {
00195         for(unsigned int i=0; i<mProperties.size(); ++i)
00196           mProperties[i]->read(file, le);
00197       }
00198       virtual void read(TextStream* text)
00199       {
00200         for(unsigned int i=0; i<mProperties.size(); ++i)
00201           mProperties[i]->read(text);
00202       }
00203       fvec3 getVertex() const
00204       {
00205         fvec3 v;
00206         if (mVertex[0]) v.x() = mVertex[0]->getAsFloat();
00207         if (mVertex[1]) v.y() = mVertex[1]->getAsFloat();
00208         if (mVertex[2]) v.z() = mVertex[2]->getAsFloat();
00209         return v;
00210       }
00211       fvec3 getNormal() const
00212       {
00213         fvec3 v;
00214         if (mNormal[0]) v.x() = mNormal[0]->getAsFloat();
00215         if (mNormal[1]) v.y() = mNormal[1]->getAsFloat();
00216         if (mNormal[2]) v.z() = mNormal[2]->getAsFloat();
00217         return v;
00218       }
00219       ubvec4 getColor() const
00220       {
00221         ubvec4 v;
00222         if (mColor[0]) v.r() = (unsigned char)mColor[0]->getAsInt();
00223         if (mColor[1]) v.g() = (unsigned char)mColor[1]->getAsInt();
00224         if (mColor[2]) v.b() = (unsigned char)mColor[2]->getAsInt();
00225         if (mColor[3]) v.a() = (unsigned char)mColor[3]->getAsInt();
00226         return v;
00227       }
00228       void analyze();
00229     protected:
00230       std::vector< ref<PlyPropertyAbstract> > mProperties;
00231       std::vector< ref<PlyScalar> > mVertex;
00232       std::vector< ref<PlyScalar> > mNormal;
00233       std::vector< ref<PlyScalar> > mColor;
00234       String mName;
00235       int mElemCount;
00236     };
00237   public:
00239     PlyLoader(): mBinary(false), mLittleEndian(false) {}
00241     ref<ResourceDatabase> loadPly(VirtualFile* file);
00242     const std::vector< ref<PlyElement> >& elements() const { return mElements; }
00243     std::vector< ref<PlyElement> >& elements() { return mElements; }
00244     bool binary() const { return mBinary; }
00245     bool littleEndian() const { return mLittleEndian; }
00246     void readElements(VirtualFile* file);
00247     void readElements(TextStream* text);
00248     void newElement(PlyElement*el);
00249     EType translateType(const String& type);
00250     void analyzeHeader();
00251     bool readHeader(TextStream* line_reader);
00252   protected:
00253     std::vector< ref<PlyElement> > mElements;
00254     ref<ArrayFloat3>  mVerts;
00255     ref<ArrayFloat3>  mNormals;
00256     ref<ArrayUByte4> mColors;
00257     std::vector<unsigned int> mIndices;
00258     int mVertexIndex;
00259     bool mBinary;
00260     bool mLittleEndian;
00261 };
00262 };
00263 
00264 #endif
00265 

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:39.
Permission is granted to use this page to write and publish articles regarding Visualization Library.