| 1 |
// Copyright Nick Thompson, 2021 |
| 2 |
// Use, modification and distribution are subject to the |
| 3 |
// Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE_1_0.txt |
| 5 |
// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_MATH_INTERPOLATORS_BILINEAR_UNIFORM_DETAIL_HPP |
| 8 |
#define BOOST_MATH_INTERPOLATORS_BILINEAR_UNIFORM_DETAIL_HPP |
| 9 |
|
| 10 |
#include <stdexcept> |
| 11 |
#include <iostream> |
| 12 |
#include <string> |
| 13 |
#include <limits> |
| 14 |
#include <cmath> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
namespace boost::math::interpolators::detail { |
| 18 |
|
| 19 |
template <class RandomAccessContainer> |
| 20 |
class bilinear_uniform_imp |
| 21 |
{ |
| 22 |
public: |
| 23 |
using Real = typename RandomAccessContainer::value_type; |
| 24 |
using Z = typename RandomAccessContainer::size_type; |
| 25 |
|
| 26 |
bilinear_uniform_imp(RandomAccessContainer && fieldData, Z rows, Z cols, Real dx = 1, Real dy = 1, Real x0 = 0, Real y0 = 0) |
| 27 |
{ |
| 28 |
using std::to_string; |
| 29 |
if(fieldData.size() != rows*cols) |
| 30 |
{ |
| 31 |
std::string err = std::string(__FILE__) + ":" + to_string(__LINE__) |
| 32 |
+ " The field data must have rows*cols elements. There are " + to_string(rows) + " rows and " + to_string(cols) + " columns but " + to_string(fieldData.size()) + " elements in the field data."; |
| 33 |
throw std::logic_error(err); |
| 34 |
} |
| 35 |
if (rows < 2) { |
| 36 |
throw std::logic_error("There must be at least two rows of data for bilinear interpolation to be well-defined."); |
| 37 |
} |
| 38 |
if (cols < 2) { |
| 39 |
throw std::logic_error("There must be at least two columns of data for bilinear interpolation to be well-defined."); |
| 40 |
} |
| 41 |
|
| 42 |
fieldData_ = std::move(fieldData); |
| 43 |
rows_ = rows; |
| 44 |
cols_ = cols; |
| 45 |
x0_ = x0; |
| 46 |
y0_ = y0; |
| 47 |
dx_ = dx; |
| 48 |
dy_ = dy; |
| 49 |
|
| 50 |
if (dx_ <= 0) { |
| 51 |
std::string err = std::string(__FILE__) + ":" + to_string(__LINE__) + " dx = " + to_string(dx) + ", but dx > 0 is required. Are the arguments out of order?"; |
| 52 |
throw std::logic_error(err); |
| 53 |
} |
| 54 |
if (dy_ <= 0) { |
| 55 |
std::string err = std::string(__FILE__) + ":" + to_string(__LINE__) + " dy = " + to_string(dy) + ", but dy > 0 is required. Are the arguments out of order?"; |
| 56 |
throw std::logic_error(err); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
Real operator()(Real x, Real y) const |
| 61 |
{ |
| 62 |
using std::floor; |
| 63 |
if (x > x0_ + (cols_ - 1)*dx_ || x < x0_) { |
| 64 |
std::cerr << __FILE__ << ":" << __LINE__ << ":" << __func__ << "\n"; |
| 65 |
std::cerr << "Querying the bilinear_uniform interpolator at (x,y) = (" << x << ", " << y << ") is not allowed.\n"; |
| 66 |
std::cerr << "x must lie in the interval [" << x0_ << ", " << x0_ + (cols_ -1)*dx_ << "]\n"; |
| 67 |
return std::numeric_limits<Real>::quiet_NaN(); |
| 68 |
} |
| 69 |
if (y > y0_ + (rows_ - 1)*dy_ || y < y0_) { |
| 70 |
std::cerr << __FILE__ << ":" << __LINE__ << ":" << __func__ << "\n"; |
| 71 |
std::cerr << "Querying the bilinear_uniform interpolator at (x,y) = (" << x << ", " << y << ") is not allowed.\n"; |
| 72 |
std::cerr << "y must lie in the interval [" << y0_ << ", " << y0_ + (rows_ -1)*dy_ << "]\n"; |
| 73 |
return std::numeric_limits<Real>::quiet_NaN(); |
| 74 |
} |
| 75 |
|
| 76 |
Real s = (x - x0_)/dx_; |
| 77 |
Real s0 = floor(s); |
| 78 |
Real t = (y - y0_)/dy_; |
| 79 |
Real t0 = floor(t); |
| 80 |
auto xidx = static_cast<Z>(s0); |
| 81 |
auto yidx = static_cast<Z>(t0); |
| 82 |
Z idx = yidx*cols_ + xidx; |
| 83 |
Real alpha = s - s0; |
| 84 |
Real beta = t - t0; |
| 85 |
|
| 86 |
Real fhi; |
| 87 |
// If alpha = 0, then we can segfault by reading fieldData_[idx+1]: |
| 88 |
if (alpha <= 2*s0*std::numeric_limits<Real>::epsilon()) { |
| 89 |
fhi = fieldData_[idx]; |
| 90 |
} else { |
| 91 |
fhi = (1 - alpha)*fieldData_[idx] + alpha*fieldData_[idx + 1]; |
| 92 |
} |
| 93 |
|
| 94 |
// Again, we can get OOB access without this check. |
| 95 |
// This corresponds to interpolation over a line segment aligned with the axes. |
| 96 |
if (beta <= 2*t0*std::numeric_limits<Real>::epsilon()) { |
| 97 |
return fhi; |
| 98 |
} |
| 99 |
|
| 100 |
auto bottom_left = fieldData_[idx + cols_]; |
| 101 |
Real flo; |
| 102 |
if (alpha <= 2*s0*std::numeric_limits<Real>::epsilon()) { |
| 103 |
flo = bottom_left; |
| 104 |
} |
| 105 |
else { |
| 106 |
flo = (1 - alpha)*bottom_left + alpha*fieldData_[idx + cols_ + 1]; |
| 107 |
} |
| 108 |
// Convex combination over vertical to get the value: |
| 109 |
return (1 - beta)*fhi + beta*flo; |
| 110 |
} |
| 111 |
|
| 112 |
friend std::ostream& operator<<(std::ostream& out, bilinear_uniform_imp<RandomAccessContainer> const & bu) { |
| 113 |
out << "(x0, y0) = (" << bu.x0_ << ", " << bu.y0_ << "), (dx, dy) = (" << bu.dx_ << ", " << bu.dy_ << "), "; |
| 114 |
out << "(xf, yf) = (" << bu.x0_ + (bu.cols_ - 1)*bu.dx_ << ", " << bu.y0_ + (bu.rows_ - 1)*bu.dy_ << ")\n"; |
| 115 |
for (Z j = 0; j < bu.rows_; ++j) { |
| 116 |
out << "{"; |
| 117 |
for (Z i = 0; i < bu.cols_ - 1; ++i) { |
| 118 |
out << bu.fieldData_[j*bu.cols_ + i] << ", "; |
| 119 |
} |
| 120 |
out << bu.fieldData_[j*bu.cols_ + bu.cols_ - 1] << "}\n"; |
| 121 |
} |
| 122 |
return out; |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
RandomAccessContainer fieldData_; |
| 127 |
Z rows_; |
| 128 |
Z cols_; |
| 129 |
Real x0_; |
| 130 |
Real y0_; |
| 131 |
Real dx_; |
| 132 |
Real dy_; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
} |
| 137 |
#endif |