00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "PrimitiveIO.h"
00018 #include "OBJPrimitiveIO.h"
00019 #include "PixelPrimitiveIO.h"
00020
00021 using namespace Fluxus;
00022
00023 map<string, Primitive*> PrimitiveIO::m_GeometryCache;
00024
00025 PrimitiveIO::PrimitiveIO()
00026 {
00027 }
00028
00029 PrimitiveIO::~PrimitiveIO()
00030 {
00031 }
00032
00033 Primitive *PrimitiveIO::Read(const string &filename, bool cache)
00034 {
00035
00036 map<string, Primitive*>::iterator i = m_GeometryCache.find(filename);
00037 if (i!=m_GeometryCache.end()) return i->second->Clone();
00038
00039
00040 string extension = filename.substr(filename.find_last_of('.')+1,filename.size());
00041 PrimitiveIO *pio = GetFromExtension(extension);
00042 Primitive *prim = NULL;
00043 if (pio!=NULL)
00044 {
00045 prim = pio->FormatRead(filename);
00046 }
00047 delete pio;
00048
00049 if (prim==NULL) return NULL;
00050 if (!cache) return prim;
00051 m_GeometryCache[filename]=prim;
00052 return prim->Clone();
00053 }
00054
00055 bool PrimitiveIO::Write(const std::string &filename, const Primitive *ob)
00056 {
00057 string extension = filename.substr(filename.find_last_of('.')+1,filename.size());
00058 PrimitiveIO *pio = GetFromExtension(extension);
00059 Primitive *prim = NULL;
00060 bool ret=false;
00061 if (pio!=NULL)
00062 {
00063 ret = pio->FormatWrite(filename, ob);
00064 }
00065 delete pio;
00066 return ret;
00067 }
00068
00069 PrimitiveIO *PrimitiveIO::GetFromExtension(const string &extension)
00070 {
00071 if (extension=="obj") return new OBJPrimitiveIO;
00072 else if (extension=="png") return new PixelPrimitiveIO;
00073 return NULL;
00074 }
00075
00076 void PrimitiveIO::ClearGeometryCache()
00077 {
00078 for (map<string, Primitive*>::iterator i=m_GeometryCache.begin();
00079 i!=m_GeometryCache.end(); ++i)
00080 {
00081 delete i->second;
00082 }
00083 m_GeometryCache.clear();
00084 }
00085
00086 void PrimitiveIO::Dump()
00087 {
00088 for (map<string, Primitive*>::iterator i=m_GeometryCache.begin();
00089 i!=m_GeometryCache.end(); ++i)
00090 {
00091 Trace::Stream<<i->first<<endl;
00092 }
00093 }