iMSTK
Interactive Medical Simulation Toolkit
imstkSurfaceInsertionConstraint.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 "imstkSurfaceInsertionConstraint.h"
10 
11 using namespace imstk;
12 
13 void
14 SurfaceInsertionConstraint::initConstraint(
15  const Vec3d& insertionPoint,
16  const PbdParticleId& ptN,
17  const PbdParticleId& ptB1,
18  const PbdParticleId& ptB2,
19  const PbdParticleId& ptB3,
20  const Vec3d& contactPt,
21  const Vec3d& barycentricPt,
22  double stiffnessA,
23  double stiffnessB)
24 {
25  m_insertionPoint = insertionPoint;
26  m_contactPt = contactPt;
27  m_barycentricPt = barycentricPt;
28 
29  m_particles[0] = ptN;
30  m_particles[1] = ptB1;
31  m_particles[2] = ptB2;
32  m_particles[3] = ptB3;
33 
34  m_stiffness[0] = stiffnessA;
35  m_stiffness[1] = stiffnessB;
36 }
37 
38 bool
40  double& c, std::vector<Vec3d>& dcdx)
41 {
42  // Get current position of puncture point
43  // Move triangle to match motion of needle
44 
45  Vec3d diff = m_contactPt - m_insertionPoint;
46 
47  c = diff.norm();
48 
49  // If sufficiently close, do not solve constraint
50  if (c < 1E-8)
51  {
52  return false;
53  }
54 
55  diff.normalize();// gradient dcdx
56 
57  // Dont adjust position of needle, force mesh to follow needle
58  dcdx[0] = -1.0 * diff;// Vec3d::Zero(); // Vec3d::Zero(); // Not two-way
59 
60  // Weight by berycentric coordinates
61  dcdx[1] = diff * m_barycentricPt[0];
62  dcdx[2] = diff * m_barycentricPt[1];
63  dcdx[3] = diff * m_barycentricPt[2];
64 
65  return true;
66 }
bool computeValueAndGradient(PbdState &bodies, double &c, std::vector< Vec3d > &dcdx) override
Compute value and gradient of the constraint.
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.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229