MNE-CPP  0.1.9
A Framework for Electrophysiology
label.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "label.h"
42 #include "surface.h"
43 
44 //=============================================================================================================
45 // QT INCLUDES
46 //=============================================================================================================
47 
48 #include <QFile>
49 #include <QTextStream>
50 #include <QStringList>
51 #include <QSet>
52 //#include <QDebug>
53 
54 #include <iostream>
55 #include <vector>
56 
57 //=============================================================================================================
58 // USED NAMESPACES
59 //=============================================================================================================
60 
61 using namespace FSLIB;
62 using namespace Eigen;
63 
64 //=============================================================================================================
65 // DEFINE MEMBER METHODS
66 //=============================================================================================================
67 
69 : hemi(-1)
70 , label_id(-1)
71 {
72 }
73 
74 //=============================================================================================================
75 
76 Label::Label(const VectorXi &p_vertices,
77  const MatrixX3f &p_pos,
78  const VectorXd &p_values,
79  qint32 p_hemi,
80  const QString &p_name,
81  qint32 p_id)
82 : vertices(p_vertices)
83 , pos(p_pos)
84 , values(p_values)
85 , hemi(p_hemi)
86 , name(p_name)
87 , label_id(p_id)
88 {
89 }
90 
91 //=============================================================================================================
92 
94 {
95 }
96 
97 //=============================================================================================================
98 
100 {
101  comment = QString("");
102  hemi = -1;
103  name = QString("");
104  vertices = VectorXi();
105  pos = MatrixX3f(0,3);
106  values = VectorXd();
107 
108  label_id = -1;
109 }
110 
111 //=============================================================================================================
112 
113 MatrixX3i Label::selectTris(const Surface & p_Surface)
114 {
115 // //check whether there are data to create the tris
116 // if(this->vertices.size() == 0)
117 // return MatrixX3i(0,3);
118 
119 // MatrixX3i tris(p_Surface.tris().rows(),3);
120 
121 // QSet<int> verts;
122 // verts.reserve(this->vertices.size());
123 // for(qint32 i = 0; i < this->vertices.size(); ++i)
124 // verts.insert(this->vertices[i]);
125 
126 // //
127 // // Search for all the tris where is at least one corner part of the label
128 // //
129 // qint32 t_size = 0;
130 // for(qint32 i = 0; i < p_Surface.tris().rows(); ++i)
131 // {
132 // if(verts.contains(p_Surface.tris()(i,0)) || verts.contains(p_Surface.tris()(i,1)) || verts.contains(p_Surface.tris()(i,2)))
133 // {
134 // tris.row(t_size) = p_Surface.tris().row(i);
135 // ++t_size;
136 // }
137 // }
138 
139 // tris.conservativeResize(t_size, 3);
140 
141  return this->selectTris(p_Surface.tris());//tris;
142 }
143 
144 //=============================================================================================================
145 
146 MatrixX3i Label::selectTris(const MatrixX3i &p_matTris)
147 {
148  //check whether there are data to create the tris
149  if(this->vertices.size() == 0)
150  return MatrixX3i(0,3);
151 
152  MatrixX3i tris(p_matTris.rows(),3);
153 
154  QSet<int> verts;
155  verts.reserve(this->vertices.size());
156  for(qint32 i = 0; i < this->vertices.size(); ++i)
157  verts.insert(this->vertices[i]);
158 
159  //
160  // Search for all the tris where is at least one corner part of the label
161  //
162  qint32 t_size = 0;
163  for(qint32 i = 0; i < p_matTris.rows(); ++i)
164  {
165  if(verts.contains(p_matTris(i,0)) || verts.contains(p_matTris(i,1)) || verts.contains(p_matTris(i,2)))
166  {
167  tris.row(t_size) = p_matTris.row(i);
168  ++t_size;
169  }
170  }
171 
172  tris.conservativeResize(t_size, 3);
173 
174  return tris;
175 }
176 
177 //=============================================================================================================
178 
179 bool Label::read(const QString& p_sFileName, Label &p_Label)
180 {
181  p_Label.clear();
182 
183  if(p_sFileName.mid(p_sFileName.size()-6,6).compare(".label") != 0)
184  {
185  qWarning("Given file (%s) is not a .label file!\n", p_sFileName.toUtf8().constData());
186  return false;
187  }
188 
189  printf("Reading label...");
190  QFile t_File(p_sFileName);
191 
192  if (!t_File.open(QIODevice::ReadOnly | QIODevice::Text))
193  {
194  qWarning("\tError: Couldn't open the label file\n");
195  return false;
196  }
197 
198  QTextStream t_TextStream(&t_File);
199 
200  QString comment = t_TextStream.readLine();
201  qint32 nv = t_TextStream.readLine().toInt();
202 
203  MatrixXd data(nv, 5);
204 
205  QStringList list;
206  qint32 count;
207  bool isNumber;
208  double value;
209  for(qint32 i = 0; i < nv; ++i)
210  {
211  count = 0;
212  list = t_TextStream.readLine().split(QRegExp("\\s+"), QString::SkipEmptyParts);
213 
214  for(qint32 j = 0; j < list.size(); ++j)
215  {
216  value = list[j].toDouble(&isNumber);
217  if(isNumber)
218  {
219  data(i, count) = value;
220  ++count;
221  }
222  }
223  }
224 
225  p_Label.comment = comment.mid(1,comment.size()-1);
226  if(t_File.fileName().contains("lh."))
227  p_Label.hemi = 0;
228  else
229  p_Label.hemi = 1;
230 
231  //This structure is not need since we don't mix both hemis
232 // p_Label.vertices.insert(p_Label.hemi, data.cast<int>().block(0,0,data.rows(),1));
233 // p_Label.pos.insert(p_Label.hemi, data.cast<float>().block(0,1,data.rows(),3).array() * 1e-3);
234 // p_Label.values.insert(p_Label.hemi, data.block(0,4,data.rows(),1));
235  p_Label.vertices = data.cast<int>().block(0,0,data.rows(),1);
236  p_Label.pos = data.cast<float>().block(0,1,data.rows(),3).array() * 1e-3f;
237  p_Label.values = data.block(0,4,data.rows(),1);
238 
239  if(t_File.fileName().contains("lh.label"))
240  {
241  QStringList tmpList = t_File.fileName().split("lh.")[0].split(QRegExp("\\W+"));
242  p_Label.name = tmpList[tmpList.size()-1];
243  }
244  else if(t_File.fileName().contains("lh."))
245  p_Label.name = t_File.fileName().split("lh.")[1].split(QRegExp("\\W+"))[0];
246 
247  printf("[done]\n");
248 
249  t_File.close();
250 
251  return true;
252 }
FSLIB::Label::label_id
qint32 label_id
Definition: label.h:172
FSLIB::Label::read
static bool read(const QString &p_sFileName, Label &p_Label)
Definition: label.cpp:179
FSLIB::Label::pos
Eigen::MatrixX3f pos
Definition: label.h:167
label.h
Label class declaration.
FSLIB::Label::hemi
qint32 hemi
Definition: label.h:169
FSLIB::Label::clear
void clear()
Definition: label.cpp:99
FSLIB::Label
Freesurfer/MNE label.
Definition: label.h:80
surface.h
Surface class declaration.
FSLIB::Label::selectTris
Eigen::MatrixX3i selectTris(const Surface &p_Surface)
Definition: label.cpp:113
FSLIB::Label::name
QString name
Definition: label.h:171
FSLIB::Label::vertices
Eigen::VectorXi vertices
Definition: label.h:166
FSLIB::Label::values
Eigen::VectorXd values
Definition: label.h:168
FSLIB::Label::Label
Label()
Definition: label.cpp:68
FSLIB::Label::~Label
~Label()
Definition: label.cpp:93
FSLIB::Label::comment
QString comment
Definition: label.h:165
FSLIB::Surface::tris
const Eigen::MatrixX3i & tris() const
Definition: surface.h:329
FSLIB::Surface
FreeSurfer surface mesh.
Definition: surface.h:75