Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

env_generator.hpp 11.5 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
#include "Algo/Modelisation/subdivision.h"
#include "Geometry/inclusion.h"

namespace CGoGN
{

namespace CityGenerator
{

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

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

	return prim;
}

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>
void generateBuilding(typename PFP::MAP& map, typename PFP::TVEC3& position, Dart d, float height, unsigned int buildingType, CellMarker& obstacleMark, CellMarker& buildingMark)
{
	// mark the face as obstacle before extrusion
	Dart dd = d;
	do
	{
		obstacleMark.mark(dd);
		dd = map.phi1(dd);
	} while(dd != d);

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

template <typename PFP>
void generateCity(typename PFP::MAP& map, typename PFP::TVEC3& position, CellMarker& obstacleMark, CellMarker& buildingMark, float sideSize, unsigned int nbSquares)
{
	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))
	{
		if(!buildingMark.isMarked(d) && (rand() % 20 == 0) && notDiagonalAdjacentToAnObstacle<PFP>(map, d, buildingMark))
		{
			generateBuilding<PFP>(map, position, d, (1+(rand()%3)) * height / 2.0f, rand() % 4, obstacleMark, buildingMark);
			--nbBuilding;
		}
	}
}

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;
	
	AutoAttributeHandler<float> tableArea(map, FACE_ORBIT);
	AutoAttributeHandler<CGoGN::NoMathIONameAttribute<VEC_D_A::iterator> > tableIt(map, EDGE_ORBIT);
	
	VEC_D_A orderedD;
	
	// compute all heuristic and construct multimap
	Algo::Geometry::computeAreaFaces<PFP>(map, position, tableArea);
	CellMarker eM(map, EDGE_CELL);
	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;
	}

	convexifyFreeSpace<PFP>(map, position, obstacleMark, buildingMark);
	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;
	if(Geom::testOrientation2D(position[dd], position[map.phi_1(dd)], position[map.phi1(dd)]) == Geom::RIGHT)
	{
		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;
	
	float area;
	CellMarker m(map, FACE_CELL);
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		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())
				{
					moultipass.erase(liloo[map.phi_1(dd)]);
					liloo.erase(map.phi_1(dd));
				}

				moultipass.erase(liloo[dd]);
				liloo.erase(dd);

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

}

}