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

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

13 14
#include "env_generator.h"

15
using namespace CGoGN ;
Pierre Kraemer's avatar
Pierre Kraemer committed
16

Pierre Kraemer's avatar
Pierre Kraemer committed
17
EnvMap::EnvMap() :
David Cazier's avatar
David Cazier committed
18 19 20 21 22 23
	obstacleDistance(Agent::range_),
	obstacleMarkS(mapScenary, EDGE),
	buildingMarkS(mapScenary, FACE),
	obstacleMark(map, EDGE),
	buildingMark(map, FACE),
	pedWayMark(map, FACE),
Pierre Kraemer's avatar
Pierre Kraemer committed
24
#ifndef SPATIAL_HASHING
David Cazier's avatar
David Cazier committed
25 26
	refineMark(map, FACE),
	coarsenMark(map, FACE)
Pierre Kraemer's avatar
Pierre Kraemer committed
27
#else
David Cazier's avatar
David Cazier committed
28 29 30
ht_agents(1024),
ht_neighbor_agents(1024),
ht_obstacles(1024)
Pierre Kraemer's avatar
Pierre Kraemer committed
31
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
32
{
33 34
	position = map.addAttribute<PFP::VEC3>(VERTEX, "position") ;
	normal = map.addAttribute<PFP::VEC3>(VERTEX, "normal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36

#ifndef SPATIAL_HASHING
37 38 39 40
	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
41

42 43 44 45 46 47 48 49
	refineCandidate.reserve(100) ;
	coarsenCandidate.reserve(100) ;
#endif
}

void EnvMap::init(unsigned int config, REAL width, REAL height, REAL minSize, REAL maxSize)
{
	std::cout << "Init EnvMap" << std::endl ;
David Cazier's avatar
David Cazier committed
50
	VEC3 bottomLeft(-width / 2, -height / 2, 0.0f) ;
51 52 53 54 55 56 57 58 59 60
	VEC3 topRight(width / 2, height / 2, 0.0f) ;

	geometry.reset() ;
	geometry.addPoint(bottomLeft) ;
	geometry.addPoint(topRight) ;
	minCellSize = minSize ;
	maxCellSize = maxSize ;
	std::cout << " - Geometry : " << geometry ;
	std::cout << " - Cell size between : " << minSize << " and " << maxSize << std::endl ;
#ifdef SPATIAL_HASHING
David Cazier's avatar
David Cazier committed
61
	std::cout << " - Table de hachage : " << agentGridSize(0) << " x " << agentGridSize(1) << std::endl ;
62 63 64 65
#endif

	unsigned int nbSquares = 6 ;

David Cazier's avatar
David Cazier committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
	switch (config)
	{
	case 0 :
		CityGenerator::generateGrid<PFP>(*this) ;
		break ;
	case 1 :
		CityGenerator::generateGrid<PFP>(*this) ;
		break ;
	case 2 :
		CityGenerator::generateCity<PFP>(*this) ;
		// CityGenerator::generateMall<PFP>(map, position, obstacleMark, buildingMark, sideSize);
		break ;
	case 3 :
	{
80 81 82 83 84 85 86 87 88 89 90 91 92
//			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) ;
//			map.closeMap() ;
//			Algo::Modelisation::CatmullClarkSubdivision<PFP>(map, position) ;
//			Algo::Modelisation::computeDual<PFP>(map) ;
David Cazier's avatar
David Cazier committed
93 94 95 96 97 98
	}
		break ;
	case 4 :
		CityGenerator::generatePlanet<PFP>(map, position, obstacleMark, buildingMark, 200.0f,
		                                   nbSquares) ;
		break ;
99 100 101 102 103 104 105
	}

//	CityGenerator::simplifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
//	CityGenerator::convexifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
//	CityGenerator::installGuardRail<PFP>(map, position, obstacleMark, buildingMark, 5.0f);

#ifndef SPATIAL_HASHING
David Cazier's avatar
David Cazier committed
106 107 108 109 110
//	for (Dart d = map.begin() ; d != map.end(); map.next(d)) {
//		Dart d1 = map.phi1(d);
//		Dart d11= map.phi1(d1);
//		if (map.phi1(d11) != d) map.splitFace(d,d11);
//	}
111 112 113 114 115
	map.init() ;
//	registerObstaclesInFaces();

//	subdivideAllToMaxLevel();

David Cazier's avatar
David Cazier committed
116 117
	for (unsigned int i = subdivisableFace.begin(); i < subdivisableFace.end();
	    subdivisableFace.next(i))
118
		subdivisableFace[i].first = false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
}

122 123
void EnvMap::markPedWay()
{
124
	CellMarker treat(map, FACE) ;
David Cazier's avatar
David Cazier committed
125 126 127 128
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!treat.isMarked(d))
		{
129 130 131
			treat.mark(d) ;

			Dart dd = d ;
David Cazier's avatar
David Cazier committed
132 133
			do
			{
134
				Dart ddd = dd ;
David Cazier's avatar
David Cazier committed
135 136 137 138
				do
				{
					if (obstacleMark.isMarked(dd))
					{
139 140
						pedWayMark.mark(d) ;
						break ;
141
					}
142 143
					ddd = map.alpha1(ddd) ;
				} while (ddd != dd) ;
144

145
				dd = map.phi1(dd) ;
146

147
			} while (dd != d) ;
148 149 150
		}
	}

151
	treat.unmarkAll() ;
David Cazier's avatar
David Cazier committed
152 153 154 155
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!treat.isMarked(d))
		{
156 157 158
			treat.mark(d) ;

			Dart dd = d ;
David Cazier's avatar
David Cazier committed
159 160 161 162 163 164
			do
			{
				if (pedWayMark.isMarked(map.phi2(dd)) && pedWayMark.isMarked(map.phi2(map.phi1(dd)))
				    && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(dd))))
				    && !pedWayMark.isMarked(map.phi2(map.phi1(map.phi1(map.phi1(dd))))))
				{
165 166 167
					pedWayMark.mark(d) ;
					break ;
				}
168

169
				dd = map.phi1(dd) ;
170

171
			} while (dd != d) ;
172 173 174 175
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
176 177
unsigned int EnvMap::mapMemoryCost()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
178
	return (map.getAttributeContainer(DART)).memorySize()
David Cazier's avatar
David Cazier committed
179 180 181
	    + (map.getAttributeContainer(VERTEX)).memorySize()
	    + (map.getAttributeContainer(EDGE)).memorySize()
	    + (map.getAttributeContainer(FACE)).memorySize() ;
Thomas's avatar
Thomas committed
182 183
}

Pierre Kraemer's avatar
Pierre Kraemer committed
184
#ifndef SPATIAL_HASHING
Thomas's avatar
Thomas committed
185 186
void EnvMap::subdivideAllToMaxLevel()
{
187
	bool subdiv ;
David Cazier's avatar
David Cazier committed
188 189
	do
	{
190
		subdiv = false ;
Thomas's avatar
Thomas committed
191
		{
192
			CellMarker subd(map, FACE) ;
David Cazier's avatar
David Cazier committed
193 194 195 196
			for (Dart d = map.begin(); d != map.end(); map.next(d))
			{
				if (!subd.isMarked(d))
				{
197
					subd.mark(d) ;
David Cazier's avatar
David Cazier committed
198 199
					if (!buildingMark.isMarked(d))
					{
Thomas's avatar
Thomas committed
200 201
						//check if subdivision is authorized
						float minDistSq = Agent::neighborDistSq_ ;
202
						bool subdivisable = true ;
Thomas's avatar
Thomas committed
203 204 205
						Dart old = map.faceOldestDart(d) ;
						unsigned int fLevel = map.faceLevel(old) ;
						map.setCurrentLevel(fLevel) ;
David Cazier's avatar
David Cazier committed
206
						PFP::VEC3 fCenter = Algo::Geometry::faceCentroid<PFP>(map, old, position) ;
Thomas's avatar
Thomas committed
207
						Dart fd = old ;
David Cazier's avatar
David Cazier committed
208 209
						do
						{
Thomas's avatar
Thomas committed
210
							PFP::VEC3& p = position[fd] ;
211
							PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(map, fd,
David Cazier's avatar
David Cazier committed
212
							                                                      position) ;
213
							PFP::VEC3 proj = fCenter
David Cazier's avatar
David Cazier committed
214 215 216
							    - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ;
							if (proj.norm2() < minDistSq)
							{
Thomas's avatar
Thomas committed
217 218 219 220
								subdivisable = false ;
								break ;
							}
							fd = map.phi1(fd) ;
221
						} while (fd != old) ;
Thomas's avatar
Thomas committed
222

David Cazier's avatar
David Cazier committed
223 224
						if (subdivisable)
						{
Thomas's avatar
Thomas committed
225 226
							map.setCurrentLevel(fLevel) ;
							Algo::IHM::subdivideFace<PFP>(map, old, position) ;
227
							subdiv = true ;
Thomas's avatar
Thomas committed
228 229 230 231 232 233
						}
						map.setCurrentLevel(map.getMaxLevel()) ;
					}
				}
			}
		}
234
	} while (subdiv) ;
Thomas's avatar
Thomas committed
235 236 237 238
}

void EnvMap::subdivideToProperLevel()
{
239
	bool subdiv ;
David Cazier's avatar
David Cazier committed
240 241
	do
	{
242
		subdiv = false ;
Thomas's avatar
Thomas committed
243
		{
244
			CellMarker subd(map, FACE) ;
David Cazier's avatar
David Cazier committed
245 246 247 248
			for (Dart d = map.begin(); d != map.end(); map.next(d))
			{
				if (!subd.isMarked(d))
				{
249
					subd.mark(d) ;
David Cazier's avatar
David Cazier committed
250 251
					if (!refineMark.isMarked(d) && agentvect[d].size() > nbAgentsToSubdivide)
					{
252
						std::pair<bool, bool>& sf = subdivisableFace[d] ;
David Cazier's avatar
David Cazier committed
253 254
						if (sf.first == false || (sf.first == true && sf.second))
						{
255 256 257
							subdiv = true ;
							refineMark.mark(d) ;
							refineCandidate.push_back(d) ;
Thomas's avatar
Thomas committed
258 259 260 261
						}
					}
				}
			}
262
			subd.unmarkAll() ;
Thomas's avatar
Thomas committed
263
		}
264 265 266 267
		updateMap() ;
		refineCandidate.clear() ;
		map.setCurrentLevel(map.getMaxLevel()) ;
	} while (subdiv) ;
Thomas's avatar
Thomas committed
268 269
}

Pierre Kraemer's avatar
Pierre Kraemer committed
270 271
Dart EnvMap::getBelongingCell(const PFP::VEC3& pos)
{
272
	CellMarkerStore m(map, FACE) ;
David Cazier's avatar
David Cazier committed
273 274 275 276
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!m.isMarked(d))
		{
277
			m.mark(d) ;
David Cazier's avatar
David Cazier committed
278 279
			if (!map.isBoundaryFace(d) && !buildingMark.isMarked(d)
			    && Algo::Geometry::isPointInConvexFace2D<PFP>(map, d, position, pos, true)) return d ;
280
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
281 282
	}

283 284
	std::cout << "ERROR : pos not in map" << std::endl ;
	return map.begin() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
285
}
Pierre Kraemer's avatar
Pierre Kraemer committed
286
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
287

Pierre Kraemer's avatar
Pierre Kraemer committed
288
#ifndef SPATIAL_HASHING
Pierre Kraemer's avatar
Pierre Kraemer committed
289 290
void EnvMap::foreach_neighborFace(Dart d, FunctorType& f)
{
291
	Dart dd = d ;
David Cazier's avatar
David Cazier committed
292 293
	do
	{
294
		Dart ddd = map.alpha1(map.alpha1(dd)) ;
David Cazier's avatar
David Cazier committed
295 296
		while (ddd != dd)
		{
297 298
			f(ddd) ;
			ddd = map.alpha1(ddd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
299
		}
300 301
		dd = map.phi1(dd) ;
	} while (dd != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303
}

Pierre Kraemer's avatar
Pierre Kraemer committed
304 305
void EnvMap::registerObstaclesInFaces()
{
306
	CellMarker m(map, FACE) ;
David Cazier's avatar
David Cazier committed
307 308 309 310
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!m.isMarked(d))
		{
311
			m.mark(d) ;
David Cazier's avatar
David Cazier committed
312 313
			if (!buildingMark.isMarked(d))
			{
314
				Dart dd = d ;
315 316 317 318

				//retrieve all obstacles within its one-ring

				//first : test all edges of the face of d
David Cazier's avatar
David Cazier committed
319 320 321 322
				do
				{
					if (obstacleMark.isMarked(dd))
					{
323 324 325 326
						Dart dd2 = map.phi2(dd) ;
						Dart next = map.phi1(dd2) ;
						Dart previous = map.phi_1(dd2) ;
						Obstacle* o = new Obstacle(position[dd2], position[next],
David Cazier's avatar
David Cazier committed
327
						                           position[previous], position[map.phi1(next)]) ;
328
						obstvect[d].push_back(o) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
329
					}
330 331
					dd = map.phi1(dd) ;
				} while (dd != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
332

333
				//second : test all edges of neighboring faces
David Cazier's avatar
David Cazier committed
334 335
				do
				{
336
					Dart ddd = map.alpha1(map.alpha1(dd)) ;
David Cazier's avatar
David Cazier committed
337 338 339 340
					while (ddd != dd)
					{
						if (!buildingMark.isMarked(ddd)) addNeighborObstacles(
						    obstvect[d], ddd, ddd == map.alpha_1(dd)) ;
341
						ddd = map.alpha1(ddd) ;
342
					}
343 344
					dd = map.phi1(dd) ;
				} while (dd != d) ;
345 346 347
			}
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
348 349 350 351
}

void EnvMap::addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbor)
{
352
	Dart stop ;
David Cazier's avatar
David Cazier committed
353 354 355 356
	if (edgeNeighbor)
		stop = map.phi_1(map.phi_1(map.phi_1(d))) ;
	else
		stop = map.phi_1(map.phi_1(d)) ;
357 358

	Dart dd = d ;
David Cazier's avatar
David Cazier committed
359 360 361 362
	do
	{
		if (obstacleMark.isMarked(dd))
		{
363 364 365 366 367 368 369 370 371 372
//			if(buildingMark.isMarked(dd))
//			{
//				std::cout << "caca prout" << std::endl;
//				Dart next = map.phi1(dd);
//				Dart previous = map.phi_1(dd);
//				Obstacle* o = new Obstacle(position[dd], position[next], position[previous], position[map.phi1(next)]);
//				obst.push_back(o);
//			}
//			else
//			{
373 374 375 376
			Dart dd2 = map.phi2(dd) ;
			Dart next = map.phi1(dd2) ;
			Dart previous = map.phi_1(dd2) ;
			Obstacle* o = new Obstacle(position[dd2], position[next], position[previous],
David Cazier's avatar
David Cazier committed
377
			                           position[map.phi1(next)]) ;
378
			obst.push_back(o) ;
379
//			}
Pierre Kraemer's avatar
Pierre Kraemer committed
380
		}
381 382
		dd = map.phi1(dd) ;
	} while (dd != stop) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
383 384
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
//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
404
//	CellMarkerNoUnmark f(map, FACE);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
//	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
462

Pierre Kraemer's avatar
Pierre Kraemer committed
463 464
void EnvMap::agentChangeFace(Agent* agent, Dart oldFace)
{
465
	Dart newFace = agent->part_.d ;
466

467 468
	popAgentInCells(agent, oldFace) ;
	pushAgentInCells(agent, newFace) ;
469

David Cazier's avatar
David Cazier committed
470 471
	if (!refineMark.isMarked(newFace) && agentvect[newFace].size() > nbAgentsToSubdivide)
	{
472
		std::pair<bool, bool>& sf = subdivisableFace[newFace] ;
David Cazier's avatar
David Cazier committed
473 474
		if (sf.first == false || (sf.first == true && sf.second))
		{
475 476
			refineMark.mark(newFace) ;
			refineCandidate.push_back(newFace) ;
477 478
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
479

David Cazier's avatar
David Cazier committed
480 481 482
	if (!coarsenMark.isMarked(oldFace) && !refineMark.isMarked(oldFace)
	    && 4 * agentvect[oldFace].size() < nbAgentsToSimplify)
	{
483 484
		coarsenMark.mark(oldFace) ;
		coarsenCandidate.push_back(map.faceOldestDart(oldFace)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
485
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
486 487
}

Pierre Kraemer's avatar
Pierre Kraemer committed
488 489
void EnvMap::obstacleChangeFace(Obstacle* agent, Dart newFace, Dart oldFace)
{
490 491
	popObstacleInCells(agent, oldFace) ;
	pushObstacleInCells(agent, newFace) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
492 493
}

Pierre Kraemer's avatar
Pierre Kraemer committed
494 495 496 497
void EnvMap::updateMap()
{
	assert(map.getCurrentLevel() == map.getMaxLevel()) ;

David Cazier's avatar
David Cazier committed
498 499 500 501
	for (std::vector<Dart>::iterator it = refineCandidate.begin(); it != refineCandidate.end();
	    ++it)
	{
		Dart d = (*it) ;
502
		refineMark.unmark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
503

David Cazier's avatar
David Cazier committed
504 505
		if (agentvect[d].size() > nbAgentsToSubdivide)
		{
Thomas's avatar
Thomas committed
506
			int fLevel = -1 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
507 508
			Dart old = map.faceOldestDart(d) ;

509
			bool subdivisable = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
510

511
			std::pair<bool, bool>& sf = subdivisableFace[old] ;
David Cazier's avatar
David Cazier committed
512 513 514 515
			if (sf.first == true)
				subdivisable = sf.second ;
			else
			{
516
				float minSizeSq = minCellSize * minCellSize ; // diametre de vision de l'agent au carré
517

Pierre Kraemer's avatar
Pierre Kraemer committed
518
				fLevel = map.faceLevel(old) ;
519 520 521
				map.setCurrentLevel(fLevel) ;
				PFP::VEC3 fCenter = Algo::Geometry::faceCentroid<PFP>(map, old, position) ;
				Dart fd = old ;
David Cazier's avatar
David Cazier committed
522 523
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
524
					PFP::VEC3& p = position[fd] ;
525
					PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(map, fd, position) ;
David Cazier's avatar
David Cazier committed
526 527 528
					PFP::VEC3 proj = fCenter - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ;
					if (proj.norm2() < minSizeSq)
					{
529 530
						subdivisable = false ;
						break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
531
					}
532
					fd = map.phi1(fd) ;
533
				} while (fd != old) ;
534 535
				map.setCurrentLevel(map.getMaxLevel()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
536 537
				sf.first = true ;
				sf.second = subdivisable ;
538 539
			}

David Cazier's avatar
David Cazier committed
540 541
			if (subdivisable)
			{
542
				if (fLevel == -1) fLevel = map.faceLevel(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
543 544

				sf.first = false ;
545

546
				PFP::AGENTS oldAgents(agentvect[old]) ;
David Cazier's avatar
David Cazier committed
547 548
				for (PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait)
					popAgentInCells(*ait, old) ;
549 550

				neighborAgentvect[old].clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
551 552

				map.setCurrentLevel(fLevel) ;
553
				Algo::IHM::subdivideFace<PFP>(map, old, position) ;
554 555 556
				CellMarkerStore newF(map, FACE) ;
				unsigned int degree = 0 ;
				Dart dd = old ;
David Cazier's avatar
David Cazier committed
557 558
				do
				{
559
					++degree ;
560
					newF.mark(dd) ;
561
					dd = map.phi1(dd) ;
562
				} while (dd != old) ;
David Cazier's avatar
David Cazier committed
563 564
				if (degree == 3)
				{
565
					Dart centerFace = map.phi2(map.phi1(old)) ;
566
					newF.mark(centerFace) ;
567
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
568
				map.setCurrentLevel(map.getMaxLevel()) ;
569

David Cazier's avatar
David Cazier committed
570 571 572 573
				for (PFP::AGENTS::iterator ait = oldAgents.begin(); ait != oldAgents.end(); ++ait)
				{
					resetAgentInFace(*ait) ;
					pushAgentInCells(*ait, (*ait)->part_.d) ;
574
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
575

576
				dd = old ;
David Cazier's avatar
David Cazier committed
577 578
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
579
					Dart d3 = dd ;
David Cazier's avatar
David Cazier committed
580 581
					do
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
582 583
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
David Cazier's avatar
David Cazier committed
584 585 586 587
						while (d4 != d3)
						{
							if (!newF.isMarked(d4))
							{
Pierre Kraemer's avatar
Pierre Kraemer committed
588 589 590 591
								PFP::AGENTS& ad4 = agentvect[d4] ;
								nad3.insert(nad3.end(), ad4.begin(), ad4.end()) ;
							}
							d4 = map.alpha1(d4) ;
592
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
593
						d3 = map.phi1(d3) ;
594
					} while (d3 != dd) ;
595 596

					map.setCurrentLevel(fLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
597
					dd = map.phi1(dd) ;
598
					map.setCurrentLevel(map.getMaxLevel()) ;
599
				} while (dd != old) ;
600

David Cazier's avatar
David Cazier committed
601 602
				if (degree == 3)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
603 604
					Dart centerFace = map.phi2(map.phi1(old)) ;
					Dart d3 = centerFace ;
David Cazier's avatar
David Cazier committed
605 606
					do
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
607 608
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
David Cazier's avatar
David Cazier committed
609 610 611 612
						while (d4 != d3)
						{
							if (!newF.isMarked(d4))
							{
Pierre Kraemer's avatar
Pierre Kraemer committed
613
								PFP::AGENTS& ad4 = agentvect[d4] ;
David Cazier's avatar
David Cazier committed
614
								nad3.insert(neighborAgentvect[d3].end(), ad4.begin(), ad4.end()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
615 616
							}
							d4 = map.alpha1(d4) ;
617
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
618
						d3 = map.phi1(d3) ;
619
					} while (d3 != centerFace) ;
620
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
621 622 623
			}
		}
	}
David Cazier's avatar
David Cazier committed
624 625 626 627 628 629 630 631
	refineCandidate.clear() ;

	// On recrée une liste des faces à simplifier en empêchant les doublons
	// On en profite pour vérifier les conditions de simplifications
	std::vector<Dart> checkCoarsenCandidate ;
	checkCoarsenCandidate.reserve(coarsenCandidate.size()) ;
	for (unsigned int it = 0; it < coarsenCandidate.size(); ++it)
	{
Thomas's avatar
Thomas committed
632
		Dart old = coarsenCandidate[it] ;
David Cazier's avatar
David Cazier committed
633
		bool oldIsMarked = coarsenMark.isMarked(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
634
		coarsenMark.unmark(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
635

Pierre Kraemer's avatar
Pierre Kraemer committed
636
		unsigned int fLevel = map.faceLevel(old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
637

David Cazier's avatar
David Cazier committed
638 639
		if (oldIsMarked && fLevel > 0 && map.getDartLevel(old) < fLevel)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
640 641 642
			unsigned int cur = map.getCurrentLevel() ;
			map.setCurrentLevel(fLevel - 1) ;

David Cazier's avatar
David Cazier committed
643 644
			if (map.faceIsSubdividedOnce(old))
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
645 646
				// on compte le nombre d'agents dans les sous-faces
				// on en profite pour compter le degré de la face grossière
Pierre Kraemer's avatar
Pierre Kraemer committed
647 648 649
				unsigned int degree = 0 ;
				unsigned int nbAgents = 0 ;
				Dart fit = old ;
David Cazier's avatar
David Cazier committed
650 651
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
652
					nbAgents += agentvect[fit].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
653
					++degree ;
Pierre Kraemer's avatar
Pierre Kraemer committed
654
					coarsenMark.unmark(fit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
655
					fit = map.phi1(fit) ;
656 657
				} while (fit != old) ;

David Cazier's avatar
David Cazier committed
658 659
				if (degree == 3)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
660 661 662
					map.setCurrentLevel(fLevel) ;
					Dart centerFace = map.phi2(map.phi1(old)) ;
					nbAgents += agentvect[centerFace].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
663
					coarsenMark.unmark(centerFace) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
664 665
					map.setCurrentLevel(fLevel - 1) ;
				}
David Cazier's avatar
David Cazier committed
666 667 668 669 670 671
				if (nbAgents < nbAgentsToSimplify) checkCoarsenCandidate.push_back(old) ;
			}
			map.setCurrentLevel(cur) ;
		}
	}
	coarsenCandidate.clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
672

David Cazier's avatar
David Cazier committed
673 674 675 676
	// On réalise la simplification (les conditions ont déjà été vérifiées)
	for (unsigned int it = 0; it < checkCoarsenCandidate.size(); ++it)
	{
		Dart old = checkCoarsenCandidate[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
677

David Cazier's avatar
David Cazier committed
678 679 680
		unsigned int fLevel = map.faceLevel(old) ;
		unsigned int cur = map.getCurrentLevel() ;
		map.setCurrentLevel(fLevel - 1) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
681

David Cazier's avatar
David Cazier committed
682 683 684 685 686 687 688 689
		// on compte le degré de la face grossière
		unsigned int degree = 0 ;
		Dart fit = old ;
		do
		{
			++degree ;
			fit = map.phi1(fit) ;
		} while (fit != old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
690

David Cazier's avatar
David Cazier committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
		PFP::AGENTS agents ;
		fit = old ;
		do
		{
			PFP::AGENTS a(agentvect[fit]) ;
			agents.insert(agents.end(), a.begin(), a.end()) ;
			map.setCurrentLevel(map.getMaxLevel()) ;
			for (PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait)
				popAgentInCells(*ait, fit) ;
			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)
				{
					if ((*ait)->part_.d == map.phi1(nf)) (*ait)->part_.d = nf ;
				}
				map.setCurrentLevel(fLevel - 1) ;
			}
			fit = map.phi1(fit) ;
		} while (fit != old) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
714

David Cazier's avatar
David Cazier committed
715 716 717 718 719 720 721 722 723 724 725 726
		if (degree == 3)
		{
			map.setCurrentLevel(fLevel) ;
			Dart centerFace = map.phi2(map.phi1(old)) ;
			PFP::AGENTS a(agentvect[centerFace]) ;
			agents.insert(agents.end(), a.begin(), a.end()) ;
			map.setCurrentLevel(map.getMaxLevel()) ;
			for (PFP::AGENTS::iterator ait = a.begin(); ait != a.end(); ++ait)
				popAgentInCells(*ait, centerFace) ;
			map.setCurrentLevel(fLevel - 1) ;
		}
		neighborAgentvect[old].clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
727

David Cazier's avatar
David Cazier committed
728
		Algo::IHM::coarsenFace<PFP>(map, old, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
729

David Cazier's avatar
David Cazier committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
		std::pair<bool, bool>& sf = subdivisableFace[old] ;
		sf.first = true ;
		sf.second = true ;

		map.setCurrentLevel(map.getMaxLevel()) ;

		for (PFP::AGENTS::iterator itA = agents.begin(); itA != agents.end(); ++itA)
		{
			(*itA)->part_.d = old ;
			pushAgentInCells(*itA, old) ;
		}

		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) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
752
			}
David Cazier's avatar
David Cazier committed
753 754 755 756 757 758 759 760
			dd = map.phi1(dd) ;
		} while (dd != old) ;

		if (fLevel > 1 && !coarsenMark.isMarked(old)
			&& agentvect[old].size() < nbAgentsToSimplify)
		{
			coarsenMark.mark(old) ;
			coarsenCandidate.push_back(map.faceOldestDart(old)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
761
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
762

David Cazier's avatar
David Cazier committed
763 764
		map.setCurrentLevel(cur) ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
765
	map.setCurrentLevel(map.getMaxLevel()) ;
David Cazier's avatar
David Cazier committed
766
	if (coarsenCandidate.size() > 0) updateMap() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
767 768 769 770
}

void EnvMap::resetAgentInFace(Agent* agent)
{
771 772 773 774
	VEC3 pos = agent->part_.m_position ;
	agent->part_.m_position = Algo::Geometry::faceCentroid<PFP>(map, agent->part_.d, position) ;
	agent->part_.state = FACE ;
	agent->part_.move(pos) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
775
}
Pierre Kraemer's avatar
Pierre Kraemer committed
776
#endif