particle_cell_3D.hpp 21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Thomas's avatar
Thomas committed
25
// #define DEBUG
26
27
28
29
30
31
32
33
34
35
36
37
namespace CGoGN
{

namespace Algo
{

namespace Volume
{

namespace MovingObjects
{

Thomas's avatar
Thomas committed
38
39
40
41
42
43
44
45
46
47
48
49
50

#define DELTA 0.00001
//static const float DELTA=0.00001;

template <typename PFP>
void ParticleCell3D<PFP>::display()
{
//	std::cout << "position : " << m_position << std::endl;
}

template <typename PFP>
typename PFP::VEC3 ParticleCell3D<PFP>::pointInFace(Dart d)
{
51
	return Algo::Surface::Geometry::faceCentroid<PFP>(m,d,position);
Thomas's avatar
Thomas committed
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
//	const VEC3& p1(m_positions[d]);
//	Dart dd=m.phi1(d);
//	const VEC3& p2(m_positions[dd]);
//	dd=m.phi1(dd);
//	VEC3& p3(m_positions[dd]);
//
//	while(Geom::testOrientation2D(p3,p1,p2)==Geom::ALIGNED) {
//		dd = m.phi1(dd);
//		p3 = m_positions[dd];
//	}
//
//	CGoGNout << "pointInFace " << (p1+p3)*0.5f << CGoGNendl;
//
//	return (p1+p3)*0.5f;
}


template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::isLeftENextVertex(VEC3 c, Dart d, VEC3 base)
{
	VEC3 p = position[d];
	VEC3 p1 = position[m.phi1(m.phi2(d))];

	Geom::Plane3D<typename PFP::REAL> pl(p,base,p1);
	return pl.orient(c);
}

template <typename PFP>
bool ParticleCell3D<PFP>::isRightVertex(VEC3 c, Dart d, VEC3 base)
{
	VEC3 p = position[d];
	VEC3 p1 = position[m.phi_1(d)];

	Geom::Plane3D<typename PFP::REAL> pl(p,base,p1);
	return pl.orient(c)==Geom::UNDER;
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::whichSideOfFace(VEC3 c, Dart d)
{
92
	Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
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
	return pl.orient(c);
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::isLeftL1DVol(VEC3 c, Dart d, VEC3 base, VEC3 top)
{
	VEC3 p2 = position[d];

	Geom::Plane3D<typename PFP::REAL> pl(top,p2,base);

	return pl.orient(c);
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::isRightDVol(VEC3 c, Dart d, VEC3 base, VEC3 top)
{
	VEC3 p1= position[m.phi1(d)];

	Geom::Plane3D<typename PFP::REAL> pl(top,p1,base);

	return pl.orient(c);
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::isAbove(VEC3 c, Dart d, VEC3 top)
{
	VEC3 p1 = position[d];
	VEC3 p2 = position[m.phi1(d)];

	Geom::Plane3D<typename PFP::REAL> pl(top,p2,p1);


	return pl.orient(c);
}

template <typename PFP>
int ParticleCell3D<PFP>::isLeftL1DFace(VEC3 c, Dart d, VEC3 base, VEC3 normal)
{
	VEC3 p2 = position[d];

	VEC3 v2(p2-base);

	VEC3 np = normal ^ v2;

	Geom::Plane3D<typename PFP::REAL> pl(np,base*np);

	return pl.orient(c);
}

template <typename PFP>
bool ParticleCell3D<PFP>::isRightDFace(VEC3 c, Dart d, VEC3 base, VEC3 normal)
{
	VEC3 p1 = position[m.phi1(d)];

	VEC3 np = normal ^ VEC3(p1-base);

	Geom::Plane3D<typename PFP::REAL> pl(np,base*np);

	return pl.orient(c)==Geom::UNDER;
}


template <typename PFP>
156
Dart ParticleCell3D<PFP>::nextDartOfVertexNotMarked(Dart d, CellMarkerGen& mark)
Thomas's avatar
Thomas committed
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
{
	// lock a marker
	Dart d1;
	DartMarkerNoUnmark markCC(m);

	// init algo with parameter dart
	std::list<Dart> darts_list;
	darts_list.push_back(d);
	markCC.mark(d);

	// use iterator for begin of not yet treated darts
	std::list<Dart>::iterator beg = darts_list.begin();

	// until all darts treated
	while (beg != darts_list.end())
	{
		d1 = *beg;
		// add phi1, phi2 and phi3 successor if they are not yet marked
		Dart d2 = m.phi1(m.phi2(d1));
		Dart d3 = m.phi1(m.phi3(d1));

		if (!markCC.isMarked(d2)) {
			darts_list.push_back(d2);
			markCC.mark(d2);
		}

		if (!markCC.isMarked(d3)) {
			darts_list.push_back(d3);
			markCC.mark(d3);
		}

		beg++;

		// apply functor
		if (!mark.isMarked(d1)) {
			for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
				markCC.unmark(*it);
			return d1;
		}
	}

	// clear markers
	for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
		markCC.unmark(*it);

	return d;
}

template <typename PFP>
Dart ParticleCell3D<PFP>::nextNonPlanar(Dart d)
{
	// lock a marker
	Dart d1;
	DartMarkerNoUnmark markCC(m);

	// init algo with parameter dart
	std::list<Dart> darts_list;
	darts_list.push_back(d);
	markCC.mark(d);

	// use iterator for begin of not yet treated darts
	std::list<Dart>::iterator beg = darts_list.begin();

	// until all darts treated
	while (beg != darts_list.end()) {
		d1 = *beg;
		// add phi1, phi2 and phi3 successor if they are not yet marked
		Dart d2 = m.phi1(d1);
		Dart d3 = m.phi2(d1);
		Dart d4 = m.phi_1(d1);

		if (!markCC.isMarked(d2)) {
			darts_list.push_back(d2);
			markCC.mark(d2);
		}

		if (!markCC.isMarked(d3)) {
			darts_list.push_back(d3);
			markCC.mark(d3);
		}

		if (!markCC.isMarked(d4)) {
			darts_list.push_back(d4);
			markCC.mark(d4);
		}

		beg++;

		// apply functor
246
247
		Geom::Plane3D<typename PFP::REAL> pl1 = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
		Geom::Plane3D<typename PFP::REAL> pl2 = Algo::Surface::Geometry::facePlane<PFP>(m,d1,position);
Thomas's avatar
Thomas committed
248
249
250
251
252
253
254
255
256
257
258
259
		if ((pl1.normal()-pl2.normal()).norm2()>0.000001)
			beg = darts_list.end();
		// remove the head of the list
	}
	// clear markers
	for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
		markCC.unmark(*it);

	return d1;
}

template <typename PFP>
260
Dart ParticleCell3D<PFP>::nextFaceNotMarked(Dart d, CellMarkerGen& mark)
Thomas's avatar
Thomas committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{
	// lock a marker
	Dart d1;
	DartMarkerNoUnmark markCC(m);

	// init algo with parameter dart
	std::list<Dart> darts_list;
	darts_list.push_back(d);
	markCC.mark(d);

	// use iterator for begin of not yet treated darts
	std::list<Dart>::iterator beg = darts_list.begin();

	// until all darts treated
	while (beg != darts_list.end())
	{
		d1 = *beg;

		if(!mark.isMarked(d1))
		{
281
282
283
284
285
286
			for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
			{
				markCC.unmark(*it);
			}

			return d1;
Thomas's avatar
Thomas committed
287
288
289
290
291
292
		}

		// add phi1, phi2 and phi3 successor if they are not yet marked
		Dart d2 = m.phi1(d1);
		Dart d3 = m.phi2(d1);
		Dart d4 = m.phi_1(d1);
293

Thomas's avatar
Thomas committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
		if (!markCC.isMarked(d2))
		{
			darts_list.push_back(d2);
			markCC.mark(d2);
		}
		if (!markCC.isMarked(d3))
		{
			darts_list.push_back(d3);
			markCC.mark(d3);
		}
		if (!markCC.isMarked(d4))
		{
			darts_list.push_back(d4);
			markCC.mark(d4);
		}
		beg++;
	}
Thomas's avatar
Thomas committed
311

Thomas's avatar
Thomas committed
312
313
314
315
316
317
	// clear markers
	for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
	{
		markCC.unmark(*it);
	}

318
//	if(beg==darts_list.end())
Thomas's avatar
Thomas committed
319
320
		return d;

321
//	return d1;
Thomas's avatar
Thomas committed
322
323
324
325
326
327
328
329
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::whichSideOfEdge(VEC3 c, Dart d)
{
	VEC3 p1 = position[m.phi1(d)];
	VEC3 p2 = position[d];

330
	Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
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
	VEC3 norm = pl.normal();
	VEC3 n2 = norm ^ VEC3(p2-p1);

	Geom::Plane3D<typename PFP::REAL> pl2(n2,p1*n2);

	return pl2.orient(c);
}

template <typename PFP>
bool ParticleCell3D<PFP>::isOnHalfEdge(VEC3 c, Dart d)
{
	VEC3 p1 = position[d];
	VEC3 p2 = position[m.phi1(d)];

	VEC3 norm(p2-p1);
	norm.normalize();

	Geom::Plane3D<typename PFP::REAL> pl(norm,p1*norm);

	return pl.orient(c)==Geom::OVER && !Geom::arePointsEquals(c,p1);
}


/**when the ParticleCell3D trajectory go through a vertex
*  searching the good volume "umbrella" where the ParticleCell3D is
*  if the ParticleCell3D is on the vertex, do nothing */
template <typename PFP>
void ParticleCell3D<PFP>::vertexState(const VEC3& current)
{
	#ifdef DEBUG
	std::cout << "vertexState" << d << std::endl;
	#endif

364
365
	crossCell = CROSS_OTHER ;

Thomas's avatar
Thomas committed
366
367
368
369
	VEC3 som = position[d];

	if(Geom::arePointsEquals(current, m_position)) {
		m_position = m_positionFace = som;
370
		state = VERTEX;
Thomas's avatar
Thomas committed
371
372
373
374
375
		return;
	}

	Dart dd=d;
	Geom::Orientation3D wsof;
376
	CellMarkerStore<FACE> mark(m);
Thomas's avatar
Thomas committed
377
378

	do {
379
		VEC3 dualsp = (som+ Algo::Surface::Geometry::vertexNormal<PFP>(m,d,position));
Thomas's avatar
Thomas committed
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
		Dart ddd=d;

		mark.mark(d);

		//searching the good orientation in a volume
		if(isLeftENextVertex(current,d,dualsp)!=Geom::UNDER) {

			d=m.phi1(m.phi2(d));
			while(isLeftENextVertex(current,d,dualsp)!=Geom::UNDER && ddd!=d)
				d=m.phi1(m.phi2(d));

			if(ddd==d) {
				if(isnan(current[0]) || isnan(current[1]) || isnan(current[2])) {
					std::cout << __FILE__ << " " << __LINE__ << " NaN !" << std::endl;
				}

				bool verif = true;
				do {
					if(whichSideOfFace(current,d)!=Geom::OVER)
						verif = false;
					else
						d=m.alpha1(d);
				} while(verif && d!=ddd);

				if(verif) {
					volumeState(current);
					return;
				}
			}
		}
		else {
			while(isRightVertex(current,d,dualsp) && dd!=m.alpha_1(d))
				d=m.phi2(m.phi_1(d));
		}

		wsof=whichSideOfFace(current,d);

		//if c is before the vertex on the edge, we have to change of umbrella, we are symetric to the good umbrella
		if(wsof!=Geom::OVER) {

			VEC3 p1=position[d];
			VEC3 p2=position[m.phi1(d)];
			VEC3 norm(p2-p1);
			norm.normalize();
			Geom::Plane3D<typename PFP::REAL> plane(norm,p1*norm);

			wsof = plane.orient(current);
		}

		//if c is on the other side of the face, we have to change umbrella
		//if the umbrella has already been tested, we just take another arbitrary one
		if(wsof==1) {

			mark.mark(d);

			if(!mark.isMarked(m.alpha1(d)))
				d=m.alpha1(d);
			else {

				Dart dtmp=d;
				d = nextDartOfVertexNotMarked(d,mark);
				if(dtmp==d) {
					std::cout << "numerical rounding ?" << std::endl;

					d = dd;
					m_position = pointInFace(d);
					m_positionFace = m_position;
					volumeState(current);
					return;
				}
			}
		}
	}while(wsof==1);

	if(wsof!=0)
	{
		m_position = pointInFace(d);
		d = nextNonPlanar(d);
		m_positionFace = pointInFace(d);
		volumeState(current);
	}
	else
	{
463
		//Dart ddd=d;
Thomas's avatar
Thomas committed
464
465
466
467
468
469
470
471
472
473
474
475
476

		edgeState(current);
	}
}

template <typename PFP>
void ParticleCell3D<PFP>::edgeState(const VEC3& current)
{
	#ifdef DEBUG
	std::cout << "edgeState" <<  d <<  " " << m_position << std::endl;

	#endif

477
478
	crossCell = CROSS_OTHER ;

Thomas's avatar
Thomas committed
479
480
481
482
483
484
485
486
487
488
489
490
	bool onEdge=false;
	Dart dd=d;
	Geom::Orientation3D wsof = whichSideOfFace(current,m.alpha2(d));
	if(wsof!=Geom::UNDER) {
		do {
			d = m.alpha2(d);
			wsof = whichSideOfFace(current,m.alpha2(d));
		}while(wsof!=1 && dd!=d);

		if(d==dd)
			onEdge = true;

491
		if(wsof==Geom::ON) {
Thomas's avatar
Thomas committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
			switch(whichSideOfEdge(current,d)) {
			case Geom::ON :
				 onEdge=true;
					break;
			case Geom::UNDER :
					d = m.phi2(d);
					break;
			default :
				break;
			}
		}

		wsof = whichSideOfFace(current,d);
	}
	else {
		wsof = whichSideOfFace(current,d);

509
		while(wsof==Geom::UNDER && dd != m.alpha_2(d)) {
Thomas's avatar
Thomas committed
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
			d = m.alpha_2(d);
			wsof = whichSideOfFace(current,d);
		}

		switch(whichSideOfEdge(current,d)) {
			case Geom::ON : onEdge=true;
					break;
			default :
					break;
		}
	}

	if(wsof==-1)  {

		m_position = pointInFace(d);
		d = nextNonPlanar(m.phi1(d));
		m_positionFace = pointInFace(d);
		volumeState(current);
		return;
	}
	else {
		if(onEdge) {
			if(isOnHalfEdge(current,d))
				if (isOnHalfEdge(current,m.phi3(d))) {
					state=2;
					m_position = m_positionFace = current;
				}
				else {
					d=m.phi1(d);
					vertexState(current);
				}
			else {
				vertexState(current);
			}
		}
		else {
			m_positionFace = m_position;
			d=m.phi1(d);
			faceState(current,wsof);
		}
	}
}

 template <typename PFP>
 void ParticleCell3D<PFP>::faceState(const VEC3& current, Geom::Orientation3D wsof)
{
	#ifdef DEBUG
	std::cout << "faceState" <<  d << std::endl;
	#endif

560
	crossCell = CROSS_FACE ;
Thomas's avatar
Thomas committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

	if(wsof==Geom::ON)
		wsof = whichSideOfFace(current,d);

	if (wsof==Geom::OVER) {
		d = m.phi3(d);
		d = nextNonPlanar(d);
		m_positionFace = pointInFace(d);
		volumeState(current);
		return;
	}
	else if(wsof==Geom::UNDER) {
		d = nextNonPlanar(d);
		m_positionFace = pointInFace(d);
		volumeState(current);
		return;
	}

579
	VEC3 norm = Algo::Surface::Geometry::faceNormal<PFP>(m,d,position);
Thomas's avatar
Thomas committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593

	Dart dd=d;
	if(isLeftL1DFace(current,d,m_positionFace,norm)!=Geom::UNDER) {
		d = m.phi_1(d);
		while(isLeftL1DFace(current,d,m_positionFace,norm)!=Geom::UNDER && dd!=d)
			d = m.phi_1(d);

		if(dd==d) {
			std::cout << "sortie ?(1)" << std::endl;
			do {
				switch (whichSideOfEdge(current,d)) {
				case Geom::OVER : d=m.phi_1(d);
					break;
				case Geom::ON :m_position = current;
594
					state = EDGE;
Thomas's avatar
Thomas committed
595
596
					return;
				default :
597
					Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
598
599
600
601
602
603
604
605
606
607
608
609
					VEC3 p3 = pl.normal()+m_position;
					Geom::Plane3D<typename PFP::REAL> plOrtho(m_position,current,p3);
					VEC3 e(position[m.phi1(d)]-position[d]);

					Geom::intersectionPlaneRay(plOrtho,m_position,current-m_position,m_position);

					edgeState(current);
					return;
				}
			} while(d!=dd);

			m_position = m_positionFace = current;
610
			state = FACE;
Thomas's avatar
Thomas committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
			return;
		}
	}
	else {
		while(isRightDFace(current,d,m_positionFace,norm) && m.phi1(d)!=dd)
			d = m.phi1(d);

		if(m.phi_1(d)==dd) {
			d = m.phi1(d);
			do {
				switch (whichSideOfEdge(current,d))
				{
				case Geom::OVER :
					d=m.phi_1(d);
					break;
				case Geom::ON :m_position = current;
627
					state = EDGE;
Thomas's avatar
Thomas committed
628
629
					return;
				default :
630
					 Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
631
632
633
634
635
636
637
638
639
640
641
					 VEC3 p3 = pl.normal()+m_position;
					 Geom::Plane3D<typename PFP::REAL> plOrtho(m_position,current,p3);
					 VEC3 e(position[m.phi1(d)]-position[d]);

					 Geom::intersectionPlaneRay(plOrtho,m_position,current-m_position,m_position);
					edgeState(current);
					return;
				}
			}while(d!=dd);

			m_position = m_positionFace = current;
642
			state = FACE;
Thomas's avatar
Thomas committed
643
644
645
646
647
648
649
650
651
			return;
		}
	}


	switch (whichSideOfEdge(current,d))
	{
	case Geom::OVER :
		 m_position = m_positionFace = current;
652
		 state = FACE;
Thomas's avatar
Thomas committed
653
654
655
		 break;
	case Geom::ON :
		 m_position = m_positionFace = current;
656
		 state = EDGE;
Thomas's avatar
Thomas committed
657
658
		 break;
	default :
659
		 Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
		 VEC3 p3 = pl.normal()+m_position;
		 Geom::Plane3D<typename PFP::REAL> plOrtho(m_position,current,p3);
		 VEC3 e(position[m.phi1(d)]-position[d]);

		 Geom::intersectionPlaneRay(plOrtho,m_position,current-m_position,m_position);

		 m_positionFace = m_position;

		 edgeState(current);
	}
}

template <typename PFP>
void ParticleCell3D<PFP>::volumeState(const VEC3& current)
{
	#ifdef DEBUG
	std::cout << "volumeState " <<  d << std::endl;
	#endif

679
	CellMarkerStore<FACE> mark(m);
Thomas's avatar
Thomas committed
680
681
682
683
684
685
686
687
688
689
690
691
	bool above;

	Geom::Orientation3D testRight=Geom::OVER;

	do {
		above=false;

		Dart dd=d;
		bool particularcase=false;
		Geom::Orientation3D testLeft = isLeftL1DVol(current,d,m_positionFace,m_position);

		if(testLeft!=Geom::UNDER) {
Thomas's avatar
Thomas committed
692

Thomas's avatar
Thomas committed
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
			d = m.phi_1(d);

			while(dd!=d && isLeftL1DVol(current,d,m_positionFace,m_position)!=Geom::UNDER)
				d = m.phi_1(d);

			if(dd==d)
				particularcase=true;
		}
		else {

			testRight = isRightDVol(current,d,m_positionFace,m_position);


			while(testRight!=Geom::OVER && dd!=m.phi1(d)) {
				d = m.phi1(d);
				testRight = isRightDVol(current,d,m_positionFace,m_position);
			}

			if(testLeft==0 && dd==m.phi1(d))
				particularcase=true;
		}

		if(particularcase) //(m_position,m_positionFace,c) presque alignés et si c est proche de la face
				  //aucun des "above" sur les dart ne va donner de résultats concluant (proche de 0 pour tous)
		{
			if(isnan(current[0]) || isnan(current[1]) || isnan(current[2]))
			{
				std::cout << __FILE__ << " " << __LINE__ << " NaN !" << std::endl;
				display();
				m_position = current;
				return;
			}

			volumeSpecialCase(current);
			return;
		}

		Geom::Orientation3D testAbove = isAbove(current,d,m_position);

		if(testAbove!=Geom::UNDER || (testRight==Geom::ON && isAbove(current,m.phi_1(d),m_position)!=Geom::UNDER)) {

			if(testAbove==Geom::OVER || whichSideOfFace(current,d)==Geom::UNDER) {

				mark.mark(d);

				above=true;
				d = m.phi2(d);

				if(mark.isMarked(d)) {
					dd = d;
					d = nextFaceNotMarked(d,mark);
Thomas's avatar
Thomas committed
744
					mark.mark(d);
Thomas's avatar
Thomas committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760

					if(d==dd) {
						volumeSpecialCase(current);
						return;
					}
				}

				m_positionFace = pointInFace(d);
			}
		}
	} while(above);

	Geom::Orientation3D wsof = whichSideOfFace(current,d);

	if(wsof==Geom::UNDER) {
		m_position = current;
761
		state = VOLUME;
Thomas's avatar
Thomas committed
762
763
764
765
	}
	else if(wsof==Geom::ON) {
		if(isAbove(current,d,m_position)==Geom::UNDER) {
			m_position = m_positionFace = current;
766
			state = FACE;
Thomas's avatar
Thomas committed
767
768
769
770
771
772
773
774
775
		}
		else {
			m_position = m_positionFace = current;
			edgeState(current);
		}
	}
	else {
		if(isAbove(current,d,m_position)==Geom::UNDER) {
//			m_position = m.intersectDartPlaneLine(d,m_position,current);
776
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
777
778
779
780
			faceState(current,wsof);
		}
		else {
//			m_position = m.intersectDartPlaneLineEdge(d,m_position,current);
781
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
782
783
784
785
786
787
788
789
790
791
792
793
794
			edgeState(current);
		}
	}
}

template <typename PFP>
void ParticleCell3D<PFP>::volumeSpecialCase(const VEC3& current)
{
	#ifdef DEBUG
	std::cout << "volumeSpecialCase " <<  d << std::endl;
	#endif

	Dart dd;
795
	CellMarkerStore<FACE> mark(m);
Thomas's avatar
Thomas committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

	Dart d_min;

	std::vector<Dart> dart_list;
	std::vector<float> dist_list;

	std::list<Dart> visitedFaces;			// Faces that are traversed
	visitedFaces.push_back(d);				// Start with the face of d
	std::list<Dart>::iterator face;

	// For every face added to the list
	for (face = visitedFaces.begin();face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))
		{	// Face has not been visited yet

			Geom::Orientation3D wsof = whichSideOfFace(current,*face);
			if(wsof==Geom::OVER)
			{
				d = *face;

				if(isAbove(current,d,m_position)==Geom::UNDER)
				{
//					m_position = m.intersectDartPlaneLine(d,m_position,current);
820
					Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
821
822
823
824
825
					faceState(current,Geom::OVER);
				}
				else
				{
//					m_position = m.intersectDartPlaneLineEdge(d,m_position,current);
826
					Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
827
828
829
830
831
832
833
834
835
836
837
838
839
840
					edgeState(current);
				}

				return;
			}
			else if(wsof==Geom::ON)
			{
				m_position = current;
				d = *face;

				faceState(current);
				return;
			}

841
			Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,*face,position);
Thomas's avatar
Thomas committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
			if(pl.normal()*VEC3(current-m_position)>0)
			{
				dist_list.push_back(-pl.distance(current));
				dart_list.push_back(*face);
			}

			// If the face wasn't crossed then mark visited darts (current face)
			// and add non visited adjacent faces to the list of face
			Dart ddd = *face;
			do {
					mark.mark(ddd);
					Dart adj = m.phi2(ddd);			// Get adjacent face
					if (adj != ddd && !mark.isMarked(adj))
						visitedFaces.push_back(adj);// Add it
			} while(ddd!=*face);
		}
	}

	if(dist_list.size()>0) {
		float min=dist_list[0];
		for(unsigned int i = 1;i<dist_list.size();++i)
		{
			if(dist_list[i]<min)
			{
				d=dart_list[i];
				min = dist_list[i];
			}
		}
	}

	m_positionFace = pointInFace(d);

	Geom::Orientation3D wsof = whichSideOfFace(current,d);

	if(wsof==Geom::UNDER) {
		m_position = current;
878
		state = VOLUME;
Thomas's avatar
Thomas committed
879
880
881
882
	}
	else if(wsof==Geom::ON) {
		if(isAbove(current,d,m_position)==Geom::UNDER) {
			m_position = current;
883
			state = FACE;
Thomas's avatar
Thomas committed
884
885
886
		}
		else {
			m_position = current;
887
			state = EDGE;
Thomas's avatar
Thomas committed
888
889
890
891
892
893
		}
	}
	else {
		if(isAbove(current,d,m_position)==Geom::UNDER)
		{
//			m_position = m.intersectDartPlaneLine(d,m_position,current);
894
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
895
896
897
898
			faceState(current,wsof);
		}
		else {
//			m_position = m.intersectDartPlaneLineEdge(d,m_position,current);
899
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,m_position,current-m_position,m_position);
Thomas's avatar
Thomas committed
900
901
902
903
			edgeState(current);
		}
	}
}
904
905
906
907
908
909

}
}
}
}