Developer documentation
Version 3.0.3-105-gd3941f44
gaussian1D.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_gaussian1D_h__
18#define __image_adapter_gaussian1D_h__
19
20#include "adapter/base.h"
21
22namespace MR
23{
24 namespace Adapter
25 {
26
27 template <class ImageType>
28 class Gaussian1D :
29 public Base<Gaussian1D<ImageType>,ImageType>
31 public:
32
33 using base_type = Base<Gaussian1D<ImageType>, ImageType>;
34 using value_type = typename ImageType::value_type;
35
36 using base_type::name;
37 using base_type::size;
38 using base_type::spacing;
39 using base_type::index;
40
41
42 Gaussian1D (const ImageType& parent,
43 default_type stdev_in = 1.0,
44 size_t axis_in = 0,
45 size_t extent = 0,
46 bool zero_boundary = false) :
47 base_type (parent),
48 stdev (stdev_in),
49 axis (axis_in),
51 if (!extent)
52 radius = ceil(2 * stdev / spacing(axis));
53 else if (extent == 1)
54 radius = 0;
55 else
56 radius = (extent - 1) / 2;
58 }
59
60
61 value_type value ()
62 {
63 if (!kernel.size())
64 return base_type::value();
65
66 const ssize_t pos = index (axis);
67
68 if (zero_boundary)
69 if (pos == 0 || pos == size(axis) - 1)
70 return 0.0;
71
72 const ssize_t from = (pos < radius) ? 0 : pos - radius;
73 const ssize_t to = (pos + radius) >= size(axis) ? size(axis) - 1 : pos + radius;
74
75 value_type result = 0.0;
76 value_type av_weights = 0.0;
77 ssize_t c = (pos < radius) ? radius - pos : 0;
78 for (ssize_t k = from; k <= to; ++k, ++c) {
79 index (axis) = k;
80 value_type neighbour_value = base_type::value();
81 if (std::isfinite (neighbour_value)) {
82 av_weights += kernel[c];
83 result += value_type (base_type::value()) * kernel[c];
84 }
85 }
86 result /= av_weights;
87
88 index (axis) = pos;
89 return result;
90 }
91
92 protected:
93
95 {
96 if ((radius < 1) || stdev <= 0.0)
97 return;
98 kernel.resize (2 * radius + 1);
99 default_type norm_factor = 0.0;
100 for (size_t c = 0; c < kernel.size(); ++c) {
101 kernel[c] = exp(-((c-radius) * (c-radius) * spacing(axis) * spacing(axis)) / (2 * stdev * stdev));
102 norm_factor += kernel[c];
103 }
104 for (size_t c = 0; c < kernel.size(); c++) {
105 kernel[c] /= norm_factor;
106 }
107 }
108
110 ssize_t radius;
111 size_t axis;
113 const bool zero_boundary;
114 };
115 }
116}
117
118
119#endif
120
vector< default_type > kernel
Definition: gaussian1D.h:112
const bool zero_boundary
Definition: gaussian1D.h:113
constexpr I ceil(const T x)
template function with cast to different type
Definition: math.h:86
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
double default_type
the default type used throughout MRtrix
Definition: types.h:228
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