summaryrefslogtreecommitdiff
path: root/src/BulletSoftBody/btDeformableBodySolver.cpp
blob: eb8e622377d27a69c103cfc5067908716b61f299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 Bullet Continuous Collision Detection and Physics Library
 Copyright (c) 2019 Google Inc. http://bulletphysics.org
 This software is provided 'as-is', without any express or implied warranty.
 In no event will the authors be held liable for any damages arising from the use of this software.
 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it freely,
 subject to the following restrictions:
 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
 3. This notice may not be removed or altered from any source distribution.
 */

#include <stdio.h>
#include <limits>
#include "btDeformableBodySolver.h"
#include "LinearMath/btQuickprof.h"

btDeformableBodySolver::btDeformableBodySolver()
: m_numNodes(0)
, m_cg(10)
{
    m_objective = new btDeformableBackwardEulerObjective(m_softBodySet, m_backupVelocity);
}

btDeformableBodySolver::~btDeformableBodySolver()
{
    delete m_objective;
}

void btDeformableBodySolver::solveConstraints(float solverdt)
{
    BT_PROFILE("solveConstraints");
    // add constraints to the solver
    setConstraints();
    
    // save v_{n+1}^* velocity after explicit forces
    backupVelocity();
    
    m_objective->computeResidual(solverdt, m_residual);
    
    computeStep(m_dv, m_residual);
    updateVelocity();
}

void btDeformableBodySolver::computeStep(TVStack& dv, const TVStack& residual)
{
    btScalar tolerance = std::numeric_limits<float>::epsilon()* 1024 * m_objective->computeNorm(residual);
    m_cg.solve(*m_objective, dv, residual, tolerance);
}

void btDeformableBodySolver::reinitialize(const btAlignedObjectArray<btSoftBody *>& softBodies, btScalar dt)
{
    m_softBodySet.copyFromArray(softBodies);
    bool nodeUpdated = updateNodes();
    
    if (nodeUpdated)
    {
        m_dv.resize(m_numNodes, btVector3(0,0,0));
        m_residual.resize(m_numNodes, btVector3(0,0,0));
        m_backupVelocity.resize(m_numNodes, btVector3(0,0,0));
    }
    
    // need to setZero here as resize only set value for newly allocated items
    for (int i = 0; i < m_numNodes; ++i)
    {
        m_dv[i].setZero();
        m_residual[i].setZero();
    }
    
    m_objective->reinitialize(nodeUpdated, dt);
}

void btDeformableBodySolver::setConstraints()
{
    BT_PROFILE("setConstraint");
    m_objective->setConstraints();
}

void btDeformableBodySolver::setWorld(btDeformableRigidDynamicsWorld* world)
{
    m_objective->setWorld(world);
}

void btDeformableBodySolver::updateVelocity()
{
    int counter = 0;
    for (int i = 0; i < m_softBodySet.size(); ++i)
    {
        btSoftBody* psb = m_softBodySet[i];
        for (int j = 0; j < psb->m_nodes.size(); ++j)
        {
            psb->m_nodes[j].m_v = m_backupVelocity[counter]+m_dv[counter];
            ++counter;
        }
    }
}

void btDeformableBodySolver::backupVelocity()
{
    int counter = 0;
    for (int i = 0; i < m_softBodySet.size(); ++i)
    {
        btSoftBody* psb = m_softBodySet[i];
        for (int j = 0; j < psb->m_nodes.size(); ++j)
        {
            m_backupVelocity[counter++] = psb->m_nodes[j].m_v;
        }
    }
}

void btDeformableBodySolver::revertVelocity()
{
    int counter = 0;
    for (int i = 0; i < m_softBodySet.size(); ++i)
    {
        btSoftBody* psb = m_softBodySet[i];
        for (int j = 0; j < psb->m_nodes.size(); ++j)
        {
            psb->m_nodes[j].m_v = m_backupVelocity[counter++];
        }
    }
}

bool btDeformableBodySolver::updateNodes()
{
    int numNodes = 0;
    for (int i = 0; i < m_softBodySet.size(); ++i)
        numNodes += m_softBodySet[i]->m_nodes.size();
    if (numNodes != m_numNodes)
    {
        m_numNodes = numNodes;
        return true;
    }
    return false;
}


void btDeformableBodySolver::predictMotion(float solverdt)
{
    for (int i = 0; i < m_softBodySet.size(); ++i)
    {
        btSoftBody *psb = m_softBodySet[i];
        
        if (psb->isActive())
        {
            // apply explicit forces to velocity
            m_objective->applyExplicitForce(m_residual);
            // predict motion for collision detection
            predictDeformableMotion(psb, solverdt);
        }
    }
}

void btDeformableBodySolver::predictDeformableMotion(btSoftBody* psb, btScalar dt)
{
    int i, ni;
    
    /* Prepare                */
    psb->m_sst.sdt = dt * psb->m_cfg.timescale;
    psb->m_sst.isdt = 1 / psb->m_sst.sdt;
    psb->m_sst.velmrg = psb->m_sst.sdt * 3;
    psb->m_sst.radmrg = psb->getCollisionShape()->getMargin();
    psb->m_sst.updmrg = psb->m_sst.radmrg * (btScalar)0.25;
    /* Integrate            */
    for (i = 0, ni = psb->m_nodes.size(); i < ni; ++i)
    {
        btSoftBody::Node& n = psb->m_nodes[i];
        n.m_q = n.m_x;
        n.m_x += n.m_v * dt;
    }
    /* Bounds                */
    psb->updateBounds();
    /* Nodes                */
    ATTRIBUTE_ALIGNED16(btDbvtVolume)
    vol;
    for (i = 0, ni = psb->m_nodes.size(); i < ni; ++i)
    {
        btSoftBody::Node& n = psb->m_nodes[i];
        vol = btDbvtVolume::FromCR(n.m_x, psb->m_sst.radmrg);
        psb->m_ndbvt.update(n.m_leaf,
                       vol,
                       n.m_v * psb->m_sst.velmrg,
                       psb->m_sst.updmrg);
    }

    /* Clear contacts        */
    psb->m_rcontacts.resize(0);
    psb->m_scontacts.resize(0);
    /* Optimize dbvt's        */
    psb->m_ndbvt.optimizeIncremental(1);
}

void btDeformableBodySolver::updateSoftBodies()
{
    for (int i = 0; i < m_softBodySet.size(); i++)
    {
        btSoftBody *psb = (btSoftBody *)m_softBodySet[i];
        if (psb->isActive())
        {
            psb->updateNormals(); // normal is updated here
        }
    }
}