MNE-CPP  beta 1.0
brainsurface.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "brainsurface.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // QT INCLUDES
48 //=============================================================================================================
49 
50 
51 //*************************************************************************************************************
52 //=============================================================================================================
53 // USED NAMESPACES
54 //=============================================================================================================
55 
56 using namespace DISP3DNEWLIB;
57 
58 
59 //*************************************************************************************************************
60 //=============================================================================================================
61 // DEFINE MEMBER METHODS
62 //=============================================================================================================
63 
65 : RenderableEntity(parent)
66 , m_pLeftHemisphere(NULL)
67 , m_pRightHemisphere(NULL)
68 , m_ColorSulci(QColor(50.0, 50.0, 50.0, 255.0))
69 , m_ColorGyri(QColor(200.0, 200.0, 200.0, 255.0))
70 {
71  init();
72 }
73 
74 
75 //*************************************************************************************************************
76 
77 BrainSurface::BrainSurface(const QString &subject_id, qint32 hemi, const QString &surf, const QString &subjects_dir, QEntity *parent)
78 : RenderableEntity(parent)
79 , m_SurfaceSet(subject_id, hemi, surf, subjects_dir)
80 , m_pLeftHemisphere(NULL)
81 , m_pRightHemisphere(NULL)
82 , m_ColorSulci(QColor(50.0, 50.0, 50.0, 255.0))
83 , m_ColorGyri(QColor(100.0, 100.0, 100.0, 255.0))
84 {
85  init();
86 }
87 
88 
89 //*************************************************************************************************************
90 
91 BrainSurface::BrainSurface(const QString &subject_id, qint32 hemi, const QString &surf, const QString &atlas, const QString &subjects_dir, QEntity *parent)
92 : RenderableEntity(parent)
93 , m_SurfaceSet(subject_id, hemi, surf, subjects_dir)
94 , m_AnnotationSet(subject_id, hemi, atlas, subjects_dir)
95 , m_pLeftHemisphere(NULL)
96 , m_pRightHemisphere(NULL)
97 , m_ColorSulci(QColor(50.0, 50.0, 50.0, 255.0))
98 , m_ColorGyri(QColor(200.0, 200.0, 200.0, 255.0))
99 {
100  init();
101 }
102 
103 
104 //*************************************************************************************************************
105 
106 BrainSurface::BrainSurface(const QString& p_sFile, QEntity *parent)
107 : RenderableEntity(parent)
108 , m_pLeftHemisphere(NULL)
109 , m_pRightHemisphere(NULL)
110 , m_ColorSulci(QColor(50.0, 50.0, 50.0, 255.0))
111 , m_ColorGyri(QColor(200.0, 200.0, 200.0, 255.0))
112 {
113  Surface t_Surf(p_sFile);
114  m_SurfaceSet.insert(t_Surf);
115 
116  init();
117 }
118 
119 
120 //*************************************************************************************************************
121 
123 {
124 
125 }
126 
127 
128 //*************************************************************************************************************
129 
131 {
132  //std::cout<<"START - BrainSurface::updateActivation()"<<std::endl;
133 
134  // LEFT HEMISPHERE
135  //Find brain mesh as component
136 // int indexSurfaceMeshLeftHemi;
137 // QComponentList componentsListLeftHemi = m_pLeftHemisphere->components();
138 // for(int i = 0; i<componentsListLeftHemi.size(); i++)
139 // if(componentsListLeftHemi.at(i)->objectName() == "m_pSurfaceMesh")
140 // indexSurfaceMeshLeftHemi = i;
141 
142 // //Remove mesh component
143 // m_pLeftHemisphere->removeComponent(componentsListLeftHemi.at(indexSurfaceMeshLeftHemi));
144 
145 // //Create new mesh component
146 // if(m_pLeftHemisphere)
147 // delete m_pLeftHemisphere;
148 
149 // m_pLeftHemisphere = new BrainHemisphere(m_SurfaceSet[0], m_qmVertexActivationColorLH, this);
150 
151 // BrainSurfaceMesh *meshLeftHemi = (BrainSurfaceMesh*)componentsListLeftHemi.at(indexSurfaceMeshLeftHemi);
152 
153  if(m_pLeftHemisphere)
154  m_pLeftHemisphere->updateActivation(m_qmVertexActivationColorLH);
155 
156  // RIGHT HEMISPHERE
157  //Find brain mesh as component
158 // int indexSurfaceMeshRightHemi;
159 // QComponentList componentsListRightHemi = m_pRightHemisphere->components();
160 // for(int i = 0; i<componentsListRightHemi.size(); i++)
161 // if(componentsListRightHemi.at(i)->objectName() == "m_pSurfaceMesh")
162 // indexSurfaceMeshRightHemi = i;
163 
164 // //Remove mesh component
165 // m_pRightHemisphere->removeComponent(componentsListRightHemi.at(indexSurfaceMeshRightHemi));
166 
167 // //Create new mesh component
168 // if(m_pRightHemisphere)
169 // delete m_pRightHemisphere;
170 
171 // m_pRightHemisphere = new BrainHemisphere(m_SurfaceSet[1], m_qmVertexActivationColorRH, this);
172 
173 // BrainSurfaceMesh *meshRightHemi = (BrainSurfaceMesh*)componentsListRightHemi.at(indexSurfaceMeshRightHemi);
174 
175  if(m_pRightHemisphere)
176  m_pRightHemisphere->updateActivation(m_qmVertexActivationColorRH);
177 
178  //std::cout<<"END - BrainSurface::updateActivation()"<<std::endl;
179 }
180 
181 
182 //*************************************************************************************************************
183 
184 void BrainSurface::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
185 {
186  //std::cout<<"START - BrainSurface::dataChanged()"<<std::endl;
187 
188  //std::cout<<"BrainSurface::dataChanged() - topLeft.column(): "<<topLeft.column()<<std::endl;
189  //std::cout<<"BrainSurface::dataChanged() - bottomRight.column(): "<<bottomRight.column()<<std::endl;
190 
191  int stcDataRoleLH = StcDataModelRoles::GetSmoothedStcValLH;
192  int stcDataRoleRH = StcDataModelRoles::GetSmoothedStcValRH;
193 
194  //check wether realtive stc data column (3) has changed
195  if(topLeft.column() > 3 || bottomRight.column() < 3) {
196  std::cout<<"BrainSurface::dataChanged() - stc data did not change"<<std::endl;
197  return;
198  }
199 
200  //Get LH activation and transform to index/color map
201  VectorXd currentActivationLH = m_pStcDataModel->data(0,4,stcDataRoleLH).value<VectorXd>();
202 
203  m_qmVertexActivationColorLH = m_qmDefaultVertexColorLH;
204 
205  std::cout<<"BrainSurface::dataChanged() - currentActivationLH.rows(): "<<currentActivationLH.rows()<<std::endl;
206 
207  for(qint32 i = 0; i < currentActivationLH.rows(); ++i) {
208  qint32 iVal = currentActivationLH(i) * 20;
209 
210  iVal = iVal > 255 ? 255 : iVal < 0 ? 0 : iVal;
211 
212  //std::cout<<(int)iVal<<std::endl;
213 
214  QRgb qRgb;
215 // qRgb = ColorMap::valueToHotNegative1((float)iVal/255.0);
216  qRgb = ColorMap::valueToHotNegative2((float)iVal/255.0);
217 // qRgb = ColorMap::valueToHot((float)iVal/255.0);
218 
219  int vertexIndex = i;
220  if(stcDataRoleLH == StcDataModelRoles::GetRelStcValLH || stcDataRoleLH == StcDataModelRoles::GetStcValLH)
221  vertexIndex = m_pStcDataModel->data(i,1,StcDataModelRoles::GetIndexLH).toInt();
222 
223 // std::cout<<"BrainSurface::dataChanged() - vertexIndex: "<<vertexIndex<<std::endl;
224 // std::cout<<"BrainSurface::dataChanged() - qRgb: "<<QColor(qRgb).redF()<<" "<<QColor(qRgb).greenF()<<" "<<QColor(qRgb).blueF()<<std::endl;
225 
226  if(iVal>0)
227  m_qmVertexActivationColorLH[vertexIndex] = QColor(qRgb);
228  }
229 
230  //Get RH activation and transform to index/color map
231  VectorXd currentActivationRH = m_pStcDataModel->data(0,4,stcDataRoleRH).value<VectorXd>();
232 
233  m_qmVertexActivationColorRH = m_qmDefaultVertexColorRH;
234 
235  for(qint32 i = 0; i < currentActivationRH.rows(); ++i) {
236  qint32 iVal = currentActivationRH(i) * 20;
237 
238  iVal = iVal > 255 ? 255 : iVal < 0 ? 0 : iVal;
239 
240  QRgb qRgb;
241 // qRgb = ColorMap::valueToHotNegative1((float)iVal/255.0);
242  qRgb = ColorMap::valueToHotNegative2((float)iVal/255.0);
243 // qRgb = ColorMap::valueToHot((float)iVal/255.0);
244 
245  int vertexIndex = i;
246  if(stcDataRoleLH == StcDataModelRoles::GetRelStcValRH || stcDataRoleLH == StcDataModelRoles::GetStcValRH)
247  vertexIndex = m_pStcDataModel->data(i,1,StcDataModelRoles::GetIndexRH).toInt();
248 
249  if(iVal>0)
250  m_qmVertexActivationColorRH[vertexIndex] = QColor(qRgb);
251  }
252 
254 
255  Q_UNUSED(roles);
256 
257  //std::cout<<"END - BrainSurface::dataChanged()"<<std::endl;
258 }
259 
260 
261 //*************************************************************************************************************
262 
264 {
265  m_pStcDataModel = model;
266 
267  //connect stc data model with surface coloring
268  connect(m_pStcDataModel.data(), &StcDataModel::dataChanged, this, &BrainSurface::dataChanged);
269 }
270 
271 
272 //*************************************************************************************************************
273 
275 {
276  //Init sulci gyri colors for LH and RH
277  m_qmDefaultVertexColorLH.clear();
278  for(int i = 0; i<m_SurfaceSet[0].rr().rows() ; i++)
279  if(m_SurfaceSet[0].curv()[i] >= 0)
280  m_qmDefaultVertexColorLH.insertMulti(i, m_ColorSulci); //Sulci
281  else
282  m_qmDefaultVertexColorLH.insertMulti(i, m_ColorGyri); //Gyri
283 
284  m_qmDefaultVertexColorRH.clear();
285  for(int i = 0; i<m_SurfaceSet[1].rr().rows() ; i++)
286  if(m_SurfaceSet[1].curv()[i] >= 0)
287  m_qmDefaultVertexColorRH.insertMulti(i, m_ColorSulci); //Sulci
288  else
289  m_qmDefaultVertexColorRH.insertMulti(i, m_ColorGyri); //Gyri
290 
291  m_qmVertexActivationColorLH = m_qmDefaultVertexColorLH;
292  m_qmVertexActivationColorRH = m_qmDefaultVertexColorRH;
293 
294  //Create hemispheres and add as childs / 0 -> lh, 1 -> rh
295  for(int i = 0; i<m_SurfaceSet.size(); i++) {
296  if(m_SurfaceSet[i].hemi() == 0)
297  m_pLeftHemisphere = new BrainHemisphere(m_SurfaceSet[i], m_qmVertexActivationColorLH, this);
298 
299  if(m_SurfaceSet[i].hemi() == 1)
300  m_pRightHemisphere = new BrainHemisphere(m_SurfaceSet[i], m_qmVertexActivationColorRH, this);
301  }
302 
303  // Calculate bounding box
304  calcBoundingBox();
305 
306  // Brain initial surface Transform
307  int scale = 300;
308  this->scaleTransform()->setScale(scale);
309 
310  //Brain initial rotation
311  this->rotateTransform()->setAxis(QVector3D(1,0,0));
312  this->rotateTransform()->setAngleDeg(-90);
313 }
314 
315 
316 //*************************************************************************************************************
317 
319 {
320  QMap<qint32, Surface>::const_iterator it = m_SurfaceSet.data().constBegin();
321 
322  QVector3D min(it.value().rr().col(0).minCoeff(), it.value().rr().col(1).minCoeff(), it.value().rr().col(2).minCoeff());
323  QVector3D max(it.value().rr().col(0).maxCoeff(), it.value().rr().col(1).maxCoeff(), it.value().rr().col(2).maxCoeff());
324 
325  for (it = m_SurfaceSet.data().begin()+1; it != m_SurfaceSet.data().end(); ++it)
326  {
327  for(qint32 i = 0; i < 3; ++i)
328  {
329  min[i] = min[i] > it.value().rr().col(i).minCoeff() ? it.value().rr().col(i).minCoeff() : min[i];
330  max[i] = max[i] < it.value().rr().col(i).maxCoeff() ? it.value().rr().col(i).maxCoeff() : max[i];
331  }
332  }
333  m_vecBoundingBoxMin = min;
334  m_vecBoundingBoxMax = max;
335 
336  for(qint32 i = 0; i < 3; ++i)
338 }
BrainSurface(QEntity *parent=0)
QSharedPointer< StcDataModel > SPtr
Definition: stcdatamodel.h:131
Declaration of BrainSurface which holds the basic functionality of a freesurfer brain surface...
void setModel(StcDataModel::SPtr model)
qint32 size() const
Definition: surfaceset.h:280
void insert(const Surface &p_Surface)
Definition: surfaceset.cpp:154
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles=QVector< int >())
static QRgb valueToHotNegative2(double v)
Definition: colormap.h:377
QMap< qint32, Surface > & data()
Definition: surfaceset.h:253
FreeSurfer surface mesh.
Definition: surface.h:92
Holds the data of one hemisphere in form of a mesh.