PrimitiveIO.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2008 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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     // look in the cache, and copy it if it is there
00036     map<string, Primitive*>::iterator i = m_GeometryCache.find(filename);
00037     if (i!=m_GeometryCache.end()) return i->second->Clone();
00038     
00039     // otherwise, we need to load it...
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 }

Generated on Wed Sep 17 21:16:30 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1