Debian Codegraph @

vcg/wrap/io_trimesh/util_dae.h

raw symbols SCIP
1 #ifndef __VCGLIB_UTILDAE
2 #define __VCGLIB_UTILDAE
3
4
5
6 #include <wrap/io_trimesh/additionalinfo.h>
7 #include <vcg/complex/trimesh/update/normal.h>
8 #include <vcg/complex/trimesh/allocate.h>
9
10 #include <wrap/io_trimesh/io_mask.h>
11
12 #include<QtXml/QDomDocument>
13 #include<QtCore/QFile>
14 #include <QtCore/QStringList>
15
16 #include<vcg/space/point3.h>
17 #include<vcg/space/texcoord2.h>
18 #include<vcg/space/color4.h>
19 #include<vcg/space/texcoord2.h>
20 #include <wrap/callback.h>
21
22 #include <vector>
23
24 #include <vcg/simplex/vertex/with/vn.h>
25 #include <vcg/simplex/edge/edge.h>
26
27 #include <wrap/gl/glu_tesselator.h>
28
29 namespace vcg {
30 namespace tri {
31 namespace io {
32 class InfoDAE
33 {
34 public:
35
36 InfoDAE()
37 {
38 doc = NULL;
39 }
40
41 ~InfoDAE()
42 {
43 delete doc;
44 }
45
46 QDomDocument* doc;
47 };
48
49 class AdditionalInfoDAE : public AdditionalInfo
50 {
51 public:
52 vcg::tri::io::InfoDAE* dae;
53 std::vector<QString> texturefile;
54
55 AdditionalInfoDAE()
56 :AdditionalInfo()
57 {
58 mask = 0;
59 numvert = 0;
60 numface = 0;
61 }
62
63 ~AdditionalInfoDAE()
64 {
65 delete dae;
66 texturefile.clear();
67 }
68 };
69
70 class UtilDAE
71 {
72 public:
73 enum DAEError
74 {
75 E_NOERROR, // 0
76 E_CANTOPEN, // 1
77 E_NOGEOMETRYLIBRARY, // 2
78 E_NOMESH, // 3
79 E_NOVERTEXPOSITION, // 4
80 E_NO3DVERTEXPOSITION, // 5
81 E_NO3DSCENE, // 6
82 E_INCOMPATIBLECOLLADA141FORMAT, //7
83 E_UNREFERENCEBLEDCOLLADAATTRIBUTE, // 8
84 E_NOPOLYGONALMESH
85 };
86
87 template<typename VERTEX_TYPE>
88 class MyPolygon
89 {
90 public:
91 typedef VERTEX_TYPE BaseVertexType;
92
93 int _nvert;
94 std::vector<VERTEX_TYPE*> _pv;
95 std::vector< vcg::TexCoord2<float> > _txc;
96
97
98 MyPolygon(int n)
99 :_nvert(n),_pv(_nvert),_txc(_nvert)
100 {
101 }
102 };
103
104 template<typename POLYGONAL_TYPE>
105 class PolygonalMesh
106 {
107 public:
108 typedef POLYGONAL_TYPE FaceType;
109
110 enum PERWEDGEATTRIBUTETYPE {NONE = 0,NORMAL = 1,MULTITEXTURECOORD = 2,MULTICOLOR = 4};
111
112 typedef typename FaceType::BaseVertexType VertexType;
113 typedef VertexType* VertexPointer;
114 typedef typename std::vector<VertexType>::iterator VertexIterator;
115 typedef typename std::vector<FaceType>::iterator PolygonIterator;
116
117 vcg::Box3<float> bbox;
118
119 std::vector<VertexType> vert;
120 std::vector<FaceType> _pols;
121
122 void generatePointsVector(std::vector<std::vector<vcg::Point3f> >& v)
123 {
124 for(typename PolygonalMesh::PolygonIterator itp = _pols.begin();itp != _pols.end();++itp)
125 {
126 v.push_back(std::vector<vcg::Point3f>());
127 for(typename std::vector<VertexPointer>::iterator itv = itp->_pv.begin();itv != itp->_pv.end();++itv)
128 {
129 v[v.size() - 1].push_back((*itv)->P());
130 }
131 }
132 }
133
134 void usePerWedgeAttributes(PERWEDGEATTRIBUTETYPE att,const unsigned int multitexture = 1,const unsigned int multicolor = 1)
135 {
136 if (att != NONE)
137 {
138 for(PolygonIterator itp = _pols.begin();itp != _pols.end();++itp)
139 {
140 if (att & MULTICOLOR) itp->usePerWedgeColor(multicolor);
141 if (att & MULTITEXTURECOORD) itp->usePerWedgeMultiTexture(multitexture);
142 if (att & NORMAL) itp->usePerWedgeNormal();
143 }
144 }
145 }
146
147 template<class TRIMESH>
148 void triangulate(TRIMESH& mesh)
149 {
150 std::vector<std::vector<vcg::Point3f> > pl;
151 mesh.vert.resize(vert.size());
152 int multicoor = 0;
153 //PolygonalMesh's points has been copied in TriangularMesh
154 for(size_t jj = 0;jj < mesh.vert.size();++jj)
155 mesh.vert[jj].P() = vert[jj].P();
156
157 bool texen = mesh.face.IsWedgeTexEnabled();
158 unsigned int totaltri = 0;
159 for(size_t ii = 0;ii < _pols.size();++ii)
160 totaltri += _pols[ii]._nvert - 2;
161
162 mesh.face.resize(totaltri);
163
164 //transform the polygonal mesh in a vector<vector<Point>>
165 generatePointsVector(pl);
166
167
168 int trioff = 0;
169 //foreach Polygon
170 for(size_t ii = 0;ii < pl.size();++ii)
171 {
172 std::vector<int> tx;
173 std::vector<std::vector<vcg::Point3f> > pl2(1);
174 pl2[0] = pl[ii];
175
176 vcg::glu_tesselator::tesselate(pl2,tx);
177 size_t ntri = tx.size() / 3;
178 assert(tx.size() % 3 == 0);
179
180
181 int polvert = 0;
182 //foreach triangle
183 for(size_t tr = 0;tr < ntri;++tr)
184 {
185
186 //typename TRIMESH::FaceType& f = mesh.face[tr];
187
188 //typename TRIMESH::FaceType& f = mesh.face[tr];
189 for(unsigned int tt = 0;tt < 3; ++tt)
190 {
191 mesh.face[trioff + tr].V(tt) = &(mesh.vert[_pols[ii]._pv[tx[3 * tr + tt]] - &(vert[0])]);
192 //vcg::Point3f ppp = mesh.face[tr].V(tt)->P();
193 if (texen)
194 {
195 /* f.WT(multicoor).U() = _pols[ii]._txc[polvert].U();
196 f.WT(multicoor).V() = _pols[ii]._txc[polvert].V();
197 f.WT(multicoor).N() = _pols[ii]._txc[polvert].N();*/
198
199 }
200 polvert = (polvert + 1) % _pols[ii]._nvert;
201 }
202 //mesh.face.push_back(f);
203 }
204 trioff += ntri;
205 }
206 assert(trioff == totaltri);
207 }
208 };
209
210 static const char *ErrorMsg(int error)
211 {
212 static const char * dae_error_msg[] =
213 {
214 "No errors",
215 "Can't open file",
216 "File without a geometry library",
217 "There isn't mesh in file",
218 "The meshes in file haven't the vertex position attribute",
219 "The importer assumes that the OpenMeshType uses a 3D point for the vertex position",
220 "There isn't any scene in Collada file",
221 "The input file is not compatible with COLLADA 1.41 standard format",
222 "Collada file is trying to referece an attribute that is not in the file",
223 "This version of Collada Importer support only triangular and polygonal mesh file"
224 };
225
226 if(error>9 || error<0) return "Unknown error";
227 else return dae_error_msg[error];
228 };
229 protected:
230 inline static void referenceToANodeAttribute(const QDomNode n,const QString& attr,QString& url_st)
231 {
232 url_st = n.toElement().attribute(attr);
233 int sz = url_st.size() - 1;
234 url_st = url_st.right(sz);
235 assert(url_st.size() != 0);
236 }
237
238 inline static QDomNode findNodeBySpecificAttributeValue(const QDomNodeList& ndl,const QString& attrname,const QString& attrvalue)
239 {
240 int ndl_size = ndl.size();
241 assert(ndl_size != 0);
242 int ind = 0;
243 while(ind < ndl_size)
244 {
245 QString st = ndl.at(ind).toElement().attribute(attrname);
246 if (st == attrvalue)
247 return ndl.at(ind);
248 ++ind;
249 }
250 return QDomNode();
251 }
252
253 inline static QDomNode findNodeBySpecificAttributeValue(const QDomNode n,const QString& tag,const QString& attrname,const QString& attrvalue)
254 {
255 return findNodeBySpecificAttributeValue(n.toElement().elementsByTagName(tag),attrname,attrvalue);
256 }
257
258 inline static QDomNode findNodeBySpecificAttributeValue(const QDomDocument n,const QString& tag,const QString& attrname,const QString& attrvalue)
259 {
260 return findNodeBySpecificAttributeValue(n.elementsByTagName(tag),attrname,attrvalue);
261 }
262
263 inline static bool isThereTag(const QDomNodeList& list)
264 {
265 return ((list.size() > 0) ? true : false);
266 }
267
268 inline static bool isThereTag(const QDomNode n,const QString& tagname)
269 {
270 return isThereTag(n.toElement().elementsByTagName(tagname));
271 }
272
273 inline static bool isThereTag(const QDomDocument n,const QString& tagname)
274 {
275 return isThereTag(n.elementsByTagName(tagname));
276 }
277
278
279 inline static QDomNode attributeSourcePerSimplex(const QDomNode n,const QDomDocument startpoint,const QString& sem)
280 {
281 QDomNodeList vertattr = n.toElement().elementsByTagName("input");
282 for(int ind = 0;ind < vertattr.size();++ind)
283 {
284 if (vertattr.at(ind).toElement().attribute("semantic") == sem)
285 {
286 QString url;
287 referenceToANodeAttribute(vertattr.at(ind),"source",url);
288 return findNodeBySpecificAttributeValue(startpoint,"source","id",url);
289 }
290 }
291 return QDomNode();
292 }
293
294 inline static void valueStringList(QStringList& res,const QDomNode srcnode,const QString& tag)
295 {
296 QDomNodeList list = srcnode.toElement().elementsByTagName(tag);
297 assert(list.size() == 1);
298 QString nd = list.at(0).firstChild().nodeValue();
299 res = nd.split(" ");
300 if (res.last() == "")
301 res.removeLast();
302
303 }
304
305 /*inline static bool removeChildNode(QDomNodeList*/
306
307 inline static bool removeChildNodeList(QDomNodeList& nodelst,const QString& tag = "", const QString& attribname = "", const QString& attribvalue = "")
308 {
309 for(int jj = 0;jj < nodelst.size();++jj)
310 {
311 removeChildNode(nodelst.at(jj),tag,attribname,attribvalue);
312 }
313 return true;
314 }
315
316
317 inline static bool removeChildNode(QDomNode node,const QString& tag = "", const QString& attribname = "", const QString& attribvalue = "")
318 {
319 QDomNodeList clst = node.childNodes();
320 for(int ii = 0;ii < clst.size();++ii)
321 {
322 QDomNode oldchild = node.childNodes().at(ii);
323 if (tag != "")
324 {
325 if ((attribname != "") && (attribvalue != ""))
326 {
327 if (clst.at(ii).toElement().attribute(attribname) == attribvalue)
328 node.removeChild(oldchild);
329 }
330 else
331 {
332 QString nm = clst.at(ii).nodeName();
333 if (clst.at(ii).nodeName() == tag)
334 {
335 node.removeChild(oldchild);
336 }
337 }
338 }
339 else node.removeChild(oldchild);
340 }
341 return true;
342 }
343
344 static void ParseRotationMatrix(vcg::Matrix44f& m,const std::vector<QDomNode>& t)
345 {
346 vcg::Matrix44f tmp;
347 tmp.SetIdentity();
348 for(unsigned int ii = 0;ii < t.size();++ii)
349 {
350 QString rt = t[ii].firstChild().nodeValue();
351 QStringList rtl = rt.split(" ");
352 if (rtl.last() == "") rtl.removeLast();
353 assert(rtl.size() == 4);
354 tmp.SetRotate(rtl.at(3).toFloat(),vcg::Point3f(rtl.at(0).toFloat(),rtl.at(1).toFloat(),rtl.at(2).toFloat()));
355 tmp *= tmp;
356 }
357 m = m * tmp;
358 }
359
360 static void AddTranslation(vcg::Matrix44f& m,const QDomNode t)
361 {
362 QDomNode tr = t.firstChild();
363 QString coord = tr.nodeValue();
364 QStringList coordlist = coord.split(" ");
365 if (coordlist.last() == "")
366 coordlist.removeLast();
367 assert(coordlist.size() == 3);
368 m[0][0] = 1.0f;
369 m[1][1] = 1.0f;
370 m[2][2] = 1.0f;
371 m[3][3] = 1.0f;
372 m[0][3] = coordlist.at(0).toFloat();
373 m[1][3] = coordlist.at(1).toFloat();
374 m[2][3] = coordlist.at(2).toFloat();
375 }
376
377 static void TransfMatrix(const QDomNode parentnode,const QDomNode presentnode,vcg::Matrix44f& m)
378 {
379 if (presentnode == parentnode) return;
380 else
381 {
382 QDomNode par = presentnode.parentNode();
383 std::vector<QDomNode> rotlist;
384 QDomNode trans;
385 for(int ch = 0;ch < par.childNodes().size();++ch)
386 {
387 if (par.childNodes().at(ch).nodeName() == "rotate")
388 rotlist.push_back(par.childNodes().at(ch));
389 else if (par.childNodes().at(ch).nodeName() == "translate")
390 {
391 trans = par.childNodes().at(ch);
392 }
393 }
394 vcg::Matrix44f tmp;
395 tmp.SetIdentity();
396 if (!trans.isNull()) AddTranslation(tmp,trans);
397 ParseRotationMatrix(tmp,rotlist);
398 m = m * tmp;
399 TransfMatrix(parentnode,par,m);
400 }
401 }
402
403 inline static int findOffSetForASingleSimplex(QDomNode node)
404 {
405 QDomNodeList wedatts = node.toElement().elementsByTagName("input");
406 int max = 0;
407 if (wedatts.size() == 0) return -1;
408 else
409 {
410 for(int ii = 0;ii < wedatts.size();++ii)
411 {
412 int tmp = wedatts.at(ii).toElement().attribute("offset").toInt();
413 if (tmp > max) max = tmp;
414 }
415 }
416 return max + 1;
417 }
418
419 inline static int findStringListAttribute(QStringList& list,const QDomNode node,const QDomNode poly,const QDomDocument startpoint,const char* token)
420 {
421 int offset;
422 if (!node.isNull())
423 {
424 offset = node.toElement().attribute("offset").toInt();
425 QDomNode st = attributeSourcePerSimplex(poly,startpoint,token);
426 valueStringList(list,st,"float_array");
427 }
428 return offset;
429 }
430
431 inline static QDomNode textureFinder(const QString& textname,const QDomDocument doc)
432 {
433 //visual_scene -> instance_material
434 QDomNodeList vis_scn = doc.elementsByTagName("library_visual_scenes");
435 if (vis_scn.size() != 1)
436 return QDomNode();
437 QDomNode symb = findNodeBySpecificAttributeValue(vis_scn.at(0),QString("instance_material"),QString("symbol"),textname);
438 if (symb.isNull())
439 return QDomNode();
440 QString tar = symb.toElement().attribute("target");
441 if (tar.isNull())
442 return QDomNode();
443 tar = tar.remove('#');
444
445 //library_material -> material -> instance_effect
446 QDomNodeList lib_mat = doc.elementsByTagName("library_materials");
447 if (lib_mat.size() != 1)
448 return QDomNode();
449 QDomNode material = findNodeBySpecificAttributeValue(lib_mat.at(0),QString("material"),QString("id"),tar);
450 if (material.isNull())
451 return QDomNode();
452 QDomNodeList in_eff = material.toElement().elementsByTagName("instance_effect");
453 if (vis_scn.size() == 0)
454 return QDomNode();
455 QString url = in_eff.at(0).toElement().attribute("url");
456 if ((url.isNull()) || (url == ""))
457 return QDomNode();
458 url = url.remove('#');
459
460 //library_effects -> effect -> instance_effect
461 QDomNodeList lib_eff = doc.elementsByTagName("library_effects");
462 if (lib_eff.size() != 1)
463 return QDomNode();
464 QDomNode effect = findNodeBySpecificAttributeValue(lib_eff.at(0),QString("effect"),QString("id"),url);
465 if (effect.isNull())
466 return QDomNode();
467 QDomNodeList init_from = effect.toElement().elementsByTagName("init_from");
468 if (init_from.size() == 0)
469 return QDomNode();
470 QString img_id = init_from.at(0).toElement().text();
471 if ((img_id.isNull()) || (img_id == ""))
472 return QDomNode();
473
474 //library_images -> image
475 QDomNodeList lib_img = doc.elementsByTagName("library_images");
476 if (lib_img.size() != 1)
477 return QDomNode();
478 QDomNode img = findNodeBySpecificAttributeValue(lib_img.at(0),QString("image"),QString("id"),img_id);
479 if (img.isNull())
480 return QDomNode();
481 return img;
482 }
483
484 static int indexTextureByImgNode(const QDomDocument doc,const QDomNode node)
485 {
486 QDomNodeList libim = doc.elementsByTagName(QString("library_images"));
487 if (libim.size() != 1)
488 return -1;
489 QDomNodeList imgs = libim.at(0).toElement().elementsByTagName("image");
490
491 int ii = 0;
492 bool found = false;
493 while((ii < imgs.size()) && (!found))
494 {
495 if (imgs.at(ii) == node)
496 found = true;
497 else ++ii;
498 }
499 if (found)
500 return ii;
501 else
502 return -1;
503 }
504
505 struct WedgeAttribute
506 {
507 QDomNode wnsrc;
508 QStringList wn;
509 int offnm;
510
511 QDomNode wtsrc;
512 QStringList wt;
513 int stride;
514 int offtx;
515
516 QDomNode wcsrc;
517 QStringList wc;
518 int offcl;
519 };
520 };
521 }
522 }
523 }
524
525 #endif