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 for (k = 0; k < isotrak[0]->nent(); ++k) {
145 kind = isotrak[0]->dir[k]->kind;
146 pos = isotrak[0]->dir[k]->pos;
147 if (kind == FIFF_DIG_POINT) {
148 p_pStream->read_tag(t_pTag, pos);
149 p_Dig.m_qListDigPoint.append(t_pTag->toDigPoint());
150 } else {
151 if (kind == FIFF_MNE_COORD_FRAME) {
152 p_pStream->read_tag(t_pTag, pos);
153 qDebug() << "NEEDS To BE DEBBUGED: FIFF_MNE_COORD_FRAME" << t_pTag->getType();
154 coord_frame = *t_pTag->toInt();
155 } else if (kind == FIFF_COORD_TRANS) {
156 p_pStream->read_tag(t_pTag, pos);
157 qDebug() << "NEEDS To BE DEBBUGED: FIFF_COORD_TRANS" << t_pTag->getType();
158 dig_trans = t_pTag->toCoordTrans();
159 }
160 }
161 }
162 }
163 for (k = 0; k < p_Dig.size(); ++k) {
164 p_Dig[k].coord_frame = coord_frame;
165 }
166
167 //
168 // All kinds of auxliary stuff
169 //
170 if (open_here) {
171 p_pStream->close();
172 }
173 return true;
174}
175
176//=============================================================================================================
177
178bool FiffDigPointSet::write(QIODevice& p_IODevice)
179{
180 FiffStream::SPtr t_pStream = FiffStream::start_file(p_IODevice);
181 if (!t_pStream) {
182 return false;
183 }
184 qInfo("Write Digitizer Points in %s...\n", t_pStream->streamName().toUtf8().constData());
185 this->writeToStream(t_pStream.data());
186 t_pStream->end_file();
187 return true;
188}
189
190//=============================================================================================================
191
192bool FiffDigPointSet::write(const QString& filePath, QString* errorMessage)
193{
194 if (filePath.isEmpty()) {
195 if (errorMessage)
196 *errorMessage = QStringLiteral("Output path is empty.");
197 return false;
198 }
199
200 const QFileInfo info(filePath);
201 if (!info.dir().exists()) {
202 if (errorMessage) {
203 *errorMessage = QStringLiteral("Destination directory '%1' does not exist.")
204 .arg(info.dir().absolutePath());
205 }
206 return false;
207 }
208
209 QFile file(filePath);
210 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
211 if (errorMessage) {
212 *errorMessage = QStringLiteral("Cannot open '%1' for writing: %2")
213 .arg(filePath, file.errorString());
214 }
215 return false;
216 }
217
218 write(file);
219 file.close();
220
221 if (!QFileInfo::exists(filePath)) {
222 if (errorMessage) {
223 *errorMessage = QStringLiteral("FiffDigPointSet::write produced no file at '%1'.")
224 .arg(filePath);
225 }
226 return false;
227 }
228 return true;
229}
230
231//=============================================================================================================
232
234{
235 p_pStream->start_block(FIFFB_MEAS);
236 p_pStream->start_block(FIFFB_MEAS_INFO);
237 p_pStream->start_block(FIFFB_ISOTRAK);
238
239 for (qint32 h = 0; h < m_qListDigPoint.size(); ++h) {
240 p_pStream->write_dig_point(m_qListDigPoint[h]);
241 }
242
243 qInfo("\t%lld digitizer points written\n", static_cast<long long>(m_qListDigPoint.size()));
244 p_pStream->end_block(FIFFB_ISOTRAK);
245 p_pStream->end_block(FIFFB_MEAS_INFO);
246 p_pStream->end_block(FIFFB_MEAS);
247}
248
249//=============================================================================================================
250
252{
253 if (idx >= m_qListDigPoint.length()) {
254 qWarning("Warning: Required DigPoint doesn't exist! Returning DigPoint '0'.");
255 idx = 0;
256 }
257 return m_qListDigPoint[idx];
258}
259
260//=============================================================================================================
261
263{
264 if (idx >= m_qListDigPoint.length()) {
265 qWarning("Warning: Required DigPoint doesn't exist! Returning DigPoint '0'.");
266 idx = 0;
267 }
268 return m_qListDigPoint[idx];
269}
270
271//=============================================================================================================
272
273FiffDigPointSet FiffDigPointSet::pickTypes(QList<int> includeTypes) const
274{
275 FiffDigPointSet pickedSet;
276
277 for (int i = 0; i < m_qListDigPoint.size(); ++i) {
278 if (includeTypes.contains(m_qListDigPoint[i].kind)) {
279 pickedSet << m_qListDigPoint[i];
280 }
281 }
282
283 return pickedSet;
284}
285
286//=============================================================================================================
287
289{
290 this->m_qListDigPoint.append(dig);
291 return *this;
292}
293
294//=============================================================================================================
295
297{
298 this->m_qListDigPoint.append(*dig);
299 return *this;
300}
301
302//=============================================================================================================
303
304void FiffDigPointSet::applyTransform(const FiffCoordTrans& coordTrans, bool bApplyInverse)
305{
306 Vector4f tempvec;
307 for (int i = 0; i < m_qListDigPoint.size(); ++i) {
308 tempvec(0) = m_qListDigPoint.at(i).r[0];
309 tempvec(1) = m_qListDigPoint.at(i).r[1];
310 tempvec(2) = m_qListDigPoint.at(i).r[2];
311 tempvec(3) = 1.0f;
312 if (bApplyInverse) {
313 tempvec = coordTrans.invtrans * tempvec;
314 } else {
315 tempvec = coordTrans.trans * tempvec;
316 }
317 m_qListDigPoint[i].r[0] = tempvec(0);
318 m_qListDigPoint[i].r[1] = tempvec(1);
319 m_qListDigPoint[i].r[2] = tempvec(2);
320 }
321}
322
323//=============================================================================================================
324
325QList<FiffDigPoint> FiffDigPointSet::getList()
326{
327 return m_qListDigPoint;
328}
#define FIFF_MNE_COORD_FRAME
#define FIFFV_COORD_HEAD
FIFF tag: the 16-byte tag header (kind, type, size, next) plus its decoded payload.
Container for the FIFF_DIG_POINT records of a measurement (a parsed FIFFB_ISOTRAK block).
#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
Recursive node of the parsed FIFF block tree (FIFFB_* hierarchy with directory entries and children).
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.
qint32 fiff_int_t
Definition fiff_types.h:86
Labelled 4x4 FIFF affine: source frame, destination frame, rotation, translation and cached inverse.
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > invtrans
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > trans
One digitizer point: kind (cardinal/HPI/EEG/extra), ident, 3D position in FIFFV_COORD_HEAD.
bool write(QIODevice &p_IODevice)
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 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:165