Debian Codegraph @

vcg/wrap/dae/util_dae.h

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