17#ifndef __registration_warp_invert_h__
18#define __registration_warp_invert_h__
28 namespace Registration
36 class DisplacementThreadKernel {
MEMALIGN(DisplacementThreadKernel)
39 DisplacementThreadKernel (Image<default_type> & displacement,
40 Image<default_type> & displacement_inverse,
41 const size_t max_iter,
43 displacement (displacement),
44 transform (displacement_inverse),
46 error_tolerance (error_tol) {}
48 void operator() (Image<default_type>& displacement_inverse)
52 Eigen::Vector3d
current = truth + Eigen::Vector3d(displacement_inverse.row(3));
55 default_type error = std::numeric_limits<default_type>::max();
56 while (iter < max_iter && error > error_tolerance) {
57 error = update (
current, truth);
60 displacement_inverse.row(3) =
current - truth;
68 Eigen::Vector3d discrepancy = truth - (
current + Eigen::Vector3d (displacement.row(3)));
70 return discrepancy.dot (discrepancy);
73 Interp::Linear<Image<default_type> > displacement;
75 const size_t max_iter;
80 class DeformationThreadKernel {
MEMALIGN(DeformationThreadKernel)
83 DeformationThreadKernel (Image<default_type> & deform,
84 Image<default_type> & inv_deform,
85 const size_t max_iter,
88 transform (inv_deform),
90 error_tolerance (error_tol) {}
92 void operator() (Image<default_type>& inv_deform)
96 Eigen::Vector3d
current = inv_deform.row(3);
99 default_type error = std::numeric_limits<default_type>::max();
100 while (iter < max_iter && error > error_tolerance) {
101 error = update (
current, truth);
112 Eigen::Vector3d discrepancy = truth - Eigen::Vector3d (deform.row(3));
114 return discrepancy.dot (discrepancy);
117 Interp::Linear<Image<default_type> > deform;
119 const size_t max_iter;
133 check_dimensions (deform_field, inv_deform_field);
134 error_tolerance *= (deform_field.
spacing(0) + deform_field.
spacing(1) + deform_field.
spacing(2)) / 3;
139 ThreadedLoop (
"inverting warp field...", inv_deform_field, 0, 3)
140 .run (DeformationThreadKernel (deform_field, inv_deform_field, max_iter, error_tolerance), inv_deform_field);
151 invert_deformation (deform_field, inv_deform, is_initialised, max_iter, error_tolerance);
160 check_dimensions (disp_field, inv_disp_field);
163 ThreadedLoop (
"inverting displacement field...", inv_disp_field, 0, 3)
164 .run (DisplacementThreadKernel (disp_field, inv_disp_field, max_iter, error_tolerance), inv_disp_field);
static Image scratch(const Header &template_header, const std::string &label="scratch image")
default_type spacing(size_t axis) const
FORCE_INLINE void invert_displacement_deformation(Image< default_type > &disp, Image< default_type > &inv_deform, bool is_initialised=false, size_t max_iter=50, default_type error_tolerance=0.0001)
FORCE_INLINE void invert_displacement(Image< default_type > &disp_field, Image< default_type > &inv_disp_field, size_t max_iter=50, default_type error_tolerance=0.0001)
FORCE_INLINE void invert_deformation(Image< default_type > &deform_field, Image< default_type > &inv_deform_field, bool is_initialised=false, size_t max_iter=50, default_type error_tolerance=0.0001)
std::pair< int, int > current()
void displacement2deformation(ImageType &input, ImageType &output)
double default_type
the default type used throughout MRtrix
ThreadedLoopRunOuter< decltype(Loop(vector< size_t >()))> ThreadedLoop(const HeaderType &source, const vector< size_t > &outer_axes, const vector< size_t > &inner_axes)
Multi-threaded loop object.