Developer documentation
Version 3.0.3-105-gd3941f44
voxel2vector.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 __misc_voxel2vector_h__
19#define __misc_voxel2vector_h__
20
21#include <limits>
22
23#include "exception.h"
24#include "header.h"
25#include "image.h"
26#include "algo/loop.h"
27#include "types.h"
28
29#include "adapter/replicate.h"
30
31
32namespace MR
33{
34
35
36
37 // A class to achieve a mapping from a voxel position in an image
38 // with any number of axes, to an index within a 1D vector of data.
41 public:
42
43 typedef uint32_t index_t;
44
45 static const index_t invalid = std::numeric_limits<index_t>::max();
46
47 template <class MaskType>
48 Voxel2Vector (MaskType& mask, const Header& data);
49
50 template <class MaskType>
51 Voxel2Vector (MaskType& mask) :
52 Voxel2Vector (mask, Header(mask)) { }
53
54 size_t size() const { return reverse.size(); }
55
56 const vector<index_t>& operator[] (const size_t index) const {
57 assert (index < reverse.size());
58 return reverse[index];
59 }
60
61 template <class PosType>
62 index_t operator() (const PosType& pos) const {
63 Image<index_t> temp (forward); // For thread-safety
64 assign_pos_of (pos).to (temp);
65 if (is_out_of_bounds (temp))
66 return invalid;
67 return temp.value();
68 }
69
70 private:
71 Image<index_t> forward;
73 };
74
75
76
77 template <class MaskType>
78 Voxel2Vector::Voxel2Vector (MaskType& mask, const Header& data) :
79 forward (Image<index_t>::scratch (data, "Voxel to vector index conversion scratch image"))
80 {
81 if (!dimensions_match (mask, data, 0, std::min (mask.ndim(), data.ndim())))
82 throw Exception ("Dimension mismatch between image data and processing mask");
83 // E.g. Mask may be 3D but data are 4D; for any voxel where the mask is
84 // true, want to include data from all volumes
85 Adapter::Replicate<MaskType> r_mask (mask, data);
86 // Loop in axis order so that those voxels contiguous in memory are still
87 // contiguous in the vectorised data
88 index_t counter = 0;
89 for (auto l = Loop(data) (r_mask, forward); l; ++l) {
90 if (r_mask.value()) {
91 forward.value() = counter++;
93 for (size_t index = 0; index != data.ndim(); ++index)
94 pos.push_back (forward.index(index));
95 reverse.push_back (pos);
96 } else {
97 forward.value() = invalid;
98 }
99 }
100 DEBUG ("Voxel2Vector class has " + str(reverse.size()) + " non-zero entries");
101 }
102
103
104
105}
106
107
108#endif
#define DEBUG(msg)
Definition: exception.h:75
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
size_t index
#define MEMALIGN(...)
Definition: types.h:185