MNE-CPP  beta 1.0
geometryview.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "geometryview.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // QT INCLUDES
48 //=============================================================================================================
49 
50 #include "qglbuilder.h"
51 #include "qglcube.h"
52 
53 #include <QtCore/qurl.h>
54 #include <QArray>
55 #include <QMouseEvent>
56 
57 
58 //*************************************************************************************************************
59 //=============================================================================================================
60 // STL INCLUDES
61 //=============================================================================================================
62 
63 #include <iostream>
64 
65 
66 //*************************************************************************************************************
67 //=============================================================================================================
68 // USED NAMESPACES
69 //=============================================================================================================
70 
71 using namespace DISP3DLIB;
72 
73 
74 //*************************************************************************************************************
75 //=============================================================================================================
76 // DEFINE MEMBER METHODS
77 //=============================================================================================================
78 
79 GeometryView::GeometryView(const MNESourceSpace &p_sourceSpace, QWindow *parent)
80 : QGLView(parent)
81 , m_sourceSpace(p_sourceSpace)
82 , m_bStereo(true)
83 , m_fOffsetZ(-100.0f)
84 , m_fOffsetZEye(60.0f)
85 , m_pSceneNodeBrain(0)
86 , m_pSceneNode(0)
87 {
88  m_vecAnnotation.append(Annotation::SPtr(new Annotation("./MNE-sample-data/subjects/sample/label/lh.aparc.a2009s.annot")));
89  m_vecAnnotation.append(Annotation::SPtr(new Annotation("./MNE-sample-data/subjects/sample/label/rh.aparc.a2009s.annot")));
90 
91 // m_pCameraFrontal = new QGLCamera(this);
92 // m_pCameraFrontal->setAdjustForAspectRatio(false);
93 }
94 
95 
96 //*************************************************************************************************************
97 
99 {
100  delete m_pSceneNode;
101 }
102 
103 //*************************************************************************************************************
104 
105 void GeometryView::initializeGL(QGLPainter *painter)
106 {
107  if(!m_sourceSpace.isEmpty())
108  {
109  // in the constructor construct a builder on the stack
110  QGLBuilder builder;
111 
112  float fac = 100.0f;
113 
114  builder << QGL::Faceted;
115  m_pSceneNodeBrain = builder.currentNode();
116 
117  builder.pushNode();
118 
119  // Collor palette
120  qint32 index;
121  QSharedPointer<QGLMaterialCollection> palette = builder.sceneNode()->palette(); // register color palette within the root node
122 
123  //get bounding box // ToDo separate function
124  m_vecBoundingBoxMin.setX(m_sourceSpace[0].rr.col(0).minCoeff()); // X lh min
125  m_vecBoundingBoxMin.setY(m_sourceSpace[0].rr.col(1).minCoeff()); // Y lh min
126  m_vecBoundingBoxMin.setZ(m_sourceSpace[0].rr.col(2).minCoeff()); // Z lh min
127  m_vecBoundingBoxMax.setX(m_sourceSpace[0].rr.col(0).maxCoeff()); // X lh max
128  m_vecBoundingBoxMax.setY(m_sourceSpace[0].rr.col(1).maxCoeff()); // Y lh max
129  m_vecBoundingBoxMax.setZ(m_sourceSpace[0].rr.col(2).maxCoeff()); // Z lh max
130 
131  m_vecBoundingBoxMin.setX(m_vecBoundingBoxMin.x() < m_sourceSpace[1].rr.col(0).minCoeff() ? m_vecBoundingBoxMin.x() : m_sourceSpace[1].rr.col(0).minCoeff()); // X rh min
132  m_vecBoundingBoxMin.setY(m_vecBoundingBoxMin.y() < m_sourceSpace[1].rr.col(1).minCoeff() ? m_vecBoundingBoxMin.y() : m_sourceSpace[1].rr.col(1).minCoeff()); // Y rh min
133  m_vecBoundingBoxMin.setZ(m_vecBoundingBoxMin.z() < m_sourceSpace[1].rr.col(2).minCoeff() ? m_vecBoundingBoxMin.z() : m_sourceSpace[1].rr.col(2).minCoeff()); // Z rh min
134  m_vecBoundingBoxMax.setX(m_vecBoundingBoxMax.x() > m_sourceSpace[1].rr.col(0).maxCoeff() ? m_vecBoundingBoxMax.x() : m_sourceSpace[1].rr.col(0).maxCoeff()); // X rh max
135  m_vecBoundingBoxMax.setY(m_vecBoundingBoxMax.y() > m_sourceSpace[1].rr.col(1).maxCoeff() ? m_vecBoundingBoxMax.y() : m_sourceSpace[1].rr.col(1).maxCoeff()); // Y rh max
136  m_vecBoundingBoxMax.setZ(m_vecBoundingBoxMax.z() > m_sourceSpace[1].rr.col(2).maxCoeff() ? m_vecBoundingBoxMax.z() : m_sourceSpace[1].rr.col(2).maxCoeff()); // Z rh max
137 
138  m_vecBoundingBoxCenter.setX((m_vecBoundingBoxMin.x()+m_vecBoundingBoxMax.x())/2.0f);
139  m_vecBoundingBoxCenter.setY((m_vecBoundingBoxMin.y()+m_vecBoundingBoxMax.y())/2.0f);
140  m_vecBoundingBoxCenter.setZ((m_vecBoundingBoxMin.z()+m_vecBoundingBoxMax.z())/2.0f);
141 
142 
143  //
144  // Build each hemisphere in its separate node
145  //
146  for(qint32 h = 0; h < 2; ++h)
147  {
148  builder.newNode();//create new hemisphere node
149  {
150 
151  MatrixX3i tris = m_sourceSpace[h].tris;
152  MatrixX3f rr = m_sourceSpace[h].rr;
153 
154  //Centralize
155  rr.col(0) = rr.col(0).array() - m_vecBoundingBoxCenter.x();
156  rr.col(1) = rr.col(1).array() - m_vecBoundingBoxCenter.y();
157  rr.col(2) = rr.col(2).array() - m_vecBoundingBoxCenter.z();
158 
159  builder.pushNode();
160  //
161  // Create each ROI in its own node
162  //
163  for(qint32 k = 0; k < m_vecAnnotation[h]->getColortable().numEntries; ++k)
164  {
165  // add new ROI node when current ROI node is not empty
166  if(builder.currentNode()->count() > 0)
167  builder.newNode();
168 
169  QGeometryData t_GeometryDataTri;
170 
171  MatrixXf t_TriCoords(3,3*tris.rows());
172  qint32 t_size = 0;
173  qint32 t_label_ids = m_vecAnnotation[h]->getColortable().table(k,4);
174 
175  for(qint32 i = 0; i < tris.rows(); ++i)
176  {
177  if(m_vecAnnotation[h]->getLabelIds()(tris(i,0)) == t_label_ids || m_vecAnnotation[h]->getLabelIds()(tris(i,1)) == t_label_ids || m_vecAnnotation[h]->getLabelIds()(tris(i,2)) == t_label_ids)
178  {
179  t_TriCoords.col(t_size*3) = rr.row( tris(i,0) ).transpose();
180  t_TriCoords.col(t_size*3+1) = rr.row( tris(i,1) ).transpose();
181  t_TriCoords.col(t_size*3+2) = rr.row( tris(i,2) ).transpose();
182  ++t_size;
183  }
184  }
185  t_TriCoords.conservativeResize(3, 3*t_size);
186  t_TriCoords *= fac;
187  t_GeometryDataTri.appendVertexArray(QArray<QVector3D>::fromRawData( reinterpret_cast<const QVector3D*>(t_TriCoords.data()), t_TriCoords.cols() ));
188 
189  //
190  // If triangles are available.
191  //
192  if (t_GeometryDataTri.count() > 0)
193  {
194 
195  //
196  // Add triangles to current node
197  //
198  builder.addTriangles(t_GeometryDataTri);
199 
200  //
201  // Colorize ROI
202  //
203  QGLMaterial *t_pMaterialROI = new QGLMaterial();
204  int r, g, b;
205  r = m_vecAnnotation[h]->getColortable().table(k,0);
206  g = m_vecAnnotation[h]->getColortable().table(k,1);
207  b = m_vecAnnotation[h]->getColortable().table(k,2);
208 
209  t_pMaterialROI->setColor(QColor(r,g,b,200));
210 // t_pMaterialROI->setEmittedLight(QColor(100,100,100,255));
211 // t_pMaterialROI->setSpecularColor(QColor(10,10,10,20));
212 
213 
214  index = palette->addMaterial(t_pMaterialROI);
215  builder.currentNode()->setMaterialIndex(index);
216  }
217  }
218  }
219  // Go one level up
220  builder.popNode();
221  }
222  // Go one level up
223  builder.popNode();
224 
225  // Optimze current scene for display and calculate lightning normals
226  m_pSceneNode = builder.finalizedSceneNode();
227  m_pSceneNode->setParent(this);
228 
229  //
230  // Create light models
231  //
232  m_pLightModel = new QGLLightModel(this);
233  m_pLightModel->setAmbientSceneColor(Qt::white);
234  m_pLightModel->setViewerPosition(QGLLightModel::LocalViewer);
235 
236  m_pLightModel = new QGLLightModel(this);
237 
238  m_pLightParametersScene = new QGLLightParameters(this);
239  m_pLightParametersScene->setPosition(QVector3D(0.0f, 0.0f, 3.0f));
240  painter->setMainLight(m_pLightParametersScene);
241 
242  //
243  // Set stereo type
244  //
245  if (m_bStereo) {
246  this->setStereoType(QGLView::RedCyanAnaglyph);
247 // this->setStereoType(QGLView::StretchedLeftRight);
248 // camera()->setEyeSeparation(0.4f);
249 // m_pCameraFrontal->setEyeSeparation(0.1f);
250 
251  //LNdT DEMO
252  camera()->setCenter(QVector3D(0,0,m_fOffsetZ));//0.8f*fac));
253  camera()->setEyeSeparation(0.4f);
254  camera()->setFieldOfView(30);
255  camera()->setEye(QVector3D(0,0,m_fOffsetZEye));
256  //LNdT DEMO end
257 
258 // m_pCameraFrontal->setEyeSeparation(0.1f);
259  }
260 
261  //set background to white
262  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
263 // //set background to light grey-blue
264 // glClearColor(0.8f, 0.8f, 1.0f, 0.0f);
265  }
266 
267 }
268 
269 //*************************************************************************************************************
270 
271 void GeometryView::paintGL(QGLPainter *painter)
272 {
273  glEnable(GL_BLEND); // enable transparency
274 
275  // painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
276 
277  painter->modelViewMatrix().push();
278  painter->projectionMatrix().push();
279 
280  painter->setStandardEffect(QGL::LitMaterial);
281 // painter->setCamera(m_pCameraFrontal);
282  painter->setLightModel(m_pLightModel);
283 
284 // material.bind(painter);
285 // material.prepareToDraw(painter, painter->attributes());
286 
287  m_pSceneNode->draw(painter);
288 
289 
290  painter->modelViewMatrix().pop();
291  painter->projectionMatrix().pop();
292 }
293 
294 //*************************************************************************************************************
295 
296 void GeometryView::keyPressEvent(QKeyEvent *e)
297 {
298  camera()->setCenter(QVector3D(0,0,0));
299 
300  float normEyeOld = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
301 
302  QGLView::keyPressEvent(e);
303 
304  float dx = (camera()->eye().x()*m_fOffsetZ)/m_fOffsetZEye;
305  float dy = (camera()->eye().y()*m_fOffsetZ)/m_fOffsetZEye;
306  float dz = (camera()->eye().z()*m_fOffsetZ)/m_fOffsetZEye;
307 
308  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
309  float scaleEye = normEyeOld/normEye;//m_fOffsetZEye/normEye;
310  camera()->setEye(QVector3D(camera()->eye().x()*scaleEye,camera()->eye().y()*scaleEye,camera()->eye().z()*scaleEye));
311 
312  camera()->setCenter(QVector3D(dx,dy,dz));
313 }
314 
315 
316 //*************************************************************************************************************
317 
318 void GeometryView::mouseMoveEvent(QMouseEvent *e)
319 {
320  camera()->setCenter(QVector3D(0,0,0));
321 
322  float normEyeOld = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
323 
324  QGLView::mouseMoveEvent(e);
325 
326  float dx = (camera()->eye().x()*m_fOffsetZ)/m_fOffsetZEye;
327  float dy = (camera()->eye().y()*m_fOffsetZ)/m_fOffsetZEye;
328  float dz = (camera()->eye().z()*m_fOffsetZ)/m_fOffsetZEye;
329 
330  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
331  float scaleEye = normEyeOld/normEye;//m_fOffsetZEye/normEye;
332  camera()->setEye(QVector3D(camera()->eye().x()*scaleEye,camera()->eye().y()*scaleEye,camera()->eye().z()*scaleEye));
333 
334  camera()->setCenter(QVector3D(dx,dy,dz));
335 }
336 
337 
338 //*************************************************************************************************************
339 
340 void GeometryView::mousePressEvent(QMouseEvent *e)
341 {
342 
343  if(e->buttons() & Qt::RightButton)
344  {
345  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
346  camera()->setCenter(QVector3D(0,0,m_fOffsetZ));
347  camera()->setEye(QVector3D(0,0,normEye));
348  }
349 
350  QGLView::mousePressEvent(e);
351 }
352 
void keyPressEvent(QKeyEvent *e)
Source Space descritpion.
void mousePressEvent(QMouseEvent *e)
void paintGL(QGLPainter *painter)
void mouseMoveEvent(QMouseEvent *e)
void initializeGL(QGLPainter *painter)
GeometryView(const MNESourceSpace &p_sourceSpace, QWindow *parent=0)
Declaration of the GeometryView Class.
QSharedPointer< Annotation > SPtr
Definition: annotation.h:101