OGS
PiecewiseConstantInterpolation.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <algorithm>
7#include <vector>
8
9#include "BaseLib/Logging.h"
10
11namespace MathLib
12{
17template <typename T = double>
19{
20public:
37 std::vector<T> const& supporting_points,
38 std::vector<double> const& values_at_supp_pnts)
39 : supp_pnts_(supporting_points),
40 values_at_supp_pnts_(values_at_supp_pnts)
41 {
42 if (supp_pnts_.size() != values_at_supp_pnts_.size())
43 {
44 OGS_FATAL(
45 "Inconsistent data given to PiecewiseConstantInterpolation, "
46 "number of given supporting points is {}, number of given "
47 "values is {}.",
48 supp_pnts_.size(), values_at_supp_pnts_.size());
49 }
50 if (supp_pnts_.empty())
51 {
52 ERR("PiecewiseConstantInterpolation: passed empty vector.");
53 }
54 }
55
68 double value(double const pnt_to_interpolate) const
69 {
70 if (pnt_to_interpolate <= supp_pnts_.front())
71 {
72 return values_at_supp_pnts_.front();
73 }
74
75 if (supp_pnts_.back() <= pnt_to_interpolate)
76 {
77 return values_at_supp_pnts_.back();
78 }
79
80 auto const& it(std::upper_bound(supp_pnts_.begin(), supp_pnts_.end(),
81 pnt_to_interpolate));
82 // Here access the iterator it without checking is okay since the
83 // corner cases are checked above.
84 auto const interval_idx = std::distance(supp_pnts_.begin(), it) - 1;
85
86 return values_at_supp_pnts_[interval_idx];
87 }
88
89private:
90 std::vector<T> const& supp_pnts_;
91 std::vector<double> const& values_at_supp_pnts_;
92};
93} // 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)