iMSTK
Interactive Medical Simulation Toolkit
imstkPbdBendConstraint.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 "imstkPbdBendConstraint.h"
8 
9 namespace imstk
10 {
11 void
13  const Vec3d& p0, const Vec3d& p1, const Vec3d& p2,
14  const PbdParticleId& pIdx0, const PbdParticleId& pIdx1, const PbdParticleId& pIdx2,
15  const double k)
16 {
17  // Instead of using the angle between the segments we can use the distance
18  // from the center of the triangle
19  const Vec3d& center = (p0 + p1 + p2) / 3.0;
20 
21  initConstraint(pIdx0, pIdx1, pIdx2, (p1 - center).norm(), k);
22 }
23 
24 void
26  const PbdParticleId& pIdx0, const PbdParticleId& pIdx1, const PbdParticleId& pIdx2,
27  const double restLength,
28  const double k)
29 {
30  m_particles[0] = pIdx0;
31  m_particles[1] = pIdx1;
32  m_particles[2] = pIdx2;
33 
34  setStiffness(k);
35 
36  m_restLength = restLength;
37 }
38 
39 bool
41  double& c, std::vector<Vec3d>& dcdx)
42 {
43  const Vec3d& p0 = bodies.getPosition(m_particles[0]);
44  const Vec3d& p1 = bodies.getPosition(m_particles[1]);
45  const Vec3d& p2 = bodies.getPosition(m_particles[2]);
46 
47  // Move towards triangle center
48  const Vec3d& center = (p0 + p1 + p2) / 3.0;
49  const Vec3d& diff = p1 - center;
50  const double dist = diff.norm();
51 
52  if (dist < 1.0e-16)
53  {
54  return false;
55  }
56 
57  c = dist - m_restLength;
58 
59  dcdx[0] = (-2.0 / dist) * diff;
60  dcdx[1] = -2.0 * dcdx[0];
61  dcdx[2] = dcdx[0];
62 
63  return true;
64 }
65 } // namespace imstk
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 constraint function.
std::vector< PbdParticleId > m_particles
body, particle index
double m_restLength
Rest length.
Compound Geometry.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229
void initConstraint(const Vec3d &initPos0, const Vec3d &initPos1, const Vec3d &initPos2, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const PbdParticleId &pIdx2, const double k)
Initialize the constraint p0 \ \ p1 / / p2.