Grid.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_GRID_HPP
12 #define CUBBYFLOW_GRID_HPP
13 
15 #include <Core/Matrix/Matrix.hpp>
17 
18 #include <string>
19 
20 namespace CubbyFlow
21 {
22 template <size_t N>
24 {
25  public:
26  using RawFunctionType =
27  std::function<Vector<double, N>(const Vector<size_t, N>&)>;
28 
29  GridDataPositionFunc(const RawFunctionType& func) : m_func(func)
30  {
31  // Do nothing
32  }
33 
34  template <typename... Indices>
35  Vector<double, N> operator()(size_t i, Indices... indices) const
36  {
37  return (*this)(Vector<size_t, N>(i, indices...));
38  }
39 
41  {
42  return m_func(idx);
43  }
44 
45  private:
46  RawFunctionType m_func;
47 };
48 
57 template <size_t N>
58 class Grid : public Serializable
59 {
60  public:
62  Grid() = default;
63 
65  ~Grid() override = default;
66 
68  Grid(const Grid& other);
69 
71  Grid(Grid&& other) noexcept;
72 
74  Grid& operator=(const Grid& other);
75 
77  Grid& operator=(Grid&& other) noexcept;
78 
80  [[nodiscard]] virtual std::string TypeName() const = 0;
81 
83  [[nodiscard]] const Vector<size_t, N>& Resolution() const;
84 
86  [[nodiscard]] const Vector<double, N>& Origin() const;
87 
89  [[nodiscard]] const Vector<double, N>& GridSpacing() const;
90 
92  [[nodiscard]] const BoundingBox<double, N>& GetBoundingBox() const;
93 
95  [[nodiscard]] GridDataPositionFunc<N> CellCenterPosition() const;
96 
104  void ForEachCellIndex(
105  const std::function<void(const Vector<size_t, N>&)>& func) const;
106 
107  template <size_t M = N>
108  std::enable_if_t<M == 2, void> ForEachCellIndex(
109  const std::function<void(size_t, size_t)>& func) const
110  {
111  ForEachCellIndex([&func](const Vector2UZ& idx) { func(idx.x, idx.y); });
112  }
113 
114  template <size_t M = N>
115  std::enable_if_t<M == 3, void> ForEachCellIndex(
116  const std::function<void(size_t, size_t, size_t)>& func) const
117  {
118  ForEachCellIndex(
119  [&func](const Vector3UZ& idx) { func(idx.x, idx.y, idx.z); });
120  }
121 
131  void ParallelForEachCellIndex(
132  const std::function<void(const Vector<size_t, N>&)>& func) const;
133 
134  template <size_t M = N>
135  std::enable_if_t<M == 2, void> ParallelForEachCellIndex(
136  const std::function<void(size_t, size_t)>& func) const
137  {
138  ParallelForEachCellIndex(
139  [&func](const Vector2UZ& idx) { func(idx.x, idx.y); });
140  }
141 
142  template <size_t M = N>
143  std::enable_if_t<M == 3, void> ParallelForEachCellIndex(
144  const std::function<void(size_t, size_t, size_t)>& func) const
145  {
146  ParallelForEachCellIndex(
147  [&func](const Vector3UZ& idx) { func(idx.x, idx.y, idx.z); });
148  }
149 
151  [[nodiscard]] bool HasSameShape(const Grid& other) const;
152 
154  virtual void Swap(Grid* other) = 0;
155 
156  protected:
159  void SetSizeParameters(const Vector<size_t, N>& resolution,
160  const Vector<double, N>& gridSpacing,
161  const Vector<double, N>& origin);
162 
164  void SwapGrid(Grid* other);
165 
167  void SetGrid(const Grid& other);
168 
170  virtual void GetData(Array1<double>& data) const = 0;
171 
173  virtual void SetData(const ConstArrayView1<double>& data) = 0;
174 
175  private:
176  Vector<size_t, N> m_resolution;
178  Vector<double, N> m_origin;
179  BoundingBox<double, N> m_boundingBox =
181 };
182 
184 using Grid2 = Grid<2>;
185 
187 using Grid3 = Grid<3>;
188 
190 using Grid2Ptr = std::shared_ptr<Grid2>;
191 
193 using Grid3Ptr = std::shared_ptr<Grid3>;
194 
195 #define CUBBYFLOW_GRID_TYPE_NAME(DerivedClassName, N) \
196  [[nodiscard]] std::string TypeName() const override \
197  { \
198  return #DerivedClassName + std::to_string(N); \
199  }
200 } // namespace CubbyFlow
201 
202 #endif
std::shared_ptr< Grid3 > Grid3Ptr
Shared pointer type for Grid3.
Definition: Grid.hpp:193
std::enable_if_t< M==2, void > ForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Definition: Grid.hpp:108
Abstract base class for any serializable class.
Definition: Serialization.hpp:21
std::enable_if_t< M==2, void > ParallelForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Definition: Grid.hpp:135
std::enable_if_t< M==3, void > ParallelForEachCellIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Definition: Grid.hpp:143
Abstract base class for N-D cartesian grid structure.
Definition: Grid.hpp:58
Definition: Matrix.hpp:27
Vector< double, N > operator()(const Vector< size_t, N > &idx) const
Definition: Grid.hpp:40
static std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >), D > MakeConstant(ValueType val)
Makes a static matrix with constant entries.
Definition: MatrixDenseBase-Impl.hpp:152
std::shared_ptr< Grid2 > Grid2Ptr
Shared pointer type for Grid.
Definition: Grid.hpp:190
Definition: pybind11Utils.hpp:20
Definition: Array-Impl.hpp:19
Definition: Grid.hpp:23
Generic N-dimensional array class interface.
Definition: Array.hpp:32
std::enable_if_t< M==3, void > ForEachCellIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Definition: Grid.hpp:115
Vector< double, N > operator()(size_t i, Indices... indices) const
Definition: Grid.hpp:35
GridDataPositionFunc(const RawFunctionType &func)
Definition: Grid.hpp:29
std::function< Vector< double, N >(const Vector< size_t, N > &)> RawFunctionType
Definition: Grid.hpp:27