env_map.h 11 KB
Newer Older
1 2
#ifndef ENV_MAP_H
#define ENV_MAP_H
Pierre Kraemer's avatar
Pierre Kraemer committed
3 4 5 6 7

#include <iostream>
#include <algorithm>

#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
update  
Pierre Kraemer committed
8
#include "Topology/map/embeddedMap2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
9 10 11 12 13 14 15 16 17

#include "Algo/ImplicitHierarchicalMesh/ihm.h"
#include "Algo/ImplicitHierarchicalMesh/subdivision.h"

#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Modelisation/extrusion.h"
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Geometry/centroid.h"
#include "Algo/Geometry/area.h"
Thomas's avatar
Thomas committed
18
#include "Geometry/bounding_box.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
19 20 21 22 23

#include "Container/fakeAttribute.h"

#include "Algo/Parallel/parallel_foreach.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
24 25
#include "spatialHashing.h"

26
using namespace CGoGN ;
Pierre Kraemer's avatar
Pierre Kraemer committed
27

pitiot's avatar
maj  
pitiot committed
28 29 30
class Agent;
class Obstacle;
class MovingObstacle;
Pierre Kraemer's avatar
Pierre Kraemer committed
31

David Cazier's avatar
David Cazier committed
32
#include "pfp.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35

class EnvMap
{
36 37
public:
	PFP::MAP map ;
38

39 40 41 42 43 44 45 46 47 48 49 50
	/* Geometry of the EnvMap : the bounding box of the scene
	 * The density of cells in the EnvMap is given by two parameters :
	 *  - minCellSize : the size under which no subdivision occurs
	 *  - maxCellSize : the size of the initial cells (before subdivisions occur)
	 *  - obstacleDistance : the minimal goal distance between agents and obstacles
	 * The number of cells in the initial EnvMap is about (width*height)/(size*size)
	 */
	Geom::BoundingBox<VEC3> geometry ;
	REAL maxCellSize ;
	REAL minCellSize ;
	REAL obstacleDistance ;

David Cazier's avatar
David Cazier committed
51
	EnvMap() ;
52
	void init(unsigned int config, REAL width, REAL height, REAL minSize, REAL maxSize) ;
53

David Cazier's avatar
David Cazier committed
54
	void markPedWay() ;
Thomas's avatar
Thomas committed
55

David Cazier's avatar
David Cazier committed
56
	unsigned int mapMemoryCost() ;
Thomas's avatar
Thomas committed
57

Pierre Kraemer's avatar
Pierre Kraemer committed
58
#ifndef SPATIAL_HASHING
59
	Dart getBelongingCell(const VEC3& pos) ;
Thomas's avatar
Thomas committed
60

61 62
	void subdivideAllToMaxLevel() ;
	void subdivideToProperLevel() ;
Thomas's avatar
Thomas committed
63

64
	void foreach_neighborFace(Dart d, FunctorType& f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
65

66
	void registerObstaclesInFaces() ;
pitiot's avatar
maj  
pitiot committed
67
	void registerWallInFaces();
68
	void addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbor) ;
69

70
	void setAgentNeighborsAndObstacles(Agent* agent) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
71

72 73 74 75 76 77
	unsigned int totalNeighborSize(Dart d) ;
	void nbAgentsIncrease(Dart d) ;
	void nbAgentsDecrease(Dart d) ;
	void pushAgentInCells(Agent* agent, Dart d) ;
	void popAgentInCells(Agent* agent, Dart d) ;
	void agentChangeFace(Agent* agent, Dart oldFace) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78

79 80 81 82 83 84
	void popAndPushObstacleInCells(Obstacle* o, int n);
	void pushObstacleInCells(Obstacle * mo);
	void pushObstacleInOneRingCells(Obstacle * o, Dart d, int n);
	void pushObstacleInCells(Obstacle* o, int n, const std::vector<Dart>& memo_cross);
	void popObstacleInCells(Obstacle* o, int n);

85
	void popObstacleInCells(Obstacle* o, Dart d) ;
86 87
	void refine() ;
	void coarse() ;
88
	void updateMap() ;
89

90
	void resetAgentInFace(Agent* agent) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
#endif

David Cazier's avatar
David Cazier committed
93 94
	PFP::TVEC3 position ;
	PFP::TVEC3 normal ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95

David Cazier's avatar
David Cazier committed
96 97 98
	PFP::MAP mapScenary ;
	PFP::TVEC3 positionScenary ;
	PFP::TVEC3 normalScenary ;
pitiot's avatar
maj  
pitiot committed
99 100 101 102 103
	CellMarker<EDGE> obstacleMarkS ;
	CellMarker<FACE> buildingMarkS ;
	CellMarker<EDGE> obstacleMark ;
	CellMarker<FACE> buildingMark ;
	CellMarker<FACE> pedWayMark ;
104
//	CellMarkerMemo<FACE> memo_mark;
pitiot's avatar
maj  
pitiot committed
105
	// ajout moving obst
106 107
	void addObstAsNeighbor (Obstacle *  o, const std::vector<Dart>& belonging_cells, std::vector<Dart> * nieghbor_cells);
	// void addMovingObstAsNeighbor (MovingObstacle *  mo,std::vector<Dart> belonging_cells, std::vector<Dart> *neighbor_cells);
pitiot's avatar
maj  
pitiot committed
108 109
	void pushObstNeighborInCells(Obstacle* o, Dart d);
	void popObstNeighborInCells(Obstacle* o, Dart d);
110
	void find_next(Obstacle* o,Dart * d, CellMarkerMemo<FACE>& cms);
pitiot's avatar
maj  
pitiot committed
111

112

David Cazier's avatar
David Cazier committed
113
	std::vector<Dart> newBuildings ;
114

Pierre Kraemer's avatar
Pierre Kraemer committed
115
#ifndef SPATIAL_HASHING
pitiot's avatar
maj  
pitiot committed
116 117 118 119 120
	FaceAttribute<PFP::BOOLATTRIB> subdivisableFace ;

	PFP::TAB_AGENTVECT agentvect ;
	PFP::TAB_AGENTVECT neighborAgentvect ;
	PFP::TAB_OBSTACLEVECT neighborObstvect;
Pierre Kraemer's avatar
Pierre Kraemer committed
121
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
122

David Cazier's avatar
David Cazier committed
123
	PFP::TAB_OBSTACLEVECT obstvect ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125


Pierre Kraemer's avatar
Pierre Kraemer committed
126
#ifndef SPATIAL_HASHING
127
	static const unsigned int nbAgentsToSubdivide = 5 ;
Jund Thomas's avatar
Jund Thomas committed
128
	static const unsigned int nbAgentsToSimplify = 4 ;
David Cazier's avatar
David Cazier committed
129

pitiot's avatar
maj  
pitiot committed
130 131
	CellMarker<FACE> refineMark ;
	CellMarker<FACE> coarsenMark ;
132 133
	std::vector<Dart> refineCandidate ;
	std::vector<Dart> coarsenCandidate ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134

135
	void clearUpdateCandidates() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138
#endif

#ifdef SPATIAL_HASHING
David Cazier's avatar
David Cazier committed
139 140 141 142 143 144 145 146 147
	unsigned int agentGridSize(unsigned int i)
	{
		return geometry.size(i) / minCellSize ;
	}

	unsigned int obstacleGridSize(unsigned int i)
	{
		return geometry.size(i) / obstacleDistance ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
148

pitiot's avatar
maj  
pitiot committed
149
	Geom::Vec2ui agentPositionCell(Agent* a) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150

pitiot's avatar
maj  
pitiot committed
151
	Geom::Vec2ui obstaclePositionCell(VEC3 pos)
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	{
David Cazier's avatar
David Cazier committed
153 154
		VEC3 relativePos = pos - geometry.min() ;
		relativePos /= obstacleDistance ;
155
		return Geom::Vec2ui(relativePos[0], relativePos[1]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157
	}

pitiot's avatar
maj  
pitiot committed
158 159 160 161 162
	const std::vector<Agent*>& getNeighbors(Geom::Vec2ui c) {
		assert(ht_agents.hasData(c)) ;
		return ht_agents[c];
	}
	const std::vector<Agent*>& getNeighbors(Agent* a) ;
David Cazier's avatar
David Cazier committed
163
	const std::vector<Agent*>& getNeighborAgents(Agent* a) ;
pitiot's avatar
maj  
pitiot committed
164 165 166 167 168 169 170

	void addAgentInGrid(Agent* a) ;
	void addAgentInGrid(Agent* a, Geom::Vec2ui c) ;

	void removeAgentFromGrid(Agent* a) ;
	void removeAgentFromGrid(Agent* a, Geom::Vec2ui c) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
171
	HashTable2D< std::vector<Agent*> > ht_agents ;
David Cazier's avatar
David Cazier committed
172
	HashTable2D< std::vector<Agent*> > ht_neighbor_agents ;
Pierre Kraemer's avatar
Pierre Kraemer committed
173 174
	HashTable2D< std::vector<Obstacle*> > ht_obstacles ;
#endif
175
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176

pitiot's avatar
maj  
pitiot committed
177 178
void update_registration(Obstacle * o);
void register_pop(Obstacle* o, int n);
179 180 181
void resetPartSubdiv(Obstacle* o);
void resetObstPartInFace(Obstacle* o, Dart d);// empeche de viser une dart ayant disparu
void resetPart(MovingObstacle * mo, Dart d); // empeche de viser une dart ayant disparu pour les voisins
pitiot's avatar
maj  
pitiot committed
182 183
void displayMO(Obstacle * o);

Pierre Kraemer's avatar
Pierre Kraemer committed
184
/**************************************
David Cazier's avatar
David Cazier committed
185 186
 *           INLINE FUNCTIONS          *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
187

188 189 190 191 192 193 194
template <typename T>
inline void addElementToVector(std::vector<T>& a, T ag)
{
	assert(std::find(a.begin(), a.end(), ag) == a.end());
	a.push_back(ag) ;
}

David Cazier's avatar
David Cazier committed
195
template <typename T>
pitiot's avatar
maj  
pitiot committed
196
inline bool removeElementFromVector(std::vector<T>& a, T ag)
Pierre Kraemer's avatar
Pierre Kraemer committed
197
{
198 199
	assert(std::find(a.begin(), a.end(), ag) != a.end());

200
	typename std::vector<T>::iterator end = a.template end() ;
David Cazier's avatar
David Cazier committed
201 202 203 204
	for (typename std::vector<T>::iterator it = a.begin(); it != end; ++it)
	{
		if (*it == ag)
		{
205 206
			*it = a.back() ;
			a.pop_back() ;
pitiot's avatar
maj  
pitiot committed
207
			return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
208 209
		}
	}
pitiot's avatar
maj  
pitiot committed
210
	return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213
}

#ifndef SPATIAL_HASHING
David Cazier's avatar
David Cazier committed
214 215
inline unsigned int EnvMap::totalNeighborSize(Dart d)
{
216 217 218 219
	return agentvect[d].size() + neighborAgentvect[d].size() ;
}

inline void EnvMap::nbAgentsIncrease(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
220
{
221 222
	if (refineMark.isMarked(d)) return ;
	if (totalNeighborSize(d) < nbAgentsToSubdivide) return ;
Pierre Kraemer's avatar
Pierre Kraemer committed
223

224
	std::pair<bool, bool>& sf = subdivisableFace[d] ;
David Cazier's avatar
David Cazier committed
225 226
	if (sf.first == false || (sf.first == true && sf.second))
	{
227 228 229
		refineMark.mark(d) ;
		refineCandidate.push_back(d) ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231
}

232
inline void EnvMap::nbAgentsDecrease(Dart d)
Thomas's avatar
Thomas committed
233
{
234 235
	if (coarsenMark.isMarked(d) || refineMark.isMarked(d)) return ;
	if (totalNeighborSize(d) > nbAgentsToSimplify) return ;
Thomas's avatar
Thomas committed
236

237 238 239
	coarsenMark.mark(d) ;
	coarsenCandidate.push_back(map.faceOldestDart(d)) ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241

inline void EnvMap::pushAgentInCells(Agent* agent, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
242
{
David Cazier's avatar
David Cazier committed
243
	assert(map.getCurrentLevel() == map.getMaxLevel()) ;
pitiot's avatar
maj  
pitiot committed
244
//	assert(std::find(agentvect[d].begin(), agentvect[d].end(), agent) == agentvect[d].end());
David Cazier's avatar
David Cazier committed
245

246 247
	addElementToVector<Agent*>(agentvect[d],agent);
//	agentvect[d].push_back(agent) ;
248
//	nbAgentsIncrease(d);
David Cazier's avatar
David Cazier committed
249 250

	Dart dd = d ;
David Cazier's avatar
David Cazier committed
251 252
	do
	{
David Cazier's avatar
David Cazier committed
253
		Dart ddd = map.alpha1(map.alpha1(dd)) ;
David Cazier's avatar
David Cazier committed
254 255
		while (ddd != dd)
		{
256 257
			addElementToVector<Agent*>(neighborAgentvect[ddd],agent);
//			neighborAgentvect[ddd].push_back(agent) ;
258
//			nbAgentsIncrease(ddd);
David Cazier's avatar
David Cazier committed
259
			ddd = map.alpha1(ddd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
260
		}
David Cazier's avatar
David Cazier committed
261 262
		dd = map.phi1(dd) ;
	} while (dd != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
263 264
}

Thomas's avatar
Thomas committed
265 266
inline void EnvMap::popAgentInCells(Agent* agent, Dart d)
{
David Cazier's avatar
David Cazier committed
267 268 269
	assert(map.getCurrentLevel() == map.getMaxLevel()) ;

	removeElementFromVector<Agent*>(agentvect[d], agent) ;
270
//	nbAgentsDecrease(d) ;
David Cazier's avatar
David Cazier committed
271 272

	Dart dd = d ;
David Cazier's avatar
David Cazier committed
273 274
	do
	{
David Cazier's avatar
David Cazier committed
275
		Dart ddd = map.alpha1(map.alpha1(dd)) ;
David Cazier's avatar
David Cazier committed
276 277
		while (ddd != dd)
		{
David Cazier's avatar
David Cazier committed
278
			removeElementFromVector<Agent*>(neighborAgentvect[ddd], agent) ;
279
//			nbAgentsDecrease(ddd) ;
David Cazier's avatar
David Cazier committed
280
			ddd = map.alpha1(ddd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
281
		}
David Cazier's avatar
David Cazier committed
282 283
		dd = map.phi1(dd) ;
	} while (dd != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
284 285
}

pitiot's avatar
maj  
pitiot committed
286 287 288

//ajout moving obst///////////////////////////////////////////////////////////////////////////////////////////////////////

289

290
inline void EnvMap::addObstAsNeighbor (Obstacle *  o, const std::vector<Dart>& belonging_cells, std::vector<Dart> *neighbor_cells)
pitiot's avatar
maj  
pitiot committed
291 292
{
	assert(map.getCurrentLevel() == map.getMaxLevel());
293
	assert(!belonging_cells.empty());
pitiot's avatar
maj  
pitiot committed
294
	neighbor_cells->clear();
295 296
	CellMarkerMemo<FACE> memo_mark(map);
	CellMarkerMemo<FACE> OneRingMark(map);
pitiot's avatar
maj  
pitiot committed
297

298 299
	for (std::vector<Dart>::const_iterator it =belonging_cells.begin();it<belonging_cells.end();++it)
		memo_mark.mark(*it);
pitiot's avatar
maj  
pitiot committed
300

301
	std::vector<Dart>::const_iterator it=belonging_cells.begin();
pitiot's avatar
maj  
pitiot committed
302 303 304 305
	Dart beg = NIL;
	Dart first =NIL;
	Dart d=NIL;
	Dart dd=NIL;
306
//	CGoGNout<<"beg : "<<(*it)<<CGoGNendl;
pitiot's avatar
maj  
pitiot committed
307 308 309 310 311 312 313 314 315
	//boucle pour trouver une face du voisinage de l'obstacle ne contenant pas l'obstacle
	do
	{
		beg = *it;
		first = NIL;
		d=beg;
		do {
			dd=map.alpha1(map.alpha1(d));
			do {
316
				if (!memo_mark.isMarked(dd))
pitiot's avatar
maj  
pitiot committed
317 318 319 320 321 322 323 324 325 326 327 328
				{
					first = dd;
				}
				dd=map.alpha1(dd);
			}while (first==NIL && dd!=d);

			d=map.phi1(d);

		} while(first==NIL && d!=beg);
		++it;
	}while(first==NIL && it!=belonging_cells.end());
	assert(!buildingMark.isMarked(d)) ;
329 330
	assert(first!=NIL) ;
//	CGoGNout<<"first : "<<first<<CGoGNendl;
pitiot's avatar
maj  
pitiot committed
331 332 333 334 335 336 337 338 339
	d=first;
	do
	{
		if (!OneRingMark.isMarked(d))
		{
			OneRingMark.mark(d);
			(*neighbor_cells).push_back(d);
		}

340
		find_next(o,&d, memo_mark);
341 342 343 344 345 346 347 348 349 350
		if(d==NIL)
		{
			CGoGNout<<"cellule de début : "<<first<<CGoGNendl;
			CGoGNout<<"cellules markées OneRing : "<<CGoGNendl;
			std::vector<Dart> v=OneRingMark.get_markedCells();
			for(std::vector<Dart>::iterator it=v.begin();it<v.end();++it)
			{
				CGoGNout<<(*it).index<<CGoGNendl;
			}
		}
351
//		CGoGNout<<"d : "<<d<<CGoGNendl;
pitiot's avatar
maj  
pitiot committed
352 353 354 355 356
	}while(!map.sameFace(d,first));
}

// find_next cherche la prochaine case "voisine" d'un obstacle faisant parti d'un movingobstacle (algo de parcours du one-ring )

357
inline void EnvMap::find_next(Obstacle* o,Dart * ddd, CellMarkerMemo<FACE>& cms)
pitiot's avatar
maj  
pitiot committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	Dart d=*ddd;
	Dart first = NIL;
	Dart dd;
	int rd =0;
	do{
		rd++;
		dd=map.alpha1((d));

		if (!(cms.isMarked(dd)))
		{
			first = dd;
		}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

		if(rd>100)
		{
			if (rd>10000)
			{
				displayMO(o);
				CGoGNout <<(first==NIL)<< " infini find next d : "<< d<<"phi 2 markée ? : "<<cms.isMarked(map.phi2(d))<<CGoGNendl;
				CGoGNout<<"cellules markées : "<<CGoGNendl;
				std::vector<Dart> v=cms.get_markedCells();
				for(std::vector<Dart>::iterator it=v.begin();it<v.end();++it)
				{
					CGoGNout<<(*it).index<<CGoGNendl;
				}
				break;
			}
			CGoGNout <<(first==NIL)<< " infini find next d : "<< d<<"phi 2 markée ? : "<<cms.isMarked(map.phi2(d))<<CGoGNendl;
			CGoGNout<<"cellules markées : "<<CGoGNendl;
							std::vector<Dart> v=cms.get_markedCells();
							for(std::vector<Dart>::iterator it=v.begin();it<v.end();++it)
							{
								CGoGNout<<(*it).index<<CGoGNendl;
							}
pitiot's avatar
maj  
pitiot committed
394
		}
395
		d=map.phi_1(d);
pitiot's avatar
maj  
pitiot committed
396 397 398 399 400 401 402 403
	}while(first==NIL);
	*ddd=first;
}

inline void EnvMap::pushObstNeighborInCells(Obstacle* o, Dart d)
{
	assert(map.getCurrentLevel() == map.getMaxLevel());

404
	addElementToVector<Obstacle*>(neighborObstvect[d],o);
pitiot's avatar
maj  
pitiot committed
405 406
}

407 408 409 410
//inline void EnvMap::pushObstacleInCell(Obstacle* o, Dart d)
//{
//	assert(map.getCurrentLevel() == map.getMaxLevel()) ;
//	assert(!buildingMark.isMarked(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
411
//
412 413
//	addElementToVector<Obstacle*>(obstvect[d],o);
//}
414

Pierre Kraemer's avatar
Pierre Kraemer committed
415 416
inline void EnvMap::clearUpdateCandidates()
{
David Cazier's avatar
David Cazier committed
417 418
	refineCandidate.clear() ;
	coarsenCandidate.clear() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
419
}
pitiot's avatar
maj  
pitiot committed
420 421
///addMovingObstAsNeighbor est pour détecter les voisins du movingobstacle global afin de le considerer comme un super agent

Pierre Kraemer's avatar
Pierre Kraemer committed
422
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
423 424

#endif