particle_cell_3D.hpp 21.7 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

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

template <typename PFP>
void ParticleCell3D<PFP>::display()
{
45
//	std::cout << "position : " << this->m_position << std::endl;
Thomas's avatar
Thomas committed
46
47
48
49
50
}

template <typename PFP>
typename PFP::VEC3 ParticleCell3D<PFP>::pointInFace(Dart d)
{
51
	return Algo::Surface::Geometry::faceCentroid<PFP>(m,d,position);
52
//	const VEC3& p1(this->m_positions[d]);
Thomas's avatar
Thomas committed
53
//	Dart dd=m.phi1(d);
54
//	const VEC3& p2(this->m_positions[dd]);
Thomas's avatar
Thomas committed
55
//	dd=m.phi1(dd);
56
//	VEC3& p3(this->m_positions[dd]);
Thomas's avatar
Thomas committed
57
58
59
//
//	while(Geom::testOrientation2D(p3,p1,p2)==Geom::ALIGNED) {
//		dd = m.phi1(dd);
60
//		p3 = this->m_positions[dd];
Thomas's avatar
Thomas committed
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
//	}
//
//	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>
Sylvain Thery's avatar
Sylvain Thery committed
156
Dart ParticleCell3D<PFP>::nextDartOfVertexNotMarked(Dart d, CellMarkerStore<FACE>& 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>
Sylvain Thery's avatar
Sylvain Thery committed
260
Dart ParticleCell3D<PFP>::nextFaceNotMarked(Dart d, CellMarkerStore<FACE>& 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
	VEC3 som = position[d];

368
369
	if(Geom::arePointsEquals(current, this->m_position)) {
		this->m_position = this->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
		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;
445
446
					this->m_position = pointInFace(d);
					this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
447
448
449
450
451
452
453
454
455
					volumeState(current);
					return;
				}
			}
		}
	}while(wsof==1);

	if(wsof!=0)
	{
456
		this->m_position = pointInFace(d);
Thomas's avatar
Thomas committed
457
		d = nextNonPlanar(d);
458
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
459
460
461
462
		volumeState(current);
	}
	else
	{
463
		//Dart ddd=d;
Thomas's avatar
Thomas committed
464
465
466
467
468
469
470
471
472

		edgeState(current);
	}
}

template <typename PFP>
void ParticleCell3D<PFP>::edgeState(const VEC3& current)
{
	#ifdef DEBUG
473
	std::cout << "edgeState" <<  d <<  " " << this->m_position << std::endl;
Thomas's avatar
Thomas committed
474
475
476

	#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
			d = m.alpha_2(d);
			wsof = whichSideOfFace(current,d);
		}

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

	if(wsof==-1)  {

524
		this->m_position = pointInFace(d);
Thomas's avatar
Thomas committed
525
		d = nextNonPlanar(m.phi1(d));
526
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
527
528
529
530
531
532
533
534
		volumeState(current);
		return;
	}
	else {
		if(onEdge) {
			if(isOnHalfEdge(current,d))
				if (isOnHalfEdge(current,m.phi3(d))) {
					state=2;
535
					this->m_position = this->m_positionFace = current;
Thomas's avatar
Thomas committed
536
537
538
539
540
541
542
543
544
545
				}
				else {
					d=m.phi1(d);
					vertexState(current);
				}
			else {
				vertexState(current);
			}
		}
		else {
546
			this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
547
548
549
550
551
552
553
554
555
556
557
558
559
			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

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

	if (wsof==Geom::OVER) {
		d = m.phi3(d);
		d = nextNonPlanar(d);
568
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
569
570
571
572
573
		volumeState(current);
		return;
	}
	else if(wsof==Geom::UNDER) {
		d = nextNonPlanar(d);
574
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
575
576
577
578
		volumeState(current);
		return;
	}

579
	VEC3 norm = Algo::Surface::Geometry::faceNormal<PFP>(m,d,position);
Thomas's avatar
Thomas committed
580
581

	Dart dd=d;
582
	if(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER) {
Thomas's avatar
Thomas committed
583
		d = m.phi_1(d);
584
		while(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER && dd!=d)
Thomas's avatar
Thomas committed
585
586
587
588
589
590
591
592
			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;
593
				case Geom::ON :this->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);
598
599
					VEC3 p3 = pl.normal()+this->m_position;
					Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
600
601
					VEC3 e(position[m.phi1(d)]-position[d]);

602
					Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
603
604
605
606
607
608

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

609
			this->m_position = this->m_positionFace = current;
610
			state = FACE;
Thomas's avatar
Thomas committed
611
612
613
614
			return;
		}
	}
	else {
615
		while(isRightDFace(current,d,this->m_positionFace,norm) && m.phi1(d)!=dd)
Thomas's avatar
Thomas committed
616
617
618
619
620
621
622
623
624
625
			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;
626
				case Geom::ON :this->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);
631
632
					 VEC3 p3 = pl.normal()+this->m_position;
					 Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
633
634
					 VEC3 e(position[m.phi1(d)]-position[d]);

635
					 Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
636
637
638
639
640
					edgeState(current);
					return;
				}
			}while(d!=dd);

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


	switch (whichSideOfEdge(current,d))
	{
	case Geom::OVER :
651
		 this->m_position = this->m_positionFace = current;
652
		 state = FACE;
Thomas's avatar
Thomas committed
653
654
		 break;
	case Geom::ON :
655
		 this->m_position = this->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);
660
661
		 VEC3 p3 = pl.normal()+this->m_position;
		 Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
662
663
		 VEC3 e(position[m.phi1(d)]-position[d]);

664
		 Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
665

666
		 this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
667
668
669
670
671
672
673
674
675
676
677
678

		 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
	bool above;

	Geom::Orientation3D testRight=Geom::OVER;

	do {
		above=false;

		Dart dd=d;
		bool particularcase=false;
689
		Geom::Orientation3D testLeft = isLeftL1DVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
690
691

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

Thomas's avatar
Thomas committed
693
694
			d = m.phi_1(d);

695
			while(dd!=d && isLeftL1DVol(current,d,this->m_positionFace,this->m_position)!=Geom::UNDER)
Thomas's avatar
Thomas committed
696
697
698
699
700
701
702
				d = m.phi_1(d);

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

703
			testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
704
705
706
707


			while(testRight!=Geom::OVER && dd!=m.phi1(d)) {
				d = m.phi1(d);
708
				testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
709
710
711
712
713
714
			}

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

715
		if(particularcase) //(this->m_position,this->m_positionFace,c) presque alignés et si c est proche de la face
Thomas's avatar
Thomas committed
716
717
718
719
720
721
				  //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();
722
				this->m_position = current;
Thomas's avatar
Thomas committed
723
724
725
726
727
728
729
				return;
			}

			volumeSpecialCase(current);
			return;
		}

730
		Geom::Orientation3D testAbove = isAbove(current,d,this->m_position);
Thomas's avatar
Thomas committed
731

732
		if(testAbove!=Geom::UNDER || (testRight==Geom::ON && isAbove(current,m.phi_1(d),this->m_position)!=Geom::UNDER)) {
Thomas's avatar
Thomas committed
733
734
735
736
737
738
739
740
741
742
743

			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

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

752
				this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
753
754
755
756
757
758
759
			}
		}
	} while(above);

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

	if(wsof==Geom::UNDER) {
760
		this->m_position = current;
761
		state = VOLUME;
Thomas's avatar
Thomas committed
762
763
	}
	else if(wsof==Geom::ON) {
764
765
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
			this->m_position = this->m_positionFace = current;
766
			state = FACE;
Thomas's avatar
Thomas committed
767
768
		}
		else {
769
			this->m_position = this->m_positionFace = current;
Thomas's avatar
Thomas committed
770
771
772
773
			edgeState(current);
		}
	}
	else {
774
775
776
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
//			this->m_position = m.intersectDartPlaneLine(d,this->m_position,current);
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
777
778
779
			faceState(current,wsof);
		}
		else {
780
781
//			this->m_position = m.intersectDartPlaneLineEdge(d,this->m_position,current);
			Algo::Surface::Geometry::intersectionLineConvexFace<PFP>(m,d,position,this->m_position,current-this->m_position,this->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

	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;

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

				return;
			}
			else if(wsof==Geom::ON)
			{
834
				this->m_position = current;
Thomas's avatar
Thomas committed
835
836
837
838
839
840
				d = *face;

				faceState(current);
				return;
			}

841
			Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,*face,position);
842
			if(pl.normal()*VEC3(current-this->m_position)>0)
Thomas's avatar
Thomas committed
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
			{
				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];
			}
		}
	}

872
	this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
873
874
875
876

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

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

}
}
}
}