iMSTK
Interactive Medical Simulation Toolkit
NeedleInteraction.cpp
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 #include "NeedleInteraction.h"
8 #include "imstkLineMesh.h"
9 #include "imstkPbdModel.h"
10 #include "imstkPbdObject.h"
11 #include "imstkPuncturable.h"
12 #include "imstkStraightNeedle.h"
13 #include "imstkTaskGraph.h"
14 #include "imstkTetrahedralMesh.h"
15 #include "imstkTetraToLineMeshCD.h"
16 #include "NeedleEmbedder.h"
17 
18 using namespace imstk;
19 
20 NeedleInteraction::NeedleInteraction(std::shared_ptr<PbdObject> tissueObj,
21  std::shared_ptr<PbdObject> needleObj,
22  const std::string& collisionName) :
23  PbdObjectCollision(tissueObj, needleObj, collisionName)
24 {
25  CHECK(needleObj->containsComponent<StraightNeedle>())
26  << "NeedleInteraction only works with objects that have a StraightNeedle component";
27  CHECK(tissueObj->containsComponent<Puncturable>())
28  << "NeedleInteraction only works with objects that have a Puncturable component";
29  CHECK(std::dynamic_pointer_cast<TetrahedralMesh>(tissueObj->getPhysicsGeometry()) != nullptr)
30  << "NeedleInteraction only works with TetrahedralMesh physics geometry on pbd tissueObj";
31 
32  // Assumes usage of physics geometry for this
33  m_embedder = std::make_shared<NeedleEmbedder>();
34  m_embedder->setCollisionHandleNode(m_collisionHandleANode);
35  m_embedder->setCollisionData(getCollisionDetection()->getCollisionData());
36  m_embedder->setTissueObject(tissueObj);
37  m_embedder->setNeedleObject(needleObj);
38 
39  // Needle interaction introduces its own collision detection step, handling, solve, and velocity correction
40  m_embedderNode =
41  std::make_shared<TaskNode>([&]() { m_embedder->update(); }, "NeedleEmbedding", true);
42  m_taskGraph->addNode(m_embedderNode);
43 }
44 
45 void
46 NeedleInteraction::setFriction(const double friction)
47 {
48  m_embedder->setFriction(friction);
49 }
50 
51 double
52 NeedleInteraction::getFriction() const
53 {
54  return m_embedder->getFriction();
55 }
56 
57 void
58 NeedleInteraction::setNeedleCompliance(const double compliance)
59 {
60  m_embedder->setCompliance(compliance);
61 }
62 
63 double
64 NeedleInteraction::getNeedleCompliance() const
65 {
66  return m_embedder->getCompliance();
67 }
68 
69 void
70 NeedleInteraction::setStaticFrictionForceThreshold(const double force)
71 {
72  m_embedder->setStaticFrictionForceThreshold(force);
73 }
74 
75 const double
76 NeedleInteraction::getStaticFrictionForceThreshold() const
77 {
78  return m_embedder->getStaticFrictionForceThreshold();
79 }
80 
81 void
82 NeedleInteraction::setPunctureForceThreshold(const double forceThreshold)
83 {
84  m_embedder->setPunctureForceThreshold(forceThreshold);
85 }
86 
87 const double
88 NeedleInteraction::getPunctureForceThreshold() const
89 {
90  return m_embedder->getPunctureForceThreshold();
91 }
92 
93 void
94 NeedleInteraction::initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink)
95 {
96  // Setup the usual collision interaction in the graph
97  // which adds contact constraints before the end of the pbd solve
98  PbdObjectCollision::initGraphEdges(source, sink);
99 
100  auto pbdObj = std::dynamic_pointer_cast<PbdObject>(m_objA);
101  std::shared_ptr<CollisionHandling> pbdCH = m_colHandlingA;
102 
103  // Collision detection should be done before so we can tell if touching or not
104  // This way state can transition Removed -> Touching -> Punctured in one step
105  m_taskGraph->addEdge(m_collisionDetectionNode, m_embedderNode);
106  m_taskGraph->addEdge(m_embedderNode, m_collisionHandleANode);
107 }
Place this on an object to make it puncturable by a needle. This allows puncturables to know they&#39;ve ...
Compound Geometry.
Definition of straight, single segment needle.
Base class for scene objects that move and/or deform under position based dynamics formulation...
This class defines a collision interaction between two PbdObjects or PbdObject & CollidingObject.
std::shared_ptr< TaskGraph > m_taskGraph
Computational Graph.
void initGraphEdges()
Initializes the edges of the SceneObject&#39;s computational graph.