File: TriangularQuadraticSpringsForceField.inl

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (458 lines) | stat: -rw-r--r-- 15,770 bytes parent folder | download | duplicates (5)
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
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 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                             *
******************************************************************************/
#include <sofa/component/forcefield/TriangularQuadraticSpringsForceField.h>
#include <fstream> // for reading the file
#include <iostream> //for debugging
#include <sofa/helper/gl/template.h>
#include <sofa/component/topology/TriangleSetGeometryAlgorithms.h>
#include <sofa/component/topology/TriangleData.inl>
#include <sofa/component/topology/EdgeData.inl>
#include <sofa/core/componentmodel/behavior/ForceField.inl>

namespace sofa
{

namespace component
{

namespace forcefield
{

using namespace sofa::defaulttype;
using namespace	sofa::component::topology;
using namespace core::componentmodel::topology;





template< class DataTypes>
void TriangularQuadraticSpringsForceField<DataTypes>::TRQSEdgeCreationFunction(int edgeIndex, void* param, EdgeRestInformation &ei,
							const Edge& ,  const sofa::helper::vector< unsigned int > &,
							  const sofa::helper::vector< double >&)
{
	TriangularQuadraticSpringsForceField<DataTypes> *ff= (TriangularQuadraticSpringsForceField<DataTypes> *)param;
	if (ff) {

		sofa::component::topology::TriangleSetGeometryAlgorithms<DataTypes>* triangleGeo; 
		ff->getContext()->get(triangleGeo);

		// store the rest length of the edge created 
		ei.restLength=triangleGeo->computeRestEdgeLength(edgeIndex);
		ei.stiffness=0;
	}
}



template< class DataTypes>
void TriangularQuadraticSpringsForceField<DataTypes>::TRQSTriangleCreationFunction (int triangleIndex, void* param, 
							TriangleRestInformation &tinfo,
							const Triangle& , 
							  const sofa::helper::vector< unsigned int > &,
							  const sofa::helper::vector< double >&)
{
	TriangularQuadraticSpringsForceField<DataTypes> *ff= (TriangularQuadraticSpringsForceField<DataTypes> *)param;
	if (ff) {
		unsigned int j,k,l;

		EdgeData<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation> &edgeInfo=ff->getEdgeInfo();
		typename DataTypes::Real area,squareRestLength[3],restLength[3],cotangent[3];
		typename DataTypes::Real lambda=ff->getLambda();
		typename DataTypes::Real mu=ff->getMu();

		helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation>& edgeInf = *(edgeInfo.beginEdit());

		/// describe the jth edge index of triangle no i 
		const TriangleEdges &te= ff->_topology->getEdgeTriangleShell(triangleIndex);
		// store square rest length
		for(j=0;j<3;++j) {
			restLength[j]=edgeInf[te[j]].restLength;
			squareRestLength[j]= restLength[j]*restLength[j];
		}
		// compute rest area based on Heron's formula
		area=0;
		for(j=0;j<3;++j) {
			area+=squareRestLength[j]*(squareRestLength[(j+1)%3] +squareRestLength[(j+2)%3]-squareRestLength[j]);
		}
		area=sqrt(area)/4;

		for(j=0;j<3;++j) {
			cotangent[j]=(squareRestLength[(j+1)%3] +squareRestLength[(j+2)%3]-squareRestLength[j])/(4*area);
			/*	if (cotangent[j]<0) 
			serr<<"negative cotangent["<<i<<"]["<<j<<"]"<<sendl;
			else
			serr<<"cotangent="<<cotangent[j]<<sendl;*/

		}
		for(j=0;j<3;++j){
			k=(j+1)%3;
			l=(j+2)%3;
			tinfo.gamma[j]=restLength[k]*restLength[l]*(2*cotangent[k]*cotangent[l]*(lambda+mu)-mu)/(8*area);
			tinfo.stiffness[j]=restLength[j]*restLength[j]*(2*cotangent[j]*cotangent[j]*(lambda+mu)+mu)/(8*area);
			edgeInf[te[j]].stiffness+=tinfo.stiffness[j];
		}
		edgeInfo.endEdit();
	}

} 


template< class DataTypes>
void TriangularQuadraticSpringsForceField<DataTypes>::TRQSTriangleDestroyFunction(int triangleIndex, void* param, typename TriangularQuadraticSpringsForceField<DataTypes>::TriangleRestInformation &tinfo)
{
	TriangularQuadraticSpringsForceField<DataTypes> *ff= (TriangularQuadraticSpringsForceField<DataTypes> *)param;
	if (ff) {
		unsigned int j;
		
		EdgeData<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation> &edgeInfo=ff->getEdgeInfo();

		helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation>& edgeInf = *(edgeInfo.beginEdit());

		/// describe the jth edge index of triangle no i 
		const TriangleEdges &te= ff->_topology->getEdgeTriangleShell(triangleIndex);
		// store square rest length
		for(j=0;j<3;++j) {
			edgeInf[te[j]].stiffness -= tinfo.stiffness[j]; 
		}
		
		edgeInfo.endEdit();
	}
} 
template <class DataTypes> TriangularQuadraticSpringsForceField<DataTypes>::TriangularQuadraticSpringsForceField() 
: _initialPoints(initData(&_initialPoints,"initialPoints", "Initial Position"))
, updateMatrix(true)
, f_poissonRatio(initData(&f_poissonRatio,(Real)0.3,"poissonRatio","Poisson ratio in Hooke's law"))
, f_youngModulus(initData(&f_youngModulus,(Real)1000.,"youngModulus","Young modulus in Hooke's law"))
, f_dampingRatio(initData(&f_dampingRatio,(Real)0.,"dampingRatio","Ratio damping/stiffness"))
, f_useAngularSprings(initData(&f_useAngularSprings,true,"useAngularSprings","If Angular Springs should be used or not"))
, lambda(0)
, mu(0)
	{
	}

template <class DataTypes> void TriangularQuadraticSpringsForceField<DataTypes>::handleTopologyChange()
{
	std::list<const TopologyChange *>::const_iterator itBegin=_topology->firstChange();
	std::list<const TopologyChange *>::const_iterator itEnd=_topology->lastChange();

	edgeInfo.handleTopologyEvents(itBegin,itEnd);
	triangleInfo.handleTopologyEvents(itBegin,itEnd);
}

template <class DataTypes> TriangularQuadraticSpringsForceField<DataTypes>::~TriangularQuadraticSpringsForceField()
{

}

template <class DataTypes> void TriangularQuadraticSpringsForceField<DataTypes>::init()
{
	serr << "initializing TriangularQuadraticSpringsForceField" << sendl;
	this->Inherited::init();

	_topology = getContext()->getMeshTopology();

	if (_topology->getNbTriangles()==0)
	{
		serr << "ERROR(TriangularQuadraticSpringsForceField): object must have a Triangular Set Topology."<<sendl;
		return;
	}
	updateLameCoefficients();

	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::TriangleRestInformation>& triangleInf = *(triangleInfo.beginEdit());

	/// prepare to store info in the triangle array
	triangleInf.resize(_topology->getNbTriangles());
	/// prepare to store info in the edge array
	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation>& edgeInf = *(edgeInfo.beginEdit());

	edgeInf.resize(_topology->getNbEdges());

	if (_initialPoints.getValue().size() == 0)
	{
    // get restPosition
	  VecCoord& p = *this->mstate->getX0();
	  _initialPoints.setValue(p);
	}
	int i;
	for (i=0;i<_topology->getNbEdges();++i) {
		TRQSEdgeCreationFunction(i, (void*) this, edgeInf[i],
			_topology->getEdge(i),  (const sofa::helper::vector< unsigned int > )0,
			(const sofa::helper::vector< double >)0);
	}
	for (i=0;i<_topology->getNbTriangles();++i) {
		TRQSTriangleCreationFunction(i, (void*) this, triangleInf[i],
			_topology->getTriangle(i),  (const sofa::helper::vector< unsigned int > )0,
			(const sofa::helper::vector< double >)0);
	}

	edgeInfo.setCreateFunction(TRQSEdgeCreationFunction);
	triangleInfo.setCreateFunction(TRQSTriangleCreationFunction);
	triangleInfo.setDestroyFunction(TRQSTriangleDestroyFunction);
	edgeInfo.setCreateParameter( (void *) this );
	edgeInfo.setDestroyParameter( (void *) this );
	triangleInfo.setCreateParameter( (void *) this );
	triangleInfo.setDestroyParameter( (void *) this );

	edgeInfo.endEdit();
	triangleInfo.endEdit();
}


template <class DataTypes> 
    double TriangularQuadraticSpringsForceField<DataTypes>::getPotentialEnergy(const VecCoord& /*x*/)
{
	serr<<"TriangularQuadraticSpringsForceField::getPotentialEnergy-not-implemented !!!"<<sendl;
    return 0;
}
template <class DataTypes> 
void TriangularQuadraticSpringsForceField<DataTypes>::addForce(VecDeriv& f, const VecCoord& x, const VecDeriv& v)
{
	unsigned int j,k,l,v0,v1;
	int nbEdges=_topology->getNbEdges();
	int nbTriangles=_topology->getNbTriangles();

	Real val,L;
	TriangleRestInformation *tinfo;
	EdgeRestInformation *einfo;

	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::TriangleRestInformation>& triangleInf = *(triangleInfo.beginEdit());

	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation>& edgeInf = *(edgeInfo.beginEdit());

	assert(this->mstate);

	Deriv force;
	Coord dp,dv;
	Real _dampingRatio=f_dampingRatio.getValue();


	for(int i=0; i<nbEdges; i++ )
	{
		einfo=&edgeInf[i];
		v0=_topology->getEdge(i)[0];
		v1=_topology->getEdge(i)[1];
		dp=x[v0]-x[v1];
		dv=v[v0]-v[v1];
		L=einfo->currentLength=dp.norm();
		einfo->dl=einfo->currentLength-einfo->restLength +_dampingRatio*dot(dv,dp)/L;
		/*if (i==0) {
			serr << "dl= " <<  einfo->dl<<sendl;
			serr << "damping= " <<  (_dampingRatio*dot(dv,dp)*einfo->restLength/(L*L))<<sendl;
		}*/
		val=einfo->stiffness*(einfo->dl)/L;
		f[v1]+=dp*val;
		f[v0]-=dp*val;
	//	serr << "einfo->stiffness= "<<einfo->stiffness<<sendl;
	}
	if (f_useAngularSprings.getValue()==true) {
		for(int i=0; i<nbTriangles; i++ )
		{
			tinfo=&triangleInf[i];
			/// describe the jth edge index of triangle no i 
			const TriangleEdges &tea= _topology->getEdgeTriangleShell(i);
			/// describe the jth vertex index of triangle no i 
			const Triangle &ta= _topology->getTriangle(i);

			// store points
			for(j=0;j<3;++j) {
				k=(j+1)%3;
				l=(j+2)%3;
				force=(x[ta[k]] - x[ta[l]])*
					(edgeInf[tea[k]].dl * tinfo->gamma[l] +edgeInf[tea[l]].dl * tinfo->gamma[k])/edgeInf[tea[j]].currentLength;
				f[ta[l]]+=force;
				f[ta[k]]-=force;			
			}
	}
	//	serr << "tinfo->gamma[0] "<<tinfo->gamma[0]<<sendl;

	}
	edgeInfo.endEdit();
	triangleInfo.endEdit();
	updateMatrix=true;
	//serr << "end addForce" << sendl;
}


template <class DataTypes> 
void TriangularQuadraticSpringsForceField<DataTypes>::addDForce(VecDeriv& df, const VecDeriv& dx)
{
	unsigned int i,j,k;	
	int nbTriangles=_topology->getNbTriangles();	

	TriangleRestInformation *tinfo;

	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::TriangleRestInformation>& triangleInf = *(triangleInfo.beginEdit());

//	serr << "start addDForce" << sendl;
	helper::vector<typename TriangularQuadraticSpringsForceField<DataTypes>::EdgeRestInformation>& edgeInf = *(edgeInfo.beginEdit());

	assert(this->mstate);
	VecDeriv& x = *this->mstate->getX();


	Deriv deltax,res;

	if (updateMatrix) {
		int u,v;
		Real val1,val2,vali,valj,valk;
		Coord dpj,dpk,dpi;
	
	//	serr <<"updating matrix"<<sendl;
		updateMatrix=false;
		for(int l=0; l<nbTriangles; l++ )
		{
			tinfo=&triangleInf[l];
			/// describe the jth edge index of triangle no i 
			const TriangleEdges &tea= _topology->getEdgeTriangleShell(l);
			/// describe the jth vertex index of triangle no i 
			const Triangle &ta= _topology->getTriangle(l);

			// store points
			for(k=0;k<3;++k) {
				i=(k+1)%3;
				j=(k+2)%3;
				Mat3 &m=tinfo->DfDx[k];
				dpk = x[ta[i]]- x[ta[j]];

				if (f_useAngularSprings.getValue()==false) {
			    	val1 = -tinfo->stiffness[k]*edgeInf[tea[k]].dl;
					val1/=edgeInf[tea[k]].currentLength;

					val2= -tinfo->stiffness[k]*edgeInf[tea[k]].restLength;
					val2/=edgeInf[tea[k]].currentLength*edgeInf[tea[k]].currentLength*edgeInf[tea[k]].currentLength;

					for (u=0;u<3;++u) {
						for (v=0;v<3;++v) {
							m[u][v]=dpk[u]*dpk[v]*val2;
						}
						m[u][u]+=val1;
					}

				} else {
					dpj = x[ta[i]]- x[ta[k]];
					dpi = x[ta[j]]- x[ta[k]];

					val1 = -(tinfo->stiffness[k]*edgeInf[tea[k]].dl+
						tinfo->gamma[i]*edgeInf[tea[j]].dl+
						tinfo->gamma[j]*edgeInf[tea[i]].dl);

					val2= -val1 - tinfo->stiffness[k]*edgeInf[tea[k]].restLength;
					val1/=edgeInf[tea[k]].currentLength;
					val2/=edgeInf[tea[k]].currentLength*edgeInf[tea[k]].currentLength*edgeInf[tea[k]].currentLength;
					valk=tinfo->gamma[k]/(edgeInf[tea[j]].currentLength*
						edgeInf[tea[i]].currentLength);
					vali=tinfo->gamma[i]/(edgeInf[tea[j]].currentLength*
						edgeInf[tea[k]].currentLength);
					valj=tinfo->gamma[j]/(edgeInf[tea[k]].currentLength*
						edgeInf[tea[i]].currentLength);


					for (u=0;u<3;++u) {
						for (v=0;v<3;++v) {
							m[u][v]=dpk[u]*dpk[v]*val2
								+dpj[u]*dpi[v]*valk
								-dpj[u]*dpk[v]*vali
								+dpk[u]*dpi[v]*valj;

						}
						m[u][u]+=val1;
					}
				}
			}
		}

	} 

	for(int l=0; l<nbTriangles; l++ )
		{
			tinfo=&triangleInf[l];
			/// describe the jth vertex index of triangle no l
			const Triangle &ta= _topology->getTriangle(l);

			// store points
			for(k=0;k<3;++k) {
				i=(k+1)%3;
				j=(k+2)%3;
				deltax= dx[ta[i]] -dx[ta[j]];
				res=tinfo->DfDx[k]*deltax;
				df[ta[i]]+=res;
				df[ta[j]]-= tinfo->DfDx[k].transposeMultiply(deltax);
			}
		}
	edgeInfo.endEdit();
	triangleInfo.endEdit();
}


template<class DataTypes>
void TriangularQuadraticSpringsForceField<DataTypes>::updateLameCoefficients()
{
	lambda= f_youngModulus.getValue()*f_poissonRatio.getValue()/(1-f_poissonRatio.getValue()*f_poissonRatio.getValue());
	mu = f_youngModulus.getValue()*(1-f_poissonRatio.getValue())/(1-f_poissonRatio.getValue()*f_poissonRatio.getValue());
//	serr << "initialized Lame coef : lambda=" <<lambda<< " mu="<<mu<<sendl;
}


template<class DataTypes>
void TriangularQuadraticSpringsForceField<DataTypes>::draw()
{
	if (!getContext()->getShowForceFields()) return;
	if (!this->mstate) return;

	if (getContext()->getShowWireFrame())
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	VecCoord& x = *this->mstate->getX();
	int nbTriangles=_topology->getNbTriangles();

	glDisable(GL_LIGHTING);

	glBegin(GL_TRIANGLES);
	for(int i=0;i<nbTriangles; ++i)
	{
		int a = _topology->getTriangle(i)[0];
		int b = _topology->getTriangle(i)[1];
		int c = _topology->getTriangle(i)[2];

		glColor4f(0,1,0,1);
		helper::gl::glVertexT(x[a]);
		glColor4f(0,0.5,0.5,1);
		helper::gl::glVertexT(x[b]);
		glColor4f(0,0,1,1);
		helper::gl::glVertexT(x[c]);
	}
	glEnd();


	if (getContext()->getShowWireFrame())
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

} // namespace forcefield

} // namespace Components

} // namespace Sofa