iMSTK
Interactive Medical Simulation Toolkit
imstkPbdAreaConstraint.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 "imstkPbdAreaConstraint.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  m_particles[0] = pIdx0;
18  m_particles[1] = pIdx1;
19  m_particles[2] = pIdx2;
20  setStiffness(k);
21 
22  m_restArea = 0.5 * (p1 - p0).cross(p2 - p0).norm();
23 }
24 
25 bool
27  double& c, std::vector<Vec3d>& dcdx)
28 {
29  const Vec3d& p0 = bodies.getPosition(m_particles[0]);
30  const Vec3d& p1 = bodies.getPosition(m_particles[1]);
31  const Vec3d& p2 = bodies.getPosition(m_particles[2]);
32 
33  const Vec3d e0 = p0 - p1;
34  const Vec3d e1 = p1 - p2;
35  const Vec3d e2 = p2 - p0;
36 
37  Vec3d n = e0.cross(e1);
38  c = 0.5 * n.norm();
39 
40  if (c < 1.0e-16)
41  {
42  return false;
43  }
44 
45  n /= 2.0 * c;
46  c -= m_restArea;
47 
48  dcdx[0] = e1.cross(n);
49  dcdx[1] = e2.cross(n);
50  dcdx[2] = e0.cross(n);
51 
52  return true;
53 }
54 } // 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.
void initConstraint(const Vec3d &p0, const Vec3d &p1, const Vec3d &p2, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const PbdParticleId &pIdx2, const double k=2.5)
Initialize the constraint.
std::vector< PbdParticleId > m_particles
body, particle index
Compound Geometry.
double m_restArea
Area at the rest position.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229