Skip to content

Commit

Permalink
♻️ Refactor Bicubic class to use interpolation factory
Browse files Browse the repository at this point in the history
  • Loading branch information
fbriol committed Feb 15, 2024
1 parent bbe8168 commit 8b9c546
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <stdexcept>

#include "pyinterp/detail/interpolation/bicubic.hpp"
#include "pyinterp/detail/interpolation/bilinear.hpp"

namespace pyinterp::detail::interpolation {

template <typename T>
static inline auto factory_2d(const std::string &kind)
-> std::unique_ptr<Interpolator2D<T>> {
if (kind == "bilinear") {
return std::make_unique<Bilinear<T>>();
}
if (kind == "bicubic") {
return std::make_unique<Bicubic<T>>();
}
throw std::invalid_argument("Invalid bicubic type: " + kind);
}

} // namespace pyinterp::detail::interpolation
21 changes: 4 additions & 17 deletions src/pyinterp/core/include/pyinterp/detail/math/bicubic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <Eigen/Core>
#include <string>

#include "pyinterp/detail/gsl/interpolate2d.hpp"
#include "pyinterp/detail/interpolation/factory_2d.hpp"
#include "pyinterp/detail/math/frame.hpp"

namespace pyinterp::detail::math {
Expand All @@ -21,30 +21,17 @@ class Bicubic {
/// @param xr Calculation window.
/// @param kind method of calculation
explicit Bicubic(const Frame2D &xr, const std::string &kind)
: interpolator_(xr.x()->size(), xr.y()->size(),
Bicubic::parse_interp2d_type(kind), gsl::Accelerator(),
gsl::Accelerator()) {}
: interpolator_(interpolation::factory_2d<double>(kind)) {}

/// Return the interpolated value of y for a given point x
auto interpolate(const double x, const double y, const Frame2D &xr)
-> double {
return interpolator_.evaluate(*(xr.x()), *(xr.y()), *(xr.q()), x, y);
return (*interpolator_)(*(xr.x()), *(xr.y()), *(xr.q()), x, y);
}

private:
/// GSL interpolator
gsl::Interpolate2D interpolator_;

static inline auto parse_interp2d_type(const std::string &kind)
-> const gsl_interp2d_type * {
if (kind == "bilinear") {
return gsl_interp2d_bilinear;
}
if (kind == "bicubic") {
return gsl_interp2d_bicubic;
}
throw std::invalid_argument("Invalid bicubic type: " + kind);
}
std::unique_ptr<interpolation::Interpolator2D<double>> interpolator_;
};

} // namespace pyinterp::detail::math

0 comments on commit 8b9c546

Please sign in to comment.