-
Notifications
You must be signed in to change notification settings - Fork 299
/
DistanceFromTargetMapping.inl
391 lines (321 loc) · 14.7 KB
/
DistanceFromTargetMapping.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/******************************************************************************
* 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/DistanceFromTargetMapping.h>
#include <sofa/core/BaseLocalMappingMatrix.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/MechanicalParams.h>
#include <sofa/type/RGBAColor.h>
#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraintEigenUtils.h>
#include <iostream>
namespace sofa::component::mapping::nonlinear
{
template <class TIn, class TOut>
DistanceFromTargetMapping<TIn, TOut>::DistanceFromTargetMapping()
: Inherit()
, f_indices(initData(&f_indices, "indices", "Indices of the parent points"))
, f_targetPositions(initData(&f_targetPositions, "targetPositions", "Positions to compute the distances from"))
, f_restDistances(initData(&f_restDistances, "restLengths", "Rest lengths of the connections."))
, d_showObjectScale(initData(&d_showObjectScale, 0.f, "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])"))
{
}
template <class TIn, class TOut>
DistanceFromTargetMapping<TIn, TOut>::~DistanceFromTargetMapping()
{
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::createTarget(unsigned index, const InCoord &position, Real distance)
{
helper::WriteAccessor< Data< type::vector<Real> > > distances(f_restDistances);
helper::WriteAccessor< Data<type::vector<unsigned> > > indices(f_indices);
helper::WriteAccessor< Data<InVecCoord > > targetPositions(f_targetPositions);
indices.push_back(index);
targetPositions.push_back(position);
distances.push_back(distance);
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::updateTarget(unsigned index, const InCoord &position)
{
helper::WriteAccessor< Data<InVecCoord > > targetPositions(f_targetPositions);
helper::WriteAccessor< Data<type::vector<unsigned> > > indices(f_indices);
// find the target with given index
unsigned i=0; while(i<indices.size() && indices[i]!=index) i++;
targetPositions[i] = position;
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::updateTarget(unsigned index, SReal x, SReal y, SReal z)
{
InCoord pos;
TIn::set( pos, x, y, z );
updateTarget( index, pos );
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::clear()
{
helper::WriteAccessor< Data< type::vector<Real> > > distances(f_restDistances);
helper::WriteAccessor< Data<InVecCoord > > positions(f_targetPositions);
helper::WriteAccessor< Data<type::vector<unsigned> > > indices(f_indices);
distances.clear();
positions.clear();
indices.clear();
this->getToModel()->resize( 0 );
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::init()
{
assert( f_indices.getValue().size()==f_targetPositions.getValue().size()) ;
// unset distances are set to 0
if(f_restDistances.getValue().size() != f_indices.getValue().size())
{
helper::WriteAccessor< Data< type::vector<Real> > > distances(f_restDistances);
const unsigned prevsize = distances.size();
distances.resize( f_indices.getValue().size() );
for(unsigned i=prevsize; i<distances.size(); i++ )
distances[i] = 0;
}
this->getToModel()->resize( f_indices.getValue().size() );
baseMatrices.resize( 1 );
baseMatrices[0] = &jacobian;
this->Inherit::init(); // applies the mapping, so after the Data init
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::computeCoordPositionDifference( Direction& r, const InCoord& a, const InCoord& b )
{
r = TIn::getCPos(b)-TIn::getCPos(a);
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::apply(const core::MechanicalParams * /*mparams*/ , Data<OutVecCoord>& dOut, const Data<InVecCoord>& dIn)
{
helper::WriteAccessor< Data<OutVecCoord> > out = dOut;
helper::ReadAccessor< Data<InVecCoord> > in = dIn;
helper::WriteAccessor<Data<type::vector<Real> > > restDistances(f_restDistances);
const helper::ReadAccessor< Data<type::vector<unsigned> > > indices(f_indices);
helper::ReadAccessor< Data<InVecCoord > > targetPositions(f_targetPositions);
jacobian.resizeBlocks(out.size(),in.size());
directions.resize(out.size());
invlengths.resize(out.size());
for(unsigned i=0; i<indices.size() ; i++ )
{
Direction& gap = directions[i];
// gap = in[indices[i]] - targetPositions[i] (only for position)
computeCoordPositionDifference( gap, targetPositions[i], in[indices[i]] );
Real gapNorm = gap.norm();
out[i] = gapNorm - restDistances[i]; // output
if( gapNorm>1.e-10 )
{
invlengths[i] = 1/gapNorm;
gap *= invlengths[i];
}
else
{
invlengths[i] = 0;
gap = Direction();
gap[0]=1.0; // arbitrary unit vector
}
for(unsigned j=0; j<Nout; j++)
{
jacobian.beginRow(i*Nout+j);
for(unsigned k=0; k<Nin; k++ )
{
jacobian.insertBack( i*Nout+j, indices[i]*Nin+k, gap[k] );
}
}
}
jacobian.compress();
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::applyJ(const core::MechanicalParams * /*mparams*/ , Data<OutVecDeriv>& dOut, const Data<InVecDeriv>& dIn)
{
if( jacobian.rowSize() > 0 )
{
auto dOutWa = sofa::helper::getWriteOnlyAccessor(dOut);
auto dInRa = sofa::helper::getReadAccessor(dIn);
jacobian.mult(dOutWa.wref(),dInRa.ref());
}
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::applyJT(const core::MechanicalParams * /*mparams*/ , Data<InVecDeriv>& dIn, const Data<OutVecDeriv>& dOut)
{
if( jacobian.rowSize() > 0 )
{
auto dOutRa = sofa::helper::getReadAccessor(dOut);
auto dInWa = sofa::helper::getWriteOnlyAccessor(dIn);
jacobian.addMultTranspose(dInWa.wref(),dOutRa.ref());
}
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<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>
void DistanceFromTargetMapping<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()));
const helper::ReadAccessor< Data<type::vector<unsigned> > > indices(f_indices);
for(unsigned i=0; i<indices.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 )
{
sofa::type::Mat<Nin,Nin,Real> b; // = (I - uu^T)
for(unsigned j=0; j<Nin; j++)
{
for(unsigned k=0; k<Nin; k++)
{
b[j][k] = static_cast<Real>(1) * ( j==k ) - directions[i][j]*directions[i][k];
}
}
// (I - uu^T)*f/l*kfactor -- do not forget kfactor !
b *= (Real)(childForce[i][0] * invlengths[i] * kfactor);
// note that computing a block is not efficient here, but it would
// makes sense for storing a stiffness matrix
InDeriv dx = parentDisplacement[indices[i]];
InDeriv df;
for(unsigned j=0; j<Nin; j++)
{
for(unsigned k=0; k<Nin; k++)
{
df[j]+=b[j][k]*dx[k];
}
}
// InDeriv df = b*dx;
parentForce[indices[i]] += df;
}
}
}
template <class TIn, class TOut>
const sofa::linearalgebra::BaseMatrix* DistanceFromTargetMapping<TIn, TOut>::getJ()
{
return &jacobian;
}
template <class TIn, class TOut>
const type::vector<sofa::linearalgebra::BaseMatrix*>* DistanceFromTargetMapping<TIn, TOut>::getJs()
{
return &baseMatrices;
}
template <class TIn, class TOut>
const linearalgebra::BaseMatrix* DistanceFromTargetMapping<TIn, TOut>::getK()
{
return &K;
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::buildGeometricStiffnessMatrix(
sofa::core::GeometricStiffnessMatrix* matrices)
{
const unsigned geometricStiffness = d_geometricStiffness.getValue().getSelectedId();
if( !geometricStiffness )
{
return;
}
const auto childForce = this->toModel->readTotalForces();
helper::ReadAccessor< Data<type::vector<unsigned> > > indices(f_indices);
const auto dJdx = matrices->getMappingDerivativeIn(this->fromModel).withRespectToPositionsIn(this->fromModel);
for(sofa::Size i=0; i<indices.size(); i++)
{
const OutDeriv force_i = childForce[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 )
{
size_t idx = indices[i];
sofa::type::MatNoInit<Nin,Nin,Real> b; // = (I - uu^T)
for(unsigned j=0; j<Nin; j++)
{
for(unsigned k=0; k<Nin; k++)
{
b[j][k] = static_cast<Real>(1) * ( j==k ) - directions[i][j]*directions[i][k];
}
}
b *= force_i[0] * invlengths[i]; // (I - uu^T)*f/l
dJdx(idx * Nin, idx * Nin) += b;
}
}
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<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 helper::ReadAccessor< Data<type::vector<unsigned> > > indices(f_indices);
helper::ReadAccessor<Data<InVecCoord> > in (*this->fromModel->read(core::ConstVecCoordId::position()));
K.resizeBlocks(in.size(),in.size());
for(size_t i=0; i<indices.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 )
{
size_t idx = indices[i];
sofa::type::Mat<Nin,Nin,Real> b; // = (I - uu^T)
for(unsigned j=0; j<Nin; j++)
{
for(unsigned k=0; k<Nin; k++)
{
b[j][k] = static_cast<Real>(1) * ( j==k ) - directions[i][j]*directions[i][k];
}
}
b *= childForce[i][0] * invlengths[i]; // (I - uu^T)*f/l
K.addBlock(idx,idx,b);
}
}
K.compress();
}
template <class TIn, class TOut>
void DistanceFromTargetMapping<TIn, TOut>::draw(const core::visual::VisualParams* vparams)
{
if( !vparams->displayFlags().getShowMechanicalMappings() ) return;
const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
const float arrowsize = d_showObjectScale.getValue();
if( arrowsize<0 ) return;
typename core::behavior::MechanicalState<In>::ReadVecCoord pos = this->getFromModel()->readPositions();
helper::ReadAccessor< Data<InVecCoord > > targetPositions(f_targetPositions);
const helper::ReadAccessor< Data<type::vector<unsigned> > > indices(f_indices);
type::vector< sofa::type::Vec3 > points;
for(unsigned i=0; i<indices.size(); i++ )
{
points.push_back( sofa::type::Vec3(TIn::getCPos(targetPositions[i]) ) );
points.push_back( sofa::type::Vec3(TIn::getCPos(pos[indices[i]]) ) );
}
if( !arrowsize )
vparams->drawTool()->drawLines ( points, 1, d_color.getValue() );
else
for (unsigned int i=0; i<points.size()/2; ++i)
vparams->drawTool()->drawArrow( points[2*i+1], points[2*i], arrowsize, d_color.getValue() );
}
} // namespace sofa::component::mapping::nonlinear