ParticleSystemData.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_PARTICLE_SYSTEM_DATA_HPP
12 #define CUBBYFLOW_PARTICLE_SYSTEM_DATA_HPP
13 
14 #include <Core/Array/Array.hpp>
17 
18 #ifndef CUBBYFLOW_DOXYGEN
19 
20 namespace flatbuffers
21 {
22 class FlatBufferBuilder;
23 template <typename T>
24 struct Offset;
25 } // namespace flatbuffers
26 
27 namespace CubbyFlow
28 {
29 namespace fbs
30 {
31 struct ParticleSystemData2;
32 struct ParticleSystemData3;
33 } // namespace fbs
34 } // namespace CubbyFlow
35 
36 #endif
37 
38 namespace CubbyFlow
39 {
47 template <size_t N>
49 {
50  public:
53 
56 
59 
61  explicit ParticleSystemData(size_t numberOfParticles);
62 
64  ~ParticleSystemData() override = default;
65 
68 
70  ParticleSystemData(ParticleSystemData&& other) noexcept;
71 
73  ParticleSystemData& operator=(const ParticleSystemData& other);
74 
76  ParticleSystemData& operator=(ParticleSystemData&& other) noexcept;
77 
89  void Resize(size_t newNumberOfParticles);
90 
92  [[nodiscard]] size_t NumberOfParticles() const;
93 
102  [[nodiscard]] size_t AddScalarData(double initialVal = 0.0);
103 
112  [[nodiscard]] size_t AddVectorData(
113  const Vector<double, N>& initialVal = Vector<double, N>{});
114 
116  [[nodiscard]] double Radius() const;
117 
119  virtual void SetRadius(double newRadius);
120 
122  [[nodiscard]] double Mass() const;
123 
125  virtual void SetMass(double newMass);
126 
128  [[nodiscard]] ConstArrayView1<Vector<double, N>> Positions() const;
129 
131  [[nodiscard]] ArrayView1<Vector<double, N>> Positions();
132 
134  [[nodiscard]] ConstArrayView1<Vector<double, N>> Velocities() const;
135 
137  [[nodiscard]] ArrayView1<Vector<double, N>> Velocities();
138 
140  [[nodiscard]] ConstArrayView1<Vector<double, N>> Forces() const;
141 
143  [[nodiscard]] ArrayView1<Vector<double, N>> Forces();
144 
146  [[nodiscard]] ConstArrayView1<double> ScalarDataAt(size_t idx) const;
147 
149  [[nodiscard]] ArrayView1<double> ScalarDataAt(size_t idx);
150 
152  [[nodiscard]] ConstArrayView1<Vector<double, N>> VectorDataAt(
153  size_t idx) const;
154 
156  [[nodiscard]] ArrayView1<Vector<double, N>> VectorDataAt(size_t idx);
157 
172  void AddParticle(const Vector<double, N>& newPosition,
173  const Vector<double, N>& newVelocity = Vector<double, N>(),
174  const Vector<double, N>& newForce = Vector<double, N>());
175 
189  void AddParticles(const ConstArrayView1<Vector<double, N>>& newPositions,
190  const ConstArrayView1<Vector<double, N>>& newVelocities =
192  const ConstArrayView1<Vector<double, N>>& newForces =
194 
203  [[nodiscard]] const std::shared_ptr<PointNeighborSearcher<N>>&
204  NeighborSearcher() const;
205 
207  void SetNeighborSearcher(
208  const std::shared_ptr<PointNeighborSearcher<N>>& newNeighborSearcher);
209 
219  [[nodiscard]] const Array1<Array1<size_t>>& NeighborLists() const;
220 
222  void BuildNeighborSearcher(double maxSearchRadius);
223 
225  void BuildNeighborLists(double maxSearchRadius);
226 
228  void Serialize(std::vector<uint8_t>* buffer) const override;
229 
231  void Deserialize(const std::vector<uint8_t>& buffer) override;
232 
234  void Set(const ParticleSystemData& other);
235 
236  protected:
237  template <size_t M = N>
238  static std::enable_if_t<M == 2, void> Serialize(
239  const ParticleSystemData<2>& particles,
240  flatbuffers::FlatBufferBuilder* builder,
241  flatbuffers::Offset<fbs::ParticleSystemData2>* fbsParticleSystemData);
242 
243  template <size_t M = N>
244  static std::enable_if_t<M == 3, void> Serialize(
245  const ParticleSystemData<3>& particles,
246  flatbuffers::FlatBufferBuilder* builder,
247  flatbuffers::Offset<fbs::ParticleSystemData3>* fbsParticleSystemData);
248 
249  template <size_t M = N>
250  static std::enable_if_t<M == 2, void> Deserialize(
251  const fbs::ParticleSystemData2* fbsParticleSystemData,
252  ParticleSystemData<2>& particles);
253 
254  template <size_t M = N>
255  static std::enable_if_t<M == 3, void> Deserialize(
256  const fbs::ParticleSystemData3* fbsParticleSystemData,
257  ParticleSystemData<3>& particles);
258 
259  private:
260  double m_radius = 1e-3;
261  double m_mass = 1e-3;
262  size_t m_numberOfParticles = 0;
263  size_t m_positionIdx = 0;
264  size_t m_velocityIdx = 0;
265  size_t m_forceIdx = 0;
266 
267  Array1<ScalarData> m_scalarDataList;
268  Array1<VectorData> m_vectorDataList;
269 
270  std::shared_ptr<PointNeighborSearcher<N>> m_neighborSearcher;
271  Array1<Array1<size_t>> m_neighborLists;
272 };
273 
276 
279 
281 using ParticleSystemData2Ptr = std::shared_ptr<ParticleSystemData2>;
282 
284 using ParticleSystemData3Ptr = std::shared_ptr<ParticleSystemData3>;
285 } // namespace CubbyFlow
286 
287 #endif
Abstract base class for any serializable class.
Definition: Serialization.hpp:21
N-D particle system data.
Definition: ParticleSystemData.hpp:48
std::shared_ptr< ParticleSystemData3 > ParticleSystemData3Ptr
Shared pointer type of ParticleSystemData3.
Definition: ParticleSystemData.hpp:284
Abstract base class for N-D neighbor point searcher.
Definition: PointNeighborSearcher.hpp:32
ParticleSystemData< 3 > ParticleSystemData3
3-D ParticleSystemData type.
Definition: ParticleSystemData.hpp:278
ParticleSystemData< 2 > ParticleSystemData2
2-D ParticleSystemData type.
Definition: ParticleSystemData.hpp:275
void Serialize(const ConstArrayView1< T > &array, std::vector< uint8_t > *buffer)
Serializes data chunk using common schema.
Definition: Serialization-Impl.hpp:19
std::shared_ptr< ParticleSystemData2 > ParticleSystemData2Ptr
Shared pointer type of ParticleSystemData2.
Definition: ParticleSystemData.hpp:281
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
void Deserialize(const std::vector< uint8_t > &buffer, Array1< T > *array)
Deserializes buffer to data chunk using common schema.
Definition: Serialization-Impl.hpp:26
Definition: Array-Impl.hpp:19
Generic N-dimensional array class interface.
Definition: Array.hpp:32