iMSTK
Interactive Medical Simulation Toolkit
imstkPbdInflatableVolumeConstraint.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 "imstkPbdVolumeConstraint.h"
10 #include "imstkPbdConstraintFunctor.h"
11 
12 namespace imstk
13 {
18 {
19 public:
21 
25  void initConstraint(const Vec3d& p0, const Vec3d& p1, const Vec3d& p2, const Vec3d& p3,
26  const PbdParticleId& pIdx0, const PbdParticleId& pIdx1,
27  const PbdParticleId& pIdx2, const PbdParticleId& pIdx3,
28  const double k = 2.0)
29  {
30  PbdVolumeConstraint::initConstraint(p0, p1, p2, p3, pIdx0, pIdx1, pIdx2, pIdx3, k);
31  m_initialRestVolume = m_restVolume;
32  }
33 
34  const double getRestVolume() const { return m_restVolume; }
35  const double getInitRestVolume() const { return m_initialRestVolume; }
36 
37  void setRestVolume(const double restVolume) { m_restVolume = restVolume; }
38  void setInitRestVolume(const double initRestVolume) { m_initialRestVolume = initRestVolume; }
39 
43  void resetRestVolume() { m_restVolume = m_initialRestVolume; }
44  void resetStiffness() { m_stiffness = m_initialStiffness; }
45 
46  bool isInflated() const { return m_inflated; }
47  void setInflated() { m_inflated = true; }
48 
49 protected:
50  double m_initialRestVolume = 0.0;
51  double m_initialStiffness = 1.0;
52  double m_diffusionRate = 0.00001;
53 
54  bool m_inflated = false;
55 };
56 
58 {
60  ~PbdInflatableVolumeConstraintFunctor() override = default;
61 
62  void operator()(PbdConstraintContainer& constraints) override
63  {
64  // Check for correct mesh type
65  CHECK(std::dynamic_pointer_cast<TetrahedralMesh>(m_geom) != nullptr)
66  << "PbdInflatableVolumeConstraint can only be generated with a TetrahedralMesh";
67 
68  // Create constraints
69  auto tetMesh = std::dynamic_pointer_cast<TetrahedralMesh>(m_geom);
70  std::shared_ptr<VecDataArray<double, 3>> verticesPtr = m_geom->getVertexPositions();
71  const VecDataArray<double, 3>& vertices = *verticesPtr;
72  std::shared_ptr<VecDataArray<int, 4>> elementsPtr = tetMesh->getCells();
73  const VecDataArray<int, 4>& elements = *elementsPtr;
74 
75  ParallelUtils::parallelFor(elements.size(),
76  [&](const int k)
77  {
78  auto& tet = elements[k];
79  auto c = std::make_shared<PbdInflatableVolumeConstraint>();
80  c->initConstraint(
81  vertices[tet[0]], vertices[tet[1]], vertices[tet[2]], vertices[tet[3]],
82  { m_bodyIndex, tet[0] }, { m_bodyIndex, tet[1] },
83  { m_bodyIndex, tet[2] }, { m_bodyIndex, tet[3] },
84  m_stiffness);
85  constraints.addConstraint(c);
86  });
87  }
88 };
89 } // imstk
void initConstraint(const Vec3d &p0, const Vec3d &p1, const Vec3d &p2, const Vec3d &p3, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const PbdParticleId &pIdx2, const PbdParticleId &pIdx3, const double k=2.0)
Initializes the volume constraint.
Container for pbd constraints.
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...
Compound Geometry.
void operator()(PbdConstraintContainer &constraints) override
Appends a set of constraint to the container given a geometry & body.
Represents a set of tetrahedrons & vertices via an array of Vec3d double vertices & Vec4i integer ind...
std::shared_ptr< VecDataArray< double, 3 > > getVertexPositions(DataType type=DataType::PostTransform) const
Returns the vector of current positions of the mesh vertices.
double m_stiffness
used in PBD, [0, 1]
void resetRestVolume()
Reset constraint rest volume.
void initConstraint(const Vec3d &p0, const Vec3d &p1, const Vec3d &p2, const Vec3d &p3, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const PbdParticleId &pIdx2, const PbdParticleId &pIdx3, const double k=2.0)
Initializes the inflatable volume constraint.
virtual void addConstraint(std::shared_ptr< PbdConstraint > constraint)
Adds a constraint to the system, thread safe.
Volume constraint for tetrahedral element.
PbdVolumeConstraintFunctor generates constraints per cell of a TetrahedralMesh.