-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathShapeSpringsForceField.cpp
More file actions
executable file
·462 lines (362 loc) · 15.7 KB
/
Copy pathShapeSpringsForceField.cpp
File metadata and controls
executable file
·462 lines (362 loc) · 15.7 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 RC 1 *
* (c) 2006-2011 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library 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 library 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 library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_RGBDTRACKING_SHAPESPRINGSFORCEFIELD_CPP
#define SOFA_RGBDTRACKING_SHAPESPRINGSFORCEFIELD_CPP
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/behavior/ForceField.inl>
#include "ShapeSpringsForceField.h"
#include <sofa/core/visual/VisualParams.h>
#include <sofa/helper/system/config.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/defaulttype/RigidTypes.h>
#include <sofa/helper/gl/template.h>
#include <assert.h>
#include <iostream>
namespace sofa
{
namespace component
{
namespace forcefield
{
using namespace sofa::defaulttype;
SOFA_DECL_CLASS(ShapeSpringsForceField)
int ShapeSpringsForceFieldClass = core::RegisterObject("Simple elastic springs applied to given degrees of freedom between their current and rest shape position")
#ifndef SOFA_FLOAT
.add< ShapeSpringsForceField<Vec3dTypes> >()
#endif
#ifndef SOFA_DOUBLE
.add< ShapeSpringsForceField<Vec3fTypes> >()
#endif
;
#ifndef SOFA_FLOAT
template class SOFA_RGBDTRACKING_API ShapeSpringsForceField<Vec3dTypes>;
#endif
#ifndef SOFA_DOUBLE
template class SOFA_RGBDTRACKING_API ShapeSpringsForceField<Vec3fTypes>;
#endif
template<class DataTypes>
ShapeSpringsForceField<DataTypes>::ShapeSpringsForceField()
: points(initData(&points, "points", "points controlled by the rest shape springs"))
, stiffness(initData(&stiffness, "stiffness", "stiffness values between the actual position and the rest shape position"))
, angularStiffness(initData(&angularStiffness, "angularStiffness", "angularStiffness assigned when controlling the rotation of the points"))
, pivotPoints(initData(&pivotPoints, "pivot_points", "global pivot points used when translations instead of the rigid mass centers"))
, external_rest_shape(initData(&external_rest_shape, "external_rest_shape", "rest_shape can be defined by the position of an external Mechanical State"))
, external_points(initData(&external_points, "external_points", "points from the external Mechancial State that define the rest shape springs"))
, ext_points(initData(&ext_points, "extpoints", "points from the external Mechancial State that define the rest shape springs"))
, recompute_indices(initData(&recompute_indices, false, "recompute_indices", "Recompute indices (should be false for BBOX)"))
, drawSpring(initData(&drawSpring,false,"drawSpring","draw Spring"))
, springColor(initData(&springColor,"springColor","spring color"))
, restMState(NULL)
// , pp_0(NULL)
{
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::bwdInit()
{
core::behavior::ForceField<DataTypes>::init();
std::cout << " ok init " << std::endl;
if (stiffness.getValue().empty())
{
std::cout << "ShapeSpringsForceField : No stiffness is defined, assuming equal stiffness on each node, k = 100.0 " << std::endl;
VecReal stiffs;
stiffs.push_back(100.0);
stiffness.setValue(stiffs);
}
x0 = this->mstate->read(core::ConstVecCoordId::position())->getValue();
const std::string path = external_rest_shape.getValue();
restMState = NULL;
if (path.size() > 0)
{
this->getContext()->get(restMState ,path);
}
if (!restMState)
{
useRestMState = false;
if (path.size() > 0)
{
serr << "ShapeSpringsForceField : " << external_rest_shape.getValue() << " not found" << sendl;
}
}
else
{
useRestMState = true;
// sout << "ShapeSpringsForceField : Mechanical state named " << restMState->getName() << " found for RestShapeSpringFF named " << this->getName() << sendl;
}
this->k = stiffness.getValue();
recomputeIndices();
std::cout << " ok init 1 " << std::endl;
#ifdef SOFA_HAVE_EIGEN2
core::behavior::BaseMechanicalState* state = this->getContext()->getMechanicalState();
assert(state);
matS.resize(state->getMatrixSize(),state->getMatrixSize());
lastUpdatedStep = -1.0;
#endif
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::recomputeIndices()
{
m_indices.clear();
m_ext_indices.clear();
for (unsigned int i = 0; i < points.getValue().size(); i++)
m_indices.push_back(points.getValue()[i]);
for (unsigned int i = 0; i < external_points.getValue().size(); i++)
m_ext_indices.push_back(external_points.getValue()[i]);
m_pivots = pivotPoints.getValue();
if (m_indices.size()==0)
{
// std::cout << "in ShapeSpringsForceField no point are defined, default case: points = all points " << std::endl;
for (unsigned int i = 0; i < (unsigned)this->mstate->getSize(); i++)
{
m_indices.push_back(i);
}
}
if (m_ext_indices.size()==0)
{
// std::cout << "in ShapeSpringsForceField no external_points are defined, default case: points = all points " << std::endl;
if (useRestMState)
{
for (unsigned int i = 0; i < (unsigned)restMState->getSize(); i++)
{
m_ext_indices.push_back(i);
}
}
else
{
for (unsigned int i = 0; i < (unsigned)this->mstate->getSize(); i++)
{
m_ext_indices.push_back(i);
}
}
}
if (m_indices.size() > m_ext_indices.size())
{
std::cerr << "Error : the dimention of the source and the targeted points are different " << std::endl;
m_indices.clear();
}
}
template<class DataTypes>
const typename ShapeSpringsForceField<DataTypes>::DataVecCoord* ShapeSpringsForceField<DataTypes>::getExtPosition() const
{
return (useRestMState ? restMState->read(core::VecCoordId::position()) : this->mstate->read(core::VecCoordId::restPosition()));
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::addForce(const core::MechanicalParams* /* mparams */ /* PARAMS FIRST */, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& /* v */)
{
int t = (int)this->getContext()->getTime();
//std::cout << " ok add forces " << std::endl;
if (t == 0)
x0 = this->mstate->read(core::ConstVecCoordId::position())->getValue();
sofa::helper::WriteAccessor< DataVecDeriv > f1 = f;
sofa::helper::ReadAccessor< DataVecCoord > p1 = x;
sofa::helper::ReadAccessor< DataVecCoord > p0 = *getExtPosition();
VecCoord extpoints = ext_points.getValue();
/*for (int k = 0; k < x0.size() ; k++)
p0[k] = x0[k];*/
f1.resize(p1.size());
if (recompute_indices.getValue())
{
recomputeIndices();
}
//std::cout << " ok add forces 1 " << std::endl;
//Springs_dir.resize(m_indices.size() );
if ( k.size()!= m_indices.size() )
{
//sout << "WARNING : stiffness is not defined on each point, first stiffness is used" << sendl;
Real k0 = k[0];
for (unsigned int i=0; i<m_indices.size(); i++)
{
const unsigned int index = m_indices[i];
unsigned int ext_index = m_indices[i];
if(useRestMState)
ext_index= m_ext_indices[i];
//Deriv dx = p1[index] - p0[ext_index];
//std::cout << " ok add forces 1 " << extpoints.size() << std::endl;
Deriv dx = p1[index] - extpoints[index];
//if (t ==10 )
{
//std::cout << " expoints " << p1[index][0] << " " << extpoints[index][0] << std::endl;
//getchar();
}
//Springs_dir[i] = p1[index] - p0[ext_index];
//Springs_dir[i].normalize();
f1[index] -= dx * k0 ;
// if (dx.norm()>0.00000001)
// std::cout<<"force on point "<<index<<std::endl;
// Deriv dx = p[i] - p_0[i];
// f[ indices[i] ] -= dx * k[0] ;
}
}
else
{
for (unsigned int i=0; i<m_indices.size(); i++)
{
const unsigned int index = m_indices[i];
unsigned int ext_index = m_indices[i];
if(useRestMState)
ext_index= m_ext_indices[i];
//Deriv dx = p1[index] - p0[ext_index];
//std::cout << " ok add forces 2 " << std::endl;
Deriv dx = p1[index] - extpoints[ext_index];
//Springs_dir[i] = p1[index] - p0[ext_index];
//Springs_dir[i].normalize();
f1[index] -= dx * k[i];
// if (dx.norm()>0.00000001)
// std::cout<<"force on point "<<index<<std::endl;
// Deriv dx = p[i] - p_0[i];
// f[ indices[i] ] -= dx * k[i] ;
}
}
if (t > 0)
x0 = this->mstate->read(core::ConstVecCoordId::position())->getValue();
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::addDForce(const core::MechanicalParams* mparams /* PARAMS FIRST */, DataVecDeriv& df, const DataVecDeriv& dx)
{
// remove to be able to build in parallel
// const VecIndex& indices = points.getValue();
// const VecReal& k = stiffness.getValue();
sofa::helper::WriteAccessor< DataVecDeriv > df1 = df;
sofa::helper::ReadAccessor< DataVecDeriv > dx1 = dx;
Real kFactor = (Real)mparams->kFactorIncludingRayleighDamping(this->rayleighStiffness.getValue());
if (k.size()!= m_indices.size() )
{
//sout << "WARNING : stiffness is not defined on each point, first stiffness is used" << sendl;
const Real k0 = k[0];
for (unsigned int i=0; i<m_indices.size(); i++)
{
df1[m_indices[i]] -= dx1[m_indices[i]] * k0 * kFactor;
}
}
else
{
for (unsigned int i=0; i<m_indices.size(); i++)
{
df1[m_indices[i]] -= dx1[m_indices[i]] * k[i] * kFactor ;
}
}
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::draw(const core::visual::VisualParams * /* vparams */ )
{
//x0 = *this->mstate->getX();
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::addKToMatrix(const core::MechanicalParams* mparams /* PARAMS FIRST */, const sofa::core::behavior::MultiMatrixAccessor* matrix )
{
// remove to be able to build in parallel
// const VecIndex& indices = points.getValue();
// const VecReal& k = stiffness.getValue();
sofa::core::behavior::MultiMatrixAccessor::MatrixRef mref = matrix->getMatrix(this->mstate);
sofa::defaulttype::BaseMatrix* mat = mref.matrix;
unsigned int offset = mref.offset;
Real kFact = (Real)mparams->kFactorIncludingRayleighDamping(this->rayleighStiffness.getValue());
const int N = Coord::total_size;
unsigned int curIndex = 0;
if (k.size()!= m_indices.size() )
{
const Real k0 = k[0];
for (unsigned int index = 0; index < m_indices.size(); index++)
{
curIndex = m_indices[index];
for(int i = 0; i < N; i++)
{
// for (unsigned int j = 0; j < N; j++)
// {
// mat->add(offset + N * curIndex + i, offset + N * curIndex + j, kFact * k[0]);
// }
mat->add(offset + N * curIndex + i, offset + N * curIndex + i, -kFact * k0);
}
}
}
else
{
for (unsigned int index = 0; index < m_indices.size(); index++)
{
curIndex = m_indices[index];
for(int i = 0; i < N; i++)
{
// for (unsigned int j = 0; j < N; j++)
// {
// mat->add(offset + N * curIndex + i, offset + N * curIndex + j, kFact * k[curIndex]);
// }
mat->add(offset + N * curIndex + i, offset + N * curIndex + i, -kFact * k[index]);
}
}
}
}
template<class DataTypes>
void ShapeSpringsForceField<DataTypes>::addSubKToMatrix(const core::MechanicalParams* mparams /* PARAMS FIRST */, const sofa::core::behavior::MultiMatrixAccessor* matrix, const helper::vector<unsigned> & addSubIndex )
{
// remove to be able to build in parallel
// const VecIndex& indices = points.getValue();
// const VecReal& k = stiffness.getValue();
sofa::core::behavior::MultiMatrixAccessor::MatrixRef mref = matrix->getMatrix(this->mstate);
sofa::defaulttype::BaseMatrix* mat = mref.matrix;
unsigned int offset = mref.offset;
Real kFact = (Real)mparams->kFactorIncludingRayleighDamping(this->rayleighStiffness.getValue());
const int N = Coord::total_size;
unsigned int curIndex = 0;
if (k.size()!= m_indices.size() )
{
const Real k0 = k[0];
for (unsigned int index = 0; index < m_indices.size(); index++)
{
curIndex = m_indices[index];
bool contains=false;
for (unsigned s=0;s<addSubIndex.size() && !contains;s++) if (curIndex==addSubIndex[s]) contains=true;
if (!contains) continue;
for(int i = 0; i < N; i++)
{
// for (unsigned int j = 0; j < N; j++)
// {
// mat->add(offset + N * curIndex + i, offset + N * curIndex + j, kFact * k[0]);
// }
mat->add(offset + N * curIndex + i, offset + N * curIndex + i, -kFact * k0);
}
}
}
else
{
for (unsigned int index = 0; index < m_indices.size(); index++)
{
curIndex = m_indices[index];
bool contains=false;
for (unsigned s=0;s<addSubIndex.size() && !contains;s++) if (curIndex==addSubIndex[s]) contains=true;
if (!contains) continue;
for(int i = 0; i < N; i++)
{
// for (unsigned int j = 0; j < N; j++)
// {
// mat->add(offset + N * curIndex + i, offset + N * curIndex + j, kFact * k[curIndex]);
// }
mat->add(offset + N * curIndex + i, offset + N * curIndex + i, -kFact * k[index]);
}
}
}
}
} // namespace forcefield
} // namespace component
} // namespace sofa
#endif // SOFA_RGBDTRACKING_SHAPESPRINGSFORCEFIELD_INL