Developer documentation
Version 3.0.3-105-gd3941f44
nearest.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_nearest_h__
18#define __interp_nearest_h__
19
20#include "datatype.h"
21#include "image_helpers.h"
22#include "types.h"
23#include "interp/base.h"
24
25namespace MR
26{
27 namespace Interp
28 {
29
31 // @{
32
34
67 template <class ImageType>
68 class Nearest : public Base<ImageType> { MEMALIGN(Nearest<ImageType>)
69 public:
70 using typename Base<ImageType>::value_type;
71
75
76 Nearest (const ImageType& parent, value_type value_when_out_of_bounds = Base<ImageType>::default_out_of_bounds_value()) :
77 Base<ImageType> (parent, value_when_out_of_bounds) { }
78
80
81 template <class VectorType>
82 bool voxel (const VectorType& pos) {
84 if (out_of_bounds)
85 return false;
86 index(0) = std::round (pos[0]);
87 index(1) = std::round (pos[1]);
88 index(2) = std::round (pos[2]);
89 return true;
90 }
91
93
94 template <class VectorType>
95 FORCE_INLINE bool image (const VectorType& pos) {
96 return voxel (Transform::voxelsize.inverse() * pos.template cast<default_type>());
97 }
98
100
101 template <class VectorType>
102 FORCE_INLINE bool scanner (const VectorType& pos) {
103 return voxel (Transform::scanner2voxel * pos.template cast<default_type>());
104 }
105
107
108 FORCE_INLINE value_type value () const {
109 if (out_of_bounds)
110 return out_of_bounds_value;
111 return ImageType::value();
112 }
113
115
116 Eigen::Matrix<value_type, Eigen::Dynamic, 1> row (size_t axis) {
117 assert (axis > 2);
118 assert (axis < ImageType::ndim());
119 if (out_of_bounds) {
120 Eigen::Matrix<value_type, Eigen::Dynamic, 1> out_of_bounds_row (ImageType::size(axis));
121 out_of_bounds_row.setOnes();
122 out_of_bounds_row *= out_of_bounds_value;
123 return out_of_bounds_row;
124 }
125 return ImageType::row(axis);
126 }
127
128 };
129
130
131
132 template <class ImageType, typename... Args>
133 inline Nearest<ImageType> make_nearest (const ImageType& parent, Args&&... args) {
134 return Nearest<ImageType> (parent, std::forward<Args> (args)...);
135 }
136
137
138
140
141 }
142}
143
144#endif
145
146
This class defines the interface for classes that perform image interpolation.
Definition: base.h:70
bool out_of_bounds
Definition: base.h:195
Eigen::Vector3d intravoxel_offset(const VectorType &pos)
Definition: base.h:211
This class provides access to the voxel intensities of an Image, using nearest-neighbour interpolatio...
Definition: nearest.h:68
const Eigen::DiagonalMatrix< default_type, 3 > voxelsize
Definition: transform.h:42
const transform_type scanner2voxel
Definition: transform.h:43
constexpr I round(const T x)
Definition: math.h:64
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Definition: SH.h:233
Nearest< ImageType > make_nearest(const ImageType &parent, Args &&... args)
Definition: nearest.h:133
Definition: base.h:24
int axis
size_t index
#define MEMALIGN(...)
Definition: types.h:185
#define FORCE_INLINE
Definition: types.h:156