MNE-CPP  beta 1.0
labelview.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "labelview.h"
42 
43 #include <fs/label.h>
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // QT INCLUDES
49 //=============================================================================================================
50 
51 #include "qglbuilder.h"
52 #include "qglcube.h"
53 
54 #include <QtCore/qurl.h>
55 #include <QArray>
56 #include <QTimer>
57 
58 
59 //*************************************************************************************************************
60 //=============================================================================================================
61 // STL INCLUDES
62 //=============================================================================================================
63 
64 #include <iostream>
65 
66 
67 //*************************************************************************************************************
68 //=============================================================================================================
69 // USED NAMESPACES
70 //=============================================================================================================
71 
72 using namespace DISP3DLIB;
73 
74 
75 //*************************************************************************************************************
76 //=============================================================================================================
77 // DEFINE MEMBER METHODS
78 //=============================================================================================================
79 
80 LabelView::LabelView(SurfaceSet &p_surfSet, QList<Label> &p_qListLabels, QList<RowVector4i> &p_qListRGBAs, QWindow *parent)
81 : QGLView(parent)
82 , m_surfSet(p_surfSet)
83 , m_qListLabels(p_qListLabels)
84 , m_qListRGBAs(p_qListRGBAs)
85 , m_bStereo(true)
86 , m_pSceneNodeBrain(0)
87 , m_pSceneNode(0)
88 , m_nTSteps(0)
89 {
90  m_pCameraFrontal = new QGLCamera(this);
91  m_pCameraFrontal->setAdjustForAspectRatio(false);
92 
93 
94  m_timer = new QTimer(this);
95  QObject::connect(m_timer, &QTimer::timeout, this, &LabelView::updateData);
96 }
97 
98 
99 //*************************************************************************************************************
100 
102 {
103  delete m_pSceneNode;
104 }
105 
106 
107 //*************************************************************************************************************
108 
109 void LabelView::pushSourceEstimate(MNESourceEstimate &p_sourceEstimate)
110 {
111  m_timer->stop();
112  m_curSourceEstimate = p_sourceEstimate;
113 
114  m_nTSteps = m_curSourceEstimate.times.size();
115 
116  qDebug() << "#########" << m_curSourceEstimate.data.rows() << m_curSourceEstimate.data.cols() << m_nTSteps;
117 
118  m_vecFirstLabelSourceEstimate = m_curSourceEstimate.data.block(0,0,3,m_curSourceEstimate.data.cols()).colwise().sum();
119 
120  m_dMaxSourceEstimate = m_vecFirstLabelSourceEstimate.maxCoeff();
121 
122 
123  m_timer->start(m_curSourceEstimate.tstep*1000);
124 }
125 
126 
127 //*************************************************************************************************************
128 
129 void LabelView::initializeGL(QGLPainter *painter)
130 {
131  // in the constructor construct a builder on the stack
132  QGLBuilder builder;
133 
134  float fac = 10.0f;
135 
136  builder << QGL::Faceted;
137  m_pSceneNodeBrain = builder.currentNode();
138 
139  builder.pushNode();
140 
141  // Collor palette
142  qint32 index;
143  QSharedPointer<QGLMaterialCollection> palette = builder.sceneNode()->palette(); // register color palette within the root node
144 
145  //
146  // Build each hemisphere in its separate node
147  //
148  for(qint32 h = 0; h < 2; ++h)
149  {
150  builder.newNode();//create new hemisphere node
151  {
152  MatrixX3i tris;
153  MatrixX3f rr = m_surfSet[h].rr();
154 
155  builder.pushNode();
156  //
157  // Create each ROI in its own node
158  //
159  for(qint32 k = 0; k < m_qListLabels.size(); ++k)
160  {
161  //check if label hemi fits current hemi
162  if(m_qListLabels[k].hemi != h)
163  continue;
164 
165  //Ggenerate label tri information
166  tris = m_qListLabels[k].selectTris(m_surfSet[h]);
167 
168  // add new ROI node when current ROI node is not empty
169  if(builder.currentNode()->count() > 0)
170  builder.newNode();
171 
172 
173  QGeometryData t_GeometryDataTri;
174 
175 
176  MatrixXf t_TriCoords(3,3*tris.rows());
177 
178  for(qint32 i = 0; i < tris.rows(); ++i)
179  {
180  t_TriCoords.col(i*3) = rr.row( tris(i,0) ).transpose();
181  t_TriCoords.col(i*3+1) = rr.row( tris(i,1) ).transpose();
182  t_TriCoords.col(i*3+2) = rr.row( tris(i,2) ).transpose();
183  }
184 
185  t_TriCoords *= fac;
186  t_GeometryDataTri.appendVertexArray(QArray<QVector3D>::fromRawData( reinterpret_cast<const QVector3D*>(t_TriCoords.data()), t_TriCoords.cols() ));
187 
188  //
189  // If triangles are available.
190  //
191  if (t_GeometryDataTri.count() > 0)
192  {
193 
194  //
195  // Add triangles to current node
196  //
197  builder.addTriangles(t_GeometryDataTri);
198 
199  //
200  // Colorize ROI
201  //
202  QGLMaterial *t_pMaterialROI = new QGLMaterial();
203  int r, g, b;
204  r = m_qListRGBAs[k][0];
205  g = m_qListRGBAs[k][1];
206  b = m_qListRGBAs[k][2];
207 
208  t_pMaterialROI->setColor(QColor(r,g,b,200));
209 // t_pMaterialROI->setEmittedLight(QColor(100,100,100,255));
210 // t_pMaterialROI->setSpecularColor(QColor(10,10,10,20));
211 
212 
213  index = palette->addMaterial(t_pMaterialROI);
214  builder.currentNode()->setMaterialIndex(index);
215  }
216  }
217  }
218  // Go one level up
219  builder.popNode();
220  }
221  // Go one level up
222  builder.popNode();
223 
224  // Optimze current scene for display and calculate lightning normals
225  m_pSceneNode = builder.finalizedSceneNode();
226  m_pSceneNode->setParent(this);
227 
228  //
229  // Create light models
230  //
231  m_pLightModel = new QGLLightModel(this);
232  m_pLightModel->setAmbientSceneColor(Qt::white);
233  m_pLightModel->setViewerPosition(QGLLightModel::LocalViewer);
234 
235  m_pLightModel = new QGLLightModel(this);
236 
237  m_pLightParametersScene = new QGLLightParameters(this);
238  m_pLightParametersScene->setPosition(QVector3D(0.0f, 0.0f, 3.0f));
239  painter->setMainLight(m_pLightParametersScene);
240 
241 
242 
243  simCount = 0;
244 
245  //
246  // Set stereo type
247  //
248  if (m_bStereo) {
249  this->setStereoType(QGLView::RedCyanAnaglyph);
250  camera()->setEyeSeparation(0.4f);
251  m_pCameraFrontal->setEyeSeparation(0.1f);
252  }
253 
254 }
255 
256 
257 //*************************************************************************************************************
258 
259 void LabelView::paintGL(QGLPainter *painter)
260 {
261  glEnable(GL_BLEND); // enable transparency
262 
263  // painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
264 
265 
266 
267  painter->modelViewMatrix().push();
268  painter->projectionMatrix().push();
269 
270  painter->setStandardEffect(QGL::LitMaterial);
271 // painter->setCamera(m_pCameraFrontal);
272  painter->setLightModel(m_pLightModel);
273 
274 // material.bind(painter);
275 // material.prepareToDraw(painter, painter->attributes());
276 
277 
278 // qint32 iVal = (simCount%m_nTSteps)*(255.0/m_nTSteps);
279 
280  if(m_nTSteps > 0)
281  {
282  qint32 iVal = (m_vecFirstLabelSourceEstimate[simCount%m_nTSteps]/m_dMaxSourceEstimate) * 255;
283  m_pSceneNode->palette()->material(15)->setSpecularColor(QColor(iVal,iVal,iVal,1));
284  }
285 
286  m_pSceneNode->draw(painter);
287 
288 
289  painter->modelViewMatrix().pop();
290  painter->projectionMatrix().pop();
291 }
292 
293 
294 //*************************************************************************************************************
295 
296 //QGLSceneNode *LabelView::createScene()
297 //{
298 // QGLBuilder builder;
299 // QGLSceneNode *root = builder.sceneNode();
300 
301 // //completed building, so finalise
302 // return builder.finalizedSceneNode();
303 //}
304 
305 
306 //*************************************************************************************************************
307 
308 void LabelView::updateData()
309 {
310 // qDebug() << simCount%m_nTSteps;
311 
312  ++simCount;
313 
314  this->update();
315 }
LabelView class declaration.
LabelView(SurfaceSet &p_surfSet, QList< Label > &p_qListLabels, QList< RowVector4i > &p_qListRGBAs, QWindow *parent=0)
Definition: labelview.cpp:80
void paintGL(QGLPainter *painter)
Definition: labelview.cpp:259
void initializeGL(QGLPainter *painter)
Definition: labelview.cpp:129
Label class declaration.
A hemisphere set of surfaces.
Definition: surfaceset.h:83