triangular.hpp 18.7 KB
Newer Older
untereiner's avatar
untereiner 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Surface
{

namespace Tilings
{

namespace Triangular
{

/*! Grid
 *************************************************************************/

template <typename PFP>
void Grid<PFP>::grid(unsigned int x, unsigned int y, bool close)
{
    // nb vertices
47 48
	unsigned int nbV = (x+1)*(y+1);
	unsigned int nbF = 2*x*y;
untereiner's avatar
untereiner committed
49

untereiner's avatar
untereiner committed
50
	// vertices reservation
51 52
	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
untereiner's avatar
untereiner committed
53 54 55 56 57 58 59

    // creation of triangles and storing vertices
    for (unsigned int i = 0; i < y; ++i)
    {
        for (unsigned int j = 1; j <= x; ++j)
        {
            Dart d = this->m_map.newFace(3, false);
untereiner's avatar
untereiner committed
60 61 62 63
			Dart d2 = this->m_map.newFace(3, false);
			this->m_map.sewFaces(this->m_map.phi1(d), this->m_map.phi_1(d2), false);
			this->m_tableVertDarts.push_back(d);

64 65 66
			this->m_tableFaceDarts.push_back(d);
			this->m_tableFaceDarts.push_back(d2);

untereiner's avatar
untereiner committed
67
            if (j == x)
untereiner's avatar
untereiner committed
68
				this->m_tableVertDarts.push_back(d2);
untereiner's avatar
untereiner committed
69 70 71 72 73 74
        }
    }

    // store last row of vertices
    for (unsigned int i = 0; i < x; ++i)
    {
untereiner's avatar
untereiner committed
75 76 77 78
		this->m_tableVertDarts.push_back(this->m_map.phi_1(this->m_tableVertDarts[(y-1)*(x+1) + i]));
	}
	this-> m_tableVertDarts.push_back(this->m_map.phi1(this->m_tableVertDarts[(y-1)*(x+1) +x]));

untereiner's avatar
untereiner committed
79

untereiner's avatar
untereiner committed
80
	//sewing pairs of triangles
untereiner's avatar
untereiner committed
81 82 83 84
    for (unsigned int i = 0; i < y; ++i)
    {
        for (unsigned int j = 0; j < x; ++j)
        {
untereiner's avatar
untereiner committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

			if (i > 0) // sew with preceeding row
			{
				int pos = i*(x+1)+j;
				Dart d = this->m_tableVertDarts[pos];
				Dart e = this->m_tableVertDarts[pos-(x+1)];
				e = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(e)));
				this->m_map.sewFaces(d, e, false);
			}
			if (j > 0) // sew with preceeding column
			{
				int pos = i*(x+1)+j;
				Dart d = this->m_tableVertDarts[pos];
				d = this->m_map.phi_1(d);
				Dart e = this->m_tableVertDarts[pos-1];
				e = this->m_map.phi1(this->m_map.phi2(this->m_map.phi1(e)));
				this->m_map.sewFaces(d, e, false);
			}
untereiner's avatar
untereiner committed
103 104 105 106 107
        }
    }

    if(close)
        this->m_map.closeHole(this->m_tableVertDarts[0]) ;
untereiner's avatar
untereiner committed
108 109

	this->m_dart = this->m_tableVertDarts[0];
untereiner's avatar
untereiner committed
110 111 112
}

template <typename PFP>
113
void Grid<PFP>::embedIntoGrid(VertexAttribute<VEC3, MAP>& position, float x, float y, float z)
untereiner's avatar
untereiner committed
114
{
untereiner's avatar
untereiner committed
115 116
	float dx = x / float(this->m_nx);
	float dy = y / float(this->m_ny);
untereiner's avatar
untereiner committed
117

untereiner's avatar
untereiner committed
118
	for(unsigned int i = 0; i <= this->m_ny; ++i)
untereiner's avatar
untereiner committed
119 120
    {
        for(unsigned int j = 0; j <= this->m_nx;++j)
untereiner's avatar
untereiner committed
121 122
		{
			position[this->m_tableVertDarts[i*(this->m_nx+1)+j]] = VEC3(dx*float(j) + dx*0.5f*float(i), dy*float(i) * sqrtf(3.0f)/2.0f, z);
untereiner's avatar
untereiner committed
123 124 125 126
        }
    }
}

untereiner's avatar
untereiner committed
127
template <typename PFP>
128
void Grid<PFP>::embedIntoTwistedStrip(VertexAttribute<VEC3, MAP>& position, float radius_min, float radius_max, float turns)
untereiner's avatar
untereiner committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
	float alpha = float(2.0*M_PI/this->m_ny);
	float beta = turns/float(this->m_ny);

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

	for(unsigned int i = 0; i <= this->m_ny; ++i)
	{
		for(unsigned int j = 0; j <= this->m_nx; ++j)
		{
			float rw = -rdiff + float(j)*2.0f*rdiff/float(this->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)));
			position[this->m_tableVertDarts[ i*(this->m_nx+1)+j ] ] = pos;
		}
	}

}

template <typename PFP>
150
void Grid<PFP>::embedIntoHelicoid(VertexAttribute<VEC3, MAP>& position, float radius_min,  float radius_max, float maxHeight, float nbTurn, int orient)
untereiner's avatar
untereiner committed
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
{
	float alpha = float(2.0*M_PI/this->m_nx)*nbTurn;
	float hS = maxHeight/this->m_nx;

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

	for(unsigned int i = 0; i <= this->m_ny; ++i)
	{
		for(unsigned int j = 0; j <= this->m_nx; ++j)
		{
			// 			float r = radius_max + radius_min*cos(beta*float(j));
			float r,x,y;
			// 			if(i==1) {
			// 				r = radius_max;
			// 			}
			// 			else {
			r = radius_min+(radius_max-radius_min)*float(i)/float(this->m_ny);
			// 			}
			x = orient*r*sin(alpha*float(j));
			y = orient*r*cos(alpha*float(j));

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

/*! Cylinder
 *************************************************************************/

template <typename PFP>
void Cylinder<PFP>::cylinder(unsigned int n, unsigned int z)
{
	// nb vertices
187 188
	unsigned int nbV = (n)*(z+1)+2;
	unsigned int nbF = 2*n*z;
untereiner's avatar
untereiner committed
189 190

	// vertices reservation
191 192
	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
untereiner's avatar
untereiner committed
193 194 195 196 197 198 199 200 201 202

	// creation of triangles and storing vertices
	for (unsigned int i = 0; i < z; ++i)
	{
		for (unsigned int j = 0; j < n; ++j)
		{
			Dart d = this->m_map.newFace(3, false);
			Dart d2 = this->m_map.newFace(3, false);
			this->m_map.sewFaces(this->m_map.phi1(d), this->m_map.phi_1(d2), false);
			this->m_tableVertDarts.push_back(d);
203 204
			this->m_tableFaceDarts.push_back(d);
			this->m_tableFaceDarts.push_back(d2);
untereiner's avatar
untereiner committed
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
		}
	}

	// store last row of vertices
	for (unsigned int i = 0; i < n; ++i)
	{
		this->m_tableVertDarts.push_back(this->m_map.phi_1(this->m_tableVertDarts[(z-1)*n+i]));
	}

	//sewing pairs of triangles
	for (unsigned int i = 0; i < z; ++i)
	{
		for (unsigned int j = 0; j < n; ++j)
		{

			if (i > 0) // sew with preceeding row
			{
				int pos = i*n+j;
				Dart d = this->m_tableVertDarts[pos];
				Dart e = this->m_tableVertDarts[pos-n];
				e = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(e)));
				this->m_map.sewFaces(d, e, false);
			}
			if (j > 0) // sew with preceeding column
			{
				int pos = i*n+j;
				Dart d = this->m_tableVertDarts[pos];
				d = this->m_map.phi_1(d);
				Dart e = this->m_tableVertDarts[pos-1];
				e = this->m_map.phi1(this->m_map.phi2(this->m_map.phi1(e)));
				this->m_map.sewFaces(d, e, false);
			}
			else
			{
				int pos = i*n;
				Dart d = this->m_tableVertDarts[pos];
				d = this->m_map.phi_1(d);
				Dart e = this->m_tableVertDarts[pos+(n-1)];
				e = this->m_map.phi1(this->m_map.phi2(this->m_map.phi1(e)));
				this->m_map.sewFaces(d, e, false);
			}
		}
	}

	if(m_top_closed)
		closeTop();

	if(m_bottom_closed)
		closeBottom();

	this->m_dart = this->m_tableVertDarts.front();
}

template <typename PFP>
void Cylinder<PFP>::closeTop()
{
	this->m_map.closeHole(this->m_map.phi1(this->m_map.phi2(this->m_tableVertDarts[this->m_nx*this->m_nz])));
	m_top_closed = true;
}

template <typename PFP>
void Cylinder<PFP>::triangleTop()
{
	if (m_top_closed)
	{
		Dart d =  this->m_map.phi1(this->m_map.phi2(this->m_tableVertDarts[this->m_nx*this->m_nz]));
		this->m_map.fillHole(d);

		d = this->m_map.phi2(d);
		if(this->m_map.faceDegree(d) > 3)
		{
			Algo::Surface::Modelisation::trianguleFace<PFP>(this->m_map, d);
			//this->m_tableVertDarts.push_back(this->m_map.phi_1(d));
			m_topVertDart = this->m_map.phi_1(d);
		}

		m_top_triangulated = true;
	}
}

template <typename PFP>
void Cylinder<PFP>::closeBottom()
{
	this->m_map.closeHole(this->m_tableVertDarts[0]);
	m_bottom_closed = true;
}

template <typename PFP>
void Cylinder<PFP>::triangleBottom()
{
	if (m_bottom_closed)
	{
		Dart d = this->m_tableVertDarts[0];
		this->m_map.fillHole(d);

		d = this->m_map.phi2(d);
		if(this->m_map.faceDegree(d) > 3)
		{
			Algo::Surface::Modelisation::trianguleFace<PFP>(this->m_map, d);
			//this->m_tableVertDarts.push_back(this->m_map.phi_1(d));
			m_bottomVertDart = this->m_map.phi_1(d);
		}

		m_bottom_triangulated = true;
	}
}

template <typename PFP>
313
void Cylinder<PFP>::embedIntoCylinder(VertexAttribute<VEC3, MAP>& position, float bottom_radius, float top_radius, float height)
untereiner's avatar
untereiner committed
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
{
	float alpha = float(2.0*M_PI/this->m_nx);
	float dz = height/float(this->m_nz);

	for(unsigned int i = 0; i <= this->m_nz; ++i)
	{
		float a = float(i)/float(this->m_nz);
		float radius = a*top_radius + (1.0f-a)*bottom_radius;
		for(unsigned int j = 0; j < this->m_nx; ++j)
		{

			float x = radius*cos(alpha*float(j));
			float y = radius*sin(alpha*float(j));
			position[this->m_tableVertDarts[i*(this->m_nx)+j] ] = VEC3(x, y, -height/2 + dz*float(i));
		}
	}

	//int indexUmbrella = this->m_nx*(this->m_nz+1);

	if (m_bottom_triangulated)
	{
		//position[this->m_tableVertDarts[indexUmbrella++] ] = VEC3(0.0f, 0.0f, height/2 );
		position[m_bottomVertDart] = VEC3(0.0f, 0.0f, -height/2 );
	}

	if (m_top_triangulated)
	{
		//position[this->m_tableVertDarts[indexUmbrella] ] = VEC3(0.0f, 0.0f, -height/2 );
		position[m_topVertDart] = VEC3(0.0f, 0.0f, height/2 );
	}
}

template <typename PFP>
347
void Cylinder<PFP>::embedIntoSphere(VertexAttribute<VEC3, MAP>& position, float radius)
untereiner's avatar
untereiner committed
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
{
	float alpha = float(2.0*M_PI/this->m_nx);
	float beta = float(M_PI/(this->m_nz+2));

	for(unsigned int i = 0; i <= this->m_nz; ++i)
	{
		for(unsigned int j = 0; j < this->m_nx; ++j)
		{
			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));

			position[this->m_tableVertDarts[i*(this->m_nx)+j] ] = VEC3(x, y, h );
		}
	}

	// bottom  pole
	if (m_bottom_triangulated)
	{
		//position[this->m_tableVertDarts[this->m_nx*(this->m_nz+1)] ] = VEC3(0.0f, 0.0f, radius);
		position[m_bottomVertDart] = VEC3(0.0f, 0.0f, -radius);
	}

	//  top pole
	if (m_top_triangulated)
	{
		//position[this->m_tableVertDarts[this->m_nx*(this->m_nz+1)+1] ] = VEC3(0.0f, 0.0f, -radius);
		position[m_topVertDart] = VEC3(0.0f, 0.0f, radius);
	}
}

template <typename PFP>
382
void Cylinder<PFP>::embedIntoCone(VertexAttribute<VEC3, MAP>& position, float radius, float height)
untereiner's avatar
untereiner committed
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
{
	if(m_top_closed && m_top_triangulated)
	{
		float alpha = float(2.0*M_PI/this->m_nx);
		float dz = height/float(this->m_nz+1);
		for( unsigned int i = 0; i <= this->m_nz; ++i)
		{
			for(unsigned int j = 0; j < this->m_nx; ++j)
			{
				float rad = radius * float(this->m_nz+1-i)/float(this->m_nz+1);
				float h = -height/2 + dz*float(i);
				float x = rad*cos(alpha*float(j));
				float y = rad*sin(alpha*float(j));

				position[this->m_tableVertDarts[i*(this->m_nx)+j] ] = VEC3(x, y, h);
			}
		}

		//int indexUmbrella = this->m_nx*(this->m_nz+1);
		if (m_bottom_triangulated)
		{
			//position[this->m_tableVertDarts[indexUmbrella] ] = VEC3(0.0f, 0.0f, -height/2 );
			position[m_bottomVertDart] = VEC3(0.0f, 0.0f, -height/2 );
		}

		//  top always closed in cone
		//position[this->m_tableVertDarts[indexUmbrella++] ] = VEC3(0.0f, 0.0f, height/2 );
		position[m_topVertDart] = VEC3(0.0f, 0.0f, height/2 );
	}

}

Lionel Untereiner's avatar
Lionel Untereiner committed
415 416 417 418 419 420 421 422 423 424
/*! Cube
 *************************************************************************/

template <typename PFP>
void Cube<PFP>::cube(unsigned int x, unsigned int y, unsigned int z)
{
    this->m_nx = x;
    this->m_ny = y;
    this->m_nz = z;

425 426 427 428 429
	unsigned int nbV = 2*(x+y)*(z+1) + 2*(x-1)*(y-1);
	unsigned int nbF = 2* (2*(x+y)*z + 2*x*y);

	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
Lionel Untereiner's avatar
Lionel Untereiner committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

    // we now have the 4 sides, just need to create store and sew top & bottom
    // the top
    Grid<PFP> gtop(this->m_map,x,y,false);
    std::vector<Dart>& tableTop = gtop.getVertexDarts();

    int index_side = 2*(x+y)*(z-1);
    for(unsigned int i = 0; i < x; ++i)
    {
        Dart d = this->m_tableVertDarts[index_side++];
        d = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(d)));
        Dart e = tableTop[i];
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < y; ++i)
    {
        Dart d = this->m_tableVertDarts[index_side++];
        d = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(d)));
        Dart e = tableTop[x+i*(x+1)];
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < x; ++i)
    {
        Dart d = this->m_tableVertDarts[index_side++];
        d = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(d)));
        Dart e = tableTop[(x+1)*(y+1) - 2 - i];
        e = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi_1(e)));
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < y; ++i)
    {
        Dart d = this->m_tableVertDarts[index_side++];
        d = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(d)));
        Dart e = tableTop[(y-1-i)*(x+1)];
        e = this->m_map.phi_1(e);
        this->m_map.sewFaces(d, e, false);
    }

    // the bottom
    Grid<PFP> gBottom(this->m_map,x,y,false);
    std::vector<Dart>& tableBottom = gBottom.getVertexDarts();

    index_side = 3*(x+y)+(x-1);
    for(unsigned int i = 0; i < x; ++i)
    {
        Dart d = this->m_tableVertDarts[(index_side--)%(2*(x+y))];
        Dart e = tableBottom[i];
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < y; ++i)
    {
        Dart d = this->m_tableVertDarts[(index_side--)%(2*(x+y))];
        Dart e = tableBottom[x+i*(x+1)];
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < x; ++i)
    {
        Dart d = this->m_tableVertDarts[(index_side--)%(2*(x+y))];
        Dart e = tableBottom[(x+1)*(y+1) - 2 - i];
        e = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi_1(e)));
        this->m_map.sewFaces(d, e, false);
    }
    for(unsigned int i = 0; i < y; ++i)
    {
        Dart d = this->m_tableVertDarts[(index_side--)%(2*(x+y))];
        Dart e = tableBottom[(y-1-i)*(x+1)];
        e = this->m_map.phi_1(e);
        this->m_map.sewFaces(d, e, false);
    }

    // and add new vertex in m_tableVertDarts
    //top  first
    for(unsigned int i = 1; i < y; ++i)
    {
        for(unsigned int j = 1; j < x; ++j)
            this->m_tableVertDarts.push_back(tableTop[i*(x+1)+j]);
    }

    // then bottom
    for(unsigned int i = 1; i < y; ++i)
    {
        for(unsigned int j = 1; j < x; ++j)
            this->m_tableVertDarts.push_back(tableBottom[i*(x+1)+j]);
    }

515 516 517 518 519 520 521 522 523 524 525
	const std::vector<Dart>& tableTopFaces = gTop.getFaceDarts();
	for (Dart f : tableTopFaces)
	{
		this-m_tableFaceDarts.push_back(f);
	}

	const std::vector<Dart>& tableBottomFaces = gBottom.getFaceDarts();
	for (Dart f : tableBottomFaces)
	{
		this-m_tableFaceDarts.push_back(f);
	}
Lionel Untereiner's avatar
Lionel Untereiner committed
526 527 528
}

template <typename PFP>
529
void Cube<PFP>::embedIntoCube(VertexAttribute<VEC3, MAP>& position, float sx, float sy, float sz)
Lionel Untereiner's avatar
Lionel Untereiner committed
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
{
    float dz = sz/float(this->m_nz);
    float dy = sy/float(this->m_ny);
    float dx = sx/float(this->m_nx);

    // first embedding the sides
    int index = 0;
    for (unsigned int k = 0; k <= this->m_nz; ++k)
    {
        float z = float(k)*dz - sz/2.0f;
        for (unsigned int i = 0; i < this->m_nx; ++i)
        {
            float x = float(i)*dx - sx/2.0f;
            position[this->m_tableVertDarts[ index++ ] ] = VEC3(x, -sy/2.0f, z);
        }
        for (unsigned int i = 0; i < this->m_ny; ++i)
        {
            float y = float(i)*dy - sy/2.0f;
            position[this->m_tableVertDarts[ index++ ] ] = VEC3(sx/2.0f, y, z);
        }
        for (unsigned int i = 0; i < this->m_nx; ++i)
        {
            float x = sx/2.0f-float(i)*dx;
            position[this->m_tableVertDarts[ index++ ] ] = VEC3(x, sy/2.0f, z);
        }
        for (unsigned int i = 0; i < this->m_ny ;++i)
        {
            float y = sy/2.0f - float(i)*dy;
            position[this->m_tableVertDarts[ index++ ] ] = VEC3(-sx/2.0f, y, z);
        }
    }

    // the top
    for(unsigned int i = 1; i  < this->m_ny; ++i)
    {
        for(unsigned int j = 1; j < this->m_nx; ++j)
        {
            VEC3 pos(-sx/2.0f+float(j)*dx, -sy/2.0f+float(i)*dy, sz/2.0f);
            position[this->m_tableVertDarts[ index++ ] ] = pos;
        }
    }

    // the bottom
    for(unsigned int i = 1; i < this->m_ny; ++i)
    {
        for(unsigned int j = 1; j < this->m_nx; ++j)
        {
            VEC3 pos(-sx/2.0f+float(j)*dx, sy/2.0f-float(i)*dy, -sz/2.0f);
            position[this->m_tableVertDarts[ index++ ] ] = pos;
        }
    }
}

/*! Tore
 *************************************************************************/

template <typename PFP>
void Tore<PFP>::tore(unsigned int n, unsigned int m)
{
    //this->cylinder(n, m);
    this->m_nx = n;
    this->m_ny = m;
    this->m_nz = -1;

    // just finish to sew
    for(unsigned int i = 0; i < n; ++i)
    {
        Dart d = this->m_tableVertDarts[i];
        Dart e = this->m_tableVertDarts[(m-1)*n+i];
        e = this->m_map.phi_1(this->m_map.phi2(this->m_map.phi1(e)));
        this->m_map.sewFaces(d, e);
    }

    // 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 ??
    this->m_tableVertDarts.resize(m*n);
}

template <typename PFP>
609
void Tore<PFP>::embedIntoTore(VertexAttribute<VEC3, MAP>& position, float big_radius, float small_radius)
Lionel Untereiner's avatar
Lionel Untereiner committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
{
    float alpha = float(2.0*M_PI/this->m_nx);
    float beta = float(2.0*M_PI/this->m_ny);

    for (unsigned int i = 0; i < this->m_nx; ++i)
    {
        for(unsigned int j = 0; j < this->m_ny; ++j)
        {
            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));
            position[this->m_tableVertDarts[j*(this->m_nx)+i] ] = VEC3(x, y, z);
        }
    }
}

untereiner's avatar
untereiner committed
627 628 629 630 631 632 633 634 635
} // namespace Triangular

} // namespace Tilings

} // namespace Surface

} // namespace Algo

} // namespace CGoGN