File: TriangularBendingSprings.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 (724 lines) | stat: -rw-r--r-- 21,397 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/******************************************************************************
*       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                             *
******************************************************************************/
//
// C++ Implementation: TriangularBendingSprings
//
// Description:
//
//
// Author: The SOFA team </www.sofa-framework.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef SOFA_COMPONENT_FORCEFIELD_TRIANGULARBENDINGSPRINGS_INL
#define SOFA_COMPONENT_FORCEFIELD_TRIANGULARBENDINGSPRINGS_INL

#include <sofa/component/forcefield/TriangularBendingSprings.h>
#include <sofa/component/topology/PointSetTopologyChange.h>
#include <fstream> // for reading the file
#include <iostream> //for debugging

#include <sofa/helper/gl/template.h>
#include <sofa/component/topology/TriangleData.inl>
#include <sofa/component/topology/EdgeData.inl>

namespace sofa
{

namespace component
{

namespace forcefield
{

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





using namespace core::componentmodel::behavior;
using core::componentmodel::topology::BaseMeshTopology;
typedef BaseMeshTopology::TriangleEdges TriangleEdges;

template< class DataTypes>
void TriangularBendingSprings<DataTypes>::TriangularBSEdgeCreationFunction(int /*edgeIndex*/, void* param, EdgeInformation &ei,
																				 const Edge& ,  const sofa::helper::vector< unsigned int > &,
																				 const sofa::helper::vector< double >&)
{
	TriangularBendingSprings<DataTypes> *ff= (TriangularBendingSprings<DataTypes> *)param;
	if (ff) {
		
		unsigned int u,v;
		/// set to zero the edge stiffness matrix
		for (u=0;u<3;++u) {
			for (v=0;v<3;++v) {
				ei.DfDx[u][v]=0;
			}
		}

		ei.is_activated=false;
		ei.is_initialized=false;

	}
}

template< class DataTypes>
void TriangularBendingSprings<DataTypes>::TriangularBSTriangleCreationFunction (const sofa::helper::vector<unsigned int> &triangleAdded, 
																					  void* param, vector<EdgeInformation> &edgeData)
{

	TriangularBendingSprings<DataTypes> *ff= (TriangularBendingSprings<DataTypes> *)param;
	if (ff) {		

		double m_ks=ff->getKs();
		double m_kd=ff->getKd();

		unsigned int u,v;

		unsigned int nb_activated = 0;
		
		const typename DataTypes::VecCoord *restPosition=ff->mstate->getX0();

		for (unsigned int i=0;i<triangleAdded.size();++i) {

			/// describe the jth edge index of triangle no i 
			TriangleEdges te2 = ff->_topology->getEdgeTriangleShell(triangleAdded[i]);
			/// describe the jth vertex index of triangle no i 
			Triangle t2 = ff->_topology->getTriangle(triangleAdded[i]);

			for(unsigned int j=0;j<3;++j) {

				EdgeInformation &ei = edgeData[te2[j]]; // ff->edgeInfo
				if(!(ei.is_initialized)){

					unsigned int edgeIndex = te2[j];
					ei.is_activated=true;

					/// set to zero the edge stiffness matrix
					for (u=0;u<3;++u) {
						for (v=0;v<3;++v) {
							ei.DfDx[u][v]=0;
						}
					}

					const sofa::helper::vector< unsigned int > shell = ff->_topology->getTriangleEdgeShell(edgeIndex);
					if (shell.size()==2) {

						nb_activated+=1;

						TriangleEdges te1;
						Triangle t1;

						if(shell[0] == triangleAdded[i]){
							
							te1 = ff->_topology->getEdgeTriangleShell(shell[1]);
							t1 = ff->_topology->getTriangle(shell[1]);

						}else{ // shell[1] == triangleAdded[i]
							
							te1 = ff->_topology->getEdgeTriangleShell(shell[0]);
							t1 = ff->_topology->getTriangle(shell[0]);
						}

						int i1 = ff->_topology->getEdgeIndexInTriangle(te1, edgeIndex); //edgeIndex //te1[j]
						int i2 = ff->_topology->getEdgeIndexInTriangle(te2, edgeIndex); // edgeIndex //te2[j]

						ei.m1 = t1[i1];
						ei.m2 = t2[i2];

						//TriangularBendingSprings<DataTypes> *fftest= (TriangularBendingSprings<DataTypes> *)param;
						ei.ks=m_ks; //(fftest->ks).getValue();
						ei.kd=m_kd; //(fftest->kd).getValue();

						Coord u = (*restPosition)[ei.m1] - (*restPosition)[ei.m2];

						Real d = u.norm();

						ei.restlength=(double) d;

						ei.is_activated=true;

					}else{

						ei.is_activated=false;

					}

					ei.is_initialized = true;
				}
			}

		}

	}
	
}

template< class DataTypes>
void TriangularBendingSprings<DataTypes>::TriangularBSTriangleDestructionFunction (const sofa::helper::vector<unsigned int> &triangleRemoved, 
																					  void* param, vector<EdgeInformation> &edgeData)
{
	TriangularBendingSprings<DataTypes> *ff= (TriangularBendingSprings<DataTypes> *)param;
	if (ff) {		

		double m_ks=ff->getKs(); // typename DataTypes::
		double m_kd=ff->getKd(); // typename DataTypes::

		//unsigned int u,v;
		
		const typename DataTypes::VecCoord *restPosition=ff->mstate->getX0();

		for (unsigned int i=0;i<triangleRemoved.size();++i) {
			/// describe the jth edge index of triangle no i 
			TriangleEdges te = ff->_topology->getEdgeTriangleShell(triangleRemoved[i]);
			/// describe the jth vertex index of triangle no i 
			Triangle t = ff->_topology->getTriangle(triangleRemoved[i]);


			for(unsigned int j=0;j<3;++j) {

				EdgeInformation &ei = edgeData[te[j]]; // ff->edgeInfo
				if(ei.is_initialized){

					unsigned int edgeIndex = te[j];

					const sofa::helper::vector< unsigned int > shell = ff->_topology->getTriangleEdgeShell(edgeIndex);
					if (shell.size()==3) {

						TriangleEdges te1;
						Triangle t1;
						TriangleEdges te2;
						Triangle t2;

						if(shell[0] == triangleRemoved[i]){
							te1 = ff->_topology->getEdgeTriangleShell(shell[1]);
							t1 = ff->_topology->getTriangle(shell[1]);
							te2 = ff->_topology->getEdgeTriangleShell(shell[2]);
							t2 = ff->_topology->getTriangle(shell[2]);

						}else{

							if(shell[1] == triangleRemoved[i]){

								te1 = ff->_topology->getEdgeTriangleShell(shell[2]);
								t1 = ff->_topology->getTriangle(shell[2]);
								te2 = ff->_topology->getEdgeTriangleShell(shell[0]);
								t2 = ff->_topology->getTriangle(shell[0]);

							}else{ // shell[2] == triangleRemoved[i]

								te1 = ff->_topology->getEdgeTriangleShell(shell[0]);
								t1 = ff->_topology->getTriangle(shell[0]);
								te2 = ff->_topology->getEdgeTriangleShell(shell[1]);
								t2 = ff->_topology->getTriangle(shell[1]);

							}
						}

						int i1 = ff->_topology->getEdgeIndexInTriangle(te1, edgeIndex);
						int i2 = ff->_topology->getEdgeIndexInTriangle(te2, edgeIndex);

						ei.m1 = t1[i1];
						ei.m2 = t2[i2];

						//TriangularBendingSprings<DataTypes> *fftest= (TriangularBendingSprings<DataTypes> *)param;
						ei.ks=m_ks; //(fftest->ks).getValue();
						ei.kd=m_kd; //(fftest->kd).getValue();

						Coord u = (*restPosition)[ei.m1] - (*restPosition)[ei.m2];
						Real d = u.norm();

						ei.restlength=(double) d;

						ei.is_activated=true;

					}else{

						ei.is_activated=false;
						ei.is_initialized = false;

					}

				}else{

					ei.is_activated=false;
					ei.is_initialized = false;

				}
			}

		}

	}
}


template<class DataTypes>
TriangularBendingSprings<DataTypes>::TriangularBendingSprings(/*component::MechanicalObject<DataTypes>* m_dof */ /*double _ks, double _kd*/)
: updateMatrix(true)
, f_ks(initData(&f_ks,(double) 100000.0,"stiffness","uniform stiffness for the all springs")) //(Real)0.3 ??
, f_kd(initData(&f_kd,(double) 1.0,"damping","uniform damping for the all springs")) // (Real)1000. ??
  {
	//serr<<"TriangularBendingSprings<DataTypes>::TriangularBendingSprings"<<sendl;
  }

template<class DataTypes>
TriangularBendingSprings<DataTypes>::~TriangularBendingSprings()
{}


template <class DataTypes> void TriangularBendingSprings<DataTypes>::handleTopologyChange()
{
	bool debug_mode = false;

	std::list<const TopologyChange *>::const_iterator itBegin=_topology->firstChange();
	std::list<const TopologyChange *>::const_iterator itEnd=_topology->lastChange();

	helper::vector<EdgeInformation> edgeInf = *(edgeInfo.beginEdit());

	edgeInfo.handleTopologyEvents(itBegin,itEnd);

	while( itBegin != itEnd )
	{
		core::componentmodel::topology::TopologyChangeType changeType = (*itBegin)->getChangeType();

		if(changeType == core::componentmodel::topology::POINTSREMOVED){

			unsigned int last = _topology->getNbPoints() -1;
			unsigned int i,j;

			const sofa::helper::vector<unsigned int> tab = ( static_cast< const sofa::component::topology::PointsRemoved * >( *itBegin ) )->getArray();

			sofa::helper::vector<unsigned int> lastIndexVec;
			for(unsigned int i_init = 0; i_init < tab.size(); ++i_init){

				lastIndexVec.push_back(last - i_init);
			}

			for ( i = 0; i < tab.size(); ++i)
			{

				unsigned int i_next = i;
				bool is_reached = false;
				while( (!is_reached) && (i_next < lastIndexVec.size() - 1)){

					i_next += 1 ;
					is_reached = is_reached || (lastIndexVec[i_next] == tab[i]);
				}

				if(is_reached){

					lastIndexVec[i_next] = lastIndexVec[i];

				}
				
				const sofa::helper::vector<unsigned int> &shell= _topology->getTriangleVertexShell(lastIndexVec[i]);
				for (j=0;j<shell.size();++j) {

					Triangle tj = _topology->getTriangle(shell[j]);
					
					int vertexIndex = _topology->getVertexIndexInTriangle(tj, lastIndexVec[i]);

					TriangleEdges tej = _topology->getEdgeTriangleShell(shell[j]);
					
					unsigned int ind_j = tej[vertexIndex];

					if (edgeInf[ind_j].m1 == (int) last){
							edgeInf[ind_j].m1=(int) tab[i];
							//sout << "INFO_print : OK m1 for ind_j =" << ind_j << sendl; 
					}else{
						if (edgeInf[ind_j].m2 == (int) last){
							edgeInf[ind_j].m2=(int) tab[i];
							//sout << "INFO_print : OK m2 for ind_j =" << ind_j << sendl; 
						}
					}
				}
				
				if(debug_mode){

					for (unsigned int j_loc=0;j_loc<edgeInf.size();++j_loc) {

						bool is_forgotten = false;
						if (edgeInf[j_loc].m1 == (int) last){
							edgeInf[j_loc].m1 =(int) tab[i];
							is_forgotten=true;
							//sout << "INFO_print : TriangularBendingSprings - MISS m1 for j_loc =" << j_loc << sendl; 

						}else{
							if (edgeInf[j_loc].m2 ==(int) last){
								edgeInf[j_loc].m2 =(int) tab[i];
								is_forgotten=true;
								//sout << "INFO_print : TriangularBendingSprings - MISS m2 for j_loc =" << j_loc << sendl; 

							}

						}					

					}
				}

				--last;
			}
			
		}else{

			if(changeType == core::componentmodel::topology::POINTSRENUMBERING){																	

				const sofa::helper::vector<unsigned int> tab = ( static_cast< const sofa::component::topology::PointsRenumbering * >( *itBegin ) )->getinv_IndexArray();

				for (int i = 0; i < _topology->getNbEdges(); ++i)
				{									
					if(edgeInf[i].is_activated){					
						edgeInf[i].m1  = tab[edgeInf[i].m1];
						edgeInf[i].m2  = tab[edgeInf[i].m2];	
					}
				}	
			}
		}

		++itBegin;
	}
	edgeInfo.endEdit();
}


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

	_topology = getContext()->getMeshTopology();

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

	/// prepare to store info in the edge array
	helper::vector<EdgeInformation>& edgeInf = *(edgeInfo.beginEdit());
	edgeInf.resize(_topology->getNbEdges());

	int i;
	// set edge tensor to 0
	for (i=0;i<_topology->getNbEdges();++i) {

		TriangularBSEdgeCreationFunction(i, (void*) this, edgeInf[i],
			_topology->getEdge(i),  (const sofa::helper::vector< unsigned int > )0,
			(const sofa::helper::vector< double >)0);
	}

	// create edge tensor by calling the triangle creation function
	sofa::helper::vector<unsigned int> triangleAdded;
	for (i=0;i<_topology->getNbTriangles();++i){
		triangleAdded.push_back(i);
	}
	TriangularBSTriangleCreationFunction(triangleAdded,(void*) this,
		edgeInf);

	edgeInfo.setCreateFunction(TriangularBSEdgeCreationFunction);
	edgeInfo.setCreateTriangleFunction(TriangularBSTriangleCreationFunction);
	edgeInfo.setDestroyTriangleFunction(TriangularBSTriangleDestructionFunction);
	edgeInfo.setCreateParameter( (void *) this );
	edgeInfo.setDestroyParameter( (void *) this );

	edgeInfo.endEdit();
}

template <class DataTypes> 
    double TriangularBendingSprings<DataTypes>::getPotentialEnergy(const VecCoord& /*x*/)
{
	serr<<"TriangularBendingSprings::getPotentialEnergy-not-implemented !!!"<<sendl;
    return 0;
}


template<class DataTypes>
void TriangularBendingSprings<DataTypes>::addForce(VecDeriv& f, const VecCoord& x, const VecDeriv& v)
{
	int nbEdges=_topology->getNbEdges();

	EdgeInformation *einfo;

	helper::vector<EdgeInformation>& edgeInf = *(edgeInfo.beginEdit());

    //const helper::vector<Spring>& m_springs= this->springs.getValue();
    //this->dfdx.resize(nbEdges); //m_springs.size()
    f.resize(x.size());
    m_potentialEnergy = 0;
    /*        serr<<"TriangularBendingSprings<DataTypes>::addForce()"<<sendl;*/

#if 0		
		const VecCoord& x_rest = *this->mstate->getX0();
#endif

	for(int i=0; i<nbEdges; i++ )
	{
		einfo=&edgeInf[i];

		// safety check
#if 0
		{
		    EdgeInformation e2;
		    const sofa::helper::vector< unsigned int > shell = _topology->getTriangleEdgeShell(i);
		    if (shell.size() != 2)
			e2.is_activated = false;
		    else
		    {
			e2.is_activated = true;
			e2.m1 = -1;
			e2.m2 = -1;
			for (int j=0;j<3;j++)
			    if (_topology->getTriangle(shell[0]][j] != getEdge(i)[0] && _topology->getTriangle(shell[0])[j] != getEdge(i)[1])
				e2.m1 = _topology->getTriangle(shell[0])[j];
			for (int j=0;j<3;j++)
			    if (_topology->getTriangle(shell[1])[j] != getEdge(i)[0] && _topology->getTriangle(shell[1])[j] != getEdge(i)[1])
				e2.m2 = _topology->getTriangle(shell[1])[j];
			if (e2.m1 >= 0 && e2.m2 >= 0)
			{
			    e2.restlength = (x_rest[e2.m2]-x_rest[e2.m1]).norm();
			}
		    }

		    if (e2.is_activated != einfo->is_activated) serr << "ERROR: EdgeInfo["<<i<<"].is_activated = "<<einfo->is_activated<<" while it should be "<<e2.is_activated<<""<<sendl;
		    else if (e2.is_activated)
		    {
			if (!((e2.m1 == einfo->m1 && e2.m2 == einfo->m2) || (e2.m1 == einfo->m2 && e2.m2 == einfo->m1)))
			    serr << "ERROR: EdgeInfo["<<i<<"] points = "<<einfo->m1<<"-"<<einfo->m2<<" while it should be "<<e2.m1<<"-"<<e2.m2<<""<<sendl;
			if (e2.restlength != einfo->restlength)
			    serr << "ERROR: EdgeInfo["<<i<<"] length = "<<einfo->restlength<<" while it should be "<<e2.restlength<<""<<sendl;			
		    }
		}
			
#endif

		/*            serr<<"TriangularBendingSprings<DataTypes>::addForce() between "<<springs[i].m1<<" and "<<springs[i].m2<<sendl;*/

		if(einfo->is_activated){
			//this->addSpringForce(m_potentialEnergy,f,x,v, i, einfo->spring);

			int a = einfo->m1;
			int b = einfo->m2;
			Coord u = x[b]-x[a];
			Real d = u.norm();
			if( d>1.0e-4 )
			{
				Real inverseLength = 1.0f/d;
				u *= inverseLength;
				Real elongation = (Real)(d - einfo->restlength);
				m_potentialEnergy += elongation * elongation * einfo->ks / 2;
		/*      serr<<"TriangularBendingSprings<DataTypes>::addSpringForce, p = "<<p<<sendl;

				serr<<"TriangularBendingSprings<DataTypes>::addSpringForce, new potential energy = "<<potentialEnergy<<sendl;*/
				Deriv relativeVelocity = v[b]-v[a];
				Real elongationVelocity = dot(u,relativeVelocity);
				Real forceIntensity = (Real)(einfo->ks*elongation+einfo->kd*elongationVelocity);
				Deriv force = u*forceIntensity;
				f[a]+=force;
				f[b]-=force;

				updateMatrix=true;

				Mat3& m = einfo->DfDx; //Mat& m = this->dfdx[i];
				Real tgt = forceIntensity * inverseLength;
				for( int j=0; j<3; ++j )
				{
					for( int k=0; k<3; ++k )
					{
						m[j][k] = ((Real)einfo->ks-tgt) * u[j] * u[k];
					}
					m[j][j] += tgt;
				}
			}
			else // null length, no force and no stiffness
			{
				Mat3& m = einfo->DfDx; //Mat& m = this->dfdx[i];
				for( int j=0; j<3; ++j )
				{
					for( int k=0; k<3; ++k )
					{
						m[j][k] = 0;
					}
				}
			}
		}
	}

	edgeInfo.endEdit();
    //for (unsigned int i=0; i<springs.size(); i++)
    //{
        /*            serr<<"TriangularBendingSprings<DataTypes>::addForce() between "<<springs[i].m1<<" and "<<springs[i].m2<<sendl;*/
    //    this->addSpringForce(m_potentialEnergy,f,x,v, i, springs[i]);
    //}
}

template<class DataTypes>
void TriangularBendingSprings<DataTypes>::addDForce(VecDeriv& df, const VecDeriv& dx)
{
	int nbEdges=_topology->getNbEdges();

	EdgeInformation *einfo;

	helper::vector<EdgeInformation>& edgeInf = *(edgeInfo.beginEdit());

    df.resize(dx.size());
    //serr<<"TriangularBendingSprings<DataTypes>::addDForce, dx1 = "<<dx1<<sendl;
    //serr<<"TriangularBendingSprings<DataTypes>::addDForce, df1 before = "<<f1<<sendl;
    //const helper::vector<Spring>& springs = this->springs.getValue();

	for(int i=0; i<nbEdges; i++ )
	{
		einfo=&edgeInf[i];

		/*            serr<<"TriangularBendingSprings<DataTypes>::addForce() between "<<springs[i].m1<<" and "<<springs[i].m2<<sendl;*/

		if(einfo->is_activated){
			//this->addSpringDForce(df,dx, i, einfo->spring);

			const int a = einfo->m1;
			const int b = einfo->m2;
			const Coord d = dx[b]-dx[a];
			const Deriv dforce = einfo->DfDx*d; //this->dfdx[i]*d;
			df[a]+=dforce;
			df[b]-=dforce;
			//serr<<"TriangularBendingSprings<DataTypes>::addSpringDForce, a="<<a<<", b="<<b<<", dforce ="<<dforce<<sendl;
		
			if(updateMatrix){

			}
			updateMatrix=false;
		}
	}

	edgeInfo.endEdit();
    //for (unsigned int i=0; i<springs.size(); i++)
    //{
    //    this->addSpringDForce(df,dx, i, springs[i]);
    //}
    //serr<<"TriangularBendingSprings<DataTypes>::addDForce, df = "<<f<<sendl;
}


/*
template<class DataTypes>
void TriangularBendingSprings<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 TriangularBendingSprings<DataTypes>::draw()
{
	unsigned int i;
	if (!getContext()->getShowForceFields()) return;
	if (!this->mstate) return;

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

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

	glDisable(GL_LIGHTING);

	
	/*
	glBegin(GL_TRIANGLES);
	for(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]);
	}
	*/
	unsigned int nb_to_draw = 0;

	helper::vector<EdgeInformation>& edgeInf = *(edgeInfo.beginEdit());

	glBegin(GL_LINES);
	for(i=0;i<edgeInf.size(); ++i)
	{
		if(edgeInf[i].is_activated){
		
			
			bool external=true;
			Real d = (x[edgeInf[i].m2]-x[edgeInf[i].m1]).norm();
			if (external)
			{
				if (d<edgeInf[i].restlength*0.9999)
					glColor4f(1,0,0,1);
				else
					glColor4f(0,1,0,1);
			}
			else
			{
				if (d<edgeInf[i].restlength*0.9999)
					glColor4f(1,0.5f,0,1);
				else
					glColor4f(0,1,0.5f,1);
			}
			

			nb_to_draw+=1;

			//glColor4f(0,1,0,1);
			helper::gl::glVertexT(x[edgeInf[i].m1]);
			helper::gl::glVertexT(x[edgeInf[i].m2]);

		}
	}
	glEnd();

	edgeInfo.endEdit();

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


} // namespace forcefield

} // namespace component

} // namespace sofa

#endif