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 Beta 1, packaged on April 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 General Public License as published by
00011  the Free Software Foundation; either version 2 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 General Public License for more details.
00018 
00019  You should have received a copy of the GNU 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::createRectangle(double l1, double l2)
00149 {
00150         GLC_3DRep newRectangle(new GLC_Rectangle(l1, l2));
00151         return newRectangle;
00152 }
00153 
00154 GLC_3DViewInstance GLC_Factory::createRectangle(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2)
00155 {
00156         // Create the rectangle to (0,0) and  z normal
00157         GLC_3DViewInstance rectangleInstance(createRectangle(l1, l2));
00158 
00159         // Create the plane rotation matrix
00160         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00161         // Vector from origin to the plane
00162         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00163 
00164         return rectangleInstance;
00165 }
00166 
00167 GLC_3DViewInstance GLC_Factory::createCuttingPlane(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2, GLC_Material* pMat)
00168 {
00169         // Create the rectangle to (0,0) and  z normal
00170         GLC_Rectangle* pRectangle= new GLC_Rectangle(l1, l2);
00171         pRectangle->replaceMasterMaterial(pMat);
00172 
00173         GLC_3DViewInstance rectangleInstance(pRectangle);
00174 
00175         // Create the plane rotation matrix
00176         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00177         // Vector from origin to the plane
00178         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00179 
00180         return rectangleInstance;
00181 
00182 }
00183 
00184 GLC_World GLC_Factory::createWorldFromFile(QFile &file, QStringList* pAttachedFileName) const
00185 {
00186         GLC_World* pWorld= NULL;
00187         if (QFileInfo(file).suffix().toLower() == "obj")
00188         {
00189                 GLC_ObjToWorld objToWorld(m_pQGLContext);
00190                 connect(&objToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00191                 pWorld= objToWorld.CreateWorldFromObj(file);
00192                 if (NULL != pAttachedFileName)
00193                 {
00194                         (*pAttachedFileName)= objToWorld.listOfAttachedFileName();
00195                 }
00196         }
00197         else if (QFileInfo(file).suffix().toLower() == "stl")
00198         {
00199                 GLC_StlToWorld stlToWorld;
00200                 connect(&stlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00201                 pWorld= stlToWorld.CreateWorldFromStl(file);
00202         }
00203         else if (QFileInfo(file).suffix().toLower() == "off")
00204         {
00205                 GLC_OffToWorld offToWorld;
00206                 connect(&offToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00207                 pWorld= offToWorld.CreateWorldFromOff(file);
00208         }
00209         else if (QFileInfo(file).suffix().toLower() == "3ds")
00210         {
00211                 GLC_3dsToWorld studioToWorld(m_pQGLContext);
00212                 connect(&studioToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00213                 pWorld= studioToWorld.CreateWorldFrom3ds(file);
00214                 if (NULL != pAttachedFileName)
00215                 {
00216                         (*pAttachedFileName)= studioToWorld.listOfAttachedFileName();
00217                 }
00218         }
00219         else if (QFileInfo(file).suffix().toLower() == "3dxml")
00220         {
00221                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00222                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00223                 pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, false);
00224                 if (NULL != pAttachedFileName)
00225                 {
00226                         (*pAttachedFileName)= d3dxmlToWorld.listOfAttachedFileName();
00227                 }
00228         }
00229         else if (QFileInfo(file).suffix().toLower() == "dae")
00230         {
00231                 GLC_ColladaToWorld colladaToWorld(m_pQGLContext);
00232                 connect(&colladaToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00233                 pWorld= colladaToWorld.CreateWorldFromCollada(file);
00234                 if (NULL != pAttachedFileName)
00235                 {
00236                         (*pAttachedFileName)= colladaToWorld.listOfAttachedFileName();
00237                 }
00238         }
00239         else if (QFileInfo(file).suffix().toLower() == "bsrep")
00240         {
00241                 GLC_BSRepToWorld bsRepToWorld;
00242                 pWorld= bsRepToWorld.CreateWorldFromBSRep(file);
00243                 emit currentQuantum(100);
00244         }
00245 
00246         if (NULL == pWorld)
00247         {
00248                 // File extension not recognize or file not loaded
00249                 QString message(QString("GLC_Factory::createWorldFromFile File ") + file.fileName() + QString(" not loaded"));
00250                 GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
00251                 throw(fileFormatException);
00252         }
00253         GLC_World resulWorld(*pWorld);
00254         delete pWorld;
00255 
00256         return resulWorld;
00257 }
00258 
00259 GLC_World GLC_Factory::createWorldStructureFrom3dxml(QFile &file) const
00260 {
00261         GLC_World* pWorld= NULL;
00262 
00263         if (QFileInfo(file).suffix().toLower() == "3dxml")
00264         {
00265                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00266                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00267                 pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, true);
00268         }
00269 
00270         if (NULL == pWorld)
00271         {
00272                 // File extension not recognize or file not loaded
00273                 QString message(QString("GLC_Factory::createWorldStructureFrom3dxml File ") + file.fileName() + QString(" not loaded"));
00274                 GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
00275                 throw(fileFormatException);
00276         }
00277         GLC_World resulWorld(*pWorld);
00278         delete pWorld;
00279 
00280         return resulWorld;
00281 }
00282 
00283 GLC_3DRep GLC_Factory::create3DRepFromFile(const QString& fileName) const
00284 {
00285         GLC_3DRep rep;
00286 
00287         if ((QFileInfo(fileName).suffix().toLower() == "3dxml") || (QFileInfo(fileName).suffix().toLower() == "3drep"))
00288         {
00289                 GLC_3dxmlToWorld d3dxmlToWorld(m_pQGLContext);
00290                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00291                 rep= d3dxmlToWorld.create3DrepFrom3dxmlRep(fileName);
00292         }
00293 
00294         return rep;
00295 
00296 }
00297 
00298 GLC_Material* GLC_Factory::createMaterial() const
00299 {
00300         return new GLC_Material();
00301 }
00302 
00303 GLC_Material* GLC_Factory::createMaterial(const GLfloat *pAmbiantColor) const
00304 {
00305         return new GLC_Material("Material", pAmbiantColor);
00306 }
00307 
00308 GLC_Material* GLC_Factory::createMaterial(const QColor &color) const
00309 {
00310         return new GLC_Material(color);
00311 }
00312 
00313 GLC_Material* GLC_Factory::createMaterial(GLC_Texture* pTexture) const
00314 {
00315         return new GLC_Material(pTexture, "TextureMaterial");
00316 }
00317 
00318 GLC_Material* GLC_Factory::createMaterial(const QString &textureFullFileName) const
00319 {
00320         GLC_Texture* pTexture= createTexture(textureFullFileName);
00321         return createMaterial(pTexture);
00322 }
00323 
00324 GLC_Material* GLC_Factory::createMaterial(const QImage &image) const
00325 {
00326         GLC_Texture* pTexture= createTexture(image);
00327         return createMaterial(pTexture);
00328 }
00329 
00330 GLC_Texture* GLC_Factory::createTexture(const QString &textureFullFileName) const
00331 {
00332         return new GLC_Texture(m_pQGLContext, textureFullFileName);
00333 }
00334 
00335 GLC_Texture* GLC_Factory::createTexture(const QImage & image, const QString& imageFileName) const
00336 {
00337         return new GLC_Texture(m_pQGLContext, image, imageFileName);
00338 }
00339 
00340 GLC_MoverController GLC_Factory::createDefaultMoverController(const QColor& color, GLC_Viewport* pViewport)
00341 {
00342         GLC_MoverController defaultController;
00343 
00345         // Pan Mover
00347         // Create Pan Mover representation
00348         GLC_RepMover* pRepMover= new GLC_RepCrossMover(pViewport);
00349         pRepMover->setMainColor(color);
00350         QList<GLC_RepMover*> listOfRep;
00351         listOfRep.append(pRepMover);
00352         // Create the Pan Mover
00353         GLC_Mover* pMover= new GLC_PanMover(pViewport, listOfRep);
00354         // Add the Pan Mover to the controller
00355         defaultController.addMover(pMover, GLC_MoverController::Pan);
00356 
00358         // Zoom Mover
00360         // Copy the pan Mover representation
00361         pRepMover= pRepMover->clone();
00362         listOfRep.clear();
00363         listOfRep.append(pRepMover);
00364         // Create the Zoom Mover
00365         pMover= new GLC_ZoomMover(pViewport, listOfRep);
00366         // Add the Zoom Mover to the controller
00367         defaultController.addMover(pMover, GLC_MoverController::Zoom);
00368 
00370         // Set Target Mover
00372         // Create the Zoom Mover
00373         pMover= new GLC_SetTargetMover(pViewport);
00374         // Add the Zoom Mover to the controller
00375         defaultController.addMover(pMover, GLC_MoverController::Target);
00376 
00378         // Track Ball Mover
00380         // Copy the pan Mover representation
00381         pRepMover= pRepMover->clone();
00382         listOfRep.clear();
00383         listOfRep.append(pRepMover);
00384         // Create the track ball representation
00385         pRepMover= new GLC_RepTrackBallMover(pViewport);
00386         pRepMover->setMainColor(color);
00387         listOfRep.append(pRepMover);
00388         // Create the Track Ball Mover
00389         pMover= new GLC_TrackBallMover(pViewport, listOfRep);
00390         // Add the Track ball Mover to the controller
00391         defaultController.addMover(pMover, GLC_MoverController::TrackBall);
00392 
00394         // Turn Table Mover
00396         // Create the Turn Table Mover
00397         pMover= new GLC_TurnTableMover(pViewport);
00398         // Add the Turn Table Mover to the controller
00399         defaultController.addMover(pMover, GLC_MoverController::TurnTable);
00401         // Fly Mover
00403         listOfRep.clear();
00404         pRepMover= new GLC_RepFlyMover(pViewport);
00405         pRepMover->setMainColor(color);
00406         listOfRep.append(pRepMover);
00407         // Create the fly mover
00408         pMover= new GLC_FlyMover(pViewport, listOfRep);
00409         // Add the fly mover to the controller
00410         defaultController.addMover(pMover, GLC_MoverController::Fly);
00411 
00412         return defaultController;
00413 }

SourceForge.net Logo

©2005 Laurent Ribon