# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/plugins/vlPLY.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    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   public:
    00059     virtual const char* className() { return "vl::LoadWriterPLY"; }
    00060     LoadWriterPLY(): ResourceLoadWriter("|ply|", "|ply|") {}
    00061
    00062     ref<ResourceDatabase> loadResource(const String& path) const
    00063     {
    00064       return loadPLY(path);
    00065     }
    00066
    00067     ref<ResourceDatabase> loadResource(VirtualFile* file) const
    00068     {
    00069       return loadPLY(file);
    00070     }
    00071
    00073     bool writeResource(const String& /*path*/, ResourceDatabase* /*resource*/) const
    00074     {
    00075       return false;
    00076     }
    00077
    00079     bool writeResource(VirtualFile* /*file*/, ResourceDatabase* /*resource*/) const
    00080     {
    00081       return false;
    00082     }
    00083   };
    00084 //-----------------------------------------------------------------------------
    00085 // PlyLoader
    00086 //-----------------------------------------------------------------------------
    00090   class VLGRAPHICS_EXPORT PlyLoader
    00091   {
    00092   public:
    00093     typedef enum
    00094     {
    00095       PlyError,
    00096       PlyChar,
    00097       PlyUChar,
    00098       PlyShort,
    00099       PlyUShort,
    00100       PlyInt,
    00101       PlyUInt,
    00102       PlyFloat,
    00103       PlyDouble
    00104     } EType;
    00106     class PlyPropertyAbstract: public Object
    00107     {
    00108     public:
    00109       const String& name() const { return mName; }
    00110       void setName(const String& name) { mName = name; }
    00111       virtual void read(VirtualFile*, bool le) = 0;
    00112       virtual void read(TextStream* text) = 0;
    00113     protected:
    00114       String mName;
    00115     };
    00117     class PlyScalar: public PlyPropertyAbstract
    00118     {
    00119     public:
    00120       PlyScalar(): mScalarType(PlyError) { mData.mDouble = 0; }
    00121       void setScalarType(EType type) { mScalarType = type; }
    00122       EType scalarType() const { return mScalarType; }
    00123       virtual void read(VirtualFile* file, bool le);
    00124       virtual void read(TextStream* text);
    00125       float getAsFloat() const;
    00126       int getAsInt() const;
    00127     protected:
    00128       union
    00129       {
    00130         char mChar;
    00131         unsigned char mUChar;
    00132         short mShort;
    00133         unsigned short mUShort;
    00134         int mInt;
    00135         unsigned int mUInt;
    00136         float mFloat;
    00137         double mDouble;
    00138       } mData;
    00139       EType mScalarType;
    00140     };
    00142     class PlyScalarList: public PlyPropertyAbstract
    00143     {
    00144     public:
    00145       PlyScalarList(): mScalarType(PlyError), mCountType(PlyError) {}
    00146       void setCountType(EType type) { mCountType = type; }
    00147       EType countType() const { return mCountType; }
    00148       void setScalarType(EType type) { mScalarType = type; }
    00149       EType scalarType() const { return mScalarType; }
    00150       const std::vector<PlyScalar>& scalars() const { return mScalars; }
    00151       std::vector<PlyScalar>& scalars() { return mScalars; }
    00152       virtual void read(VirtualFile* file, bool le)
    00153       {
    00154         PlyScalar c;
    00155         c.setScalarType(countType());
    00156         c.read(file,le);
    00157         scalars().resize(c.getAsInt());
    00158         for(unsigned i=0; i<scalars().size(); ++i)
    00159         {
    00160           scalars()[i].setScalarType(scalarType());
    00161           scalars()[i].read(file,le);
    00162         }
    00163       }
    00164       virtual void read(TextStream* text)
    00165       {
    00166         PlyScalar c;
    00167         c.setScalarType(countType());
    00168         c.read(text);
    00169         scalars().resize(c.getAsInt());
    00170         for(unsigned i=0; i<scalars().size(); ++i)
    00171         {
    00172           scalars()[i].setScalarType(scalarType());
    00173           scalars()[i].read(text);
    00174         }
    00175       }
    00176     protected:
    00177       std::vector<PlyScalar> mScalars;
    00178       EType mScalarType;
    00179       EType mCountType;
    00180     };
    00182     class PlyElement: public Object
    00183     {
    00184     public:
    00185       PlyElement(): mElemCount(0) {}
    00186       const String& name() const { return mName; }
    00187       void setName(const String& name) { mName = name; }
    00188       const std::vector< ref<PlyPropertyAbstract> >& properties() const { return mProperties; }
    00189       std::vector< ref<PlyPropertyAbstract> >& properties() { return mProperties; }
    00190       int elemCount() const { return mElemCount; }
    00191       void setElemCount(int count) { mElemCount = count; }
    00192       virtual void read(VirtualFile* file, bool le)
    00193       {
    00194         for(unsigned int i=0; i<mProperties.size(); ++i)
    00195           mProperties[i]->read(file, le);
    00196       }
    00197       virtual void read(TextStream* text)
    00198       {
    00199         for(unsigned int i=0; i<mProperties.size(); ++i)
    00200           mProperties[i]->read(text);
    00201       }
    00202       fvec3 getVertex() const
    00203       {
    00204         fvec3 v;
    00205         if (mVertex[0]) v.x() = mVertex[0]->getAsFloat();
    00206         if (mVertex[1]) v.y() = mVertex[1]->getAsFloat();
    00207         if (mVertex[2]) v.z() = mVertex[2]->getAsFloat();
    00208         return v;
    00209       }
    00210       fvec3 getNormal() const
    00211       {
    00212         fvec3 v;
    00213         if (mNormal[0]) v.x() = mNormal[0]->getAsFloat();
    00214         if (mNormal[1]) v.y() = mNormal[1]->getAsFloat();
    00215         if (mNormal[2]) v.z() = mNormal[2]->getAsFloat();
    00216         return v;
    00217       }
    00218       ubvec4 getColor() const
    00219       {
    00220         ubvec4 v;
    00221         if (mColor[0]) v.r() = (unsigned char)mColor[0]->getAsInt();
    00222         if (mColor[1]) v.g() = (unsigned char)mColor[1]->getAsInt();
    00223         if (mColor[2]) v.b() = (unsigned char)mColor[2]->getAsInt();
    00224         if (mColor[3]) v.a() = (unsigned char)mColor[3]->getAsInt();
    00225         return v;
    00226       }
    00227       void analyze();
    00228     protected:
    00229       std::vector< ref<PlyPropertyAbstract> > mProperties;
    00230       std::vector< ref<PlyScalar> > mVertex;
    00231       std::vector< ref<PlyScalar> > mNormal;
    00232       std::vector< ref<PlyScalar> > mColor;
    00233       String mName;
    00234       int mElemCount;
    00235     };
    00236   public:
    00238     PlyLoader(): mBinary(false), mLittleEndian(false) {}
    00240     ref<ResourceDatabase> loadPly(VirtualFile* file);
    00241     const std::vector< ref<PlyElement> >& elements() const { return mElements; }
    00242     std::vector< ref<PlyElement> >& elements() { return mElements; }
    00243     bool binary() const { return mBinary; }
    00244     bool littleEndian() const { return mLittleEndian; }
    00245     void readElements(VirtualFile* file);
    00246     void readElements(TextStream* text);
    00247     void newElement(PlyElement*el);
    00248     EType translateType(const String& type);
    00249     void analyzeHeader();
    00250     bool readHeader(TextStream* line_reader);
    00251   protected:
    00252     std::vector< ref<PlyElement> > mElements;
    00253     ref<ArrayFloat3>  mVerts;
    00254     ref<ArrayFloat3>  mNormals;
    00255     ref<ArrayUByte4> mColors;
    00256     std::vector<unsigned int> mIndices;
    00257     int mVertexIndex;
    00258     bool mBinary;
    00259     bool mLittleEndian;
    00260 };
    00261 };
    00262
    00263 #endif
    00264

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:24.
Permission is granted to use this page to write and publish articles regarding Visualization Library.