File: PFacet.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (824 lines) | stat: -rw-r--r-- 34,602 bytes parent folder | download | duplicates (3)
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*****************************************************************************
*  Copyright (C) 2015 by Anna Effeindzourou   anna.effeindzourou@gmail.com   *
*  Copyright (C) 2015 by Bruno Chareyre       bruno.chareyre@grenoble-inp.fr     *
*  Copyright (C) 2015 by Klaus Thoeni         klaus.thoeni@gmail.com         *
*  This program is free software; it is licensed under the terms of the      *
*  GNU General Public License v2 or later. See file LICENSE for details.     *
******************************************************************************/
#include "PFacet.hpp"
#ifdef YADE_OPENGL
#include <lib/opengl/OpenGLWrapper.hpp>
#endif

namespace yade { // Cannot have #include directive inside.

using math::max;
using math::min; // using inside .cpp file is ok.


//!##################	IGeom Functors   #####################
// Function used in order to calculate the projection of the sphere on the PFacet element. The function returns if the the porjection is on the inside the triangle and the barycentric coordinates of the projection P on PFacet the element.
boost::tuple<Vector3r, bool, double, double, double> Ig2_Sphere_PFacet_ScGridCoGeom::projection(const shared_ptr<Shape>& cm2, const State& state1)
{
	const State* sphereSt = YADE_CAST<const State*>(&state1);
	PFacet*      Pfacet   = YADE_CAST<PFacet*>(cm2.get());

	vector<Vector3r> vertices;
	vertices.push_back(Pfacet->node1->state->pos);
	vertices.push_back(Pfacet->node2->state->pos);
	vertices.push_back(Pfacet->node3->state->pos);
	Vector3r center = vertices[0]
	        + ((vertices[2] - vertices[0]) * (vertices[1] - vertices[0]).norm() + (vertices[1] - vertices[0]) * (vertices[2] - vertices[0]).norm())
	                / ((vertices[1] - vertices[0]).norm() + (vertices[2] - vertices[1]).norm() + (vertices[0] - vertices[2]).norm());


	Vector3r e[3]   = { vertices[1] - vertices[0], vertices[2] - vertices[1], vertices[0] - vertices[2] };
	Vector3r normal = e[0].cross(e[1]) / ((e[0].cross(e[1])).norm());

	// 	Vector3r centerS=sphereSt->pos+shift2;//FIXME: periodicity?
	const Vector3r& centerS = sphereSt->pos;
	Vector3r        cl      = centerS - center;

	Real dist = normal.dot(cl);

	if (dist < 0) {
		normal = -normal;
		dist   = -dist;
	}

	Vector3r P = center + (cl - dist * normal);

	Vector3r v0 = vertices[1] - vertices[0];
	Vector3r v1 = vertices[2] - vertices[0];
	Vector3r v2 = P - vertices[0];

	// Compute dot products
	Real dot00 = v0.dot(v0);
	Real dot01 = v0.dot(v1);
	Real dot02 = v0.dot(v2);
	Real dot11 = v1.dot(v1);
	Real dot12 = v1.dot(v2);

	// Compute the barycentric coordinates of the projection P
	Real invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
	Real p2       = (dot11 * dot02 - dot01 * dot12) * invDenom;
	Real p3       = (dot00 * dot12 - dot01 * dot02) * invDenom;
	Real p1       = 1 - p2 - p3;

	// Check if P is in triangle
	bool isintriangle = (p1 > 0) && (p2 > 0) && (p1 + p2 < 1);
	return boost::make_tuple(P, isintriangle, p1, p2, p3);
}


bool Ig2_Sphere_PFacet_ScGridCoGeom::go(
        const shared_ptr<Shape>& cm1,
        const shared_ptr<Shape>& cm2,
        const State&             state1,
        const State& /*state2*/,
        const Vector3r&                shift2,
        const bool&                    force,
        const shared_ptr<Interaction>& c)
{
	TIMING_DELTAS_START();

	Sphere* sphere = YADE_CAST<Sphere*>(cm1.get());
	PFacet* Pfacet = YADE_CAST<PFacet*>(cm2.get());

	const State* sphereSt = YADE_CAST<const State*>(&state1);

	Real sphereRadius = sphere->radius;
	Real PFacetradius = Pfacet->radius;

	vector<Vector3r> vertices;
	vertices.push_back(Pfacet->node1->state->pos);
	vertices.push_back(Pfacet->node2->state->pos);
	vertices.push_back(Pfacet->node3->state->pos);
	Vector3r center = vertices[0]
	        + ((vertices[2] - vertices[0]) * (vertices[1] - vertices[0]).norm() + (vertices[1] - vertices[0]) * (vertices[2] - vertices[0]).norm())
	                / ((vertices[1] - vertices[0]).norm() + (vertices[2] - vertices[1]).norm() + (vertices[0] - vertices[2]).norm());


	Vector3r e[3]   = { vertices[1] - vertices[0], vertices[2] - vertices[1], vertices[0] - vertices[2] };
	Vector3r normal = e[0].cross(e[1]) / ((e[0].cross(e[1])).norm());

	// 	Vector3r centerS=sphereSt->pos+shift2;//FIXME: periodicity?
	const Vector3r& centerS = sphereSt->pos;
	Vector3r        cl      = centerS - center;
	Real            dist    = normal.dot(cl);

	shared_ptr<ScGridCoGeom> scm;
	bool                     isNew = !(c->geom);

	if (c->geom) scm = YADE_PTR_CAST<ScGridCoGeom>(c->geom);
	else
		scm = shared_ptr<ScGridCoGeom>(new ScGridCoGeom());

	if (scm->isDuplicate == 2 && scm->trueInt != c->id2) return true; //the contact will be deleted into the Law, no need to compute here.
	scm->isDuplicate = 0;
	scm->trueInt     = -1;


	if (math::abs(dist) > (PFacetradius + sphereRadius) && !c->isReal()
	    && !force) { // no contact, but only if there was no previous contact; ortherwise, the constitutive law is responsible for setting Interaction::isReal=false
		TIMING_DELTAS_CHECKPOINT("Ig2_Sphere_PFacet_ScGridCoGeom");
		return false;
	}
	if (dist < 0) {
		normal = -normal;
		dist   = -dist;
	}


	boost::tuple<Vector3r, bool, double, double, double> projectionres = projection(cm2, state1);
	Vector3r                                             P             = boost::get<0>(projectionres);
	bool                                                 isintriangle  = boost::get<1>(projectionres);
	Real                                                 p1            = boost::get<2>(projectionres);
	Real                                                 p2            = boost::get<3>(projectionres);
	Real                                                 p3            = boost::get<4>(projectionres);


	shared_ptr<Body> GridList[3] = { Pfacet->conn1, Pfacet->conn2, Pfacet->conn3 };

	// Check if the projection of the contact point is inside the triangle
	bool isconn1 = ((p1 > 0) && (p2 <= 0) && (p1 + p2 < 1)) || ((p1 > 0) && (p2 <= 0) && (p1 + p2 >= 1));
	bool isconn2 = ((p1 > 0) && (p2 > 0) && (p1 + p2 >= 1)) || ((p1 <= 0) && (p2 > 0) && (p1 + p2 >= 1));
	bool isconn3 = ((p1 <= 0) && (p2 > 0) && (p1 + p2 < 1)) || ((p1 <= 0) && (p2 <= 0) && (p1 + p2 < 1));

	Real penetrationDepth = 0;
	int  connnum          = -1;

	GridNode* GridNodeList[3] = { YADE_CAST<GridNode*>(Pfacet->node1->shape.get()),
		                      YADE_CAST<GridNode*>(Pfacet->node2->shape.get()),
		                      YADE_CAST<GridNode*>(Pfacet->node3->shape.get()) };
	// 	If the contact projection is in the triangle, a search for an old contact is performed to export the information regarding the contact (phys)
	if (isintriangle) {
		if (isNew) {
			for (int unsigned i = 0; i < 3; i++) {
				for (int unsigned j = 0; j < GridNodeList[i]->pfacetList.size(); j++) {
					if (GridNodeList[i]->pfacetList[j]->getId() != c->id2) {
						boost::tuple<Vector3r, bool, double, double, double> projectionPrev
						        = projection(GridNodeList[i]->pfacetList[j]->shape, state1);
						bool isintrianglePrev = boost::get<1>(projectionPrev);
						if (!isintrianglePrev) { //if(!isintrianglePrev)
							const shared_ptr<Interaction> intr
							        = scene->interactions->find(c->id1, GridNodeList[i]->pfacetList[j]->getId());
							if (intr
							    && intr->isReal()) { // if an interaction exist between the sphere and the previous pfacet, import parameters.
								// 								cout<<"Copying contact with pfacet geom and phys from "<<intr->id1<<"-"<<intr->id2<<" to here ("<<c->id1<<"-"<<c->id2<<")"<<endl;
								scm              = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
								c->geom          = scm;
								c->phys          = intr->phys;
								c->iterMadeReal  = intr->iterMadeReal; // iteration from the time when the contact became real
								scm->trueInt     = c->id2;
								scm->isDuplicate = 2; //command the old contact deletion.
								isNew            = 0;
								break;
							}
						}
					}
				}
			}
		}
	} else {
		// 		identification of the cylinder possibly in contact with the sphere
		if (isconn1) connnum = 0;
		if (isconn2) connnum = 1;
		if (isconn3) connnum = 2;
		// 		check if the identified cylinder  previously is in contact with the sphere
		if (connnum != -1) {
			//verify if there is a contact between the a neighbouring PFacet, avoid double contacts
			for (int unsigned i = 0; i < 3; i++) {
				for (int unsigned j = 0; j < GridNodeList[i]->pfacetList.size(); j++) {
					if (GridNodeList[i]->pfacetList[j]->getId() != c->id2) {
						boost::tuple<Vector3r, bool, double, double, double> projectionPrev
						        = projection(GridNodeList[i]->pfacetList[j]->shape, state1);
						bool isintrianglePrev = boost::get<1>(projectionPrev);
						if (isintrianglePrev) {
							const shared_ptr<Interaction> intr
							        = scene->interactions->find(c->id1, GridNodeList[i]->pfacetList[j]->getId());
							if (intr
							    && intr->isReal()) { // if an interaction exist between the sphere and the previous pfacet, import parameters.
								if (isNew) {
									return false;
								} else {
									scm->isDuplicate = 1;
									scm->trueInt     = -1;
									return true;
								}
							}
						}
					}
				}
			}
			//SPhere-cylinder contact
			const State*    sphereSt2 = YADE_CAST<const State*>(&state1);
			GridConnection* gridCo    = YADE_CAST<GridConnection*>(GridList[connnum]->shape.get());
			GridNode*       gridNo1   = YADE_CAST<GridNode*>(gridCo->node1->shape.get());
			GridNode*       gridNo2   = YADE_CAST<GridNode*>(gridCo->node2->shape.get());
			State*          gridNo1St = YADE_CAST<State*>(gridCo->node1->state.get());
			State*          gridNo2St = YADE_CAST<State*>(gridCo->node2->state.get());

			Vector3r segt = gridCo->getSegment();
			Real     len  = gridCo->getLength();

			Vector3r spherePos = sphereSt2->pos;
			Vector3r branch    = spherePos - gridNo1St->pos;
			Vector3r branchN   = spherePos - gridNo2St->pos;
			for (int i = 0; i < 3; i++) {
				if (math::abs(branch[i]) < 1e-14) branch[i] = 0.0;
				if (math::abs(branchN[i]) < 1e-14) branchN[i] = 0.0;
			}
			Real relPos = branch.dot(segt) / (len * len);
			bool SGr    = true;
			if (relPos <= 0) {                          // if the sphere projection is BEFORE the segment ...
				if (gridNo1->ConnList.size() > 1) { //	if the node is not an extremity of the Grid (only one connection)
					for (int unsigned i = 0; i < gridNo1->ConnList.size(); i++) { // ... loop on all the Connections of the same Node ...
						GridConnection* GC = (GridConnection*)gridNo1->ConnList[i]->shape.get();
						if (GC == gridCo) continue; //	self comparison.
						Vector3r segtCandidate1 = GC->node1->state->pos
						        - gridNo1St->pos; // (be sure of the direction of segtPrev to compare relPosPrev.)
						Vector3r segtCandidate2 = GC->node2->state->pos - gridNo1St->pos;
						Vector3r segtPrev       = segtCandidate1.norm() > segtCandidate2.norm() ? segtCandidate1 : segtCandidate2;
						for (int j = 0; j < 3; j++) {
							if (math::abs(segtPrev[j]) < 1e-14) segtPrev[j] = 0.0;
						}
						Real relPosPrev = (branch.dot(segtPrev)) / (segtPrev.norm() * segtPrev.norm());
						// ... and check whether the sphere projection is before the neighbours connections too.
						if (relPosPrev
						    <= 0) { //if the sphere projection is outside both the current Connection AND this neighbouring connection, then create the interaction if the neighbour did not already do it before.
							for (int unsigned j = 0; j < GC->pfacetList.size(); j++) {
								if (GC->pfacetList[j]->getId() != c->id2) {
									const shared_ptr<Interaction> intr
									        = scene->interactions->find(c->id1, GC->pfacetList[j]->getId());

									if (intr && intr->isReal()) {
										shared_ptr<ScGridCoGeom> intrGeom = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
										if (!(intrGeom->isDuplicate == 1)) { //skip contact.
											if (isNew) {
												SGr = false;
											} else {
												scm->isDuplicate = 1;
											} /*cout<<"Declare "<<c->id1<<"-"<<c->id2<<" as duplicated."<<endl;*/
										}
									}
								}
							}
						} else { //the sphere projection is outside the current Connection but inside the previous neighbour. The contact has to be handled by the Prev GridConnection, not here.
							if (isNew) {
								SGr = false;
							} else {
								//cout<<"The contact "<<c->id1<<"-"<<c->id2<<" HAVE to be copied and deleted NOW."<<endl ;
								scm->isDuplicate = 1;
								scm->trueInt     = -1; //trueInt id de l'objet avec lequel il y a un contact -1 = rien faire
								return true;
							}
						}
					}
				}
			}

			//Exactly the same but in the case the sphere projection is AFTER the segment.
			else if (relPos >= 1) {
				if (gridNo2->ConnList.size() > 1) {
					for (int unsigned i = 0; i < gridNo2->ConnList.size(); i++) {
						GridConnection* GC = (GridConnection*)gridNo2->ConnList[i]->shape.get();
						if (GC == gridCo) continue; //	self comparison.
						Vector3r segtCandidate1 = GC->node1->state->pos - gridNo2St->pos;
						Vector3r segtCandidate2 = GC->node2->state->pos - gridNo2St->pos;
						Vector3r segtNext       = segtCandidate1.norm() > segtCandidate2.norm() ? segtCandidate1 : segtCandidate2;
						for (int j = 0; j < 3; j++) {
							if (math::abs(segtNext[j]) < 1e-14) segtNext[j] = 0.0;
						}

						Real relPosNext = (branchN.dot(segtNext)) / (segtNext.norm() * segtNext.norm());
						if (relPosNext
						    <= 0) { //if the sphere projection is outside both the current Connection AND this neighbouring connection, then create the interaction if the neighbour did not already do it before.
							for (int unsigned j = 0; j < GC->pfacetList.size(); j++) {
								if (GC->pfacetList[j]->getId() != c->id2) {
									const shared_ptr<Interaction> intr
									        = scene->interactions->find(c->id1, GC->pfacetList[j]->getId());
									if (intr && intr->isReal()) {
										shared_ptr<ScGridCoGeom> intrGeom = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
										if (!(intrGeom->isDuplicate == 1)) {
											if (isNew) SGr = false;
											else
												scm->isDuplicate
												        = 1; /*cout<<"Declare "<<c->id1<<"-"<<c->id2<<" as duplicated."<<endl;*/
										}
									}
								}
							}
						} else { //the sphere projection is outside the current Connection but inside the previous neighbour. The contact has to be handled by the Prev GridConnection, not here.
							if (isNew) SGr = false;
							else { //cout<<"The contact "<<c->id1<<"-"<<c->id2<<" HAVE to be copied and deleted NOW."<<endl ;
								scm->isDuplicate = 1;
								scm->trueInt     = -1;
								return true;
							}
						}
					}
				}
			} else if (relPos <= 0.5) {
				if (gridNo1->ConnList.size() > 1) { //	if the node is not an extremity of the Grid (only one connection)
					for (int unsigned i = 0; i < gridNo1->ConnList.size(); i++) { // ... loop on all the Connections of the same Node ...
						GridConnection* GC = (GridConnection*)gridNo1->ConnList[i]->shape.get();
						if (GC == gridCo) continue; //	self comparison.

						Vector3r segtCandidate1 = GC->node1->state->pos
						        - gridNo1St->pos; // (be sure of the direction of segtPrev to compare relPosPrev.)
						Vector3r segtCandidate2 = GC->node2->state->pos - gridNo1St->pos;
						Vector3r segtPrev       = segtCandidate1.norm() > segtCandidate2.norm() ? segtCandidate1 : segtCandidate2;
						for (int j = 0; j < 3; j++) {
							if (math::abs(segtPrev[j]) < 1e-14) segtPrev[j] = 0.0;
						}
						Real relPosPrev = (branch.dot(segtPrev)) / (segtPrev.norm() * segtPrev.norm());
						if (relPosPrev
						    <= 0) { //the sphere projection is inside the current Connection and outide this neighbour connection.
							for (int unsigned j = 0; j < GC->pfacetList.size(); j++) {
								if (GC->pfacetList[j]->getId() != c->id2) {
									const shared_ptr<Interaction> intr
									        = scene->interactions->find(c->id1, GC->pfacetList[j]->getId());
									if (intr
									    && intr->isReal()) { // if an ineraction exist between the sphere and the previous connection, import parameters.
										// 									cout<<"1Copying contact geom and phys from "<<intr->id1<<"-"<<intr->id2<<" to here ("<<c->id1<<"-"<<c->id2<<")"<<endl;
										scm = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
										if (isNew) {
											c->geom         = scm;
											c->phys         = intr->phys;
											c->iterMadeReal = intr->iterMadeReal;
										}
										scm->trueInt     = c->id2;
										scm->isDuplicate = 2; //command the old contact deletion.
										isNew            = 0;
										break;
									}
								}
							}
						}
					}
				}

			}

			else if (relPos > 0.5) {
				if (gridNo2->ConnList.size() > 1) {
					for (int unsigned i = 0; i < gridNo2->ConnList.size(); i++) {
						GridConnection* GC = (GridConnection*)gridNo2->ConnList[i]->shape.get();
						if (GC == gridCo) continue; //	self comparison.
						Vector3r segtCandidate1 = GC->node1->state->pos - gridNo2St->pos;
						Vector3r segtCandidate2 = GC->node2->state->pos - gridNo2St->pos;
						Vector3r segtNext       = segtCandidate1.norm() > segtCandidate2.norm() ? segtCandidate1 : segtCandidate2;
						for (int j = 0; j < 3; j++) {
							if (math::abs(segtNext[j]) < 1e-14) segtNext[j] = 0.0;
						}
						Real relPosNext = (branchN.dot(segtNext)) / (segtNext.norm() * segtNext.norm());
						if (relPosNext
						    <= 0) { //the sphere projection is inside the current Connection and outside this neighbour connection.
							for (int unsigned j = 0; j < GC->pfacetList.size(); j++) {
								if (GC->pfacetList[j]->getId() != c->id2) {
									const shared_ptr<Interaction> intr
									        = scene->interactions->find(c->id1, GC->pfacetList[j]->getId());
									if (intr
									    && intr->isReal()) { // if an ineraction exist between the sphere and the previous connection, import parameters.
										scm = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
										if (isNew) {
											c->geom         = scm;
											c->phys         = intr->phys;
											c->iterMadeReal = intr->iterMadeReal;
										}
										scm->trueInt     = c->id2;
										scm->isDuplicate = 2; //command the old contact deletion.
										break;
									}
								}
							}
						}
					}
				}
			}
			if (SGr) {
				relPos               = relPos < 0 ? 0 : relPos; //min value of relPos : 0
				relPos               = relPos > 1 ? 1 : relPos; //max value of relPos : 1
				Vector3r fictiousPos = gridNo1St->pos + relPos * segt;
				Vector3r branchF     = fictiousPos - spherePos;

				Real dist2 = branchF.norm();
				bool SG    = !(isNew && (dist2 > (sphere->radius + gridCo->radius)));
				if (SG) {
					//	Create the geometry :
					if (isNew) c->geom = scm;
					scm->radius1 = sphere->radius;
					scm->radius2 = gridCo->radius;
					scm->id3     = gridCo->node1->getId();
					scm->id4     = gridCo->node2->getId();

					scm->relPos               = relPos;
					Vector3r normal2          = branchF / dist2;
					scm->penetrationDepth     = sphere->radius + gridCo->radius - dist2;
					scm->fictiousState.pos    = fictiousPos;
					scm->contactPoint         = spherePos + normal2 * (scm->radius1 - 0.5 * scm->penetrationDepth);
					scm->fictiousState.vel    = (1 - relPos) * gridNo1St->vel + relPos * gridNo2St->vel;
					scm->fictiousState.angVel = ((1 - relPos) * gridNo1St->angVel + relPos * gridNo2St->angVel).dot(segt / len) * segt
					                / len                                  //twist part : interpolated
					        + segt.cross(gridNo2St->vel - gridNo1St->vel); // non-twist part : defined from nodes velocities
					scm->precompute(
					        state1,
					        scm->fictiousState,
					        scene,
					        c,
					        normal2,
					        isNew,
					        shift2,
					        true); //use sphere-sphere precompute (with a virtual sphere)
					return true;
				}
			}
		} else { //taking into account the shadow zone
			for (int unsigned i = 0; i < 3; i++) {
				for (int unsigned j = 0; j < GridNodeList[i]->pfacetList.size(); j++) {
					if (GridNodeList[i]->pfacetList[j]->getId() != c->id2) {
						boost::tuple<Vector3r, bool, double, double, double> projectionPrev
						        = projection(GridNodeList[i]->pfacetList[j]->shape, state1);
						bool isintrianglePrev = boost::get<1>(projectionPrev);
						if (!isintrianglePrev) {
							//if the sphere projection is outside both the current PFacet AND this neighbouring PFacet, then create the interaction if the neighbour did not already do it before.
							const shared_ptr<Interaction> intr
							        = scene->interactions->find(c->id1, GridNodeList[i]->pfacetList[j]->getId());
							if (intr && intr->isReal()) {
								shared_ptr<ScGridCoGeom> intrGeom = YADE_PTR_CAST<ScGridCoGeom>(intr->geom);
								if (!(intrGeom->isDuplicate == 1)) { //skip contact.
									if (isNew) {
										return false;
									} else {
										scm->isDuplicate = 1;
									} /*cout<<"Declare "<<c->id1<<"-"<<c->id2<<" as duplicated."<<endl;*/
								}
							}
						} else { //the sphere projection is outside the current PFacet but inside the previous neighbour. The contact has to be handled by the Prev GridConnection, not here.
							if (isNew) {
								return false;
							} else {
								// 						cout<<"The contact "<<c->id1<<"-"<<c->id2<<" HAVE to be copied and deleted NOW."<<endl ;
								scm->isDuplicate = 1;  //scm->isDuplicate=1;
								scm->trueInt     = -1; //trueInt id de l'objet avec lequel il y a un contact -1 = rien faire
								return true;
							}
						}
					}
				}
			}
		}
	}
	if (isintriangle) {
		penetrationDepth = sphereRadius + PFacetradius - math::abs(dist);
		normal.normalize();
		if (penetrationDepth > 0 || c->isReal()) {
			if (isNew) c->geom = scm;
			scm->radius1              = sphereRadius;
			scm->radius2              = PFacetradius;
			scm->id3                  = Pfacet->node1->getId();
			scm->id4                  = Pfacet->node2->getId();
			scm->id5                  = Pfacet->node3->getId();
			scm->weight[0]            = p1;
			scm->weight[1]            = p2;
			scm->weight[2]            = p3;
			scm->penetrationDepth     = penetrationDepth;
			scm->fictiousState.pos    = P;
			scm->contactPoint         = centerS - (PFacetradius - 0.5 * penetrationDepth) * normal;
			scm->fictiousState.vel    = (p1 * Pfacet->node1->state->vel + p2 * Pfacet->node2->state->vel + p3 * Pfacet->node3->state->vel);
			scm->fictiousState.angVel = (p1 * Pfacet->node1->state->angVel + p2 * Pfacet->node2->state->angVel + p3 * Pfacet->node3->state->angVel);
			scm->precompute(
			        state1, scm->fictiousState, scene, c, -normal, isNew, shift2, true); //use sphere-sphere precompute (with a virtual sphere)
			TIMING_DELTAS_CHECKPOINT("Ig2_Sphere_PFacet_ScGridCoGeom");
			return true;
		}
	}
	TIMING_DELTAS_CHECKPOINT("Ig2_Sphere_PFacet_ScGridCoGeom");
	return false;
}
bool Ig2_Sphere_PFacet_ScGridCoGeom::goReverse(
        const shared_ptr<Shape>&       cm1,
        const shared_ptr<Shape>&       cm2,
        const State&                   state1,
        const State&                   state2,
        const Vector3r&                shift2,
        const bool&                    force,
        const shared_ptr<Interaction>& c)
{
	c->swapOrder();
	return go(cm2, cm1, state2, state1, -shift2, force, c);
}
YADE_PLUGIN((Ig2_Sphere_PFacet_ScGridCoGeom));

bool Ig2_GridConnection_PFacet_ScGeom::go(
        const shared_ptr<Shape>& cm1,
        const shared_ptr<Shape>& cm2,
        const State& /*state1*/,
        const State& /*state2*/,
        const Vector3r& /*shift2*/,
        const bool& /*force*/,
        const shared_ptr<Interaction>& c)
{
	GridConnection* gridCo = YADE_CAST<GridConnection*>(cm1.get());
	PFacet*         Pfacet = YADE_CAST<PFacet*>(cm2.get());

	if (gridCo->node1 == Pfacet->node1 || gridCo->node1 == Pfacet->node2 || gridCo->node1 == Pfacet->node3 || gridCo->node2 == Pfacet->node1
	    || gridCo->node2 == Pfacet->node2 || gridCo->node2 == Pfacet->node3) {
		return false;
	}

	Body::id_t idNode1 = gridCo->node1->getId();
	Body::id_t idNode2 = gridCo->node2->getId();
	Body::id_t ids2[3] = { Pfacet->conn1->getId(), Pfacet->conn2->getId(), Pfacet->conn3->getId() };
	Body::id_t id1     = c->id1;
	Body::id_t id2     = c->id2;
	if (!scene->interactions->found(idNode1, id2)) {
		shared_ptr<Interaction> scm1(new Interaction(idNode1, id2));
		scene->interactions->insert(scm1);
	}

	if (!scene->interactions->found(idNode2, id2)) {
		shared_ptr<Interaction> scm2(new Interaction(idNode2, id2));
		scene->interactions->insert(scm2);
	}

	for (int i = 0; i < 3; i++) {
		int                entier = i;
		std::ostringstream oss;
		string             chaine = "scm";
		oss << chaine << entier;
		string chaine1 = oss.str();
		if (!scene->interactions->found(id1, ids2[i])) {
			shared_ptr<Interaction> chain1(new Interaction(id1, ids2[i]));
			scene->interactions->insert(chain1);
		}
	}

	return (false);
}

bool Ig2_GridConnection_PFacet_ScGeom::goReverse(
        const shared_ptr<Shape>&       cm1,
        const shared_ptr<Shape>&       cm2,
        const State&                   state1,
        const State&                   state2,
        const Vector3r&                shift2,
        const bool&                    force,
        const shared_ptr<Interaction>& c)
{
	return go(cm1, cm2, state2, state1, -shift2, force, c);
}
YADE_PLUGIN((Ig2_GridConnection_PFacet_ScGeom));


bool Ig2_PFacet_PFacet_ScGeom::go(
        const shared_ptr<Shape>& cm1,
        const shared_ptr<Shape>& cm2,
        const State& /*state1*/,
        const State& /*state2*/,
        const Vector3r& /*shift2*/,
        const bool& /*force*/,
        const shared_ptr<Interaction>& c)
{
	PFacet*    Pfacet1 = YADE_CAST<PFacet*>(cm1.get());
	PFacet*    Pfacet2 = YADE_CAST<PFacet*>(cm2.get());
	Body::id_t id1     = c->id1;
	Body::id_t id2     = c->id2;

	if (Pfacet1->node1 == Pfacet2->node1 || Pfacet1->node1 == Pfacet2->node2 || Pfacet1->node1 == Pfacet2->node3 || Pfacet1->node2 == Pfacet2->node1
	    || Pfacet1->node2 == Pfacet2->node2 || Pfacet1->node2 == Pfacet2->node3 || Pfacet1->node3 == Pfacet2->node1 || Pfacet1->node3 == Pfacet2->node2
	    || Pfacet1->node3 == Pfacet2->node3) {
		return false;
	}


	boost::tuple<Vector3r, bool, double, double, double> p1 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm1, *Pfacet2->node1->state);
	boost::tuple<Vector3r, bool, double, double, double> p2 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm1, *Pfacet2->node2->state);
	boost::tuple<Vector3r, bool, double, double, double> p3 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm1, *Pfacet2->node3->state);

	boost::tuple<Vector3r, bool, double, double, double> p4 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm2, *Pfacet1->node1->state);
	boost::tuple<Vector3r, bool, double, double, double> p5 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm2, *Pfacet1->node2->state);
	boost::tuple<Vector3r, bool, double, double, double> p6 = Ig2_Sphere_PFacet_ScGridCoGeom::projection(cm2, *Pfacet1->node3->state);

	bool isintriangle1 = boost::get<1>(p1);
	bool isintriangle2 = boost::get<1>(p2);
	bool isintriangle3 = boost::get<1>(p3);


	bool isintriangle4 = boost::get<1>(p4);
	bool isintriangle5 = boost::get<1>(p5);
	bool isintriangle6 = boost::get<1>(p6);


	bool istriangleNodes2P1 = (isintriangle1 == true) && (isintriangle2 == true) && (isintriangle3 == true);
	bool istriangleNodes1P2 = (isintriangle4 == true) && (isintriangle5 == true) && (isintriangle6 == true);

	if (istriangleNodes1P2) {
		Body::id_t idNode11 = Pfacet1->node1->getId();
		Body::id_t idNode12 = Pfacet1->node2->getId();
		Body::id_t idNode13 = Pfacet1->node3->getId();

		// FIXME: if projection of some nodes is in triangle but not in contact, do we need to create interaxctions here?
		if (!scene->interactions->found(idNode11, id2)) {
			shared_ptr<Interaction> scm1(new Interaction(idNode11, id2));
			scene->interactions->insert(scm1);
		}

		if (!scene->interactions->found(idNode12, id2)) {
			shared_ptr<Interaction> scm2(new Interaction(idNode12, id2));
			scene->interactions->insert(scm2);
		}

		if (!scene->interactions->found(idNode13, id2)) {
			shared_ptr<Interaction> scm3(new Interaction(idNode13, id2));
			scene->interactions->insert(scm3);
		}
	} else if (istriangleNodes2P1) {
		Body::id_t idNode21 = Pfacet2->node1->getId();
		Body::id_t idNode22 = Pfacet2->node2->getId();
		Body::id_t idNode23 = Pfacet2->node3->getId();


		if (!scene->interactions->found(idNode21, id1)) {
			shared_ptr<Interaction> scm1(new Interaction(idNode21, id1));
			scene->interactions->insert(scm1);
		}

		if (!scene->interactions->found(idNode22, id1)) {
			shared_ptr<Interaction> scm2(new Interaction(idNode22, id1));
			scene->interactions->insert(scm2);
		}

		if (!scene->interactions->found(idNode23, id1)) {
			shared_ptr<Interaction> scm3(new Interaction(idNode23, id1));
			scene->interactions->insert(scm3);
		}
	} else {
		Body::id_t ids1[3] = { Pfacet1->conn1->getId(), Pfacet1->conn2->getId(), Pfacet1->conn3->getId() };
		Body::id_t ids2[3] = { Pfacet2->conn1->getId(), Pfacet2->conn2->getId(), Pfacet2->conn3->getId() };

		if ((isintriangle1 == false) && (isintriangle2 == false) && (isintriangle3 == false) && (isintriangle4 == false) && (isintriangle5 == false)
		    && (isintriangle6 == false)) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					if (!scene->interactions->found(ids1[i], ids2[j])) {
						if ((Body::byId(ids1[i])->getGroupMask() != 0) and (Body::byId(ids2[j])->getGroupMask() != 0)) {
							shared_ptr<Interaction> chain1(new Interaction(ids1[i], ids2[j]));
							scene->interactions->insert(chain1);
						}
					}
				}
			}
		} else {
			Body::id_t idNode11 = Pfacet1->node1->getId();
			Body::id_t idNode12 = Pfacet1->node2->getId();
			Body::id_t idNode13 = Pfacet1->node3->getId();


			if (!scene->interactions->found(idNode11, id2)) {
				shared_ptr<Interaction> scm1(new Interaction(idNode11, id2));
				scene->interactions->insert(scm1);
			}

			if (!scene->interactions->found(idNode12, id2)) {
				shared_ptr<Interaction> scm2(new Interaction(idNode12, id2));
				scene->interactions->insert(scm2);
			}

			if (!scene->interactions->found(idNode13, id2)) {
				shared_ptr<Interaction> scm3(new Interaction(idNode13, id2));
				scene->interactions->insert(scm3);
			}

			Body::id_t idNode21 = Pfacet2->node1->getId();
			Body::id_t idNode22 = Pfacet2->node2->getId();
			Body::id_t idNode23 = Pfacet2->node3->getId();


			if (!scene->interactions->found(idNode21, id1)) {
				shared_ptr<Interaction> scm1(new Interaction(idNode21, id1));
				scene->interactions->insert(scm1);
			}

			if (!scene->interactions->found(idNode22, id1)) {
				shared_ptr<Interaction> scm2(new Interaction(idNode22, id1));
				scene->interactions->insert(scm2);
			}

			if (!scene->interactions->found(idNode23, id1)) {
				shared_ptr<Interaction> scm3(new Interaction(idNode23, id1));
				scene->interactions->insert(scm3);
			}

			Body::id_t ids1a[3] = { Pfacet1->conn1->getId(), Pfacet1->conn2->getId(), Pfacet1->conn3->getId() };
			Body::id_t ids2a[3] = { Pfacet2->conn1->getId(), Pfacet2->conn2->getId(), Pfacet2->conn3->getId() };

			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					int                entier = j + i * 3;
					std::ostringstream oss;
					string             chaine = "scm";
					oss << chaine << entier;
					string chaine1 = oss.str();
					if (!scene->interactions->found(ids1a[i], ids2a[j])) {
						if ((Body::byId(ids1a[i])->getGroupMask() != 0) and (Body::byId(ids2a[j])->getGroupMask() != 0)) {
							shared_ptr<Interaction> chain1(new Interaction(ids1a[i], ids2a[j]));
							scene->interactions->insert(chain1);
						}
					}
				}
			}
		}
	}
	return (false);
}

bool Ig2_PFacet_PFacet_ScGeom::goReverse(
        const shared_ptr<Shape>&       cm1,
        const shared_ptr<Shape>&       cm2,
        const State&                   state1,
        const State&                   state2,
        const Vector3r&                shift2,
        const bool&                    force,
        const shared_ptr<Interaction>& c)
{
	return go(cm1, cm2, state2, state1, -shift2, force, c);
}
YADE_PLUGIN((Ig2_PFacet_PFacet_ScGeom));


/********* Wall + Sphere **********/

bool Ig2_Wall_PFacet_ScGeom::go(
        const shared_ptr<Shape>& /*cm1*/,
        const shared_ptr<Shape>& cm2,
        const State& /*state1*/,
        const State& /*state2*/,
        const Vector3r& /*shift2*/,
        const bool& /*force*/,
        const shared_ptr<Interaction>& c)
{
	PFacet* Pfacet = YADE_CAST<PFacet*>(cm2.get());

	Body::id_t idNode1 = Pfacet->node1->getId();
	Body::id_t idNode2 = Pfacet->node2->getId();
	Body::id_t idNode3 = Pfacet->node3->getId();

	Body::id_t id1 = c->id1;

	if (!scene->interactions->found(id1, idNode1)) {
		shared_ptr<Interaction> scm1(new Interaction(id1, idNode1));
		scene->interactions->insert(scm1);
	}

	if (!scene->interactions->found(id1, idNode2)) {
		shared_ptr<Interaction> scm2(new Interaction(id1, idNode2));
		scene->interactions->insert(scm2);
	}

	if (!scene->interactions->found(id1, idNode3)) {
		shared_ptr<Interaction> scm3(new Interaction(id1, idNode3));
		scene->interactions->insert(scm3);
	}

	return (false);
}

YADE_PLUGIN((Ig2_Wall_PFacet_ScGeom));
//!##################	Bounds   #####################

void Bo1_PFacet_Aabb::go(const shared_ptr<Shape>& cm, shared_ptr<Bound>& bv, const Se3r& /*se3*/, const Body* /*b*/)
{
	PFacet* Pfacet = YADE_CAST<PFacet*>(cm.get());
	if (!bv) { bv = shared_ptr<Bound>(new Aabb); }
	Aabb*    aabb = static_cast<Aabb*>(bv.get());
	Vector3r O    = Pfacet->node1->state->pos;
	Vector3r O2   = Pfacet->node2->state->pos;
	Vector3r O3   = Pfacet->node3->state->pos;

	if (!scene->isPeriodic) {
		for (int k = 0; k < 3; k++) {
			aabb->min[k] = min(min(O[k], O2[k]), O3[k]) - Pfacet->radius;
			aabb->max[k] = max(max(O[k], O2[k]), O3[k]) + Pfacet->radius;
		}
		return;
	} else {
		O  = scene->cell->unshearPt(O);
		O2 = scene->cell->unshearPt(O2);
		O3 = scene->cell->unshearPt(O3);

		Vector3r T = scene->cell->hSize * Pfacet->cellDist.cast<Real>();
		O          = O + T;
		O2         = O2 + T;
		O3         = O3 + T;
		for (int k = 0; k < 3; k++) {
			aabb->min[k] = min(min(O[k], O2[k]), O3[k]) - Pfacet->radius;
			aabb->max[k] = max(max(O[k], O2[k]), O3[k]) + Pfacet->radius;
		}
	}
}

YADE_PLUGIN((Bo1_PFacet_Aabb));

} // namespace yade