Developer documentation
Version 3.0.3-105-gd3941f44
warp.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __adapter_warp_h__
18#define __adapter_warp_h__
19
20#include "image.h"
21#include "transform.h"
22#include "interp/cubic.h"
23#include "adapter/jacobian.h"
24
25namespace MR
26{
27 namespace Adapter
28 {
29
31 // @{
32
34
51 template <template <class ImageType> class Interpolator, class ImageType, class WarpType>
52 class Warp :
53 public ImageBase<Warp<Interpolator,ImageType,WarpType>, typename ImageType::value_type>
55 public:
56 using value_type = typename ImageType::value_type;
57
58 Warp (const ImageType& original,
59 const WarpType& warp,
60 const value_type value_when_out_of_bounds = Interpolator<ImageType>::default_out_of_bounds_value(),
61 const bool jacobian_modulate = false) :
62 interp (original, value_when_out_of_bounds),
63 warp (warp),
64 x { 0, 0, 0 },
65 dim { warp.size(0), warp.size(1), warp.size(2) },
66 vox { warp.spacing(0), warp.spacing(1), warp.spacing(2) },
67 value_when_out_of_bounds (value_when_out_of_bounds),
68 jac_modulate (jacobian_modulate),
69 jacobian_adapter (warp, true) {
70 assert (warp.ndim() == 4);
71 assert (warp.size(3) == 3);
72 }
73
74
75 size_t ndim () const { return interp.ndim(); }
76 bool valid () const { return interp.valid(); }
77 int size (size_t axis) const { return axis < 3 ? dim[axis]: interp.size (axis); }
78 default_type spacing (size_t axis) const { return axis < 3 ? vox[axis] : interp.spacing (axis); }
79 const std::string& name () const { return interp.name(); }
80
81 ssize_t stride (size_t axis) const {
82 return interp.stride (axis);
83 }
84
85 void reset () {
86 x[0] = x[1] = x[2] = 0;
87 for (size_t n = 3; n < interp.ndim(); ++n)
88 interp.index(n) = 0;
89 }
90
91
92 value_type value () {
93 Eigen::Vector3d pos = get_position();
94 if (std::isnan(pos[0]) || std::isnan(pos[1]) || std::isnan(pos[2]))
95 return value_when_out_of_bounds;
96 interp.scanner (pos);
97 default_type val = interp.value();
98 if (jac_modulate && val != 0.0) {
99 for (size_t dim = 0; dim < 3; ++dim)
100 jacobian_adapter.index(dim) = x[dim];
101 val *= jacobian_adapter.value().template cast<default_type>().determinant();
102 }
103 return (value_type) val;
104 }
105
106 ssize_t get_index (size_t axis) const { return axis < 3 ? x[axis] : interp.index(axis); }
107 void move_index (size_t axis, ssize_t increment) {
108 if (axis < 3) x[axis] += increment;
109 else interp.index(axis) += increment;
110 }
111
112 private:
113
114 Eigen::Vector3d get_position (){
115 warp.index(0) = x[0];
116 warp.index(1) = x[1];
117 warp.index(2) = x[2];
118
119 return warp.row(3);
120 }
121
122 Interpolator<ImageType> interp;
123 WarpType warp;
124 ssize_t x[3];
125 const ssize_t dim[3];
126 const default_type vox[3];
127 const value_type value_when_out_of_bounds;
128 const bool jac_modulate;
129 Adapter::Jacobian<Image<default_type> > jacobian_adapter;
130 };
131
133
134 }
135}
136
137#endif
138
139
140
141
an Image providing interpolated values from another Image
Definition: warp.h:54
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
int axis
#define MEMALIGN(...)
Definition: types.h:185