iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkPbdModel.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkAbstractDynamicalModel.h"
10 #include "imstkPbdBody.h"
11 #include "imstkPbdConstraint.h"
12 
13 #include <unordered_map>
14 #include <unordered_set>
15 
16 namespace imstk
17 {
18 class PbdConstraintContainer;
19 class PbdModelConfig;
20 class PbdSolver;
21 
42 {
43 public:
44  PbdModel();
45  ~PbdModel() override = default;
46 
47  void resetToInitialState() override;
48 
52  void configure(std::shared_ptr<PbdModelConfig> params);
53 
57  std::shared_ptr<PbdBody> addBody();
58  void removeBody(std::shared_ptr<PbdBody> body);
60 
64  std::shared_ptr<PbdBody> getBody(size_t index) const;
65 
66  PbdState& getBodies() { return m_state; }
67 
73  const Vec3d& pos, const Quatd& orientation,
74  const double mass, const Mat3d inertia,
75  const Vec3d& velocity = Vec3d::Zero(), const Vec3d& angularVelocity = Vec3d::Zero(),
76  const bool persist = false);
77 
83  const Vec3d& pos, const double mass,
84  const Vec3d& velocity = Vec3d::Zero(),
85  const bool persist = false);
86 
90  void clearVirtualParticles();
91 
95  std::shared_ptr<PbdModelConfig> getConfig() const;
96 
103  void addConstraints(std::shared_ptr<std::unordered_set<size_t>> vertices, const int bodyId);
104 
105  void setTimeStep(const double timeStep) override;
106  double getTimeStep() const override;
107 
111  void setVelocityThreshold(double velCap) { m_velocityThreshold = velCap; }
112  double getVelocityThreshold() { return m_velocityThreshold; }
114 
118  std::shared_ptr<PbdConstraintContainer> getConstraints() { return m_constraints; }
119 
123  void integratePosition();
124  void integratePosition(PbdBody& body);
126 
130  void updateVelocity();
131  void updateVelocity(PbdBody& body);
133 
137  void solveConstraints();
138 
142  bool initialize() override;
143 
147  void setConstraintPartitionThreshold(size_t threshold) { m_partitionThreshold = threshold; }
148 
152  std::shared_ptr<PbdSolver> getSolver() const { return m_pbdSolver; }
153 
157  void setSolver(std::shared_ptr<PbdSolver> solver) { this->m_pbdSolver = solver; }
158 
159  std::shared_ptr<TaskNode> getIntegratePositionNode() const { return m_integrationPositionNode; }
160  std::shared_ptr<TaskNode> getSolveNode() const { return m_solveConstraintsNode; }
161  std::shared_ptr<TaskNode> getUpdateVelocityNode() const { return m_updateVelocityNode; }
162 
163 protected:
164  // Hide this function as PbdModel doesn't require it. It can support multiple bodies
166 
170  void resizeBodyParticles(PbdBody& body, const int particleCount);
171 
175  void initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink) override;
176 
177  size_t m_partitionThreshold = 16;
178 
179  bool m_modified = true;
180  int m_iterKey = 0;
181 
182  PbdState m_initialState;
183  PbdState m_state;
184 
185  std::shared_ptr<PbdSolver> m_pbdSolver = nullptr;
186  std::shared_ptr<PbdModelConfig> m_config = nullptr;
187  std::shared_ptr<PbdConstraintContainer> m_constraints;
188 
191  std::shared_ptr<TaskNode> m_integrationPositionNode = nullptr;
192  std::shared_ptr<TaskNode> m_solveConstraintsNode = nullptr;
193  std::shared_ptr<TaskNode> m_updateVelocityNode = nullptr;
195 
199  double m_velocityThreshold = 100000.0;
200 };
201 } // namespace imstk
Represents a pbd body in the model. This is a data only object. It does no function. PbdBody can be of different types. The types effect what properties it has.
Definition: imstkPbdBody.h:53
This class implements the position based dynamics model. The PbdModel is a constraint based model tha...
Definition: imstkPbdModel.h:41
void configure(std::shared_ptr< PbdModelConfig > params)
Set simulation parameters.
void setVelocityThreshold(double velCap)
Set/Get filter value for velocity, default is 10 in meters/second.
double m_velocityThreshold
Limit on velocity, assumes units in Meters. If any component of velocity exceeds value set here it wi...
std::pair< int, int > PbdParticleId
Index pair that refers to a particle in a PbdState. Index 0 is the body id, Index 1 is the particle i...
void addConstraints(std::shared_ptr< std::unordered_set< size_t >> vertices, const int bodyId)
Add/generate constraints for given set of vertices on the body, useful for topology changes...
void setTimeStep(const double timeStep) override
Set the time step size.
Compound Geometry.
void clearVirtualParticles()
Resize 0 the virtual particles.
std::shared_ptr< PbdConstraintContainer > m_constraints
The set of constraints to update/use.
void setConstraintPartitionThreshold(size_t threshold)
Set the threshold for constraint partitioning.
Abstract class for mathematical model of the physics governing the dynamic object.
void updateVelocity()
Time integrate the velocity.
bool initialize() override
Initialize the PBD model.
std::shared_ptr< PbdSolver > getSolver() const
Returns the solver used for internal constraints.
void resizeBodyParticles(PbdBody &body, const int particleCount)
Resize the amount of particles for a body.
int m_iterKey
Iterative key used for body ids.
PbdParticleId addVirtualParticle(const Vec3d &pos, const Quatd &orientation, const double mass, const Mat3d inertia, const Vec3d &velocity=Vec3d::Zero(), const Vec3d &angularVelocity=Vec3d::Zero(), const bool persist=false)
Add a particle to a virtual pool/buffer of particles for quick removal/insertion The persist flag ind...
void setModelGeometry(std::shared_ptr< Geometry > geometry)
Sets the model geometry.
std::shared_ptr< PbdSolver > m_pbdSolver
PBD solver.
std::shared_ptr< PbdBody > getBody(size_t index) const
void setSolver(std::shared_ptr< PbdSolver > solver)
Sets the solver used for internal constraints.
void integratePosition()
Time integrate the position.
std::shared_ptr< PbdModelConfig > m_config
Model parameters, must be set before simulation.
void solveConstraints()
Solve the internal constraints.
void resetToInitialState() override
Reset the current state to the initial state.
size_t m_partitionThreshold
Threshold for constraint partitioning.
std::shared_ptr< PbdModelConfig > getConfig() const
Get the simulation parameters.
std::shared_ptr< PbdConstraintContainer > getConstraints()
Return all constraints that are solved sequentially.
double getTimeStep() const override
Returns the time step size.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229
std::shared_ptr< PbdBody > addBody()
Add/remove PbdBody.
void initGraphEdges()
Initializes the edges of the graph.