52#define _USE_MATH_DEFINES
76,
trans(MatrixXf::Identity(4,4))
86,
trans(MatrixXf::Identity(4,4))
89 if(!
read(p_IODevice, *
this))
91 qWarning(
"\tCoordindate transform not found.\n");
100,
to(p_FiffCoordTrans.
to)
128 this->
from = from_new;
141 qInfo(
"Reading coordinate transform from %s...\n", pStream->streamName().toUtf8().constData());
149 bool success =
false;
154 for ( qint32 k = 0; k < pStream->dir().size(); ++k )
158 pStream->read_tag(t_pTag,pStream->dir()[k]->pos);
159 p_Trans = t_pTag->toCoordTrans();
173 qInfo(
"Write coordinate transform in %s...\n", pStream->streamName().toUtf8().constData());
190 MatrixX4f rr_ones(rr.rows(), 4);
196 rr_ones.block(0,0,rr.rows(),3) = rr;
197 return rr_ones*
trans.block<3,4>(0,0).transpose();
204 MatrixX4f rr_ones(rr.rows(), 4);
210 rr_ones.block(0,0,rr.rows(),3) = rr;
211 return rr_ones*
invtrans.block<3,4>(0,0).transpose();
234 default:
return "unknown";
242 this->
trans = MatrixXf::Zero(4,4);
247 this->
trans.block<3,3>(0,0) = rot;
248 this->
trans.block<3,1>(0,3) = move;
249 this->
trans(3,3) = 1.0f;
258 this->
trans = matTrans;
264 this->
trans.row(3) = Vector4f(0,0,0,1);
282 std::cout <<
"Coordinate transformation: ";
283 std::cout << (QString(
"%1 -> %2\n").arg(
frame_name(this->from)).arg(
frame_name(this->to))).toUtf8().data();
285 for (
int p = 0; p < 3; p++)
286 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));
294 MatrixX4f mDevHeadT = this->
trans;
295 Matrix3f mRot = mDevHeadT.block(0,0,3,3);
296 Matrix3f mRotNew = mTransDest.block(0,0,3,3);
298 Quaternionf quat(mRot);
299 Quaternionf quatNew(mRotNew);
304 Quaternionf quatCompare;
306 quatCompare = quat*quatNew.inverse();
307 fAngle = quat.angularDistance(quatNew);
308 fAngle = fAngle * 180 /
M_PI;
317 VectorXf vTrans = this->
trans.col(3);
318 VectorXf vTransDest = mTransDest.col(3);
320 float fMove = (vTrans-vTransDest).norm();
329 for (
int j = 0; j < 3; j++) {
330 res[j] = do_move ? t.
trans(j,3) : 0.0f;
331 for (
int k = 0; k < 3; k++)
332 res[j] += t.
trans(j,k) * r[k];
334 for (
int j = 0; j < 3; j++)
343 for (
int j = 0; j < 3; j++) {
344 res[j] = do_move ? t.
invtrans(j,3) : 0.0f;
345 for (
int k = 0; k < 3; k++)
348 for (
int j = 0; j < 3; j++)
382 for (
int swapped = 0; swapped < 2 && !found; swapped++) {
387 a = s1; b = s2; found =
true;
389 a = s1.
inverted(); b = s2; found =
true;
391 a = s1; b = s2.
inverted(); found =
true;
397 if (!found || a.
from != b.
to) {
398 qCritical(
"Cannot combine coordinate transforms");
407 result.
trans.row(3) << 0.0f, 0.0f, 0.0f, 1.0f;
419 Map<const Vector3f> L(rL);
420 Map<const Vector3f> N(rN);
421 Map<const Vector3f> R(rR);
423 Vector3f diff1 = N - L;
424 Vector3f diff2 = R - L;
426 float alpha = diff1.dot(diff2) / diff2.dot(diff2);
427 Vector3f r0 = (1.0f - alpha) * L + alpha * R;
428 Vector3f ex = diff2.normalized();
429 Vector3f ey = (N - r0).normalized();
430 Vector3f ez = ex.cross(ey);
435 result.
rot().col(0) = ex;
436 result.
rot().col(1) = ey;
437 result.
rot().col(2) = ez;
450 if (!stream->open()) {
455 for (
int k = 0; k < stream->dir().size(); k++) {
457 if (!stream->read_tag(t_pTag, stream->dir()[k]->pos))
472 qCritical(
"No suitable coordinate transformation found in %s.", name.toUtf8().constData());
496 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
497 qCritical(
"Cannot open %s", name.toUtf8().constData());
501 QTextStream in(&file);
506 while (!in.atEnd() && row < 4) {
507 QString line = in.readLine();
509 int commentIdx = line.indexOf(
'#');
511 line = line.left(commentIdx);
512 line = line.trimmed();
517 QStringList parts = line.simplified().split(
' ', Qt::SkipEmptyParts);
518 if (parts.size() < 4) {
519 qCritical(
"Cannot read the coordinate transformation from %s",
520 name.toUtf8().constData());
523 bool ok1, ok2, ok3, ok4;
524 rot(row, 0) = parts[0].toFloat(&ok1);
525 rot(row, 1) = parts[1].toFloat(&ok2);
526 rot(row, 2) = parts[2].toFloat(&ok3);
527 moveVec[row] = parts[3].toFloat(&ok4) / 1000.0f;
528 if (!ok1 || !ok2 || !ok3 || !ok4) {
529 qCritical(
"Bad floating point number in coordinate transformation");
538 qCritical(
"Cannot read the coordinate transformation from %s",
539 name.toUtf8().constData());
561 qint32* t_pInt32 = (qint32*)tag->data();
562 t.
from = t_pInt32[0];
565 float* t_pFloat = (
float*)tag->data();
568 for (r = 0; r < 3; ++r) {
569 t.
trans(r, 3) = t_pFloat[11 + r];
570 for (c = 0; c < 3; ++c) {
571 t.
trans(r, c) = t_pFloat[2 + count];
578 for (r = 0; r < 3; ++r) {
579 t.
invtrans(r, 3) = t_pFloat[23 + r];
580 for (c = 0; c < 3; ++c) {
581 t.
invtrans(r, c) = t_pFloat[14 + count];
600 for (k = 0; k < node->nent(); k++)
601 kind = node->dir[k]->kind;
602 pos = node->dir[k]->pos;
604 if (!stream->read_tag(t_pTag, pos))
616 qWarning(
"No suitable coordinate transformation found");
634 Eigen::MatrixXf fromPts(np, 3), toPts(np, 3);
635 for (
int j = 0; j < np; ++j)
636 for (
int c = 0; c < 3; ++c) {
637 fromPts(j, c) = fromp[j][c];
638 toPts(j, c) = top[j][c];
642 Eigen::Vector3f from0 = fromPts.colwise().mean();
643 Eigen::Vector3f to0 = toPts.colwise().mean();
645 Eigen::MatrixXf fromC = fromPts.rowwise() - from0.transpose();
646 Eigen::MatrixXf toC = toPts.rowwise() - to0.transpose();
651 Eigen::VectorXf wVec = Eigen::Map<Eigen::VectorXf>(w, np);
652 S = fromC.transpose() * wVec.asDiagonal() * toC;
654 S = fromC.transpose() * toC;
658 Eigen::JacobiSVD<Eigen::Matrix3f> svd(S, Eigen::ComputeFullU | Eigen::ComputeFullV);
659 Eigen::Matrix3f R = svd.matrixV() * svd.matrixU().transpose();
662 Eigen::Vector3f moveVec = to0 - R * from0;
665 for (
int p = 0; p < np; ++p) {
666 Eigen::Vector3f rr = R * fromPts.row(p).transpose() + moveVec;
667 float diff = (toPts.row(p).transpose() - rr).norm();
668 if (diff > max_diff) {
669 qWarning(
"Too large difference in matching : %7.1f > %7.1f mm",
670 1000.0f * diff, 1000.0f * max_diff);
FiffTag class declaration, which provides fiff tag I/O and processing methods.
#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
FiffStream class declaration.
FiffDirNode class declaration, which provides fiff dir tree processing methods.
#define FIFFT_COORD_TRANS_STRUCT
FiffCoordTrans class declaration.
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
static FiffCoordTrans readTransformFromNode(FiffStream::SPtr &stream, const FiffDirNode::SPtr &node, int from, int to)
static FiffCoordTrans combine(int from, int to, const FiffCoordTrans &t1, const FiffCoordTrans &t2)
static FiffCoordTrans identity(int from, int to)
static FiffCoordTrans readMriTransform(const QString &name)
FiffCoordTrans inverted() const
static FiffCoordTrans readMeasTransform(const QString &name)
void write(QIODevice &p_IODevice)
Writes the transformation to file.
static FiffCoordTrans procrustesAlign(int from_frame, int to_frame, float **fromp, float **top, float *w, int np, float max_diff)
float translationTo(Eigen::MatrixX4f mTransDest)
static FiffCoordTrans fromCardinalPoints(int from, int to, const float *rL, const float *rN, const float *rR)
static FiffCoordTrans readTransform(const QString &name, int from, int to)
Eigen::MatrixX3f apply_inverse_trans(const Eigen::MatrixX3f &rr, bool do_move=true) const
static FiffCoordTrans readFShead2mriTransform(const QString &name)
Eigen::MatrixX3f apply_trans(const Eigen::MatrixX3f &rr, bool do_move=true) const
static bool read(QIODevice &p_IODevice, FiffCoordTrans &p_Trans)
float angleTo(Eigen::MatrixX4f mTransDest)
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > trans
void writeToStream(FiffStream *p_pStream)
Writes the transformation to a FIFF stream.
static QString frame_name(int frame)
static FiffCoordTrans readFromTag(const std::unique_ptr< FiffTag > &tag)
static bool addInverse(FiffCoordTrans &t)
Eigen::Matrix< float, 4, 4, Eigen::DontAlign > invtrans
static FiffCoordTrans readTransformAscii(const QString &name, int from, int to)
QSharedPointer< FiffDirNode > SPtr
QSharedPointer< FiffStream > SPtr
fiff_long_t write_coord_trans(const FiffCoordTrans &trans)
static FiffStream::SPtr start_file(QIODevice &p_IODevice)
std::unique_ptr< FiffTag > UPtr