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