ArraySamplers.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_ARRAY_SAMPLERS_HPP
12 #define CUBBYFLOW_ARRAY_SAMPLERS_HPP
13 
14 #include <Core/Array/ArrayView.hpp>
15 
16 #include <functional>
17 
18 namespace CubbyFlow
19 {
28 template <typename T, size_t N>
30 {
31  public:
32  static_assert(N > 0, "Dimension should be greater than 0");
33 
35 
36  static_assert(std::is_floating_point<ScalarType>::value,
37  "NearestArraySampler only can be instantiated with floating "
38  "point types");
39 
42 
44  NearestArraySampler() = default;
45 
53  explicit NearestArraySampler(const ArrayView<const T, N>& view,
54  const VectorType& gridSpacing,
55  const VectorType& gridOrigin);
56 
58  ~NearestArraySampler() = default;
59 
62 
64  NearestArraySampler(NearestArraySampler&& other) noexcept;
65 
68 
71 
73  T operator()(const VectorType& pt) const;
74 
76  CoordIndexType GetCoordinate(const VectorType& pt) const;
77 
79  [[nodiscard]] std::function<T(const VectorType&)> Functor() const;
80 
81  private:
82  ArrayView<const T, N> m_view;
83  VectorType m_gridSpacing;
84  VectorType m_invGridSpacing;
85  VectorType m_gridOrigin;
86 };
87 
88 template <typename T>
90 
91 template <typename T>
93 
94 template <typename T>
96 
105 template <typename T, size_t N>
107 {
108  public:
109  static_assert(N > 0, "N should be greater than 0");
110 
112 
113  static_assert(std::is_floating_point<ScalarType>::value,
114  "LinearArraySampler only can be instantiated with floating "
115  "point types");
116 
119 
120  static constexpr size_t FLAT_KERNEL_SIZE = 1 << N;
121 
123  LinearArraySampler() = default;
124 
132  explicit LinearArraySampler(const ArrayView<const T, N>& view,
133  const VectorType& gridSpacing,
134  const VectorType& gridOrigin);
135 
137  ~LinearArraySampler() = default;
138 
141 
143  LinearArraySampler(LinearArraySampler&& other) noexcept;
144 
147 
150 
152  T operator()(const VectorType& pt) const;
153 
155  void GetCoordinatesAndWeights(
156  const VectorType& pt,
157  std::array<CoordIndexType, FLAT_KERNEL_SIZE>& indices,
158  std::array<ScalarType, FLAT_KERNEL_SIZE>& weights) const;
159 
162  void GetCoordinatesAndGradientWeights(
163  const VectorType& pt,
164  std::array<CoordIndexType, FLAT_KERNEL_SIZE>& indices,
165  std::array<VectorType, FLAT_KERNEL_SIZE>& weights) const;
166 
168  [[nodiscard]] std::function<T(const VectorType&)> Functor() const;
169 
170  private:
171  ArrayView<const T, N> m_view;
172  VectorType m_gridSpacing;
173  VectorType m_invGridSpacing;
174  VectorType m_gridOrigin;
175 };
176 
177 template <typename T>
179 
180 template <typename T>
182 
183 template <typename T>
185 
194 template <typename T, size_t N, typename CubicInterpolationOp>
195 class CubicArraySampler final
196 {
197  public:
198  static_assert(N > 0, "N should be greater than 0");
199 
201 
202  static_assert(std::is_floating_point<ScalarType>::value,
203  "CubicArraySampler only can be instantiated with floating "
204  "point types");
205 
208 
210  CubicArraySampler() = default;
211 
219  explicit CubicArraySampler(const ArrayView<const T, N>& view,
220  const VectorType& gridSpacing,
221  const VectorType& gridOrigin);
222 
224  ~CubicArraySampler() = default;
225 
227  CubicArraySampler(const CubicArraySampler& other);
228 
230  CubicArraySampler(CubicArraySampler&& other) noexcept;
231 
234 
236  CubicArraySampler& operator=(CubicArraySampler&& other) noexcept;
237 
239  T operator()(const VectorType& pt) const;
240 
242  [[nodiscard]] std::function<T(const VectorType&)> Functor() const;
243 
244  private:
245  ArrayView<const T, N> m_view;
246  VectorType m_gridSpacing;
247  VectorType m_invGridSpacing;
248  VectorType m_gridOrigin;
249 };
250 
251 template <typename T>
253 {
255 
256  T operator()(const T& f0, const T& f1, const T& f2, const T& f3,
257  ScalarType t) const
258  {
259  return CatmullRom(f0, f1, f2, f3, t);
260  }
261 };
262 
263 template <typename T>
265 {
267 
268  T operator()(const T& f0, const T& f1, const T& f2, const T& f3,
269  ScalarType t) const
270  {
271  return MonotonicCatmullRom(f0, f1, f2, f3, t);
272  }
273 };
274 
275 template <typename T>
277 
278 template <typename T>
280 
281 template <typename T>
283 
284 template <typename T>
287 
288 template <typename T>
291 
292 template <typename T>
295 } // namespace CubbyFlow
296 
298 
299 #endif
NearestArraySampler()=default
Default constructor.
Definition: ArraySamplers.hpp:252
N-D nearest array sampler class.
Definition: ArraySamplers.hpp:29
Definition: ArrayView.hpp:60
NearestArraySampler & operator=(const NearestArraySampler &other)
Copy assignment operator.
Definition: ArraySamplers-Impl.hpp:207
typename GetScalarType< T >::value ScalarType
Definition: ArraySamplers.hpp:266
typename GetScalarType< CubbyFlow::Matrix< double, N > >::value ScalarType
Definition: ArraySamplers.hpp:111
~NearestArraySampler()=default
Default destructor.
typename GetScalarType< T >::value ScalarType
Definition: ArraySamplers.hpp:254
typename GetScalarType< T >::value ScalarType
Definition: ArraySamplers.hpp:34
Definition: Matrix.hpp:27
T operator()(const VectorType &pt) const
Returns sampled value at point pt.
Definition: ArraySamplers-Impl.hpp:231
T operator()(const T &f0, const T &f1, const T &f2, const T &f3, ScalarType t) const
Definition: ArraySamplers.hpp:256
Definition: pybind11Utils.hpp:20
N-D cubic array sampler class.
Definition: ArraySamplers.hpp:195
std::function< T(const VectorType &)> Functor() const
Returns a std::function object that wraps this instance.
Definition: ArraySamplers-Impl.hpp:257
std::enable_if_t< std::is_arithmetic< T >::value, S > CatmullRom(const S &f0, const S &f1, const S &f2, const S &f3, T t)
Computes Catmull-Rom interpolation.
Definition: MathUtils-Impl.hpp:318
Definition: ArraySamplers.hpp:264
std::enable_if_t< std::is_arithmetic< T >::value, T > MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.hpp:336
CoordIndexType GetCoordinate(const VectorType &pt) const
Returns the nearest array index for point pt.
Definition: ArraySamplers-Impl.hpp:238
N-D array sampler using linear interpolation.
Definition: ArraySamplers.hpp:106
T operator()(const T &f0, const T &f1, const T &f2, const T &f3, ScalarType t) const
Definition: ArraySamplers.hpp:268
T value
Definition: TypeHelpers.hpp:20
typename GetScalarType< T >::value ScalarType
Definition: ArraySamplers.hpp:200