particle_cell_3D.hpp 21.8 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 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
	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>
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 323 324 325 326 327
}

template <typename PFP>
Geom::Orientation3D ParticleCell3D<PFP>::whichSideOfEdge(VEC3 c, Dart d)
{
	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 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
	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

361 362
	crossCell = CROSS_OTHER ;

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

365 366
	if(Geom::arePointsEquals(current, this->m_position)) {
		this->m_position = this->m_positionFace = som;
367
		state = VERTEX;
Thomas's avatar
Thomas committed
368 369 370 371 372
		return;
	}

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

	do {
376
		VEC3 dualsp = (som+ Algo::Surface::Geometry::vertexNormal<PFP>(m,d,position));
Thomas's avatar
Thomas committed
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
		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
412
		wsof = whichSideOfFace(current,d);
Thomas's avatar
Thomas committed
413 414

		//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
415 416
		if(wsof != Geom::OVER)
		{
Thomas's avatar
Thomas committed
417 418 419 420 421 422 423 424 425 426 427
			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
428 429
		if(wsof == 1)
		{
Thomas's avatar
Thomas committed
430 431 432 433
			mark.mark(d);

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

					d = dd;
442 443
					this->m_position = pointInFace(d);
					this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
444 445 446 447 448
					volumeState(current);
					return;
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
449
	} while(wsof == 1);
Thomas's avatar
Thomas committed
450

Pierre Kraemer's avatar
Pierre Kraemer committed
451
	if(wsof != 0)
Thomas's avatar
Thomas committed
452
	{
453
		this->m_position = pointInFace(d);
Thomas's avatar
Thomas committed
454
		d = nextNonPlanar(d);
455
		this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
456 457 458 459
		volumeState(current);
	}
	else
	{
460
		//Dart ddd=d;
Thomas's avatar
Thomas committed
461 462 463 464 465 466 467 468
		edgeState(current);
	}
}

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

	#endif

473 474
	crossCell = CROSS_OTHER ;

Thomas's avatar
Thomas committed
475 476 477 478 479 480 481 482 483 484 485 486
	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;

487
		if(wsof==Geom::ON) {
Thomas's avatar
Thomas committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
			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);

505
		while(wsof==Geom::UNDER && dd != m.alpha_2(d)) {
Thomas's avatar
Thomas committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519
			d = m.alpha_2(d);
			wsof = whichSideOfFace(current,d);
		}

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

	if(wsof==-1)  {

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

556
	crossCell = CROSS_FACE ;
Thomas's avatar
Thomas committed
557 558 559 560 561 562 563

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

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

575
	VEC3 norm = Algo::Surface::Geometry::faceNormal<PFP>(m,d,position);
Thomas's avatar
Thomas committed
576 577

	Dart dd=d;
578
	if(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER) {
Thomas's avatar
Thomas committed
579
		d = m.phi_1(d);
580
		while(isLeftL1DFace(current,d,this->m_positionFace,norm)!=Geom::UNDER && dd!=d)
Thomas's avatar
Thomas committed
581 582 583 584 585 586 587 588
			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;
589
				case Geom::ON :this->m_position = current;
590
					state = EDGE;
Thomas's avatar
Thomas committed
591 592
					return;
				default :
593
					Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
594 595
					VEC3 p3 = pl.normal()+this->m_position;
					Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
596 597
					VEC3 e(position[m.phi1(d)]-position[d]);

598
					Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
599 600 601 602 603 604

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

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

631
					 Geom::intersectionPlaneRay(plOrtho,this->m_position,current-this->m_position,this->m_position);
Thomas's avatar
Thomas committed
632 633 634 635 636
					edgeState(current);
					return;
				}
			}while(d!=dd);

637
			this->m_position = this->m_positionFace = current;
638
			state = FACE;
Thomas's avatar
Thomas committed
639 640 641 642 643 644 645
			return;
		}
	}

	switch (whichSideOfEdge(current,d))
	{
	case Geom::OVER :
646
		 this->m_position = this->m_positionFace = current;
647
		 state = FACE;
Thomas's avatar
Thomas committed
648 649
		 break;
	case Geom::ON :
650
		 this->m_position = this->m_positionFace = current;
651
		 state = EDGE;
Thomas's avatar
Thomas committed
652 653
		 break;
	default :
654
		 Geom::Plane3D<typename PFP::REAL> pl = Algo::Surface::Geometry::facePlane<PFP>(m,d,position);
655 656
		 VEC3 p3 = pl.normal()+this->m_position;
		 Geom::Plane3D<typename PFP::REAL> plOrtho(this->m_position,current,p3);
Thomas's avatar
Thomas committed
657 658
		 VEC3 e(position[m.phi1(d)]-position[d]);

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

661
		 this->m_positionFace = this->m_position;
Thomas's avatar
Thomas committed
662 663 664 665 666 667 668 669 670 671 672 673

		 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
674
	CellMarkerStore<MAP, FACE> mark(m);
Thomas's avatar
Thomas committed
675 676 677 678 679 680 681 682 683
	bool above;

	Geom::Orientation3D testRight=Geom::OVER;

	do {
		above=false;

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

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

Thomas's avatar
Thomas committed
688 689
			d = m.phi_1(d);

690
			while(dd!=d && isLeftL1DVol(current,d,this->m_positionFace,this->m_position)!=Geom::UNDER)
Thomas's avatar
Thomas committed
691 692 693 694 695 696 697
				d = m.phi_1(d);

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

698
			testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
699 700 701 702


			while(testRight!=Geom::OVER && dd!=m.phi1(d)) {
				d = m.phi1(d);
703
				testRight = isRightDVol(current,d,this->m_positionFace,this->m_position);
Thomas's avatar
Thomas committed
704 705 706 707 708 709
			}

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

710
		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
711 712 713 714 715 716
				  //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();
717
				this->m_position = current;
Thomas's avatar
Thomas committed
718 719 720 721 722 723 724
				return;
			}

			volumeSpecialCase(current);
			return;
		}

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

727
		if(testAbove!=Geom::UNDER || (testRight==Geom::ON && isAbove(current,m.phi_1(d),this->m_position)!=Geom::UNDER)) {
Thomas's avatar
Thomas committed
728 729 730 731 732 733 734 735 736 737 738

			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
739
					mark.mark(d);
Thomas's avatar
Thomas committed
740 741 742 743 744 745 746

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

747
				this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
748 749 750 751 752 753 754
			}
		}
	} while(above);

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

	if(wsof==Geom::UNDER) {
755
		this->m_position = current;
756
		state = VOLUME;
Thomas's avatar
Thomas committed
757 758
	}
	else if(wsof==Geom::ON) {
759 760
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
			this->m_position = this->m_positionFace = current;
761
			state = FACE;
Thomas's avatar
Thomas committed
762 763
		}
		else {
764
			this->m_position = this->m_positionFace = current;
Thomas's avatar
Thomas committed
765 766 767 768
			edgeState(current);
		}
	}
	else {
769 770 771
		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
772 773 774
			faceState(current,wsof);
		}
		else {
775 776
//			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
777 778 779 780 781 782 783 784 785 786 787 788 789
			edgeState(current);
		}
	}
}

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
790
	CellMarkerStore<MAP, FACE> mark(m);
Thomas's avatar
Thomas committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

	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;

812
				if(isAbove(current,d,this->m_position)==Geom::UNDER)
Thomas's avatar
Thomas committed
813
				{
814 815
//					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
816 817 818 819
					faceState(current,Geom::OVER);
				}
				else
				{
820 821
//					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
822 823 824 825 826 827 828
					edgeState(current);
				}

				return;
			}
			else if(wsof==Geom::ON)
			{
829
				this->m_position = current;
Thomas's avatar
Thomas committed
830 831 832 833 834 835
				d = *face;

				faceState(current);
				return;
			}

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

867
	this->m_positionFace = pointInFace(d);
Thomas's avatar
Thomas committed
868 869 870 871

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

	if(wsof==Geom::UNDER) {
872
		this->m_position = current;
873
		state = VOLUME;
Thomas's avatar
Thomas committed
874 875
	}
	else if(wsof==Geom::ON) {
876 877
		if(isAbove(current,d,this->m_position)==Geom::UNDER) {
			this->m_position = current;
878
			state = FACE;
Thomas's avatar
Thomas committed
879 880
		}
		else {
881
			this->m_position = current;
882
			state = EDGE;
Thomas's avatar
Thomas committed
883 884 885
		}
	}
	else {
886
		if(isAbove(current,d,this->m_position)==Geom::UNDER)
Thomas's avatar
Thomas committed
887
		{
888 889
//			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
890 891 892
			faceState(current,wsof);
		}
		else {
893 894
//			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
895 896 897 898
			edgeState(current);
		}
	}
}
899

Pierre Kraemer's avatar
Pierre Kraemer committed
900 901 902 903 904
} // namespace MovingObjects

} // namespace Surface

} // namespace Algo
905

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