colladaformat.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004-2008                                           \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 #ifndef _COLLADA_FORMAT_H
00024 #define _COLLADA_FORMAT_H
00025 
00026 #include <wrap/dae/xmldocumentmanaging.h>
00027 #include <vcg/space/point4.h>
00028 #include <vcg/space/color4.h>
00029 
00030 #include <QtGui/QImage>
00031 #include <QtCore/QVector>
00032 
00033 #include <QDateTime>
00034 
00035 
00036 
00037 
00038 template<typename POINTTYPE>
00039 struct CoordNumber{public:      static unsigned int coord()     {               return 0;       }};
00040 
00041 template<> struct CoordNumber<vcg::Point2f> { public:   static unsigned int coord() { return 2; } };
00042 template<> struct CoordNumber<vcg::Point3f> { public:   static unsigned int coord() {   return 3;       } };
00043 template<> struct CoordNumber<vcg::Point4f> { public: static unsigned int coord() {     return 4;       } };
00044 
00045 template<> struct CoordNumber<vcg::Color4b> { public:   static unsigned int coord() { return 4; } };
00046 
00047 
00048 namespace Collada
00049 {
00050 namespace Tags
00051 {
00052         static const QString testSharp(const QString& str)
00053         {
00054                 QString sharp = "";
00055                 if (str.at(0) != '#')
00056                         sharp = '#';
00057                 return (sharp + str);
00058         }
00059 
00060         class ColladaTag : public XMLTag
00061         {
00062         public:
00063                 ColladaTag()
00064                         :XMLTag("COLLADA")
00065                 {
00066                         _attributes.push_back(TagAttribute("xmlns","http://www.collada.org/2005/11/COLLADASchema"));
00067                         _attributes.push_back(TagAttribute("version","1.4.1"));
00068                 }
00069         };
00070 
00071         class AssetTag : public XMLTag
00072         {
00073         public:
00074                 AssetTag()
00075                         :XMLTag("asset")
00076                 {
00077                 }
00078         };
00079 
00080         class ContributorTag : public XMLTag
00081         {
00082         public:
00083                 ContributorTag()
00084                         :XMLTag("contributor")
00085                 {
00086                 }
00087         };
00088 
00089         class AuthorTag : public XMLLeafTag
00090         {
00091         public:
00092                 AuthorTag()
00093                         :XMLLeafTag("author")
00094                 {
00095                         _text.push_back("VCGLab");
00096                 }
00097         };
00098 
00099         class AuthoringToolTag : public XMLLeafTag
00100         {
00101         public:
00102                 AuthoringToolTag()
00103                         :XMLLeafTag("authoring_tool")
00104                 {
00105                         _text.push_back("VCGLib | MeshLab");
00106                 }
00107         };
00108 
00109         class UpAxisTag : public XMLLeafTag
00110         {
00111         public:
00112                 UpAxisTag(const QString& up = "Y_UP")
00113                         :XMLLeafTag("up_axis")
00114                 {
00115                         _text.push_back(up);
00116                 }
00117         };
00118 
00119         class LibraryImagesTag : public XMLTag
00120         {
00121         public:
00122                 LibraryImagesTag()
00123                         :XMLTag("library_images")
00124                 {
00125                 }
00126         };
00127 
00128         class ImageTag : public XMLTag
00129         {
00130         public:
00131                 ImageTag(const QString& id,const QString& name)
00132                         :XMLTag("image")
00133                 {
00134                         _attributes.push_back(TagAttribute("id",id));
00135                         _attributes.push_back(TagAttribute("name",name));
00136                 }
00137         };
00138 
00139         class InitFromTag : public XMLLeafTag
00140         {
00141         public:
00142                 InitFromTag(const QString& txtpathname)
00143                         :XMLLeafTag("init_from")
00144                 {
00145                         _text.push_back(txtpathname);
00146                 }
00147         };
00148 
00149         class LibraryMaterialsTag : public XMLTag
00150         {
00151         public:
00152                 LibraryMaterialsTag()
00153                         :XMLTag("library_materials")
00154                 {
00155                 }
00156         };
00157         
00158         class MaterialTag : public XMLTag
00159         {
00160         public:
00161                 MaterialTag(const QString& id,const QString& name)
00162                         :XMLTag("material")
00163                 {
00164                         _attributes.push_back(TagAttribute("id",id));
00165                         _attributes.push_back(TagAttribute("name",name));
00166                 }
00167         };
00168 
00169         class InstanceEffectTag : public XMLLeafTag
00170         {
00171         public:
00172                 InstanceEffectTag(const QString& url)
00173                         :XMLLeafTag("instance_effect")
00174                 {
00175                         _attributes.push_back(TagAttribute("url",testSharp(url)));
00176                 }
00177         };
00178 
00179         class LibraryEffectsTag : public XMLTag
00180         {
00181         public:
00182                 LibraryEffectsTag()
00183                         :XMLTag("library_effects")
00184                 {
00185                 }
00186         };
00187 
00188         class EffectTag : public XMLTag
00189         {
00190         public:
00191                 EffectTag(const QString& id)
00192                         :XMLTag("effect")
00193                 {
00194                         _attributes.push_back(TagAttribute("id",id));
00195                 }
00196         };
00197         
00198         class ProfileCommonTag : public XMLTag
00199         {
00200         public:
00201                 ProfileCommonTag()
00202                         :XMLTag("profile_COMMON")
00203                 {
00204                 }
00205         };
00206 
00207         class NewParamTag : public XMLTag
00208         {
00209         public:
00210                 NewParamTag(const QString& sid)
00211                         :XMLTag("newparam")
00212                 {
00213                         _attributes.push_back(TagAttribute("sid",sid));
00214                 }
00215         };
00216 
00217         class SurfaceTag : public XMLTag
00218         {
00219         public:
00220                 SurfaceTag(const QString& type = QString("2D"))
00221                         :XMLTag("surface")
00222                 {
00223                         _attributes.push_back(TagAttribute("type",type));
00224                 }
00225         };
00226 
00227         class FormatTag : public XMLLeafTag
00228         {
00229         public:
00230                 FormatTag(const QString& format)
00231                         :XMLLeafTag("format")
00232                 {
00233                         _text.push_back(format);
00234                 }
00235         };
00236         
00237         class Sampler2DTag : public XMLTag
00238         {
00239         public:
00240                 Sampler2DTag()
00241                         :XMLTag("sampler2D")
00242                 {
00243                 }
00244         };
00245 
00246         class SourceTag : public XMLLeafTag
00247         {
00248         public:
00249                 SourceTag(const QString& id,const QString& name)
00250                         :XMLLeafTag("source")
00251                 {
00252                         _attributes.push_back(TagAttribute("id",id));
00253                         _attributes.push_back(TagAttribute("name",name));
00254                 }
00255 
00256                 SourceTag(const QString& source)
00257                         :XMLLeafTag("source")
00258                 {
00259                         _text.push_back(source);
00260                 }
00261         };
00262 
00263         class MinFilterTag : public XMLLeafTag
00264         {
00265         public:
00266                 MinFilterTag(const QString& filter)
00267                         :XMLLeafTag("minfilter")
00268                 {
00269                         _text.push_back(filter);
00270                 }
00271         };
00272 
00273         class MagFilterTag : public XMLLeafTag
00274         {
00275         public:
00276                 MagFilterTag(const QString& filter)
00277                         :XMLLeafTag("magfilter")
00278                 {
00279                         _text.push_back(filter);
00280                 }
00281         };
00282 
00283         class TechniqueTag : public XMLTag
00284         {
00285         public:
00286                 TechniqueTag(const QString& sid)
00287                         :XMLTag("technique")
00288                 {
00289                         _attributes.push_back(TagAttribute("sid",sid));
00290                 }
00291         };
00292 
00293         class TechniqueCommonTag : public XMLTag
00294         {
00295         public:
00296                 TechniqueCommonTag()
00297                         :XMLTag("technique_common")
00298                 {
00299                 }
00300         };
00301 
00302         class BlinnTag : public XMLTag
00303         {
00304         public:
00305                 BlinnTag()
00306                         :XMLTag("blinn")
00307                 {
00308                 }
00309         };
00310 
00311         class EmissionTag : public XMLTag
00312         {
00313         public:
00314                 EmissionTag()
00315                         :XMLTag("emission")
00316                 {
00317                 }
00318         };
00319 
00320         class ColorTag : public XMLLeafTag
00321         {
00322         public:
00323                 ColorTag(const float r,const float g,const float b,const float a)
00324                         :XMLLeafTag("color")
00325                 {
00326                         
00327                         _text.push_back(QString::number(r));
00328                         _text.push_back(QString::number(g));
00329                         _text.push_back(QString::number(b));
00330                         _text.push_back(QString::number(a));
00331                 }
00332         };
00333 
00334         class AmbientTag : public XMLTag
00335         {
00336         public:
00337                 AmbientTag()
00338                         :XMLTag("ambient")
00339                 {
00340                 }
00341         };
00342 
00343         class DiffuseTag : public XMLTag
00344         {
00345         public:
00346                 DiffuseTag()
00347                         :XMLTag("diffuse")
00348                 {
00349                 }
00350         };
00351 
00352         class TextureTag : public XMLLeafTag
00353         {
00354         public:
00355                 TextureTag(const QString& texture,const QString& texcoord)
00356                         :XMLLeafTag("texture")
00357                 {
00358                         _attributes.push_back(TagAttribute("texture",texture));
00359                         _attributes.push_back(TagAttribute("texcoord",texcoord));
00360                 }
00361         };
00362 
00363         class SpecularTag : public XMLTag
00364         {
00365         public:
00366                 SpecularTag()
00367                         :XMLTag("specular")
00368                 {
00369                 }
00370         };
00371 
00372         class ShininessTag : public XMLTag
00373         {
00374         public:
00375                 ShininessTag()
00376                         :XMLTag("shininess")
00377                 {
00378                 }
00379         };
00380 
00381         class FloatTag : public XMLLeafTag
00382         {
00383         public:
00384                 FloatTag(const float floatnum)
00385                         :XMLLeafTag("float")
00386                 {
00387                         _text.push_back(QString::number(floatnum));
00388                 }
00389         };
00390 
00391         class ReflectiveTag : public XMLTag
00392         {
00393         public:
00394                 ReflectiveTag()
00395                         :XMLTag("reflective")
00396                 {
00397                 }
00398         };
00399 
00400         class ReflectivityTag : public XMLTag
00401         {
00402         public:
00403                 ReflectivityTag()
00404                         :XMLTag("reflectivity")
00405                 {
00406                 }
00407         };
00408 
00409         class TransparentTag : public XMLTag
00410         {
00411         public:
00412                 TransparentTag()
00413                         :XMLTag("transparent")
00414                 {
00415                 }
00416         };
00417 
00418         class TransparencyTag : public XMLTag
00419         {
00420         public:
00421                 TransparencyTag()
00422                         :XMLTag("transparency")
00423                 {
00424                 }
00425         };
00426 
00427         class IndexOfRefractionTag : public XMLTag
00428         {
00429         public:
00430                 IndexOfRefractionTag()
00431                         :XMLTag("index_of_refraction")
00432                 {
00433                 }
00434         };
00435 
00436         class LibraryGeometriesTag : public XMLTag
00437         {
00438         public:
00439                 LibraryGeometriesTag()
00440                         :XMLTag("library_geometries")
00441                 {
00442                 }
00443         };
00444         
00445         class GeometryTag : public XMLTag
00446         {
00447         public:
00448                 GeometryTag(const QString& id,const QString& name)
00449                         :XMLTag("geometry")
00450                 {
00451                         _attributes.push_back(TagAttribute("id",id));
00452                         _attributes.push_back(TagAttribute("name",name));
00453                 }
00454         };
00455 
00456         class MeshTag : public XMLTag
00457         {
00458         public:
00459                 MeshTag()
00460                         :XMLTag("mesh")
00461                 {
00462                 }
00463         };
00464 
00465         class ArraySourceTag : public XMLTag
00466         {
00467         public:
00468                 ArraySourceTag(const QString& id,const QString& name)
00469                         :XMLTag("source")
00470                 {
00471                         _attributes.push_back(TagAttribute("id",id));
00472                         _attributes.push_back(TagAttribute("name",name));
00473                 }
00474         };
00475 
00476         class FloatArrayTag : public XMLLeafTag
00477         {
00478         public:
00479                 enum ARRAYSEMANTIC {VERTPOSITION,VERTNORMAL,VERTCOLOR, FACENORMAL,WEDGETEXCOORD};
00480 
00481                 template<typename MESHTYPE>
00482                 FloatArrayTag(const QString& id,const int count,const MESHTYPE& m,ARRAYSEMANTIC sem,const unsigned int componenttype)
00483                         :XMLLeafTag("float_array")
00484                 {
00485                         _attributes.push_back(TagAttribute("id",id));
00486                         _attributes.push_back(TagAttribute("count",QString::number(count)));
00487 
00488                         if ((sem == VERTPOSITION) || (sem == VERTNORMAL) || (sem == VERTCOLOR))
00489                         {
00490                                 for(typename MESHTYPE::ConstVertexIterator vit = m.vert.begin();vit != m.vert.end();++vit)
00491                                 {
00492                                         for(unsigned int ii = 0; ii < componenttype;++ii)
00493                                         {
00494                                                 if (sem == VERTPOSITION)
00495                                                         _text.push_back(QString::number(vit->P()[ii]));
00496                                                 else if (sem == VERTCOLOR)
00497                                                         _text.push_back(QString::number((vit->C()[ii])/255.0));
00498                                                 else
00499                                                 {
00500                                                         typename MESHTYPE::VertexType::NormalType r = vit->cN();
00501                                                         r.Normalize();
00502                                                         _text.push_back(QString::number(r[ii]));
00503                                         
00504                                                 }
00505                                         }
00506                                 }
00507                         }
00508                         else
00509                         {
00510                                 for(typename MESHTYPE::ConstFaceIterator fit = m.face.begin();fit != m.face.end();++fit)
00511                                 {
00512                                         if (sem == FACENORMAL)
00513                                         {
00514                                                 for(unsigned int ii = 0; ii < componenttype;++ii)
00515                                                 {
00516                                                                 typename MESHTYPE::FaceType::NormalType r = fit->cN();
00517                                                                 r.Normalize();
00518                                                                 _text.push_back(QString::number(r[ii]));
00519                                                 }
00520                                         }
00521                                         else
00522                                         {
00523                                                 for(unsigned int ii = 0; ii < 3;++ii)
00524                                                 {                               
00525                                                         _text.push_back(QString::number(fit->cWT(ii).U()));
00526                                                         _text.push_back(QString::number(fit->cWT(ii).V()));
00527                                                 }
00528                                         }
00529                                 }
00530                         }
00531                 }
00532         };
00533 
00534         //class FloatWedgeArrayTag : public XMLLeafTag
00535         //{
00536         //public:
00537         //      template<typename MESHTYPE,typename SIMPLEXACCESSOR>
00538         //      FloatWedgeArrayTag(const QString& id,const int count,const MESHTYPE& m,const AccessorComponentNumberInfo<MESHTYPE,SIMPLEXACCESSOR>& accessor)
00539         //              :XMLLeafTag("float_array")
00540         //      {
00541         //              _attributes.push_back(TagAttribute("id",id));
00542         //              _attributes.push_back(TagAttribute("count",QString::number(count)));
00543         //              for(typename SIMPLEXACCESSOR::ConstIterator it= accessor._a.begin();it != accessor._a.end(); ++it)
00544         //              {
00545         //                      for(unsigned int ii = 0; ii < 3;++ii)
00546         //                      {                               
00547         //                              _text.push_back(QString::number(accessor._a(*it,ii).U()));
00548         //                              _text.push_back(QString::number(accessor._a(*it,ii).V()));
00549         //                      }
00550         //              }
00551         //      }
00552         //};
00553 
00554         class AccessorTag : public XMLTag
00555         {
00556         public:
00557                 AccessorTag(const int count,const QString& source,const int stride)
00558                         :XMLTag("accessor")
00559                 {
00560                         _attributes.push_back(TagAttribute("count",QString::number(count)));
00561                         _attributes.push_back(TagAttribute("source",testSharp(source)));
00562                         _attributes.push_back(TagAttribute("stride",QString::number(stride)));
00563                 }
00564         };
00565 
00566         class ParamTag : public XMLTag
00567         {
00568         public:
00569                 ParamTag(const QString& name,const QString& type)
00570                         :XMLTag("param")
00571                 {
00572                         _attributes.push_back(TagAttribute("name",name));
00573                         _attributes.push_back(TagAttribute("type",type));
00574                 }
00575         };
00576 
00577         class VerticesTag : public XMLTag
00578         {
00579         public:
00580                 VerticesTag(const QString& id)
00581                         :XMLTag("vertices")
00582                 {
00583                         _attributes.push_back(TagAttribute("id",id));
00584                 }
00585         };
00586 
00587         class InputTag : public XMLTag
00588         {
00589         public:
00590 
00591                 InputTag(const QString& semantic,const QString& source)
00592                         :XMLTag("input")
00593                 {
00594                         _attributes.push_back(TagAttribute("semantic",semantic));
00595                         _attributes.push_back(TagAttribute("source",testSharp(source)));
00596                 }
00597 
00598                 InputTag(const int offset,const QString& semantic,const QString& source)
00599                         :XMLTag("input")
00600                 {
00601                         _attributes.push_back(TagAttribute("offset",QString::number(offset)));
00602                         _attributes.push_back(TagAttribute("semantic",semantic));
00603                         _attributes.push_back(TagAttribute("source",testSharp(source)));
00604                 }
00605         };
00606 
00607         class TrianglesTag : public XMLTag
00608         {
00609         public:
00610                 TrianglesTag(const int count)
00611                         :XMLTag("triangles")
00612                 {
00613                         _attributes.push_back(TagAttribute("count",QString::number(count)));
00614                 }
00615 
00616                 TrianglesTag(const int count,const QString& material)
00617                         :XMLTag("triangles")
00618                 {
00619                         _attributes.push_back(TagAttribute("count",QString::number(count)));
00620                         _attributes.push_back(TagAttribute("material",material));
00621                 }
00622         };
00623 
00624         class PTag : public XMLLeafTag
00625         {
00626         public:
00627                 template<typename MESHTYPE>
00628                 PTag(const MESHTYPE& m,const unsigned int nedge,bool vcol=false, bool norm = false,bool texcoord = false)
00629                         :XMLLeafTag("p")
00630                 {
00631                         int cont = 0;
00632                         for(typename MESHTYPE::ConstFaceIterator it= m.face.begin();it != m.face.end(); ++it)
00633                         {
00634                                 for(unsigned int ii = 0; ii < nedge; ++ii)
00635                                 {
00636                                         int dist  = vcg::tri::Index(m,it->cV(ii));
00637                                         _text.push_back(QString::number(dist));
00638                                         if (vcol)
00639                                                 _text.push_back(QString::number(dist));
00640                                         if (norm)
00641                                                 _text.push_back(QString::number(cont));
00642                                         if (texcoord)
00643                                                 _text.push_back(QString::number(cont * nedge + ii));
00644                                 }
00645                                 ++cont;
00646                         }
00647                 }
00648 
00649                 template<typename MESHTYPE>
00650                 PTag(const MESHTYPE& m,const unsigned int nedge,QVector<int>& patchfaces,bool vcol = false, bool norm = false,bool texcoord = false)
00651                         :XMLLeafTag("p")
00652                 {
00653                         int cont = 0;
00654                         for(QVector<int>::iterator it = patchfaces.begin();it != patchfaces     .end(); ++it)
00655                         {
00656                                 for(unsigned int ii = 0; ii < nedge; ++ii)
00657                                 {
00658                                         const typename MESHTYPE::FaceType& f = m.face[*it];
00659                                         int dist  = f.cV(ii) - &(*m.vert.begin());
00660                                         _text.push_back(QString::number(dist));
00661                                         if (vcol)
00662                                                 _text.push_back(QString::number(dist));
00663                                         if (norm)
00664                                                 _text.push_back(QString::number(*it));
00665                                         if (texcoord)
00666                                                 _text.push_back(QString::number(*it * nedge + ii));
00667                                 }
00668                                 ++cont;
00669                         }
00670                 }
00671         };
00672 
00673         class LibraryVisualScenesTag : public XMLTag
00674         {
00675         public:
00676                 LibraryVisualScenesTag()
00677                         :XMLTag("library_visual_scenes")
00678                 {
00679                 }
00680         };
00681 
00682         class VisualSceneTag : public XMLTag
00683         {
00684         public:
00685                 VisualSceneTag(const QString& id,const QString& name)
00686                         :XMLTag("visual_scene")
00687                 {
00688                         _attributes.push_back(TagAttribute("id",id));
00689                         _attributes.push_back(TagAttribute("name",name));
00690                 }
00691         };
00692         
00693         class NodeTag : public XMLTag
00694         {
00695         public:
00696                 NodeTag(const QString& id,const QString& name)
00697                         :XMLTag("node")
00698                 {
00699                         _attributes.push_back(TagAttribute("id",id));
00700                         _attributes.push_back(TagAttribute("name",name));
00701                 }
00702         };
00703 
00704         class RotateTag : public XMLLeafTag
00705         {
00706         public:
00707                 RotateTag(const QString& sid,const vcg::Point4f& p)
00708                         :XMLLeafTag("rotate")
00709                 {
00710                         _attributes.push_back(TagAttribute("sid",sid));
00711 
00712                         for(unsigned int ii =0;ii < 4; ++ii)
00713                                 _text.push_back(QString::number(p[ii]));
00714                 }
00715         };
00716 
00717         class TranslateTag : public XMLLeafTag
00718         {
00719         public:
00720                 TranslateTag(const QString& sid,const vcg::Point4f& p)
00721                         :XMLLeafTag("translate")
00722                 {
00723                         _attributes.push_back(TagAttribute("sid",sid));
00724 
00725                         for(unsigned int ii =0;ii < 4; ++ii)
00726                                 _text.push_back(QString::number(p[ii]));
00727                 }
00728         }; 
00729 
00730         class InstanceGeometryTag : public XMLTag
00731         {
00732         public:
00733                 InstanceGeometryTag(const QString& url)
00734                         :XMLTag("instance_geometry")
00735                 {
00736                         _attributes.push_back(TagAttribute("url",testSharp(url)));
00737                 }
00738         };
00739 
00740         class BindMaterialTag : public XMLTag
00741         {
00742         public:
00743                 BindMaterialTag()
00744                         :XMLTag("bind_material")
00745                 {
00746                 }
00747         };
00748 
00749         class InstanceMaterialTag : public XMLTag
00750         {
00751         public:
00752                 InstanceMaterialTag(const QString& symbol,const QString& target)
00753                         :XMLTag("instance_material")
00754                 {
00755                         _attributes.push_back(TagAttribute("symbol",symbol));
00756                         _attributes.push_back(TagAttribute("target",testSharp(target)));
00757                 }
00758         };
00759 
00760         class BindVertexInputTag : public XMLTag
00761         {
00762         public:
00763                 BindVertexInputTag(const QString& semantic,const QString& input_semantic,const QString& input_set)
00764                         :XMLTag("bind_vertex_input")
00765                 {
00766                         _attributes.push_back(TagAttribute("semantic",semantic));
00767                         _attributes.push_back(TagAttribute("input_semantic",input_semantic));
00768                 }
00769         };
00770 
00771         class SceneTag : public XMLTag
00772         {
00773         public:
00774                 SceneTag()
00775                         :XMLTag("scene")
00776                 {
00777                 }
00778         };
00779 
00780         class CreatedTag : public XMLLeafTag//added
00781         {
00782         public:
00783                 CreatedTag()
00784                         :XMLLeafTag("created")
00785                 {
00786                         QDateTime dateCreated = QDateTime::currentDateTime().toUTC();
00787                         QString dateCreatedStr = dateCreated.toString();
00788                         _text.push_back(dateCreatedStr);
00789                 }
00790         };
00791 
00792         class ModifiedTag : public XMLLeafTag//added
00793         {
00794         public:
00795                 ModifiedTag()
00796                         :XMLLeafTag("modified")
00797                 {
00798                         QDateTime dateModified = QDateTime::currentDateTime().toUTC();
00799                         QString dateModifiedStr = dateModified.toString();
00800                         _text.push_back(dateModifiedStr);
00801                 }
00802         };
00803 
00804         class InstanceVisualSceneTag : public XMLTag
00805         {
00806         public:
00807                 InstanceVisualSceneTag(const QString& url)
00808                         :XMLTag("instance_visual_scene")
00809                 {
00810                         _attributes.push_back(TagAttribute("url",testSharp(url)));
00811                 }
00812         };
00813 } //Tags
00814 
00815 class DocumentManager
00816 {
00817 private:
00818         static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLLeafNode* leaf)
00819         {
00820                 node1->_sons.push_back(leaf);
00821                 node0->_sons.push_back(node1);
00822         }
00823 
00824         static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2,XMLInteriorNode* node3,XMLNode* node4)
00825         {
00826                 node3->_sons.push_back(node4);
00827                 node2->_sons.push_back(node3);
00828                 node1->_sons.push_back(node2);
00829                 node0->_sons.push_back(node1);
00830         }
00831 
00832         static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2,XMLInteriorNode* node3)
00833         {
00834                 node2->_sons.push_back(node3);
00835                 node1->_sons.push_back(node2);
00836                 node0->_sons.push_back(node1);
00837         }
00838 
00839         static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2)
00840         {
00841                 node1->_sons.push_back(node2);
00842                 node0->_sons.push_back(node1);
00843         }
00844 
00845         template<typename MESHMODELTYPE>
00846         static void splitMeshInTexturedPatches(const MESHMODELTYPE& m,QVector<QVector<int> >& patches)
00847         {
00848                 patches.resize(m.textures.size());
00849                 int cc = 0;
00850                 for(typename MESHMODELTYPE::ConstFaceIterator itf = m.face.begin();itf != m.face.end();++itf)
00851                 {
00852                         int tmp = itf->cWT(0).N();
00853                         assert(tmp>=0 && tmp<patches.size());   
00854                         patches[tmp].push_back(cc);
00855                         ++cc;
00856                 }
00857         }
00858 
00859 public:
00860         template<typename MESHMODELTYPE>
00861         static XMLDocument* createColladaDocument(const MESHMODELTYPE& m,const int mask)
00862         {
00863                 //for now we export only triangularface
00864                 const unsigned int edgefacenum = 3;
00865                 typedef XMLInteriorNode XNode;
00866                 typedef XMLLeafNode XLeaf;
00867 
00868                 XNode* root = new XNode(new Tags::ColladaTag());
00869                 XNode* assetnode = new XNode(new Tags::AssetTag());
00870                 XNode* contributornode = new XNode(new Tags::ContributorTag());
00871                 contributornode->_sons.push_back(new XLeaf(new Tags::AuthorTag()));
00872                 contributornode->_sons.push_back(new XLeaf(new Tags::AuthoringToolTag()));
00873                 
00874                 assetnode->_sons.push_back(contributornode);
00875                 assetnode->_sons.push_back(new XLeaf(new Tags::CreatedTag()));//added
00876                 assetnode->_sons.push_back(new XLeaf(new Tags::ModifiedTag()));
00877                 assetnode->_sons.push_back(new XLeaf(new Tags::UpAxisTag()));
00878                 root->_sons.push_back(assetnode);
00879                 
00880                 XNode* libimages = NULL;
00881                 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00882                 {
00883                         if ( ii == 0)
00884                                 libimages = new XNode(new Tags::LibraryImagesTag());
00885                         QString subfix = QString::number(ii); 
00886                         XNode* imagenode = new XNode(new Tags::ImageTag(QString("texture") + subfix,QString("texture") + subfix));
00887                         XLeaf* initfromnode = new XLeaf(new Tags::InitFromTag(QString::fromStdString(m.textures[ii])));
00888                         imagenode->_sons.push_back(initfromnode);
00889                         libimages->_sons.push_back(imagenode);
00890                         if (ii == 0)
00891                                 root->_sons.push_back(libimages);
00892                 }
00893 
00894                 XNode* libmaterials = NULL;
00895                 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00896                 {
00897                         if ( ii == 0)
00898                                 libmaterials = new XNode(new Tags::LibraryMaterialsTag());
00899                         QString subfix = QString::number(ii); 
00900                         QString mat = "material" + subfix;
00901                         XNode* materialnode = new XNode(new Tags::MaterialTag(mat,mat));
00902                         XLeaf* instanceeff = new XLeaf(new Tags::InstanceEffectTag(mat+"-fx"));
00903                         materialnode->_sons.push_back(instanceeff);
00904                         libmaterials->_sons.push_back(materialnode);
00905                         if ( ii == 0)
00906                                 root->_sons.push_back(libmaterials);
00907                 }
00908 
00909                 XNode* libeffects = NULL;
00910                 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00911                 {
00912                         if ( ii == 0)
00913                                 libeffects = new XNode(new Tags::LibraryEffectsTag());
00914                         QString subfix = QString::number(ii); 
00915                         QString mat = "material" + subfix + "-fx";
00916                         XNode* effectnode = new XNode(new Tags::EffectTag(mat));
00917                         XNode* procommnode = new XNode(new Tags::ProfileCommonTag());
00918                         QString tex = QString("texture")+subfix;
00919                         XNode* newparamnode = new XNode(new Tags::NewParamTag(tex+"-surface"));
00920                         XNode* surfacenode = new XNode(new Tags::SurfaceTag());
00921                         XLeaf* initfromnode = new XLeaf(new Tags::InitFromTag(tex));
00922                         QImage img(QString::fromStdString(m.textures[ii]));
00923                         QImage::Format f = img.format();
00924                         QString form = "R8G8B8";
00925                         if (f==QImage::Format_ARGB32)
00926                                 form = "A8R8G8B8";
00927                         XLeaf* formatnode = new XLeaf(new Tags::FormatTag(form));
00928                         surfacenode->_sons.push_back(initfromnode);
00929                         surfacenode->_sons.push_back(formatnode);
00930                         newparamnode->_sons.push_back(surfacenode);
00931                         procommnode->_sons.push_back(newparamnode);
00932 
00933                         XNode* newparamnode2 = new XNode(new Tags::NewParamTag(tex+"-sampler"));
00934                         XNode* samplernode = new XNode(new Tags::Sampler2DTag());
00935                         XLeaf* sourcenode = new XLeaf(new Tags::SourceTag(tex+"-surface"));
00936                         XLeaf* minfilt = new XLeaf(new Tags::MinFilterTag("LINEAR"));
00937                         XLeaf* magfilt = new XLeaf(new Tags::MagFilterTag("LINEAR"));
00938                         samplernode->_sons.push_back(sourcenode);
00939                         samplernode->_sons.push_back(minfilt);
00940                         samplernode->_sons.push_back(magfilt);
00941                         newparamnode2->_sons.push_back(samplernode);
00942                         procommnode->_sons.push_back(newparamnode2);
00943 
00944                         XNode* technode = new XNode(new Tags::TechniqueTag("common"));
00945                         XNode* blinnnode = new XNode(new Tags::BlinnTag());
00946                         
00947                         XNode* diffusenode = new XNode(new Tags::DiffuseTag());
00948                         XLeaf* texturenode = new XLeaf(new Tags::TextureTag(tex+"-sampler","UVSET0"));
00949 
00950                         connectHierarchyNode(blinnnode,diffusenode,texturenode);
00951                         connectHierarchyNode(procommnode,technode,blinnnode);
00952 
00953                         effectnode->_sons.push_back(procommnode);
00954                         libeffects->_sons.push_back(effectnode);
00955                         if ( ii == 0)
00956                                 root->_sons.push_back(libeffects);
00957                 }
00958 
00959                 XNode* libgeo = new XNode(new Tags::LibraryGeometriesTag());
00960                 QString subfix = "0"; 
00961                 QString shape = "shape" + subfix;
00962                 XNode* geometrynode = new XNode(new Tags::GeometryTag(shape+"-lib",shape));
00963                 XNode* meshnode = new XNode(new Tags::MeshTag());
00964                 XNode* sourcepos = new XNode(new Tags::SourceTag(shape+"-lib-positions","position"));
00965                 //AccessorComponentNumberInfo<MESHMODELTYPE,MeshAccessors::VertexPositionAccessor<const MESHMODELTYPE>> acc(m);
00966                 unsigned int return_component_number = CoordNumber<typename MESHMODELTYPE::CoordType>::coord();
00967                 XLeaf* floatarr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-positions-array",m.vert.size() * return_component_number,m,Tags::FloatArrayTag::VERTPOSITION,return_component_number));
00968                 XNode* techcommnode = new XNode(new Tags::TechniqueCommonTag());
00969                 XNode* accessornode = new XNode(new Tags::AccessorTag(m.vert.size(),shape+"-lib-positions-array",return_component_number));
00970                 XNode* paramx = new XNode(new Tags::ParamTag("X","float"));
00971                 XNode* paramy = new XNode(new Tags::ParamTag("Y","float"));
00972                 XNode* paramz = new XNode(new Tags::ParamTag("Z","float"));
00973                 
00974                 sourcepos->_sons.push_back(floatarr);
00975                 accessornode->_sons.push_back(paramx);
00976                 accessornode->_sons.push_back(paramy);
00977                 accessornode->_sons.push_back(paramz);
00978 
00979                 techcommnode->_sons.push_back(accessornode);
00980                 sourcepos->_sons.push_back(techcommnode);
00981 
00982                 meshnode->_sons.push_back(sourcepos);
00983                 
00984 
00985                 //CHANGE THIS PIECE OF CODE!
00986                 bool normalmask = bool((mask & vcg::tri::io::Mask::IOM_FACENORMAL) || (mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) || (mask & vcg::tri::io::Mask::IOM_VERTNORMAL));
00987                 if (normalmask)
00988                 {
00989                         XNode* sourcenormal = new XNode(new Tags::SourceTag(shape+"-lib-normals","normal"));
00990 
00991                         //we export only triangular face
00992                         XLeaf* floatnormarr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-normals-array",m.face.size() * return_component_number,m,Tags::FloatArrayTag::FACENORMAL,return_component_number));
00993                         XNode* techcommnormnode = new XNode(new Tags::TechniqueCommonTag());
00994                         XNode* accessornormnode = new XNode(new Tags::AccessorTag(m.face.size(),shape+"-lib-normals-array",return_component_number));
00995                         
00996                         //I have to make up the following piece of code
00997                         XNode* paramnormx = new XNode(new Tags::ParamTag("X","float"));
00998                         XNode* paramnormy = new XNode(new Tags::ParamTag("Y","float"));
00999                         XNode* paramnormz = new XNode(new Tags::ParamTag("Z","float"));
01000 
01001                         sourcenormal->_sons.push_back(floatnormarr);
01002 
01003                         accessornormnode->_sons.push_back(paramnormx);
01004                         accessornormnode->_sons.push_back(paramnormy);
01005                         accessornormnode->_sons.push_back(paramnormz);
01006 
01007                         techcommnormnode->_sons.push_back(accessornormnode);
01008                         sourcenormal->_sons.push_back(techcommnormnode);
01009 
01010                         meshnode->_sons.push_back(sourcenormal);
01011                 }
01012 
01013                 //CHANGE THIS PIECE OF CODE!
01014                 bool vcolormask = bool((mask & vcg::tri::io::Mask::IOM_VERTCOLOR));
01015                 if (vcolormask)
01016                 {
01017                         unsigned int color_component_number = 4;
01018                         XNode* sourcevcolor = new XNode(new Tags::SourceTag(shape+"-lib-vcolor","vcolor"));
01019 
01020                         //we export only triangular face
01021                         XLeaf* floatvcolorarr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-vcolor-array",m.vert.size() * color_component_number,m,Tags::FloatArrayTag::VERTCOLOR,color_component_number));
01022                         XNode* techcommvcolornode = new XNode(new Tags::TechniqueCommonTag());
01023                         XNode* accessorvcolornode = new XNode(new Tags::AccessorTag(m.vert.size(),shape+"-lib-vcolor-array",color_component_number));
01024                         
01025                         //I have to make up the following piece of code
01026                         XNode* paramvcolorx = new XNode(new Tags::ParamTag("R","float"));
01027                         XNode* paramvcolory = new XNode(new Tags::ParamTag("G","float"));
01028                         XNode* paramvcolorz = new XNode(new Tags::ParamTag("B","float"));
01029                         XNode* paramvcolora = new XNode(new Tags::ParamTag("A","float"));
01030 
01031                         sourcevcolor->_sons.push_back(floatvcolorarr);
01032 
01033                         accessorvcolornode->_sons.push_back(paramvcolorx);
01034                         accessorvcolornode->_sons.push_back(paramvcolory);
01035                         accessorvcolornode->_sons.push_back(paramvcolorz);
01036                         accessorvcolornode->_sons.push_back(paramvcolora);
01037 
01038                         techcommvcolornode->_sons.push_back(accessorvcolornode);
01039                         sourcevcolor->_sons.push_back(techcommvcolornode);
01040 
01041                         meshnode->_sons.push_back(sourcevcolor);
01042                 }
01043                 
01044                 bool texmask = bool(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD);
01045                 if (texmask)
01046                 {
01047                         XNode* sourcewedge = new XNode(new Tags::SourceTag(shape+"-lib-map","map"));
01048                         return_component_number = CoordNumber<typename MESHMODELTYPE::FaceType::TexCoordType::PointType>::coord();
01049                         //we export only triangular face
01050                         XLeaf* floatwedgearr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-map-array",m.face.size() * return_component_number * edgefacenum,m,Tags::FloatArrayTag::WEDGETEXCOORD,return_component_number));
01051                         XNode* techcommwedgenode = new XNode(new Tags::TechniqueCommonTag());
01052                         XNode* accessorwedgenode = new XNode(new Tags::AccessorTag(m.face.size() * edgefacenum,shape+"-lib-map-array",return_component_number));
01053 
01054                         //I have to make up the following piece of code
01055                         XNode* paramwedgeu = new XNode(new Tags::ParamTag("U","float"));
01056                         XNode* paramwedgev = new XNode(new Tags::ParamTag("V","float"));
01057 
01058                         sourcewedge->_sons.push_back(floatwedgearr);
01059 
01060                         accessorwedgenode->_sons.push_back(paramwedgeu);
01061                         accessorwedgenode->_sons.push_back(paramwedgev);
01062                 
01063                         techcommwedgenode->_sons.push_back(accessorwedgenode);
01064                         sourcewedge->_sons.push_back(techcommwedgenode);
01065 
01066                         meshnode->_sons.push_back(sourcewedge);
01067                 }
01068 
01069                 XNode* vertnode = new XNode(new Tags::VerticesTag(shape+"-lib-vertices"));
01070                 XNode* inputposnode = new XNode(new Tags::InputTag("POSITION",shape+"-lib-positions"));
01071                 vertnode->_sons.push_back(inputposnode);
01072         
01073                 //XNode* inputvcolnode = new XNode(new Tags::InputTag("COLOR",shape+"-lib-vcolor"));
01074                 //vertnode->_sons.push_back(inputvcolnode);
01075 
01076                 meshnode->_sons.push_back(vertnode);
01077                 XNode* trianglesnode = NULL;
01078                 //if ((m.textures.size() == 0) || (!texmask))
01079                 //{     
01080                 //      //there isn't any texture file
01081                 //      trianglesnode = new XNode(new Tags::TrianglesTag(m.face.size()));
01082                 //}
01083                 //else
01084                 //{
01085                 //      std::vector<std::vector<int>> _mytripatches(m.textures.size());
01086                 //      if ((texmask) && (m.textures.size() > 1))
01087                 //      {
01088                 //              //there are many textures files - I have to split the mesh in triangular patches that share the same texture's file.
01089                 //              for(MESHMODELTYPE::ConstFaceIterator itf = m.face.begin();itf != m.face.end();++itf)
01090                 //              {
01091                 //                      
01092                 //              }
01093                 //              trianglesnode = new XNode(new Tags::TrianglesTag(m.face.size(),"instancematerial"));
01094                 //}
01095                 
01096                 QVector<QVector<int> > mytripatches;
01097                 if ((texmask) && (m.textures.size() != 0))
01098                         splitMeshInTexturedPatches(m,mytripatches);
01099                 
01100                 QVector<QVector<int> >::iterator itp = mytripatches.begin();
01101                 int indmat = 0;
01102                 do 
01103                 {
01104                         if ((m.textures.size() == 0) || (!texmask))
01105                         {       
01106                                 //there isn't any texture file
01107                                 trianglesnode = new XNode(new Tags::TrianglesTag(m.face.size()));
01108                         }
01109                         else
01110                                 trianglesnode = new XNode(new Tags::TrianglesTag(mytripatches[indmat].size(),"material" + QString::number(indmat)));
01111 
01112                         XNode* inputtrinode = new XNode(new Tags::InputTag(0,"VERTEX",shape+"-lib-vertices"));
01113                         trianglesnode->_sons.push_back(inputtrinode);
01114 
01115                         int offs=1;
01116                         if (vcolormask)
01117                         {
01118                                 XNode* inputvcolnode = new XNode(new Tags::InputTag(offs,"COLOR",shape+"-lib-vcolor"));
01119                                 trianglesnode->_sons.push_back(inputvcolnode);
01120                                 ++offs;
01121                         }
01122 
01123                         if (normalmask)
01124                         {
01125                                 XNode* inputnormnode = new XNode(new Tags::InputTag(offs,"NORMAL",shape+"-lib-normals"));
01126                                 trianglesnode->_sons.push_back(inputnormnode);
01127                                 ++offs;
01128                         }
01129 
01130                         if (texmask)
01131                         {
01132                                 XNode* inputwedgenode = new XNode(new Tags::InputTag(offs,"TEXCOORD",shape+"-lib-map"));
01133                                 trianglesnode->_sons.push_back(inputwedgenode);
01134                                 offs++;
01135                         }
01136 
01137                         XLeaf* polyleaf = NULL;
01138                         if (itp == mytripatches.end())
01139                                 polyleaf = new XLeaf(new Tags::PTag(m,edgefacenum,vcolormask,normalmask,texmask));
01140                         else
01141                                 polyleaf = new XLeaf(new Tags::PTag(m,edgefacenum,(*itp),vcolormask,normalmask,texmask));
01142 
01143                         trianglesnode->_sons.push_back(polyleaf);
01144                         meshnode->_sons.push_back(trianglesnode);
01145                         
01146                         ++indmat;
01147                         if (itp != mytripatches.end()) 
01148                                 ++itp;
01149                 }while(itp != mytripatches.end());
01150                 
01151                 connectHierarchyNode(libgeo,geometrynode,meshnode);
01152                 root->_sons.push_back(libgeo);
01153 
01154                 XNode* libvisualnode = new XNode(new Tags::LibraryVisualScenesTag());
01155                 XNode* visualscenenode = new XNode(new Tags::VisualSceneTag("VisualSceneNode","VisualScene"));
01156                 XNode* nodenode = new XNode(new Tags::NodeTag("node","node"));
01157                 XNode* instgeonode = new XNode(new Tags::InstanceGeometryTag(shape+"-lib"));
01158                 if (m.textures.size() > 0)
01159                 {
01160                         XNode* bindnode = new XNode(new Tags::BindMaterialTag());
01161                         XNode* techcommmatnode = new XNode(new Tags::TechniqueCommonTag());
01162                         for(unsigned int ii = 0; ii < m.textures.size(); ++ii)
01163                         {
01164                                 XNode* instmatnode = new XNode(new Tags::InstanceMaterialTag("material"  + QString::number(ii),"material" + QString::number(ii)));
01165                                 XNode* bindvertnode = new XNode(new Tags::BindVertexInputTag("UVSET0","TEXCOORD","0"));
01166                                 connectHierarchyNode(techcommmatnode,instmatnode,bindvertnode);
01167                         }
01168                         connectHierarchyNode(visualscenenode,nodenode,instgeonode,bindnode,techcommmatnode);
01169                 }
01170                 else
01171                         connectHierarchyNode(visualscenenode,nodenode,instgeonode);
01172                 libvisualnode->_sons.push_back(visualscenenode);
01173                 root->_sons.push_back(libvisualnode);
01174 
01175                 XNode* scenenode = new XNode(new Tags::SceneTag());
01176                 XNode* instvisualscenenode = new XNode(new Tags::InstanceVisualSceneTag("VisualSceneNode"));
01177 
01178                 scenenode->_sons.push_back(instvisualscenenode);
01179                 root->_sons.push_back(scenenode);
01180 
01181                 return new XMLDocument(root);
01182         }
01183 
01184         static void destroyColladaDocument(XMLDocument* doc)
01185         {
01186                 delete doc;
01187         }
01188 
01189         //template<typename MESHMODELTYPE>
01190         //static int importColladaDocument(const QDomDocument& doc,MESHMODELTYPE& m,const int mask)
01191         //{
01192         //      QDomElement root = doc.toElement();
01193         //      if (root.isNull())
01194         //              return UtilDAE::E_BAD_CONVERTION_FROM_NODE_TO_ELEMENT;
01195         //      QDomNodeList lst = root.elementsByTagName("COLLADA");
01196         //      int err = UtilDAE::checkOccurencies(lst,UtilDAE::ONE);
01197         //      if (
01198         //      return vcg::tri::io::UtilDAE::E_NOERROR; 
01199         //}
01200 };
01201 } //Collada
01202 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:29:37