gKitGL
MeshIO.h
00001 
00002 #ifndef _IOMESH_H
00003 #define _IOMESH_H
00004 
00005 #include "IOManager.h"
00006 
00007 #include "Mesh.h"
00008 #include "MeshOBJ.h"
00009 
00010 //~ #include "MeshGK.h"
00011 #define NO_MESHGK
00012 
00013 namespace gk {
00014 
00015 //! manager pour importer des maillages.
00016 class MeshIO : public IOManager<Mesh>
00017 {
00018     // non copyable
00019     MeshIO( const MeshIO& );
00020     MeshIO& operator=( const MeshIO& );
00021     
00022     // private default constructor, singleton
00023     MeshIO( )
00024         :
00025         IOManager<Mesh>()
00026     {}
00027     
00028     ~MeshIO( ) {}
00029     
00030 public:
00031     //! importe l'objet 'name' du fichier 'filename'
00032     static
00033     Mesh *read( const std::string& filename, const std::string& name= "" ) 
00034     {
00035         // importer le fichier, si necessaire
00036         Mesh *mesh= manager().find(filename, name);
00037         if(mesh != NULL)
00038             return mesh;
00039         
00040         // importer le fichier
00041         mesh= new Mesh;
00042         if(isMeshOBJ(filename) && MeshLoadFromOBJ(filename, mesh) < 0)
00043         {
00044             printf("'%s' failed.\n", filename.c_str());
00045             delete mesh;
00046             return NULL;
00047         }
00048         
00049     #ifndef NO_MESHGK
00050         if(isMeshGK(filename) && MeshLoadFromGK(filename, mesh) < 0)
00051         {
00052             printf("'%s' failed.\n", filename.c_str());
00053             delete mesh;
00054             return NULL;
00055         }
00056     #endif
00057         
00058         // reference le mesh avec le manager
00059         return manager().insert(mesh, filename, name);
00060     }
00061 
00062     //! ecrit un mesh dans le fichier nomme 'filename'. 
00063     static
00064     int write( Mesh *mesh, const std::string& filename )
00065     {
00066     #ifndef NO_MESHGK
00067         if(isMeshGK(filename) && MeshWriteToGK(mesh, filename) < 0)
00068             return -1;
00069     #endif
00070         
00071         if(isMeshOBJ(filename) && MeshWriteToOBJ(mesh, filename) < 0)
00072             return -1;
00073         
00074         return 0;
00075     }
00076     
00077     static
00078     MeshIO& manager( )  // singleton
00079     {
00080         static MeshIO manager;
00081         return manager;
00082     }
00083 };
00084 
00085 } // namespace
00086 
00087 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerator Friends