Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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& , ResourceDatabase* ) const
00074 {
00075 return false;
00076 }
00077
00079 bool writeResource(VirtualFile* , ResourceDatabase* ) const
00080 {
00081 return false;
00082 }
00083 };
00084
00085
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