GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_factory.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  Copyright (C) 2011 Jrme Forrissier
6  http://glc-lib.sourceforge.net
7 
8  GLC-lib is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  GLC-lib is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with GLC-lib; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 *****************************************************************************/
23 
25 
26 #include "glc_factory.h"
27 #include "io/glc_fileloader.h"
28 #include "io/glc_3dxmltoworld.h"
30 
31 #include "viewport/glc_panmover.h"
32 #include "viewport/glc_zoommover.h"
38 #include "viewport/glc_flymover.h"
40 #include "viewport/glc_tsrmover.h"
41 #include "maths/glc_line3d.h"
42 #include "maths/glc_geomtools.h"
43 
45 
46 // init static member
48 QList<GLC_WorldReaderPlugin*> GLC_Factory::m_WorldReaderPluginList;
50 
52 // static method
54 // Return the unique instance of the factory
56 {
57  if(m_pFactory == NULL)
58  {
59  m_pFactory= new GLC_Factory();
60  }
61  return m_pFactory;
62 }
63 
65 // Constructor destructor
67 
68 // Protected constructor
70 {
71  loadPlugins();
72 }
73 
74 // Destructor
76 {
77 
78 }
79 
81 // Create functions
83 
85 {
86  GLC_3DRep newPoint(new GLC_Point(coord));
87  return newPoint;
88 }
89 
90 GLC_3DRep GLC_Factory::createPoint(double x, double y, double z) const
91 {
92  GLC_3DRep newPoint(new GLC_Point(x, y, z));
93  return newPoint;
94 }
95 
96 GLC_3DRep GLC_Factory::createPointCloud(const GLfloatVector& data, const QColor& color)
97 {
98  GLC_PointCloud* pPointCloud= new GLC_PointCloud();
99  pPointCloud->addPoint(data);
100  pPointCloud->setWireColor(color);
101  return GLC_3DRep(pPointCloud);
102 }
103 
104 GLC_3DRep GLC_Factory::createPointCloud(const QList<GLC_Point3d>& pointList, const QColor& color)
105 {
106  GLC_PointCloud* pPointCloud= new GLC_PointCloud();
107  pPointCloud->addPoint(pointList);
108  pPointCloud->setWireColor(color);
109  return GLC_3DRep(pPointCloud);
110 }
111 
112 GLC_3DRep GLC_Factory::createPointCloud(const QList<GLC_Point3df>& pointList, const QColor& color)
113 {
114  GLC_PointCloud* pPointCloud= new GLC_PointCloud();
115  pPointCloud->addPoint(pointList);
116  pPointCloud->setWireColor(color);
117  return GLC_3DRep(pPointCloud);
118 }
119 
120 
122 {
123  GLC_3DRep newPoint(new GLC_PointSprite(size, pMaterial));
124  return newPoint;
125 }
126 
127 GLC_3DRep GLC_Factory::createLine(const GLC_Point3d& point1, const GLC_Point3d& point2) const
128 {
129  GLC_3DRep newPoint(new GLC_Line(point1, point2));
130  return newPoint;
131 }
132 
133 GLC_3DRep GLC_Factory::createCircle(double radius, double angle) const
134 {
135  GLC_3DRep newCircle(new GLC_Circle(radius, angle));
136  return newCircle;
137 }
138 
139 GLC_3DRep GLC_Factory::createBox(double lx, double ly, double lz) const
140 {
141 
142  GLC_3DRep newBox(new GLC_Box(lx, ly, lz));
143  return newBox;
144 }
145 
147 {
148  const double lx= boundingBox.upperCorner().x() - boundingBox.lowerCorner().x();
149  const double ly= boundingBox.upperCorner().y() - boundingBox.lowerCorner().y();
150  const double lz= boundingBox.upperCorner().z() - boundingBox.lowerCorner().z();
151  GLC_Box* pBox= new GLC_Box(lx, ly, lz);
152  GLC_3DViewInstance newBox(pBox);
153  newBox.translate(boundingBox.center().x(), boundingBox.center().y()
154  , boundingBox.center().z());
155  return newBox;
156 }
157 
158 GLC_3DRep GLC_Factory::createCylinder(double radius, double length) const
159 {
160 
161  GLC_3DRep newCylinder(new GLC_Cylinder(radius, length));
162  return newCylinder;
163 }
164 
165 GLC_3DRep GLC_Factory::createCone(double radius, double length) const
166 {
167  GLC_3DRep newCone(new GLC_Cone(radius, length));
168  return newCone;
169 }
170 
172 {
173  GLC_3DRep newSphere(new GLC_Sphere(radius));
174  return newSphere;
175 }
176 
178 {
179  GLC_3DRep newRectangle(new GLC_Rectangle(l1, l2));
180  return newRectangle;
181 }
182 
183 GLC_3DViewInstance GLC_Factory::createRectangle(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2)
184 {
185  // Create the rectangle to (0,0) and z normal
186  GLC_3DViewInstance rectangleInstance(createRectangle(l1, l2));
187 
188  // Create the plane rotation matrix
189  const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
190  // Vector from origin to the plane
191  rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
192 
193  return rectangleInstance;
194 }
195 
196 GLC_3DViewInstance GLC_Factory::createCuttingPlane(const GLC_Point3d& point, const GLC_Vector3d& normal, double l1, double l2, GLC_Material* pMat)
197 {
198  // Create the rectangle to (0,0) and z normal
199  GLC_Rectangle* pRectangle= new GLC_Rectangle(l1, l2);
200  pRectangle->replaceMasterMaterial(pMat);
201 
202  GLC_3DViewInstance rectangleInstance(pRectangle);
203 
204  // Create the plane rotation matrix
205  const GLC_Matrix4x4 rotationMatrix(glc::Z_AXIS, normal);
206  // Vector from origin to the plane
207  rectangleInstance.setMatrix(GLC_Matrix4x4(point) * rotationMatrix);
208 
209  return rectangleInstance;
210 
211 }
212 
213 GLC_World GLC_Factory::createWorldFromFile(QFile &file, QStringList* pAttachedFileName) const
214 {
215  GLC_FileLoader* pLoader = createFileLoader();
216  connect(pLoader, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
217  GLC_World world = pLoader->createWorldFromFile(file, pAttachedFileName);
218  delete pLoader;
219 
220  return world;
221 }
222 
223 GLC_World GLC_Factory::createWorldStructureFrom3dxml(QFile &file, bool GetExtRefName) const
224 {
225  GLC_World* pWorld= NULL;
226 
227  if (QFileInfo(file).suffix().toLower() == "3dxml")
228  {
229  GLC_3dxmlToWorld d3dxmlToWorld;
230  connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
231  pWorld= d3dxmlToWorld.createWorldFrom3dxml(file, true, GetExtRefName);
232  }
233 
234  if (NULL == pWorld)
235  {
236  // File extension not recognize or file not loaded
237  QString message(QString("GLC_Factory::createWorldStructureFrom3dxml File ") + file.fileName() + QString(" not loaded"));
238  GLC_FileFormatException fileFormatException(message, file.fileName(), GLC_FileFormatException::FileNotSupported);
239  throw(fileFormatException);
240  }
241  GLC_World resulWorld(*pWorld);
242  delete pWorld;
243 
244  return resulWorld;
245 }
246 
247 GLC_3DRep GLC_Factory::create3DRepFromFile(const QString& fileName) const
248 {
249  GLC_3DRep rep;
250 
251  if ((QFileInfo(fileName).suffix().toLower() == "3dxml") || (QFileInfo(fileName).suffix().toLower() == "3drep") || (QFileInfo(fileName).suffix().toLower() == "xml"))
252  {
253  GLC_3dxmlToWorld d3dxmlToWorld;
254  connect(&d3dxmlToWorld, SIGNAL(currentQuantum(int)), this, SIGNAL(currentQuantum(int)));
255  rep= d3dxmlToWorld.create3DrepFrom3dxmlRep(fileName);
256  }
257 
258  return rep;
259 
260 }
261 
263 {
264  return new GLC_FileLoader;
265 }
266 
268 {
269  return new GLC_Material();
270 }
271 
272 GLC_Material* GLC_Factory::createMaterial(const GLfloat *pAmbiantColor) const
273 {
274  return new GLC_Material("Material", pAmbiantColor);
275 }
276 
277 GLC_Material* GLC_Factory::createMaterial(const QColor &color) const
278 {
279  return new GLC_Material(color);
280 }
281 
283 {
284  return new GLC_Material(pTexture, "TextureMaterial");
285 }
286 
287 GLC_Material* GLC_Factory::createMaterial(const QString &textureFullFileName) const
288 {
289  GLC_Texture* pTexture= createTexture(textureFullFileName);
290  return createMaterial(pTexture);
291 }
292 
293 GLC_Material* GLC_Factory::createMaterial(const QImage &image) const
294 {
295  GLC_Texture* pTexture= createTexture(image);
296  return createMaterial(pTexture);
297 }
298 
299 GLC_Texture* GLC_Factory::createTexture(const QString &textureFullFileName) const
300 {
301  return new GLC_Texture(textureFullFileName);
302 }
303 
304 GLC_Texture* GLC_Factory::createTexture(const QImage & image, const QString& imageFileName) const
305 {
306  return new GLC_Texture(image, imageFileName);
307 }
308 
310 {
311  GLC_MoverController defaultController;
312 
314  // Pan Mover
316  // Create Pan Mover representation
317  GLC_RepMover* pRepMover= new GLC_RepCrossMover(pViewport);
318  pRepMover->setMainColor(color);
319  QList<GLC_RepMover*> listOfRep;
320  listOfRep.append(pRepMover);
321  // Create the Pan Mover
322  GLC_Mover* pMover= new GLC_PanMover(pViewport, listOfRep);
323  // Add the Pan Mover to the controller
324  defaultController.addMover(pMover, GLC_MoverController::Pan);
325 
327  // Zoom Mover
329  // Copy the pan Mover representation
330  pRepMover= pRepMover->clone();
331  listOfRep.clear();
332  listOfRep.append(pRepMover);
333  // Create the Zoom Mover
334  pMover= new GLC_ZoomMover(pViewport, listOfRep);
335  // Add the Zoom Mover to the controller
336  defaultController.addMover(pMover, GLC_MoverController::Zoom);
337 
339  // Set Target Mover
341  // Create the Zoom Mover
342  pMover= new GLC_SetTargetMover(pViewport);
343  // Add the Zoom Mover to the controller
344  defaultController.addMover(pMover, GLC_MoverController::Target);
345 
347  // Track Ball Mover
349  // Copy the pan Mover representation
350  pRepMover= pRepMover->clone();
351  listOfRep.clear();
352  listOfRep.append(pRepMover);
353  // Create the track ball representation
354  pRepMover= new GLC_RepTrackBallMover(pViewport);
355  pRepMover->setMainColor(color);
356  listOfRep.append(pRepMover);
357  // Create the Track Ball Mover
358  pMover= new GLC_TrackBallMover(pViewport, listOfRep);
359  // Add the Track ball Mover to the controller
360  defaultController.addMover(pMover, GLC_MoverController::TrackBall);
361 
363  // Turn Table Mover
365  // Create the Turn Table Mover
366  pMover= new GLC_TurnTableMover(pViewport);
367  // Add the Turn Table Mover to the controller
368  defaultController.addMover(pMover, GLC_MoverController::TurnTable);
369 
371  // Fly Mover
373  listOfRep.clear();
374  pRepMover= new GLC_RepFlyMover(pViewport);
375  pRepMover->setMainColor(color);
376  listOfRep.append(pRepMover);
377  // Create the fly mover
378  pMover= new GLC_FlyMover(pViewport, listOfRep);
379  // Add the fly mover to the controller
380  defaultController.addMover(pMover, GLC_MoverController::Fly);
381 
383  // Translation, rotation and scaling Mover
385  // Create the Turn Table Mover
386  pMover= new GLC_TsrMover(pViewport);
387  // Add the Turn Table Mover to the controller
388  defaultController.addMover(pMover, GLC_MoverController::TSR);
389 
390  return defaultController;
391 }
392 
393 
395 {
396  Q_ASSERT(NULL != QCoreApplication::instance());
397  const QStringList libraryPath= QCoreApplication::libraryPaths();
398  foreach(QString path, libraryPath)
399  {
400  const QDir pluginsDir= QDir(path);
401  const QStringList pluginNames= pluginsDir.entryList(QDir::Files);
402  foreach (QString fileName, pluginNames)
403  {
404  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
405  QObject* pPlugin= loader.instance();
406  GLC_WorldReaderPlugin* pWorldReader = qobject_cast<GLC_WorldReaderPlugin *>(pPlugin);
407  if (pWorldReader)
408  {
409  m_WorldReaderPluginList.append(pWorldReader);
410  m_SupportedExtensionSet.unite(QSet<QString>::fromList(pWorldReader->keys()));
411  }
412  }
413  }
414 
415  //qDebug() << m_WorldReaderPluginList.size() << " Loaded plugins.";
416 }
417 
418 QList<GLC_WorldReaderPlugin*> GLC_Factory::worldReaderPlugins()
419 {
420  if (NULL == m_pFactory)
421  {
422  instance();
423  }
425 }
426 
427 bool GLC_Factory::canBeLoaded(const QString& extension)
428 {
429  if (NULL == m_pFactory)
430  {
431  instance();
432  }
433 
434  return m_SupportedExtensionSet.contains(extension.toLower());
435 }
436 
438 {
439  if (NULL == m_pFactory)
440  {
441  instance();
442  }
443 
444  const QString extension= QFileInfo(fileName).suffix();
445  if (canBeLoaded(extension))
446  {
448  {
449  if (pPlugin->keys().contains(extension.toLower()))
450  {
451  return pPlugin->readerHandler();
452  }
453  }
454  }
455  return NULL;
456 }
457 
458 

©2005-2013 Laurent Ribon