Loading...
Searching...
No Matches
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
17
18#include <string>
19
20namespace CubbyFlow
21{
22template <size_t N>
24{
25 public:
27 std::function<Vector<double, N>(const Vector<size_t, N>&)>;
28
30 {
31 // Do nothing
32 }
33
34 template <typename... Indices>
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
57template <size_t N>
58class 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
75
77 Grid& operator=(Grid&& other) noexcept;
78
80 [[nodiscard]] virtual std::string TypeName() const = 0;
81
84
87
90
93
96
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 {
119 [&func](const Vector3UZ& idx) { func(idx.x, idx.y, idx.z); });
120 }
121
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 {
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 {
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:
161 const Vector<double, N>& origin);
162
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
185
188
190using Grid2Ptr = std::shared_ptr<Grid2>;
191
193using 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
Definition Grid.hpp:24
GridDataPositionFunc(const RawFunctionType &func)
Definition Grid.hpp:29
Vector< double, N > operator()(const Vector< size_t, N > &idx) const
Definition Grid.hpp:40
std::function< Vector< double, N >(const Vector< size_t, N > &)> RawFunctionType
Definition Grid.hpp:27
Vector< double, N > operator()(size_t i, Indices... indices) const
Definition Grid.hpp:35
Abstract base class for N-D cartesian grid structure.
Definition Grid.hpp:59
virtual void SetData(const ConstArrayView1< double > &data)=0
Sets the data from a continuous linear array.
void ParallelForEachCellIndex(const std::function< void(const Vector< size_t, N > &)> &func) const
Invokes the given function func for each grid cell in parallel.
const Vector< double, N > & Origin() const
Returns the grid origin.
void SetSizeParameters(const Vector< size_t, N > &resolution, const Vector< double, N > &gridSpacing, const Vector< double, N > &origin)
const BoundingBox< double, N > & GetBoundingBox() const
Returns the bounding box of the grid.
std::enable_if_t< M==2, void > ParallelForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Definition Grid.hpp:135
virtual void Swap(Grid *other)=0
Swaps the data with other grid.
const Vector< size_t, N > & Resolution() const
Returns the grid resolution.
~Grid() override=default
Default virtual destructor.
Grid & operator=(Grid &&other) noexcept
Move assignment operator.
GridDataPositionFunc< N > CellCenterPosition() const
Returns the function that maps grid index to the cell-center position.
Grid(Grid &&other) noexcept
Move constructor.
Grid()=default
Constructs an empty grid.
std::enable_if_t< M==3, void > ForEachCellIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Definition Grid.hpp:115
const Vector< double, N > & GridSpacing() const
Returns the grid spacing.
Grid & operator=(const Grid &other)
Copy assignment operator.
virtual std::string TypeName() const =0
Returns the type name of derived grid.
void SetGrid(const Grid &other)
Sets the size parameters with given grid other.
std::enable_if_t< M==3, void > ParallelForEachCellIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Definition Grid.hpp:143
virtual void GetData(Array1< double > &data) const =0
Fetches the data into a continuous linear array.
bool HasSameShape(const Grid &other) const
Returns true if resolution, grid-spacing and origin are same.
void SwapGrid(Grid *other)
Swaps the size parameters with given grid other.
void ForEachCellIndex(const std::function< void(const Vector< size_t, N > &)> &func) const
Invokes the given function func for each grid cell.
Grid(const Grid &other)
Copy constructor.
std::enable_if_t< M==2, void > ForEachCellIndex(const std::function< void(size_t, size_t)> &func) const
Definition Grid.hpp:108
static std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >(), D > MakeConstant(ValueType val)
Makes a static matrix with constant entries.
Definition MatrixDenseBase-Impl.hpp:152
Definition Matrix.hpp:30
Abstract base class for any serializable class.
Definition Serialization.hpp:22
Definition pybind11Utils.hpp:21
std::shared_ptr< Grid3 > Grid3Ptr
Shared pointer type for Grid3.
Definition Grid.hpp:193
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
std::shared_ptr< Grid2 > Grid2Ptr
Shared pointer type for Grid.
Definition Grid.hpp:190