MNE-CPP  beta 1.0
cluststcview.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "cluststcview.h"
43 #include "cluststcmodel.h"
44 
45 #include <disp/colormap.h>
46 
47 
48 //*************************************************************************************************************
49 //=============================================================================================================
50 // Qt INCLUDES
51 //=============================================================================================================
52 
53 #include "qglbuilder.h"
54 #include "qglcube.h"
55 #include <QMouseEvent>
56 
57 
58 //*************************************************************************************************************
59 //=============================================================================================================
60 // USED NAMESPACES
61 //=============================================================================================================
62 
63 using namespace DISPLIB;
64 using namespace DISP3DLIB;
65 
66 
67 //*************************************************************************************************************
68 //=============================================================================================================
69 // DEFINE MEMBER METHODS
70 //=============================================================================================================
71 
72 ClustStcView::ClustStcView(bool showRegions, bool isStereo, QGLView::StereoType stereoType, QWindow *parent)
73 : QGLView(parent)
74 , m_pModel(Q_NULLPTR)
75 , m_bIsInitialized(false)
76 , m_bShowRegions(showRegions)
77 , m_bStereo(isStereo)
78 , m_stereoType(stereoType)//QGLView::StretchedLeftRight)//QGLView::RedCyanAnaglyph
79 , m_pSceneNodeBrain(Q_NULLPTR)
80 , m_pSceneNode(Q_NULLPTR)
81 , m_pLightModel(Q_NULLPTR)
82 , m_pLightParametersScene(Q_NULLPTR)
83 {
84  m_fOffsetZ = -100.0f;
85  m_fOffsetZEye = 60.0f;
86 }
87 
88 
89 //*************************************************************************************************************
90 
91 ClustStcView::~ClustStcView()
92 {
93 
94 }
95 
96 
97 //*************************************************************************************************************
98 
99 void ClustStcView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
100 {
101  if(!m_bIsInitialized)
102  return;
103 
104  //check wether realtive stc data column (3) has changed
105  if(topLeft.column() > 3 || bottomRight.column() < 3)
106  return;
107 
108  for(qint32 i = 0; i < m_pSceneNode->palette()->size(); ++i)
109  {
110  //ToDo label id check -> necessary?
111  //qint32 colorIdx = m_qListMapLabelIdIndex[h][labelId];
112 
113  qint32 iVal = m_pModel->data(i,3).value<VectorXd>().maxCoeff() * 255;
114 
115  iVal = iVal > 255 ? 255 : iVal < 0 ? 0 : iVal;
116 
117  QRgb qRgb;
118  qRgb = ColorMap::valueToHotNegative1((double)iVal/255.0);
119 // qRgb = ColorMap::valueToHotNegative2((double)iVal/255.0);
120 // qRgb = ColorMap::valueToHot((double)iVal/255.0);
121 
122  m_pSceneNode->palette()->material(i)->setSpecularColor(QColor(qRgb));
123  }
124 
125  this->update();
126 
127  Q_UNUSED( roles );
128 
129 }
130 
131 
132 //*************************************************************************************************************
133 
134 void ClustStcView::setModel(ClustStcModel* model)
135 {
136  m_pModel = model;
137  connect(m_pModel, &ClustStcModel::dataChanged, this, &ClustStcView::dataChanged);
138 }
139 
140 
141 //*************************************************************************************************************
142 
143 void ClustStcView::initializeGL(QGLPainter *painter)
144 {
145  if(!m_pModel)
146  return;
147 
148  // in the constructor construct a builder on the stack
149  QGLBuilder builder;
150 
151  float fac = 100.0f; // too small vertices distances cause clipping errors --> 100 is a good value for freesurfer brain measures
152 
153  builder << QGL::Faceted;
154  if(m_pSceneNodeBrain)
155  delete m_pSceneNodeBrain;
156  m_pSceneNodeBrain = builder.currentNode();
157 
158  builder.pushNode();
159 
160  // Collor palette
161  qint32 index;
162  QSharedPointer<QGLMaterialCollection> palette = builder.sceneNode()->palette(); // register color palette within the root node
163 
164  m_qMapLabelIdIndex.clear();
165 
166 // //
167 // // Build each hemisphere in its separate node
168 // //
169 // for(qint32 h = 0; h < 1; ++h)
170 // {
171  builder.newNode();//create new hemisphere node
172  {
173  //
174  // Create each ROI in its own node
175  //
176  for(qint32 k = 0; k < m_pModel->rowCount(); ++k)
177  {
178  // add new ROI node when current ROI node is not empty
179  if(builder.currentNode()->count() > 0)
180  builder.newNode();
181 
182  QGeometryData t_GeometryDataTri;
183 
184  Matrix3Xf t_TriCoords = m_pModel->data(k,6,Qt::DisplayRole).value<Matrix3Xf>();
185 
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 
205  if(m_bShowRegions)
206  t_pMaterialROI->setColor(m_pModel->data(k,5,Qt::DisplayRole).value<QColor>());
207  else
208  t_pMaterialROI->setColor(QColor(100,100,100,230));
209 
210  index = palette->addMaterial(t_pMaterialROI);
211  builder.currentNode()->setMaterialIndex(index);
212 
213  m_qMapLabelIdIndex.insert(m_pModel->data(k,4,Qt::DisplayRole).value<Label>().label_id, index);
214  }
215  }
216  }
217  // Go one level up
218  builder.popNode();
219 // }
220 // // Go one level up
221 // builder.popNode();
222 
223  // Optimze current scene for display and calculate lightning normals
224  if(m_pSceneNode)
225  delete m_pSceneNode;
226  m_pSceneNode = builder.finalizedSceneNode();
227  m_pSceneNode->setParent(this);
228 
229  //
230  // Create light models
231  //
232  if(m_pLightModel)
233  delete m_pLightModel;
234  m_pLightModel = new QGLLightModel(this);
235  m_pLightModel->setAmbientSceneColor(Qt::white);
236  m_pLightModel->setViewerPosition(QGLLightModel::LocalViewer);
237 
238  m_pLightModel = new QGLLightModel(this);
239 
240  if(m_pLightParametersScene)
241  delete m_pLightParametersScene;
242  m_pLightParametersScene = new QGLLightParameters(this);
243  m_pLightParametersScene->setPosition(QVector3D(0.0f, 0.0f, 3.0f));
244  painter->setMainLight(m_pLightParametersScene);
245 
246  //
247  // Set stereo type
248  //
249  if (m_bStereo) {
250  this->setStereoType(m_stereoType);
251 // camera()->setEyeSeparation(0.4f);
252 // m_pCameraFrontal->setEyeSeparation(0.1f);
253 
254  //LNdT DEMO
255  camera()->setCenter(QVector3D(0,0,m_fOffsetZ));//0.8f*fac));
256  camera()->setEyeSeparation(0.4f);
257  camera()->setFieldOfView(30);
258  camera()->setEye(QVector3D(0,0,m_fOffsetZEye));
259  //LNdT DEMO end
260  }
261  else
262  {
263  camera()->setCenter(QVector3D(0,0,m_fOffsetZ));
264  camera()->setFieldOfView(30);
265  camera()->setEye(QVector3D(0,0,m_fOffsetZEye));
266  }
267 
268 // //set background to light grey-blue
269 // glClearColor(0.8f, 0.8f, 1.0f, 0.0f);
270 
271 // //set background to light white
272  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
273 
274  m_bIsInitialized = true;
275 }
276 
277 
278 //*************************************************************************************************************
279 
280 void ClustStcView::paintGL(QGLPainter *painter)
281 {
282  glEnable(GL_BLEND); // enable transparency
283 
284  // painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
285 
286 
287  painter->modelViewMatrix().push();
288  painter->projectionMatrix().push();
289 
290  painter->setStandardEffect(QGL::LitMaterial);
291 // painter->setCamera(m_pCameraFrontal);
292  painter->setLightModel(m_pLightModel);
293 
294 // material.bind(painter);
295 // material.prepareToDraw(painter, painter->attributes());
296 
297  m_pSceneNode->draw(painter);
298 
299 
300  painter->modelViewMatrix().pop();
301  painter->projectionMatrix().pop();
302 }
303 
304 
305 //*************************************************************************************************************
306 
307 void ClustStcView::keyPressEvent(QKeyEvent *e)
308 {
309  camera()->setCenter(QVector3D(0,0,0));
310 
311  float normEyeOld = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
312 
313  QGLView::keyPressEvent(e);
314 
315  float dx = (camera()->eye().x()*m_fOffsetZ)/m_fOffsetZEye;
316  float dy = (camera()->eye().y()*m_fOffsetZ)/m_fOffsetZEye;
317  float dz = (camera()->eye().z()*m_fOffsetZ)/m_fOffsetZEye;
318 
319  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
320  float scaleEye = normEyeOld/normEye;//m_fOffsetZEye/normEye;
321  camera()->setEye(QVector3D(camera()->eye().x()*scaleEye,camera()->eye().y()*scaleEye,camera()->eye().z()*scaleEye));
322 
323  camera()->setCenter(QVector3D(dx,dy,dz));
324 }
325 
326 
327 //*************************************************************************************************************
328 
329 void ClustStcView::mouseMoveEvent(QMouseEvent *e)
330 {
331  camera()->setCenter(QVector3D(0,0,0));
332 
333  float normEyeOld = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
334 
335  QGLView::mouseMoveEvent(e);
336 
337  float dx = (camera()->eye().x()*m_fOffsetZ)/m_fOffsetZEye;
338  float dy = (camera()->eye().y()*m_fOffsetZ)/m_fOffsetZEye;
339  float dz = (camera()->eye().z()*m_fOffsetZ)/m_fOffsetZEye;
340 
341  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
342  float scaleEye = normEyeOld/normEye;//m_fOffsetZEye/normEye;
343  camera()->setEye(QVector3D(camera()->eye().x()*scaleEye,camera()->eye().y()*scaleEye,camera()->eye().z()*scaleEye));
344 
345  camera()->setCenter(QVector3D(dx,dy,dz));
346 }
347 
348 
349 //*************************************************************************************************************
350 
351 void ClustStcView::mousePressEvent(QMouseEvent *e)
352 {
353  if(e->buttons() & Qt::RightButton)
354  {
355  float normEye = sqrt(pow(camera()->eye().x(),2) + pow(camera()->eye().y(),2) + pow(camera()->eye().z(),2));
356  camera()->setCenter(QVector3D(0,0,m_fOffsetZ));
357  camera()->setEye(QVector3D(0,0,normEye));
358  }
359 
360  QGLView::mousePressEvent(e);
361 }
ColorMap class declaration.
void paintGL(QGLPainter *painter)
Table model which prepares source estimate information.
void keyPressEvent(QKeyEvent *e)
void initializeGL(QGLPainter *painter)
QVariant data(int row, int column, int role=Qt::DisplayRole) const
ClustStcModel class declaration.
ClustStcView class declaration.
Freesurfer/MNE label.
Definition: label.h:97
void mousePressEvent(QMouseEvent *e)
void mouseMoveEvent(QMouseEvent *e)