courses/columbia-cg-animation/week04/FOSSSim/PenaltyForce.cpp
void PenaltyForce::addParticleParticleGradEToTotal(const VectorXs &x, int idx1, int idx2, VectorXs &gradE)
{
    int index1 = 2*idx1;
    int index2 = 2*idx2;
    
    VectorXs x1 = x.segment<2>(index1);
    VectorXs x2 = x.segment<2>(index2);
    
    double r1 = m_scene.getRadius(idx1);
    double r2 = m_scene.getRadius(idx2);
    
    VectorXs n = x2-x1;
    scalar nnorm = n.norm();
    
    VectorXs nhat = n;
    nhat.normalize();
    
    scalar size = r1 + r2 + m_thickness; 
    if(nnorm < size)
    {        
        gradE.segment<2>(index1) -= m_k * (nnorm - size) * nhat;
        gradE.segment<2>(index2) += m_k * (nnorm - size) * nhat;
    }    
}
// Adds the gradient of the penalty potential (-1 * force) for a pair of 
// particles to the total.
// Read the positions of the particles from the input variable x. Radii can
// be obtained from the member variable m_scene, the penalty force stiffness 
// from member variable m_k, and penalty force thickness from member variable
// m_thickness.
// Inputs:
//   x:    The positions of the particles in the scene. 
//   idx1: The index of the first particle, i.e. the position of this particle
//         is ( x[2*idx1], x[2*idx1+1] ).
//   idx2: The index of the second particle.
// Outputs:
//   gradE: The total gradient of penalty force. *ADD* the particle-particle
//          gradient to this total gradient.
Adds the gradient of the penalty potential (-1 * force) for a pair of
particles to the total.
Read the positions of the particles from the input variable x. Radii can
be obtained from the member variable m_scene, the penalty force stiffness
from member variable m_k, and penalty force thickness from member variable
m_thickness.
Inputs:
x:    The positions of the particles in the scene.
idx1: The index of the first particle, i.e. the position of this particle
is ( x[2*idx1], x[2*idx1+1] ).
idx2: The index of the second particle.
Outputs:
gradE: The total gradient of penalty force. *ADD* the particle-particle
gradient to this total gradient.
Adds the gradient of the penalty potential ( - 1 * force ) for a pair of particles to the total . Read the positions of the particles from the input variable x . Radii can be obtained from the member variable m_scene the penalty force stiffness from member variable m_k and penalty force thickness from member variable m_thickness . Inputs : x : The positions of the particles in the scene . idx1 : The index of the first particle i . e . the position of this particle is ( x [ 2 * idx1 ] x [ 2 * idx1 + 1 ] ) . idx2 : The index of the second particle . Outputs : gradE : The total gradient of penalty force . * ADD * the particle - particle gradient to this total gradient .
void PenaltyForce::addParticleParticleGradEToTotal(const VectorXs &x, int idx1, int idx2, VectorXs &gradE)
{
    int index1 = 2*idx1;
    int index2 = 2*idx2;
    VectorXs x1 = x.segment<2>(index1);
    VectorXs x2 = x.segment<2>(index2);
    double r1 = m_scene.getRadius(idx1);
    double r2 = m_scene.getRadius(idx2);
    VectorXs n = x2-x1;
    scalar nnorm = n.norm();
    VectorXs nhat = n;
    nhat.normalize();
    scalar size = r1 + r2 + m_thickness; 
    if(nnorm < size)
    {        
        gradE.segment<2>(index1) -= m_k * (nnorm - size) * nhat;
        gradE.segment<2>(index2) += m_k * (nnorm - size) * nhat;
    }    
}
void PenaltyForce :: addParticleParticleGradEToTotal ( const VectorXs & x , int idx1 , int idx2 , VectorXs & gradE ) { int index1 = 2 * idx1 ; int index2 = 2 * idx2 ; VectorXs x1 = x . segment < 2 > ( index1 ) ; VectorXs x2 = x . segment < 2 > ( index2 ) ; double r1 = m_scene . getRadius ( idx1 ) ; double r2 = m_scene . getRadius ( idx2 ) ; VectorXs n = x2 - x1 ; scalar nnorm = n . norm ( ) ; VectorXs nhat = n ; nhat . normalize ( ) ; scalar size = r1 + r2 + m_thickness ; if ( nnorm < size ) { gradE . segment < 2 > ( index1 ) -= m_k * ( nnorm - size ) * nhat ; gradE . segment < 2 > ( index2 ) += m_k * ( nnorm - size ) * nhat ; } }
Adds the gradient of the penalty potential (-1 * force) for a pair of
particles to the total.
Adds the gradient of the penalty potential ( - 1 * force ) for a pair of particles to the total .