STIR 6.4.0
FastErf.inl
Go to the documentation of this file.
1
9/*
10 Copyright (C) 2022, University College London
11 This file is part of STIR.
12 SPDX-License-Identifier: Apache-2.0
13 See STIR/LICENSE.txt for details
14*/
15
17#include "stir/numerics/erf.h"
18
19START_NAMESPACE_STIR
20
21void
22FastErf::set_up()
23{
24 this->_sampling_period = (2 * this->_maximum_sample_value) / this->get_num_samples();
25
26 // Add two samples for end cases and compute a vector of erf values
27 std::vector<double> erf_values(this->get_num_samples() + 2);
28 for (int i = 0; i < this->get_num_samples() + 2; ++i)
29 erf_values[i] = erf(i * this->_sampling_period - this->_maximum_sample_value);
30
31 // Setup BSplines
32 BSpline::BSplines1DRegularGrid<double, double> spline(erf_values, BSpline::linear);
33 this->_spline = spline;
34
35 erf_values_vec = erf_values;
36 // this->_is_setup = true;
37}
38
39double
40FastErf::get_erf_BSplines_interpolation(double xp) const
41{
42#if 0
43 xp = std::clamp(xp,-this->_maximum_sample_value, this->_maximum_sample_value);
44#else
45 xp = std::max(std::min(this->_maximum_sample_value, xp), -this->_maximum_sample_value);
46#endif
47 return this->_spline.BSplines((xp + this->_maximum_sample_value) / this->_sampling_period);
48}
49
50double
51FastErf::get_erf_linear_interpolation(double xp) const
52{
53#if 0
54 xp = std::clamp(xp,-this->_maximum_sample_value, this->_maximum_sample_value);
55#else
56 xp = std::max(std::min(this->_maximum_sample_value, xp), -this->_maximum_sample_value);
57#endif
58 // Find xp in index sequence
59 double xp_in_index = ((xp + this->_maximum_sample_value) / this->_sampling_period);
60
61 // Find lower integer in index space
62 int lower = static_cast<int>(floor(xp_in_index));
63
64 // Linear interpolation of xp between vec[lower] and vec[lower + 1]
65 return erf_values_vec[lower] + (xp_in_index - lower) * (erf_values_vec[lower + 1] - erf_values_vec[lower]);
66}
67
68double
69FastErf::get_erf_nearest_neighbour_interpolation(double xp) const
70{
71#if 0
72 xp = std::clamp(xp,-this->_maximum_sample_value, this->_maximum_sample_value);
73#else
74 xp = std::max(std::min(this->_maximum_sample_value, xp), -this->_maximum_sample_value);
75#endif
76 // Selects index of the nearest neighbour via rounding
77 return erf_values_vec[static_cast<int>(std::round((xp + this->_maximum_sample_value) / this->_sampling_period))];
78}
79
80void
81FastErf::set_num_samples(const int num_samples)
82{
83 this->_num_samples = num_samples;
84}
85
86int
87FastErf::get_num_samples() const
88{
89 return this->_num_samples;
90}
91
92double
93FastErf::get_maximum_sample_value() const
94{
95 return this->_maximum_sample_value;
96}
97
98void
99FastErf::set_maximum_sample_value(double maximum_sample_value)
100{
101 this->_maximum_sample_value = maximum_sample_value;
102}
103
104const double
105FastErf::operator()(const double xp) const
106{
107 return get_erf_linear_interpolation(xp);
108}
109
110END_NAMESPACE_STIR
Implementation of the B-Splines Interpolation.