-
Notifications
You must be signed in to change notification settings - Fork 297
/
SquareDistanceMapping.inl
344 lines (283 loc) · 13.4 KB
/
SquareDistanceMapping.inl
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/component/mapping/nonlinear/SquareDistanceMapping.h>
#include <sofa/core/BaseLocalMappingMatrix.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/MechanicalParams.h>
#include <iostream>
#include <sofa/simulation/Node.h>
#include <sofa/core/behavior/BaseForceField.h>
#include <sofa/core/behavior/MechanicalState.inl>
#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraintEigenUtils.h>
namespace sofa::component::mapping::nonlinear
{
template <class TIn, class TOut>
SquareDistanceMapping<TIn, TOut>::SquareDistanceMapping()
: Inherit()
, d_showObjectScale(initData(&d_showObjectScale, Real(0), "showObjectScale", "Scale for object display"))
, d_color(initData(&d_color, sofa::type::RGBAColor(1,1,0,1), "showColor", "Color for object display. (default=[1.0,1.0,0.0,1.0])"))
, l_topology(initLink("topology", "link to the topology container"))
{
}
template <class TIn, class TOut>
SquareDistanceMapping<TIn, TOut>::~SquareDistanceMapping()
{
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::init()
{
if (l_topology.empty())
{
msg_warning() << "link to Topology container should be set to ensure right behavior. First Topology found in current context will be used.";
l_topology.set(this->getContext()->getMeshTopologyLink());
}
msg_info() << "Topology path used: '" << l_topology.getLinkedPath() << "'";
if (l_topology->getNbEdges() < 1)
{
msg_error() << "No Topology component containing edges found at path: " << l_topology.getLinkedPath() << ", nor in current context: " << this->getContext()->name;
sofa::core::objectmodel::BaseObject::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
return;
}
const SeqEdges& links = l_topology->getEdges();
this->getToModel()->resize( links.size() );
// only used for warning message
const bool compliance = ((simulation::Node*)(this->getContext()))->forceField.size() && ((simulation::Node*)(this->getContext()))->forceField[0]->isCompliance.getValue();
msg_error_when(compliance) << "Null rest Lengths cannot be used for stable compliant constraint, prefer to use a DifferenceMapping if those dofs are used with a compliance";
baseMatrices.resize( 1 );
baseMatrices[0] = &jacobian;
this->Inherit::init(); // applies the mapping, so after the Data init
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::computeCoordPositionDifference( Direction& r, const InCoord& a, const InCoord& b )
{
r = TIn::getCPos(b)-TIn::getCPos(a);
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::apply(const core::MechanicalParams * /*mparams*/ , Data<OutVecCoord>& dOut, const Data<InVecCoord>& dIn)
{
helper::WriteOnlyAccessor< Data<OutVecCoord> > out = dOut;
helper::ReadAccessor< Data<InVecCoord> > in = dIn;
const SeqEdges& links = l_topology->getEdges();
jacobian.resizeBlocks(out.size(),in.size());
Direction gap;
for(unsigned i=0; i<links.size(); i++ )
{
const InCoord& p0 = in[links[i][0]];
const InCoord& p1 = in[links[i][1]];
// gap = in[links[i][1]] - in[links[i][0]] (only for position)
computeCoordPositionDifference( gap, p0, p1 );
// if d = N - R ==> d² = N² + R² - 2.N.R
const Real gapNorm = gap.norm2();
out[i][0] = gapNorm; // d = N²
// insert in increasing column order
gap *= 2; // 2*p[1]-2*p[0]
// if( restLengths[i] )
// {
// out[i][0] -= ( 2*sqrt(gapNorm) + restLengths[i] ) * restLengths[i]; // d = N² + R² - 2.N.R
// // TODO implement Jacobian when restpos != 0
// // gap -= d2NR/dx
// }
jacobian.beginRow(i);
if( links[i][1]<links[i][0] )
{
for(unsigned k=0; k<In::spatial_dimensions; k++ )
jacobian.insertBack( i, links[i][1]*Nin+k, gap[k] );
for(unsigned k=0; k<In::spatial_dimensions; k++ )
jacobian.insertBack( i, links[i][0]*Nin+k, -gap[k] );
}
else
{
for(unsigned k=0; k<In::spatial_dimensions; k++ )
jacobian.insertBack( i, links[i][0]*Nin+k, -gap[k] );
for(unsigned k=0; k<In::spatial_dimensions; k++ )
jacobian.insertBack( i, links[i][1]*Nin+k, gap[k] );
}
}
jacobian.compress();
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::applyJ(const core::MechanicalParams * /*mparams*/ , Data<OutVecDeriv>& dOut, const Data<InVecDeriv>& dIn)
{
if( jacobian.rowSize() )
{
auto dOutWa = sofa::helper::getWriteOnlyAccessor(dOut);
auto dInRa = sofa::helper::getReadAccessor(dIn);
jacobian.mult(dOutWa.wref(),dInRa.ref());
}
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::applyJT(const core::MechanicalParams * /*mparams*/ , Data<InVecDeriv>& dIn, const Data<OutVecDeriv>& dOut)
{
if( jacobian.rowSize() )
{
auto dOutRa = sofa::helper::getReadAccessor(dOut);
auto dInWa = sofa::helper::getWriteOnlyAccessor(dIn);
jacobian.addMultTranspose(dInWa.wref(),dOutRa.ref());
}
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::applyDJT(const core::MechanicalParams* mparams, core::MultiVecDerivId parentDfId, core::ConstMultiVecDerivId )
{
const unsigned& geometricStiffness = d_geometricStiffness.getValue().getSelectedId();
if( !geometricStiffness ) return;
helper::WriteAccessor<Data<InVecDeriv> > parentForce (*parentDfId[this->fromModel.get()].write());
helper::ReadAccessor<Data<InVecDeriv> > parentDisplacement (*mparams->readDx(this->fromModel.get())); // parent displacement
const SReal& kfactor = mparams->kFactor();
helper::ReadAccessor<Data<OutVecDeriv> > childForce (*mparams->readF(this->toModel.get()));
if( K.compressedMatrix.nonZeros() )
{
K.addMult( parentForce.wref(), parentDisplacement.ref(), (typename In::Real)kfactor );
}
else
{
const SeqEdges& links = l_topology->getEdges();
for(unsigned i=0; i<links.size(); i++ )
{
// force in compression (>0) can lead to negative eigen values in geometric stiffness
// this results in a undefinite implicit matrix that causes instabilies
// if stabilized GS (geometricStiffness==2) -> keep only force in extension
if( childForce[i][0] < 0 || geometricStiffness==1 )
{
SReal tmp = 2*childForce[i][0]*kfactor;
InDeriv df = (parentDisplacement[links[i][0]]-parentDisplacement[links[i][1]])*tmp;
// it is symmetric so -df = (parentDisplacement[links[i][1]]-parentDisplacement[links[i][0]])*tmp;
parentForce[links[i][0]] += df;
parentForce[links[i][1]] -= df;
}
}
}
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::applyJT(const core::ConstraintParams* cparams, Data<InMatrixDeriv>& out, const Data<OutMatrixDeriv>& in)
{
SOFA_UNUSED(cparams);
auto childMatRa = sofa::helper::getReadAccessor(in);
auto parentMatWa = sofa::helper::getWriteAccessor(out);
addMultTransposeEigen(parentMatWa.wref(), jacobian.compressedMatrix, childMatRa.ref());
}
template <class TIn, class TOut>
const sofa::linearalgebra::BaseMatrix* SquareDistanceMapping<TIn, TOut>::getJ()
{
return &jacobian;
}
template <class TIn, class TOut>
const type::vector<sofa::linearalgebra::BaseMatrix*>* SquareDistanceMapping<TIn, TOut>::getJs()
{
return &baseMatrices;
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::updateK(const core::MechanicalParams *mparams, core::ConstMultiVecDerivId childForceId )
{
SOFA_UNUSED(mparams);
const unsigned geometricStiffness = d_geometricStiffness.getValue().getSelectedId();
if( !geometricStiffness ) { K.resize(0,0); return; }
helper::ReadAccessor<Data<OutVecDeriv> > childForce( *childForceId[this->toModel.get()].read() );
const SeqEdges& links = l_topology->getEdges();
unsigned int size = this->fromModel->getSize();
K.resizeBlocks(size,size);
for(size_t i=0; i<links.size(); i++)
{
// force in compression (>0) can lead to negative eigen values in geometric stiffness
// this results in a undefinite implicit matrix that causes instabilies
// if stabilized GS (geometricStiffness==2) -> keep only force in extension
if( childForce[i][0] < 0 || geometricStiffness==1 )
{
SReal tmp = 2*childForce[i][0];
for(unsigned k=0; k<In::spatial_dimensions; k++)
{
K.add( links[i][0]*Nin+k, links[i][0]*Nin+k, tmp );
K.add( links[i][0]*Nin+k, links[i][1]*Nin+k, -tmp );
K.add( links[i][1]*Nin+k, links[i][1]*Nin+k, tmp );
K.add( links[i][1]*Nin+k, links[i][0]*Nin+k, -tmp );
}
}
}
K.compress();
}
template <class TIn, class TOut>
const linearalgebra::BaseMatrix* SquareDistanceMapping<TIn, TOut>::getK()
{
return &K;
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::buildGeometricStiffnessMatrix(
sofa::core::GeometricStiffnessMatrix* matrices)
{
const unsigned geometricStiffness = d_geometricStiffness.getValue().getSelectedId();
if( !geometricStiffness )
{
return;
}
const auto childForce = this->toModel->readTotalForces();
const SeqEdges& links = l_topology->getEdges();
const auto dJdx = matrices->getMappingDerivativeIn(this->fromModel).withRespectToPositionsIn(this->fromModel);
for(sofa::Size i=0; i<links.size(); i++)
{
const OutDeriv force_i = childForce[i];
const sofa::topology::Edge link = links[i];
// force in compression (>0) can lead to negative eigen values in geometric stiffness
// this results in a undefinite implicit matrix that causes instabilies
// if stabilized GS (geometricStiffness==2) -> keep only force in extension
if( force_i[0] < 0 || geometricStiffness==1 )
{
const Real tmp = 2 * force_i[0];
for(unsigned k=0; k<In::spatial_dimensions; k++)
{
dJdx(link[0] * Nin + k, link[0] * Nin + k) += tmp;
dJdx(link[0] * Nin + k, link[1] * Nin + k) += -tmp;
dJdx(link[1] * Nin + k, link[0] * Nin + k) += -tmp;
dJdx(link[1] * Nin + k, link[1] * Nin + k) += tmp;
}
}
}
}
template <class TIn, class TOut>
void SquareDistanceMapping<TIn, TOut>::draw(const core::visual::VisualParams* vparams)
{
if( !vparams->displayFlags().getShowMechanicalMappings() ) return;
const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
typename core::behavior::MechanicalState<In>::ReadVecCoord pos = this->getFromModel()->readPositions();
const SeqEdges& links = l_topology->getEdges();
if( d_showObjectScale.getValue() == 0 )
{
vparams->drawTool()->disableLighting();
type::vector< type::Vec3 > points;
for(std::size_t i=0; i<links.size(); i++ )
{
points.push_back( sofa::type::Vec3( TIn::getCPos(pos[links[i][0]]) ) );
points.push_back( sofa::type::Vec3( TIn::getCPos(pos[links[i][1]]) ));
}
vparams->drawTool()->drawLines ( points, 1, d_color.getValue() );
}
else
{
vparams->drawTool()->enableLighting();
for(std::size_t i=0; i<links.size(); i++ )
{
type::Vec3 p0 = TIn::getCPos(pos[links[i][0]]);
type::Vec3 p1 = TIn::getCPos(pos[links[i][1]]);
vparams->drawTool()->drawCylinder( p0, p1, (float)d_showObjectScale.getValue(), d_color.getValue() );
}
}
}
} // namespace sofa::component::mapping::nonlinear