38#ifndef _USE_MATH_DEFINES
39#define _USE_MATH_DEFINES
65, trans(MatrixXf::Identity(4, 4))
66, invtrans(MatrixXf::Identity(4, 4))
75, trans(MatrixXf::Identity(4, 4))
76, invtrans(MatrixXf::Identity(4, 4))
78 if (!read(p_IODevice, *
this)) {
79 throw std::runtime_error(
"Coordinate transform not found");
86: from(p_FiffCoordTrans.from)
87, to(p_FiffCoordTrans.to)
88, trans(p_FiffCoordTrans.trans)
89, invtrans(p_FiffCoordTrans.invtrans)
115 this->
from = from_new;
126 FiffStream::SPtr pStream(
new FiffStream(&p_IODevice));
128 qInfo(
"Reading coordinate transform from %s...\n", pStream->streamName().toUtf8().constData());
129 if (!pStream->open())
135 FiffTag::UPtr t_pTag;
136 bool success =
false;
141 for (qint32 k = 0; k < pStream->dir().size() && !success; ++k) {
142 if (pStream->dir()[k]->kind ==
FIFF_COORD_TRANS && pStream->read_tag(t_pTag, pStream->dir()[k]->pos)) {
143 p_Trans = t_pTag->toCoordTrans();
156 FiffStream::SPtr pStream = FiffStream::start_file(qIODevice);
160 qInfo(
"Write coordinate transform in %s...\n", pStream->streamName().toUtf8().constData());
178 MatrixX4f rr_ones(rr.rows(), 4);
184 rr_ones.block(0, 0, rr.rows(), 3) = rr;
185 return rr_ones *
trans.block<3, 4>(0, 0).transpose();
192 MatrixX4f rr_ones(rr.rows(), 4);
198 rr_ones.block(0, 0, rr.rows(), 3) = rr;
199 return rr_ones *
invtrans.block<3, 4>(0, 0).transpose();
218 return "MRI (surface RAS)";
224 return "MRI display";
226 return "CTF MEG device";
228 return "CTF/4D/KIT head";
230 return "RAS (non-zero origin)";
232 return "MNI Talairach";
234 return "Talairach (MNI z > 0)";
236 return "Talairach (MNI z < 0)";
246 this->
trans = MatrixXf::Zero(4, 4);
251 this->
trans.block<3, 3>(0, 0) =
rot;
253 this->
trans(3, 3) = 1.0f;
262 this->
trans = matTrans;
268 this->
trans.row(3) = Vector4f(0, 0, 0, 1).transpose();
286 std::cout <<
"Coordinate transformation: ";
289 for (
int p = 0; p < 3; p++)
290 qDebug(
"\t% 8.6f % 8.6f % 8.6f\t% 7.2f mm\n",
trans(p, 0),
trans(p, 1),
trans(p, 2), 1000 *
trans(p, 3));
298 MatrixX4f mDevHeadT = this->
trans;
299 Matrix3f mRot = mDevHeadT.block(0, 0, 3, 3);
300 Matrix3f mRotNew = mTransDest.block(0, 0, 3, 3);
302 Quaternionf quat(mRot);
303 Quaternionf quatNew(mRotNew);
308 Quaternionf quatCompare;
310 quatCompare = quat * quatNew.inverse();
311 fAngle = quat.angularDistance(quatNew);
312 fAngle = fAngle * 180 /
M_PI;
321 VectorXf vTrans = this->
trans.col(3);
322 VectorXf vTransDest = mTransDest.col(3);
324 float fMove = (vTrans - vTransDest).norm();
333 for (
int j = 0; j < 3; j++) {
334 res[j] = do_move ? t.
trans(j, 3) : 0.0f;
335 for (
int k = 0; k < 3; k++)
336 res[j] += t.
trans(j, k) * r[k];
338 for (
int j = 0; j < 3; j++)
347 for (
int j = 0; j < 3; j++) {
348 res[j] = do_move ? t.
invtrans(j, 3) : 0.0f;
349 for (
int k = 0; k < 3; k++)
352 for (
int j = 0; j < 3; j++)
386 for (
int swapped = 0; swapped < 2 && !found; swapped++) {
409 if (!found || a.
from != b.
to) {
410 qCritical(
"Cannot combine coordinate transforms");
419 result.
trans.row(3) << 0.0f, 0.0f, 0.0f, 1.0f;
431 Map<const Vector3f> L(rL);
432 Map<const Vector3f> N(rN);
433 Map<const Vector3f>
R(rR);
435 Vector3f diff1 = N - L;
436 Vector3f diff2 =
R - L;
438 float alpha = diff1.dot(diff2) / diff2.dot(diff2);
439 Vector3f r0 = (1.0f - alpha) * L + alpha *
R;
440 Vector3f ex = diff2.normalized();
441 Vector3f ey = (N - r0).normalized();
442 Vector3f ez = ex.cross(ey);
447 result.
rot().col(0) = ex;
448 result.
rot().col(1) = ey;
449 result.
rot().col(2) = ez;
460 FiffStream::SPtr stream(
new FiffStream(&file));
462 if (!stream->open()) {
466 FiffTag::UPtr t_pTag;
467 for (
int k = 0; k < stream->dir().size(); k++) {
469 if (!stream->read_tag(t_pTag, stream->dir()[k]->pos))
484 qCritical(
"No suitable coordinate transformation found in %s.", name.toUtf8().constData());
508 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
509 qCritical(
"Cannot open %s", name.toUtf8().constData());
513 QTextStream in(&file);
518 while (!in.atEnd() && row < 4) {
519 QString line = in.readLine();
521 int commentIdx = line.indexOf(
'#');
523 line = line.left(commentIdx);
524 line = line.trimmed();
529 QStringList parts = line.simplified().split(
' ', Qt::SkipEmptyParts);
530 if (parts.size() < 4) {
531 qCritical(
"Cannot read the coordinate transformation from %s",
532 name.toUtf8().constData());
535 bool ok1, ok2, ok3, ok4;
536 rot(row, 0) = parts[0].toFloat(&ok1);
537 rot(row, 1) = parts[1].toFloat(&ok2);
538 rot(row, 2) = parts[2].toFloat(&ok3);
539 moveVec[row] = parts[3].toFloat(&ok4) / 1000.0f;
540 if (!ok1 || !ok2 || !ok3 || !ok4) {
541 qCritical(
"Bad floating point number in coordinate transformation");
550 qCritical(
"Cannot read the coordinate transformation from %s",
551 name.toUtf8().constData());
573 qint32* t_pInt32 = (qint32*)tag->data();
574 t.
from = t_pInt32[0];
577 float* t_pFloat = (
float*)tag->data();
580 for (r = 0; r < 3; ++r) {
581 t.
trans(r, 3) = t_pFloat[11 + r];
582 for (c = 0; c < 3; ++c) {
583 t.
trans(r, c) = t_pFloat[2 + count];
587 t.
trans(3, 0) = 0.0f;
588 t.
trans(3, 1) = 0.0f;
589 t.
trans(3, 2) = 0.0f;
590 t.
trans(3, 3) = 1.0f;
593 for (r = 0; r < 3; ++r) {
594 t.
invtrans(r, 3) = t_pFloat[23 + r];
595 for (c = 0; c < 3; ++c) {
596 t.
invtrans(r, c) = t_pFloat[14 + count];
614 FiffTag::UPtr t_pTag;
620 for (
int k = 0; k < node->nent(); ++k) {
621 const FiffDirEntry::SPtr& entry = node->dir[k];
625 if (!stream->read_tag(t_pTag, entry->pos))
640 qWarning(
"No suitable coordinate transformation found");
648 const Eigen::MatrixXf& fromPts,
649 const Eigen::MatrixXf& toPts,
650 const Eigen::VectorXf& w,
653 int np = fromPts.rows();
656 Eigen::Vector3f
from0 = fromPts.colwise().mean();
657 Eigen::Vector3f
to0 = toPts.colwise().mean();
659 Eigen::MatrixXf
fromC = fromPts.rowwise() -
from0.transpose();
660 Eigen::MatrixXf
toC = toPts.rowwise() -
to0.transpose();
665 S =
fromC.transpose() * w.asDiagonal() *
toC;
671 Eigen::JacobiSVD<Eigen::Matrix3f>
svd(
S, Eigen::ComputeFullU | Eigen::ComputeFullV);
672 Eigen::Matrix3f
R =
svd.matrixV() *
svd.matrixU().transpose();
678 for (
int p = 0; p < np; ++p) {
679 Eigen::Vector3f rr =
R * fromPts.row(p).transpose() +
moveVec;
680 float diff = (toPts.row(p).transpose() - rr).norm();
681 if (diff > max_diff) {
682 qWarning(
"Too large difference in matching : %7.1f > %7.1f mm",
683 1000.0f * diff, 1000.0f * max_diff);
#define FIFFV_MNE_COORD_MNI_TAL
#define FIFFV_MNE_COORD_FS_TAL_LTZ
#define FIFFV_COORD_MRI_SLICE
#define FIFFV_MNE_COORD_CTF_DEVICE
#define FIFFV_COORD_DEVICE
#define FIFFV_COORD_MRI_DISPLAY
#define FIFFV_MNE_COORD_MRI_VOXEL
#define FIFFV_MNE_COORD_CTF_HEAD
#define FIFFV_COORD_ISOTRAK
#define FIFFV_COORD_UNKNOWN
#define FIFFV_MNE_COORD_FS_TAL_GTZ
#define FIFFV_MNE_COORD_RAS
FIFF tag: the 16-byte tag header (kind, type, size, next) plus its decoded payload.
FIFF binary tag-stream layer: wraps a QIODevice to read and write FIFF tags, directories,...
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
#define FIFFT_COORD_TRANS_STRUCT
return FiffCoordTrans(from_frame, to_frame, R, moveVec)
Eigen::JacobiSVD< Eigen::Matrix3f > svd(S, Eigen::ComputeFullU|Eigen::ComputeFullV)
Recursive node of the parsed FIFF block tree (FIFFB_* hierarchy with directory entries and children).
FIFF file I/O, in-memory data structures and high-level readers/writers.
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > invtrans
static FiffCoordTrans procrustesAlign(int from_frame, int to_frame, const Eigen::MatrixXf &fromp, const Eigen::MatrixXf &top, const Eigen::VectorXf &w, float max_diff)
static FiffCoordTrans combine(int from, int to, const FiffCoordTrans &t1, const FiffCoordTrans &t2)
static QString frame_name(int frame)
static FiffCoordTrans readTransformFromNode(FiffStream::SPtr &stream, const FiffDirNode::SPtr &node, int from, int to)
void writeToStream(FiffStream *p_pStream)
Writes the transformation to a FIFF stream.
static FiffCoordTrans readMeasTransform(const QString &name)
static FiffCoordTrans readFShead2mriTransform(const QString &name)
Eigen::MatrixX3f apply_inverse_trans(const Eigen::MatrixX3f &rr, bool do_move=true) const
bool write(QIODevice &p_IODevice)
Writes the transformation to file.
static FiffCoordTrans readMriTransform(const QString &name)
static FiffCoordTrans readTransformAscii(const QString &name, int from, int to)
static bool read(QIODevice &p_IODevice, FiffCoordTrans &p_Trans)
FiffCoordTrans inverted() const
Eigen::MatrixX3f apply_trans(const Eigen::MatrixX3f &rr, bool do_move=true) const
float angleTo(Eigen::MatrixX4f mTransDest)
static FiffCoordTrans identity(int from, int to)
static FiffCoordTrans fromCardinalPoints(int from, int to, const float *rL, const float *rN, const float *rR)
float translationTo(Eigen::MatrixX4f mTransDest)
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > trans
static FiffCoordTrans readTransform(const QString &name, int from, int to)
static bool addInverse(FiffCoordTrans &t)
static FiffCoordTrans readFromTag(const std::unique_ptr< FiffTag > &tag)
QSharedPointer< FiffDirNode > SPtr
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
fiff_long_t write_coord_trans(const FiffCoordTrans &trans)
std::unique_ptr< FiffTag > UPtr
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > invtrans
FiffCoordTrans inverted() const
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > trans
static bool addInverse(FiffCoordTrans &t)