CustomScalarField.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_CUSTOM_SCALAR_FIELD_HPP
12 #define CUBBYFLOW_CUSTOM_SCALAR_FIELD_HPP
13 
15 
16 namespace CubbyFlow
17 {
19 template <size_t N>
20 class CustomScalarField final : public ScalarField<N>
21 {
22  public:
23  class Builder;
24 
34  std::function<double(const Vector<double, N>&)> customFunction,
35  double derivativeResolution = 1e-3);
36 
46  std::function<double(const Vector<double, N>&)> customFunction,
47  std::function<Vector<double, N>(const Vector<double, N>&)>
48  customGradientFunction,
49  double derivativeResolution = 1e-3);
50 
53  std::function<double(const Vector<double, N>&)> customFunction,
54  std::function<Vector<double, N>(const Vector<double, N>&)>
55  customGradientFunction,
56  std::function<double(const Vector<double, N>&)>
57  customLaplacianFunction);
58 
60  [[nodiscard]] double Sample(const Vector<double, N>& x) const override;
61 
63  [[nodiscard]] std::function<double(const Vector<double, N>&)> Sampler()
64  const override;
65 
67  [[nodiscard]] Vector<double, N> Gradient(
68  const Vector<double, N>& x) const override;
69 
71  [[nodiscard]] double Laplacian(const Vector<double, N>& x) const override;
72 
74  static Builder GetBuilder();
75 
76  private:
77  std::function<double(const Vector<double, N>&)> m_customFunction;
78  std::function<Vector<double, N>(const Vector<double, N>&)>
79  m_customGradientFunction;
80  std::function<double(const Vector<double, N>&)> m_customLaplacianFunction;
81  double m_resolution = 1e-3;
82 };
83 
86 
89 
91 using CustomScalarField2Ptr = std::shared_ptr<CustomScalarField2>;
92 
94 using CustomScalarField3Ptr = std::shared_ptr<CustomScalarField3>;
95 
99 template <size_t N>
100 class CustomScalarField<N>::Builder final
101 {
102  public:
104  Builder& WithFunction(
105  const std::function<double(const Vector<double, N>&)>& func);
106 
108  Builder& WithGradientFunction(
109  const std::function<Vector<double, N>(const Vector<double, N>&)>& func);
110 
112  Builder& WithLaplacianFunction(
113  const std::function<double(const Vector<double, N>&)>& func);
114 
116  Builder& WithDerivativeResolution(double resolution);
117 
119  CustomScalarField<N> Build() const;
120 
122  std::shared_ptr<CustomScalarField<N>> MakeShared() const;
123 
124  private:
125  double m_resolution = 1e-3;
126  std::function<double(const Vector<double, N>&)> m_customFunction;
127  std::function<Vector<double, N>(const Vector<double, N>&)>
128  m_customGradientFunction;
129  std::function<double(const Vector<double, N>&)> m_customLaplacianFunction;
130 };
131 } // namespace CubbyFlow
132 
133 #endif
std::shared_ptr< CustomScalarField2 > CustomScalarField2Ptr
Shared pointer type for the CustomScalarField2.
Definition: CustomScalarField.hpp:91
double Laplacian(const Vector< double, N > &x) const override
Returns the Laplacian at given position x.
CustomScalarField(std::function< double(const Vector< double, N > &)> customFunction, double derivativeResolution=1e-3)
Constructs a field with given function.
Vector< double, N > Gradient(const Vector< double, N > &x) const override
Returns the gradient vector at given position x.
Abstract base class for N-D scalar field.
Definition: ScalarField.hpp:24
std::function< double(const Vector< double, N > &)> Sampler() const override
Returns the sampler function.
static Builder GetBuilder()
Returns builder for CustomScalarField.
double Sample(const Vector< double, N > &x) const override
Returns the sampled value at given position x.
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
N-D scalar field with custom field function.
Definition: CustomScalarField.hpp:20
std::shared_ptr< CustomScalarField3 > CustomScalarField3Ptr
Shared pointer type for the CustomScalarField3.
Definition: CustomScalarField.hpp:94
Front-end to create CustomScalarField objects step by step.
Definition: CustomScalarField.hpp:100