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 <vector>
7
8#include "BaseLib/Logging.h"
9
10namespace MathLib
11{
16template <typename T = double>
18{
19public:
36 std::vector<T> const& supporting_points,
37 std::vector<double> const& values_at_supp_pnts)
38 : supp_pnts_(supporting_points),
39 values_at_supp_pnts_(values_at_supp_pnts)
40 {
41 if (supp_pnts_.size() != values_at_supp_pnts_.size())
42 {
43 OGS_FATAL(
44 "Inconsistent data given to PiecewiseConstantInterpolation, "
45 "number of given supporting points is {}, number of given "
46 "values is {}.",
47 supp_pnts_.size(), values_at_supp_pnts_.size());
48 }
49 if (supp_pnts_.empty())
50 {
51 ERR("PiecewiseConstantInterpolation: passed empty vector.");
52 }
53 }
54
67 double value(double const pnt_to_interpolate) const
68 {
69 if (pnt_to_interpolate <= supp_pnts_.front())
70 {
71 return values_at_supp_pnts_.front();
72 }
73
74 if (supp_pnts_.back() <= pnt_to_interpolate)
75 {
76 return values_at_supp_pnts_.back();
77 }
78
79 auto const& it(std::upper_bound(supp_pnts_.begin(), supp_pnts_.end(),
80 pnt_to_interpolate));
81 // Here access the iterator it without checking is okay since the
82 // corner cases are checked above.
83 auto const interval_idx = std::distance(supp_pnts_.begin(), it) - 1;
84
85 return values_at_supp_pnts_[interval_idx];
86 }
87
88private:
89 std::vector<T> const& supp_pnts_;
90 std::vector<double> const& values_at_supp_pnts_;
91};
92} // 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)