Wildmeshing Toolkit
Sampling.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <Eigen/Core>
3 #include "Image.hpp"
5 
7 enum class SAMPLING_MODE { BICUBIC, SPLINE };
8 class Sampling
9 {
10 public:
11  using DScalar = DScalar2<double, Eigen::Matrix<double, -1, 1>, Eigen::Matrix<double, -1, -1>>;
12  virtual ~Sampling(){};
13 
14 public:
15  virtual double sample(const double u, const double v) const = 0;
16  virtual DScalar sample(const DScalar& u, const DScalar& v) const = 0;
17 };
18 
19 
22 {
23 public:
25 
26 protected:
28  double A = 0.0;
29  double B = 0.0;
30  double C = 0.0;
31 
32  template <typename S>
33  auto evaluate(const S& u, const S& v) const
34  {
35  if (m_type == Linear) {
36  return evaluate_linear(u, v);
37  } else
38  return static_cast<S>(0.0);
39  }
40 
41  template <typename S>
42  auto evaluate_linear(const S& u, const S& v) const
43  {
44  return A * u + B * v + C;
45  }
46 
47 public:
48  // make a contructor
50  const FunctionType type,
51  const double a,
52  const double b,
53  const double c)
54  : m_type(type)
55  , A(a)
56  , B(b)
57  , C(c)
58  {}
59 
60 
61  void set_coefficients(double a, const double b, const double c)
62  {
63  A = a;
64  B = b;
65  C = c;
66  }
67  double sample(const double u, const double v) const override { return evaluate<double>(u, v); }
68  DScalar sample(const DScalar& u, const DScalar& v) const override
69  {
70  return evaluate<DScalar>(u, v);
71  }
72 };
73 
74 template <typename Derived>
75 class SamplingImage : public Sampling
76 {
77 protected:
78  const Image& m_image;
79 
80 public:
81  SamplingImage(const Image& img)
82  : m_image(img)
83  {
84  assert(m_image.width() == m_image.height());
85  assert(m_image.width() != 0);
86  }
87 
88 public:
89  double sample(const double u, const double v) const override
90  {
91  return static_cast<const Derived*>(this)->sample(u, v);
92  }
93  DScalar sample(const DScalar& u, const DScalar& v) const override
94  {
95  return static_cast<const Derived*>(this)->sample(u, v);
96  }
97 };
98 
99 class SamplingBicubic : public SamplingImage<SamplingBicubic>
100 {
101 public:
103  using Super::Super;
104  template <class T>
105  T sample_T(T u, T v) const
106  {
107  auto w = m_image.width();
108  auto h = m_image.height();
109  // x, y are between 0 and 1
110  T x = u * static_cast<std::decay_t<T>>(w);
111  T y = v * static_cast<std::decay_t<T>>(h);
112 
113  // use bicubic interpolation
114  BicubicVector<float> sample_vector = extract_samples(
115  static_cast<size_t>(w),
116  static_cast<size_t>(h),
117  m_image.get_raw_image().data(),
122  BicubicVector<float> bicubic_coeff = get_bicubic_matrix() * sample_vector;
123  return eval_bicubic_coeffs(bicubic_coeff, x, y);
124  }
125  double sample(const double u, const double v) const override { return sample_T<double>(u, v); }
126  DScalar sample(const DScalar& u, const DScalar& v) const override
127  {
128  return sample_T<DScalar>(u, v);
129  }
130 };
131 } // namespace wmtk::components::adaptive_tessellation::image
const ImageMatrixf & get_raw_image() const
Definition: Image.hpp:34
double sample(const double u, const double v) const override
Definition: Sampling.hpp:67
DScalar sample(const DScalar &u, const DScalar &v) const override
Definition: Sampling.hpp:68
void set_coefficients(double a, const double b, const double c)
Definition: Sampling.hpp:61
SamplingAnalyticFunction(const FunctionType type, const double a, const double b, const double c)
Definition: Sampling.hpp:49
double sample(const double u, const double v) const override
Definition: Sampling.hpp:125
DScalar sample(const DScalar &u, const DScalar &v) const override
Definition: Sampling.hpp:126
virtual double sample(const double u, const double v) const =0
virtual DScalar sample(const DScalar &u, const DScalar &v) const =0
double sample(const double u, const double v) const override
Definition: Sampling.hpp:89
DScalar sample(const DScalar &u, const DScalar &v) const override
Definition: Sampling.hpp:93
std::decay_t< T > eval_bicubic_coeffs(const BicubicVector< float > &coeffs, const T &sx, const T &sy)
BicubicVector< float > extract_samples(const size_t width, const size_t height, const float *buffer, const double sx_, const double sy_, const WrappingMode mode_x, const WrappingMode mode_y)
Automatic differentiation scalar with first- and second-order derivatives.
Definition: autodiff.h:501