bilinear_uniform.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // This implements bilinear interpolation on a uniform grid.
  7. // If dx and dy are both positive, then the (x,y) = (x0, y0) is associated with data index 0 (herein referred to as f[0])
  8. // The point (x0 + dx, y0) is associated with f[1], and (x0 + i*dx, y0) is associated with f[i],
  9. // i.e., we are assuming traditional C row major order.
  10. // The y coordinate increases *downward*, as is traditional in 2D computer graphics.
  11. // This is *not* how people generally think in numerical analysis (although it *is* how they lay out matrices).
  12. // Providing the capability of a grid rotation is too expensive and not ergonomic; you'll need to perform any rotations at the call level.
  13. // For clarity, the value f(x0 + i*dx, y0 + j*dy) must be stored in the f[j*cols + i] position.
  14. #ifndef BOOST_MATH_INTERPOLATORS_BILINEAR_UNIFORM_HPP
  15. #define BOOST_MATH_INTERPOLATORS_BILINEAR_UNIFORM_HPP
  16. #include <utility>
  17. #include <memory>
  18. #include <boost/math/interpolators/detail/bilinear_uniform_detail.hpp>
  19. namespace boost::math::interpolators {
  20. template <class RandomAccessContainer>
  21. class bilinear_uniform
  22. {
  23. public:
  24. using Real = typename RandomAccessContainer::value_type;
  25. using Z = typename RandomAccessContainer::size_type;
  26. bilinear_uniform(RandomAccessContainer && fieldData, Z rows, Z cols, Real dx = 1, Real dy = 1, Real x0 = 0, Real y0 = 0)
  27. : m_imp(std::make_shared<detail::bilinear_uniform_imp<RandomAccessContainer>>(std::move(fieldData), rows, cols, dx, dy, x0, y0))
  28. {
  29. }
  30. Real operator()(Real x, Real y) const
  31. {
  32. return m_imp->operator()(x,y);
  33. }
  34. friend std::ostream& operator<<(std::ostream& out, bilinear_uniform<RandomAccessContainer> const & bu) {
  35. out << *bu.m_imp;
  36. return out;
  37. }
  38. private:
  39. std::shared_ptr<detail::bilinear_uniform_imp<RandomAccessContainer>> m_imp;
  40. };
  41. }
  42. #endif