MNE-CPP  0.1.9
A Framework for Electrophysiology
view3D.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "view3D.h"
41 #include "../model/items/common/types.h"
42 #include "../model/data3Dtreemodel.h"
43 #include "../model/3dhelpers/renderable3Dentity.h"
44 #include "customframegraph.h"
45 #include "../model/3dhelpers/geometrymultiplier.h"
46 #include "../model/materials/geometrymultipliermaterial.h"
48 
49 //=============================================================================================================
50 // QT INCLUDES
51 //=============================================================================================================
52 
53 #include <QPropertyAnimation>
54 #include <QKeyEvent>
55 #include <QDate>
56 #include <QTime>
57 #include <QDir>
58 
59 #include <Qt3DCore/QTransform>
60 #include <Qt3DCore/QAspectEngine>
61 #include <Qt3DRender/QCamera>
62 #include <Qt3DExtras/QPhongMaterial>
63 #include <Qt3DRender/QPointLight>
64 #include <Qt3DRender/QRenderCaptureReply>
65 #include <Qt3DExtras/QCylinderGeometry>
66 #include <Qt3DExtras/QSphereMesh>
67 #include <Qt3DRender/QRenderSettings>
68 #include <Qt3DRender/QRenderSurfaceSelector>
69 #include <Qt3DRender/QCameraSelector>
70 #include <Qt3DRender/QTechniqueFilter>
71 #include <Qt3DRender/QClearBuffers>
72 #include <Qt3DRender/QNoDraw>
73 
74 #include <QObjectPicker>
75 #include <QPickingSettings>
76 #include <QRenderSettings>
77 #include <QPickEvent>
78 #include <QPickTriangleEvent>
79 
80 //=============================================================================================================
81 // USED NAMESPACES
82 //=============================================================================================================
83 
84 using namespace MNELIB;
85 using namespace DISP3DLIB;
86 using namespace FSLIB;
87 using namespace CONNECTIVITYLIB;
88 
89 //=============================================================================================================
90 // DEFINE MEMBER METHODS
91 //=============================================================================================================
92 
93 View3D::View3D()
94 : Qt3DExtras::Qt3DWindow()
95 , m_pRootEntity(new Qt3DCore::QEntity())
96 , m_p3DObjectsEntity(new Qt3DCore::QEntity(m_pRootEntity))
97 , m_pLightEntity(new Qt3DCore::QEntity(m_pRootEntity))
98 , m_pCamera(this->camera())
99 , m_pMultiCam1(new Qt3DRender::QCamera)
100 , m_pMultiCam2(new Qt3DRender::QCamera)
101 , m_pMultiCam3(new Qt3DRender::QCamera)
102 , m_pPicker(new Qt3DRender::QObjectPicker(m_pRootEntity))
103 , m_pCamController(new OrbitalCameraController(m_pRootEntity))
104 , m_MultiViewOrientation(View3D::MultiViewOrientation::Horizontal)
105 {
106  //Root entity
107  this->setRootEntity(m_pRootEntity);
108 
109  initSingleView();
110 
111  //Only render new frames when needed
112  this->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
113 
114  initLight();
115 
117  toggleCoordAxis(false);
118 
119  // initialize object picking and disable it by default to save resources
121  activatePicker(true);
122 
123  initMultiView();
124 
125  this->setRootEntity(m_pRootEntity);
126 
127  showSingleView();
128 }
129 
130 //=============================================================================================================
131 
133 {
134  delete m_pMultiCam1;
135  delete m_pMultiCam2;
136  delete m_pMultiCam3;
137 
138  delete m_pMultiViewport1;
139  delete m_pMultiViewport2;
140  delete m_pMultiViewport3;
141 
142  delete m_pMultiFrame;
143 }
144 
145 //=============================================================================================================
146 
148 {
149  // add object picker to root entity
150  m_pRootEntity->addComponent(m_pPicker);
151 
152  // emit signal whenever pick event occured
153  connect(m_pPicker, &Qt3DRender::QObjectPicker::pressed,
155 
156  // define renderSettings
157  this->renderSettings()->setActiveFrameGraph(m_pFrameGraph);
158  this->renderSettings()->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::PrimitivePicking);
159  this->renderSettings()->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::NearestPick);
160  this->renderSettings()->pickingSettings()->setWorldSpaceTolerance(0.00000001f);
161 }
162 
163 //=============================================================================================================
164 
165 void View3D::activatePicker(const bool bActivatePicker)
166 {
167  m_pPicker->setEnabled(bActivatePicker);
168 }
169 
170 //=============================================================================================================
171 
172 void View3D::handlePickerPress(Qt3DRender::QPickEvent *qPickEvent)
173 {
174  // only catch click events for left mouse button
175  if(qPickEvent->button() == qPickEvent->LeftButton) {
176  emit pickEventOccured(qPickEvent);
177  }
178 }
179 
180 //=============================================================================================================
181 
183 {
184  //Setup light positions, intensities and color
185  QList<QVector3D> lLightPositions;
186  const QColor lightColor(255,255,255);
187  const float lightIntensity = 0.4f;
188 
189  lLightPositions << QVector3D(-0.5,0,0) << QVector3D(0.5,0,0); /*<< QVector3D(0,0,-0.5)
190  << QVector3D(0.5,0,0) << QVector3D(0,0,0.5)
191  << QVector3D(0,0.5,0) << QVector3D(0,-0.5,0);*/
192 
193  //Create all the lights - make it shine
194  for(int i = 0; i < lLightPositions.size(); ++i) {
195  //Light source
196  Qt3DCore::QEntity* pLightEntity = new Qt3DCore::QEntity(m_pLightEntity);
197 
198  Qt3DCore::QTransform* pTransform = new Qt3DCore::QTransform();
199  pTransform->setTranslation(lLightPositions.at(i));
200  pLightEntity->addComponent(pTransform);
201 
202  Qt3DRender::QPointLight *pPointLight = new Qt3DRender::QPointLight(pLightEntity);
203  pPointLight->setColor(lightColor);
204  pPointLight->setIntensity(lightIntensity);
205  pLightEntity->addComponent(pPointLight);
206 
207  m_lLightSources.append(pPointLight);
208 
209  Qt3DExtras::QSphereMesh* lightSphere = new Qt3DExtras::QSphereMesh(pLightEntity);
210  lightSphere->setRadius(0.1f);
211  pLightEntity->addComponent(lightSphere);
212 
213  //Uncomment the following to visualize the light sources for debugging:
214 // Qt3DExtras::QPhongMaterial* material = new Qt3DExtras::QPhongMaterial(pLightEntity);
215 // material->setAmbient(lightColor);
216 // pLightEntity->addComponent(material);
217  }
218 }
219 
220 //=============================================================================================================
221 
222 void View3D::setModel(QSharedPointer<Data3DTreeModel> pModel)
223 {
224  pModel->getRootEntity()->setParent(m_p3DObjectsEntity);
225 }
226 
227 //=============================================================================================================
228 
229 void View3D::setSceneColor(const QColor& colSceneColor)
230 {
231  m_pFrameGraph->setClearColor(colSceneColor);
232 }
233 
234 //=============================================================================================================
235 
236 void View3D::toggleCoordAxis(bool bChecked)
237 {
238  m_pCoordSysEntity->setEnabled(bChecked);
239 }
240 
241 //=============================================================================================================
242 
243 void View3D::showFullScreen(bool bChecked)
244 {
245  if(bChecked) {
246  this->Qt3DWindow::showFullScreen();
247  }
248  else {
249  this->showNormal();
250  }
251 }
252 
253 //=============================================================================================================
254 
255 void View3D::setLightColor(const QColor &color)
256 {
257  for(int i = 0; i < m_lLightSources.size(); ++i) {
258  m_lLightSources.at(i)->setColor(color);
259  }
260 }
261 
262 //=============================================================================================================
263 
264 void View3D::setLightIntensity(double value)
265 {
266  for(int i = 0; i < m_lLightSources.size(); ++i) {
267  m_lLightSources.at(i)->setIntensity(value);
268  }
269 }
270 
271 //=============================================================================================================
272 
274 {
276  if(!m_pScreenCaptureReply->isComplete()) {
277  return;
278  }
279  }
280 
281  m_pScreenCaptureReply = m_pFrameGraph->requestRenderCaptureReply();
282 
283  if(!m_pScreenCaptureReply) {
284  return;
285  }
286 
287  QObject::connect(m_pScreenCaptureReply.data(), &Qt3DRender::QRenderCaptureReply::completed,
288  this, &View3D::saveScreenshot);
289 }
290 
291 //=============================================================================================================
292 
293 void View3D::saveScreenshot()
294 {
295  if(!m_pScreenCaptureReply) {
296  return;
297  }
298 
299  // Create file name
300  QString sDate = QDate::currentDate().toString("yyyy_MM_dd");
301  QString sTime = QTime::currentTime().toString("hh_mm_ss");
302 
303  if(!QDir("./Screenshots").exists()) {
304  QDir().mkdir("./Screenshots");
305  }
306 
307  QString fileName = QString("./Screenshots/%1-%2-View3D.bmp").arg(sDate).arg(sTime);
308  m_pScreenCaptureReply->saveImage(fileName);
309 
310  delete m_pScreenCaptureReply;
311 }
312 
313 //=============================================================================================================
314 
315 void View3D::keyPressEvent(QKeyEvent* e)
316 {
317  if(e->key() == Qt::Key_Escape) {
318  this->showNormal();
319  }
320 
321  Qt3DWindow::keyPressEvent(e);
322 }
323 
324 //=============================================================================================================
325 
326 void View3D::createCoordSystem(Qt3DCore::QEntity* parent)
327 {
328  m_pCoordSysEntity = QSharedPointer<Qt3DCore::QEntity>::create(parent);
329 
330  //create geometry
331  QSharedPointer<Qt3DExtras::QCylinderGeometry> pAxis = QSharedPointer<Qt3DExtras::QCylinderGeometry>::create();
332  pAxis->setRadius(0.001f);
333  pAxis->setLength(30);
334  pAxis->setRings(100);
335  pAxis->setSlices(20);
336 
337  //create mesh
338  GeometryMultiplier *pCoordSysMesh = new GeometryMultiplier(pAxis);
339  QVector<QColor> vColors;
340  vColors.reserve(3);
341  QVector<QMatrix4x4> vTransforms;
342  vTransforms.reserve(3);
343  QMatrix4x4 transformMat;
344 
345  // Y - red
346  transformMat.setToIdentity();
347  vTransforms.push_back(transformMat);
348  vColors.push_back(QColor(255, 0, 0));
349 
350  // X - blue
351  transformMat.setToIdentity();
352  transformMat.rotate(90.0f, QVector3D(0,0,1));
353  vTransforms.push_back(transformMat);
354  vColors.push_back(QColor(0, 0, 255));
355 
356  // Z - green
357  transformMat.setToIdentity();
358  transformMat.rotate(90.0f, QVector3D(1,0,0));
359  vTransforms.push_back(transformMat);
360  vColors.push_back(QColor(0, 255, 0));
361 
362  //Set transforms and colors
363  pCoordSysMesh->setTransforms(vTransforms);
364  pCoordSysMesh->setColors(vColors);
365 
366  //Add material
368  pCoordSysMaterial->setAmbient(QColor(0,0,0));
369  pCoordSysMaterial->setAlpha(1.0f);
370 
371  m_pCoordSysEntity->addComponent(pCoordSysMesh);
372  m_pCoordSysEntity->addComponent(pCoordSysMaterial);
373 }
374 
375 //=============================================================================================================
376 
377 void View3D::setCameraRotation(float fAngle)
378 {
379  m_pCamera->setPosition(QVector3D(0.0f, -0.4f, -0.25f));
380  m_pCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
381  m_pCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
382  m_pCamera->tiltAboutViewCenter(180);
383  QQuaternion quat = QQuaternion::QQuaternion::fromEulerAngles(0,0,fAngle);
384  m_pCamera->rotateAboutViewCenter(quat);
385 }
386 
387 //=============================================================================================================
388 
389 //Qt3DCore::QTransform View3D::getCameraTransform() {
390 // Qt3DCore::QTransform trans = *new Qt3DCore::QTransform(m_pCamera->transform());
391 // return trans;
392 //}
393 
394 //=============================================================================================================
395 
397 {
398  if(!m_pCameraAnimation) {
399  m_pCameraAnimation = new QPropertyAnimation(m_pCamController, "rotating");
400  m_pCameraAnimation->setStartValue(QVariant::fromValue(1));
401  m_pCameraAnimation->setEndValue(QVariant::fromValue(10000));
402  m_pCameraAnimation->setDuration(10000);
403  m_pCameraAnimation->setLoopCount(-1);
404  }
405 
406  if(bChecked) {
407  //Start animation
408  m_pCameraAnimation->start();
409  }
410  else {
411  m_pCameraAnimation->stop();
412  }
413 }
414 
415 //=============================================================================================================
416 
418 {
419  m_pCamera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.0001f, 100000.0f);
420  m_pCamera->setPosition(QVector3D(0.0f, -0.4f, -0.25f));
421  m_pCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
422  m_pCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
423  m_pCamera->tiltAboutViewCenter(180);
424  m_pCamera->lens()->setPerspectiveProjection(45.0f, this->width()/this->height(), 0.01f, 5000.0f);
425  m_pFrameGraph->setCamera(m_pCamera);
426 
427  m_pCamController->setCamera(m_pCamera);
428 }
429 
430 //=============================================================================================================
431 
433 {
434  m_pMultiCam1->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.0001f, 100000.0f);
435  m_pMultiCam1->setPosition(QVector3D(0.0f, 0.65f, 0.0f));
436  m_pMultiCam1->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
437  m_pMultiCam1->setUpVector(QVector3D(0.0f, 0.0f, 1.0f));
438 // m_pMultiCam1->setParent(m_pRootEntity);
439 
440  m_pMultiCam2->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.0001f, 100000.0f);
441  m_pMultiCam2->setPosition(QVector3D(-0.65f, 0.0f, 0.0f));
442  m_pMultiCam2->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
443  m_pMultiCam2->setUpVector(QVector3D(0.0f, 0.0f, 1.0f));
444 // m_pMultiCam2->setParent(m_pRootEntity);
445 
446  m_pMultiCam3->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.0001f, 100000.0f);
447  m_pMultiCam3->setPosition(QVector3D(0.0f, 0.0f, 0.65f));
448  m_pMultiCam3->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
449  m_pMultiCam3->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
450 // m_pMultiCam3->setParent(m_pRootEntity);
451 
452 }
453 
454 //=============================================================================================================
455 
457 {
458  //FrameGraph
460  m_pFrameGraph->setClearColor(QColor::fromRgbF(0.0, 0.0, 0.0, 1.0));
461 
462  initSingleCam();
463 }
464 
465 //=============================================================================================================
466 
468 {
469  initMultiCams();
470 
471  // Framegraph root node
472  m_pMultiFrame = new Qt3DRender::QRenderSurfaceSelector();
473  auto mainViewPort = new Qt3DRender::QViewport(m_pMultiFrame);
474 
475  // First RenderView: clear buffers
476  auto clearBuffers = new Qt3DRender::QClearBuffers(mainViewPort);
477  clearBuffers->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);
478  clearBuffers->setClearColor(QColor::fromRgbF(0.0, 0.0, 0.0, 1.0));
479 
480  m_pMultiViewport1 = new Qt3DRender::QViewport(mainViewPort);
481  m_pMultiViewport1->setNormalizedRect(QRectF(0.0f, 0.0f, 0.333f, 1.0f));
482  auto cameraSelector1 = new Qt3DRender::QCameraSelector(m_pMultiViewport1);
483  cameraSelector1->setCamera(m_pMultiCam1);
484 
485  m_pMultiViewport2 = new Qt3DRender::QViewport(mainViewPort);
486  m_pMultiViewport2->setNormalizedRect(QRectF(0.333f, 0.0f, 0.333f, 1.0f));
487  auto cameraSelector2= new Qt3DRender::QCameraSelector(m_pMultiViewport2);
488  cameraSelector2->setCamera(m_pMultiCam2);
489 
490  m_pMultiViewport3 = new Qt3DRender::QViewport(mainViewPort);
491  m_pMultiViewport3->setNormalizedRect(QRectF(0.666f, 0.0f, 0.333f, 1.0f));
492  auto cameraSelector3= new Qt3DRender::QCameraSelector(m_pMultiViewport3);
493  cameraSelector3->setCamera(m_pMultiCam3);
494 
496 
497  auto noDraw = new Qt3DRender::QNoDraw(clearBuffers);
498 }
499 
500 //=============================================================================================================
501 
502 void View3D::resizeEvent(QResizeEvent *event)
503 {
505  //single view updates implicitly due to use of default camera from Qt3DWindow.
506 
507  Qt3DWindow::resizeEvent(event);
508 }
509 
510 //=============================================================================================================
511 
513 {
514  float fAspectRatio = static_cast<float>(this->width())/static_cast<float>(this->height());
515 
516  if(fAspectRatio > 1.5f ){
517  m_pMultiCam1->setAspectRatio(fAspectRatio / 3.0f);
518  m_pMultiCam2->setAspectRatio(fAspectRatio / 3.0f);
519  m_pMultiCam3->setAspectRatio(fAspectRatio / 3.0f);
520 
522  } else {
523  m_pMultiCam1->setAspectRatio(fAspectRatio * 3.0f);
524  m_pMultiCam2->setAspectRatio(fAspectRatio * 3.0f);
525  m_pMultiCam3->setAspectRatio(fAspectRatio * 3.0f);
526 
528  }
529 }
530 
531 //=============================================================================================================
532 
534 {
535  if (m_MultiViewOrientation != View3D::MultiViewOrientation::Veritical){
536  m_pMultiViewport1->setNormalizedRect(QRectF(0.0f, 0.0f, 1.0f, 0.333f));
537  m_pMultiViewport2->setNormalizedRect(QRectF(0.0f, 0.333f, 1.0f, 0.333f));
538  m_pMultiViewport3->setNormalizedRect(QRectF(0.0f, 0.666f, 1.0f, 0.333f));
539 
540  m_MultiViewOrientation = View3D::MultiViewOrientation::Veritical;
541  }
542 }
543 
544 //=============================================================================================================
545 
547 {
548  if (m_MultiViewOrientation != View3D::MultiViewOrientation::Horizontal){
549  m_pMultiViewport1->setNormalizedRect(QRectF(0.0f, 0.0f, 0.333f, 1.0f));
550  m_pMultiViewport2->setNormalizedRect(QRectF(0.333f, 0.0f, 0.333f, 1.0f));
551  m_pMultiViewport3->setNormalizedRect(QRectF(0.666f, 0.0f, 0.333f, 1.0f));
552 
553  m_MultiViewOrientation = View3D::MultiViewOrientation::Horizontal;
554  }
555 }
556 
557 //=============================================================================================================
558 
560 {
561  this->setActiveFrameGraph(m_pFrameGraph);
562 }
563 
564 //=============================================================================================================
565 
567 {
568  this->setActiveFrameGraph(m_pMultiFrame);
569 }
QPointer< Qt3DRender::QRenderCaptureReply > m_pScreenCaptureReply
Definition: view3D.h:311
QPointer< Qt3DCore::QEntity > m_pRootEntity
Definition: view3D.h:294
void setModel(QSharedPointer< DISP3DLIB::Data3DTreeModel > pModel)
Definition: view3D.cpp:222
QPointer< Qt3DRender::QCamera > m_pMultiCam3
Definition: view3D.h:305
QPointer< Qt3DRender::QObjectPicker > m_pPicker
Definition: view3D.h:312
void showMultiView()
Definition: view3D.cpp:566
MultiViewOrientation m_MultiViewOrientation
Definition: view3D.h:319
OrbitalCameraController class declaration.
void startStopCameraRotation(bool bChecked)
Definition: view3D.cpp:396
This class allows controlling the scene camera along an orbital path.
QPointer< QPropertyAnimation > m_pCameraAnimation
Definition: view3D.h:315
QPointer< OrbitalCameraController > m_pCamController
Definition: view3D.h:314
void setColors(const QVector< QColor > &tInstanceColors)
void toggleCoordAxis(bool bChecked)
Definition: view3D.cpp:236
QPointer< Qt3DCore::QEntity > m_p3DObjectsEntity
Definition: view3D.h:295
QPointer< CustomFrameGraph > m_pFrameGraph
Definition: view3D.h:299
View3D class declaration.
void setTransforms(const QVector< QMatrix4x4 > &tInstanceTansform)
Instaced based renderer.
CustomFrameGraph class declaration.
QPointer< Qt3DRender::QViewport > m_pMultiViewport1
Definition: view3D.h:307
Custom framegaph class.
QPointer< Qt3DRender::QViewport > m_pMultiViewport3
Definition: view3D.h:309
void initLight()
Definition: view3D.cpp:182
QSharedPointer< Qt3DCore::QEntity > m_pCoordSysEntity
Definition: view3D.h:297
Custom phong alpha material for instanced rendering.
QPointer< Qt3DRender::QViewport > m_pMultiViewport2
Definition: view3D.h:308
void setCameraRotation(float fAngle)
Definition: view3D.cpp:377
void initMultiCams()
Definition: view3D.cpp:432
void updateMultiViewAspectRatio()
Definition: view3D.cpp:512
void setMultiViewHorizontal()
Definition: view3D.cpp:546
QList< QPointer< Qt3DRender::QPointLight > > m_lLightSources
Definition: view3D.h:317
void setLightColor(const QColor &color)
Definition: view3D.cpp:255
void activatePicker(const bool bActivatePicker)
Definition: view3D.cpp:165
void resizeEvent(QResizeEvent *) override
Definition: view3D.cpp:502
void createCoordSystem(Qt3DCore::QEntity *parent)
Definition: view3D.cpp:326
QPointer< Qt3DCore::QEntity > m_pLightEntity
Definition: view3D.h:296
QPointer< Qt3DRender::QCamera > m_pCamera
Definition: view3D.h:302
void initObjectPicking()
Definition: view3D.cpp:147
void handlePickerPress(Qt3DRender::QPickEvent *qPickEvent)
Definition: view3D.cpp:172
void keyPressEvent(QKeyEvent *e) override
Definition: view3D.cpp:315
QPointer< Qt3DRender::QCamera > m_pMultiCam1
Definition: view3D.h:303
void showSingleView()
Definition: view3D.cpp:559
void takeScreenshot()
Definition: view3D.cpp:273
void initMultiView()
Definition: view3D.cpp:467
void setLightIntensity(double value)
Definition: view3D.cpp:264
void setMultiViewVertical()
Definition: view3D.cpp:533
void initSingleCam()
Definition: view3D.cpp:417
QPointer< Qt3DRender::QCamera > m_pMultiCam2
Definition: view3D.h:304
void setSceneColor(const QColor &colSceneColor)
Definition: view3D.cpp:229
Visualizes 3D data.
Definition: view3D.h:90
void showFullScreen(bool bChecked)
Definition: view3D.cpp:243
void initSingleView()
Definition: view3D.cpp:456