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  Copyright (C) 2011 Jrme Forrissier
00006  http://glc-lib.sourceforge.net
00007 
00008  GLC-lib is free software; you can redistribute it and/or modify
00009  it under the terms of the GNU Lesser General Public License as published by
00010  the Free Software Foundation; either version 3 of the License, or
00011  (at your option) any later version.
00012 
00013  GLC-lib is distributed in the hope that it will be useful,
00014  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  GNU Lesser General Public License for more details.
00017 
00018  You should have received a copy of the GNU Lesser General Public License
00019  along with GLC-lib; if not, write to the Free Software
00020  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 
00022 *****************************************************************************/
00023 
00025 
00026 #include "glc_factory.h"
00027 #include "io/glc_fileloader.h"
00028 #include "io/glc_3dxmltoworld.h"
00029 #include "io/glc_worldreaderplugin.h"
00030 
00031 #include "viewport/glc_panmover.h"
00032 #include "viewport/glc_zoommover.h"
00033 #include "viewport/glc_settargetmover.h"
00034 #include "viewport/glc_trackballmover.h"
00035 #include "viewport/glc_turntablemover.h"
00036 #include "viewport/glc_repcrossmover.h"
00037 #include "viewport/glc_reptrackballmover.h"
00038 #include "viewport/glc_flymover.h"
00039 #include "viewport/glc_repflymover.h"
00040 #include "viewport/glc_tsrmover.h"
00041 #include "maths/glc_line3d.h"
00042 #include "maths/glc_geomtools.h"
00043 
00044 #include "glc_fileformatexception.h"
00045 
00046 // init static member
00047 GLC_Factory* GLC_Factory::m_pFactory= NULL;
00048 QList<GLC_WorldReaderPlugin*> GLC_Factory::m_WorldReaderPluginList;
00049 QSet<QString> GLC_Factory::m_SupportedExtensionSet;
00050 
00052 // static method
00054 // Return the unique instance of the factory
00055 GLC_Factory* GLC_Factory::instance()
00056 {
00057         if(m_pFactory == NULL)
00058         {
00059                 m_pFactory= new GLC_Factory();
00060         }
00061         return m_pFactory;
00062 }
00063 
00065 // Constructor destructor
00067 
00068 // Protected constructor
00069 GLC_Factory::GLC_Factory()
00070 {
00071         loadPlugins();
00072 }
00073 
00074 // Destructor
00075 GLC_Factory::~GLC_Factory()
00076 {
00077 
00078 }
00079 
00081 // Create functions
00083 
00084 GLC_3DRep GLC_Factory::createPoint(const GLC_Point3d &coord) const
00085 {
00086         GLC_3DRep newPoint(new GLC_Point(coord));
00087         return newPoint;
00088 }
00089 
00090 GLC_3DRep GLC_Factory::createPoint(double x, double y, double z) const
00091 {
00092         GLC_3DRep newPoint(new GLC_Point(x, y, z));
00093         return newPoint;
00094 }
00095 
00096 GLC_3DRep GLC_Factory::createPointCloud(const GLfloatVector& data, const QColor& color)
00097 {
00098         GLC_PointCloud* pPointCloud= new GLC_PointCloud();
00099         pPointCloud->addPoint(data);
00100         pPointCloud->setWireColor(color);
00101         return GLC_3DRep(pPointCloud);
00102 }
00103 
00104 GLC_3DRep GLC_Factory::createPointCloud(const QList<GLC_Point3d>& pointList, const QColor& color)
00105 {
00106         GLC_PointCloud* pPointCloud= new GLC_PointCloud();
00107         pPointCloud->addPoint(pointList);
00108         pPointCloud->setWireColor(color);
00109         return GLC_3DRep(pPointCloud);
00110 }
00111 
00112 GLC_3DRep GLC_Factory::createPointCloud(const QList<GLC_Point3df>& pointList, const QColor& color)
00113 {
00114         GLC_PointCloud* pPointCloud= new GLC_PointCloud();
00115         pPointCloud->addPoint(pointList);
00116         pPointCloud->setWireColor(color);
00117         return GLC_3DRep(pPointCloud);
00118 }
00119 
00120 
00121 GLC_3DRep GLC_Factory::createPointSprite(float size, GLC_Material* pMaterial) const
00122 {
00123         GLC_3DRep newPoint(new GLC_PointSprite(size, pMaterial));
00124         return newPoint;
00125 }
00126 
00127 GLC_3DRep GLC_Factory::createLine(const GLC_Point3d& point1, const GLC_Point3d& point2) const
00128 {
00129         GLC_3DRep newPoint(new GLC_Line(point1, point2));
00130         return newPoint;
00131 }
00132 
00133 GLC_3DRep GLC_Factory::createCircle(double radius, double angle) const
00134 {
00135         GLC_3DRep newCircle(new GLC_Circle(radius, angle));
00136         return newCircle;
00137 }
00138 
00139 GLC_3DRep GLC_Factory::createBox(double lx, double ly, double lz) const
00140 {
00141 
00142         GLC_3DRep newBox(new GLC_Box(lx, ly, lz));
00143         return newBox;
00144 }
00145 
00146 GLC_3DViewInstance GLC_Factory::createBox(const GLC_BoundingBox& boundingBox) const
00147 {
00148         const double lx= boundingBox.upperCorner().x() - boundingBox.lowerCorner().x();
00149         const double ly= boundingBox.upperCorner().y() - boundingBox.lowerCorner().y();
00150         const double lz= boundingBox.upperCorner().z() - boundingBox.lowerCorner().z();
00151         GLC_Box* pBox= new GLC_Box(lx, ly, lz);
00152         GLC_3DViewInstance newBox(pBox);
00153         newBox.translate(boundingBox.center().x(), boundingBox.center().y()
00154                                         , boundingBox.center().z());
00155         return newBox;
00156 }
00157 
00158 GLC_3DRep GLC_Factory::createCylinder(double radius, double length) const
00159 {
00160 
00161         GLC_3DRep newCylinder(new GLC_Cylinder(radius, length));
00162         return newCylinder;
00163 }
00164 
00165 GLC_3DRep GLC_Factory::createCone(double radius, double length) const
00166 {
00167         GLC_3DRep newCone(new GLC_Cone(radius, length));
00168         return newCone;
00169 }
00170 
00171 GLC_3DRep GLC_Factory::createSphere(double radius) const
00172 {
00173         GLC_3DRep newSphere(new GLC_Sphere(radius));
00174         return newSphere;
00175 }
00176 
00177 GLC_3DRep GLC_Factory::createRectangle(double l1, double l2)
00178 {
00179         GLC_3DRep newRectangle(new GLC_Rectangle(l1, l2));
00180         return newRectangle;
00181 }
00182 
00183 GLC_3DViewInstance GLC_Factory::createRectangle(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2)
00184 {
00185         // Create the rectangle to (0,0) and  z normal
00186         GLC_3DViewInstance rectangleInstance(createRectangle(l1, l2));
00187 
00188         // Create the plane rotation matrix
00189         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00190         // Vector from origin to the plane
00191         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00192 
00193         return rectangleInstance;
00194 }
00195 
00196 GLC_3DViewInstance GLC_Factory::createCuttingPlane(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2, GLC_Material* pMat)
00197 {
00198         // Create the rectangle to (0,0) and  z normal
00199         GLC_Rectangle* pRectangle= new GLC_Rectangle(l1, l2);
00200         pRectangle->replaceMasterMaterial(pMat);
00201 
00202         GLC_3DViewInstance rectangleInstance(pRectangle);
00203 
00204         // Create the plane rotation matrix
00205         const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
00206         // Vector from origin to the plane
00207         rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
00208 
00209         return rectangleInstance;
00210 
00211 }
00212 
00213 GLC_World GLC_Factory::createWorldFromFile(QFile &file, QStringList* pAttachedFileName) const
00214 {
00215         GLC_FileLoader* pLoader = createFileLoader();
00216         connect(pLoader, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00217         GLC_World world = pLoader->createWorldFromFile(file, pAttachedFileName);
00218         delete pLoader;
00219 
00220         return world;
00221 }
00222 
00223 GLC_World GLC_Factory::createWorldStructureFrom3dxml(QFile &file, bool GetExtRefName) const
00224 {
00225         GLC_World* pWorld= NULL;
00226 
00227         if (QFileInfo(file).suffix().toLower() == "3dxml")
00228         {
00229                 GLC_3dxmlToWorld d3dxmlToWorld;
00230                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00231                 pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, true, GetExtRefName);
00232         }
00233 
00234         if (NULL == pWorld)
00235         {
00236                 // File extension not recognize or file not loaded
00237                 QString message(QString("GLC_Factory::createWorldStructureFrom3dxml File ") + file.fileName() + QString(" not loaded"));
00238                 GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
00239                 throw(fileFormatException);
00240         }
00241         GLC_World resulWorld(*pWorld);
00242         delete pWorld;
00243 
00244         return resulWorld;
00245 }
00246 
00247 GLC_3DRep GLC_Factory::create3DRepFromFile(const QString& fileName) const
00248 {
00249         GLC_3DRep rep;
00250 
00251         if ((QFileInfo(fileName).suffix().toLower() == "3dxml") || (QFileInfo(fileName).suffix().toLower() == "3drep"))
00252         {
00253                 GLC_3dxmlToWorld d3dxmlToWorld;
00254                 connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
00255                 rep= d3dxmlToWorld.create3DrepFrom3dxmlRep(fileName);
00256         }
00257 
00258         return rep;
00259 
00260 }
00261 
00262 GLC_FileLoader* GLC_Factory::createFileLoader() const
00263 {
00264     return new GLC_FileLoader;
00265 }
00266 
00267 GLC_Material* GLC_Factory::createMaterial() const
00268 {
00269         return new GLC_Material();
00270 }
00271 
00272 GLC_Material* GLC_Factory::createMaterial(const GLfloat *pAmbiantColor) const
00273 {
00274         return new GLC_Material("Material", pAmbiantColor);
00275 }
00276 
00277 GLC_Material* GLC_Factory::createMaterial(const QColor &color) const
00278 {
00279         return new GLC_Material(color);
00280 }
00281 
00282 GLC_Material* GLC_Factory::createMaterial(GLC_Texture* pTexture) const
00283 {
00284         return new GLC_Material(pTexture, "TextureMaterial");
00285 }
00286 
00287 GLC_Material* GLC_Factory::createMaterial(const QString &textureFullFileName) const
00288 {
00289         GLC_Texture* pTexture= createTexture(textureFullFileName);
00290         return createMaterial(pTexture);
00291 }
00292 
00293 GLC_Material* GLC_Factory::createMaterial(const QImage &image) const
00294 {
00295         GLC_Texture* pTexture= createTexture(image);
00296         return createMaterial(pTexture);
00297 }
00298 
00299 GLC_Texture* GLC_Factory::createTexture(const QString &textureFullFileName) const
00300 {
00301         return new GLC_Texture(textureFullFileName);
00302 }
00303 
00304 GLC_Texture* GLC_Factory::createTexture(const QImage & image, const QString& imageFileName) const
00305 {
00306         return new GLC_Texture(image, imageFileName);
00307 }
00308 
00309 GLC_MoverController GLC_Factory::createDefaultMoverController(const QColor& color, GLC_Viewport* pViewport)
00310 {
00311         GLC_MoverController defaultController;
00312 
00314         // Pan Mover
00316         // Create Pan Mover representation
00317         GLC_RepMover* pRepMover= new GLC_RepCrossMover(pViewport);
00318         pRepMover->setMainColor(color);
00319         QList<GLC_RepMover*> listOfRep;
00320         listOfRep.append(pRepMover);
00321         // Create the Pan Mover
00322         GLC_Mover* pMover= new GLC_PanMover(pViewport, listOfRep);
00323         // Add the Pan Mover to the controller
00324         defaultController.addMover(pMover, GLC_MoverController::Pan);
00325 
00327         // Zoom Mover
00329         // Copy the pan Mover representation
00330         pRepMover= pRepMover->clone();
00331         listOfRep.clear();
00332         listOfRep.append(pRepMover);
00333         // Create the Zoom Mover
00334         pMover= new GLC_ZoomMover(pViewport, listOfRep);
00335         // Add the Zoom Mover to the controller
00336         defaultController.addMover(pMover, GLC_MoverController::Zoom);
00337 
00339         // Set Target Mover
00341         // Create the Zoom Mover
00342         pMover= new GLC_SetTargetMover(pViewport);
00343         // Add the Zoom Mover to the controller
00344         defaultController.addMover(pMover, GLC_MoverController::Target);
00345 
00347         // Track Ball Mover
00349         // Copy the pan Mover representation
00350         pRepMover= pRepMover->clone();
00351         listOfRep.clear();
00352         listOfRep.append(pRepMover);
00353         // Create the track ball representation
00354         pRepMover= new GLC_RepTrackBallMover(pViewport);
00355         pRepMover->setMainColor(color);
00356         listOfRep.append(pRepMover);
00357         // Create the Track Ball Mover
00358         pMover= new GLC_TrackBallMover(pViewport, listOfRep);
00359         // Add the Track ball Mover to the controller
00360         defaultController.addMover(pMover, GLC_MoverController::TrackBall);
00361 
00363         // Turn Table Mover
00365         // Create the Turn Table Mover
00366         pMover= new GLC_TurnTableMover(pViewport);
00367         // Add the Turn Table Mover to the controller
00368         defaultController.addMover(pMover, GLC_MoverController::TurnTable);
00369 
00371         // Fly Mover
00373         listOfRep.clear();
00374         pRepMover= new GLC_RepFlyMover(pViewport);
00375         pRepMover->setMainColor(color);
00376         listOfRep.append(pRepMover);
00377         // Create the fly mover
00378         pMover= new GLC_FlyMover(pViewport, listOfRep);
00379         // Add the fly mover to the controller
00380         defaultController.addMover(pMover, GLC_MoverController::Fly);
00381 
00383         // Translation, rotation and scaling Mover
00385         // Create the Turn Table Mover
00386         pMover= new GLC_TsrMover(pViewport);
00387         // Add the Turn Table Mover to the controller
00388         defaultController.addMover(pMover, GLC_MoverController::TSR);
00389 
00390         return defaultController;
00391 }
00392 
00393 
00394 void GLC_Factory::loadPlugins()
00395 {
00396         Q_ASSERT(NULL != QCoreApplication::instance());
00397         const QStringList libraryPath= QCoreApplication::libraryPaths();
00398         foreach(QString path, libraryPath)
00399         {
00400                 const QDir pluginsDir= QDir(path);
00401                 const QStringList pluginNames= pluginsDir.entryList(QDir::Files);
00402                 foreach (QString fileName, pluginNames)
00403                 {
00404                  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
00405                  QObject* pPlugin= loader.instance();
00406                  GLC_WorldReaderPlugin* pWorldReader = qobject_cast<GLC_WorldReaderPlugin *>(pPlugin);
00407                  if (pWorldReader)
00408                  {
00409                          m_WorldReaderPluginList.append(pWorldReader);
00410                          m_SupportedExtensionSet.unite(QSet<QString>::fromList(pWorldReader->keys()));
00411                  }
00412                 }
00413         }
00414 
00415         //qDebug() << m_WorldReaderPluginList.size() << " Loaded plugins.";
00416 }
00417 
00418 QList<GLC_WorldReaderPlugin*> GLC_Factory::worldReaderPlugins()
00419 {
00420         if (NULL == m_pFactory)
00421         {
00422                 instance();
00423         }
00424         return m_WorldReaderPluginList;
00425 }
00426 
00427 bool GLC_Factory::canBeLoaded(const QString& extension)
00428 {
00429         if (NULL == m_pFactory)
00430         {
00431                 instance();
00432         }
00433 
00434         return m_SupportedExtensionSet.contains(extension.toLower());
00435 }
00436 
00437 GLC_WorldReaderHandler* GLC_Factory::loadingHandler(const QString& fileName)
00438 {
00439         if (NULL == m_pFactory)
00440         {
00441                 instance();
00442         }
00443 
00444         const QString extension= QFileInfo(fileName).suffix();
00445         if (canBeLoaded(extension))
00446         {
00447                 foreach(GLC_WorldReaderPlugin* pPlugin, m_WorldReaderPluginList)
00448                 {
00449                         if (pPlugin->keys().contains(extension.toLower()))
00450                         {
00451                                 return pPlugin->readerHandler();
00452                         }
00453                 }
00454         }
00455         return NULL;
00456 }
00457 
00458 

SourceForge.net Logo

©2005-2011 Laurent Ribon