54 #include <QDataStream>
64 using namespace FSLIB;
81 : m_sFileName(p_sFileName)
101 m_sFileName = QString(
"");
102 m_Vertices = VectorXi::Zero(0);
103 m_LabelIds = VectorXi::Zero(0);
104 m_Colortable.
clear();
112 if(hemi != 0 && hemi != 1)
115 QString p_sFile = QString(
"%1/%2/label/%3.%4.annot").arg(subjects_dir).arg(subject_id).arg(hemi == 0 ?
"lh" :
"rh").arg(atlas);
117 return read(p_sFile, p_Annotation);
125 if(hemi != 0 && hemi != 1)
128 QString p_sFile = QString(
"%1/%2.%3.annot").arg(path).arg(hemi == 0 ?
"lh" :
"rh").arg(atlas);
130 return read(p_sFile, p_Annotation);
138 p_Annotation.
clear();
140 printf(
"Reading annotation...\n");
141 QFile t_File(p_sFileName);
143 if (!t_File.open(QIODevice::ReadOnly))
145 printf(
"\tError: Couldn't open the file\n");
149 QDataStream t_Stream(&t_File);
150 t_Stream.setByteOrder(QDataStream::BigEndian);
155 p_Annotation.m_Vertices = VectorXi(numEl);
156 p_Annotation.m_LabelIds = VectorXi(numEl);
158 for(qint32 i = 0; i < numEl; ++i)
160 t_Stream >> p_Annotation.m_Vertices[i];
161 t_Stream >> p_Annotation.m_LabelIds[i];
164 qint32 hasColortable;
165 t_Stream >> hasColortable;
168 p_Annotation.m_Colortable.
clear();
172 t_Stream >> numEntries;
177 printf(
"\tReading from Original Version\n");
178 p_Annotation.m_Colortable.
numEntries = numEntries;
182 t_Stream.readRawData(tmp.data(),len);
183 p_Annotation.m_Colortable.
orig_tab = tmp;
185 for(qint32 i = 0; i < numEntries; ++i)
188 p_Annotation.m_Colortable.
table = MatrixXi(numEntries,5);
190 for(qint32 i = 0; i < numEntries; ++i)
194 t_Stream.readRawData(tmp.data(),len);
198 for(qint32 j = 0; j < 4; ++j)
199 t_Stream >> p_Annotation.m_Colortable.
table(i,j);
201 p_Annotation.m_Colortable.
table(i,4) = p_Annotation.m_Colortable.
table(i,0)
202 + p_Annotation.m_Colortable.
table(i,1) * 256
203 + p_Annotation.m_Colortable.
table(i,2) * 65536
204 + p_Annotation.m_Colortable.
table(i,3) * 16777216;
209 qint32 version = -numEntries;
211 printf(
"\tError! Does not handle version %d\n", version);
213 printf(
"\tReading from version %d\n", version);
215 t_Stream >> numEntries;
216 p_Annotation.m_Colortable.
numEntries = numEntries;
221 t_Stream.readRawData(tmp.data(),len);
222 p_Annotation.m_Colortable.
orig_tab = tmp;
224 for(qint32 i = 0; i < numEntries; ++i)
227 p_Annotation.m_Colortable.
table = MatrixXi(numEntries,5);
229 qint32 numEntriesToRead;
230 t_Stream >> numEntriesToRead;
233 for(qint32 i = 0; i < numEntriesToRead; ++i)
236 t_Stream >> structure;
238 printf(
"\tError! Read entry, index %d\n", structure);
240 if(!p_Annotation.m_Colortable.
struct_names[structure].isEmpty())
241 printf(
"Error! Duplicate Structure %d", structure);
245 t_Stream.readRawData(tmp.data(),len);
249 for(qint32 j = 0; j < 4; ++j)
250 t_Stream >> p_Annotation.m_Colortable.
table(structure,j);
252 p_Annotation.m_Colortable.
table(structure,4) = p_Annotation.m_Colortable.
table(structure,0)
253 + p_Annotation.m_Colortable.
table(structure,1) * 256
254 + p_Annotation.m_Colortable.
table(structure,2) * 65536
255 + p_Annotation.m_Colortable.
table(structure,3) * 16777216;
258 printf(
"\tcolortable with %d entries read\n\t(originally %s)\n", p_Annotation.m_Colortable.
numEntries, p_Annotation.m_Colortable.
orig_tab.toUtf8().constData());
262 printf(
"\tError! No colortable stored\n");
266 if(t_File.fileName().contains(
"lh."))
267 p_Annotation.m_iHemi = 0;
269 p_Annotation.m_iHemi = 1;
283 if(this->m_iHemi != p_surf.
hemi())
285 qWarning(
"Annotation and surface hemisphere (annot = %d; surf = %d) do not match!\n", this->m_iHemi, p_surf.
hemi());
289 if(m_LabelIds.size() == 0)
291 qWarning(
"Annotation doesn't' contain data!\n");
295 printf(
"Converting labels from annotation...");
302 QStringList label_names = m_Colortable.
getNames();
303 MatrixX4i label_rgbas = m_Colortable.
getRGBAs();
306 MatrixX3f vert_pos = p_surf.
rr();
312 qint32 label_id, count;
313 RowVector4i label_rgba;
318 for(qint32 i = 0; i < label_rgbas.rows(); ++i)
320 label_id = label_ids[i];
321 label_rgba = label_rgbas.row(i);
323 vertices.resize(m_LabelIds.size());
325 for(qint32 j = 0; j < m_LabelIds.size(); ++j)
327 if(m_LabelIds[j] == label_id)
336 vertices.conservativeResize(count);
338 pos.resize(count, 3);
339 for(qint32 j = 0; j < count; ++j)
340 pos.row(j) = vert_pos.row(vertices[j]);
342 values = VectorXd::Zero(count);
343 name = QString(
"%1-%2").arg(label_names[i]).arg(this->m_iHemi == 0 ?
"lh" :
"rh");
347 p_qListLabels.append(
Label(vertices, pos, values, this->m_iHemi, name, label_id));
350 p_qListLabelRGBAs.append(label_rgba);
VectorXi getLabelIds() const
Annotation class declaration.
bool toLabels(const Surface &p_surf, QList< Label > &p_qListLabels, QList< RowVector4i > &p_qListLabelRGBAs) const
MatrixX4i getRGBAs() const
Surface class declaration.
const MatrixX3f & rr() const
static bool read(const QString &subject_id, qint32 hemi, const QString &atlas, const QString &subjects_dir, Annotation &p_Annotation)
QStringList getNames() const