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 if (verts.contains(p_matTris(i, 0)) || verts.contains(p_matTris(i, 1)) || verts.contains(p_matTris(i, 2))) {
142 tris.row(t_size) = p_matTris.row(i);
143 ++t_size;
144 }
145 }
146
147 tris.conservativeResize(t_size, 3);
148
149 return tris;
150}
151
152//=============================================================================================================
153
154bool FsLabel::read(const QString& p_sFileName, FsLabel& p_Label)
155{
156 p_Label.clear();
157
158 if (p_sFileName.mid(p_sFileName.size() - 6, 6).compare(".label") != 0) {
159 qWarning("Given file (%s) is not a .label file!\n", p_sFileName.toUtf8().constData());
160 return false;
161 }
162
163 qInfo("Reading label...");
164 QFile t_File(p_sFileName);
165
166 if (!t_File.open(QIODevice::ReadOnly | QIODevice::Text)) {
167 qWarning("\tError: Couldn't open the label file\n");
168 return false;
169 }
170
171 QTextStream t_TextStream(&t_File);
172
173 QString comment = t_TextStream.readLine();
174 qint32 nv = t_TextStream.readLine().toInt();
175
176 MatrixXd data(nv, 5);
177
178 QStringList list;
179 qint32 count;
180 bool isNumber;
181 double value;
182 for (qint32 i = 0; i < nv; ++i) {
183 count = 0;
184
185#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
186 auto skip = QString::SkipEmptyParts;
187#else
188 auto skip = Qt::SkipEmptyParts;
189#endif
190
191 list = t_TextStream.readLine().split(QRegularExpression("\\s+"), skip);
192
193 for (qint32 j = 0; j < list.size(); ++j) {
194 value = list[j].toDouble(&isNumber);
195 if (isNumber) {
196 data(i, count) = value;
197 ++count;
198 }
199 }
200 }
201
202 p_Label.comment = comment.mid(1, comment.size() - 1);
203 if (t_File.fileName().contains("lh."))
204 p_Label.hemi = 0;
205 else
206 p_Label.hemi = 1;
207
208 //This structure is not need since we don't mix both hemis
209 // p_Label.vertices.insert(p_Label.hemi, data.cast<int>().block(0,0,data.rows(),1));
210 // p_Label.pos.insert(p_Label.hemi, data.cast<float>().block(0,1,data.rows(),3).array() * 1e-3);
211 // p_Label.values.insert(p_Label.hemi, data.block(0,4,data.rows(),1));
212 p_Label.vertices = data.cast<int>().block(0, 0, data.rows(), 1);
213 p_Label.pos = data.cast<float>().block(0, 1, data.rows(), 3).array() * 1e-3f;
214 p_Label.values = data.block(0, 4, data.rows(), 1);
215
216 if (t_File.fileName().contains("lh.label")) {
217 QStringList tmpList = t_File.fileName().split("lh.")[0].split(QRegularExpression("\\W+"));
218 p_Label.name = tmpList[tmpList.size() - 1];
219 } else if (t_File.fileName().contains("lh."))
220 p_Label.name = t_File.fileName().split("lh.")[1].split(QRegularExpression("\\W+"))[0];
221
222 qInfo("[done]\n");
223
224 t_File.close();
225
226 return true;
227}
Reader and in-memory representation of a FreeSurfer/MNE surface label (.label).
Reader and in-memory representation of a single FreeSurfer triangular surface (e.g....
FreeSurfer surface, annotation and parcellation I/O for mne-cpp.
QString comment
Definition fs_label.h:165
Eigen::VectorXd values
Definition fs_label.h:168
QString name
Definition fs_label.h:171
static bool read(const QString &p_sFileName, FsLabel &p_Label)
Definition fs_label.cpp:154
Eigen::MatrixX3i selectTris(const FsSurface &p_Surface)
Definition fs_label.cpp:90
qint32 label_id
Definition fs_label.h:172
Eigen::MatrixX3f pos
Definition fs_label.h:167
Eigen::VectorXi vertices
Definition fs_label.h:166
In-memory FreeSurfer triangular cortical surface for one hemisphere.
Definition fs_surface.h:94
const Eigen::MatrixX3i & tris() const
Definition fs_surface.h:385