Developer documentation
Version 3.0.3-105-gd3941f44
neighbourhooditerator.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 __algo_neighbourhooditerator_h__
18#define __algo_neighbourhooditerator_h__
19
20#include "types.h"
21#include "algo/iterator.h"
22
23namespace MR
24{
25
30 // Does not work properly with Loop() functions! Use instead:
31 //
32 // vector extent(iter.ndim(),3) // max number of voxels to iterate over
33 // auto niter = NeighbourhoodIterator(iter, extent);
34 // while(niter.loop()){
35 // std::cerr << niter << std::endl;
36 // }
37 //
38 //
41 public:
42 NeighbourhoodIterator() = delete;
43 template <class IteratorType>
44 NeighbourhoodIterator (const IteratorType& iter, const vector<size_t>& extent) :
45 dim (iter.ndim()),
46 offset (iter.ndim()),
47 // pos (iter.ndim()),
48 pos_orig (iter.ndim()),
49 ext (container_cast<decltype(ext)>(extent)),
50 has_next_(false) {
51 assert (iter.ndim() == extent.size());
52 pos.resize(iter.ndim());
53 for (size_t i = 0; i < iter.ndim(); ++i){
54 ext[i] = (ext[i]-1) / 2;
55 offset[i] = iter.index(i);
56 // set pos to lower bound
57 pos[i] = (offset[i] - ext[i] < 0) ? 0 : offset[i] - ext[i];
58 pos_orig[i] = pos[i];
59 // upper bound:
60 auto high = (offset[i] + ext[i] >= iter.size(i)) ? iter.size(i) - 1 : offset[i] + ext[i];
61 dim[i] = high - pos[i] + 1;
62 }
63 }
64
65 size_t ndim () const { return dim.size(); }
66 ssize_t size (size_t axis) const { return dim[axis]; }
67
68 const ssize_t& index (size_t axis) const { return pos[axis]; }
69 ssize_t& index (size_t axis) { return pos[axis]; }
70
71 const Eigen::Matrix< ssize_t, 1, Eigen::Dynamic > get_pos() const { return pos; }
72
73 const ssize_t& extent (size_t axis) const { return dim[axis]; }
74 const ssize_t& centre (size_t axis) const { return offset[axis]; }
75
76 void reset (size_t axis) { pos[axis] = pos_orig[axis]; }
77
78 bool loop () {
79 if (not this->has_next_){
80 this->has_next_ = true;
81 for (auto axis = dim.size(); axis-- != 0; )
82 this->reset(axis);
83 return true;
84 }
85 for (auto axis = dim.size(); axis-- != 0; ) { // TODO loop in stride order
86 ++pos[axis];
87 if (pos[axis] != (pos_orig[axis] + dim[axis]))
88 return true;
89 this->reset(axis);
90 }
91 this->has_next_ = false;
92 return false;
93 }
94
95 friend std::ostream& operator<< (std::ostream& stream, const NeighbourhoodIterator& V) {
96 stream << "neighbourhood iterator, position [ ";
97 for (size_t n = 0; n < V.ndim(); ++n)
98 stream << V.index(n) << " ";
99 stream << "], extent [ ";
100 for (size_t n = 0; n < V.ndim(); ++n)
101 stream << V.extent(n) << " ";
102 stream << "], centre [ ";
103 for (size_t n = 0; n < V.ndim(); ++n)
104 stream << V.centre(n) << " ";
105 stream << "]";
106 return stream;
107 }
108
109
110 private:
111 vector<ssize_t> dim, offset, pos_orig, ext;
112 Eigen::Matrix< ssize_t, 1, Eigen::Dynamic > pos;
113 bool has_next_;
114
115 void value () const { assert (0); }
116 };
117
119}
120
121#endif
122
123
124
a dummy image to iterate over a certain neighbourhood, useful for multi-threaded looping.
friend std::ostream & operator<<(std::ostream &stream, const NeighbourhoodIterator &V)
Definition: base.h:24
int axis
#define MEMALIGN(...)
Definition: types.h:185