env_map.cpp 16.3 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4
#include "env_map.h"
#include "utils.h"
#include "agent.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
5 6
#include "Geometry/inclusion.h"
#include "Algo/MovingObjects/particle_cell_2D_memo.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
7 8
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Geometry/normal.h"
Thomas's avatar
Thomas committed
9 10
#include "Algo/Import/importSvg.h"
#include "Algo/BooleanOperator/mergeVertices.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
11 12 13

using namespace CGoGN;

Pierre Kraemer's avatar
Pierre Kraemer committed
14
EnvMap::EnvMap() :
Pierre Kraemer's avatar
Pierre Kraemer committed
15 16 17 18
	obstacleMark(map, EDGE),
	buildingMark(map, FACE),
	refineMark(map, FACE),
	coarsenMark(map, FACE)
Pierre Kraemer's avatar
Pierre Kraemer committed
19
{
Pierre Kraemer's avatar
Pierre Kraemer committed
20 21 22 23 24 25
	position = map.addAttribute<PFP::VEC3>(VERTEX, "position");
	normal = map.addAttribute<PFP::VEC3>(VERTEX, "normal");
	agentvect = map.addAttribute<PFP::AGENTVECT>(FACE, "agents");
	neighborAgentvect = map.addAttribute<PFP::AGENTVECT>(FACE, "neighborAgents");
	obstvect = map.addAttribute<PFP::OBSTACLEVECT>(FACE, "obstacles");
	subdivisableFace = map.addAttribute<PFP::BOOLATTRIB>(FACE, "subdivisableFace");
Thomas's avatar
Thomas committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

	refineCandidate.reserve(100);
	coarsenCandidate.reserve(100);

//	std::string filename = "/home/jund/Desktop/drawingQuads.svg";
//	std::string filename = "/home/jund/Desktop/drawingTest.svg";
//	std::string filename = "/home/jund/Desktop/drawingSimple.svg";
//	std::string filename = "/home/jund/Desktop/drawingLines.svg";
//	std::string filename = "/home/jund/Desktop/mapKrutSimple.svg";
//	std::string filename = "/home/jund/Desktop/mapKrut.svg";
//	Algo::Import::importSVG<PFP>(map,filename,position, obstacleMark, buildingMark);
//	scale(3.2808399f);

//	Algo::BooleanOperator::mergeVertices<PFP>(map,position);
//	DartMarker amelk(map);
//	map.closeMap(amelk);
//	Algo::Modelisation::CatmullClarkSubdivision<PFP>(map,position);
//	Algo::Modelisation::computeDual<PFP>(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46 47
}

unsigned int EnvMap::mapMemoryCost()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51
	return (map.getAttributeContainer(DART)).memorySize()
			+ (map.getAttributeContainer(VERTEX)).memorySize()
			+ (map.getAttributeContainer(EDGE)).memorySize()
			+ (map.getAttributeContainer(FACE)).memorySize();
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
}

Thomas's avatar
Thomas committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
void EnvMap::scale(float scaleVal)
{
	Geom::BoundingBox<PFP::VEC3> bb(position.begin());

	for(unsigned int v = position.begin() ; v != position.end() ; position.next(v))
	{
		bb.addPoint(position[v]);
	}

	VEC3 center = bb.center();
	for(unsigned int v = position.begin() ; v != position.end() ; position.next(v))
	{
		position[v] -= center;
		position[v] *= scaleVal;
	}
}

void EnvMap::subdivideAllToMaxLevel()
{
	bool subdiv;
	do
	{
		subdiv=false;
		{
			CellMarker subd(map,FACE);
			for(Dart d = map.begin(); d!= map.end(); map.next(d))
			{
				if(!subd.isMarked(d))
				{
					subd.mark(d);
					if(!buildingMark.isMarked(d))
					{
						//check if subdivision is authorized
						float minDistSq = Agent::neighborDistSq_ ;
						bool subdivisable = true;
						Dart old = map.faceOldestDart(d) ;
						unsigned int fLevel = map.faceLevel(old) ;
						map.setCurrentLevel(fLevel) ;
						PFP::VEC3 fCenter = Algo::Geometry::faceCentroid<PFP>(map, old, position) ;
						Dart fd = old ;
						do
						{
							PFP::VEC3& p = position[fd] ;
							PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(map, fd, position) ;
							PFP::VEC3 proj = fCenter - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ;
							if(proj.norm2() < minDistSq)
							{
								subdivisable = false ;
								break ;
							}
							fd = map.phi1(fd) ;
						} while(fd != old) ;

						if(subdivisable)
						{
							map.setCurrentLevel(fLevel) ;
							Algo::IHM::subdivideFace<PFP>(map, old, position) ;
							subdiv = true;
						}
						map.setCurrentLevel(map.getMaxLevel()) ;
					}
				}
			}
		}
	} while(subdiv);
}

void EnvMap::subdivideToProperLevel()
{
	bool subdiv;
	do
	{
		subdiv=false;
		{
			CellMarker subd(map,FACE);
			for(Dart d = map.begin(); d!= map.end(); map.next(d))
			{
				if(!subd.isMarked(d))
				{
					subd.mark(d);
					if(!refineMark.isMarked(d) && agentvect[d].size() > nbAgentsToSubdivide)
					{
						std::pair<bool,bool>& sf = subdivisableFace[d];
						if(sf.first == false || (sf.first == true && sf.second))
						{
							subdiv=true;
							refineMark.mark(d);
							refineCandidate.push_back(d);
						}
					}
				}
			}
			subd.unmarkAll();
		}
		updateMap();
		refineCandidate.clear();
		map.setCurrentLevel(map.getMaxLevel());
	} while(subdiv);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
154 155
Dart EnvMap::getBelongingCell(const PFP::VEC3& pos)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	CellMarkerStore m(map, FACE);
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160
		if(!m.isMarked(d))
		{
161 162 163 164
			m.mark(d);
			if(!buildingMark.isMarked(d) && Algo::Geometry::isPointInConvexFace2D<PFP>(map, d, position, pos, true))
				return d;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169 170 171 172 173
	}

	std::cout << "ERROR : pos not in map" << std::endl;
	return map.begin();
}

void EnvMap::init()
{
	float sideSize = 70.0f;
Thomas's avatar
Thomas committed
174 175 176 177 178 179
	unsigned int nbSquares = 5;
//	float sideSize = 420.0f;
//	unsigned int nbSquares = 4;

//	CityGenerator::generateGrid<PFP>(map, position,nbSquares,nbSquares, sideSize, obstacleMark,buildingMark);
//	CityGenerator::generateCorridor<PFP>(map, position, obstacleMark, buildingMark, 50, nbSquares);
Pierre Kraemer's avatar
Pierre Kraemer committed
180
	CityGenerator::generateCity<PFP>(map, position, obstacleMark, buildingMark, sideSize, nbSquares);
181
//	CityGenerator::generateMall<PFP>(map, position, obstacleMark, buildingMark, sideSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
182
//	CityGenerator::simplifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
Thomas's avatar
Thomas committed
183 184
//	CityGenerator::convexifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
//	CityGenerator::installGuardRail<PFP>(map, position, obstacleMark, buildingMark, 5.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186
	map.init();
	registerObstaclesInFaces();
Thomas's avatar
Thomas committed
187 188 189

//	subdivideAllToMaxLevel();

190 191
	for(unsigned int i = subdivisableFace.begin(); i < subdivisableFace.end(); subdivisableFace.next(i))
		subdivisableFace[i].first = false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
192 193
}

Pierre Kraemer's avatar
Pierre Kraemer committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
void EnvMap::foreach_neighborFace(Dart d, FunctorType& f)
{
	Dart dd = d;
	do
	{
		Dart ddd = map.alpha1(map.alpha1(dd));
		while(ddd != dd)
		{
			f(ddd);
			ddd = map.alpha1(ddd);
		}
		dd = map.phi1(dd);
	} while(dd != d);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
209 210
void EnvMap::registerObstaclesInFaces()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
211
	CellMarker m(map, FACE);
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213 214 215 216
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!m.isMarked(d))
		{
			m.mark(d);
217
			if(!buildingMark.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
218
			{
219 220 221 222 223 224 225
				Dart dd = d;
				do
				{
					if(obstacleMark.isMarked(dd))
						obstvect[d].push_back(map.phi2(dd));
					dd = map.phi1(dd);
				} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
226

227
				do
Pierre Kraemer's avatar
Pierre Kraemer committed
228
				{
229 230 231 232 233 234 235 236 237 238 239 240
					Dart ddd = map.alpha1(map.alpha1(dd));
					while(ddd != dd)
					{
						if(!buildingMark.isMarked(ddd))
							addNeighborObstacles(obstvect[d], ddd, ddd == map.alpha_1(dd));
						ddd = map.alpha1(ddd);
					}
					dd = map.phi1(dd);
				} while(dd != d);
			}
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

void EnvMap::addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbor)
{
	Dart stop;
	if(edgeNeighbor)
		stop = map.phi_1(map.phi_1(map.phi_1(d)));
	else
		stop = map.phi_1(map.phi_1(d));
	
	Dart dd = d;
	do
	{
		if(obstacleMark.isMarked(dd))
		{
			if(buildingMark.isMarked(dd))
				obst.push_back(dd);
			else
				obst.push_back(map.phi2(dd));
		}
		dd = map.phi1(dd);
	} while(dd != d);
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
//void EnvMap::agentChangeFaceThroughEdge(Agent* agent)
//{
//	Dart oldFace = agent->part_.lastCrossed;
//	Dart newFace = agent->part_.d;
//
//	agentvect[newFace].push_back(agent);
//
//	PFP::AGENTS::iterator end = agentvect[oldFace].end();
//	for(PFP::AGENTS::iterator it = agentvect[oldFace].begin(); it != end; ++it)
//	{
//		if(*it == agent)
//		{
//			*it = agentvect[oldFace].back();
//			agentvect[oldFace].pop_back();
//			break;
//		}
//	}
//
//	// mark adjacent cells shared by newFace and oldFace
Pierre Kraemer's avatar
Pierre Kraemer committed
284
//	CellMarkerNoUnmark f(map, FACE);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
//	Dart d = oldFace;
//	do
//	{
//		f.mark(d);
//		d = map.alpha1(d);
//	} while(d != oldFace);
//
//	d = map.phi1(oldFace);
//	do
//	{
//		f.mark(d);
//		d = map.alpha1(d);
//	} while(d != map.phi1(oldFace));
//
//	// remove agent from cells adjacent to oldFace but not to newFace
//	Dart dd = oldFace;
//	do
//	{
//		Dart ddd = map.alpha1(map.alpha1(dd));
//		while(ddd != dd)
//		{
//			if(!f.isMarked(ddd))
//			{
//				bool found = false;
//				PFP::AGENTS::iterator end = neighborAgentvect[ddd].end();
//				for(PFP::AGENTS::iterator it = neighborAgentvect[ddd].begin(); it != end && !found; ++it)
//				{
//					if(*it == agent)
//					{
//						*it = neighborAgentvect[ddd].back();
//						neighborAgentvect[ddd].pop_back();
//						found = true;
//					}
//				}
//			}
//			ddd = map.alpha1(ddd);
//		}
//		dd = map.phi1(dd);
//	} while(dd != oldFace);
//
//	// add agent from cells adjacent to oldFace but not to newFace
//	// and unmark shared cells
//	dd = newFace;
//	do
//	{
//		Dart ddd = map.alpha1(map.alpha1(dd));
//		while(ddd != dd)
//		{
//			if(!f.isMarked(ddd))
//				neighborAgentvect[ddd].push_back(agent);
//			else
//				f.unmark(ddd);
//			ddd = map.alpha1(ddd);
//		}
//		dd = map.phi1(dd);
//	} while(dd != newFace);
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
342

Pierre Kraemer's avatar
Pierre Kraemer committed
343 344
void EnvMap::agentChangeFace(Agent* agent, Dart oldFace)
{
345 346
	Dart newFace = agent->part_.d;

Pierre Kraemer's avatar
Pierre Kraemer committed
347
	popAgentInCells(agent, oldFace);
348 349
	pushAgentInCells(agent, newFace);

Pierre Kraemer's avatar
Pierre Kraemer committed
350
	if(!refineMark.isMarked(newFace) && agentvect[newFace].size() > nbAgentsToSubdivide)
351 352 353 354 355 356 357 358
	{
		std::pair<bool,bool>& sf = subdivisableFace[newFace];
		if(sf.first == false || (sf.first == true && sf.second))
		{
			refineMark.mark(newFace);
			refineCandidate.push_back(newFace);
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
359

Pierre Kraemer's avatar
Pierre Kraemer committed
360
	if(!coarsenMark.isMarked(oldFace) && !refineMark.isMarked(oldFace) && agentvect[oldFace].size() < nbAgentsToSimplify / 4)
Pierre Kraemer's avatar
Pierre Kraemer committed
361 362 363 364
	{
		coarsenMark.mark(oldFace);
		coarsenCandidate.push_back(map.faceOldestDart(oldFace));
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
365 366 367 368 369 370
}

void EnvMap::updateMap()
{
	assert(map.getCurrentLevel() == map.getMaxLevel()) ;

371
	for(std::vector<Dart>::iterator it = refineCandidate.begin(); it != refineCandidate.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
372
	{
373 374
		Dart d = (*it) ;
		refineMark.unmark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
375

Pierre Kraemer's avatar
Pierre Kraemer committed
376
		if(agentvect[d].size() > nbAgentsToSubdivide)
Pierre Kraemer's avatar
Pierre Kraemer committed
377
		{
Thomas's avatar
Thomas committed
378
			int fLevel = -1 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380
			Dart old = map.faceOldestDart(d) ;

381
			bool subdivisable = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
382

Pierre Kraemer's avatar
Pierre Kraemer committed
383
			std::pair<bool,bool>& sf = subdivisableFace[old] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
384 385
			if(sf.first == true)
				subdivisable = sf.second ;
386
			else
Pierre Kraemer's avatar
Pierre Kraemer committed
387
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
388
				float minDistSq = Agent::neighborDistSq_ ; // diametre de vision de l'agent au carré
389

Pierre Kraemer's avatar
Pierre Kraemer committed
390
				fLevel = map.faceLevel(old) ;
391 392 393
				map.setCurrentLevel(fLevel) ;
				PFP::VEC3 fCenter = Algo::Geometry::faceCentroid<PFP>(map, old, position) ;
				Dart fd = old ;
Pierre Kraemer's avatar
Pierre Kraemer committed
394 395
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
396
					PFP::VEC3& p = position[fd] ;
397
					PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(map, fd, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
398
					PFP::VEC3 proj = fCenter - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
399
					if(proj.norm2() < minDistSq)
Pierre Kraemer's avatar
Pierre Kraemer committed
400
					{
401 402
						subdivisable = false ;
						break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
403
					}
404 405 406 407
					fd = map.phi1(fd) ;
				} while(fd != old) ;
				map.setCurrentLevel(map.getMaxLevel()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
408 409
				sf.first = true ;
				sf.second = subdivisable ;
410 411 412 413
			}

			if(subdivisable)
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
414
				if(fLevel == -1)
Pierre Kraemer's avatar
Pierre Kraemer committed
415
					fLevel = map.faceLevel(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417

				sf.first = false ;
418 419 420 421 422 423

				PFP::AGENTS oldAgents(agentvect[old]);
				for(PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait)
					popAgentInCells(*ait, old);

				neighborAgentvect[old].clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
424 425

				map.setCurrentLevel(fLevel) ;
426
				Algo::IHM::subdivideFace<PFP>(map, old, position) ;
427 428 429 430 431 432 433 434 435 436 437 438 439 440
				CellMarkerStore newF(map, FACE) ;
				unsigned int degree = 0 ;
				Dart dd = old ;
				do
				{
					++degree ;
					newF.mark(dd);
					dd = map.phi1(dd) ;
				} while(dd != old) ;
				if(degree == 3)
				{
					Dart centerFace = map.phi2(map.phi1(old)) ;
					newF.mark(centerFace);
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
441
				map.setCurrentLevel(map.getMaxLevel()) ;
442

443 444 445 446 447
				for(PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait)
				{
					resetAgentInFace(*ait) ;
					pushAgentInCells(*ait, (*ait)->part_.d) ;
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
448

449
				dd = old ;
Pierre Kraemer's avatar
Pierre Kraemer committed
450 451
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
452
					Dart d3 = dd ;
453
					do
Pierre Kraemer's avatar
Pierre Kraemer committed
454
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
455 456
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
457 458 459
						while(d4 != d3)
						{
							if(!newF.isMarked(d4))
Pierre Kraemer's avatar
Pierre Kraemer committed
460 461 462 463 464
							{
								PFP::AGENTS& ad4 = agentvect[d4] ;
								nad3.insert(nad3.end(), ad4.begin(), ad4.end()) ;
							}
							d4 = map.alpha1(d4) ;
465
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
466 467
						d3 = map.phi1(d3) ;
					} while(d3 != dd) ;
468 469

					map.setCurrentLevel(fLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
470
					dd = map.phi1(dd) ;
471 472 473 474 475
					map.setCurrentLevel(map.getMaxLevel()) ;
				} while(dd != old);

				if(degree == 3)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
476 477
					Dart centerFace = map.phi2(map.phi1(old)) ;
					Dart d3 = centerFace ;
478 479
					do
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
480 481
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
482 483 484
						while(d4 != d3)
						{
							if(!newF.isMarked(d4))
Pierre Kraemer's avatar
Pierre Kraemer committed
485 486 487 488 489
							{
								PFP::AGENTS& ad4 = agentvect[d4] ;
								nad3.insert(neighborAgentvect[d3].end(), ad4.begin(), ad4.end()) ;
							}
							d4 = map.alpha1(d4) ;
490
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
491 492
						d3 = map.phi1(d3) ;
					} while(d3 != centerFace) ;
493
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
494 495 496
			}
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
497

Thomas's avatar
Thomas committed
498
	for(unsigned int it = 0; it < coarsenCandidate.size(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
499
	{
Thomas's avatar
Thomas committed
500
		Dart old = coarsenCandidate[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
501
		coarsenMark.unmark(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
502

Pierre Kraemer's avatar
Pierre Kraemer committed
503
		unsigned int fLevel = map.faceLevel(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
504 505 506 507 508 509 510 511 512 513 514 515 516

		if(fLevel > 0 && map.getDartLevel(old) < fLevel)
		{
			unsigned int cur = map.getCurrentLevel() ;
			map.setCurrentLevel(fLevel - 1) ;

			if(map.faceIsSubdividedOnce(old))
			{
				unsigned int degree = 0 ;
				unsigned int nbAgents = 0 ;
				Dart fit = old ;
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
517
					nbAgents += agentvect[fit].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
					++degree ;
					fit = map.phi1(fit) ;
				} while(fit != old) ;
				if(degree == 3)
				{
					map.setCurrentLevel(fLevel) ;
					Dart centerFace = map.phi2(map.phi1(old)) ;
					nbAgents += agentvect[centerFace].size() ;
					map.setCurrentLevel(fLevel - 1) ;
				}

				if(nbAgents < nbAgentsToSimplify)
				{
					PFP::AGENTS agents ;
					Dart fit = old ;
					do
					{
						map.setCurrentLevel(fLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
536
						coarsenMark.unmark(fit) ;
Thomas's avatar
Thomas committed
537
						unsigned int start = it + 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
538
						unsigned int fEmb = map.getEmbedding(FACE, fit) ;
Thomas's avatar
Thomas committed
539
						while(start < coarsenCandidate.size())
Pierre Kraemer's avatar
Pierre Kraemer committed
540
						{
Thomas's avatar
Thomas committed
541
							if(map.getEmbedding(FACE, coarsenCandidate[start]) == fEmb)
Pierre Kraemer's avatar
Pierre Kraemer committed
542
							{
Thomas's avatar
Thomas committed
543
								coarsenCandidate[start] = coarsenCandidate.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
544
								coarsenCandidate.pop_back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
545
							}
Pierre Kraemer's avatar
Pierre Kraemer committed
546 547
							else
								++start ;
Pierre Kraemer's avatar
Pierre Kraemer committed
548
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
549 550 551
						PFP::AGENTS a(agentvect[fit]) ;
						agents.insert(agents.end(), a.begin(), a.end()) ;
						map.setCurrentLevel(map.getMaxLevel()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
552 553
						for(PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait)
							popAgentInCells(*ait, fit);
Pierre Kraemer's avatar
Pierre Kraemer committed
554 555 556 557 558 559 560 561
						map.setCurrentLevel(fLevel - 1) ;
						Dart nf = map.phi2(fit) ;
						if(!map.faceIsSubdivided(nf))
						{
							map.setCurrentLevel(fLevel) ;
							PFP::AGENTS& an = agentvect[nf] ;
							for(PFP::AGENTS::iterator ait = an.begin(); ait != an.end(); ++ait)
							{
Pierre Kraemer's avatar
Pierre Kraemer committed
562 563
								if((*ait)->part_.d == map.phi1(nf))
									(*ait)->part_.d = nf ;
Pierre Kraemer's avatar
Pierre Kraemer committed
564 565 566 567 568
							}
							map.setCurrentLevel(fLevel - 1) ;
						}
						fit = map.phi1(fit) ;
					} while(fit != old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
569

Pierre Kraemer's avatar
Pierre Kraemer committed
570 571 572 573
					if(degree == 3)
					{
						map.setCurrentLevel(fLevel) ;
						Dart centerFace = map.phi2(map.phi1(old)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
574
						coarsenMark.unmark(centerFace) ;
Thomas's avatar
Thomas committed
575
						unsigned int start = it + 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
576
						unsigned int fEmb = map.getEmbedding(FACE, centerFace) ;
Thomas's avatar
Thomas committed
577
						while(start < coarsenCandidate.size())
Pierre Kraemer's avatar
Pierre Kraemer committed
578
						{
Thomas's avatar
Thomas committed
579
							if(map.getEmbedding(FACE, coarsenCandidate[start]) == fEmb)
Pierre Kraemer's avatar
Pierre Kraemer committed
580
							{
Thomas's avatar
Thomas committed
581
								coarsenCandidate[start] = coarsenCandidate.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
582
								coarsenCandidate.pop_back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
583
							}
Pierre Kraemer's avatar
Pierre Kraemer committed
584 585
							else
								++start ;
Pierre Kraemer's avatar
Pierre Kraemer committed
586
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
587 588 589
						PFP::AGENTS a(agentvect[centerFace]) ;
						agents.insert(agents.end(), a.begin(), a.end()) ;
						map.setCurrentLevel(map.getMaxLevel());
Pierre Kraemer's avatar
Pierre Kraemer committed
590 591
						for(PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait)
							popAgentInCells(*ait, centerFace);
Pierre Kraemer's avatar
Pierre Kraemer committed
592 593 594 595 596
						map.setCurrentLevel(fLevel - 1) ;
					}

					Algo::IHM::coarsenFace<PFP>(map, old, position) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
597 598 599
					std::pair<bool,bool>& sf = subdivisableFace[old] ;
					sf.first = true;
					sf.second = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
600 601 602 603

					map.setCurrentLevel(map.getMaxLevel());

					agentvect[old].clear();
Thomas's avatar
Thomas committed
604
					for(PFP::AGENTS::iterator itA = agents.begin(); itA != agents.end(); ++itA)
Pierre Kraemer's avatar
Pierre Kraemer committed
605
					{
Thomas's avatar
Thomas committed
606 607
						(*itA)->part_.d = old ;
						pushAgentInCells(*itA, old);
Pierre Kraemer's avatar
Pierre Kraemer committed
608
					}
Pierre Kraemer's avatar
Pierre Kraemer committed
609

Pierre Kraemer's avatar
Pierre Kraemer committed
610 611 612 613 614 615 616 617 618 619 620
					Dart dd = old;
					do
					{
						Dart ddd = map.alpha1(map.alpha1(dd));
						while(ddd != dd)
						{
							neighborAgentvect[old].insert(neighborAgentvect[old].end(), agentvect[ddd].begin(), agentvect[ddd].end());
							ddd = map.alpha1(ddd);
						}
						dd = map.phi1(dd);
					} while(dd != old);
Pierre Kraemer's avatar
Pierre Kraemer committed
621

Thomas's avatar
Thomas committed
622 623 624 625 626 627
					if(fLevel>1 && !coarsenMark.isMarked(old) && agentvect[old].size() < nbAgentsToSimplify / 4)
					{
						coarsenMark.mark(old);
						coarsenCandidate.push_back(map.faceOldestDart(old));
					}

Pierre Kraemer's avatar
Pierre Kraemer committed
628 629 630
				}
			}
			map.setCurrentLevel(cur) ;
Thomas's avatar
Thomas committed
631 632


Pierre Kraemer's avatar
Pierre Kraemer committed
633 634
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
635

Pierre Kraemer's avatar
Pierre Kraemer committed
636 637 638 639 640 641 642
	map.setCurrentLevel(map.getMaxLevel()) ;
}

void EnvMap::resetAgentInFace(Agent* agent)
{
	VEC3 pos = agent->part_.m_position;
	agent->part_.m_position = Algo::Geometry::faceCentroid<PFP>(map, agent->part_.d, position);
Pierre Kraemer's avatar
Pierre Kraemer committed
643
	agent->part_.state = FACE;
Pierre Kraemer's avatar
Pierre Kraemer committed
644 645
	agent->part_.move(pos);
}