Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
* For more details, see
#include "command.h"
#include "image.h"
#include "interp/cubic.h"
using namespace MR;
using namespace App;
void usage ()
AUTHOR = "Max (";
SYNOPSIS = "Apply a spatial transformation to scanner-space coordinates";
+ Argument ("coord", "the input coordinate file (Nx3 for N coordinates).").type_file_in()
+ Argument ("warp", "the 4D image containing the transform.").type_image_in()
+ Argument ("output", "the output coordinates").type_file_out();
using value_type = default_type;
class Warper { MEMALIGN(Warper)
Warper (const Image<value_type>& warp) :
interp (warp) { }
void operator () (Eigen::MatrixXd& coords) {
for (size_t n = 0; n < coords.rows(); ++n) {
pos = coords.row(n);
coords.row(n) = interp.row(3);
Interp::Cubic< Image<value_type> > interp;
Eigen::Vector3d pos;
void run ()
Eigen::MatrixXd coords = load_matrix (argument[0]);
if (coords.cols() != 3)
throw Exception ("number of columns in input needs to be 3");
auto warp = Image<value_type>::open (argument[1]).with_direct_io (Stride::contiguous_along_axis(3));
if (warp.ndim() != 4)
throw Exception ("the input -warp file must be a 4D deformation field");
if (warp.size(3) != 3)
throw Exception ("the input -warp file must have 3 volumes in the 4th dimension (x,y,z positions)");
Warper warper (warp);
save_matrix (coords, argument[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment