Debian Codegraph @

vcg/wrap/io_trimesh/export_idtf.h

raw symbols SCIP
1 #ifndef __VCGLIB_EXPORTERIDTF
2 #define __VCGLIB_EXPORTERIDTF
3
4
5 #include <sstream>
6 #include <fstream>
7 #include <ostream>
8 #include <vcg/complex/trimesh/update/bounding.h>
9 #include <wrap/io_trimesh/io_mask.h>
10
11
12 class TextUtility
13 {
14 public:
15 template<typename NUMERICTYPE>
16 static std::string nmbToStr(NUMERICTYPE n)
17 {
18 std::stringstream ss;
19 ss << n;
20 return ss.str();
21 }
22 };
23
24 class Output_File
25 {
26 public:
27 Output_File(const std::string& file)
28 :_file()
29 {
30 _file.open(file.c_str(),std::ios::out);
31 }
32
33 void write(unsigned int tabl,const std::string& st)
34 {
35 std::string tmp;
36 for(unsigned int ii = 0;ii < tabl;++ii)
37 tmp += '\t';
38 _file << tmp << st << std::endl;
39 }
40
41 ~Output_File()
42 {
43 _file.close();
44 }
45
46 private:
47 std::ofstream _file;
48 std::string _tab;
49 };
50
51 #include <QString>
52 #include <QtGlobal>
53 #include <fstream>
54 #include <QImage>
55
56
57 namespace vcg {
58 namespace tri {
59 namespace io {
60
61
62 namespace QtUtilityFunctions
63 {
64 static void splitFilePath(const QString& filepath,QStringList& trim_path)
65 {
66 QString file_uniformed = filepath;
67 file_uniformed.replace(QString("\\"),QString("/"));
68 trim_path = file_uniformed.split("/");
69 }
70
71 static QString fileNameFromTrimmedPath(const QStringList& file_path)
72 {
73
74 if (file_path.size() > 0)
75 return file_path.at(file_path.size() - 1);
76 else
77 return QString();
78 }
79
80 static QString fileNameFromPath(const QString& filepath)
81 {
82 QStringList list;
83 splitFilePath(filepath,list);
84 return fileNameFromTrimmedPath(list);
85 }
86
87 static QString pathWithoutFileName(const QString& filepath)
88 {
89 QString tmp(filepath);
90 tmp.remove(fileNameFromPath(filepath));
91 return tmp;
92 }
93
94 static QString fileExtension(const QString& filepath)
95 {
96 QStringList trim_list;
97 splitFilePath(filepath,trim_list);
98 QString file = fileNameFromTrimmedPath(trim_list);
99 trim_list = file.split(".");
100 return trim_list.at(trim_list.size() - 1);
101 }
102 }
103
104 class TGA_Exporter
105 {
106 public:
107
108 struct TGAHeader
109 {
110 unsigned char identsize;
111 unsigned char colourmaptype;
112 unsigned char imagetype;
113
114 unsigned char colormapspecs[5];
115
116 short xstart;
117 short ystart;
118 short width;
119 short height;
120 unsigned char bits;
121 unsigned char descriptor;
122 };
123
124 static void convert(const QString& outfile,const QImage& im)
125 {
126 TGAHeader tga;
127 tga.identsize = 0;
128 tga.colourmaptype = 0;
129 tga.imagetype = 2;
130
131 memset(tga.colormapspecs,0,5);
132 tga.xstart = (short) im.offset().x();
133 tga.ystart = (short) im.offset().y();
134 tga.height = (short) im.height();
135 tga.width = (short) im.width();
136
137 std::ofstream file(qPrintable(outfile),std::ofstream::binary);
138 unsigned char* tmpchan;
139 int totbyte;
140 if (im.hasAlphaChannel())
141 {
142 //is a 8-digits binary number code
143 // always 0 0 | mirroring | bits
144 //(future uses)| image | for alpha-channel
145 //--------------------------------------------
146 // 7 6 | 5 4 | 3 2 1 0
147 //--------------------------------------------
148 // 0 0 | 1 0 | 1 0 0 0
149 tga.descriptor = (char) 40;
150 tga.bits = (char) 32;
151 }
152 else
153 {
154 //is a 8-digits binary number code
155 // always 0 0 | mirroring | bits
156 //(future uses)| image | for alpha-channel
157 //--------------------------------------------
158 // 7 6 | 5 4 | 3 2 1 0
159 //--------------------------------------------
160 // 0 0 | 1 0 | 0 0 0 0
161 tga.descriptor = (char) 32;
162 tga.bits = (char) 24;
163 }
164
165 totbyte = tga.height * tga.width * (tga.bits / 8);
166
167 if (im.hasAlphaChannel())
168 tmpchan = const_cast<unsigned char*>(im.bits());
169 else
170 {
171 tmpchan = new unsigned char[totbyte];
172
173 int ii = 0;
174 while(ii < totbyte)
175 {
176 tmpchan[ii] = const_cast<unsigned char*>(im.bits())[ii + (ii/3)];
177 ++ii;
178 }
179 }
180
181 file.write((char *) &tga,sizeof(tga));
182 file.write(reinterpret_cast<const char*>(tmpchan),totbyte);
183 file.close();
184 }
185
186 template<typename SaveMeshType>
187 static void convertTexturesFiles(SaveMeshType& m,const QString& file_path,QStringList& conv_file)
188 {
189 for(unsigned int ii = 0; ii < m.textures.size(); ++ii)
190 {
191 QString qtmp(m.textures[ii].c_str());
192 QString ext = QtUtilityFunctions::fileExtension(qtmp);
193 if (ext.toLower() != "tga")
194 {
195 QImage img(qtmp);
196 QString stmp;
197 if ((file_path.at(file_path.length() - 1) != '/') || (file_path.at(file_path.length() - 1) != '\\'))
198 stmp = file_path + QString("/");
199 else
200 stmp = file_path;
201 qtmp = stmp + qtmp.remove(ext) + "tga";
202 m.textures[ii] = qtmp.toStdString();
203 TGA_Exporter::convert(qtmp,img);
204 conv_file.push_back(qtmp);
205 }
206 }
207 }
208
209 static void removeConvertedTexturesFiles(const QStringList& conv_file)
210 {
211 for(unsigned int ii = 0;ii < conv_file.size();++ii)
212 {
213 QDir dir(QtUtilityFunctions::pathWithoutFileName(conv_file[ii]));
214 dir.remove(QtUtilityFunctions::fileNameFromPath(conv_file[ii]));
215 }
216 }
217 };
218
219
220
221 template<typename SaveMeshType>
222 class ExporterIDTF
223 {
224
225 public:
226 typedef typename SaveMeshType::VertexPointer VertexPointer;
227 typedef typename SaveMeshType::ScalarType ScalarType;
228 typedef typename SaveMeshType::VertexType VertexType;
229 typedef typename SaveMeshType::FaceType FaceType;
230 typedef typename SaveMeshType::ConstVertexIterator ConstVertexIterator;
231 typedef typename SaveMeshType::VertexIterator VertexIterator;
232 typedef typename SaveMeshType::FaceIterator FaceIterator;
233 typedef typename SaveMeshType::ConstFaceIterator ConstFaceIterator;
234 typedef typename SaveMeshType::CoordType CoordType;
235
236 enum IDTFError
237 {
238 E_NOERROR // 0
239 };
240
241 static const char *ErrorMsg(int error)
242 {
243 static const char * dae_error_msg[] =
244 {
245 "No errors"
246 };
247
248 if(error>0 || error<0) return "Unknown error";
249 else return dae_error_msg[error];
250 };
251
252 static QStringList convertInTGATextures(SaveMeshType& m,const QString& path,QStringList& textures_to_be_restored)
253 {
254 //if there are textures file that aren't in tga format I have to convert them
255 //I maintain the converted file name (i.e. file_path + originalname without extension + tga) in mesh.textures but I have to revert to the original ones
256 //before the function return.
257 for(unsigned int ii = 0; ii < m.textures.size();++ii)
258 textures_to_be_restored.push_back(m.textures[ii].c_str());
259
260 //tmp vector to save the tga created files that should be deleted.
261 QStringList convfile;
262 vcg::tri::io::TGA_Exporter::convertTexturesFiles(m,path,convfile);
263 return convfile;
264 }
265
266 static void removeConvertedTGATextures(const QStringList& convfile)
267 {
268 //if some tga files have been created I have to delete them
269 vcg::tri::io::TGA_Exporter::removeConvertedTexturesFiles(convfile);
270 }
271
272 static void restoreConvertedTextures(SaveMeshType& mesh_with_textures_to_be_restored,const QStringList& textures_to_be_restored)
273 {
274 mesh_with_textures_to_be_restored.textures.clear();
275 for(QStringList::ConstIterator it = textures_to_be_restored.begin();it != textures_to_be_restored.end();++it)
276 mesh_with_textures_to_be_restored.textures.push_back(it->toStdString());
277 }
278
279 static int Save(SaveMeshType& m,const char* file,const int mask)
280 {
281
282 Output_File idtf(file);
283
284 idtf.write(0,"FILE_FORMAT \"IDTF\"");
285 idtf.write(0,"FORMAT_VERSION 100\n");
286
287 idtf.write(0,"NODE \"MODEL\" {");
288 idtf.write(1,"NODE_NAME \"VcgMesh01\"");
289 idtf.write(1,"PARENT_LIST {");
290 idtf.write(2,"PARENT_COUNT 1");
291 idtf.write(2,"PARENT 0 {");
292 idtf.write(3,"PARENT_NAME \"<NULL>\"");
293 idtf.write(3,"PARENT_TM {");
294 idtf.write(4,"1.000000 0.000000 0.000000 0.000000");
295 idtf.write(4,"0.000000 1.000000 0.000000 0.000000");
296 idtf.write(4,"0.000000 0.000000 1.000000 0.000000");
297 idtf.write(4,"0.000000 0.000000 0.000000 1.000000");
298 idtf.write(3,"}");
299 idtf.write(2,"}");
300 idtf.write(1,"}");
301 idtf.write(1,"RESOURCE_NAME \"MyVcgMesh01\"");
302 idtf.write(0,"}");
303
304
305 if (mask & Mask::IOM_WEDGTEXCOORD)
306 {
307 idtf.write(0,"");
308 idtf.write(0,"RESOURCE_LIST \"SHADER\" {");
309 idtf.write(1,"RESOURCE_COUNT " + TextUtility::nmbToStr(m.textures.size()));
310 for(unsigned int ii = 0; ii < m.textures.size(); ++ii)
311 {
312 idtf.write(1,"RESOURCE " + TextUtility::nmbToStr(ii) + " {");
313 idtf.write(2,"RESOURCE_NAME \"ModelShader" + TextUtility::nmbToStr(ii) +"\"");
314 idtf.write(2,"SHADER_MATERIAL_NAME \"Material1\"");
315 idtf.write(2,"SHADER_ACTIVE_TEXTURE_COUNT 1");
316 idtf.write(2,"SHADER_TEXTURE_LAYER_LIST {");
317 idtf.write(3,"TEXTURE_LAYER 0 {");
318 idtf.write(4,"TEXTURE_NAME \"Texture" + TextUtility::nmbToStr(ii) +"\"");
319 idtf.write(3,"}");
320 idtf.write(2,"}");
321 }
322 idtf.write(1,"}");
323 idtf.write(0,"}");
324 idtf.write(0,"");
325 idtf.write(0,"RESOURCE_LIST \"MATERIAL\" {");
326 idtf.write(1,"RESOURCE_COUNT 1");
327 idtf.write(1,"RESOURCE 0 {");
328 idtf.write(2,"RESOURCE_NAME \"Material1\"");
329 idtf.write(2,"MATERIAL_AMBIENT 0.2 0.2 0.2");
330 idtf.write(2,"MATERIAL_DIFFUSE 0.8 0.8 0.8");
331 idtf.write(2,"MATERIAL_SPECULAR 0.0 0.0 0.0");
332 idtf.write(2,"MATERIAL_EMISSIVE 0.0 0.0 0.0");
333 idtf.write(2,"MATERIAL_REFLECTIVITY 0.100000");
334 idtf.write(2,"MATERIAL_OPACITY 1.000000");
335 idtf.write(1,"}");
336 idtf.write(0,"}");
337 idtf.write(0,"");
338 idtf.write(0,"RESOURCE_LIST \"TEXTURE\" {");
339 idtf.write(1,"RESOURCE_COUNT " + TextUtility::nmbToStr(m.textures.size()));
340 for(unsigned int ii = 0; ii < m.textures.size();++ii)
341 {
342 idtf.write(1,"RESOURCE " + TextUtility::nmbToStr(ii) + " {");
343 idtf.write(2,"RESOURCE_NAME \"Texture" + TextUtility::nmbToStr(ii) + "\"");
344 idtf.write(2,"TEXTURE_PATH \"" + m.textures[ii] + "\"");
345 idtf.write(1,"}");
346 }
347 idtf.write(0,"}");
348 }
349 idtf.write(0,"");
350 idtf.write(0,"RESOURCE_LIST \"MODEL\" {");
351 idtf.write(1,"RESOURCE_COUNT 1");
352 idtf.write(1,"RESOURCE 0 {");
353 idtf.write(2,"RESOURCE_NAME \"MyVcgMesh01\"");
354 idtf.write(2,"MODEL_TYPE \"MESH\"");
355 idtf.write(2,"MESH {");
356 idtf.write(3,"FACE_COUNT " + TextUtility::nmbToStr(m.face.size()));
357 idtf.write(3,"MODEL_POSITION_COUNT " + TextUtility::nmbToStr(m.vert.size()));
358 idtf.write(3,"MODEL_NORMAL_COUNT " + TextUtility::nmbToStr(m.face.size() * 3));
359 if (mask & vcg::tri::io::Mask::IOM_VERTCOLOR)
360 idtf.write(3,"MODEL_DIFFUSE_COLOR_COUNT " + TextUtility::nmbToStr(m.face.size() * 3));
361 else
362 idtf.write(3,"MODEL_DIFFUSE_COLOR_COUNT 0");
363 idtf.write(3,"MODEL_SPECULAR_COLOR_COUNT 0");
364 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) idtf.write(3,"MODEL_TEXTURE_COORD_COUNT " + TextUtility::nmbToStr(m.face.size() * 3));
365 else idtf.write(3,"MODEL_TEXTURE_COORD_COUNT 0");
366 idtf.write(3,"MODEL_BONE_COUNT 0");
367 unsigned int mod_sha;
368 if (m.textures.size() == 0)
369 mod_sha = 1;
370 else
371 mod_sha = m.textures.size();
372 idtf.write(3,"MODEL_SHADING_COUNT " + TextUtility::nmbToStr(mod_sha));
373 idtf.write(3,"MODEL_SHADING_DESCRIPTION_LIST {");
374 unsigned int hh = 0;
375 do
376 {
377 idtf.write(4,"SHADING_DESCRIPTION " + TextUtility::nmbToStr(hh) + " {");
378 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
379 {
380 idtf.write(5,"TEXTURE_LAYER_COUNT 1");
381 idtf.write(5,"TEXTURE_COORD_DIMENSION_LIST {");
382 idtf.write(6,"TEXTURE_LAYER 0 DIMENSION: 2");
383 idtf.write(5,"}");
384 idtf.write(5,"SHADER_ID 0");
385 }
386 else
387 {
388 idtf.write(5,"TEXTURE_LAYER_COUNT 0");
389 idtf.write(5,"SHADER_ID 0");
390 }
391 idtf.write(4,"}");
392 ++hh;
393 }
394 while(hh < m.textures.size());
395 idtf.write(3,"}");
396 idtf.write(3,"MESH_FACE_POSITION_LIST {");
397 for(ConstFaceIterator fit = m.face.begin();fit != m.face.end();++fit)
398 {
399 idtf.write(4,TextUtility::nmbToStr(fit->V(0) - &(*m.vert.begin())) + " " +
400 TextUtility::nmbToStr(fit->V(1) - &(*m.vert.begin())) + " " +
401 TextUtility::nmbToStr(fit->V(2) - &(*m.vert.begin())));
402 }
403 idtf.write(3,"}");
404
405 idtf.write(3,"MESH_FACE_NORMAL_LIST {");
406 unsigned int nn = 0;
407 for(ConstFaceIterator fit = m.face.begin();fit != m.face.end();++fit)
408 {
409 idtf.write(4,TextUtility::nmbToStr(nn) + " " +
410 TextUtility::nmbToStr(nn + 1) + " " +
411 TextUtility::nmbToStr(nn + 2));
412 nn += 3;
413 }
414 idtf.write(3,"}");
415
416 idtf.write(3,"MESH_FACE_SHADING_LIST {");
417 for(FaceIterator fit = m.face.begin();fit != m.face.end();++fit)
418 {
419 unsigned int texind = 0;
420 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
421 texind = fit->WT(0).N();
422 idtf.write(4,TextUtility::nmbToStr(texind));
423 }
424 idtf.write(3,"}");
425
426 if (mask & vcg::tri::io::Mask::IOM_VERTCOLOR)
427 {
428 idtf.write(3,"MESH_FACE_DIFFUSE_COLOR_LIST {");
429 nn = 0;
430 for(FaceIterator fit = m.face.begin();fit != m.face.end();++fit)
431 {
432 idtf.write(4,TextUtility::nmbToStr(nn) + " " +
433 TextUtility::nmbToStr(nn + 2) + " " +
434 TextUtility::nmbToStr(nn + 1));
435 nn += 3;
436 }
437 idtf.write(3,"}");
438 }
439 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
440 {
441 idtf.write(3,"MESH_FACE_TEXTURE_COORD_LIST {");
442 for(unsigned int ii = 0; ii < m.face.size();++ii)
443 {
444 idtf.write(4,"FACE " + TextUtility::nmbToStr(ii) + " {");
445 idtf.write(5,"TEXTURE_LAYER 0 TEX_COORD: " + TextUtility::nmbToStr(ii * 3) + " " + TextUtility::nmbToStr(ii * 3 + 1) + " " + TextUtility::nmbToStr(ii * 3 + 2));
446 idtf.write(4,"}");
447 }
448 idtf.write(3,"}");
449 }
450
451 idtf.write(3,"MODEL_POSITION_LIST {");
452 //vcg::tri::UpdateBounding<SaveMeshType>::Box(m);
453 //ScalarType diag = m.bbox.Diag();
454 //CoordType center = m.bbox.Center();
455 for(ConstVertexIterator vit = m.vert.begin();vit != m.vert.end();++vit)
456 {
457 CoordType tmp = vit->P();// - center);// /diag;
458 idtf.write(4,TextUtility::nmbToStr(-tmp.X()) + " " +
459 TextUtility::nmbToStr(tmp.Z()) + " " +
460 TextUtility::nmbToStr(tmp.Y()));
461 }
462 idtf.write(3,"}");
463
464 idtf.write(3,"MODEL_NORMAL_LIST {");
465 for(FaceIterator fitn = m.face.begin();fitn != m.face.end();++fitn)
466 {
467 for(unsigned int ii = 0;ii < 3;++ii)
468 {
469 fitn->N().Normalize();
470 idtf.write(4,TextUtility::nmbToStr(-fitn->N().X()) + " " +
471 TextUtility::nmbToStr(fitn->N().Z()) + " " +
472 TextUtility::nmbToStr(fitn->N().Y()));
473 }
474 }
475 idtf.write(3,"}");
476
477 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
478 {
479 idtf.write(3,"MODEL_TEXTURE_COORD_LIST {");
480 for(FaceIterator fitn = m.face.begin();fitn != m.face.end();++fitn)
481 {
482 for(unsigned int ii = 0;ii < 3;++ii)
483 {
484 idtf.write(4,TextUtility::nmbToStr(fitn->WT(ii).U()) + " " +
485 TextUtility::nmbToStr(-fitn->WT(ii).V()) + " " + TextUtility::nmbToStr(0.0f) + " " + TextUtility::nmbToStr(0.0f));
486 }
487 }
488 idtf.write(3,"}");
489 }
490
491 if (mask & vcg::tri::io::Mask::IOM_VERTCOLOR)
492 {
493 idtf.write(3,"MODEL_DIFFUSE_COLOR_LIST {");
494 //ScalarType diag = m.bbox.Diag();
495 //CoordType center = m.bbox.Center();
496 for(FaceIterator vit = m.face.begin();vit != m.face.end();++vit)
497 {
498 for (unsigned int ii =0; ii <3;++ii)
499 idtf.write(4,TextUtility::nmbToStr((int) vit->V(ii)->C().X()) + " " +
500 TextUtility::nmbToStr((int) vit->V(ii)->C().Y()) + " " +
501 TextUtility::nmbToStr((int) vit->V(ii)->C().Z()) + " 0");
502 }
503 idtf.write(3,"}");
504 }
505
506 idtf.write(2,"}");
507 idtf.write(1,"}");
508 idtf.write(0,"}");
509
510 if (mask & vcg::tri::io::Mask::IOM_VERTCOLOR)
511 {
512 idtf.write(0,"RESOURCE_LIST \"SHADER\" {");
513 idtf.write(1,"RESOURCE_COUNT 1");
514 idtf.write(1,"RESOURCE 0 {");
515 idtf.write(2,"RESOURCE_NAME \"VcgMesh010\"");
516 idtf.write(2,"ATTRIBUTE_USE_VERTEX_COLOR \"TRUE\"");
517 idtf.write(2,"SHADER_ACTIVE_TEXTURE_COUNT 0");
518 idtf.write(1,"}");
519 idtf.write(0,"}");
520 }
521
522 if (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
523 {
524 idtf.write(0,"");
525 idtf.write(0,"MODIFIER \"SHADING\" {");
526 idtf.write(1,"MODIFIER_NAME \"VcgMesh01\"");
527 idtf.write(1,"PARAMETERS {");
528 idtf.write(2,"SHADER_LIST_COUNT " + TextUtility::nmbToStr(m.textures.size()));
529 idtf.write(2,"SHADING_GROUP {");
530 for(unsigned int ii = 0; ii < m.textures.size();++ii)
531 {
532 idtf.write(3,"SHADER_LIST " + TextUtility::nmbToStr(ii) + "{");
533 idtf.write(4,"SHADER_COUNT 1");
534 idtf.write(4,"SHADER_NAME_LIST {");
535 idtf.write(5,"SHADER 0 NAME: \"ModelShader" + TextUtility::nmbToStr(ii) + "\"");
536 idtf.write(4,"}");
537 idtf.write(3,"}");
538 }
539 idtf.write(2,"}");
540 idtf.write(1,"}");
541 idtf.write(0,"}");
542 }
543
544 return E_NOERROR;
545 }
546
547 static int GetExportMaskCapability()
548 {
549 int capability = 0;
550
551 //vert
552 capability |= vcg::tri::io::Mask::IOM_VERTNORMAL;
553 capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
554
555 ////wedg
556 capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD;
557 capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL;
558
559 return capability;
560 }
561 };
562 }
563 }
564 }
565
566 #endif