env_map.cpp 12 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 9 10 11
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Geometry/normal.h"

using namespace CGoGN;

12
EnvMap::EnvMap() : obstacleMark(map, EDGE_CELL), buildingMark(map, FACE_CELL), refineMark(map, FACE_CELL)
Pierre Kraemer's avatar
Pierre Kraemer committed
13 14 15 16
{
	position = map.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
	normal = map.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal");
	agentvect = map.addAttribute<PFP::AGENTVECT>(FACE_ORBIT, "agents");
Pierre Kraemer's avatar
Pierre Kraemer committed
17
	neighborAgentvect = map.addAttribute<PFP::AGENTVECT>(FACE_ORBIT, "neighborAgents");
Pierre Kraemer's avatar
Pierre Kraemer committed
18
	obstvect = map.addAttribute<PFP::OBSTACLEVECT>(FACE_ORBIT, "obstacles");
19
	subdivisableFace = map.addAttribute<PFP::BOOLATTRIB>(FACE_ORBIT, "subdivisableFace");
Pierre Kraemer's avatar
Pierre Kraemer committed
20 21 22 23 24 25 26 27 28 29 30 31
}

unsigned int EnvMap::mapMemoryCost()
{
	return (map.getAttributeContainer(DART_ORBIT)).memorySize()
			+ (map.getAttributeContainer(VERTEX_ORBIT)).memorySize()
			+ (map.getAttributeContainer(EDGE_ORBIT)).memorySize()
			+ (map.getAttributeContainer(FACE_ORBIT)).memorySize();
}

Dart EnvMap::getBelongingCell(const PFP::VEC3& pos)
{
32
	CellMarkerStore m(map,FACE_ORBIT);
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
35 36 37 38 39
		if(!m.isMarked(d)) {
			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
40 41 42 43 44 45 46 47 48
	}

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

void EnvMap::init()
{
	float sideSize = 70.0f;
49
	unsigned int nbSquares = 25;
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	CityGenerator::generateCity<PFP>(map, position, obstacleMark, buildingMark, sideSize, nbSquares);
51
//	CityGenerator::generateMall<PFP>(map, position, obstacleMark, buildingMark, sideSize);
52
	CityGenerator::simplifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
Pierre Kraemer's avatar
Pierre Kraemer committed
53 54
	map.init();
	registerObstaclesInFaces();
55 56
	for(unsigned int i = subdivisableFace.begin(); i < subdivisableFace.end(); subdivisableFace.next(i))
		subdivisableFace[i].first = false ;
57

58
//	Algo::Geometry::computeNormalFaces<PFP>(map, position, normal);
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60
}

Pierre Kraemer's avatar
Pierre Kraemer committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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
76 77 78 79 80 81 82 83
void EnvMap::registerObstaclesInFaces()
{
	CellMarker m(map, FACE_CELL);
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!m.isMarked(d))
		{
			m.mark(d);
84
			if(!buildingMark.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
85
			{
86 87 88 89 90 91 92
				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
93

94
				do
Pierre Kraemer's avatar
Pierre Kraemer committed
95
				{
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
					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);
			}
		}
	}

	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(m.isMarked(d) && !buildingMark.isMarked(d)) {
			m.unmark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		}
	}
}

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);
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//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
//	CellMarkerNoUnmark f(map, FACE_CELL);
//	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
216

Pierre Kraemer's avatar
Pierre Kraemer committed
217 218
void EnvMap::agentChangeFace(Agent* agent, Dart oldFace)
{
219 220
	Dart newFace = agent->part_.d;

Pierre Kraemer's avatar
Pierre Kraemer committed
221
	popAgentInCells(agent, oldFace);
222 223 224
	pushAgentInCells(agent, newFace);

//	coarsenCandidate.insert(std::pair<unsigned int, Dart>(map.getEmbedding(FACE_ORBIT, oldFace), oldFace));
Pierre Kraemer's avatar
Pierre Kraemer committed
225

226 227 228 229 230 231 232 233 234
	if(!refineMark.isMarked(newFace) && agentvect[newFace].size() > nbAgentsToSubdivide)
	{
		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
235 236 237 238 239 240
}

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

241
	for(std::vector<Dart>::iterator it = refineCandidate.begin(); it != refineCandidate.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
242
	{
243 244
		Dart d = (*it) ;
		refineMark.unmark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
245

Pierre Kraemer's avatar
Pierre Kraemer committed
246
		if(agentvect[d].size() > nbAgentsToSubdivide)
Pierre Kraemer's avatar
Pierre Kraemer committed
247
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250
			unsigned int fLevel = -1 ;
			Dart old = map.faceOldestDart(d) ;

251
			bool subdivisable = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253 254 255

			std::pair<bool,bool>& sf = subdivisableFace[d] ;
			if(sf.first == true)
				subdivisable = sf.second ;
256
			else
Pierre Kraemer's avatar
Pierre Kraemer committed
257
			{
258 259
				float maxEdgeSize = Agent::neighborDistSq_ ; // diametre de vision de l'agent au carré

Pierre Kraemer's avatar
Pierre Kraemer committed
260
				fLevel = map.faceLevel(d) ;
261 262 263
				map.setCurrentLevel(fLevel) ;
				PFP::VEC3 fCenter = Algo::Geometry::faceCentroid<PFP>(map, old, position) ;
				Dart fd = old ;
Pierre Kraemer's avatar
Pierre Kraemer committed
264 265
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
266
					PFP::VEC3& p = position[fd] ;
267
					PFP::VEC3 edge = Algo::Geometry::vectorOutOfDart<PFP>(map, fd, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
268
					PFP::VEC3 proj = fCenter - (p + (edge * (fCenter - p) / edge.norm2()) * edge) ;
269 270
					if(proj.norm2() < maxEdgeSize)
	//				if(Algo::Geometry::vectorOutOfDart<PFP>(map, fd, position).norm2() < maxEdgeSize)
Pierre Kraemer's avatar
Pierre Kraemer committed
271
					{
272 273
						subdivisable = false ;
						break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
274
					}
275 276 277 278
					fd = map.phi1(fd) ;
				} while(fd != old) ;
				map.setCurrentLevel(map.getMaxLevel()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
279 280
				sf.first = true ;
				sf.second = subdivisable ;
281 282 283 284
			}

			if(subdivisable)
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286 287 288
				if(fLevel == -1)
					fLevel = map.faceLevel(d) ;

				sf.first = false ;
289 290 291 292 293 294

				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
295 296

				map.setCurrentLevel(fLevel) ;
297 298 299 300
				CellMarkerStore newF(map, FACE_CELL);
				newF.mark(old);
				Algo::IHM::subdivideFace<PFP>(map, old, position) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
301
				map.setCurrentLevel(map.getMaxLevel()) ;
302 303 304 305 306
				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
307

Pierre Kraemer's avatar
Pierre Kraemer committed
308 309
				unsigned int degree = 0 ;
				Dart dd = old ;
Pierre Kraemer's avatar
Pierre Kraemer committed
310 311
				do
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
312 313
					++degree ;
					Dart d3 = dd ;
314
					do
Pierre Kraemer's avatar
Pierre Kraemer committed
315
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
318 319 320
						while(d4 != d3)
						{
							if(!newF.isMarked(d4))
Pierre Kraemer's avatar
Pierre Kraemer committed
321 322 323 324 325
							{
								PFP::AGENTS& ad4 = agentvect[d4] ;
								nad3.insert(nad3.end(), ad4.begin(), ad4.end()) ;
							}
							d4 = map.alpha1(d4) ;
326
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328
						d3 = map.phi1(d3) ;
					} while(d3 != dd) ;
329 330

					map.setCurrentLevel(fLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
331
					dd = map.phi1(dd) ;
332 333 334 335 336
					map.setCurrentLevel(map.getMaxLevel()) ;
				} while(dd != old);

				if(degree == 3)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
337 338
					Dart centerFace = map.phi2(map.phi1(old)) ;
					Dart d3 = centerFace ;
339 340
					do
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
341 342
						Dart d4 = map.alpha1(map.alpha1(d3)) ;
						PFP::AGENTS& nad3 = neighborAgentvect[d3] ;
343 344 345
						while(d4 != d3)
						{
							if(!newF.isMarked(d4))
Pierre Kraemer's avatar
Pierre Kraemer committed
346 347 348 349 350
							{
								PFP::AGENTS& ad4 = agentvect[d4] ;
								nad3.insert(neighborAgentvect[d3].end(), ad4.begin(), ad4.end()) ;
							}
							d4 = map.alpha1(d4) ;
351
						}
Pierre Kraemer's avatar
Pierre Kraemer committed
352 353
						d3 = map.phi1(d3) ;
					} while(d3 != centerFace) ;
354
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
			}
		}
	}
/*
	for(std::map<unsigned int, Dart>::iterator it = coarsenCandidate.begin(); it != coarsenCandidate.end(); ++it)
	{
		unsigned int face = (*it).first ;
		Dart d = (*it).second ;

		unsigned int fLevel = map.faceLevel(d) ;
		Dart old = map.faceOldestDart(d) ;

		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
379
					nbAgents += agentvect[fit].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
					++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) ;
						unsigned int fEmb = map.getEmbedding(fit, FACE_ORBIT) ;
						if(fEmb != face)
							coarsenCandidate.erase(fEmb) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
401 402 403 404 405
						PFP::AGENTS a(agentvect[fit]) ;
						agents.insert(agents.end(), a.begin(), a.end()) ;
						map.setCurrentLevel(map.getMaxLevel()) ;
						for(PFP::AGENTS::iterator it = a.begin(); it != a.end(); ++it)
							popAgentInCells(*it, fit);
Pierre Kraemer's avatar
Pierre Kraemer committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
						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 == nf)
									(*ait)->part_.d = map.phi1(nf) ;
							}
							map.setCurrentLevel(fLevel - 1) ;
						}
						fit = map.phi1(fit) ;
					} while(fit != old) ;
					if(degree == 3)
					{
						map.setCurrentLevel(fLevel) ;
						Dart centerFace = map.phi2(map.phi1(old)) ;
						unsigned int fEmb = map.getEmbedding(centerFace, FACE_ORBIT) ;
						if(fEmb != face)
							coarsenCandidate.erase(fEmb) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
428 429 430 431 432
						PFP::AGENTS a(agentvect[centerFace]) ;
						agents.insert(agents.end(), a.begin(), a.end()) ;
						map.setCurrentLevel(map.getMaxLevel());
						for(PFP::AGENTS::iterator it = a.begin(); it != a.end(); ++it)
							popAgentInCells(*it, centerFace);
Pierre Kraemer's avatar
Pierre Kraemer committed
433 434 435 436 437
						map.setCurrentLevel(fLevel - 1) ;
					}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
438 439 440 441 442
					// gestion des obstacles !!

					map.setCurrentLevel(map.getMaxLevel());

					agentvect[old].clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
443
					for(PFP::AGENTS::iterator it = agents.begin(); it != agents.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
444
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
445
						(*it)->part_.d = old ;
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447
						pushAgentInCells(*it, old);
					}
Pierre Kraemer's avatar
Pierre Kraemer committed
448

Pierre Kraemer's avatar
Pierre Kraemer committed
449 450 451 452 453 454 455 456 457 458 459
					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
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
				}
			}
			map.setCurrentLevel(cur) ;
		}
	}
*/
	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);
	agent->part_.state = FACE_ORBIT;
	agent->part_.move(pos);
}