Debian Codegraph @

vcg/wrap/io_trimesh/import.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 /****************************************************************************
25 History
26
27 $Log: import.h,v $
28 Revision 1.12 2007/03/08 12:21:26 granzuglia
29 fixed bug about string iterator
30
31 Revision 1.11 2006/03/29 09:27:07 cignoni
32 Added managemnt of non critical errors
33
34 Revision 1.10 2006/03/29 08:16:31 corsini
35 Minor change in LoadMask
36
37 Revision 1.9 2006/03/27 07:17:49 cignoni
38 Added generic LoadMask
39
40 Revision 1.8 2006/03/07 13:19:29 cignoni
41 First Release with OBJ import support
42
43 Revision 1.7 2006/02/28 14:50:00 corsini
44 Fix comments
45
46 Revision 1.6 2006/02/10 16:14:53 corsini
47 Fix typo
48
49 Revision 1.5 2006/02/10 08:14:32 cignoni
50 Refactored import. No more duplicated code
51
52 Revision 1.4 2006/02/09 16:04:45 corsini
53 Expose load mask
54
55 Revision 1.3 2006/01/11 10:37:45 cignoni
56 Added use of Callback
57
58 Revision 1.2 2005/01/26 22:43:19 cignoni
59 Add std:: to stl containers
60
61 Revision 1.1 2004/11/29 08:12:10 cignoni
62 Initial Update
63
64
65 ****************************************************************************/
66
67 #ifndef __VCGLIB_IMPORT
68 #define __VCGLIB_IMPORT
69
70 #include <wrap/io_trimesh/import_obj.h>
71 #include <wrap/io_trimesh/import_ply.h>
72 #include <wrap/io_trimesh/import_stl.h>
73 #include <wrap/io_trimesh/import_off.h>
74
75 #include <locale>
76
77 namespace vcg {
78 namespace tri {
79 namespace io {
80
81 /**
82 This class encapsulate a filter for automatically importing meshes by guessing
83 the right filter according to the extension
84 */
85
86 template <class OpenMeshType>
87 class Importer
88 {
89 private:
90 enum KnownTypes { KT_UNKNOWN, KT_PLY, KT_STL, KT_OFF, KT_OBJ };
91 static int &LastType()
92 {
93 static int lastType= KT_UNKNOWN;
94 return lastType;
95 }
96
97 public:
98 // simple aux function that returns true if a given file has a given extesnion
99 static bool FileExtension(std::string filename, std::string extension)
100 {
101 std::locale loc1 ;
102 std::use_facet<std::ctype<char> > ( loc1 ).tolower(&*filename.begin(),&*filename.rbegin());
103 std::use_facet<std::ctype<char> > ( loc1 ).tolower(&*extension.begin(),&*extension.rbegin());
104 std::string end=filename.substr(filename.length()-extension.length(),extension.length());
105 return end==extension;
106 }
107
108 // Open Mesh
109 static int Open(OpenMeshType &m, const char *filename, CallBackPos *cb=0)
110 {
111 int dummymask = 0;
112 return Open(m,filename,dummymask,cb);
113 }
114
115 // Open Mesh and return the load mask (the load mask must be initialized first)
116 static int Open(OpenMeshType &m, const char *filename, int &loadmask, CallBackPos *cb=0)
117 {
118 int err;
119 if(FileExtension(filename,"ply"))
120 {
121 err = ImporterPLY<OpenMeshType>::Open(m, filename, loadmask, cb);
122 LastType()=KT_PLY;
123 }
124 else if(FileExtension(filename,"stl"))
125 {
126 err = ImporterSTL<OpenMeshType>::Open(m, filename, loadmask, cb);
127 LastType()=KT_STL;
128 }
129 else if(FileExtension(filename,"off"))
130 {
131 err = ImporterOFF<OpenMeshType>::Open(m, filename, loadmask, cb);
132 LastType()=KT_OFF;
133 }
134 else if(FileExtension(filename,"obj"))
135 {
136 err = ImporterOBJ<OpenMeshType>::Open(m, filename, loadmask, cb);
137 LastType()=KT_OBJ;
138 }
139 else {
140 err=1;
141 LastType()=KT_UNKNOWN;
142 }
143
144 return err;
145 }
146
147 static bool ErrorCritical(int error)
148 {
149 switch(LastType())
150 {
151 case KT_PLY : return (error>0); break;
152 case KT_STL : return (error>0); break;
153 case KT_OFF : return (error>0); break;
154 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorCritical(error); break;
155 }
156
157 return true;
158 }
159
160 static const char *ErrorMsg(int error)
161 {
162 switch(LastType())
163 {
164 case KT_PLY : return ImporterPLY<OpenMeshType>::ErrorMsg(error); break;
165 case KT_STL : return ImporterSTL<OpenMeshType>::ErrorMsg(error); break;
166 case KT_OFF : return ImporterOFF<OpenMeshType>::ErrorMsg(error); break;
167 case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorMsg(error); break;
168 }
169 return "Unknown type";
170 }
171
172 static bool LoadMask(const char * filename, int &mask)
173 {
174 bool err;
175
176 if(FileExtension(filename,"ply"))
177 {
178 err = ImporterPLY<OpenMeshType>::LoadMask(filename, mask);
179 LastType()=KT_PLY;
180 }
181 else if(FileExtension(filename,"stl"))
182 {
183 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
184 err = true;
185 LastType()=KT_STL;
186 }
187 else if(FileExtension(filename,"off"))
188 {
189 mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
190 err = ImporterOFF<OpenMeshType>::LoadMask(filename, mask);
191 LastType()=KT_OFF;
192 }
193 else if(FileExtension(filename,"obj"))
194 {
195 err = ImporterOBJ<OpenMeshType>::LoadMask(filename, mask);
196 LastType()=KT_OBJ;
197 }
198 else
199 {
200 err = false;
201 LastType()=KT_UNKNOWN;
202 }
203
204 return err;
205 }
206 }; // end class
207 } // end Namespace tri
208 } // end Namespace io
209 } // end Namespace vcg
210
211 #endif