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::locale loc1 ;
00100 std::use_facet<std::ctype<char> > ( loc1 ).tolower(&*filename.begin(),&*filename.rbegin());
00101 std::use_facet<std::ctype<char> > ( loc1 ).tolower(&*extension.begin(),&*extension.rbegin());
00102 std::string end=filename.substr(filename.length()-extension.length(),extension.length());
00103 return end==extension;
00104 }
00105
00106
00107 static int Open(OpenMeshType &m, const char *filename, CallBackPos *cb=0)
00108 {
00109 int dummymask = 0;
00110 return Open(m,filename,dummymask,cb);
00111 }
00112
00113
00114 static int Open(OpenMeshType &m, const char *filename, int &loadmask, CallBackPos *cb=0)
00115 {
00116 int err;
00117 if(FileExtension(filename,"ply"))
00118 {
00119 err = ImporterPLY<OpenMeshType>::Open(m, filename, loadmask, cb);
00120 LastType()=KT_PLY;
00121 }
00122 else if(FileExtension(filename,"stl"))
00123 {
00124 err = ImporterSTL<OpenMeshType>::Open(m, filename, loadmask, cb);
00125 LastType()=KT_STL;
00126 }
00127 else if(FileExtension(filename,"off"))
00128 {
00129 err = ImporterOFF<OpenMeshType>::Open(m, filename, loadmask, cb);
00130 LastType()=KT_OFF;
00131 }
00132 else if(FileExtension(filename,"obj"))
00133 {
00134 err = ImporterOBJ<OpenMeshType>::Open(m, filename, loadmask, cb);
00135 LastType()=KT_OBJ;
00136 }
00137 else if(FileExtension(filename,"vmi"))
00138 {
00139 err = ImporterVMI<OpenMeshType>::Open(m, filename, loadmask, cb);
00140 LastType()=KT_VMI;
00141 }
00142 else {
00143 err=1;
00144 LastType()=KT_UNKNOWN;
00145 }
00146
00147 return err;
00148 }
00149
00150 static bool ErrorCritical(int error)
00151 {
00152 switch(LastType())
00153 {
00154 case KT_PLY : return (error>0); break;
00155 case KT_STL : return (error>0); break;
00156 case KT_OFF : return (error>0); break;
00157 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorCritical(error); break;
00158 }
00159
00160 return true;
00161 }
00162
00163 static const char *ErrorMsg(int error)
00164 {
00165 switch(LastType())
00166 {
00167 case KT_PLY : return ImporterPLY<OpenMeshType>::ErrorMsg(error); break;
00168 case KT_STL : return ImporterSTL<OpenMeshType>::ErrorMsg(error); break;
00169 case KT_OFF : return ImporterOFF<OpenMeshType>::ErrorMsg(error); break;
00170 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorMsg(error); break;
00171 case KT_VMI : return ImporterVMI<OpenMeshType>::ErrorMsg(error); break;
00172 }
00173 return "Unknown type";
00174 }
00175
00176 static bool LoadMask(const char * filename, int &mask)
00177 {
00178 bool err;
00179
00180 if(FileExtension(filename,"ply"))
00181 {
00182 err = ImporterPLY<OpenMeshType>::LoadMask(filename, mask);
00183 LastType()=KT_PLY;
00184 }
00185 else if(FileExtension(filename,"stl"))
00186 {
00187 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
00188 err = true;
00189 LastType()=KT_STL;
00190 }
00191 else if(FileExtension(filename,"off"))
00192 {
00193 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
00194 err = ImporterOFF<OpenMeshType>::LoadMask(filename, mask);
00195 LastType()=KT_OFF;
00196 }
00197 else if(FileExtension(filename,"obj"))
00198 {
00199 err = ImporterOBJ<OpenMeshType>::LoadMask(filename, mask);
00200 LastType()=KT_OBJ;
00201 }
00202 else
00203 {
00204 err = false;
00205 LastType()=KT_UNKNOWN;
00206 }
00207
00208 return err;
00209 }
00210 };
00211 }
00212 }
00213 }
00214
00215 #endif