iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkThreadInsertionConstraint.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 #pragma once
8 
9 #include "imstkThreadInsertionConstraint.h"
10 
11 using namespace imstk;
12 
13 void
14 ThreadInsertionConstraint::initConstraint(
15  const PbdState& bodies,
16  const PbdParticleId& ptA1,
17  const PbdParticleId& ptA2,
18  const Vec2d& threadBaryPoint,
19  const PbdParticleId& ptB1,
20  const PbdParticleId& ptB2,
21  const PbdParticleId& ptB3,
22  const Vec3d& triBaryPoint,
23  double stiffnessA,
24  double stiffnessB)
25 {
26  // Vertex mass pairs for thread
27  m_particles[0] = ptA1;
28  m_particles[1] = ptA2;
29 
30  // Barycentric coordinate on thread of intersection point
31  m_threadBaryPt = threadBaryPoint;
32 
33  // Computing world coordinates of intersecting point along thread
34  m_threadInsertionPoint = m_threadBaryPt[0] * bodies.getPosition(m_particles[0])
35  + m_threadBaryPt[1] * bodies.getPosition(m_particles[1]);
36 
37  // Vertex mass pairs for triangle
38  m_particles[2] = ptB1;
39  m_particles[3] = ptB2;
40  m_particles[4] = ptB3;
41 
42  // Barycentric coordinate of puncture point on triangle
43  m_triangleBaryPt = triBaryPoint;
44 
45  // Computing world coordinates of puncture point
46  m_triInsertionPoint = m_triangleBaryPt[0] * bodies.getPosition(m_particles[2])
47  + m_triangleBaryPt[1] * bodies.getPosition(m_particles[3])
48  + m_triangleBaryPt[2] * bodies.getPosition(m_particles[4]);
49 
50  // Saving stiffness
51  m_stiffness[0] = stiffnessA;
52  m_stiffness[1] = stiffnessB;
53 }
54 
55 bool
57  double& c, std::vector<Vec3d>& dcdx)
58 {
59  // \todo: No reprojection is done here so smooth multi iteration solve is not possible
60  // two-way solve not possible
61 
62  // Move thread such that the thread stays intersected with the
63  // puncture point on the triangle
64 
65  Vec3d diff = m_triInsertionPoint - m_threadInsertionPoint; // gradient dcdx
66  c = diff.norm();
67 
68  // If sufficiently close, do not solve constraint
69  if (c < 1E-8)
70  {
71  c = 0.0;
72  return false;
73  }
74  diff.normalize();
75 
76  // Move thread to follow insertion point
77  dcdx[0] = diff * m_threadBaryPt[0];
78  dcdx[1] = diff * m_threadBaryPt[1];
79 
80  // Move triangle to follow thread point (WARNING: Currently inactive)
81  dcdx[2] = -diff * m_triangleBaryPt[0];
82  dcdx[3] = -diff * m_triangleBaryPt[1];
83  dcdx[4] = -diff * m_triangleBaryPt[2];
84 
85  return true;
86 }
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...
bool computeValueAndGradient(PbdState &bodies, double &c, std::vector< Vec3d > &dcdx) override
Compute value and gradient of the constraint.
Compound Geometry.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229