v2.0.0
Loading...
Searching...
No Matches
fs_label.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "fs_label.h"
18#include "fs_surface.h"
19
20//=============================================================================================================
21// QT INCLUDES
22//=============================================================================================================
23
24#include <QFile>
25#include <QTextStream>
26#include <QStringList>
27#include <QSet>
28#include <QRegularExpression>
29//#include <QDebug>
30
31#include <iostream>
32#include <vector>
33
34//=============================================================================================================
35// USED NAMESPACES
36//=============================================================================================================
37
38using namespace FSLIB;
39using namespace Eigen;
40
41//=============================================================================================================
42// DEFINE MEMBER METHODS
43//=============================================================================================================
44
46: hemi(-1)
47, label_id(-1)
48{
49}
50
51//=============================================================================================================
52
53FsLabel::FsLabel(const VectorXi &p_vertices,
54 const MatrixX3f &p_pos,
55 const VectorXd &p_values,
56 qint32 p_hemi,
57 const QString &p_name,
58 qint32 p_id)
59: vertices(p_vertices)
60, pos(p_pos)
61, values(p_values)
62, hemi(p_hemi)
63, name(p_name)
64, label_id(p_id)
65{
66}
67
68//=============================================================================================================
69
73
74//=============================================================================================================
75
77{
78 comment = QString("");
79 hemi = -1;
80 name = QString("");
81 vertices = VectorXi();
82 pos = MatrixX3f(0,3);
83 values = VectorXd();
84
85 label_id = -1;
86}
87
88//=============================================================================================================
89
90MatrixX3i FsLabel::selectTris(const FsSurface & p_Surface)
91{
92// //check whether there are data to create the tris
93// if(this->vertices.size() == 0)
94// return MatrixX3i(0,3);
95
96// MatrixX3i tris(p_Surface.tris().rows(),3);
97
98// QSet<int> verts;
99// verts.reserve(this->vertices.size());
100// for(qint32 i = 0; i < this->vertices.size(); ++i)
101// verts.insert(this->vertices[i]);
102
103// //
104// // Search for all the tris where is at least one corner part of the label
105// //
106// qint32 t_size = 0;
107// for(qint32 i = 0; i < p_Surface.tris().rows(); ++i)
108// {
109// if(verts.contains(p_Surface.tris()(i,0)) || verts.contains(p_Surface.tris()(i,1)) || verts.contains(p_Surface.tris()(i,2)))
110// {
111// tris.row(t_size) = p_Surface.tris().row(i);
112// ++t_size;
113// }
114// }
115
116// tris.conservativeResize(t_size, 3);
117
118 return this->selectTris(p_Surface.tris());//tris;
119}
120
121//=============================================================================================================
122
123MatrixX3i FsLabel::selectTris(const MatrixX3i &p_matTris)
124{
125 //check whether there are data to create the tris
126 if(this->vertices.size() == 0)
127 return MatrixX3i(0,3);
128
129 MatrixX3i tris(p_matTris.rows(),3);
130
131 QSet<int> verts;
132 verts.reserve(this->vertices.size());
133 for(qint32 i = 0; i < this->vertices.size(); ++i)
134 verts.insert(this->vertices[i]);
135
136 //
137 // Search for all the tris where is at least one corner part of the label
138 //
139 qint32 t_size = 0;
140 for(qint32 i = 0; i < p_matTris.rows(); ++i)
141 {
142 if(verts.contains(p_matTris(i,0)) || verts.contains(p_matTris(i,1)) || verts.contains(p_matTris(i,2)))
143 {
144 tris.row(t_size) = p_matTris.row(i);
145 ++t_size;
146 }
147 }
148
149 tris.conservativeResize(t_size, 3);
150
151 return tris;
152}
153
154//=============================================================================================================
155
156bool FsLabel::read(const QString& p_sFileName, FsLabel &p_Label)
157{
158 p_Label.clear();
159
160 if(p_sFileName.mid(p_sFileName.size()-6,6).compare(".label") != 0)
161 {
162 qWarning("Given file (%s) is not a .label file!\n", p_sFileName.toUtf8().constData());
163 return false;
164 }
165
166 qInfo("Reading label...");
167 QFile t_File(p_sFileName);
168
169 if (!t_File.open(QIODevice::ReadOnly | QIODevice::Text))
170 {
171 qWarning("\tError: Couldn't open the label file\n");
172 return false;
173 }
174
175 QTextStream t_TextStream(&t_File);
176
177 QString comment = t_TextStream.readLine();
178 qint32 nv = t_TextStream.readLine().toInt();
179
180 MatrixXd data(nv, 5);
181
182 QStringList list;
183 qint32 count;
184 bool isNumber;
185 double value;
186 for(qint32 i = 0; i < nv; ++i)
187 {
188 count = 0;
189
190#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
191 auto skip = QString::SkipEmptyParts;
192#else
193 auto skip = Qt::SkipEmptyParts;
194#endif
195
196 list = t_TextStream.readLine().split(QRegularExpression("\\s+"), skip);
197
198 for(qint32 j = 0; j < list.size(); ++j)
199 {
200 value = list[j].toDouble(&isNumber);
201 if(isNumber)
202 {
203 data(i, count) = value;
204 ++count;
205 }
206 }
207 }
208
209 p_Label.comment = comment.mid(1,comment.size()-1);
210 if(t_File.fileName().contains("lh."))
211 p_Label.hemi = 0;
212 else
213 p_Label.hemi = 1;
214
215 //This structure is not need since we don't mix both hemis
216// p_Label.vertices.insert(p_Label.hemi, data.cast<int>().block(0,0,data.rows(),1));
217// p_Label.pos.insert(p_Label.hemi, data.cast<float>().block(0,1,data.rows(),3).array() * 1e-3);
218// p_Label.values.insert(p_Label.hemi, data.block(0,4,data.rows(),1));
219 p_Label.vertices = data.cast<int>().block(0,0,data.rows(),1);
220 p_Label.pos = data.cast<float>().block(0,1,data.rows(),3).array() * 1e-3f;
221 p_Label.values = data.block(0,4,data.rows(),1);
222
223 if(t_File.fileName().contains("lh.label"))
224 {
225 QStringList tmpList = t_File.fileName().split("lh.")[0].split(QRegularExpression("\\W+"));
226 p_Label.name = tmpList[tmpList.size()-1];
227 }
228 else if(t_File.fileName().contains("lh."))
229 p_Label.name = t_File.fileName().split("lh.")[1].split(QRegularExpression("\\W+"))[0];
230
231 qInfo("[done]\n");
232
233 t_File.close();
234
235 return true;
236}
Reader and in-memory representation of a single FreeSurfer triangular surface (e.g....
Reader and in-memory representation of a FreeSurfer/MNE surface label (.label).
FreeSurfer surface, annotation and parcellation I/O for mne-cpp.
QString comment
Definition fs_label.h:163
Eigen::VectorXd values
Definition fs_label.h:166
QString name
Definition fs_label.h:169
static bool read(const QString &p_sFileName, FsLabel &p_Label)
Definition fs_label.cpp:156
Eigen::MatrixX3i selectTris(const FsSurface &p_Surface)
Definition fs_label.cpp:90
qint32 label_id
Definition fs_label.h:170
Eigen::MatrixX3f pos
Definition fs_label.h:165
Eigen::VectorXi vertices
Definition fs_label.h:164
In-memory FreeSurfer triangular cortical surface for one hemisphere.
Definition fs_surface.h:92
const Eigen::MatrixX3i & tris() const
Definition fs_surface.h:383