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
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifndef __VCGLIB_IMPORT
00065 #define __VCGLIB_IMPORT
00066
00067 #include <wrap/io_trimesh/import_obj.h>
00068 #include <wrap/io_trimesh/import_ply.h>
00069 #include <wrap/io_trimesh/import_stl.h>
00070 #include <wrap/io_trimesh/import_off.h>
00071 #include <wrap/io_trimesh/import_vmi.h>
00072
00073 #include <locale>
00074
00075 namespace vcg {
00076 namespace tri {
00077 namespace io {
00078
00084 template <class OpenMeshType>
00085 class Importer
00086 {
00087 private:
00088 enum KnownTypes { KT_UNKNOWN, KT_PLY, KT_STL, KT_OFF, KT_OBJ, KT_VMI };
00089 static int &LastType()
00090 {
00091 static int lastType= KT_UNKNOWN;
00092 return lastType;
00093 }
00094
00095 public:
00096
00097 static bool FileExtension(std::string filename, std::string extension)
00098 {
00099 std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
00100 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
00101 std::string end=filename.substr(filename.length()-extension.length(),extension.length());
00102 return end==extension;
00103 }
00104
00105 static int Open(OpenMeshType &m, const char *filename, CallBackPos *cb=0)
00106 {
00107 int dummymask = 0;
00108 return Open(m,filename,dummymask,cb);
00109 }
00110
00112 static int Open(OpenMeshType &m, const char *filename, int &loadmask, CallBackPos *cb=0)
00113 {
00114 int err;
00115 if(FileExtension(filename,"ply"))
00116 {
00117 err = ImporterPLY<OpenMeshType>::Open(m, filename, loadmask, cb);
00118 LastType()=KT_PLY;
00119 }
00120 else if(FileExtension(filename,"stl"))
00121 {
00122 err = ImporterSTL<OpenMeshType>::Open(m, filename, loadmask, cb);
00123 LastType()=KT_STL;
00124 }
00125 else if(FileExtension(filename,"off"))
00126 {
00127 err = ImporterOFF<OpenMeshType>::Open(m, filename, loadmask, cb);
00128 LastType()=KT_OFF;
00129 }
00130 else if(FileExtension(filename,"obj"))
00131 {
00132 err = ImporterOBJ<OpenMeshType>::Open(m, filename, loadmask, cb);
00133 LastType()=KT_OBJ;
00134 }
00135 else if(FileExtension(filename,"vmi"))
00136 {
00137 err = ImporterVMI<OpenMeshType>::Open(m, filename, loadmask, cb);
00138 LastType()=KT_VMI;
00139 }
00140 else {
00141 err=1;
00142 LastType()=KT_UNKNOWN;
00143 }
00144
00145 return err;
00146 }
00147
00148 static bool ErrorCritical(int error)
00149 {
00150 switch(LastType())
00151 {
00152 case KT_PLY : return (error>0); break;
00153 case KT_STL : return (error>0); break;
00154 case KT_OFF : return (error>0); break;
00155 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorCritical(error); break;
00156 }
00157
00158 return true;
00159 }
00160
00161 static const char *ErrorMsg(int error)
00162 {
00163 switch(LastType())
00164 {
00165 case KT_PLY : return ImporterPLY<OpenMeshType>::ErrorMsg(error); break;
00166 case KT_STL : return ImporterSTL<OpenMeshType>::ErrorMsg(error); break;
00167 case KT_OFF : return ImporterOFF<OpenMeshType>::ErrorMsg(error); break;
00168 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorMsg(error); break;
00169 case KT_VMI : return ImporterVMI<OpenMeshType>::ErrorMsg(error); break;
00170 }
00171 return "Unknown type";
00172 }
00173
00174 static bool LoadMask(const char * filename, int &mask)
00175 {
00176 bool err;
00177
00178 if(FileExtension(filename,"ply"))
00179 {
00180 err = ImporterPLY<OpenMeshType>::LoadMask(filename, mask);
00181 LastType()=KT_PLY;
00182 }
00183 else if(FileExtension(filename,"stl"))
00184 {
00185 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
00186 err = true;
00187 LastType()=KT_STL;
00188 }
00189 else if(FileExtension(filename,"off"))
00190 {
00191 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
00192 err = ImporterOFF<OpenMeshType>::LoadMask(filename, mask);
00193 LastType()=KT_OFF;
00194 }
00195 else if(FileExtension(filename,"obj"))
00196 {
00197 err = ImporterOBJ<OpenMeshType>::LoadMask(filename, mask);
00198 LastType()=KT_OBJ;
00199 }
00200 else
00201 {
00202 err = false;
00203 LastType()=KT_UNKNOWN;
00204 }
00205
00206 return err;
00207 }
00208 };
00209 }
00210 }
00211 }
00212
00213 #endif