Skip to content

Instantly share code, notes, and snippets.

Embed
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 http://mozilla.org/MPL/2.0/.
*
* 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 http://www.mrtrix.org/.
*/
#include "command.h"
#include "image.h"
#include "interp/cubic.h"
using namespace MR;
using namespace App;
void usage ()
{
AUTHOR = "Max (mail@maxpietsch.com)";
SYNOPSIS = "Apply a spatial transformation to scanner-space coordinates";
ARGUMENTS
+ 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)
public:
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);
interp.scanner(pos);
coords.row(n) = interp.row(3);
}
}
protected:
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);
warper(coords);
save_matrix (coords, argument[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment