square.hpp 19.4 KB
Newer Older
Lionel Untereiner's avatar
Lionel 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Modelisation/subdivision.h"

namespace CGoGN
{

namespace Algo
{

namespace Surface
{

namespace Tilings
{

namespace Square
{

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

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

    // vertice reservation
53 54
	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
Lionel Untereiner's avatar
Lionel Untereiner committed
55 56 57 58 59 60 61 62 63 64

    // creation of quads 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(4, false);
            this->m_tableVertDarts.push_back(d);
            if (j == x)
                this->m_tableVertDarts.push_back(this->m_map.phi1(d));
65
			this->m_tableFaceDarts.push_back(d);
Lionel Untereiner's avatar
Lionel Untereiner committed
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
        }
    }

    // store last row of vertices
    for (unsigned int i = 0; i < x; ++i)
    {
        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]) );

    //sewing the quads
    for (unsigned int i = 0; i < y; ++i)
    {
        for (unsigned int j = 0; j < x; ++j)
        {
            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.phi1(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(e);
                this->m_map.sewFaces(d, e, false);
            }
        }
    }

untereiner's avatar
untereiner committed
101
    if(close)
Lionel Untereiner's avatar
Lionel Untereiner committed
102
        this->m_map.closeHole(this->m_tableVertDarts[0]) ;
untereiner's avatar
untereiner committed
103 104

    this->m_dart = this->m_tableVertDarts[0];
Lionel Untereiner's avatar
Lionel Untereiner committed
105 106 107
}

template <typename PFP>
108
void Grid<PFP>::embedIntoGrid(VertexAttribute<VEC3, MAP>& position, float x, float y, float z)
Lionel Untereiner's avatar
Lionel Untereiner committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122
{
    float dx = x / float(this->m_nx);
    float dy = y / float(this->m_ny);

    for(unsigned int i = 0; i <= this->m_ny; ++i)
    {
        for(unsigned int j = 0; j <= this->m_nx;++j)
        {
            position[this->m_tableVertDarts[i*(this->m_nx+1)+j] ] = VEC3(-x/2 + dx*float(j), -y/2 + dy*float(i), z);
        }
    }
}

template <typename PFP>
123
void Grid<PFP>::embedIntoTwistedStrip(VertexAttribute<VEC3, MAP>& position, float radius_min, float radius_max, float turns)
Lionel Untereiner's avatar
Lionel Untereiner committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
    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>
144
void Grid<PFP>::embedIntoHelicoid(VertexAttribute<VEC3, MAP>& position, float radius_min,  float radius_max, float maxHeight, float nbTurn, int orient)
Lionel Untereiner's avatar
Lionel Untereiner committed
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
{
    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)
{
180 181
	unsigned int nbV = (n)*(z+1)+2;
	unsigned int nbF = n*z;
Lionel Untereiner's avatar
Lionel Untereiner committed
182 183

    // vertice reservation
184 185
	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
Lionel Untereiner's avatar
Lionel Untereiner committed
186 187 188 189 190 191 192 193

    // creation of quads 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(4, false);
            this->m_tableVertDarts.push_back(d);
194
			this->m_tableFaceDarts.push_back(d);
Lionel Untereiner's avatar
Lionel Untereiner committed
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
        }
    }

    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 the quads
    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.phi1(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(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(e);
                this->m_map.sewFaces(d, e, false);
            }
        }
    }
untereiner's avatar
untereiner committed
236 237 238 239 240 241

    if(m_top_closed)
        closeTop();

    if(m_bottom_closed)
        closeBottom();
untereiner's avatar
untereiner committed
242 243

    this->m_dart = this->m_tableVertDarts.front();
Lionel Untereiner's avatar
Lionel Untereiner committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

template <typename PFP>
void Cylinder<PFP>::closeTop()
{
    this->m_map.closeHole(this->m_map.phi_1(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.phi_1(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);
untereiner's avatar
untereiner committed
265 266
            //this->m_tableVertDarts.push_back(this->m_map.phi_1(d));
            m_topVertDart = this->m_map.phi_1(d);
Lionel Untereiner's avatar
Lionel Untereiner committed
267 268
        }

untereiner's avatar
untereiner committed
269
        m_top_triangulated = true;
Lionel Untereiner's avatar
Lionel Untereiner committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    }
}

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);
untereiner's avatar
untereiner committed
292 293
            //this->m_tableVertDarts.push_back(this->m_map.phi_1(d));
            m_bottomVertDart = this->m_map.phi_1(d);
Lionel Untereiner's avatar
Lionel Untereiner committed
294 295
        }

untereiner's avatar
untereiner committed
296
        m_bottom_triangulated = true;
Lionel Untereiner's avatar
Lionel Untereiner committed
297 298 299 300
    }
}

template <typename PFP>
301
void Cylinder<PFP>::embedIntoCylinder(VertexAttribute<VEC3, MAP>& position, float bottom_radius, float top_radius, float height)
Lionel Untereiner's avatar
Lionel Untereiner committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
    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));
        }
    }

untereiner's avatar
untereiner committed
319
    //int indexUmbrella = this->m_nx*(this->m_nz+1);
Lionel Untereiner's avatar
Lionel Untereiner committed
320

untereiner's avatar
untereiner committed
321
    if (m_bottom_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
322
    {
untereiner's avatar
untereiner committed
323 324
        //position[this->m_tableVertDarts[indexUmbrella++] ] = VEC3(0.0f, 0.0f, height/2 );
        position[m_bottomVertDart] = VEC3(0.0f, 0.0f, -height/2 );
Lionel Untereiner's avatar
Lionel Untereiner committed
325 326
    }

untereiner's avatar
untereiner committed
327
    if (m_top_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
328
    {
untereiner's avatar
untereiner committed
329 330
        //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
331 332 333 334
    }
}

template <typename PFP>
335
void Cylinder<PFP>::embedIntoSphere(VertexAttribute<VEC3, MAP>& position, float radius)
Lionel Untereiner's avatar
Lionel Untereiner committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
    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
untereiner's avatar
untereiner committed
355
    if (m_bottom_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
356
    {
untereiner's avatar
untereiner committed
357 358
        //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);
Lionel Untereiner's avatar
Lionel Untereiner committed
359 360 361
    }

    //  top pole
untereiner's avatar
untereiner committed
362
    if (m_top_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
363
    {
untereiner's avatar
untereiner committed
364 365
        //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);
Lionel Untereiner's avatar
Lionel Untereiner committed
366 367 368 369
    }
}

template <typename PFP>
370
void Cylinder<PFP>::embedIntoCone(VertexAttribute<VEC3, MAP>& position, float radius, float height)
Lionel Untereiner's avatar
Lionel Untereiner committed
371
{
untereiner's avatar
untereiner committed
372
    if(m_top_closed && m_top_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    {
        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);
            }
        }

untereiner's avatar
untereiner committed
389 390
        //int indexUmbrella = this->m_nx*(this->m_nz+1);
        if (m_bottom_triangulated)
Lionel Untereiner's avatar
Lionel Untereiner committed
391
        {
untereiner's avatar
untereiner committed
392 393
            //position[this->m_tableVertDarts[indexUmbrella] ] = VEC3(0.0f, 0.0f, -height/2 );
            position[m_bottomVertDart] = VEC3(0.0f, 0.0f, -height/2 );
Lionel Untereiner's avatar
Lionel Untereiner committed
394 395 396
        }

        //  top always closed in cone
untereiner's avatar
untereiner committed
397 398
        //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
399
    }
untereiner's avatar
untereiner committed
400

Lionel Untereiner's avatar
Lionel Untereiner committed
401 402 403 404 405 406 407 408
}

/*! Cube
 *************************************************************************/

template <typename PFP>
void Cube<PFP>::cube(unsigned int x, unsigned int y, unsigned int z)
{
untereiner's avatar
untereiner committed
409
    //this->cylinder(2*(x+y), z, false, false);
Lionel Untereiner's avatar
Lionel Untereiner committed
410 411 412 413
    this->m_nx = x;
    this->m_ny = y;
    this->m_nz = z;

414 415 416 417 418
	unsigned int nbV = 2*(x+y)*(z+1) + 2*(x-1)*(y-1);
	unsigned int nbF = 2*(x+y)*z + 2*x*y;

	this->m_tableVertDarts.reserve(nbV);
	this->m_tableFaceDarts.reserve(nbF);
Lionel Untereiner's avatar
Lionel Untereiner committed
419 420 421

    // we now have the 4 sides, just need to create store and sew top & bottom
    // the top
422 423
	Grid<PFP> gTop(this->m_map,x,y,false);
	std::vector<Dart>& tableTop = gTop.getVertexDarts();
Lionel Untereiner's avatar
Lionel Untereiner committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

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

    // the bottom
untereiner's avatar
untereiner committed
454
    Grid<PFP> gBottom(this->m_map,x,y,false);
Lionel Untereiner's avatar
Lionel Untereiner committed
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
    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(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]);
    }

500 501 502 503 504 505 506 507 508 509 510
	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
511 512 513
}

template <typename PFP>
514
void Cube<PFP>::embedIntoCube(VertexAttribute<VEC3, MAP>& position, float sx, float sy, float sz)
Lionel Untereiner's avatar
Lionel Untereiner committed
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
{
    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
untereiner's avatar
untereiner committed
548
    for(unsigned int i = 1; i  < this->m_ny; ++i)
Lionel Untereiner's avatar
Lionel Untereiner committed
549
    {
untereiner's avatar
untereiner committed
550
        for(unsigned int j = 1; j < this->m_nx; ++j)
Lionel Untereiner's avatar
Lionel Untereiner committed
551 552 553 554 555 556 557
        {
            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
untereiner's avatar
untereiner committed
558
    for(unsigned int i = 1; i < this->m_ny; ++i)
Lionel Untereiner's avatar
Lionel Untereiner committed
559
    {
untereiner's avatar
untereiner committed
560
        for(unsigned int j = 1; j < this->m_nx; ++j)
Lionel Untereiner's avatar
Lionel Untereiner committed
561 562 563 564 565 566 567 568 569 570 571 572 573
        {
            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)
{
untereiner's avatar
untereiner committed
574
    //this->cylinder(n, m);
Lionel Untereiner's avatar
Lionel Untereiner committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    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*n)+i];
        e = this->m_map.phi_1(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 ??
untereiner's avatar
untereiner committed
590
    this->m_tableVertDarts.resize(m*n);
Lionel Untereiner's avatar
Lionel Untereiner committed
591 592 593
}

template <typename PFP>
594
void Tore<PFP>::embedIntoTore(VertexAttribute<VEC3, MAP>& position, float big_radius, float small_radius)
Lionel Untereiner's avatar
Lionel Untereiner committed
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
{
    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);
        }
    }
}

} // namespace Square

} // namespace Tilings

} // namespace Surface

} // namespace Algo

} // namespace CGoGN