iMSTK
Interactive Medical Simulation Toolkit
imstkPbdVolumeConstraint.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 "imstkPbdVolumeConstraint.h"
8 
9 namespace imstk
10 {
11 void
13  const Vec3d& p0, const Vec3d& p1, const Vec3d& p2, const Vec3d& p3,
14  const PbdParticleId& pIdx0, const PbdParticleId& pIdx1,
15  const PbdParticleId& pIdx2, const PbdParticleId& pIdx3,
16  const double k)
17 {
18  m_particles[0] = pIdx0;
19  m_particles[1] = pIdx1;
20  m_particles[2] = pIdx2;
21  m_particles[3] = pIdx3;
22 
23  setStiffness(k);
24 
25  m_restVolume = (1.0 / 6.0) * ((p1 - p0).cross(p2 - p0)).dot(p3 - p0);
26 }
27 
28 bool
30  double& c, std::vector<Vec3d>& dcdx)
31 {
32  const Vec3d& x0 = bodies.getPosition(m_particles[0]);
33  const Vec3d& x1 = bodies.getPosition(m_particles[1]);
34  const Vec3d& x2 = bodies.getPosition(m_particles[2]);
35  const Vec3d& x3 = bodies.getPosition(m_particles[3]);
36 
37  const double onesixth = 1.0 / 6.0;
38 
39  dcdx[0] = onesixth * (x1 - x2).cross(x3 - x1);
40  dcdx[1] = onesixth * (x2 - x0).cross(x3 - x0);
41  dcdx[2] = onesixth * (x3 - x0).cross(x1 - x0);
42  dcdx[3] = onesixth * (x1 - x0).cross(x2 - x0);
43 
44  const double volume = dcdx[3].dot(x3 - x0);
45  c = volume - m_restVolume;
46  return true;
47 }
48 } // namespace imstk
void initConstraint(const Vec3d &p0, const Vec3d &p1, const Vec3d &p2, const Vec3d &p3, const PbdParticleId &pIdx0, const PbdParticleId &pIdx1, const PbdParticleId &pIdx2, const PbdParticleId &pIdx3, const double k=2.0)
Initializes the volume 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...
std::vector< PbdParticleId > m_particles
body, particle index
Compound Geometry.
bool computeValueAndGradient(PbdState &bodies, double &c, std::vector< Vec3d > &dcdx) override
Compute value and gradient of constraint function.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
Definition: imstkPbdBody.h:229