Developer documentation
Version 3.0.3-105-gd3941f44
regrid.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
18#ifndef __adapter_regrid_h__
19#define __adapter_regrid_h__
20
21#include "image.h"
22#include "adapter/base.h"
23
24namespace MR
25{
26 namespace Adapter {
27
28 template <class ImageType>
29 class Regrid :
30 public Base<Regrid<ImageType>,ImageType>
32 public:
33
34 using base_type = Base<Regrid<ImageType>, ImageType>;
35 using value_type = typename ImageType::value_type;
36
37 using base_type::name;
38 using base_type::spacing;
39
40 template <class VectorType>
41 Regrid (const ImageType& original, const VectorType& from, const VectorType& size, const value_type fill = 0) :
42 base_type (original),
43 from_ (container_cast<decltype(from_)>(from)),
44 size_ (container_cast<decltype(size_)>(size)),
45 index_invalid_lower_upper ([&]{ vector<vector<ssize_t>> v; for (size_t d = 0; d < from_.size(); ++d) {
46 v.push_back (vector<ssize_t> {from_[d] < 0 ? -(ssize_t) from_[d] - 1 : -1, original.size(d) - from_[d]}); } return v; }()),
47 index_requires_bound_check ([&]{ vector<bool> v; for (size_t d = 0; d < from_.size(); ++d) {
48 v.push_back (from_[d] < 0 || size_[d] > original.size(d) - from_[d]); } return v; }()),
49 fill_ (fill),
50 transform_ (original.transform()),
51 index_ (ndim(), 0) {
52 assert(from_.size() == size_.size());
53 assert(from_.size() == ndim());
54
55 for (size_t n = 0; n < ndim(); ++n)
56 if (size_[n] < 0)
57 throw Exception("FIXME: negative size in Regrid adapter");
58
59 // adjust location of origin
60 for (size_t j = 0; j < 3; ++j)
61 for (size_t i = 0; i < 3; ++i)
62 transform_(i,3) += from[j] * spacing(j) * transform_(i,j);
63
64 }
65
66 void reset () {
67 for (size_t n = 0; n < ndim(); ++n)
68 set_pos (n, 0);
69 }
70
71 size_t ndim () const { return size_.size(); }
72 ssize_t size (size_t axis) const { return size_ [axis]; }
73 const transform_type& transform() const { return transform_; }
74
75 ssize_t get_index (size_t axis) const {
76 return index_requires_bound_check[axis] ? index_[axis] : parent().index(axis) - from_[axis];
77 }
78
79 void move_index (size_t axis, ssize_t increment) {
81 index_[axis] += increment;
82 if (increment > 0) {
84 parent().index(axis) = index_[axis] + from_[axis];
85 } else
87 parent().index(axis) = index_[axis] + from_[axis];
88 } else
89 parent().index(axis) += increment;
90 }
91
92 value_type value () {
93 for (size_t axis = 0; axis < index_.size(); ++axis)
96 return fill_;
97 return parent().value();
98 }
99
100 protected:
101 using base_type::parent;
105 const value_type fill_;
108 };
109
110 }
111}
112
113#endif
114
115
transform_type transform_
Definition: regrid.h:106
const vector< ssize_t > from_
Definition: regrid.h:102
vector< ssize_t > index_
Definition: regrid.h:107
const vector< vector< ssize_t > > index_invalid_lower_upper
Definition: regrid.h:103
const vector< bool > index_requires_bound_check
Definition: regrid.h:104
const value_type fill_
Definition: regrid.h:105
const vector< ssize_t > size_
Definition: regrid.h:102
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
Eigen::Transform< default_type, 3, Eigen::AffineCompact > transform_type
the type for the affine transform of an image:
Definition: types.h:234
int axis
#define MEMALIGN(...)
Definition: types.h:185
const std::string name
Definition: thread.h:108