courses/columbia-cg-animation/week04/FOSSSim/PenaltyForce.cpp
void PenaltyForce::addParticleHalfplaneGradEToTotal(const VectorXs &x, int vidx, int pidx, VectorXs &gradE)
{
    int index1 = 2*vidx;
    
    VectorXs x1 = x.segment<2>(index1);
    std::pair<VectorXs, VectorXs> plane = m_scene.getHalfplane(pidx);
    VectorXs pdir = plane.first;
    VectorXs pn = plane.second;
    
    scalar pndotpn = pn.dot(pn);
    VectorXs n = ((pdir - x1).dot(pn)/pndotpn)*pn;
    scalar nnorm = n.norm();
    
    VectorXs nhat = n;
    nhat.normalize();
    
    double r = m_scene.getRadius(vidx);    
    scalar size = r + m_thickness;
    if(nnorm < size)
    {
        gradE.segment<2>(index1) -= m_k*(nnorm-size)*nhat.dot(pn)/pndotpn*pn;
    }
    
}
// Adds the gradient of the penalty potential (-1 * force) for a particle-
// half-plane pair to the total.
// Read the positions of the particle from the input variable x.
// Inputs:
//   x:    The positions of the particles in the scene.
//   vidx: The index of the particle.
//   pidx: The index of the half-plane, i.e. the position and normal vectors
//         for the half-plane can be retrieved by calling
//         m_scene.getHalfplane(pidx).
// Outputs:
//   gradE: The total gradient of the penalty force. *ADD* the particle-
//          half-plane gradient to this total gradient.
Adds the gradient of the penalty potential (-1 * force) for a particle
half-plane pair to the total.
Read the positions of the particle from the input variable x.
Inputs:
x:    The positions of the particles in the scene.
vidx: The index of the particle.
pidx: The index of the half-plane, i.e. the position and normal vectors
for the half-plane can be retrieved by calling
m_scene.getHalfplane(pidx).
Outputs:
gradE: The total gradient of the penalty force. *ADD* the particle
half-plane gradient to this total gradient.
Adds the gradient of the penalty potential ( - 1 * force ) for a particle half - plane pair to the total . Read the positions of the particle from the input variable x . Inputs : x : The positions of the particles in the scene . vidx : The index of the particle . pidx : The index of the half - plane i . e . the position and normal vectors for the half - plane can be retrieved by calling m_scene . getHalfplane ( pidx ) . Outputs : gradE : The total gradient of the penalty force . * ADD * the particle half - plane gradient to this total gradient .
void PenaltyForce::addParticleHalfplaneGradEToTotal(const VectorXs &x, int vidx, int pidx, VectorXs &gradE)
{
    int index1 = 2*vidx;
    VectorXs x1 = x.segment<2>(index1);
    std::pair<VectorXs, VectorXs> plane = m_scene.getHalfplane(pidx);
    VectorXs pdir = plane.first;
    VectorXs pn = plane.second;
    scalar pndotpn = pn.dot(pn);
    VectorXs n = ((pdir - x1).dot(pn)/pndotpn)*pn;
    scalar nnorm = n.norm();
    VectorXs nhat = n;
    nhat.normalize();
    double r = m_scene.getRadius(vidx);    
    scalar size = r + m_thickness;
    if(nnorm < size)
    {
        gradE.segment<2>(index1) -= m_k*(nnorm-size)*nhat.dot(pn)/pndotpn*pn;
    }
}
void PenaltyForce :: addParticleHalfplaneGradEToTotal ( const VectorXs & x , int vidx , int pidx , VectorXs & gradE ) { int index1 = 2 * vidx ; VectorXs x1 = x . segment < 2 > ( index1 ) ; std :: pair < VectorXs , VectorXs > plane = m_scene . getHalfplane ( pidx ) ; VectorXs pdir = plane . first ; VectorXs pn = plane . second ; scalar pndotpn = pn . dot ( pn ) ; VectorXs n = ( ( pdir - x1 ) . dot ( pn ) / pndotpn ) * pn ; scalar nnorm = n . norm ( ) ; VectorXs nhat = n ; nhat . normalize ( ) ; double r = m_scene . getRadius ( vidx ) ; scalar size = r + m_thickness ; if ( nnorm < size ) { gradE . segment < 2 > ( index1 ) -= m_k * ( nnorm - size ) * nhat . dot ( pn ) / pndotpn * pn ; } }
Adds the gradient of the penalty potential (-1 * force) for a particle
half-plane pair to the total.
Adds the gradient of the penalty potential ( - 1 * force ) for a particle half - plane pair to the total .