Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

particle_cell_3D.hpp 24.2 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
Pierre Kraemer's avatar
Pierre Kraemer committed
26

27
28
29
30
31
32
33
34
35
36
37
38
namespace CGoGN
{

namespace Algo
{

namespace Volume
{

namespace MovingObjects
{

Thomas's avatar
Thomas committed
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
//	}
//
//	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)
{
91
	Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
Thomas's avatar
Thomas committed
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
	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>
pitiot's avatar
up  
pitiot committed
128
int ParticleCell3D<PFP>::isLeftL1DFace(VEC3 c, Dart d, VEC3 base, VEC3 normal) // orientation par rapport au plan de gauche de l'arete visée
Thomas's avatar
Thomas committed
129
130
131
132
133
134
135
136
137
138
139
140
141
{
	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>
pitiot's avatar
up  
pitiot committed
142
bool ParticleCell3D<PFP>::isRightDFace(VEC3 c, Dart d, VEC3 base, VEC3 normal) // orientation par rapport au plan de droite de l'arete visée
Thomas's avatar
Thomas committed
143
144
145
146
147
148
149
150
151
152
153
{
	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>
Pierre Kraemer's avatar
Pierre Kraemer committed
154
Dart ParticleCell3D<PFP>::nextDartOfVertexNotMarked(Dart d, CellMarkerStore<MAP, FACE>& mark)
Thomas's avatar
Thomas committed
155
156
157
{
	// lock a marker
	Dart d1;
Pierre Kraemer's avatar
Pierre Kraemer committed
158
	DartMarkerNoUnmark<MAP> markCC(m);
Thomas's avatar
Thomas committed
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

	// 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;
Pierre Kraemer's avatar
Pierre Kraemer committed
208
	DartMarkerNoUnmark<MAP> markCC(m);
Thomas's avatar
Thomas committed
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

	// 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
244
245
		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
246
247
248
249
250
251
252
253
254
255
256
257
		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>
Pierre Kraemer's avatar
Pierre Kraemer committed
258
Dart ParticleCell3D<PFP>::nextFaceNotMarked(Dart d, CellMarkerStore<MAP, FACE>& mark)
Thomas's avatar
Thomas committed
259
260
261
{
	// lock a marker
	Dart d1;
Pierre Kraemer's avatar
Pierre Kraemer committed
262
	DartMarkerNoUnmark<MAP> markCC(m);
Thomas's avatar
Thomas committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

	// 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))
		{
279
280
281
282
283
284
			for (std::list<Dart>::iterator it=darts_list.begin(); it!=darts_list.end(); ++it)
			{
				markCC.unmark(*it);
			}

			return d1;
Thomas's avatar
Thomas committed
285
286
287
288
289
290
		}

		// 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);
291

Thomas's avatar
Thomas committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
		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
309

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

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

319
//	return d1;
Thomas's avatar
Thomas committed
320
321
322
}

template <typename PFP>
pitiot's avatar
up  
pitiot committed
323
Geom::Orientation3D ParticleCell3D<PFP>::whichSideOfEdge(VEC3 c, Dart d) // orientation par rapport au plan orthogonal a la face passant par l'arete visée
Thomas's avatar
Thomas committed
324
325
326
327
{
	VEC3 p1 = position[m.phi1(d)];
	VEC3 p2 = position[d];

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

pitiot's avatar
up  
pitiot committed
332
    Geom::Plane3D<typename PFP::REAL> pl2(n2,p1*n2);
Thomas's avatar
Thomas committed
333
334
335
336
337

	return pl2.orient(c);
}

template <typename PFP>
pitiot's avatar
up  
pitiot committed
338
bool ParticleCell3D<PFP>::isOnHalfEdge(VEC3 c, Dart d) // booleen : vrai si on est sur l'arete mais pas sur le sommet
Thomas's avatar
Thomas committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
{
	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

361
362
	crossCell = CROSS_OTHER ;

Thomas's avatar
Thomas committed
363
364
	VEC3 som = position[d];

pitiot's avatar
up  
pitiot committed
365
366
367
368
369
370
    if(Geom::arePointsEquals(current, som)) {
#ifdef DEBUG
    std::cout << "points equal vrai :" << current << " || "<<this->m_position<<" || "<<d<<" || "<<position[d]<< std::endl;
#endif
//		this->m_position = som;
        this->m_positionFace = som;
371
		state = VERTEX;
Thomas's avatar
Thomas committed
372
373
374
375
376
		return;
	}

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
416
		wsof = whichSideOfFace(current,d);
Thomas's avatar
Thomas committed
417
418

		//if c is before the vertex on the edge, we have to change of umbrella, we are symetric to the good umbrella
Pierre Kraemer's avatar
Pierre Kraemer committed
419
420
		if(wsof != Geom::OVER)
		{
Thomas's avatar
Thomas committed
421
422
423
424
425
426
427
428
429
430
431
			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
Pierre Kraemer's avatar
Pierre Kraemer committed
432
433
		if(wsof == 1)
		{
Thomas's avatar
Thomas committed
434
435
436
437
			mark.mark(d);

			if(!mark.isMarked(m.alpha1(d)))
				d=m.alpha1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
438
439
			else
			{
Thomas's avatar
Thomas committed
440
441
442
443
444
445
				Dart dtmp=d;
				d = nextDartOfVertexNotMarked(d,mark);
				if(dtmp==d) {
					std::cout << "numerical rounding ?" << std::endl;

					d = dd;
446
447
					this->m_position = pointInFace(d);
					this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
448
449
450
451
452
					volumeState(current);
					return;
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
453
	} while(wsof == 1);
Thomas's avatar
Thomas committed
454

Pierre Kraemer's avatar
Pierre Kraemer committed
455
	if(wsof != 0)
Thomas's avatar
Thomas committed
456
	{
457
		this->m_position = pointInFace(d);
Thomas's avatar
Thomas committed
458
		d = nextNonPlanar(d);
459
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
460
461
462
463
		volumeState(current);
	}
	else
	{
464
		//Dart ddd=d;
Thomas's avatar
Thomas committed
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
	bool onEdge=false;
pitiot's avatar
up  
pitiot committed
480
481
482
483
	Dart dd=d;                                                              // dd = arete visée
	Geom::Orientation3D wsof = whichSideOfFace(current,m.alpha2(d));        // on toruve l'orientation apr rapport a la face adjacente dans le meme volume
	if(wsof!=Geom::UNDER) {                                                 // si on est dessus ou sur
		do {                                                                // on tourne tant qu'on est pas dessous ou sur
Thomas's avatar
Thomas committed
484
485
			d = m.alpha2(d);
			wsof = whichSideOfFace(current,m.alpha2(d));
pitiot's avatar
up  
pitiot committed
486
        }while(wsof!=Geom::OVER && dd!=d);
Thomas's avatar
Thomas committed
487

pitiot's avatar
up  
pitiot committed
488
        if(d==dd)                                                           // si on est de retour a la face de départ, c'est qu'on est sur l'arête
Thomas's avatar
Thomas committed
489
490
			onEdge = true;

pitiot's avatar
up  
pitiot committed
491
492
493
        if(wsof==Geom::ON) {                                                // si on est sur une face
            switch(whichSideOfEdge(current,d)) {                            // teste pour savoir de quel coté de l'arete visée on se trouve
            case Geom::ON :                                                 // si on est sur l'arête on le marque
Thomas's avatar
Thomas committed
494
495
				 onEdge=true;
					break;
pitiot's avatar
up  
pitiot committed
496
            case Geom::UNDER :                                              // si on est de l'autre coté , .... ?
Thomas's avatar
Thomas committed
497
498
499
500
501
502
503
					d = m.phi2(d);
					break;
			default :
				break;
			}
		}

pitiot's avatar
up  
pitiot committed
504
        wsof = whichSideOfFace(current,d);                                  // wsof = maintenant on compare avec l'autre face du camembert
Thomas's avatar
Thomas committed
505
	}
pitiot's avatar
up  
pitiot committed
506
    else {                                                                  // sinon on tourne dans l'autre sens
Thomas's avatar
Thomas committed
507
508
		wsof = whichSideOfFace(current,d);

509
		while(wsof==Geom::UNDER && dd != m.alpha_2(d)) {
Thomas's avatar
Thomas committed
510
511
512
513
			d = m.alpha_2(d);
			wsof = whichSideOfFace(current,d);
		}

pitiot's avatar
up  
pitiot committed
514
515
516
        if(whichSideOfEdge(current,d)==Geom::ON)                            // si on est sur l'arete on le note
        {
            onEdge=true;
Thomas's avatar
Thomas committed
517
518
519
		}
	}

pitiot's avatar
up  
pitiot committed
520
    if(wsof==Geom::OVER)  { // on a trouvé le camembert et on est dans le volume
Thomas's avatar
Thomas committed
521

522
		this->m_position = pointInFace(d);
Thomas's avatar
Thomas committed
523
		d = nextNonPlanar(m.phi1(d));
524
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
525
526
527
528
		volumeState(current);
		return;
	}
	else {
pitiot's avatar
up  
pitiot committed
529
530
531
532
533
534
		if(onEdge) { // on est sur l'arete visée
			if(isOnHalfEdge(current,d)) //
            {
                if (isOnHalfEdge(current,m.phi3(d))) { // sur l'arete
					state=EDGE;
                    this->m_position = this->m_positionFace = current;
Thomas's avatar
Thomas committed
535
				}
pitiot's avatar
up  
pitiot committed
536
537
                else {  // sur le sommet de l'arete
                    this->m_position = position[2];
Thomas's avatar
Thomas committed
538
539
					vertexState(current);
				}
pitiot's avatar
up  
pitiot committed
540
541
542
            }
            else {  // sur l'autre sommet de l'arete
                d=m.phi1(d);
Thomas's avatar
Thomas committed
543
544
545
				vertexState(current);
			}
		}
pitiot's avatar
up  
pitiot committed
546
		else { // on est sur la face
547
			this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
548
549
550
551
552
553
554
555
556
557
558
559
560
			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

561
	crossCell = CROSS_FACE ;
Thomas's avatar
Thomas committed
562
563
564
565
566
567

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

	if (wsof==Geom::OVER) {
		d = m.phi3(d);
pitiot's avatar
ok  
pitiot committed
568
569
570
        d = nextNonPlanar(d);
        this->m_positionFace = pointInFace(d);
        volumeState(current);
Thomas's avatar
Thomas committed
571
572
573
		return;
	}
	else if(wsof==Geom::UNDER) {
pitiot's avatar
ok  
pitiot committed
574
575
        d = nextNonPlanar(d);
        this->m_positionFace = pointInFace(d);
pitiot's avatar
change  
pitiot committed
576
577
578
579
580
581
582
583
584
585
        this->compteur_anti_infinite++;
        if(this->compteur_anti_infinite < 300)
        {
            volumeState(current);
        }
        else
        {
            CGoGNout<< "infinite loop detected" << CGoGNendl;
        }
        return;
Thomas's avatar
Thomas committed
586
587
	}

588
	VEC3 norm = Algo::Surface::Geometry::faceNormal<PFP>(m,d,position);
Thomas's avatar
Thomas committed
589
590

	Dart dd=d;
591
	if(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER) {
Thomas's avatar
Thomas committed
592
		d = m.phi_1(d);
593
		while(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER && dd!=d)
Thomas's avatar
Thomas committed
594
595
596
597
598
599
600
601
			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;
602
				case Geom::ON :this->m_position = current;
603
					state = EDGE;
Thomas's avatar
Thomas committed
604
605
					return;
				default :
606
					Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
607
608
					VEC3 p3 = pl.normal()+this->m_position;
					Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
609
610
					VEC3 e(position[m.phi1(d)]-position[d]);

611
					Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
612
613
614
615
616
617

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

618
			this->m_position = this->m_positionFace = current;
619
			state = FACE;
Thomas's avatar
Thomas committed
620
621
622
623
			return;
		}
	}
	else {
624
		while(isRightDFace(current,d,this->m_positionFace,norm) && m.phi1(d)!=dd)
Thomas's avatar
Thomas committed
625
626
627
628
629
630
631
632
633
634
			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;
635
				case Geom::ON :this->m_position = current;
636
					state = EDGE;
Thomas's avatar
Thomas committed
637
638
					return;
				default :
639
					 Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
640
641
					 VEC3 p3 = pl.normal()+this->m_position;
					 Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
642
643
					 VEC3 e(position[m.phi1(d)]-position[d]);

644
					 Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
645
646
647
648
649
					edgeState(current);
					return;
				}
			}while(d!=dd);

650
			this->m_position = this->m_positionFace = current;
651
			state = FACE;
Thomas's avatar
Thomas committed
652
653
654
655
656
657
658
			return;
		}
	}

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

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

674
		 this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
675
676
677
678
679
680
681
682
683
684
685
686

		 edgeState(current);
	}
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
687
	CellMarkerStore<MAP, FACE> mark(m);
Thomas's avatar
Thomas committed
688
689
690
691
692
693
694
695
696
	bool above;

	Geom::Orientation3D testRight=Geom::OVER;

	do {
		above=false;

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

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

Thomas's avatar
Thomas committed
701
702
			d = m.phi_1(d);

703
			while(dd!=d && isLeftL1DVol(current,d,this->m_positionFace,this->m_position)!=Geom::UNDER)
Thomas's avatar
Thomas committed
704
705
706
707
708
709
710
				d = m.phi_1(d);

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

711
			testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
712
713
714
715


			while(testRight!=Geom::OVER && dd!=m.phi1(d)) {
				d = m.phi1(d);
716
				testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
717
718
719
720
721
722
			}

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

723
		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
724
725
726
727
728
729
				  //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();
730
				this->m_position = current;
Thomas's avatar
Thomas committed
731
732
733
734
735
736
737
				return;
			}

			volumeSpecialCase(current);
			return;
		}

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

740
		if(testAbove!=Geom::UNDER || (testRight==Geom::ON && isAbove(current,m.phi_1(d),this->m_position)!=Geom::UNDER)) {
Thomas's avatar
Thomas committed
741
742
743
744
745
746
747
748
749
750
751

			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
752
					mark.mark(d);
Thomas's avatar
Thomas committed
753
754
755
756
757
758
759

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

760
				this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
761
762
763
764
765
766
767
			}
		}
	} while(above);

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

	if(wsof==Geom::UNDER) {
768
		this->m_position = current;
769
		state = VOLUME;
Thomas's avatar
Thomas committed
770
771
	}
	else if(wsof==Geom::ON) {
772
773
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
			this->m_position = this->m_positionFace = current;
774
			state = FACE;
Thomas's avatar
Thomas committed
775
776
		}
		else {
777
			this->m_position = this->m_positionFace = current;
Thomas's avatar
Thomas committed
778
779
780
781
			edgeState(current);
		}
	}
	else {
782
783
784
		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);
pitiot's avatar
change  
pitiot committed
785
786
787
788
789
790
791
792
793
794
            this->compteur_anti_infinite++;
            if(this->compteur_anti_infinite < 300)
            {
                faceState(current,wsof);
            }
            else
            {
                CGoGNout<< "infinite loop detected" << CGoGNendl;
            }

Thomas's avatar
Thomas committed
795
796
		}
		else {
797
798
//			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);
pitiot's avatar
pitiot committed
799
800

            edgeState(current);
Thomas's avatar
Thomas committed
801
802
803
804
805
806
807
808
809
810
811
812
		}
	}
}

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

	Dart dd;
Pierre Kraemer's avatar
Pierre Kraemer committed
813
	CellMarkerStore<MAP, FACE> mark(m);
Thomas's avatar
Thomas committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834

	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;

835
				if(isAbove(current,d,this->m_position)==Geom::UNDER)
Thomas's avatar
Thomas committed
836
				{
837
838
//					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
839
840
841
842
					faceState(current,Geom::OVER);
				}
				else
				{
843
844
//					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
845
846
847
848
849
850
851
					edgeState(current);
				}

				return;
			}
			else if(wsof==Geom::ON)
			{
852
				this->m_position = current;
Thomas's avatar
Thomas committed
853
854
855
856
857
858
				d = *face;

				faceState(current);
				return;
			}

859
			Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,*face,position);
860
			if(pl.normal()*VEC3(current-this->m_position)>0)
Thomas's avatar
Thomas committed
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
			{
				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];
			}
		}
	}

890
	this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
891
892
893
894

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

	if(wsof==Geom::UNDER) {
895
		this->m_position = current;
896
		state = VOLUME;
Thomas's avatar
Thomas committed
897
898
	}
	else if(wsof==Geom::ON) {
899
900
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
			this->m_position = current;
901
			state = FACE;
Thomas's avatar
Thomas committed
902
903
		}
		else {
904
			this->m_position = current;
905
			state = EDGE;
Thomas's avatar
Thomas committed
906
907
908
		}
	}
	else {
909
		if(isAbove(current,d,this->m_position)==Geom::UNDER)
Thomas's avatar
Thomas committed
910
		{
911
912
//			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
913
914
915
			faceState(current,wsof);
		}
		else {
916
917
//			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
918
919
920
921
			edgeState(current);
		}
	}
}
922

Pierre Kraemer's avatar
Pierre Kraemer committed
923
924
925
926
927
} // namespace MovingObjects

} // namespace Surface

} // namespace Algo
928

Pierre Kraemer's avatar
Pierre Kraemer committed
929
} // namespace CGoGN