iMSTK
Interactive Medical Simulation Toolkit
imstkPbdDistanceConstraint.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 "imstkPbdDistanceConstraint.h"
8 
9 namespace imstk
10 {
11 void
13  const double restLength,
14  const PbdParticleId& pIdx0, const PbdParticleId& pIdx1,
15  const double k)
16 {
17  m_particles[0] = pIdx0;
18  m_particles[1] = pIdx1;
19  setStiffness(k);
20 
21  m_restLength = restLength;
22 }
23 
24 bool
26  double& c, std::vector<Vec3d>& dcdx)
27 {
28  const Vec3d& p0 = bodies.getPosition(m_particles[0]);
29  const Vec3d& p1 = bodies.getPosition(m_particles[1]);
30 
31  dcdx[0] = p0 - p1;
32  const double len = dcdx[0].norm();
33  if (len < 1.0e-16)
34  {
35  return false;
36  }
37  dcdx[0] /= len;
38  dcdx[1] = -dcdx[0];
39  c = len - m_restLength;
40 
41  return true;
42 }
43 } // namespace imstk
double m_restLength
Rest length between the nodes.
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...
std::vector< PbdParticleId > m_particles
body, particle index
Compound Geometry.
void initConstraint(const Vec3d &p0, const Vec3d &p1, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const double k=1e5)
Initialize the constraint with resting length as the length between the two points.
bool computeValueAndGradient(PbdState &bodies, double &c, std::vector< Vec3d > &dcdx) override
Compute value and gradient of the constraint.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229