v2.0.0
Loading...
Searching...
No Matches
fiff_dig_point_set.cpp
Go to the documentation of this file.
1//=============================================================================================================
20
21//=============================================================================================================
22// INCLUDES
23//=============================================================================================================
24
25#include "fiff_dig_point_set.h"
26
27//=============================================================================================================
28// INCLUDES
29//=============================================================================================================
30
31#include "fiff_dig_point.h"
32#include "fiff_dir_node.h"
33#include "fiff_tag.h"
34#include "fiff_types.h"
35#include <QDebug>
36#include <QDir>
37#include <QFile>
38#include <QFileInfo>
39
40//=============================================================================================================
41// QT INCLUDES
42//=============================================================================================================
43
44//=============================================================================================================
45// EIGEN INCLUDES
46//=============================================================================================================
47
48#include <stdexcept>
49//=============================================================================================================
50// USED NAMESPACES
51//=============================================================================================================
52
53using namespace FIFFLIB;
54using namespace Eigen;
55
56//=============================================================================================================
57// DEFINE GLOBAL METHODS
58//=============================================================================================================
59
60//=============================================================================================================
61// DEFINE MEMBER METHODS
62//=============================================================================================================
63
65 :m_qListDigPoint()
66{
67}
68
69//=============================================================================================================
70
72: m_qListDigPoint(p_FiffDigPointSet.m_qListDigPoint)
73{
74}
75
76//=============================================================================================================
77
78FiffDigPointSet::FiffDigPointSet(QList<FIFFLIB::FiffDigPoint> pointList)
79: m_qListDigPoint(pointList)
80{
81}
82
83//=============================================================================================================
84
85FiffDigPointSet::FiffDigPointSet(QIODevice &p_IODevice) //const FiffDigPointSet &p_FiffDigPointSet
86{
87 //
88 // Open the file
89 //
90 FiffStream::SPtr t_pStream(new FiffStream(&p_IODevice));
91
92 if(!FiffDigPointSet::readFromStream(t_pStream, *this)) {
93 t_pStream->close();
94 throw std::runtime_error("Could not read the FiffDigPointSet");
95 }
96
97 qInfo("[FiffDigPointSet::FiffDigPointSet] %i digitizer Points read from file.", this->size());
98}
99
100//=============================================================================================================
101
105
106//=============================================================================================================
107
109{
110 //
111 // Open the file, create directory
112 //
113 bool open_here = false;
114
115 if (!p_pStream->device()->isOpen()) {
116 QString t_sFileName = p_pStream->streamName();
117
118 if(!p_pStream->open())
119 return false;
120
121 qInfo("Opening header data %s...\n",t_sFileName.toUtf8().constData());
122
123 open_here = true;
124 }
125
126 //
127 // Read the measurement info
128 //
129 //read_hpi_info(p_pStream,p_Tree, info);
130 fiff_int_t kind = -1;
131 fiff_int_t pos = -1;
132 FiffTag::UPtr t_pTag;
133
134 //
135 // Locate the Electrodes
136 //
137 QList<FiffDirNode::SPtr> isotrak = p_pStream->dirtree()->dir_tree_find(FIFFB_ISOTRAK);
138
139 fiff_int_t coord_frame = FIFFV_COORD_HEAD;
140 FiffCoordTrans dig_trans;
141 qint32 k = 0;
142
143 if (isotrak.size() == 1)
144 {
145 for (k = 0; k < isotrak[0]->nent(); ++k)
146 {
147 kind = isotrak[0]->dir[k]->kind;
148 pos = isotrak[0]->dir[k]->pos;
149 if (kind == FIFF_DIG_POINT)
150 {
151 p_pStream->read_tag(t_pTag, pos);
152 p_Dig.m_qListDigPoint.append(t_pTag->toDigPoint());
153 }
154 else
155 {
156 if (kind == FIFF_MNE_COORD_FRAME)
157 {
158 p_pStream->read_tag(t_pTag, pos);
159 qDebug() << "NEEDS To BE DEBBUGED: FIFF_MNE_COORD_FRAME" << t_pTag->getType();
160 coord_frame = *t_pTag->toInt();
161 }
162 else if (kind == FIFF_COORD_TRANS)
163 {
164 p_pStream->read_tag(t_pTag, pos);
165 qDebug() << "NEEDS To BE DEBBUGED: FIFF_COORD_TRANS" << t_pTag->getType();
166 dig_trans = t_pTag->toCoordTrans();
167 }
168 }
169 }
170 }
171 for(k = 0; k < p_Dig.size(); ++k)
172 {
173 p_Dig[k].coord_frame = coord_frame;
174 }
175
176 //
177 // All kinds of auxliary stuff
178 //
179 if(open_here)
180 {
181 p_pStream->close();
182 }
183 return true;
184}
185
186//=============================================================================================================
187
188void FiffDigPointSet::write(QIODevice &p_IODevice)
189{
190 //
191 // Open the file, create directory
192 //
193
194 // Create the file and save the essentials
195 FiffStream::SPtr t_pStream = FiffStream::start_file(p_IODevice);
196 qInfo("Write Digitizer Points in %s...\n", t_pStream->streamName().toUtf8().constData());
197 this->writeToStream(t_pStream.data());
198 t_pStream->end_file();
199}
200
201//=============================================================================================================
202
203bool FiffDigPointSet::write(const QString& filePath, QString* errorMessage)
204{
205 if (filePath.isEmpty()) {
206 if (errorMessage) *errorMessage = QStringLiteral("Output path is empty.");
207 return false;
208 }
209
210 const QFileInfo info(filePath);
211 if (!info.dir().exists()) {
212 if (errorMessage) {
213 *errorMessage = QStringLiteral("Destination directory '%1' does not exist.")
214 .arg(info.dir().absolutePath());
215 }
216 return false;
217 }
218
219 QFile file(filePath);
220 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
221 if (errorMessage) {
222 *errorMessage = QStringLiteral("Cannot open '%1' for writing: %2")
223 .arg(filePath, file.errorString());
224 }
225 return false;
226 }
227
228 write(file);
229 file.close();
230
231 if (!QFileInfo::exists(filePath)) {
232 if (errorMessage) {
233 *errorMessage = QStringLiteral("FiffDigPointSet::write produced no file at '%1'.")
234 .arg(filePath);
235 }
236 return false;
237 }
238 return true;
239}
240
241//=============================================================================================================
242
244{
245 p_pStream->start_block(FIFFB_MEAS);
246 p_pStream->start_block(FIFFB_MEAS_INFO);
247 p_pStream->start_block(FIFFB_ISOTRAK);
248
249 for(qint32 h = 0; h < m_qListDigPoint.size(); ++h)
250 {
251 p_pStream->write_dig_point(m_qListDigPoint[h]);
252 }
253
254 qInfo("\t%lld digitizer points written\n", m_qListDigPoint.size());
255 p_pStream->end_block(FIFFB_ISOTRAK);
256 p_pStream->end_block(FIFFB_MEAS_INFO);
257 p_pStream->end_block(FIFFB_MEAS);
258}
259
260//=============================================================================================================
261
263{
264 if (idx>=m_qListDigPoint.length())
265 {
266 qWarning("Warning: Required DigPoint doesn't exist! Returning DigPoint '0'.");
267 idx=0;
268 }
269 return m_qListDigPoint[idx];
270}
271
272//=============================================================================================================
273
275{
276 if (idx >= m_qListDigPoint.length())
277 {
278 qWarning("Warning: Required DigPoint doesn't exist! Returning DigPoint '0'.");
279 idx = 0;
280 }
281 return m_qListDigPoint[idx];
282}
283
284//=============================================================================================================
285
286FiffDigPointSet FiffDigPointSet::pickTypes(QList<int> includeTypes) const
287{
288 FiffDigPointSet pickedSet;
289
290 for(int i = 0; i < m_qListDigPoint.size(); ++i) {
291 if(includeTypes.contains(m_qListDigPoint[i].kind)) {
292 pickedSet << m_qListDigPoint[i];
293 }
294 }
295
296 return pickedSet;
297}
298
299//=============================================================================================================
300
302{
303 this->m_qListDigPoint.append(dig);
304 return *this;
305}
306
307//=============================================================================================================
308
310{
311 this->m_qListDigPoint.append(*dig);
312 return *this;
313}
314
315//=============================================================================================================
316
317void FiffDigPointSet::applyTransform(const FiffCoordTrans& coordTrans, bool bApplyInverse)
318{
319 Vector4f tempvec;
320 for(int i = 0; i < m_qListDigPoint.size(); ++i) {
321 tempvec(0) = m_qListDigPoint.at(i).r[0];
322 tempvec(1) = m_qListDigPoint.at(i).r[1];
323 tempvec(2) = m_qListDigPoint.at(i).r[2];
324 tempvec(3) = 1.0f;
325 if(bApplyInverse) {
326 tempvec = coordTrans.invtrans * tempvec;
327 } else {
328 tempvec = coordTrans.trans * tempvec;
329 }
330 m_qListDigPoint[i].r[0] = tempvec(0);
331 m_qListDigPoint[i].r[1] = tempvec(1);
332 m_qListDigPoint[i].r[2] = tempvec(2);
333 }
334}
335
336//=============================================================================================================
337
338QList<FiffDigPoint> FiffDigPointSet::getList()
339{
340 return m_qListDigPoint;
341}
Container for the FIFF_DIG_POINT records of a measurement (a parsed FIFFB_ISOTRAK block).
#define FIFF_MNE_COORD_FRAME
#define FIFFV_COORD_HEAD
Recursive node of the parsed FIFF block tree (FIFFB_* hierarchy with directory entries and children).
#define FIFFB_ISOTRAK
Definition fiff_file.h:363
#define FIFFB_MEAS
Definition fiff_file.h:355
#define FIFF_COORD_TRANS
Definition fiff_file.h:468
#define FIFF_DIG_POINT
Definition fiff_file.h:459
#define FIFFB_MEAS_INFO
Definition fiff_file.h:356
FIFF tag: the 16-byte tag header (kind, type, size, next) plus its decoded payload.
Single digitization point (FIFF_DIG_POINT) with kind (cardinal/HPI/EEG/extra), identifier and 3D coor...
Primitive scalar typedefs and forward-compatible aliases backing the FIFF type system.
FIFF file I/O, in-memory data structures and high-level readers/writers.
Labelled 4x4 FIFF affine: source frame, destination frame, rotation, translation and cached inverse.
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > trans
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > invtrans
One digitizer point: kind (cardinal/HPI/EEG/extra), ident, 3D position in FIFFV_COORD_HEAD.
const FiffDigPoint & operator[](qint32 idx) const
FiffDigPointSet & operator<<(const FiffDigPoint &dig)
FiffDigPointSet pickTypes(QList< int > includeTypes) const
void applyTransform(const FiffCoordTrans &coordTrans, bool bApplyInverse=false)
void write(QIODevice &p_IODevice)
void writeToStream(FiffStream *p_pStream)
static bool readFromStream(FiffStream::SPtr &p_Stream, FiffDigPointSet &p_Dig)
QList< FiffDigPoint > getList()
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
fiff_long_t start_block(fiff_int_t kind)
QSharedPointer< FiffStream > SPtr
fiff_long_t write_dig_point(const FiffDigPoint &dig)
static FiffStream::SPtr start_file(QIODevice &p_IODevice)
fiff_long_t end_block(fiff_int_t kind, fiff_int_t next=FIFFV_NEXT_SEQ)
std::unique_ptr< FiffTag > UPtr
Definition fiff_tag.h:164