env_generator.hpp 23.1 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
#include "Algo/Modelisation/subdivision.h"
#include "Geometry/inclusion.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
3
#include "Topology/generic/traversorCell.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
4
5
6
7
8
9
10

namespace CGoGN
{

namespace CityGenerator
{

Pierre Kraemer's avatar
Pierre Kraemer committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename PFP>
bool notAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildingMark)
{
	Dart dd = d;
	do
	{
		if(buildingMark.isMarked(dd))
			return false;
		dd = map.phi1(dd);
	} while(dd != d);

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
template <typename PFP>
bool notDiagonalAdjacentToAnObstacle(typename PFP::MAP& map, Dart d, CellMarker& buildingMark)
{
	Dart dd = d;
	do
	{
		if(buildingMark.isMarked(map.alpha1(map.alpha1(dd))))
			return false;
		dd = map.phi1(dd);
	} while(dd != d);

	return true;	
}

template <typename PFP>
Algo::Modelisation::Polyhedron<PFP> generateGrid(typename PFP::MAP& map, typename PFP::TVEC3& position, unsigned int cX, unsigned int cY, float sideLength, CellMarker& obstacleMark, CellMarker& buildingMark)
{
	Algo::Modelisation::Polyhedron<PFP> prim(map, position);
	prim.grid_topo(cX, cY);
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
	Dart boundary;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
48
		if(map.isBoundaryMarked(map.phi2(d)))
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
		{
			obstacleMark.mark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
51
			boundary = map.phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
52
53
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	buildingMark.mark(boundary);
Pierre Kraemer's avatar
Pierre Kraemer committed
55
56
57
58
59
60

	prim.embedGrid(sideLength * cX, sideLength * cY);

	return prim;
}

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
154
155
156
157
158
template <typename PFP>
Algo::Modelisation::Polyhedron<PFP> generateTrianGrid(typename PFP::MAP& map, typename PFP::TVEC3& position, unsigned int cX, unsigned int cY, float sideLength, CellMarker& obstacleMark, CellMarker& buildingMark)
{
	Algo::Modelisation::Polyhedron<PFP> prim(map, position);
	prim.grid_topo(cX, cY);


	prim.embedGrid(sideLength * cX, sqrt(sideLength*sideLength*3.0f/4.0f) * cY);

	Dart dY = prim.getDart(); //remind the first quad of the line
	Dart dX = prim.getDart(); //goes through the line
	bool odd=true; //odd line or not
	for(unsigned int i=0;i<cX*cY; )
	{
		Dart dNext = map.phi1(map.phi2(map.phi1(dX)));

		Dart toCut = dX;
		if(odd)
		{
			toCut = map.phi1(toCut); //change the side of the split face
			position[toCut][0]-= sideLength/2.0f; //move vertices for equilateral triangles
		}

		map.splitFace(toCut,map.phi1(map.phi1(toCut)));

		++i;

		if(i%cX==0 && i>0) //goes up and change side of split
		{
			Dart endSquare = map.newOrientedFace(3); //add triangle add end of lines to make a square
			Dart dN;
			if(odd)
			{
				dN = map.phi1(map.phi2(map.phi1(dX)));
				map.sewFaces(dN,endSquare);
				position[map.phi_1(endSquare)] = position[map.phi1(endSquare)];
				position[map.phi_1(endSquare)][0]+= sideLength/2.0f;
			}
			else
			{
				dN = map.phi1(dX);
				map.sewFaces(dN,endSquare);
				position[map.phi_1(endSquare)] = position[endSquare];
			}

			if(odd)
				dY = map.phi2(map.phi_1(map.phi2(map.phi1(dY))));
			else
				dY = map.phi2(map.phi1(map.phi2(map.phi_1(dY))));

			dX = dY;
			odd=!odd;
		}
		else
			dX = dNext;
	}

	Dart boundary;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(map.phi2(d) == d)
		{
			Dart dA = map.alpha1(map.phi1(d));
			if(map.phi2(dA)==dA && position[dA]==position[map.phi1(d)] && position[map.phi1(dA)]==position[d])
				map.sewFaces(dA,d);
			else
			{
				obstacleMark.mark(d);
				boundary = d;
			}
		}
	}

	map.closeHole(boundary);
	buildingMark.mark(map.phi2(boundary));


	if(odd) //last top line if odd
	{
		for(unsigned int i=0;i<cX; ++i)
		{
			dX = map.phi1(dX);
			position[dX][0]-= sideLength/2.0f;
		}
	}

	//add hexagons
//	for(Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		if(map.vertexDegree(d)==6)
//		{
//			map.deleteVertex(d);
//		}
//	}

	return prim;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
template <typename PFP>
Dart extrudeFaceAndMark(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, CellMarker& buildingMark, float height)
{
	Dart dRoof = Algo::Modelisation::extrudeFace<PFP>(map, position, d, height);
	buildingMark.mark(dRoof);
	Dart dd = dRoof;
	do
	{
		buildingMark.mark(map.phi2(dd));
		dd = map.phi1(dd);
	} while(dd != dRoof);
	return dRoof;
}

template <typename PFP>
174
bool animateCity(EnvMap* envMap)
Pierre Kraemer's avatar
Pierre Kraemer committed
175
{
176
177
178
179
180
181
182
183
	typename PFP::MAP& map = envMap->map;
	typename PFP::TVEC3& position = envMap->position;
	typename PFP::TAB_AGENTVECT& agents = envMap->agentvect;
	typename PFP::TAB_AGENTVECT& agentNeighbors = envMap->neighborAgentvect;
	CellMarker& obstacleMark = envMap->obstacleMark;
	CellMarker& buildingMark = envMap->buildingMark;
	std::vector<Dart>& newBuildings = envMap->newBuildings;

Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
	unsigned int state = rand() % 10;
	if(state < 2)
186
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
187
188
		map.setCurrentLevel(0) ;

189
		//generate new building
Pierre Kraemer's avatar
Pierre Kraemer committed
190
		Dart d(rand() % map.getNbDarts());
191
192
		if(!map.isDartValid(d))
			map.next(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
193
194
		while(d != map.end() && map.getDartLevel(d) > 0)
			map.next(d);
195

Pierre Kraemer's avatar
Pierre Kraemer committed
196
		if(d != map.end())
197
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			if(
				!buildingMark.isMarked(d) &&
				agents[d].size() == 0 &&
				agentNeighbors[d].size() == 0 &&
				!map.faceIsSubdivided(d) &&
				notAdjacentToAnObstacle<PFP>(map,d,obstacleMark) &&
				notDiagonalAdjacentToAnObstacle<PFP>(map,d,obstacleMark)
			)
			{
				map.setCurrentLevel(map.getMaxLevel()) ;
				d = generateBuilding<PFP>(envMap, d, 2.0f, 0);
				newBuildings.push_back(d);
				return true;
			}
212
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
213
214

		map.setCurrentLevel(map.getMaxLevel()) ;
215
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
216
	else if(state < 6 && newBuildings.size() > 0)
217
218
219
220
221
222
	{
	//take a newBuilding and make it higher
		state = rand()%newBuildings.size();
		Dart d = newBuildings[state];
		//decide if we create a new floor, or just make it higher
		unsigned int typeOfUpdate = rand()%10;
Pierre Kraemer's avatar
Pierre Kraemer committed
223
		if(typeOfUpdate < 9)
224
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
225
			Dart dd = d;
226
227
228
229
			do
			{
				position[dd][2] += 2.0f;
				dd = map.phi1(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
230
			} while(dd != d);
231

Pierre Kraemer's avatar
Pierre Kraemer committed
232
			if(position[dd][2] > ((10+rand()%10)*10.0f))
233
234
235
236
237
238
239
240
241
242
243
				newBuildings.erase(newBuildings.begin()+state);
		}
		else
		{
			typename PFP::VEC3 c = Algo::Geometry::faceCentroid<PFP>(map,d,position);
			Dart dRoofSmall = Algo::Modelisation::extrudeFace<PFP>(map,position,d,0.0f);
			Dart dd = dRoofSmall;
			do
			{
				position[dd] = position[dd] + (c-position[dd])/3.0f;
				dd = map.phi1(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
244
			} while (dd != dRoofSmall);
245
//			Dart dRoof = Algo::Modelisation::extrudeFace<PFP>(map,position,dRoofSmall,2.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
246
			Dart dRoof = extrudeFaceAndMark<PFP>(map, position, dRoofSmall, buildingMark, 2.0f);
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

			newBuildings.erase(newBuildings.begin()+state);
			newBuildings.push_back(dRoof);
		}
	}

	return false;
}

//template <typename PFP>
//Dart generateBuilding(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, float height, unsigned int buildingType, CellMarker& obstacleMark, CellMarker& buildingMark)


//template <typename PFP>
//void generateCity(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize, unsigned int nbSquares)

template <typename PFP>
Dart generateBuilding(EnvMap* envMap, Dart d, float height, unsigned int buildingType)
Pierre Kraemer's avatar
Pierre Kraemer committed
265
{
266
267
268
269
270
	typename PFP::MAP& map = envMap->map;
	typename PFP::TVEC3& position = envMap->position;
	CellMarker& obstacleMark = envMap->obstacleMark;
	CellMarker& buildingMark = envMap->buildingMark;

Pierre Kraemer's avatar
Pierre Kraemer committed
271
272
	// mark the face as obstacle before extrusion
	Dart dd = d;
Thomas's avatar
Thomas committed
273
	buildingMark.mark(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
274
275
276
	do
	{
		obstacleMark.mark(dd);
277
278
279
280
281
282
283

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

		envMap->pushObstacleInCells(o, map.phi2(dd));

Pierre Kraemer's avatar
Pierre Kraemer committed
284
285
286
		dd = map.phi1(dd);
	} while(dd != d);

Pierre Kraemer's avatar
Pierre Kraemer committed
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
342
343
	Dart dRoof;
	dRoof = extrudeFaceAndMark<PFP>(map, position, d, buildingMark, height);

	switch(buildingType)
	{
		case 0 :
		{
			break;
		}
		case 1 :
		{
			dRoof = extrudeFaceAndMark<PFP>(map, position, dRoof, buildingMark, height/3);
			Dart dNext = map.phi1(dRoof);
			Dart dPrev = map.phi2(map.phi_1(dRoof));
			map.collapseEdge(dNext);
			map.collapseEdge(dPrev);
			break;
		}
		case 2 :
		{
			dRoof = extrudeFaceAndMark<PFP>(map, position, dRoof, buildingMark, height/3);
			Dart dNext = map.phi1(dRoof);
			Dart dPrev = map.phi2(map.phi_1(dRoof));
			typename PFP::VEC3 mid1 = (position[dNext] + position[map.phi1(dNext)]) / 2.0f;
			typename PFP::VEC3 mid2 = (position[dPrev] + position[map.phi1(dPrev)]) / 2.0f;
			map.collapseEdge(dNext);
			map.collapseEdge(dPrev);
			position[dRoof] = mid2;
			position[map.phi1(dRoof)] = mid1;
			break;
		}
		case 3 :
		{
			unsigned int nbStairs = rand() % 5;
			for(unsigned int i = 0; i < nbStairs; ++i)
			{
				typename PFP::VEC3 c = Algo::Geometry::faceCentroid<PFP>(map, dRoof, position);
				Dart dRoofSmall = extrudeFaceAndMark<PFP>(map, position, dRoof, buildingMark, 0.0f);
				Dart dd = dRoofSmall;
				do
				{
					position[dd] = position[dd] + (c - position[dd]) / 3.0f;
					dd = map.phi1(dd);
				} while (dd != dRoofSmall);
				dRoof = extrudeFaceAndMark<PFP>(map, position, dRoofSmall, buildingMark, height / 2.0f);
			}
			bool spike= rand() % 2;
			if(spike)
			{
				typename PFP::VEC3 c = Algo::Geometry::faceCentroid<PFP>(map, dRoof, position);
				c[2] += height / 1.5f;
				dRoof = Algo::Modelisation::trianguleFace<PFP>(map, dRoof);
				position[dRoof] = c;
			}
			break;
		}
	}
344
345

	return dRoof;
Pierre Kraemer's avatar
Pierre Kraemer committed
346
347
348
}

template <typename PFP>
349
void generateCity(EnvMap* envMap, float sideSize, unsigned int nbSquares)
Pierre Kraemer's avatar
Pierre Kraemer committed
350
{
351
352
353
354
355
	typename PFP::MAP& map = envMap->map;
	typename PFP::TVEC3& position = envMap->position;
	CellMarker& obstacleMark = envMap->obstacleMark;
	CellMarker& buildingMark = envMap->buildingMark;

Pierre Kraemer's avatar
Pierre Kraemer committed
356
357
358
359
360
361
362
363
	unsigned int nbBuilding = 1000;
	float height = sideSize / 2.0f;

	generateGrid<PFP>(map, position, nbSquares, nbSquares, sideSize, obstacleMark, buildingMark);

	Dart dEnd = map.end();
	for(Dart d = map.begin(); d != dEnd && nbBuilding > 0; map.next(d))
	{
Pierre Kraemer's avatar
yeah..    
Pierre Kraemer committed
364
		if(!buildingMark.isMarked(d) && (rand() % 12 == 0) && notDiagonalAdjacentToAnObstacle<PFP>(map, d, buildingMark))
Pierre Kraemer's avatar
Pierre Kraemer committed
365
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
366
367
368
369
370
371
372
373
374
375
//			typename PFP::VEC3 p = position[d];
//
//			float sideSize = 70.0f;
//			unsigned int nbSquares = 24;
//			unsigned int nbEmpty = 3;
//
//			int emptySquareX = (nbSquares * sideSize / 2.0f) - (nbEmpty * sideSize) - 10;
//			int emptySquareY = (nbSquares * sideSize / 2.0f) - (nbEmpty * sideSize) - 10;
//			if(! ((p[0] < -emptySquareX && p[1] < -emptySquareY) || (p[0] > emptySquareX && p[1] > emptySquareY)) )
//			{
376
				generateBuilding<PFP>(envMap, d, (1+(rand()%3)) * height / 2.0f, rand() % 4);
Pierre Kraemer's avatar
yeah..    
Pierre Kraemer committed
377
				--nbBuilding;
Pierre Kraemer's avatar
Pierre Kraemer committed
378
//			}
Pierre Kraemer's avatar
Pierre Kraemer committed
379
380
381
382
		}
	}
}

383
384
385
386
387
388
389
390
391
392
393
template <typename PFP>
void generateMall(typename PFP::MAP& map,typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize)
{
	unsigned int side = 5;
	std::vector<Algo::Modelisation::Polyhedron<PFP> > floors;

	Algo::Modelisation::Polyhedron<PFP> * floor2 = new Algo::Modelisation::Polyhedron<PFP>(map,position);
	floor2->grid_topo(side,side);

	floor2->embedGrid(sideSize*side,sideSize*side);

Thomas's avatar
Thomas committed
394
	for(unsigned int i=0;i<3;++i)
395
396
397
	{
		float floorHeight = 100;
		typename PFP::VEC3 transl(0,0,floorHeight);
Pierre Kraemer's avatar
Pierre Kraemer committed
398
		CellMarker m(map, VERTEX) ;
399
400
401
402
403
404
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

		for(Dart d=map.begin();d!=map.end();map.next(d))
		{
			if(!m.isMarked(d))
			{
				m.mark(d);
				position[d] += transl;
			}
		}

		Algo::Modelisation::Polyhedron<PFP> * floor1 = new Algo::Modelisation::Polyhedron<PFP>(map,position);
		floor1->grid_topo(side,side);
		floor1->embedGrid(sideSize*side,sideSize*side);

		Dart d1 = map.template phi<121>(floor1->getDart());
		Dart d2 = map.template phi<121121121>(floor2->getDart());

		generatePathToUpperStair<PFP>(map,position,d1,d2);

		floor2=floor1;
	}

	Dart boundary;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(map.phi2(d) == d)
		{
			obstacleMark.mark(d);
			boundary = d;
		}
	}

	map.closeHole(boundary);
	buildingMark.mark(map.phi2(boundary));

Thomas's avatar
Thomas committed
434
	installGuardRail<PFP>(map,position,obstacleMark,buildingMark,sideSize/10.0f);
435
436
437
438
439
440
441
442
443
444
445
}

template <typename PFP>
void generatePathToUpperStair(typename PFP::MAP& map,typename PFP::TVEC3& position, Dart dLower,Dart dUpper)
{
	if(dLower!= map.phi2(dLower) || dUpper!=map.phi2(dUpper)) {
		std::cout << "generatePathToUpperStair : lower and upper stair darts must be fixpoint in phi2" << std::endl;
		return;
	}

	//create the path
Pierre Kraemer's avatar
Pierre Kraemer committed
446
447
448
449
450
	Dart dPathUp = map.newFace(4);
	Dart dInBetweenUp = map.newFace(4);
	Dart dInBetweenDown = map.newFace(4);
	Dart dInBetweenMid = map.newFace(4);
	Dart dPathDown = map.newFace(4);
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495

	//sew the path
	map.sewFaces(dUpper,map.phi1(map.phi1(dPathUp)));
	map.sewFaces(dPathUp,map.phi1(map.phi1(dInBetweenUp)));
	map.sewFaces(dLower,map.phi1(map.phi1(dPathDown)));
	map.sewFaces(dPathDown,map.phi1(map.phi1(dInBetweenDown)));


	map.sewFaces(map.phi1(dInBetweenDown),map.phi_1(dInBetweenMid));
	map.sewFaces(map.phi_1(dInBetweenUp),map.phi1(dInBetweenMid));

	//embed the inBetween floor
	float z = (position[dLower][2]+position[dUpper][2])/2.0f;

	//from lower path
	typename PFP::VEC3 vDown = position[map.phi1(dLower)]-position[map.phi1(map.phi1(dLower))];

	position[map.phi1(map.phi1(dInBetweenDown))] = position[map.phi1(dLower)] + vDown;
	position[map.phi_1(dInBetweenDown)] =  position[dLower] + vDown;

	position[dInBetweenDown] = position[map.phi_1(dInBetweenDown)]+vDown;
	position[map.phi1(dInBetweenDown)] = position[map.phi1(map.phi1(dInBetweenDown))]+vDown;

	//from upper path
	position[map.phi1(map.phi1(dInBetweenUp))] = position[map.phi1(dUpper)] + vDown;
	position[map.phi_1(dInBetweenUp)] =  position[dUpper] + vDown;

	position[dInBetweenUp] = position[map.phi_1(dInBetweenUp)]+vDown;
	position[map.phi1(dInBetweenUp)] = position[map.phi1(map.phi1(dInBetweenUp))]+vDown;

	Dart dd = dInBetweenUp;
	do
	{
		position[dd][2] = z;
		dd = map.phi1(dd);
	} while(dd!=dInBetweenUp);

	dd = dInBetweenDown;
	do
	{
		position[dd][2] = z;
		dd = map.phi1(dd);
	} while(dd!=dInBetweenDown);
}

Thomas's avatar
Thomas committed
496
497
498
499
500
501
502
503
504
template <typename PFP>
void generatePlanet(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float radius, unsigned int nbSquares)
{
	Algo::Modelisation::Polyhedron<PFP> prim(map, position);
	prim.cylinder_topo(nbSquares, nbSquares, true, true);

	prim.embedSphere(radius);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
505
506
507
508
509
510
template <typename PFP>
void simplifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark)
{
	typedef typename PFP::VEC3 VEC3;
	typedef std::multimap<float, Dart> VEC_D_A;
	
Pierre Kraemer's avatar
Pierre Kraemer committed
511
512
	AutoAttributeHandler<float> tableArea(map, FACE);
	AutoAttributeHandler<CGoGN::NoMathIONameAttribute<VEC_D_A::iterator> > tableIt(map, EDGE);
Pierre Kraemer's avatar
Pierre Kraemer committed
513
514
515
516
517
	
	VEC_D_A orderedD;
	
	// compute all heuristic and construct multimap
	Algo::Geometry::computeAreaFaces<PFP>(map, position, tableArea);
Pierre Kraemer's avatar
Pierre Kraemer committed
518
	CellMarker eM(map, EDGE);
Pierre Kraemer's avatar
Pierre Kraemer committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!eM.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d))
		{
			eM.mark(d);
			tableIt[d] = orderedD.insert(std::make_pair(tableArea[d] + tableArea[map.phi2(d)], d));
		}
	}
	eM.unmarkAll();
	
	bool stillSimplif = true;
	VEC_D_A::iterator it = orderedD.begin();
	if(it == orderedD.end())
		stillSimplif = false;
	
	while(stillSimplif)
	{
  		Dart d = it->second;
		if(!map.sameFace(d, map.phi2(d)))
		{
			// remove selected edge
			float sumArea = it->first;
			orderedD.erase(it);
			Dart dd = map.phi1(d);
			map.mergeFaces(d);
			tableArea[dd] = sumArea;
			
			// erase all edges of new face from multimap
			Dart ddd = dd ;
			do
			{
				if(!eM.isMarked(ddd) && !obstacleMark.isMarked(ddd))
				{
					eM.mark(ddd);
					orderedD.erase(tableIt[ddd]);
				}
				ddd = map.phi1(ddd);
			} while(ddd != dd);

			// collapse edges in faces if possible
			ddd = dd;
			do
			{
				Dart dN = map.phi1(ddd);
				
				// remove pending edges
				if(map.phi2(dN) == map.phi1(dN))
				{
					if(dN == dd || map.phi1(dN) == dd)
						dd = map.phi1(map.phi1(dN));
//					VEC3 p = position[dN];
					map.collapseEdge(dN);
					dN = map.phi1(ddd);
//					position[dN] = p;
				}

				// simplify vertices of valence 2 -> check if no intersection is created
				if(map.alpha1(map.alpha1(dN)) == dN && !obstacleMark.isMarked(dN))
				{
					if(dN == dd || dN == map.phi2(dd))
						dd = map.phi1(dd);
					VEC3 p = position[map.phi1(dN)];

					bool intersect = false;
					Dart dI = map.phi1(map.phi1(dN));
					VEC3 pA = position[map.phi_1(dN)];
					VEC3 pB = position[map.phi1(dN)];
					do
					{ 
						VEC3 p1 = position[dI];
						VEC3 p2 = position[map.phi1(dI)];
						if(	Geom::testOrientation2D(p1, pA, pB) != Geom::testOrientation2D(p2, pA, pB) &&
							Geom::testOrientation2D(pA, p1, p2) != Geom::testOrientation2D(pB, p1, p2)
						)
						{
							if(	!Geom::arePointsEquals(p1, pA) && !Geom::arePointsEquals(p1, pB) &&
								!Geom::arePointsEquals(p2, pA) && !Geom::arePointsEquals(p2, pB)
							)
								intersect = true;
						}
						dI = map.phi1(dI);
					} while(dI != map.phi_1(dN));

					if(!intersect)
					{
						dN = map.phi2(dN);
						Dart dI = map.phi1(map.phi1(dN));
						do
						{
							VEC3 v;
							VEC3 p1 = position[dI];
							VEC3 p2 = position[map.phi1(dI)];
							if(	Geom::testOrientation2D(p1, pA, pB)!= Geom::testOrientation2D(p2, pA, pB) &&
								Geom::testOrientation2D(pA, p1, p2)!= Geom::testOrientation2D(pB, p1, p2)
							)
							{
								if(	!Geom::arePointsEquals(p1, pA) && !Geom::arePointsEquals(p1, pB) &&
									!Geom::arePointsEquals(p2, pA) && !Geom::arePointsEquals(p2, pB)
								)
									intersect = true;
							}
							dI = map.phi1(dI);
						}while(dI != map.phi_1(dN));
					}

					if(!intersect)
					{
						map.collapseEdge(dN);
						position[map.phi1(ddd)] = p;
					}
				}

				ddd = map.phi1(ddd);
			} while(ddd != dd);

			ddd = dd;
			do
			{
				if(eM.isMarked(ddd))
				{
					eM.unmark(ddd);
					tableIt[ddd] = orderedD.insert(std::make_pair(tableArea[ddd] + tableArea[map.phi2(ddd)], ddd));
				}
				ddd = map.phi1(ddd);
			} while(ddd != dd);
			
			it = orderedD.begin();
		}
		else
		{
			++it;
		}
		
		if(it == orderedD.end())
			stillSimplif = false;
	}

656
	convexifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673

	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(buildingMark.isMarked(d) && !buildingMark.isMarked(map.phi2(d)))
		{
			if(map.alpha1(map.alpha1(map.phi1(d)))==map.phi1(d)
					&& Geom::testOrientation2D(position[d], position[map.phi1(d)],position[map.phi1(map.phi1(d))])==Geom::ALIGNED)

			{
				VEC3 p = position[map.phi1(map.phi1(d))];
//				Dart dN = map.phi1(d);
				map.collapseEdge(map.phi1(d));
				position[map.phi1(d)] = p;
			}
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
674
675
676
677
678
679
680
681
682
	map.check();
}

template <typename PFP>
bool isAnEar(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart dd, float& area)
{
	typedef typename PFP::VEC3 VEC3;
	
	bool check = false;
Thomas's avatar
Thomas committed
683
	if(Geom::testOrientation2D(position[dd], position[map.phi_1(dd)], position[map.phi1(dd)]) == Geom::RIGHT && !map.sameFace(map.phi2(dd),map.phi2(map.phi_1(dd))))
Pierre Kraemer's avatar
Pierre Kraemer committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
	{
		VEC3 p = position[dd];
		VEC3 p1 = position[map.phi1(dd)];
		VEC3 p_1 = position[map.phi_1(dd)];
		Dart ddd = map.phi1(map.phi1(dd));
		check = true;
		do
		{
			unsigned int incl = isPointInTriangle(position[ddd], p_1, p, p1);
			if((incl == Geom::FACE_INCLUSION) || (incl == Geom::EDGE_INCLUSION))
				check = false;
			ddd = map.phi1(ddd);
		} while(check && ddd != map.phi_1(dd));

		if(check)
		{
			VEC3 v1(p1 - p);
			VEC3 v2(p - p_1);
			VEC3 v3(p_1 - p1);
			area = 1.0f / std::min(std::min(Geom::angle(v1, -1.0f*v2), Geom::angle(-1.0f*v1, v3)), Geom::angle(v2, -1.0f*v3));
//			area = 1.0f / Geom::triangleArea(p_1, p, p1);
		}
	}

	return check;
}

template <typename PFP>
bool canRemoveEdgeConvex(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, CellMarker& obstacleMark)
{
	typedef typename PFP::VEC3 VEC3;
	
	if(!obstacleMark.isMarked(d) && position[d][2] == 0.0f)
	{
		Dart d2 = map.phi2(d);
		VEC3 p1 = position[map.phi_1(d)];
		VEC3 p2 = position[d];

		VEC3 p3 = position[d2];
		VEC3 p4 = position[map.phi_1(d2)];

		VEC3 p5 = position[map.phi1(map.phi1(d))];
		VEC3 p6 = position[map.phi1(map.phi1(d2))];
		if( (p1[0] != p6[0] || p1[1] != p6[1] || p1[2] != p6[2]) &&
			(p5[0] != p4[0] || p5[1] != p4[1] || p5[2] != p4[2]) &&
			Geom::testOrientation2D<VEC3>(p6,p1,p2) != Geom::RIGHT &&
			Geom::testOrientation2D<VEC3>(p5,p4,p3) != Geom::RIGHT
		)
			return true;
	}

	return false;
}

template <typename PFP>
void convexifyFreeSpace(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark)
{
	typedef typename PFP::VEC3 VEC3;
742

Pierre Kraemer's avatar
Pierre Kraemer committed
743
	float area;
Pierre Kraemer's avatar
Pierre Kraemer committed
744
	CellMarker m(map, FACE);
Thomas's avatar
Thomas committed
745
746

	for(Dart d = map.begin();d != map.end(); map.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
	{
		if(!m.isMarked(d) && !obstacleMark.isMarked(d) && !buildingMark.isMarked(d))
		{
			std::multimap<float, Dart> moultipass;
			std::map<Dart, std::multimap<float, Dart>::iterator> liloo;

			// store ears in multimap
			Dart dd = d;
			do
			{
				if(isAnEar<PFP>(map, position, dd, area))
					liloo.insert(std::pair<Dart, std::multimap<float, Dart>::iterator>(dd, moultipass.insert(std::pair<float, Dart>(area, dd))));
				dd = map.phi1(dd);
			} while(dd != d);

			while(map.faceDegree(moultipass.begin()->second) > 3)
			{
				Dart dd = moultipass.begin()->second;

				if(liloo.find(map.phi_1(dd)) != liloo.end())
				{
Thomas's avatar
Thomas committed
768
 					moultipass.erase(liloo[map.phi_1(dd)]);
Pierre Kraemer's avatar
Pierre Kraemer committed
769
					liloo.erase(map.phi_1(dd));
Thomas's avatar
Thomas committed
770

Pierre Kraemer's avatar
Pierre Kraemer committed
771
772
				}

Thomas's avatar
Thomas committed
773
774
775
776
777
				if(moultipass.size()>0)
				{
					moultipass.erase(liloo[dd]);
					liloo.erase(dd);
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

				if(liloo.find(map.phi1(dd))!=liloo.end())
				{
					moultipass.erase(liloo[map.phi1(dd)]);
					liloo.erase(map.phi1(dd));
				}

				map.splitFace(map.phi_1(dd), map.phi1(dd));

				Dart dNext = map.alpha_1(map.phi1(dd));
				if(isAnEar<PFP>(map, position, dNext,area))
					liloo.insert(std::pair<Dart, std::multimap<float, Dart>::iterator>(dNext, moultipass.insert(std::pair<float, Dart>(area, dNext))));

				Dart dPrev = map.alpha1(map.phi_1(dd));
				if(isAnEar<PFP>(map, position, dPrev, area))
					liloo.insert(std::pair<Dart, std::multimap<float, Dart>::iterator>(dPrev, moultipass.insert(std::pair<float, Dart>(area, dPrev))));

				m.mark(dd);
			}
			m.mark(moultipass.begin()->second);
		}
	}

	//simplification
Thomas's avatar
Thomas committed
802
803
804
//	for(Dart d = map.begin(); d != map.end(); map.next(d))
//		if(canRemoveEdgeConvex<PFP>(map, position, d, obstacleMark) && canRemoveEdgeConvex<PFP>(map, position, map.phi2(d), obstacleMark))
//			map.mergeFaces(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
805
806
}

807
808
809
template <typename PFP>
void installGuardRail(typename PFP::MAP& map,typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float height)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
810
811
	CellMarker cm(map,EDGE);
	CellMarkerNoUnmark railVert(map,EDGE);
812
813
814
815
816
817
818
819
820
821
822
823
824
825
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!cm.isMarked(d))
		{
			cm.mark(d);
			if(obstacleMark.isMarked(d))
			{
				Dart dExt = map.newFace(4);
				Dart dIn = map.newFace(4);

				//mark new faces as obstacles, building and treated darts
				obstacleMark.mark(dIn);
				buildingMark.mark(dIn);
				buildingMark.mark(dExt);
Thomas's avatar
Thomas committed
826
				buildingMark.mark(dIn);
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
				railVert.mark(map.phi_1(dIn));
				railVert.mark(map.phi1(dExt));
//				obstacleMark.mark(map.phi_1(dIn));
//				obstacleMark.mark(map.phi1(dExt));

				Dart ddExt = dExt;
				Dart ddIn = dIn;
				do
				{
					cm.mark(ddExt);
					cm.mark(ddIn);
					ddExt = map.phi1(ddExt);
					ddIn = map.phi1(ddIn);
				} while(ddExt!=dExt);

				//sew the upper part of the guard rail
				map.sewFaces(map.phi1(map.phi1(dExt)),dIn);
				//sew the guard rail to the floor
				Dart d2 = map.phi2(d);
				map.unsewFaces(d);
				map.sewFaces(d,dExt);
				map.sewFaces(d2,map.phi1(map.phi1(dIn)));

				//set the position of the guard rail
				position[dIn] = position[d2];
				position[dIn][2] += height;
				position[map.phi2(dIn)] = position[d];
				position[map.phi2(dIn)][2] += height;
			}
		}
	}

	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(railVert.isMarked(d))
		{
			railVert.unmark(d);
			map.sewFaces(d,map.phi_1(map.alpha_1(d)));
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
869
870
871
872
}

}