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