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