ParticleSystemSolver3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ParticleSystemSolver3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Basic 3-D particle system solver.
6 > Created Time: 2017/05/21
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_PARTICLE_SYSTEM_SOLVER3_H
10 #define CUBBYFLOW_PARTICLE_SYSTEM_SOLVER3_H
11 
17 #include <Core/Utils/Constants.h>
18 #include <Core/Vector/Vector3.h>
19 
20 namespace CubbyFlow
21 {
35  {
36  public:
37  class Builder;
38 
41 
43  ParticleSystemSolver3(double radius, double mass);
44 
46  virtual ~ParticleSystemSolver3();
47 
49  double GetDragCoefficient() const;
50 
59  void SetDragCoefficient(double newDragCoefficient);
60 
62  double GetRestitutionCoefficient() const;
63 
73  void SetRestitutionCoefficient(double newRestitutionCoefficient);
74 
76  const Vector3D& GetGravity() const;
77 
79  void SetGravity(const Vector3D& newGravity);
80 
90 
92  const Collider3Ptr& GetCollider() const;
93 
95  void SetCollider(const Collider3Ptr& newCollider);
96 
98  const ParticleEmitter3Ptr& GetEmitter() const;
99 
101  void SetEmitter(const ParticleEmitter3Ptr& newEmitter);
102 
104  const VectorField3Ptr& GetWind() const;
105 
114  void SetWind(const VectorField3Ptr& newWind);
115 
117  static Builder GetBuilder();
118 
119  protected:
121  void OnInitialize() override;
122 
124  void OnAdvanceTimeStep(double timeStepInSeconds) override;
125 
127  virtual void AccumulateForces(double timeStepInSeconds);
128 
130  virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds);
131 
133  virtual void OnEndAdvanceTimeStep(double timeStepInSeconds);
134 
136  void ResolveCollision();
137 
140  void ResolveCollision(ArrayAccessor1<Vector3D> newPositions, ArrayAccessor1<Vector3D> newVelocities);
141 
143  void SetParticleSystemData(const ParticleSystemData3Ptr& newParticles);
144 
145  private:
146  double m_dragCoefficient = 1e-4;
147  double m_restitutionCoefficient = 0.0;
148  Vector3D m_gravity = Vector3D(0.0, GRAVITY, 0.0);
149 
150  ParticleSystemData3Ptr m_particleSystemData;
151  ParticleSystemData3::VectorData m_newPositions;
152  ParticleSystemData3::VectorData m_newVelocities;
153  Collider3Ptr m_collider;
154  ParticleEmitter3Ptr m_emitter;
155  VectorField3Ptr m_wind;
156 
157  void BeginAdvanceTimeStep(double timeStepInSeconds);
158 
159  void EndAdvanceTimeStep(double timeStepInSeconds);
160 
161  void AccumulateExternalForces();
162 
163  void TimeIntegration(double timeStepInSeconds);
164 
165  void UpdateCollider(double timeStepInSeconds) const;
166 
167  void UpdateEmitter(double timeStepInSeconds) const;
168  };
169 
171  using ParticleSystemSolver3Ptr = std::shared_ptr<ParticleSystemSolver3>;
172 
176  template <typename DerivedBuilder>
178  {
179  public:
181  DerivedBuilder& WithRadius(double radius);
182 
184  DerivedBuilder& WithMass(double mass);
185 
186  protected:
187  double m_radius = 1e-3;
188  double m_mass = 1e-3;
189  };
190 
191  template <typename T>
193  {
194  m_radius = radius;
195  return static_cast<T&>(*this);
196  }
197 
198  template <typename T>
200  {
201  m_mass = mass;
202  return static_cast<T&>(*this);
203  }
204 
209  : public ParticleSystemSolverBuilderBase3<ParticleSystemSolver3::Builder>
210  {
211  public:
214 
217  };
218 }
219 
220 #endif
ParticleSystemSolver3Ptr MakeShared() const
Builds shared pointer of ParticleSystemSolver3 instance.
3-D vector class.
Definition: Vector3.h:26
Front-end to create ParticleSystemSolver3 objects step by step.
Definition: ParticleSystemSolver3.h:208
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider3.
Definition: Collider3.h:112
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.h:71
void SetWind(const VectorField3Ptr &newWind)
Sets the wind.
ParticleSystemSolver3 Build() const
Builds ParticleSystemSolver3.
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.h:34
std::shared_ptr< ParticleSystemData3 > ParticleSystemData3Ptr
Shared pointer type of ParticleSystemData3.
Definition: ParticleSystemData3.h:250
std::shared_ptr< VectorField3 > VectorField3Ptr
Shared pointer for the VectorField3 type.
Definition: VectorField3.h:44
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
Gravity.
Definition: Constants.h:156
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.h:177
1-D array class.
Definition: Array1.h:29
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.h:22
void SetRestitutionCoefficient(double newRestitutionCoefficient)
Sets the restitution coefficient.
DerivedBuilder & WithRadius(double radius)
Returns builder with particle radius.
Definition: ParticleSystemSolver3.h:192
Definition: pybind11Utils.h:24
double m_radius
Definition: ParticleSystemSolver3.h:187
virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds)
Called when a time-step is about to begin.
static Builder GetBuilder()
Returns builder fox ParticleSystemSolver3.
double GetRestitutionCoefficient() const
Gets the restitution coefficient.
double m_mass
Definition: ParticleSystemSolver3.h:188
1-D array accessor class.
Definition: ArrayAccessor1.h:28
double GetDragCoefficient() const
Returns the drag coefficient.
virtual ~ParticleSystemSolver3()
Destructor.
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.h:199
void SetDragCoefficient(double newDragCoefficient)
Sets the drag coefficient.
std::shared_ptr< ParticleSystemSolver3 > ParticleSystemSolver3Ptr
Shared pointer type for the ParticleSystemSolver3.
Definition: ParticleSystemSolver3.h:171
Vector3< double > Vector3D
Double-type 3D vector.
Definition: Vector3.h:353