ScalarGrid2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ScalarGrid2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Abstract base class for 2-D scalar grid structure.
6 > Created Time: 2017/07/06
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_SCALAR_GRID2_H
10 #define CUBBYFLOW_SCALAR_GRID2_H
11 
12 #include <Core/Array/Array2.h>
16 #include <Core/Grid/Grid2.h>
17 
18 namespace CubbyFlow
19 {
21  class ScalarGrid2 : public ScalarField2, public Grid2
22  {
23  public:
26 
29 
31  ScalarGrid2();
32 
34  virtual ~ScalarGrid2();
35 
42  virtual Size2 GetDataSize() const = 0;
43 
51  virtual Vector2D GetDataOrigin() const = 0;
52 
54  virtual std::shared_ptr<ScalarGrid2> Clone() const = 0;
55 
57  void Clear();
58 
60  void Resize(
61  size_t resolutionX, size_t resolutionY,
62  double gridSpacingX = 1.0, double gridSpacingY = 1.0,
63  double originX = 0.0, double originY = 0.0,
64  double initialValue = 0.0);
65 
67  void Resize(
68  const Size2& resolution,
69  const Vector2D& gridSpacing = Vector2D(1, 1),
70  const Vector2D& origin = Vector2D(),
71  double initialValue = 0.0);
72 
74  void Resize(
75  double gridSpacingX, double gridSpacingY,
76  double originX, double originY);
77 
79  void Resize(const Vector2D& gridSpacing, const Vector2D& origin);
80 
82  const double& operator()(size_t i, size_t j) const;
83 
85  double& operator()(size_t i, size_t j);
86 
88  Vector2D GradientAtDataPoint(size_t i, size_t j) const;
89 
91  double LaplacianAtDataPoint(size_t i, size_t j) const;
92 
95 
98 
101 
103  void Fill(double value, ExecutionPolicy policy = ExecutionPolicy::Parallel);
104 
106  void Fill(const std::function<double(const Vector2D&)>& func, ExecutionPolicy policy = ExecutionPolicy::Parallel);
107 
115  void ForEachDataPointIndex(const std::function<void(size_t, size_t)>& func) const;
116 
125  void ParallelForEachDataPointIndex(const std::function<void(size_t, size_t)>& func) const;
126 
127  // ScalarField2 implementations
128 
135  double Sample(const Vector2D& x) const override;
136 
143  std::function<double(const Vector2D&)> Sampler() const override;
144 
146  Vector2D Gradient(const Vector2D& x) const override;
147 
149  double Laplacian(const Vector2D& x) const override;
150 
152  void Serialize(std::vector<uint8_t>* buffer) const override;
153 
155  void Deserialize(const std::vector<uint8_t>& buffer) override;
156 
157  protected:
159  void SwapScalarGrid(ScalarGrid2* other);
160 
162  void SetScalarGrid(const ScalarGrid2& other);
163 
165  void GetData(std::vector<double>* data) const override;
166 
168  void SetData(const std::vector<double>& data) override;
169 
170  private:
171  Array2<double> m_data;
172  LinearArraySampler2<double, double> m_linearSampler;
173  std::function<double(const Vector2D&)> m_sampler;
174 
175  void ResetSampler();
176  };
177 
179  using ScalarGrid2Ptr = std::shared_ptr<ScalarGrid2>;
180 
183  {
184  public:
187 
189  virtual ~ScalarGridBuilder2();
190 
192  virtual ScalarGrid2Ptr Build(
193  const Size2& resolution,
194  const Vector2D& gridSpacing,
195  const Vector2D& gridOrigin,
196  double initialVal) const = 0;
197  };
198 
200  using ScalarGridBuilder2Ptr = std::shared_ptr<ScalarGridBuilder2>;
201 }
202 
203 #endif
void Serialize(std::vector< uint8_t > *buffer) const override
Serializes the grid instance to the output buffer.
2-D read-only array accessor class.
Definition: ArrayAccessor2.h:261
void SetData(const std::vector< double > &data) override
Sets the data from a continuous linear array.
void ParallelForEachDataPointIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each data point in parallel.
Abstract base class for 2-D cartesian grid structure.
Definition: Grid2.h:30
Abstract base class for 2-D scalar field.
Definition: ScalarField2.h:21
virtual Size2 GetDataSize() const =0
Returns the size of the grid data.
void ForEachDataPointIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each data point.
Abstract base class for 2-D scalar grid builder.
Definition: ScalarGrid2.h:182
std::function< double(const Vector2D &)> Sampler() const override
Returns the sampler function.
2-D point class.
Definition: Point2.h:25
virtual ~ScalarGrid2()
Default destructor.
virtual std::shared_ptr< ScalarGrid2 > Clone() const =0
Returns the copy of the grid instance.
double LaplacianAtDataPoint(size_t i, size_t j) const
Returns the Laplacian at given data point.
void GetData(std::vector< double > *data) const override
Fetches the data into a continuous linear array.
2-D array accessor class.
Definition: ArrayAccessor2.h:31
void Clear()
Clears the contents of the grid.
double Sample(const Vector2D &x) const override
Returns the sampled value at given position x.
void Resize(size_t resolutionX, size_t resolutionY, double gridSpacingX=1.0, double gridSpacingY=1.0, double originX=0.0, double originY=0.0, double initialValue=0.0)
Resizes the grid using given parameters.
Vector2D GradientAtDataPoint(size_t i, size_t j) const
Returns the gradient vector at given data point.
Definition: pybind11Utils.h:24
std::shared_ptr< ScalarGrid2 > ScalarGrid2Ptr
Shared pointer for the ScalarGrid2 type.
Definition: ScalarGrid2.h:179
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
ScalarGrid2()
Constructs an empty grid.
void SwapScalarGrid(ScalarGrid2 *other)
Swaps the data storage and predefined samplers with given grid.
virtual ScalarGrid2Ptr Build(const Size2 &resolution, const Vector2D &gridSpacing, const Vector2D &gridOrigin, double initialVal) const =0
Returns 2-D scalar grid with given parameters.
virtual ~ScalarGridBuilder2()
Default destructor.
virtual Vector2D GetDataOrigin() const =0
Returns the origin of the grid data.
const double & operator()(size_t i, size_t j) const
Returns the grid data at given data point.
Vector2D Gradient(const Vector2D &x) const override
Returns the gradient vector at given position x.
double Laplacian(const Vector2D &x) const override
Returns the Laplacian at given position x.
DataPositionFunc GetDataPosition() const
Returns the function that maps data point to its position.
ScalarGridBuilder2()
Creates a builder.
2-D vector class.
Definition: Vector2.h:26
std::shared_ptr< ScalarGridBuilder2 > ScalarGridBuilder2Ptr
Shared pointer for the ScalarGridBuilder2 type.
Definition: ScalarGrid2.h:200
void Fill(double value, ExecutionPolicy policy=ExecutionPolicy::Parallel)
Fills the grid with given value.
2-D array class.
Definition: Array2.h:42
ScalarDataAccessor GetDataAccessor()
Returns the read-write data array accessor.
ExecutionPolicy
Execution policy tag.
Definition: Parallel.h:15
void SetScalarGrid(const ScalarGrid2 &other)
Sets the data storage and predefined samplers with given grid.
2-D linear array sampler class.
Definition: ArraySamplers2.h:78
Abstract base class for 2-D scalar grid structure.
Definition: ScalarGrid2.h:21
void Deserialize(const std::vector< uint8_t > &buffer) override
Deserializes the input buffer to the grid instance.
std::function< Vector2D(size_t, size_t)> DataPositionFunc
Function type for mapping data index to actual position.
Definition: Grid2.h:34
ConstScalarDataAccessor GetConstDataAccessor() const
Returns the read-only data array accessor.