glc_factory.cpp

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  Version 2.0.0, packaged on July 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU Lesser General Public License as published by
00011  the Free Software Foundation; either version 3 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU Lesser General Public License for more details.
00018 
00019  You should have received a copy of the GNU Lesser General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 *****************************************************************************/
00024 
00026 
00027 #include "glc_factory.h"
00028 #include "io/glc_objtoworld.h"
00029 #include "io/glc_stltoworld.h"
00030 #include "io/glc_offtoworld.h"
00031 #include "io/glc_3dstoworld.h"
00032 #include "io/glc_3dxmltoworld.h"
00033 #include "io/glc_colladatoworld.h"
00034 #include "io/glc_bsreptoworld.h"
00035 
00036 #include "viewport/glc_panmover.h"
00037 #include "viewport/glc_zoommover.h"
00038 #include "viewport/glc_settargetmover.h"
00039 #include "viewport/glc_trackballmover.h"
00040 #include "viewport/glc_turntablemover.h"
00041 #include "viewport/glc_repcrossmover.h"
00042 #include "viewport/glc_reptrackballmover.h"
00043 #include "viewport/glc_flymover.h"
00044 #include "viewport/glc_repflymover.h"
00045 #include "maths/glc_line3d.h"
00046 #include "maths/glc_geomtools.h"
00047 
00048 #include "glc_fileformatexception.h"
00049 
00050 // init static member
00051 GLC_Factory* GLC_Factory::m_pFactory= NULL;
00052 QGLContext* GLC_Factory::m_pQGLContext= NULL;
00053 
00055 // static method
00057 // Return the unique instance of the factory
00058 GLC_Factory* GLC_Factory::instance(const QGLContext *pContext)
00059 {
00060         if(m_pFactory == NULL)
00061         {
00062                 m_pFactory= new GLC_Factory(pContext);
00063         }
00064         else if ((NULL != pContext) && (m_pQGLContext != pContext))
00065         {
00066                 m_pQGLContext= const_cast<QGLContext*>(pContext);
00067         }
00068         return m_pFactory;
00069 }
00070 
00072 // Constructor destructor
00074 
00075 // Protected constructor
00076 GLC_Factory::GLC_Factory(const QGLContext *pContext)
00077 {
00078         m_pQGLContext= (const_cast<QGLContext*>(pContext));
00079 }
00080 
00081 // Destructor
00082 GLC_Factory::~GLC_Factory()
00083 {
00084 
00085 }
00086 
00088 // Create functions
00090 
00091 GLC_3DRep GLC_Factory::createPoint(const GLC_Point3d &coord) const
00092 {
00093         GLC_3DRep newPoint(new GLC_Point(coord));
00094         return newPoint;
00095 }
00096 
00097 GLC_3DRep GLC_Factory::createPoint(double x, double y, double z) const
00098 {
00099         GLC_3DRep newPoint(new GLC_Point(x, y, z));
00100         return newPoint;
00101 }
00102 
00103 
00104 GLC_3DRep GLC_Factory::createPointSprite(float size, GLC_Material* pMaterial) const
00105 {
00106         GLC_3DRep newPoint(new GLC_PointSprite(size, pMaterial));
00107         return newPoint;
00108 }
00109 
00110 GLC_3DRep GLC_Factory::createLine(const GLC_Point3d& point1, const GLC_Point3d& point2) const
00111 {
00112         GLC_3DRep newPoint(new GLC_Line(point1, point2));
00113         return newPoint;
00114 }
00115 
00116 GLC_3DRep GLC_Factory::createCircle(double radius, double angle) const
00117 {
00118         GLC_3DRep newCircle(new GLC_Circle(radius, angle));
00119         return newCircle;
00120 }
00121 
00122 GLC_3DRep GLC_Factory::createBox(double lx, double ly, double lz) const
00123 {
00124 
00125         GLC_3DRep newBox(new GLC_Box(lx, ly, lz));
00126         return newBox;
00127 }
00128 
00129 GLC_3DViewInstance GLC_Factory::createBox(const GLC_BoundingBox& boundingBox) const
00130 {
00131         const double lx= boundingBox.upperCorner().x() - boundingBox.lowerCorner().x();
00132         const double ly= boundingBox.upperCorner().y() - boundingBox.lowerCorner().y();
00133         const double lz= boundingBox.upperCorner().z() - boundingBox.lowerCorner().z();
00134         GLC_Box* pBox= new GLC_Box(lx, ly, lz);
00135         GLC_3DViewInstance newBox(pBox);
00136         newBox.translate(boundingBox.center().x(), boundingBox.center().y()
00137                                         , boundingBox.center().z());
00138         return newBox;
00139 }
00140 
00141 GLC_3DRep GLC_Factory::createCylinder(double radius, double length) const
00142 {
00143 
00144         GLC_3DRep newCylinder(new GLC_Cylinder(radius, length));
00145         return newCylinder;
00146 }
00147 
00148 GLC_3DRep GLC_Factory::createCone(double radius, double length) const
00149 {
00150         GLC_3DRep newCone(new GLC_Cone(radius, length));
00151         return newCone;
00152 }
00153 
00154 GLC_3DRep GLC_Factory::createSphere(double radius) const
00155 {
00156         GLC_3DRep newSphere(new GLC_Sphere(radius));
00157         return newSphere;
00158 }
00159 
00160 GLC_3DRep GLC_Factory::createRectangle(double l1, double l2)
00161 {
00162         GLC_3DRep newRectangle(new GLC_Rectangle(l1, l2));
00163         return newRectangle;
00164 }
00165 
00166 GLC_3DViewInstance GLC_Factory::createRectangle(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2)
00167 {
00168         // Create the rectangle to (0,0) and  z normal
00169         GLC_3DViewInstance rectangleInstance(createRectangle(l1, l2));
00170 
00171         // Create the plane rotation matrix
00172         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00173         // Vector from origin to the plane
00174         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00175 
00176         return rectangleInstance;
00177 }
00178 
00179 GLC_3DViewInstance GLC_Factory::createCuttingPlane(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2, GLC_Material* pMat)
00180 {
00181         // Create the rectangle to (0,0) and  z normal
00182         GLC_Rectangle* pRectangle= new GLC_Rectangle(l1, l2);
00183         pRectangle->replaceMasterMaterial(pMat);
00184 
00185         GLC_3DViewInstance rectangleInstance(pRectangle);
00186 
00187         // Create the plane rotation matrix
00188         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00189         // Vector from origin to the plane
00190         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00191 
00192         return rectangleInstance;
00193 
00194 }
00195 
00196 GLC_World GLC_Factory::createWorldFromFile(QFile &file, QStringList* pAttachedFileName) const
00197 {
00198         GLC_World* pWorld= NULL;
00199         if (QFileInfo(file).suffix().toLower() == "obj")
00200         {
00201                 GLC_ObjToWorld objToWorld(m_pQGLContext);
00202                 connect(&objToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00203                 pWorld= objToWorld.CreateWorldFromObj(file);
00204                 if (NULL != pAttachedFileName)
00205                 {
00206                         (*pAttachedFileName)= objToWorld.listOfAttachedFileName();
00207                 }
00208         }
00209         else if (QFileInfo(file).suffix().toLower() == "stl")
00210         {
00211                 GLC_StlToWorld stlToWorld;
00212                 connect(&stlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00213                 pWorld= stlToWorld.CreateWorldFromStl(file);
00214         }
00215         else if (QFileInfo(file).suffix().toLower() == "off")
00216         {
00217                 GLC_OffToWorld offToWorld;
00218                 connect(&offToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00219                 pWorld= offToWorld.CreateWorldFromOff(file);
00220         }
00221         else if (QFileInfo(file).suffix().toLower() == "3ds")
00222         {
00223                 GLC_3dsToWorld studioToWorld(m_pQGLContext);
00224                 connect(&studioToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00225                 pWorld= studioToWorld.CreateWorldFrom3ds(file);
00226                 if (NULL != pAttachedFileName)
00227                 {
00228                         (*pAttachedFileName)= studioToWorld.listOfAttachedFileName();
00229                 }
00230         }
00231         else if (QFileInfo(file).suffix().toLower() == "3dxml")
00232         {
00233                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00234                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00235                 pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, false);
00236                 if (NULL != pAttachedFileName)
00237                 {
00238                         (*pAttachedFileName)= d3dxmlToWorld.listOfAttachedFileName();
00239                 }
00240         }
00241         else if (QFileInfo(file).suffix().toLower() == "dae")
00242         {
00243                 GLC_ColladaToWorld colladaToWorld(m_pQGLContext);
00244                 connect(&colladaToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00245                 pWorld= colladaToWorld.CreateWorldFromCollada(file);
00246                 if (NULL != pAttachedFileName)
00247                 {
00248                         (*pAttachedFileName)= colladaToWorld.listOfAttachedFileName();
00249                 }
00250         }
00251         else if (QFileInfo(file).suffix().toLower() == "bsrep")
00252         {
00253                 GLC_BSRepToWorld bsRepToWorld;
00254                 pWorld= bsRepToWorld.CreateWorldFromBSRep(file);
00255                 emit currentQuantum(100);
00256         }
00257 
00258         if (NULL == pWorld)
00259         {
00260                 // File extension not recognize or file not loaded
00261                 QString message(QString("GLC_Factory::createWorldFromFile File ") + file.fileName() + QString(" not loaded"));
00262                 GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
00263                 throw(fileFormatException);
00264         }
00265         GLC_World resulWorld(*pWorld);
00266         delete pWorld;
00267 
00268         return resulWorld;
00269 }
00270 
00271 GLC_World GLC_Factory::createWorldStructureFrom3dxml(QFile &file) const
00272 {
00273         GLC_World* pWorld= NULL;
00274 
00275         if (QFileInfo(file).suffix().toLower() == "3dxml")
00276         {
00277                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00278                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00279                 pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, true);
00280         }
00281 
00282         if (NULL == pWorld)
00283         {
00284                 // File extension not recognize or file not loaded
00285                 QString message(QString("GLC_Factory::createWorldStructureFrom3dxml File ") + file.fileName() + QString(" not loaded"));
00286                 GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
00287                 throw(fileFormatException);
00288         }
00289         GLC_World resulWorld(*pWorld);
00290         delete pWorld;
00291 
00292         return resulWorld;
00293 }
00294 
00295 GLC_3DRep GLC_Factory::create3DRepFromFile(const QString& fileName) const
00296 {
00297         GLC_3DRep rep;
00298 
00299         if ((QFileInfo(fileName).suffix().toLower() == "3dxml") || (QFileInfo(fileName).suffix().toLower() == "3drep"))
00300         {
00301                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00302                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00303                 rep= d3dxmlToWorld.create3DrepFrom3dxmlRep(fileName);
00304         }
00305 
00306         return rep;
00307 
00308 }
00309 
00310 GLC_Material* GLC_Factory::createMaterial() const
00311 {
00312         return new GLC_Material();
00313 }
00314 
00315 GLC_Material* GLC_Factory::createMaterial(const GLfloat *pAmbiantColor) const
00316 {
00317         return new GLC_Material("Material", pAmbiantColor);
00318 }
00319 
00320 GLC_Material* GLC_Factory::createMaterial(const QColor &color) const
00321 {
00322         return new GLC_Material(color);
00323 }
00324 
00325 GLC_Material* GLC_Factory::createMaterial(GLC_Texture* pTexture) const
00326 {
00327         return new GLC_Material(pTexture, "TextureMaterial");
00328 }
00329 
00330 GLC_Material* GLC_Factory::createMaterial(const QString &textureFullFileName) const
00331 {
00332         GLC_Texture* pTexture= createTexture(textureFullFileName);
00333         return createMaterial(pTexture);
00334 }
00335 
00336 GLC_Material* GLC_Factory::createMaterial(const QImage &image) const
00337 {
00338         GLC_Texture* pTexture= createTexture(image);
00339         return createMaterial(pTexture);
00340 }
00341 
00342 GLC_Texture* GLC_Factory::createTexture(const QString &textureFullFileName) const
00343 {
00344         return new GLC_Texture(m_pQGLContext, textureFullFileName);
00345 }
00346 
00347 GLC_Texture* GLC_Factory::createTexture(const QImage & image, const QString& imageFileName) const
00348 {
00349         return new GLC_Texture(m_pQGLContext, image, imageFileName);
00350 }
00351 
00352 GLC_MoverController GLC_Factory::createDefaultMoverController(const QColor& color, GLC_Viewport* pViewport)
00353 {
00354         GLC_MoverController defaultController;
00355 
00357         // Pan Mover
00359         // Create Pan Mover representation
00360         GLC_RepMover* pRepMover= new GLC_RepCrossMover(pViewport);
00361         pRepMover->setMainColor(color);
00362         QList<GLC_RepMover*> listOfRep;
00363         listOfRep.append(pRepMover);
00364         // Create the Pan Mover
00365         GLC_Mover* pMover= new GLC_PanMover(pViewport, listOfRep);
00366         // Add the Pan Mover to the controller
00367         defaultController.addMover(pMover, GLC_MoverController::Pan);
00368 
00370         // Zoom Mover
00372         // Copy the pan Mover representation
00373         pRepMover= pRepMover->clone();
00374         listOfRep.clear();
00375         listOfRep.append(pRepMover);
00376         // Create the Zoom Mover
00377         pMover= new GLC_ZoomMover(pViewport, listOfRep);
00378         // Add the Zoom Mover to the controller
00379         defaultController.addMover(pMover, GLC_MoverController::Zoom);
00380 
00382         // Set Target Mover
00384         // Create the Zoom Mover
00385         pMover= new GLC_SetTargetMover(pViewport);
00386         // Add the Zoom Mover to the controller
00387         defaultController.addMover(pMover, GLC_MoverController::Target);
00388 
00390         // Track Ball Mover
00392         // Copy the pan Mover representation
00393         pRepMover= pRepMover->clone();
00394         listOfRep.clear();
00395         listOfRep.append(pRepMover);
00396         // Create the track ball representation
00397         pRepMover= new GLC_RepTrackBallMover(pViewport);
00398         pRepMover->setMainColor(color);
00399         listOfRep.append(pRepMover);
00400         // Create the Track Ball Mover
00401         pMover= new GLC_TrackBallMover(pViewport, listOfRep);
00402         // Add the Track ball Mover to the controller
00403         defaultController.addMover(pMover, GLC_MoverController::TrackBall);
00404 
00406         // Turn Table Mover
00408         // Create the Turn Table Mover
00409         pMover= new GLC_TurnTableMover(pViewport);
00410         // Add the Turn Table Mover to the controller
00411         defaultController.addMover(pMover, GLC_MoverController::TurnTable);
00413         // Fly Mover
00415         listOfRep.clear();
00416         pRepMover= new GLC_RepFlyMover(pViewport);
00417         pRepMover->setMainColor(color);
00418         listOfRep.append(pRepMover);
00419         // Create the fly mover
00420         pMover= new GLC_FlyMover(pViewport, listOfRep);
00421         // Add the fly mover to the controller
00422         defaultController.addMover(pMover, GLC_MoverController::Fly);
00423 
00424         return defaultController;
00425 }

SourceForge.net Logo

©2005-2010 Laurent Ribon