Grid2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Grid2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Abstract base class for 2-D cartesian grid structure.
6 > Created Time: 2017/07/05
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_GRID2_H
10 #define CUBBYFLOW_GRID2_H
11 
13 #include <Core/Size/Size2.h>
15 #include <Core/Vector/Vector2.h>
16 
17 #include <memory>
18 #include <string>
19 
20 namespace CubbyFlow
21 {
30  class Grid2 : public Serializable
31  {
32  public:
34  using DataPositionFunc = std::function<Vector2D(size_t, size_t)>;
35 
37  Grid2();
38 
40  virtual ~Grid2();
41 
43  virtual std::string TypeName() const = 0;
44 
46  const Size2& Resolution() const;
47 
49  const Vector2D& Origin() const;
50 
52  const Vector2D& GridSpacing() const;
53 
55  const BoundingBox2D& BoundingBox() const;
56 
59 
67  void ForEachCellIndex(const std::function<void(size_t, size_t)>& func) const;
68 
77  void ParallelForEachCellIndex(const std::function<void(size_t, size_t)>& func) const;
78 
80  bool HasSameShape(const Grid2& other) const;
81 
83  virtual void Swap(Grid2* other) = 0;
84 
85  protected:
87  void SetSizeParameters(const Size2& resolution, const Vector2D& gridSpacing, const Vector2D& origin);
88 
90  void SwapGrid(Grid2* other);
91 
93  void SetGrid(const Grid2& other);
94 
96  virtual void GetData(std::vector<double>* data) const = 0;
97 
99  virtual void SetData(const std::vector<double>& data) = 0;
100 
101  private:
102  Size2 m_resolution;
103  Vector2D m_gridSpacing = Vector2D(1, 1);
104  Vector2D m_origin;
105  BoundingBox2D m_boundingBox = BoundingBox2D(Vector2D(), Vector2D());
106  };
107 
108  using Grid2Ptr = std::shared_ptr<Grid2>;
109 
110 #define CUBBYFLOW_GRID2_TYPE_NAME(DerivedClassName) \
111  std::string TypeName() const override \
112  { \
113  return #DerivedClassName; \
114  }
115 }
116 
117 #endif
DataPositionFunc CellCenterPosition() const
Returns the function that maps grid index to the cell-center position.
virtual ~Grid2()
Default destructor.
Abstract base class for any serializable class.
Definition: Serialization.h:20
Abstract base class for 2-D cartesian grid structure.
Definition: Grid2.h:30
virtual void Swap(Grid2 *other)=0
Swaps the data with other grid.
void SetGrid(const Grid2 &other)
Sets the size parameters with given grid other.
bool HasSameShape(const Grid2 &other) const
Returns true if resolution, grid-spacing and origin are same.
virtual void SetData(const std::vector< double > &data)=0
Sets the data from a continuous linear array.
virtual void GetData(std::vector< double > *data) const =0
Fetches the data into a continuous linear array.
void ForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each grid cell.
Grid2()
Constructs an empty grid.
const BoundingBox2D & BoundingBox() const
Returns the bounding box of the grid.
2-D point class.
Definition: Point2.h:25
BoundingBox2< double > BoundingBox2D
Double-type 2-D BoundingBox.
Definition: BoundingBox2.h:126
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
std::shared_ptr< Grid2 > Grid2Ptr
Definition: Grid2.h:108
Definition: pybind11Utils.h:24
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
void ParallelForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each grid cell in parallel.
const Size2 & Resolution() const
Returns the grid resolution.
const Vector2D & GridSpacing() const
Returns the grid spacing.
void SwapGrid(Grid2 *other)
Swaps the size parameters with given grid other.
2-D vector class.
Definition: Vector2.h:26
void SetSizeParameters(const Size2 &resolution, const Vector2D &gridSpacing, const Vector2D &origin)
Sets the size parameters including the resolution, grid spacing, and origin.
const Vector2D & Origin() const
Returns the grid origin.
virtual std::string TypeName() const =0
Returns the type name of derived grid.
std::function< Vector2D(size_t, size_t)> DataPositionFunc
Function type for mapping data index to actual position.
Definition: Grid2.h:34