CustomVectorField2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: CustomVectorField2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D vector field with custom field function.
6 > Created Time: 2017/03/15
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_CUSTOM_VECTOR_FIELD2_H
10 #define CUBBYFLOW_CUSTOM_VECTOR_FIELD2_H
11 
13 
14 namespace CubbyFlow
15 {
17  class CustomVectorField2 final : public VectorField2
18  {
19  public:
20  class Builder;
21 
31  const std::function<Vector2D(const Vector2D&)>& customFunction,
32  double derivativeResolution = 1e-3);
33 
43  const std::function<Vector2D(const Vector2D&)>& customFunction,
44  const std::function<double(const Vector2D&)>& customDivergenceFunction,
45  double derivativeResolution = 1e-3);
46 
49  const std::function<Vector2D(const Vector2D&)>& customFunction,
50  const std::function<double(const Vector2D&)>& customDivergenceFunction,
51  const std::function<double(const Vector2D&)>& customCurlFunction);
52 
54  Vector2D Sample(const Vector2D& x) const override;
55 
57  double Divergence(const Vector2D& x) const override;
58 
60  double Curl(const Vector2D& x) const override;
61 
63  std::function<Vector2D(const Vector2D&)> Sampler() const override;
64 
66  static Builder GetBuilder();
67 
68  private:
69  std::function<Vector2D(const Vector2D&)> m_customFunction;
70  std::function<double(const Vector2D&)> m_customDivergenceFunction;
71  std::function<double(const Vector2D&)> m_customCurlFunction;
72  double m_resolution = 1e-3;
73  };
74 
76  using CustomVectorField2Ptr = std::shared_ptr<CustomVectorField2>;
77 
82  {
83  public:
85  Builder& WithFunction(const std::function<Vector2D(const Vector2D&)>& func);
86 
88  Builder& WithDivergenceFunction(const std::function<double(const Vector2D&)>& func);
89 
91  Builder& WithCurlFunction(const std::function<double(const Vector2D&)>& func);
92 
94  Builder& WithDerivativeResolution(double resolution);
95 
97  CustomVectorField2 Build() const;
98 
101 
102  private:
103  double m_resolution = 1e-3;
104  std::function<Vector2D(const Vector2D&)> m_customFunction;
105  std::function<double(const Vector2D&)> m_customDivergenceFunction;
106  std::function<double(const Vector2D&)> m_customCurlFunction;
107  };
108 }
109 
110 #endif
2-D vector field with custom field function.
Definition: CustomVectorField2.h:17
Vector2D Sample(const Vector2D &x) const override
Returns the sampled value at given position x.
CustomVectorField2 Build() const
Builds CustomVectorField2.
Builder & WithDerivativeResolution(double resolution)
Returns builder with derivative resolution.
static Builder GetBuilder()
Returns builder fox CustomVectorField2.
Builder & WithFunction(const std::function< Vector2D(const Vector2D &)> &func)
Returns builder with field function.
Builder & WithCurlFunction(const std::function< double(const Vector2D &)> &func)
Returns builder with curl function.
Definition: pybind11Utils.h:24
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
Front-end to create CustomVectorField2 objects step by step.
Definition: CustomVectorField2.h:81
CustomVectorField2(const std::function< Vector2D(const Vector2D &)> &customFunction, double derivativeResolution=1e-3)
Constructs a field with given function.
std::function< Vector2D(const Vector2D &)> Sampler() const override
Returns the sampler function.
Builder & WithDivergenceFunction(const std::function< double(const Vector2D &)> &func)
Returns builder with divergence function.
2-D vector class.
Definition: Vector2.h:26
double Divergence(const Vector2D &x) const override
Returns the divergence at given position x.
std::shared_ptr< CustomVectorField2 > CustomVectorField2Ptr
Shared pointer type for the CustomVectorField2.
Definition: CustomVectorField2.h:76
CustomVectorField2Ptr MakeShared() const
Builds shared pointer of CustomVectorField2 instance.
double Curl(const Vector2D &x) const override
Returns the curl at given position x.
Abstract base class for 2-D vector field.
Definition: VectorField2.h:21