glc_3dxmltoworld.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #ifndef GLC_3DXMLTOWORLD_H_
00026 #define GLC_3DXMLTOWORLD_H_
00027 
00028 #include <QFile>
00029 #include <QObject>
00030 #include <QXmlStreamReader>
00031 #include <QHash>
00032 #include <QSet>
00033 #include <QDateTime>
00034 #include "../maths/glc_matrix4x4.h"
00035 #include "../sceneGraph/glc_3dviewinstance.h"
00036 
00037 #include "../glc_config.h"
00038 
00039 class GLC_World;
00040 class QGLContext;
00041 class QuaZip;
00042 class QuaZipFile;
00043 class GLC_StructReference;
00044 class GLC_StructInstance;
00045 class GLC_StructOccurence;
00046 class GLC_Mesh;
00047 
00050 
00051 
00052 class GLC_LIB_EXPORT GLC_3dxmlToWorld : public QObject
00053 {
00054         Q_OBJECT
00055 
00057 
00058         struct AssyLink
00059         {
00060                 unsigned int m_ParentRefId;
00061                 GLC_StructInstance* m_pChildInstance;
00062                 unsigned int m_InstanceId;
00063                 inline bool operator < (const AssyLink& l) const
00064                 {return m_InstanceId < l.m_InstanceId;}
00065         };
00067 
00068         struct RepLink
00069         {
00070                 unsigned int m_ReferenceId;
00071                 unsigned int  m_RepId;
00072         };
00074 
00075         struct MaterialRef
00076         {
00077                 QString m_Id;
00078                 QString m_Name;
00079                 QString m_AssociatedFile;
00080         };
00081 
00083 
00084         struct V3OccurenceAttrib
00085         {
00086                 inline V3OccurenceAttrib()
00087                 : m_IsVisible(true)
00088                 , m_pRenderProperties(NULL)
00089                 {}
00090                 inline ~V3OccurenceAttrib()
00091                 {delete m_pRenderProperties;}
00092 
00094                 bool m_IsVisible;
00096                 GLC_RenderProperties* m_pRenderProperties;
00097         };
00098 
00100 
00101         struct V4OccurenceAttrib
00102         {
00103                 inline V4OccurenceAttrib()
00104                 : m_IsVisible(true)
00105                 , m_pRenderProperties(NULL)
00106                 , m_pMatrix(NULL)
00107                 , m_Path()
00108                 {}
00109                 inline ~V4OccurenceAttrib()
00110                 {
00111                         delete m_pRenderProperties;
00112                         delete m_pMatrix;
00113                 }
00114 
00116                 bool m_IsVisible;
00118                 GLC_RenderProperties* m_pRenderProperties;
00120                 GLC_Matrix4x4* m_pMatrix;
00122                 QList<unsigned int> m_Path;
00123         };
00124 
00125         typedef QHash<unsigned int, GLC_StructReference*> ReferenceHash;
00126         typedef QHash<GLC_StructInstance*, unsigned int> InstanceOfHash;
00127         typedef QHash<GLC_StructInstance*, QString> InstanceOfExtRefHash;
00128         typedef QSet<const QString> SetOfExtRef;
00129         typedef QList<AssyLink> AssyLinkList;
00130         typedef QList<RepLink> RepLinkList;
00131         typedef QHash<const QString, GLC_StructReference*> ExternalReferenceHash;
00132         typedef QHash<const QString, GLC_Material*> MaterialHash;
00133         typedef QHash<const unsigned int, QString> ReferenceRepHash;
00134 
00136 
00138 
00139 public:
00141         GLC_3dxmlToWorld();
00142 
00143         virtual ~GLC_3dxmlToWorld();
00145 
00146 
00148 
00149 public:
00151         GLC_World* createWorldFrom3dxml(QFile &, bool StructureOnly, bool getExternalRef= false);
00152 
00154         GLC_3DRep create3DrepFrom3dxmlRep(const QString&);
00155 
00157         inline QStringList listOfAttachedFileName() const
00158         {return m_SetOfAttachedFileName.toList();}
00159 
00160 
00162 
00164 // Qt Signals
00166         signals:
00167         void currentQuantum(int);
00168 
00170 
00172 
00173 private:
00175         void loadManifest();
00176 
00178         void clear();
00179 
00181         void goToRepId(const QString&);
00182 
00184         void gotToPolygonalRepType();
00185 
00187         QString readAttribute(const QString&, bool required= false);
00188 
00190         void readHeader();
00191 
00193         void loadProductStructure();
00194 
00196         void loadReference3D();
00197 
00199         void loadInstance3D();
00200 
00202         void loadReferenceRep();
00203 
00205         void loadInstanceRep();
00206 
00208         void loadExternalRef3D();
00209 
00211         GLC_StructReference* createReferenceRep(QString id, GLC_3DRep* pRep);
00212 
00214         GLC_Matrix4x4 loadMatrix(const QString&);
00215 
00217         void createUnfoldedTree();
00218 
00221         void checkForXmlError(const QString&);
00222 
00224         void loadFace(GLC_Mesh*, const int lod, double accuracy);
00225 
00227         void loadPolyline(GLC_Mesh*);
00228 
00230         void clearMaterialHash();
00231 
00233         GLC_Material* loadSurfaceAttributes();
00234 
00236         GLC_Material* getMaterial();
00237 
00239         bool setStreamReaderToFile(QString, bool test= false);
00240 
00242         void loadDefaultView();
00243 
00245         void loadV3DefaultViewProperty();
00246 
00248         void loadV4DefaultViewProperty();
00249 
00251         QList<unsigned int> loadOccurencePath();
00252 
00254         void loadGraphicProperties(V4OccurenceAttrib* pAttrib);
00255 
00257         void loadLocalRepresentations();
00258 
00260         void loadExternRepresentations();
00261 
00263         GLC_3DRep loadCurrentExtRep();
00264 
00266         void loadCatMaterialRef();
00267 
00269         void loadMaterialDef(const MaterialRef&);
00270 
00272         void loadCatRepImage();
00273 
00275         GLC_Texture* loadTexture(QString);
00276 
00278         void setRepresentationFileName(GLC_3DRep* pRep);
00279 
00281         inline QXmlStreamReader::TokenType readNext();
00282 
00284         inline bool goToElement(QXmlStreamReader* pReader, const QString& element);
00285 
00286         // Return the content of an element
00287         inline QString getContent(QXmlStreamReader* pReader, const QString& element);
00288 
00290         inline QString readAttribute(QXmlStreamReader* pReader, const QString& attribute);
00291 
00293         inline bool endElementNotReached(QXmlStreamReader* pReader, const QString& element);
00294 
00296         inline bool startElementNotReached(QXmlStreamReader* pReader, const QString& element);
00297 
00299         inline void goToEndElement(QXmlStreamReader* pReader, const QString& element);
00300 
00302         void checkFileValidity(QIODevice* pIODevice);
00303 
00305         void applyV4Attribute(GLC_StructOccurence* pOccurence, V4OccurenceAttrib* pV4OccurenceAttrib, QHash<GLC_StructInstance*, unsigned int>& InstanceToIdHash);
00306 
00308         void loadRep(GLC_Mesh* pMesh);
00309 
00311         void loadVertexBuffer(GLC_Mesh* pMesh);
00312 
00314 
00316 // Private members
00318 private:
00320         QXmlStreamReader* m_pStreamReader;
00321 
00323         QString m_FileName;
00324 
00326         QuaZip* m_p3dxmlArchive;
00327 
00329         QFile* m_pCurrentFile;
00330 
00332         QString m_RootName;
00333 
00335         GLC_World* m_pWorld;
00336 
00338         ReferenceHash m_ReferenceHash;
00339 
00341         AssyLinkList m_AssyLinkList;
00342 
00344         InstanceOfHash m_InstanceOf;
00345 
00347         SetOfExtRef m_SetOfExtRef;
00348 
00350         InstanceOfExtRefHash m_InstanceOfExtRefHash;
00351 
00353         ExternalReferenceHash m_ExternalReferenceHash;
00354 
00356         MaterialHash m_MaterialHash;
00357 
00359         bool m_IsInArchive;
00360 
00362         ReferenceRepHash m_ReferenceRepHash;
00363 
00365         RepLinkList m_LocalRepLinkList;
00366 
00368         RepLinkList m_ExternRepLinkList;
00369 
00371         SetOfExtRef m_SetOfExtRep;
00372 
00374         GLC_Material* m_pCurrentMaterial;
00375 
00377         QHash<QString, QString> m_TextureImagesHash;
00378 
00380         bool m_LoadStructureOnly;
00381 
00383         QSet<QString> m_SetOfAttachedFileName;
00384 
00386         QString m_CurrentFileName;
00387 
00389         QDateTime m_CurrentDateTime;
00390 
00392         QHash<unsigned int, V3OccurenceAttrib*> m_V3OccurenceAttribHash;
00393 
00395         QList<V4OccurenceAttrib*> m_V4OccurenceAttribList;
00396 
00398         bool m_GetExternalRef3DName;
00399 
00400         static QMutex m_ZipMutex;
00401 
00402         QList<QByteArray> m_ByteArrayList;
00403 
00405         bool m_IsVersion3;
00406 
00407 };
00408 
00409 QXmlStreamReader::TokenType GLC_3dxmlToWorld::readNext()
00410 {
00411         QXmlStreamReader::TokenType token= m_pStreamReader->readNext();
00412         if (QXmlStreamReader::PrematureEndOfDocumentError == m_pStreamReader->error())
00413         {
00414                 //qDebug() << "QXmlStreamReader::PrematureEndOfDocumentError == m_pStreamReader->error()";
00415                 if (!m_ByteArrayList.isEmpty())
00416                 {
00417                         m_pStreamReader->addData(m_ByteArrayList.takeFirst());
00418                         return readNext();
00419                 }
00420         }
00421         return token;
00422 }
00423 
00424 bool GLC_3dxmlToWorld::goToElement(QXmlStreamReader* pReader, const QString& element)
00425 {
00426         while(!pReader->atEnd() && !pReader->hasError() && !(pReader->isStartElement() && (pReader->name() == element)))
00427         {
00428                 readNext();
00429         }
00430         return !pReader->atEnd() && !pReader->hasError();
00431 }
00432 
00433 QString GLC_3dxmlToWorld::getContent(QXmlStreamReader* pReader, const QString& element)
00434 {
00435         QString content;
00436         while(endElementNotReached(pReader, element))
00437         {
00438                 readNext();
00439                 if (pReader->isCharacters() && !pReader->text().isEmpty())
00440                 {
00441                         content+= pReader->text().toString();
00442                 }
00443         }
00444 
00445         return content.trimmed();
00446 }
00447 
00448 QString GLC_3dxmlToWorld::readAttribute(QXmlStreamReader* pReader, const QString& attribute)
00449 {
00450         return pReader->attributes().value(attribute).toString();
00451 }
00452 
00453 bool GLC_3dxmlToWorld::endElementNotReached(QXmlStreamReader* pReader, const QString& element)
00454 {
00455         return !pReader->atEnd() && !pReader->hasError() && !(pReader->isEndElement() && (pReader->name() == element));
00456 }
00457 
00458 bool GLC_3dxmlToWorld::startElementNotReached(QXmlStreamReader* pReader, const QString& element)
00459 {
00460         return !pReader->atEnd() && !pReader->hasError() && !(pReader->isStartElement() && (pReader->name() == element));
00461 }
00462 
00463 void GLC_3dxmlToWorld::goToEndElement(QXmlStreamReader* pReader, const QString& element)
00464 {
00465         while(endElementNotReached(pReader, element))
00466         {
00467                 readNext();
00468         }
00469 }
00470 
00471 
00472 #endif /* GLC_3DXMLTOWORLD_H_ */

SourceForge.net Logo

©2005-2011 Laurent Ribon