polyhedron.hpp 30.5 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

untereiner's avatar
untereiner committed
34
35
36
37
38
39
40
41
template <typename PFP>
void explodPolyhedron(typename PFP::MAP& map, Dart d, typename PFP::TVEC3 position)
{
	map.unsewVertexUmbrella(d);
	unsigned int newFaceDegree = map.closeHole(map.phi1(d));

	if(newFaceDegree != 3)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
42
		// chercher le brin ou demarrer
untereiner's avatar
untereiner committed
43
44
45
46
47
48

		std::multimap<Dart, int> edges ;
		typename std::multimap<Dart, int>::iterator it ;

		Dart d12 = map.phi2(map.phi1(d));
		Dart fit = d12;
Pierre Kraemer's avatar
Pierre Kraemer committed
49
		unsigned int i;
untereiner's avatar
untereiner committed
50
51
52
53
54
55

		do
		{
			i = map.faceDegree(map.phi2(fit));
			std::cout << "edge(" << fit << "," << i << ")" << std::endl;
			edges.insert(std::make_pair(fit, i));
Pierre Kraemer's avatar
Pierre Kraemer committed
56
			fit = map.phi1(fit);
untereiner's avatar
untereiner committed
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
		}
		while(fit != d12);

		do
		{
			//44 44
			if(edges.find(fit)->second == 4 && edges.find(map.phi1(fit))->second == 4
				&& !map.sameFace(map.phi2(fit), map.phi2(map.phi1(fit))))
			{
				map.splitFace(fit, map.phi1(map.phi1(fit)));
				fit = map.phi2(map.phi_1(fit));
				int i = map.faceDegree(fit);
				edges.insert(std::make_pair(fit, i));

//				Dart fit2 = map.phi2(fit) ;
//				typename PFP::VEC3 p1 = position[fit] ;
//				typename PFP::VEC3 p2 = position[fit2] ;
//
//				map.cutEdge(fit) ;
//				position[map.phi1(fit)] = typename PFP::REAL(0.5) * (p1 + p2);


				std::cout << "flip cas quad quad " << std::endl;
			}

			//3 3
			if(edges.find(fit)->second == 3 && edges.find(map.phi1(fit))->second == 3
				&& !map.sameFace(map.phi2(fit), map.phi2(map.phi1(fit))))
			{
				map.splitFace(fit, map.phi1(fit));
				fit = map.phi2(map.phi_1(fit));
				int i = map.faceDegree(fit);
				edges.insert(std::make_pair(fit, i));

				std::cout << "flip cas tri tri" << std::endl;
			}

			//3 44 ou 44 3
			if( ((edges.find(fit)->second == 4 && edges.find(map.phi1(fit))->second == 3)
				|| (edges.find(fit)->second == 3 && edges.find(map.phi1(fit))->second == 4))
					&& !map.sameFace(map.phi2(fit), map.phi2(map.phi1(fit))))
			{
				map.splitFace(fit, map.phi1(map.phi1(fit)));
				fit = map.phi2(map.phi_1(fit));
				int i = map.faceDegree(fit);
				edges.insert(std::make_pair(fit, i));

				std::cout << "flip cas quad tri" << std::endl;
			}

			fit = map.phi1(fit);
		}
		while(map.faceDegree(fit) > 4 && fit != d12);

	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
114
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
115
116
117
118
119
120
121
Polyhedron<PFP>::Polyhedron(const Polyhedron<PFP>& p1, const Polyhedron<PFP>& p2) :
	m_map(p1.m_map),
	m_dart(p1.m_dart),
	m_kind(COMPOSED),
	m_nx(-1), m_ny(-1), m_nz(-1),
	m_top_closed(false), m_bottom_closed(false),
	m_positions(p1.m_positions)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
123
124
{
	if (&(p1.map) != &(p2.map))
	{
125
		CGoGNerr << "Warning, can not merge to Polyhedrons of different maps"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
128
129
130
131
	}

	m_tableVertDarts.reserve(p1.m_tableVertDarts.size() + p2.m_tableVertDarts.size()); // can be too much but ...

	typename PFP::VEC3 center(0);

Pierre Kraemer's avatar
Pierre Kraemer committed
132
	for(typename std::vector<Dart>::const_iterator di = p1.m_tableVertDarts.begin(); di != p1.m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
133
134
135
136
137
138
	{
		m_tableVertDarts.push_back(*di);
		center += m_positions[*di];
	}

	// O(n2) pas terrible !!
Pierre Kraemer's avatar
Pierre Kraemer committed
139
	for(typename std::vector<Dart>::const_iterator di = p2.m_tableVertDarts.begin(); di != p2.m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
140
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
141
		unsigned int em = m_map.getEmbedding(*di, VERTEX);
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
144
145
146

		typename std::vector<Dart>::const_iterator dj=p1.m_tableVertDarts.begin();
		bool found = false;
		while ((dj !=p1.m_tableVertDarts.end()) && (!found))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
			unsigned int xm = m_map.getEmbedding(*dj, VERTEX);
			if (xm == em)
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
				found = true;
			else
				++dj;
		}
		if (!found)
		{
			m_tableVertDarts.push_back(*di);
			center += m_positions[*di];
		}
	}

	m_center = center / typename PFP::REAL(m_tableVertDarts.size());
}

template <typename PFP>
164
Dart Polyhedron<PFP>::createTetra(typename PFP::MAP& the_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
165
{
untereiner's avatar
untereiner committed
166
	Dart base = the_map.newFace(3, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
167

untereiner's avatar
untereiner committed
168
169
	Dart side1 = the_map.newFace(3, false);
	the_map.sewFaces(base, side1, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
170

untereiner's avatar
untereiner committed
171
172
173
	Dart side2 = the_map.newFace(3, false);
	the_map.sewFaces(the_map.phi1(base), side2, false);
	the_map.sewFaces(the_map.phi_1(side1), the_map.phi1(side2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
174

untereiner's avatar
untereiner committed
175
176
177
	Dart side3 = the_map.newFace(3, false);
	the_map.sewFaces(the_map.phi_1(base), side3, false);
	the_map.sewFaces(the_map.phi_1(side2), the_map.phi1(side3), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
178

untereiner's avatar
untereiner committed
179
	the_map.sewFaces(the_map.phi_1(side3), the_map.phi1(side1), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
180
181
182
183
184

	return base;
}

template <typename PFP>
185
Dart Polyhedron<PFP>::createPyra(typename PFP::MAP& the_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
186
{
Pierre Kraemer's avatar
Pierre Kraemer committed
187
	Dart base = the_map.newFace(4, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
188

Pierre Kraemer's avatar
Pierre Kraemer committed
189
190
	Dart side1 = the_map.newFace(3, false);
	the_map.sewFaces(base, side1, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
191

Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
194
	Dart side2 = the_map.newFace(3, false);
	the_map.sewFaces(the_map.phi1(base), side2, false);
	the_map.sewFaces(the_map.phi_1(side1), the_map.phi1(side2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
195

Pierre Kraemer's avatar
Pierre Kraemer committed
196
197
198
	Dart side3 = the_map.newFace(3, false);
	the_map.sewFaces(the_map.phi1(the_map.phi1(base)), side3, false);
	the_map.sewFaces(the_map.phi_1(side2), the_map.phi1(side3), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
199

Pierre Kraemer's avatar
Pierre Kraemer committed
200
201
202
	Dart side4 = the_map.newFace(3, false);
	the_map.sewFaces(the_map.phi_1(base), side4, false);
	the_map.sewFaces(the_map.phi_1(side3), the_map.phi1(side4), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
203

Pierre Kraemer's avatar
Pierre Kraemer committed
204
	the_map.sewFaces(the_map.phi_1(side4), the_map.phi1(side1), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
207
208
209

	return base;
}

template <typename PFP>
210
Dart Polyhedron<PFP>::createHexa(typename PFP::MAP& the_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
211
{
Pierre Kraemer's avatar
Pierre Kraemer committed
212
	Dart base = the_map.newFace(4, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
213

Pierre Kraemer's avatar
Pierre Kraemer committed
214
215
	Dart side1 = the_map.newFace(4, false);
	the_map.sewFaces(base, side1, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
216

Pierre Kraemer's avatar
Pierre Kraemer committed
217
218
219
	Dart side2 = the_map.newFace(4, false);
	the_map.sewFaces(the_map.phi1(base), side2, false);
	the_map.sewFaces(the_map.phi_1(side1), the_map.phi1(side2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
220

Pierre Kraemer's avatar
Pierre Kraemer committed
221
222
223
	Dart side3 = the_map.newFace(4, false);
	the_map.sewFaces(the_map.phi1(the_map.phi1(base)), side3, false);
	the_map.sewFaces(the_map.phi_1(side2), the_map.phi1(side3), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
224

Pierre Kraemer's avatar
Pierre Kraemer committed
225
226
227
	Dart side4 = the_map.newFace(4, false);
	the_map.sewFaces(the_map.phi_1(base), side4, false);
	the_map.sewFaces(the_map.phi_1(side3), the_map.phi1(side4), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
228

Pierre Kraemer's avatar
Pierre Kraemer committed
229
	the_map.sewFaces(the_map.phi_1(side4), the_map.phi1(side1), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
230

Pierre Kraemer's avatar
Pierre Kraemer committed
231
232
233
234
235
	Dart top = the_map.newFace(4, false);
	the_map.sewFaces(top, the_map.phi1(the_map.phi1(side1)), false);
	the_map.sewFaces(the_map.phi_1(top), the_map.phi1(the_map.phi1(side2)), false);
	the_map.sewFaces(the_map.phi1(the_map.phi1(top)), the_map.phi1(the_map.phi1(side3)), false);
	the_map.sewFaces(the_map.phi1(top), the_map.phi1(the_map.phi1(side4)), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
236
237
238
239
240

	return base;
}

template <typename PFP>
241
Dart  Polyhedron<PFP>::createPrism(typename PFP::MAP& the_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
242
{
Pierre Kraemer's avatar
Pierre Kraemer committed
243
	Dart base = the_map.newFace(3, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
244

Pierre Kraemer's avatar
Pierre Kraemer committed
245
246
	Dart side1 = the_map.newFace(4, false);
	the_map.sewFaces(base, side1, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
247

Pierre Kraemer's avatar
Pierre Kraemer committed
248
249
250
	Dart side2 = the_map.newFace(4, false);
	the_map.sewFaces(the_map.phi1(base), side2, false);
	the_map.sewFaces(the_map.phi_1(side1), the_map.phi1(side2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
251

Pierre Kraemer's avatar
Pierre Kraemer committed
252
253
254
	Dart side3 = the_map.newFace(4, false);
	the_map.sewFaces(the_map.phi1(the_map.phi1(base)), side3, false);
	the_map.sewFaces(the_map.phi_1(side2), the_map.phi1(side3), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
255

Pierre Kraemer's avatar
Pierre Kraemer committed
256
	the_map.sewFaces(the_map.phi_1(side3), the_map.phi1(side1), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
257

Pierre Kraemer's avatar
Pierre Kraemer committed
258
259
260
261
	Dart top = the_map.newFace(3, false);
	the_map.sewFaces(top, the_map.phi1(the_map.phi1(side1)), false);
	the_map.sewFaces(the_map.phi_1(top), the_map.phi1(the_map.phi1(side2)), false);
	the_map.sewFaces(the_map.phi1(top), the_map.phi1(the_map.phi1(side3)), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
262
263
264
265
266

	return base;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
267
Dart Polyhedron<PFP>::createPolyhedron(typename PFP::MAP& the_map, unsigned int n)
Pierre Kraemer's avatar
Pierre Kraemer committed
268
269
270
271
{
	Dart d;
	switch (n)
	{
272
		case 4 : d = createTetra(the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
273
				break;
274
		case 5 : d = createPyra(the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
275
				break;
276
		case 6 : d = createHexa(the_map);
Pierre Kraemer's avatar
Pierre Kraemer committed
277
278
279
280
281
282
				break;
	}
	return d;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
283
Dart Polyhedron<PFP>::grid_topo(unsigned int x, unsigned int y)
Pierre Kraemer's avatar
Pierre Kraemer committed
284
285
286
287
288
289
290
291
292
293
294
295
296
{
	if (m_kind != NONE) return m_dart;

	m_kind = GRID;
	m_nx = x;
	m_ny = y;
	// nb vertices
	int nb = (x+1)*(y+1);

	// vertice reservation
	m_tableVertDarts.reserve(nb);

	// creation of quads and storing vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
297
	for (unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
298
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
299
		for (unsigned int j = 1; j <= x; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
300
		{
301
			Dart d = m_map.newFace(4,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
302
			m_tableVertDarts.push_back(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
303
			if (j == x)
Pierre Kraemer's avatar
Pierre Kraemer committed
304
305
306
				m_tableVertDarts.push_back(m_map.phi1(d));
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
307

Pierre Kraemer's avatar
Pierre Kraemer committed
308
	// store last row of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
309
	for (unsigned int i = 0; i < x; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
310
311
312
313
314
315
	{
		m_tableVertDarts.push_back( m_map.phi_1(m_tableVertDarts[(y-1)*(x+1) + i]) );	
	}
	m_tableVertDarts.push_back( m_map.phi1(m_tableVertDarts[(y-1)*(x+1) +x]) );	

	//sewing the quads
Pierre Kraemer's avatar
Pierre Kraemer committed
316
	for (unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
317
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
318
		for (unsigned int j = 0; j < x; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
319
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
320
			if (i > 0) // sew with preceeding row
Pierre Kraemer's avatar
Pierre Kraemer committed
321
322
323
324
325
			{
				int pos = i*(x+1)+j;
				Dart d = m_tableVertDarts[pos];
				Dart e = m_tableVertDarts[pos-(x+1)];
				e = m_map.phi1(m_map.phi1(e));
326
				m_map.sewFaces(d,e,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
327
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
328
			if (j > 0) // sew with preceeding column
Pierre Kraemer's avatar
Pierre Kraemer committed
329
330
331
332
333
334
			{
				int pos = i*(x+1)+j;
				Dart d = m_tableVertDarts[pos];
				d = m_map.phi_1(d);
				Dart e = m_tableVertDarts[pos-1];
				e = m_map.phi1(e);
335
				m_map.sewFaces(d,e,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
336
337
338
339
340
341
			}
		}
	}

	// store & return reference dart
	m_dart = m_tableVertDarts[0]; // util ?
342
343
344

	m_map.closeHole(m_dart) ;

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

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
349
Dart Polyhedron<PFP>::cylinder_topo(unsigned int n, unsigned int z, bool top_closed, bool bottom_closed)
Pierre Kraemer's avatar
Pierre Kraemer committed
350
351
352
353
354
355
356
{
	if (m_kind != NONE) return m_dart;

	m_kind = CYLINDER;
	m_nx = n;
	m_nz = z;

Pierre Kraemer's avatar
Pierre Kraemer committed
357
358
	m_bottom_closed = bottom_closed;
	m_top_closed = top_closed;
Pierre Kraemer's avatar
Pierre Kraemer committed
359
360
361
362
363
364
365
366
367

	int nb = (n)*(z+1);
	if (bottom_closed) nb++;
	if (top_closed) nb++;

	// vertice reservation
	m_tableVertDarts.reserve(nb);

	// creation of quads and storing vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
368
	for (unsigned int i = 0; i < z; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
369
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
370
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
371
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
372
			Dart d = m_map.newFace(4, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
373
374
375
			m_tableVertDarts.push_back(d);
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
376
	for (unsigned int i = 0; i < n; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
377
378
379
380
381
	{
		m_tableVertDarts.push_back( m_map.phi_1(m_tableVertDarts[(z-1)*n+i]) );	
	}

	//sewing the quads
Pierre Kraemer's avatar
Pierre Kraemer committed
382
	for (unsigned int i = 0; i < z; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
383
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
384
		for (unsigned int j = 0; j < n; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
385
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
386
			if (i > 0) // sew with preceeding row
Pierre Kraemer's avatar
Pierre Kraemer committed
387
388
389
390
391
			{
				int pos = i*n+j;
				Dart d = m_tableVertDarts[pos];
				Dart e = m_tableVertDarts[pos-n];
				e = m_map.phi1(m_map.phi1(e));
Pierre Kraemer's avatar
Pierre Kraemer committed
392
				m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
393
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
394
			if (j > 0) // sew with preceeding column
Pierre Kraemer's avatar
Pierre Kraemer committed
395
396
397
398
399
400
			{
				int pos = i*n+j;
				Dart d = m_tableVertDarts[pos];
				d = m_map.phi_1(d);
				Dart e = m_tableVertDarts[pos-1];
				e = m_map.phi1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
401
				m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
402
403
404
405
406
407
408
409
			}
			else 
			{
				int pos = i*n;
				Dart d = m_tableVertDarts[pos];
				d = m_map.phi_1(d);
				Dart e = m_tableVertDarts[pos+(n-1)];
				e = m_map.phi1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
410
				m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
411
412
413
414
415
416
417
			}
		}
	}

	if (bottom_closed)
	{
		Dart d = m_tableVertDarts[0];
418
		if(m_map.closeHole(d, false))
Pierre Kraemer's avatar
Pierre Kraemer committed
419
420
		{
			d = m_map.phi2(d);
421
			if(m_map.faceDegree(d) > 3)
Pierre Kraemer's avatar
Pierre Kraemer committed
422
			{
423
				Algo::Modelisation::trianguleFace<PFP>(m_map, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
424
425
426
427
				m_tableVertDarts.push_back(m_map.phi_1(d));
			}
		}
	}
428
429
	else
		m_map.closeHole(m_tableVertDarts[0]);
Pierre Kraemer's avatar
Pierre Kraemer committed
430
431
432
433

	if (top_closed)
	{
		Dart d =  m_map.phi_1(m_tableVertDarts[n*z]);
Pierre Kraemer's avatar
Pierre Kraemer committed
434
		if(m_map.closeHole(d, true))
Pierre Kraemer's avatar
Pierre Kraemer committed
435
436
		{
			d = m_map.phi2(d);
437
			if(m_map.faceDegree(d) > 3)
Pierre Kraemer's avatar
Pierre Kraemer committed
438
			{
439
				Algo::Modelisation::trianguleFace<PFP>(m_map, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
440
441
442
443
				m_tableVertDarts.push_back(m_map.phi_1(d));
			}
		}
	}
444
445
	else
		m_map.closeHole(m_map.phi_1(m_tableVertDarts[n*z]));
Pierre Kraemer's avatar
Pierre Kraemer committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472

	m_dart = m_tableVertDarts.front();
	return m_dart;
}

// template <typename PFP>
// Dart Polyhedron<PFP>::cone_topo(int n, int z, bool bottom_closed)
// {
// 	if (m_kind != NONE) return m_dart;
// 
// 	if (z>1)
// 	{
// 		if  (n>4) // normal case
// 		{
// 			Dart d = cylinder_topo(n,z-1,true,bottom_closed);
// 			m_kind = CONE;
// 			m_nz = z;
// 			return d;
// 		}
// 		else if (n==4)
// 		{
// 			Dart d = cylinder_topo(n,z-1,true,false);
// 			m_kind = CONE;
// 			m_nz = z;
// 			// close bottom with one quad not three triangles
// 			if (bottom_closed)
// 			{
473
// 				Dart t= m_map.newFace(4,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// 				m_map.sewFaces(m_tableVertDarts[0],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[1],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[2],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[3],t);
// 			}
// 			return d;
// 		}
// 		else // n==3 (base is triangle)
// 		{
// 			Dart d = cylinder_topo(n,z-1,true,false);
// 			m_kind = CONE;
// 			m_nz = z;
// 			// close bottom with one triangle not three
// 			if (bottom_closed)
// 			{
492
// 				Dart t= m_map.newFace(3,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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
// 				m_map.sewFaces(d,t);
// 				d=nextDV(d); t=m_map.phi_1(t);
// 				m_map.sewFaces(d,t);
// 				d=nextDV(d); t=m_map.phi_1(t);
// 				m_map.sewFaces(d,t);
// 			}
// 			return d;
// 		}
// 	}
// 	else //z==1 only on slice
// 	{
// 		m_kind = CONE;		
// 		m_nx = n;
// 		m_nz = z;
// 		if  (n>4) // normal case
// 		{
// 			if (bottom_closed)
// 				m_tableVertDarts.reserve(n+2);
// 			else
// 				m_tableVertDarts.reserve(n+1);
// 
// 			Dart ref = triangleFan_topo<PFP>(m_map,n);
// 			Dart d = ref;
// 			for (int i=0;i<n;++i)
// 			{
// 				m_tableVertDarts.push_back(d);
// 				d = nextDV(d);
// 			}
// 
// 			if (bottom_closed)
// 			{
// 				m_bottom_closed=true;
// 				Dart dd = triangleFan_topo<PFP>(m_map,n);
// 				m_tableVertDarts.push_back(m_map.phi_1(dd));
// 				for (int i=0;i<n;++i)
// 				{
// 					m_map.sewFaces(d,dd);
// 					d=nextDV(d);
// 					dd=precDV(dd);
// 				}
// 			}
// 
// 			m_tableVertDarts.push_back(m_map.phi_1(ref));
// 
// 			m_dart=ref;
// 			return ref;
// 		}
// 		else if (n==4)
// 		{
// 			m_bottom_closed=false; // because no vertex in bottom
// 			m_tableVertDarts.reserve(4);
// 
// 			Dart dd = triangleFan_topo<PFP>(m_map,4);
// 			for (int i=0;i<4;++i)
// 			{
// 				m_tableVertDarts.push_back(dd);
// 				dd = nextDV(dd);
// 			}
// 			m_tableVertDarts.push_back(m_map.phi_1(dd));
// 
// 			if (bottom_closed)
// 			{
555
// 				Dart t= m_map.newFace(4,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
// 				m_map.sewFaces(m_tableVertDarts[0],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[1],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[2],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[3],t);
// 			}
// 			m_dart=dd;
// 			return dd;
// 		}
// 		else // n==3 (base is triangle, here we create a tetrahedron)
// 		{
// 			m_bottom_closed=false; // because no vertex in bottom
// 			m_tableVertDarts.reserve(4);
// 
// 			Dart dd = triangleFan_topo<PFP>(m_map,3);
// 			for (int i=0;i<3;++i)
// 			{
// 				m_tableVertDarts.push_back(dd);
// 				dd = nextDV(dd);
// 			}
// 			m_tableVertDarts.push_back(m_map.phi_1(dd));
// 
// 			if (bottom_closed)
// 			{
582
// 				Dart t= m_map.newFace(3,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
583
584
585
586
587
588
589
590
591
592
593
594
595
// 				m_map.sewFaces(m_tableVertDarts[0],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[1],t);
// 				t=m_map.phi_1(t);
// 				m_map.sewFaces(m_tableVertDarts[2],t);
// 			}
// 			m_dart=dd;
// 			return dd;
// 		}
// 	}
// }

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
596
Dart Polyhedron<PFP>::cube_topo(unsigned int x, unsigned int y, unsigned int z)
Pierre Kraemer's avatar
Pierre Kraemer committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
{
	if (m_kind != NONE) return m_dart;

	m_dart = cylinder_topo(2*(x+y),z, false,false);
	m_kind = CUBE;
	m_nx = x;
	m_ny = y;
	m_nz = z;

	int nb = 2*(x+y)*(z+1) + 2*(x-1)*(y-1);
	m_tableVertDarts.reserve(nb);
	
	// we now have the 4 sides, just need to create store and sew top & bottom
	// the top
611
	Polyhedron<PFP> primTop(m_map, m_positions);
Pierre Kraemer's avatar
Pierre Kraemer committed
612
613
614
615
	primTop.grid_topo(x,y);
	std::vector<Dart>& tableTop = primTop.getVertexDarts();

	int index_side = 2*(x+y)*z;
Pierre Kraemer's avatar
Pierre Kraemer committed
616
	for(unsigned int i = 0; i < x; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
617
618
619
	{
		Dart d = m_map.phi_1(m_tableVertDarts[index_side++]);
		Dart e = tableTop[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
620
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
621
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
622
	for(unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
623
624
625
	{
		Dart d = m_map.phi_1(m_tableVertDarts[index_side++]);
		Dart e = tableTop[x+i*(x+1)];
Pierre Kraemer's avatar
Pierre Kraemer committed
626
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
627
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
628
	for(unsigned int i = 0; i < x; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
629
630
631
632
	{
		Dart d = m_map.phi_1(m_tableVertDarts[index_side++]);
		Dart e = tableTop[(x+1)*(y+1)-2 - i];
		e = m_map.phi_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
633
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
634
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
635
	for(unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
636
637
638
639
	{
		Dart d = m_map.phi_1(m_tableVertDarts[index_side++]);
		Dart e = tableTop[(y-1-i)*(x+1)];
		e = m_map.phi_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
640
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
641
642
643
	}

	// the bottom
644
	Polyhedron<PFP> primBottom(m_map,m_positions);
Pierre Kraemer's avatar
Pierre Kraemer committed
645
646
647
648
	primBottom.grid_topo(x,y);
	std::vector<Dart>& tableBottom = primBottom.getVertexDarts();

	index_side = 3*(x+y)+(x-1);
Pierre Kraemer's avatar
Pierre Kraemer committed
649
	for(unsigned int i = 0; i < x; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
650
651
652
	{
		Dart d = m_tableVertDarts[(index_side--)%(2*(x+y))];
		Dart e = tableBottom[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
653
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
654
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
655
	for(unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
656
657
658
	{
		Dart d = m_tableVertDarts[(index_side--)%(2*(x+y))];
		Dart e = tableBottom[x+i*(x+1)];
Pierre Kraemer's avatar
Pierre Kraemer committed
659
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
660
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
661
	for(unsigned int i = 0; i < x; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
662
663
664
665
	{
		Dart d = m_tableVertDarts[(index_side--)%(2*(x+y))];
		Dart e = tableBottom[(x+1)*(y+1)-2 - i];
		e = m_map.phi_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
666
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
667
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
668
	for(unsigned int i = 0; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
669
670
671
672
	{
		Dart d = m_tableVertDarts[(index_side--)%(2*(x+y))];
		Dart e = tableBottom[(y-1-i)*(x+1)];
		e = m_map.phi_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
673
		m_map.sewFaces(d, e, false);
Pierre Kraemer's avatar
Pierre Kraemer committed
674
675
676
677
	}

	// and add new vertex in m_tableVertDarts
	//top  first
Pierre Kraemer's avatar
Pierre Kraemer committed
678
	for(unsigned int i = 1; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
679
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
680
		for(unsigned int j = 1; j < x; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
681
682
683
684
			m_tableVertDarts.push_back(tableTop[i*(x+1)+j]);
	}

	// then bottom
Pierre Kraemer's avatar
Pierre Kraemer committed
685
	for(unsigned int i = 1; i < y; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
686
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
687
		for(unsigned int j = 1; j < x; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
688
689
690
691
692
693
694
			m_tableVertDarts.push_back(tableBottom[i*(x+1)+j]);
	}

	return m_dart;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
695
Dart Polyhedron<PFP>::tore_topo(unsigned int m, unsigned int n)
Pierre Kraemer's avatar
Pierre Kraemer committed
696
697
698
699
700
701
702
703
704
{
	if (m_kind != NONE) return m_dart;

	m_dart = cylinder_topo(n, m, false, false);
	m_nx=n;
	m_ny=m;
	m_kind = TORE;

	// juste finish to sew
Pierre Kraemer's avatar
Pierre Kraemer committed
705
	for(unsigned int i = 0; i < n; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
706
707
708
709
	{
		Dart d = m_tableVertDarts[i];
		Dart e = m_tableVertDarts[(m*n)+i];
		e = m_map.phi_1(e);
710
		m_map.sewFaces(d, e, true);
Pierre Kraemer's avatar
Pierre Kraemer committed
711
712
713
714
715
716
717
718
719
720
	}

	// remove the last n vertex darts that are no more necessary (sewed with n first)
	// memory not freed (but will be when destroy the Polyhedron), not important ??
	m_tableVertDarts.resize(m*n); 

	return m_dart;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
721
void Polyhedron<PFP>::embedGrid(float x, float y, float z)
Pierre Kraemer's avatar
Pierre Kraemer committed
722
723
724
725
{
	typedef typename PFP::VEC3 VEC3 ;

	if (m_kind != GRID) {
726
		CGoGNerr << "Warning try to embedGrid something that is not a grid"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
727
728
729
730
731
732
		return;
	}

	float dx = x/float(m_nx);
	float dy = y/float(m_ny);

Pierre Kraemer's avatar
Pierre Kraemer committed
733
	for(unsigned int i = 0; i <= m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
734
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
735
		for(unsigned int j = 0; j <= m_nx;++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
736
737
738
739
		{
			VEC3 pos(-x/2 + dx*float(j), -y/2 + dy*float(i), z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[i*(m_nx+1)+j];
Pierre Kraemer's avatar
Pierre Kraemer committed
740
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
741
742
743
744
745
		}
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
746
void Polyhedron<PFP>::embedCylinder(float bottom_radius, float top_radius, float height)
Pierre Kraemer's avatar
Pierre Kraemer committed
747
748
749
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
750
751
	if (m_kind != CYLINDER)
	{
752
		CGoGNerr << "Warning try to embedCylinder something that is not a cylnder"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
753
754
755
756
757
758
		return;
	}

	float alpha = float(2.0*M_PI/m_nx);
	float dz = height/float(m_nz);

Pierre Kraemer's avatar
Pierre Kraemer committed
759
	for(unsigned int i = 0; i <= m_nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
760
761
762
	{
		float a = float(i)/float(m_nz);
		float radius = a*top_radius + (1.0f-a)*bottom_radius;
Pierre Kraemer's avatar
Pierre Kraemer committed
763
		for(unsigned int j = 0; j < m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
764
765
766
767
768
769
770
		{
	
			float x = radius*cos(alpha*float(j));
			float y = radius*sin(alpha*float(j));
			VEC3 pos(x, y, -height/2 + dz*float(i));
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[i*(m_nx)+j];
Pierre Kraemer's avatar
Pierre Kraemer committed
771
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
772
773
774
775
776
777
778
779
780
		}
	}

	int indexUmbrella = m_nx*(m_nz+1);
	if (m_bottom_closed)
	{
		VEC3 pos(0.0f, 0.0f, -height/2 );
		unsigned int em = m_positions.insert(pos);
		Dart d = m_tableVertDarts[indexUmbrella++];
Pierre Kraemer's avatar
Pierre Kraemer committed
781
		m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
782
783
784
785
786
787
788
	}

	if (m_top_closed)
	{
		VEC3 pos(0.0f ,0.0f, height/2 );
		unsigned int em = m_positions.insert(pos);
		Dart d = m_tableVertDarts[indexUmbrella];
Pierre Kraemer's avatar
Pierre Kraemer committed
789
		m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
790
791
792
793
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
794
void Polyhedron<PFP>::embedCone(float radius, float height)
Pierre Kraemer's avatar
Pierre Kraemer committed
795
796
797
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
798
799
	if (m_kind != CONE)
	{
800
		CGoGNerr << "Warning try to embedCone something that is not a cone"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
801
802
803
804
805
806
807
		return;
	}

	float alpha = float(2.0*M_PI/m_nx);
	int zcyl = m_nz-1;
	float dz = height/float(m_nz);
	
Pierre Kraemer's avatar
Pierre Kraemer committed
808
	for(unsigned int i = 0; i <= zcyl; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
809
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
810
		for(unsigned int j = 0; j < m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
811
812
813
814
815
816
817
818
819
820
		{
			float rad = radius * float(m_nz-i)/float(m_nz);
			float h = -height/2 + dz*float(i);

			float x = rad*cos(alpha*float(j));
			float y = rad*sin(alpha*float(j));

			VEC3 pos(x, y, h);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[i*(m_nx)+j];
Pierre Kraemer's avatar
Pierre Kraemer committed
821
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
822
823
824
825
826
827
828
829
830
		}
	}

	int indexUmbrella = m_nx*(m_nz);
	if (m_bottom_closed)
	{
		VEC3 pos(0.0f, 0.0f, -height/2 );
		unsigned int em = m_positions.insert(pos);
		Dart d = m_tableVertDarts[indexUmbrella++];
Pierre Kraemer's avatar
Pierre Kraemer committed
831
		m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
832
833
834
835
836
837
	}

	//  top always closed in cone 
	VEC3 pos(0.0f ,0.0f, height/2.0f );
	unsigned int em = m_positions.insert(pos);
	Dart d = m_tableVertDarts[indexUmbrella];
Pierre Kraemer's avatar
Pierre Kraemer committed
838
	m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
839
840
841
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
842
void Polyhedron<PFP>::embedSphere(float radius)
Pierre Kraemer's avatar
Pierre Kraemer committed
843
844
845
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
846
847
	if (!((m_kind == CYLINDER) && (m_top_closed) && (m_bottom_closed)))
	{
848
		CGoGNerr << "Warning try to embedSphere something that is not a sphere (closed cylinder)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
849
850
851
852
853
854
		return;
	}

	float alpha = float(2.0*M_PI/m_nx);
	float beta = float(M_PI/(m_nz+2));

Pierre Kraemer's avatar
Pierre Kraemer committed
855
	for(unsigned int i = 0; i <= m_nz; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
856
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
857
		for(unsigned int j = 0; j < m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
858
859
860
861
862
863
864
865
866
		{
			float h = float(radius * sin(-M_PI/2.0+(i+1)*beta));
			float rad = float(radius * cos(-M_PI/2.0+(i+1)*beta));

			float x = rad*cos(alpha*float(j));
			float y = rad*sin(alpha*float(j));
			VEC3 pos(x, y, h );
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[i*(m_nx)+j];
Pierre Kraemer's avatar
Pierre Kraemer committed
867
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
868
869
870
871
		}
	}

	// bottom  pole
Pierre Kraemer's avatar
Pierre Kraemer committed
872
	VEC3 pos(0.0f, 0.0f, -radius);
Pierre Kraemer's avatar
Pierre Kraemer committed
873
874
	unsigned int em = m_positions.insert(pos);
	Dart d = m_tableVertDarts[m_nx*(m_nz+1)];
Pierre Kraemer's avatar
Pierre Kraemer committed
875
	m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
876
877

	//  top pole
Pierre Kraemer's avatar
Pierre Kraemer committed
878
	pos = VEC3(0.0f, 0.0f, radius);
Pierre Kraemer's avatar
Pierre Kraemer committed
879
880
	em = m_positions.insert(pos);
	d = m_tableVertDarts[m_nx*(m_nz+1)+1];
Pierre Kraemer's avatar
Pierre Kraemer committed
881
	m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
882
883
884
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
885
void Polyhedron<PFP>::embedTore(float big_radius, float small_radius)
Pierre Kraemer's avatar
Pierre Kraemer committed
886
887
888
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
889
890
	if (m_kind != TORE)
	{
891
		CGoGNerr << "Warning try to embedTore something that is not a tore"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
892
893
894
895
896
897
		return;
	}

	float alpha = float(2.0*M_PI/m_nx);
	float beta = float(2.0*M_PI/m_ny);

Pierre Kraemer's avatar
Pierre Kraemer committed
898
	for (unsigned int i = 0; i < m_nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
899
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
900
		for(unsigned int j = 0; j < m_ny; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
901
902
903
904
905
906
907
908
		{
			float z = small_radius*sin(beta*float(j));
			float r = big_radius + small_radius*cos(beta*float(j));
			float x = r*cos(alpha*float(i));
			float y = r*sin(alpha*float(i));
			VEC3 pos(x, y, z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[j*(m_nx)+i];
Pierre Kraemer's avatar
Pierre Kraemer committed
909
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
910
911
912
913
914
		}
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
915
void Polyhedron<PFP>::embedCube(float sx, float sy, float sz)
Pierre Kraemer's avatar
Pierre Kraemer committed
916
917
918
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
919
920
	if (m_kind != CUBE)
	{
921
		CGoGNerr << "Warning try to embedCube something that is not a cube"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
922
923
924
925
926
927
928
929
930
		return;
	}

	float dz = sz/float(m_nz);
	float dy = sy/float(m_ny);
	float dx = sx/float(m_nx);

	// first embedding the sides
	int index = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
931
	for (unsigned int k = 0; k <= m_nz; ++k)
Pierre Kraemer's avatar
Pierre Kraemer committed
932
933
	{
		float z = float(k)*dz - sz/2.0f;
Pierre Kraemer's avatar
Pierre Kraemer committed
934
		for (unsigned int i = 0; i < m_nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
935
936
937
938
939
		{
			float x = float(i)*dx - sx/2.0f;
			VEC3 pos(x, -sy/2.0f, z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
940
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
941
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
942
		for (unsigned int i = 0; i < m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
943
944
945
946
947
		{
			float y = float(i)*dy - sy/2.0f;
			VEC3 pos(sx/2.0f, y, z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
948
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
949
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
950
		for (unsigned int i = 0; i < m_nx; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
951
952
953
954
955
		{
			float x = sx/2.0f-float(i)*dx;
			VEC3 pos(x, sy/2.0f, z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
956
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
957
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
958
		for (unsigned int i = 0; i < m_ny ;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
959
960
961
962
963
		{
			float y = sy/2.0f - float(i)*dy;
			VEC3 pos(-sx/2.0f, y, z);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
964
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
965
966
967
968
		}
	}

	// the top
Pierre Kraemer's avatar
Pierre Kraemer committed
969
	for(unsigned int i = 1; i  <m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
970
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
971
		for(unsigned int j = 1; j < m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
972
973
974
975
		{
			VEC3 pos(-sx/2.0f+float(j)*dx, -sy/2.0f+float(i)*dy, sz/2.0f);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
976
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
977
978
979
980
		}
	}

	// the bottom
Pierre Kraemer's avatar
Pierre Kraemer committed
981
	for(unsigned int i = 1; i < m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
982
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
983
		for(unsigned int j = 1; j < m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
984
985
986
987
		{
			VEC3 pos(-sx/2.0f+float(j)*dx, sy/2.0f-float(i)*dy, -sz/2.0f);
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[index++];
Pierre Kraemer's avatar
Pierre Kraemer committed
988
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
989
990
991
992
993
994
995
996
997
		}
	}
}

template <typename PFP>
void Polyhedron<PFP>::computeCenter()
{
	typename PFP::VEC3 center(0);

Pierre Kraemer's avatar
Pierre Kraemer committed
998
	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
	{
		center += m_positions[*di];
	}

	m_center = center / typename PFP::REAL(m_tableVertDarts.size());
}

template <typename PFP>
//void Polyhedron<PFP>::transform(float* matrice)
void Polyhedron<PFP>::transform(const Geom::Matrix44f& matrice)
{
//	Geom::Vec4f v1(matrice[0],matrice[4],matrice[8], matrice[12]);
//	Geom::Vec4f v2(matrice[1],matrice[5],matrice[9], matrice[13]);
//	Geom::Vec4f v3(matrice[2],matrice[6],matrice[10],matrice[14]);
//	Geom::Vec4f v4(matrice[3],matrice[7],matrice[11],matrice[15]);

Pierre Kraemer's avatar
Pierre Kraemer committed
1015
	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
	{

		typename PFP::VEC3& pos = m_positions[*di];
//
//		Geom::Vec4f VA(pos[0],pos[1],pos[2],1.0f);
//
//		Geom::Vec4f VB((VA*v1),(VA*v2),(VA*v3),(VA*v4));
//		VEC3 newPos(VB[0]/VB[3],VB[1]/VB[3],VB[2]/VB[3]);
		
		pos = Geom::transform(pos, matrice);
	}
	// transform the center
	m_center = Geom::transform(m_center, matrice);

}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
1033
void Polyhedron<PFP>::mark(CellMarker& m)
Pierre Kraemer's avatar
Pierre Kraemer committed
1034
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1035
	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
Pierre Kraemer's avatar
Pierre Kraemer committed
1036
	{
Sylvain Thery's avatar
Sylvain Thery committed
1037
		m.mark(*di);
Pierre Kraemer's avatar
Pierre Kraemer committed
1038
1039
1040
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
//template <typename PFP>
//void Polyhedron<PFP>::markEmbVertices(Mark m)
//{
//	AttributeHandler<Mark> markers(VERTEX << 24, m_map);
//	for(typename std::vector<Dart>::iterator di = m_tableVertDarts.begin(); di != m_tableVertDarts.end(); ++di)
//	{
//		markers[*di].setMark(m);
//	}
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
1050
1051

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
1052
void Polyhedron<PFP>::embedTwistedStrip(float radius_min,  float radius_max, float turns)
Pierre Kraemer's avatar
Pierre Kraemer committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
{
	typedef typename PFP::VEC3 VEC3 ;

	float alpha = float(2.0*M_PI/m_ny);
	float beta = turns/float(m_ny);

	float radius = (radius_max + radius_min)/2.0f;
	float rdiff = (radius_max - radius_min)/2.0f;

Pierre Kraemer's avatar
Pierre Kraemer committed
1062
	for(unsigned int i = 0; i <= m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
1063
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1064
		for(unsigned int j = 0; j <= m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
1065
1066
1067
1068
1069
1070
		{
			float rw = -rdiff + float(j)*2.0f*rdiff/float(m_nx);
			float r = radius + rw*cos(beta*float(i));
			VEC3 pos(r*cos(alpha*float(i)), r*sin(alpha*float(i)), rw*sin(beta*float(i)));
			unsigned int em = m_positions.insert(pos);
			Dart d = m_tableVertDarts[i*(m_nx+1)+j];
Pierre Kraemer's avatar
Pierre Kraemer committed
1071
			m_map.embedOrbit(VERTEX, d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
1072
1073
1074
1075
1076
		}
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
1077
void Polyhedron<PFP>::embedHelicoid(float radius_min, float radius_max, float maxHeight, float nbTurn, int orient)
Pierre Kraemer's avatar
Pierre Kraemer committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
{
	typedef typename PFP::VEC3 VEC3 ;

	float alpha = float(2.0*M_PI/m_nx)*nbTurn;
	float hS = maxHeight/m_nx;

// 	float radius = (radius_max + radius_min)/2.0f;
// 	float rdiff = (radius_max - radius_min)/2.0f;

Pierre Kraemer's avatar
Pierre Kraemer committed
1087
	for(unsigned int i = 0; i <= m_ny; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
1088
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1089
		for(unsigned int j = 0; j <= m_nx; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
1090
1091
1092
		{
// 			float r = radius_max + radius_min*cos(beta*float(j));
			float r,x,y;
Thomas's avatar
Thomas committed
1093
1094
1095
1096
// 			if(i==1) {
// 				r = radius_max;
// 			}
// 			else {
Pierre Kraemer's avatar
Pierre Kraemer committed
1097
				r = radius_min+(radius_max-radius_min)*float(i)/float(m_ny);
Thomas's avatar
Thomas committed
1098
1099
1100
// 			}
			x = orient*r*sin(alpha*float(j));
			y = orient*r*cos(alpha*float(j));
Pierre Kraemer's avatar
Pierre Kraemer committed
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127

			VEC3 pos(x, y, j*hS);
			Dart d = m_tableVertDarts[i*(m_nx+1)+j];
			m_positions[d]=pos;
		}
	}
}

// template <typename PFP>
// void onlyTriangles(typename PFP::MAP& the_map, Dart primd)
// {
// 	DartMarker m(the_map);
// 
// 	// list of faces to process and processed(before pos iterator)
// 	std::list<Dart> ld;
// 	ld.push_back(primd);
// 	// current position in list
// 	typename std::list<Dart>::iterator pos = ld.begin();
// 	do
// 	{
// 		Dart d = *pos;
// 		
// 		// cut the face of first dart of list
// 		Dart d1 = the_map.phi1(d);
// 		Dart e = the_map.phi1(d1);
// 		Dart e1 = the_map.phi1(e);
// 		Dart f = the_map.phi1(e1);
1128
// 		m.markOrbit(FACE, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
// 		if (f==d) // quad
// 		{
// 			Dart n = the_map.cutFace(d,e);
// 			Dart nn = the_map.phi2(n);
// 			// mark the face
// 			m.mark(n);
// 			m.mark(nn);
// 		}
// 
// 		// and store neighbours faces in the list
// 		d = the_map.phi2(d);
// 		e = the_map.phi2(e);
// 		d1 = the_map.phi1(the_map.phi2(d1));
// 		e1 = the_map.phi1(the_map.phi2(e1));
// 
// 		if (!m.isMarked(d))
// 			ld.push_back(d);
// 		if (!m.isMarked(e))
// 			ld.push_back(e);
// 		if (!m.isMarked(d1))
// 			ld.push_back(d1);
// 		if ((f==d) && (!m.isMarked(e1)))
// 			ld.push_back(e1);
// 		pos++;
// 	}while (pos!=ld.end()); // stop when no more face to process
// }

// template <typename PFP>
// Dart triangleFan_topo(typename PFP::MAP& the_map, int n)
// {
1159
// 	Dart d = the_map.newFace(3,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
1160
1161
1162
// 	Dart e = the_map.phi1(d);
// 	for(int i=1;i<n;++i)
// 	{
1163
// 		Dart f = the_map.newFace(3,false);
Pierre Kraemer's avatar
Pierre Kraemer committed
1164
1165
1166
1167
1168
1169
1170
// 		the_map.sewFaces(the_map.phi_1(f),e);
// 		e = the_map.phi1(f);
// 	}
// 	the_map.sewFaces(the_map.phi_1(d),e);
// 	return d;
// }

Pierre Kraemer's avatar
Pierre Kraemer committed
1171
} // namespace Modelisation
Pierre Kraemer's avatar
Pierre Kraemer committed
1172

Pierre Kraemer's avatar
Pierre Kraemer committed
1173
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
1174

Pierre Kraemer's avatar
Pierre Kraemer committed
1175
} // namespace CGoGN