Developer documentation
Version 3.0.3-105-gd3941f44
normalise3D.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 __image_adapter_normalise3D_h__
18#define __image_adapter_normalise3D_h__
19
20#include "adapter/base.h"
21
22namespace MR
23{
24 namespace Adapter
25 {
26
27
28 template <class ImageType>
30 public Base<Normalise3D<ImageType>,ImageType>
32 public:
33
34 using base_type = Base<Normalise3D<ImageType>, ImageType>;
35 using value_type = typename ImageType::value_type;
36 using voxel_type = Normalise3D;
37
38 using base_type::name;
39 using base_type::size;
40 using base_type::index;
41
42 Normalise3D (const ImageType& parent) :
43 base_type (parent) {
44 set_extent (vector<int>(1,3));
45 }
46
47 Normalise3D (const ImageType& parent, const vector<uint32_t>& extent) :
48 base_type (parent) {
49 set_extent (extent);
50 }
51
52 void set_extent (const vector<uint32_t>& ext)
53 {
54 for (size_t i = 0; i < ext.size(); ++i)
55 if (! (ext[i] & uint32_t(1)))
56 throw Exception ("expected odd number for extent");
57 if (ext.size() != 1 && ext.size() != 3)
58 throw Exception ("unexpected number of elements specified in extent");
59 if (ext.size() == 1)
60 extent = vector<uint32_t> (3, ext[0]);
61 else
62 extent = ext;
63
64 DEBUG ("normalise3D adapter for image \"" + name() + "\" initialised with extent " + str(extent));
65
66 for (size_t i = 0; i < 3; ++i)
67 extent[i] = (extent[i]-1)/2;
68 }
69
70
71
72 value_type value ()
73 {
74 const ssize_t old_pos [3] = { index(0), index(1), index(2) };
76 nelements = 0;
77
78 const ssize_t from[3] = {
79 index(0) < extent[0] ? 0 : index(0) - extent[0],
80 index(1) < extent[1] ? 0 : index(1) - extent[1],
81 index(2) < extent[2] ? 0 : index(2) - extent[2]
82 };
83 const ssize_t to[3] = {
84 index(0) >= size(0)-extent[0] ? size(0) : index(0)+extent[0]+1,
85 index(1) >= size(1)-extent[1] ? size(1) : index(1)+extent[1]+1,
86 index(2) >= size(2)-extent[2] ? size(2) : index(2)+extent[2]+1
87 };
88
89 mean = 0.0;
90
91 for (index(2) = from[2]; index(2) < to[2]; ++index(2))
92 for (index(1) = from[1]; index(1) < to[1]; ++index(1))
93 for (index(0) = from[0]; index(0) < to[0]; ++index(0)){
95 nelements += 1;
96 }
97 mean /= nelements;
98
99 index(0) = old_pos[0];
100 index(1) = old_pos[1];
101 index(2) = old_pos[2];
102
103 return pos_value - mean;
104 }
105
106 protected:
108 value_type mean;
109 value_type pos_value;
110 size_t nelements;
111 };
112
113 }
114}
115
116
117#endif
118
vector< uint32_t > extent
Definition: normalise3D.h:107
#define DEBUG(msg)
Definition: exception.h:75
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
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
T to(const std::string &string)
Definition: mrtrix.h:260
size_t index
#define MEMALIGN(...)
Definition: types.h:185
const std::string name
Definition: thread.h:108