ParticleSystemSolver3.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_SOLVER3_HPP
12 #define CUBBYFLOW_PARTICLE_SYSTEM_SOLVER3_HPP
13 
18 #include <Core/Matrix/Matrix.hpp>
20 #include <Core/Utils/Constants.hpp>
21 
22 namespace CubbyFlow
23 {
37 {
38  public:
39  class Builder;
40 
43 
45  ParticleSystemSolver3(double radius, double mass);
46 
49 
51  ParticleSystemSolver3(ParticleSystemSolver3&&) noexcept = delete;
52 
54  ~ParticleSystemSolver3() override = default;
55 
58 
61 
63  [[nodiscard]] double GetDragCoefficient() const;
64 
73  void SetDragCoefficient(double newDragCoefficient);
74 
76  [[nodiscard]] double GetRestitutionCoefficient() const;
77 
87  void SetRestitutionCoefficient(double newRestitutionCoefficient);
88 
90  [[nodiscard]] const Vector3D& GetGravity() const;
91 
93  void SetGravity(const Vector3D& newGravity);
94 
103  [[nodiscard]] const ParticleSystemData3Ptr& GetParticleSystemData() const;
104 
106  [[nodiscard]] const Collider3Ptr& GetCollider() const;
107 
109  void SetCollider(const Collider3Ptr& newCollider);
110 
112  [[nodiscard]] const ParticleEmitter3Ptr& GetEmitter() const;
113 
115  void SetEmitter(const ParticleEmitter3Ptr& newEmitter);
116 
118  [[nodiscard]] const VectorField3Ptr& GetWind() const;
119 
128  void SetWind(const VectorField3Ptr& newWind);
129 
131  [[nodiscard]] static Builder GetBuilder();
132 
133  protected:
135  void OnInitialize() override;
136 
138  void OnAdvanceTimeStep(double timeStepInSeconds) override;
139 
141  virtual void AccumulateForces(double timeStepInSeconds);
142 
144  virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds);
145 
147  virtual void OnEndAdvanceTimeStep(double timeStepInSeconds);
148 
150  void ResolveCollision();
151 
154  void ResolveCollision(ArrayView1<Vector3D> newPositions,
155  ArrayView1<Vector3D> newVelocities);
156 
158  void SetParticleSystemData(const ParticleSystemData3Ptr& newParticles);
159 
160  private:
161  void BeginAdvanceTimeStep(double timeStepInSeconds);
162 
163  void EndAdvanceTimeStep(double timeStepInSeconds);
164 
165  void AccumulateExternalForces();
166 
167  void TimeIntegration(double timeStepInSeconds);
168 
169  void UpdateCollider(double timeStepInSeconds) const;
170 
171  void UpdateEmitter(double timeStepInSeconds) const;
172 
173  double m_dragCoefficient = 1e-4;
174  double m_restitutionCoefficient = 0.0;
175  Vector3D m_gravity = Vector3D{ 0.0, GRAVITY, 0.0 };
176 
177  ParticleSystemData3Ptr m_particleSystemData;
178  ParticleSystemData3::VectorData m_newPositions;
179  ParticleSystemData3::VectorData m_newVelocities;
180  Collider3Ptr m_collider;
181  ParticleEmitter3Ptr m_emitter;
182  VectorField3Ptr m_wind;
183 };
184 
186 using ParticleSystemSolver3Ptr = std::shared_ptr<ParticleSystemSolver3>;
187 
191 template <typename DerivedBuilder>
193 {
194  public:
196  [[nodiscard]] DerivedBuilder& WithRadius(double radius);
197 
199  [[nodiscard]] DerivedBuilder& WithMass(double mass);
200 
201  protected:
202  double m_radius = 1e-3;
203  double m_mass = 1e-3;
204 };
205 
206 template <typename T>
208 {
209  m_radius = radius;
210  return static_cast<T&>(*this);
211 }
212 
213 template <typename T>
215 {
216  m_mass = mass;
217  return static_cast<T&>(*this);
218 }
219 
224  : public ParticleSystemSolverBuilderBase3<Builder>
225 {
226  public:
228  [[nodiscard]] ParticleSystemSolver3 Build() const;
229 
231  [[nodiscard]] ParticleSystemSolver3Ptr MakeShared() const;
232 };
233 } // namespace CubbyFlow
234 
235 #endif
~ParticleSystemSolver3() override=default
Default virtual destructor.
Front-end to create ParticleSystemSolver3 objects step by step.
Definition: ParticleSystemSolver3.hpp:223
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider3.
Definition: Collider.hpp:144
void OnInitialize() override
Initializes the simulator.
const Vector3D & GetGravity() const
Returns the gravity.
std::shared_ptr< ParticleEmitter3 > ParticleEmitter3Ptr
Shared pointer for the ParticleEmitter3 type.
Definition: ParticleEmitter3.hpp:94
void SetWind(const VectorField3Ptr &newWind)
Sets the wind.
void ResolveCollision()
Resolves any collisions occurred by the particles.
const Collider3Ptr & GetCollider() const
Returns the collider.
const ParticleEmitter3Ptr & GetEmitter() const
Returns the emitter.
Basic 3-D particle system solver.
Definition: ParticleSystemSolver3.hpp:36
std::shared_ptr< ParticleSystemData3 > ParticleSystemData3Ptr
Shared pointer type of ParticleSystemData3.
Definition: ParticleSystemData.hpp:284
std::shared_ptr< VectorField3 > VectorField3Ptr
Shared pointer for the VectorField3 type.
Definition: VectorField.hpp:90
virtual void AccumulateForces(double timeStepInSeconds)
Accumulates forces applied to the particles.
ParticleSystemSolver3()
Constructs an empty solver.
const VectorField3Ptr & GetWind() const
Returns the wind field.
constexpr double GRAVITY
Definition: Constants.hpp:299
const ParticleSystemData3Ptr & GetParticleSystemData() const
Returns the particle system data.
void SetParticleSystemData(const ParticleSystemData3Ptr &newParticles)
Assign a new particle system data.
void SetCollider(const Collider3Ptr &newCollider)
Sets the collider.
void SetEmitter(const ParticleEmitter3Ptr &newEmitter)
Sets the emitter.
Base class for particle-based solver builder.
Definition: ParticleSystemSolver3.hpp:192
Definition: Matrix.hpp:27
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.hpp:24
void SetRestitutionCoefficient(double newRestitutionCoefficient)
Sets the restitution coefficient.
DerivedBuilder & WithRadius(double radius)
Returns builder with particle radius.
Definition: ParticleSystemSolver3.hpp:207
Definition: pybind11Utils.hpp:20
Definition: Array-Impl.hpp:19
virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds)
Called when a time-step is about to begin.
static Builder GetBuilder()
Returns builder fox ParticleSystemSolver3.
ParticleSystemSolver3 & operator=(const ParticleSystemSolver3 &)=delete
Deleted copy assignment operator.
double GetRestitutionCoefficient() const
Gets the restitution coefficient.
Generic N-dimensional array class interface.
Definition: Array.hpp:32
double GetDragCoefficient() const
Returns the drag coefficient.
void SetGravity(const Vector3D &newGravity)
Sets the gravity.
virtual void OnEndAdvanceTimeStep(double timeStepInSeconds)
Called after a time-step is completed.
void OnAdvanceTimeStep(double timeStepInSeconds) override
Called to advance a single time-step.
DerivedBuilder & WithMass(double mass)
Returns builder with mass per particle.
Definition: ParticleSystemSolver3.hpp:214
void SetDragCoefficient(double newDragCoefficient)
Sets the drag coefficient.
std::shared_ptr< ParticleSystemSolver3 > ParticleSystemSolver3Ptr
Shared pointer type for the ParticleSystemSolver3.
Definition: ParticleSystemSolver3.hpp:186