Developer documentation
Version 3.0.3-105-gd3941f44
bootstrap.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 __dwi_bootstrap_h__
18#define __dwi_bootstrap_h__
19
20#include "adapter/base.h"
21
22
23namespace MR {
24 namespace DWI {
25
26
27 template <class ImageType, class Functor, size_t NUM_VOX_PER_CHUNK = 256>
28 class Bootstrap :
29 public Adapter::Base<Bootstrap<ImageType,Functor,NUM_VOX_PER_CHUNK>,ImageType>
31 public:
32
34 using value_type = typename ImageType::value_type;
35
36 using base_type::ndim;
37 using base_type::size;
38 using base_type::index;
39
40 class IndexCompare { NOMEMALIGN
41 public:
42 bool operator() (const Eigen::Vector3i& a, const Eigen::Vector3i& b) const {
43 if (a[0] < b[0]) return true;
44 if (a[1] < b[1]) return true;
45 if (a[2] < b[2]) return true;
46 return false;
47 }
48 };
49
50
51 Bootstrap (const ImageType& Image, const Functor& functor) :
52 base_type (Image),
53 func (functor),
54 next_voxel (nullptr),
55 last_voxel (nullptr) {
56 assert (ndim() == 4);
57 }
58
59 value_type value () {
60 return get_voxel()[index(3)];
61 }
62
63 template <class VectorType>
64 void get_values (VectorType& values) {
65 if (index(0) < 0 || index(0) >= size(0) ||
66 index(1) < 0 || index(1) >= size(1) ||
67 index(2) < 0 || index(2) >= size(2))
68 values.setZero();
69 else {
70 auto p = get_voxel();
71 for (ssize_t n = 0; n < size(3); ++n)
72 values[n] = p[n];
73 }
74 }
75
76 void clear ()
77 {
78 voxels.clear();
79 if (voxel_buffer.empty())
80 voxel_buffer.push_back (vector<value_type> (NUM_VOX_PER_CHUNK * size(3)));
81 next_voxel = &voxel_buffer[0][0];
82 last_voxel = next_voxel + NUM_VOX_PER_CHUNK * size(3);
83 current_chunk = 0;
84 }
85
86 protected:
87 Functor func;
88 std::map<Eigen::Vector3i,value_type*,IndexCompare> voxels;
90 value_type* next_voxel;
91 value_type* last_voxel;
93
94 value_type* allocate_voxel ()
95 {
96 if (next_voxel == last_voxel) {
98 if (current_chunk >= voxel_buffer.size())
99 voxel_buffer.push_back (vector<value_type> (NUM_VOX_PER_CHUNK * size(3)));
100 assert (current_chunk < voxel_buffer.size());
101 next_voxel = &voxel_buffer.back()[0];
102 last_voxel = next_voxel + NUM_VOX_PER_CHUNK * size(3);
103 }
104 value_type* retval = next_voxel;
105 next_voxel += size(3);
106 return retval;
107 }
108
109 value_type* get_voxel ()
110 {
111 value_type*& data (voxels.insert (std::make_pair (Eigen::Vector3i (index(0), index(1), index(2)), nullptr)).first->second);
112 if (!data) {
113 data = allocate_voxel ();
114 ssize_t pos = index(3);
115 for (auto l = Loop(3)(*this); l; ++l)
116 data[index(3)] = base_type::value();
117 index(3) = pos;
118 func (data);
119 }
120 return data;
121 }
122 };
123
124 }
125}
126
127#endif
128
129
130
131
value_type * allocate_voxel()
Definition: bootstrap.h:94
value_type * last_voxel
Definition: bootstrap.h:91
size_t current_chunk
Definition: bootstrap.h:92
std::map< Eigen::Vector3i, value_type *, IndexCompare > voxels
Definition: bootstrap.h:88
value_type * get_voxel()
Definition: bootstrap.h:109
value_type * next_voxel
Definition: bootstrap.h:90
vector< vector< value_type > > voxel_buffer
Definition: bootstrap.h:89
functions and classes related to image data input/output
Definition: image.h:41
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
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
#define NOMEMALIGN
Definition: memory.h:22
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
size_t index
#define MEMALIGN(...)
Definition: types.h:185
std::remove_reference< Functor >::type & functor
Definition: thread.h:215