OGS
PiecewiseConstantInterpolation.h
Go to the documentation of this file.
1
13#pragma once
14
15#include <vector>
16
17#include "BaseLib/Logging.h"
18
19namespace MathLib
20{
25template <typename T = double>
27{
28public:
45 std::vector<T> const& supporting_points,
46 std::vector<double> const& values_at_supp_pnts)
47 : supp_pnts_(supporting_points),
48 values_at_supp_pnts_(values_at_supp_pnts)
49 {
50 if (supp_pnts_.size() != values_at_supp_pnts_.size())
51 {
52 OGS_FATAL(
53 "Inconsistent data given to PiecewiseConstantInterpolation, "
54 "number of given supporting points is {}, number of given "
55 "values is {}.",
56 supp_pnts_.size(), values_at_supp_pnts_.size());
57 }
58 if (supp_pnts_.empty())
59 {
60 ERR("PiecewiseConstantInterpolation: passed empty vector.");
61 }
62 }
63
76 double value(double const pnt_to_interpolate) const
77 {
78 if (pnt_to_interpolate <= supp_pnts_.front())
79 {
80 return values_at_supp_pnts_.front();
81 }
82
83 if (supp_pnts_.back() <= pnt_to_interpolate)
84 {
85 return values_at_supp_pnts_.back();
86 }
87
88 auto const& it(std::upper_bound(supp_pnts_.begin(), supp_pnts_.end(),
89 pnt_to_interpolate));
90 // Here access the iterator it without checking is okay since the
91 // corner cases are checked above.
92 auto const interval_idx = std::distance(supp_pnts_.begin(), it) - 1;
93
94 return values_at_supp_pnts_[interval_idx];
95 }
96
97private:
98 std::vector<T> const& supp_pnts_;
99 std::vector<double> const& values_at_supp_pnts_;
100};
101} // end namespace MathLib
double value(double const pnt_to_interpolate) const
Calculates the interpolation value.
PiecewiseConstantInterpolation(std::vector< T > const &supporting_points, std::vector< double > const &values_at_supp_pnts)