Developer documentation
Version 3.0.3-105-gd3941f44
sinc.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 __interp_sinc_h__
18#define __interp_sinc_h__
19
20#include "types.h"
21#include "interp/base.h"
22#include "math/sinc.h"
23
24
25#define SINC_WINDOW_SIZE 7
26
27
28namespace MR
29{
30 namespace Interp
31 {
32
34 // @{
35
37
70 template <class ImageType> class Sinc : public Base<ImageType>
72 public:
73 using typename Base<ImageType>::value_type;
76
77 Sinc (const ImageType& parent, value_type value_when_out_of_bounds = Base<ImageType>::default_out_of_bounds_value(), const size_t w = SINC_WINDOW_SIZE) :
78 Base<ImageType> (parent, value_when_out_of_bounds),
79 window_size (w),
81 Sinc_x (w),
82 Sinc_y (w),
83 Sinc_z (w),
84 y_values (w, 0.0),
85 z_values (w, 0.0)
86 {
87 assert (w % 2);
88 }
89
90
92
93 template <class VectorType>
94 bool voxel (const VectorType& pos) {
96 return false;
97 Sinc_x.set (*this, 0, pos[0]);
98 Sinc_y.set (*this, 1, pos[1]);
99 Sinc_z.set (*this, 2, pos[2]);
100 return true;
101 }
102
104
105 template <class VectorType>
106 FORCE_INLINE bool image (const VectorType& pos) {
107 return voxel (Transform::voxelsize.inverse() * pos.template cast<default_type>());
108 }
109
111
112 template <class VectorType>
113 FORCE_INLINE bool scanner (const VectorType& pos) {
114 return voxel (Transform::scanner2voxel * pos.template cast<default_type>());
115 }
116
118
119 FORCE_INLINE value_type value () {
120 if (out_of_bounds)
121 return out_of_bounds_value;
122 for (size_t z = 0; z != window_size; ++z) {
124 for (size_t y = 0; y != window_size; ++y) {
126 // Cast necessary so that Sinc_x calls the ImageType value() function
127 y_values[y] = Sinc_x.value (static_cast<ImageType&>(*this), 0);
128 }
130 }
131 return Sinc_z.value (z_values);
132 }
133
135
136 Eigen::Matrix<value_type, Eigen::Dynamic, 1> row (size_t axis) {
137 assert (axis > 2);
138 assert (axis < ImageType::ndim());
139 if (out_of_bounds) {
140 Eigen::Matrix<value_type, Eigen::Dynamic, 1> out_of_bounds_row (ImageType::size(axis));
141 out_of_bounds_row.setOnes();
142 out_of_bounds_row *= out_of_bounds_value;
143 return out_of_bounds_row;
144 }
145
146 Eigen::Matrix<value_type, Eigen::Dynamic, 1> row (ImageType::size(axis));
147 // Lazy, non-optimized code, since nothing is actually using this yet
148 // Just make use of the kernel setup within voxel()
149 for (ssize_t volume = 0; volume != ImageType::size(axis); ++volume) {
150 ImageType::index (axis) = volume;
151 row (volume,0) = value();
152 }
153 return row;
154 }
155
156
157 protected:
158 const size_t window_size;
159 const int kernel_width;
162
163 };
164
165
166
167
168
169 template <class ImageType, typename... Args>
170 inline Sinc<ImageType> make_sinc (const ImageType& parent, Args&&... args) {
171 return Sinc<ImageType> (parent, std::forward<Args> (args)...);
172 }
173
174
176
177 }
178}
179
180#endif
181
182
This class defines the interface for classes that perform image interpolation.
Definition: base.h:70
bool out_of_bounds
Definition: base.h:195
This class provides access to the voxel intensities of an image, using sinc interpolation.
Definition: sinc.h:71
Math::Sinc< value_type > Sinc_y
Definition: sinc.h:160
const size_t window_size
Definition: sinc.h:158
Math::Sinc< value_type > Sinc_z
Definition: sinc.h:160
vector< value_type > z_values
Definition: sinc.h:161
vector< value_type > y_values
Definition: sinc.h:161
Math::Sinc< value_type > Sinc_x
Definition: sinc.h:160
const int kernel_width
Definition: sinc.h:159
void set(const ImageType &image, const size_t axis, const value_type position)
Definition: sinc.h:43
value_type value(ImageType &image, const size_t axis) const
Definition: sinc.h:95
size_t index(const size_t i) const
Definition: sinc.h:92
const Eigen::DiagonalMatrix< default_type, 3 > voxelsize
Definition: transform.h:42
const transform_type scanner2voxel
Definition: transform.h:43
#define SINC_WINDOW_SIZE
Definition: sinc.h:25
Sinc< ImageType > make_sinc(const ImageType &parent, Args &&... args)
Definition: sinc.h:170
Definition: base.h:24
int axis
size_t index
#define MEMALIGN(...)
Definition: types.h:185
#define FORCE_INLINE
Definition: types.h:156