Developer documentation
Version 3.0.3-105-gd3941f44
extract.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_extract_h__
18#define __adapter_extract_h__
19
20#include "adapter/base.h"
21
22namespace MR
23{
24 namespace Adapter
25 {
26
27 template <class ImageType>
28 class Extract1D :
29 public Base<Extract1D<ImageType>,ImageType>
31 public:
32
33 using base_type = Base<Extract1D<ImageType>, ImageType>;
34 using value_type = typename ImageType::value_type;
35
36 using base_type::ndim;
37 using base_type::spacing;
38 using base_type::parent;
39
40
41 Extract1D (const ImageType& original, const size_t axis, const vector<uint32_t>& indices) :
42 base_type (original),
43 extract_axis (axis),
44 indices (indices),
45 nsize (indices.size()),
46 trans (original.transform()) {
47 reset();
48
49 if (extract_axis < 3) {
50 Eigen::Vector3d a (0.0, 0.0, 0.0);
51 a[extract_axis] = indices[0] * spacing (extract_axis);
52 trans.translation() = trans * a;
53 }
54
55 this->indices.push_back (indices.back());
56 }
57
58
59 void reset () {
60 for (size_t n = 0; n < ndim(); ++n)
61 parent().index(n) = ( n == extract_axis ? indices[0] : 0 );
62 current_pos = 0;
63 }
64
65 ssize_t size (size_t axis) const {
66 return ( axis == extract_axis ? nsize : base_type::size (axis) );
67 }
68
69 const transform_type& transform () const { return trans; }
70
71 ssize_t get_index (size_t axis) const { return ( axis == extract_axis ? current_pos : parent().index(axis) ); }
72 void move_index (size_t axis, ssize_t increment) {
73 if (axis == extract_axis) {
74 ssize_t prev_pos = current_pos < nsize ? indices[current_pos] : 0;
75 current_pos += increment;
76 if (current_pos < nsize)
77 parent().index(axis) += indices[current_pos] - prev_pos;
78 else
79 parent().index(axis) = 0;
80 }
81 else
82 parent().index(axis) += increment;
83 }
84
85
86 friend std::ostream& operator<< (std::ostream& stream, const Extract1D& V) {
87 stream << "Extract1D adapter for image \"" << V.name() << "\", position [ ";
88 for (size_t n = 0; n < V.ndim(); ++n)
89 stream << V.index(n) << " ";
90 stream << "], value = " << V.value();
91 return stream;
92 }
93
94 private:
95 const size_t extract_axis;
96 vector<uint32_t> indices;
97 const ssize_t nsize;
98 transform_type trans;
99 ssize_t current_pos;
100
101 };
102
103
104
105
106
107
108
109
110
111
112 template <class ImageType>
113 class Extract :
114 public Base<Extract<ImageType>,ImageType>
116 public:
117
118 using base_type = Base<Extract<ImageType>, ImageType>;
119 using value_type = typename ImageType::value_type;
120
121 using base_type::ndim;
122 using base_type::spacing;
123 using base_type::parent;
124
125
126 Extract (const ImageType& original, const vector<vector<uint32_t>>& indices) :
127 base_type (original),
128 current_pos (ndim()),
129 indices (indices),
130 trans (original.transform()) {
131 reset();
132 trans.translation() = trans * Eigen::Vector3d (
133 indices[0][0] * spacing (0),
134 indices[1][0] * spacing (1),
135 indices[2][0] * spacing (2)
136 );
137
138 for (const auto& i : indices)
139 sizes.push_back (i.size());
140 }
141
142
143 ssize_t size (size_t axis) const { return sizes[axis]; }
144
145 const transform_type& transform () const { return trans; }
146
147 void reset () {
148 for (size_t n = 0; n < ndim(); ++n) {
149 current_pos[n] = 0;
150 parent().index(n) = indices[n][0];
151 }
152 }
153
154 ssize_t get_index (size_t axis) const { return current_pos[axis]; }
155 void move_index (size_t axis, ssize_t increment) {
156 current_pos[axis] += increment;
157 if (current_pos[axis] < 0)
158 parent().index (axis) = -1;
159 else if (current_pos[axis] >= sizes[axis])
160 parent().index (axis) = parent().size (axis);
161 else
162 parent().index (axis) = indices[axis][current_pos[axis]];
163 }
164
165 private:
166 vector<ssize_t> current_pos;
167 vector<vector<uint32_t> > indices;
168 vector<ssize_t> sizes;
169 transform_type trans;
170 };
171
172 }
173}
174
175#endif
176
177
friend std::ostream & operator<<(std::ostream &stream, const Extract1D &V)
Definition: extract.h:86
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
size_t index
#define MEMALIGN(...)
Definition: types.h:185