surface_modelisation.cpp 25.7 KB
Newer Older
1 2 3 4 5 6 7
#include "surface_modelisation.h"

#include "schnapps.h"
#include "view.h"
#include "mapHandler.h"

#include "Algo/Modelisation/polyhedron.h"
8 9 10 11 12
#include "Algo/Modelisation/extrusion.h"
#include <QMessageBox>
#include <QMouseEvent>
#include <iostream>
#include "Topology/generic/cellmarker.h"
13 14 15 16 17 18 19 20

namespace CGoGN
{

namespace SCHNApps
{

Surface_Modelisation_Plugin::Surface_Modelisation_Plugin()
21 22 23
{
    collect = false;
}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

bool Surface_Modelisation_Plugin::enable()
{
	m_dockTab = new Surface_Modelisation_DockTab(m_schnapps, this);
	m_schnapps->addPluginDockTab(this, m_dockTab, "Surface_Modelisation");

	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapAdded(map);

	m_dockTab->updateMapParameters();

39 40 41
    m_drawer = new Utils::Drawer();
    registerShader(m_drawer->getShader());

42 43
    mapNumber = 1;

44 45 46 47 48 49
	return true;
}

void Surface_Modelisation_Plugin::disable()
{
	disconnect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    disconnect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
    disconnect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

    delete m_drawer;
}

void Surface_Modelisation_Plugin::draw(View *view)
{
        if(collect)
        {
            m_drawer->newList(GL_COMPILE_AND_EXECUTE);
            m_drawer->pointSize(3.0f);
            m_drawer->color3f(1.0f, 1.0f, 1.0f);
            m_drawer->begin(GL_POINTS);
            for (unsigned int i = 0; i < collectedVertices.size(); i++)
                m_drawer->vertex(collectedVertices[i]);
            m_drawer->end();
            m_drawer->endList();
        }
69 70
}

71 72
void Surface_Modelisation_Plugin::drawMap(View* view, MapHandlerGen* map)
{
73

74
}
75

76 77
void Surface_Modelisation_Plugin::mousePress(View* view, QMouseEvent* event)
{
78
    if(collect && (event->button() == Qt::LeftButton))
79 80 81 82 83 84 85
    {
        qglviewer::Vec point(event->x(), event->y(), 0.5);
        qglviewer::Vec vertex = view->camera()->unprojectedCoordinatesOf(point);
        PFP2::VEC3 position(vertex.x, vertex.y, vertex.z);
        collectedVertices.push_back(position);
        view->updateGL();
    }
86

87
}
88 89 90 91 92 93 94 95 96

void Surface_Modelisation_Plugin::selectedMapChanged(MapHandlerGen *prev, MapHandlerGen *cur)
{
	m_dockTab->updateMapParameters();
}

void Surface_Modelisation_Plugin::mapAdded(MapHandlerGen* map)
{
	connect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
97 98
	connect(map, SIGNAL(cellSelectorAdded(unsigned int, const QString&)), this, SLOT(cellSelectorAdded(unsigned int, const QString&)));
	connect(map, SIGNAL(cellSelectorRemoved(unsigned int, const QString&)), this, SLOT(cellSelectorRemoved(unsigned int, const QString&)));
99 100 101 102 103
}

void Surface_Modelisation_Plugin::mapRemoved(MapHandlerGen* map)
{
	disconnect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
104 105
    disconnect(map, SIGNAL(cellSelectorAdded(unsigned int, const QString&)), this, SLOT(cellSelectorAdded(unsigned int, const QString&)));
    disconnect(map, SIGNAL(cellSelectorRemoved(unsigned int, const QString&)), this, SLOT(cellSelectorRemoved(unsigned int, const QString&)));
106 107 108 109 110 111 112 113 114
}

void Surface_Modelisation_Plugin::attributeAdded(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
	if(orbit == VERTEX && map->isSelectedMap())
		m_dockTab->addVertexAttribute(name);
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
void Surface_Modelisation_Plugin::cellSelectorAdded(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(map->isSelectedMap())
	{
		switch(orbit)
		{
			case VERTEX :
				m_dockTab->addVertexSelector(name);
				break;
			case EDGE :
				m_dockTab->addEdgeSelector(name);
				break;
			case FACE :
				m_dockTab->addFaceSelector(name);
				break;
		}
	}
}

void Surface_Modelisation_Plugin::cellSelectorRemoved(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(map->isSelectedMap())
	{
		switch(orbit)
		{
			case VERTEX :
				m_dockTab->removeVertexSelector(name);
				break;
			case EDGE :
				m_dockTab->removeEdgeSelector(name);
				break;
			case FACE :
				m_dockTab->removeFaceSelector(name);
				break;
		}
	}
}

157 158 159 160 161
void Surface_Modelisation_Plugin::changePositionAttribute(const QString& view, const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
162 163
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(name);
164 165 166 167 168 169 170 171 172 173
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

void Surface_Modelisation_Plugin::changeVertexSelector(const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].vertexSelector = mh->getCellSelector<VERTEX>(name);
176 177 178 179 180 181 182 183 184 185
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

void Surface_Modelisation_Plugin::changeEdgeSelector(const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].edgeSelector = mh->getCellSelector<EDGE>(name);
188 189 190 191 192 193 194 195 196 197
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

void Surface_Modelisation_Plugin::changeFaceSelector(const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].faceSelector = mh->getCellSelector<FACE>(name);
200 201 202 203 204
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

205 206 207
/*-------------------------------------------------------------------------------------*/
/* --------------------------------- OPERATIONS ---------------------------------------*/
/*-------------------------------------------------------------------------------------*/
208

209
/* --------------------------- general operations -------------------------------*/
210

211 212
void Surface_Modelisation_Plugin::createEmptyMap()
{
213 214
    QString mapName = "map_" + QString::number(mapNumber);
    MapHandlerGen* mhg = m_schnapps->addMap(mapName, 2);
215 216 217 218 219
	if(mhg)
	{
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);

		// add vertex position attribute
Sylvain Thery's avatar
Sylvain Thery committed
220
		VertexAttribute<PFP2::VEC3, PFP2::MAP> position = mh->addAttribute<PFP2::VEC3, VERTEX>("position");
221 222
		// update corresponding VBO & emit attribute update signal
		mh->notifyAttributeModification(position);
223
        mapNumber++;
224 225 226
	}
}

227 228 229 230 231 232
void Surface_Modelisation_Plugin::createNewFace(MapHandlerGen* mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();
    if (h_parameterSet[mhg].positionAttribute.isValid())
    {
Sylvain Thery's avatar
Sylvain Thery committed
233
		VertexAttribute<PFP2::VEC3, PFP2::MAP>& position = h_parameterSet[mhg].positionAttribute;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        if (collectedVertices.size() >= 3)
        {
            Dart d = map->newFace(collectedVertices.size());

            int i = 0;
            Traversor2FV<PFP2::MAP> t(*map, d);
            for(Dart it = t.begin(); it != t.end(); it = t.next())
            {
                position[it] = collectedVertices[i];
                i++;
            }

            mh->notifyConnectivityModification();
            mh->notifyAttributeModification(position);
            mh->updateBB(position);
            collectedVertices.clear();
        }
        else QMessageBox::information(0, "Attention", "To create a face you need at least 3 vertices");
    }

}

256
void Surface_Modelisation_Plugin::addCube(MapHandlerGen *mhg)
257 258
{
	MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
259 260 261
    PFP2::MAP* map = mh->getMap();
    if (h_parameterSet[mhg].positionAttribute.isValid())
    {
Sylvain Thery's avatar
Sylvain Thery committed
262
		VertexAttribute<PFP2::VEC3, PFP2::MAP>& position = h_parameterSet[mhg].positionAttribute;
263

264
        Algo::Surface::Modelisation::embedPrism<PFP2>(*map, position, 4, true, 0.7f, 0.7f, 1.0f);
265

266 267 268 269 270 271
        mh->notifyAttributeModification(position);
        mh->notifyConnectivityModification();
        // compute map bounding box
        mh->updateBB(position);
    }
}
272

273 274 275 276 277 278
void Surface_Modelisation_Plugin::mergeVolumes(MapHandlerGen* mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
279 280
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty() && (p.edgeSelector->getSelectedCells().size() == 2) &&
       p.faceSelector && !p.faceSelector->getSelectedCells().empty() && (p.faceSelector->getSelectedCells().size() == 2))
281
    {
282 283
		const std::vector<Edge>& selectedDarts = p.edgeSelector->getSelectedCells();
		const std::vector<Face>& selectedFaces = p.faceSelector->getSelectedCells();
284

285 286 287 288 289 290
        Dart d1 = selectedDarts[0];
        Dart d2 = selectedDarts[1];
        Dart f1 = selectedFaces[0];
        Dart f2 = selectedFaces[1];
        int i = 1, j = 1;
        bool isWellSelected = true;
291

292
        if (!(map->sameFace(d1,f1) && (map->sameFace(d2,f2))))
293
        {
294 295 296 297 298 299 300 301 302 303 304 305 306
            if (!(map->sameFace(d1,f2) && (map->sameFace(d2,f1))))
            {
                if ((map->sameFace(map->phi2(d1),f1)) || (map->sameFace(map->phi2(d1),f2)))
                    d1 = map->phi2(d1);

                if ((map->sameFace(map->phi2(d2),f1)) || (map->sameFace(map->phi2(d2),f2)))
                    d2 = map->phi2(d2);

                if (map->sameFace(d1,d2) ||
                    !((map->sameFace(d1,f1) && (map->sameFace(d2,f2))) ||
                      (map->sameFace(d1,f2) && (map->sameFace(d2,f1)))))
                    isWellSelected = false;
            }
307 308
        }

309
        if(isWellSelected)
310
        {
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            Dart it = map->phi1(d1);
            do
            {
                it = map->phi1(it);
                i++;
            }
            while(it != d1);

            it = map->phi1(d2);
            do
            {
                it = map->phi1(it);
                j++;
            }
            while(it != d2);

            if(i == j)
            {
                map->mergeVolumes(d1, d2);
                mh->notifyConnectivityModification();
            }
            else QMessageBox::information(0, "Attention", "Selected faces should have the same number of edges");
333 334 335 336 337 338 339 340 341
        }
    }
}

void Surface_Modelisation_Plugin::splitSurface(MapHandlerGen* mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

342
    MapParameters& p = h_parameterSet[mhg];
343 344
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
345 346 347 348 349
		std::vector<Edge> selectedDarts = p.edgeSelector->getSelectedCells();
		std::vector<Dart> path;
		for (std::vector<Edge>::iterator it = selectedDarts.begin(); it != selectedDarts.end(); ++it)
			path.push_back((*it).dart);

350 351 352 353 354 355 356 357 358 359 360 361
        bool isPath = true;
        unsigned int i = 0;

        if (!(map->sameVertex(path[0], map->phi1(path[path.size()-1])) ||
              map->sameVertex(path[0], map->phi_1(path[path.size()-1]))))
        {
            if (map->sameVertex(map->phi2(path[0]), map->phi1(path[path.size()-1])) ||
                map->sameVertex(map->phi2(path[0]), map->phi_1(path[path.size()-1])))
                    path[0] = map->phi2(path[0]);
            else isPath = false;
        }

Pierre Kraemer's avatar
Pierre Kraemer committed
362 363
		CellMarker<PFP2::MAP, VERTEX> vm(*map);

364 365
        if (isPath)
        {
366
			for(std::vector<Dart>::iterator it = path.begin() ; it != path.end()-1; ++it)
367 368
            {
                if (isPath)
Pierre Kraemer's avatar
Pierre Kraemer committed
369 370
				{
					if(vm.isMarked(*it))
371
                        isPath = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
372
					vm.mark(*it);
373

374
					std::vector<Dart>::iterator next;
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
                    next = it + 1 ;

                    if (!map->sameVertex(map->phi1(*it), *next))
                    {
                        if (map->sameVertex(map->phi1(*it), map->phi2(*next)))
                        {
                            path[i+1] = map->phi2(*next);
                        }
                        else isPath = false;
                    }
                    i++;
                }
                else break;
            }
        }
390

391
        if(isPath)
392
        {
393
            map->splitSurface(path);
394
            mh->notifyConnectivityModification();
395
            mh->notifyAttributeModification(p.positionAttribute);
396
        }
397
        else QMessageBox::information(0, "Attention", "To split surface you need to select edges in order without intersections and first edge should be connected with last one");
398 399 400 401 402 403 404 405 406 407 408
    }
}

void Surface_Modelisation_Plugin::extrudeRegion(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
    if(p.faceSelector && !p.faceSelector->getSelectedCells().empty())
    {
409
		const std::vector<Face>& selectedDarts = p.faceSelector->getSelectedCells();
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        Algo::Surface::Modelisation::extrudeRegion<PFP2>(*map, p.positionAttribute, selectedDarts[0], p.faceSelector->getMarker());

        mh->notifyConnectivityModification();
        mh->notifyAttributeModification(p.positionAttribute);
        mh->updateBB(p.positionAttribute);
    }
}

/* ------------------- operations with vertices ----------------------------*/

void Surface_Modelisation_Plugin::splitVertex(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty() && (p.edgeSelector->getSelectedCells().size() == 2))
    {
428
		const std::vector<Edge>& selectedEdges = p.edgeSelector->getSelectedCells();
429 430

        Dart d0, d1;
431 432 433 434
		unsigned int v00 = map->getEmbedding<VERTEX>(selectedEdges[0].dart);
		unsigned int v01 = map->getEmbedding<VERTEX>(map->phi2(selectedEdges[0].dart));
		unsigned int v10 = map->getEmbedding<VERTEX>(selectedEdges[1].dart);
		unsigned int v11 = map->getEmbedding<VERTEX>(map->phi2(selectedEdges[1].dart));
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

        if (v00 == v10)
        {
            d0 = selectedEdges[0];
            d1 = selectedEdges[1];
        }
        else if (v00 == v11)
        {
            d0 = selectedEdges[0];
            d1 = map->phi2(selectedEdges[1]);
        }
        else if (v01 == v10)
        {
            d0 = map->phi2(selectedEdges[0]);
            d1 = selectedEdges[1];
        }
        else if (v01 == v11)
        {
            d0 = map->phi2(selectedEdges[0]);
            d1 = map->phi2(selectedEdges[1]);
        }
        else
        {
            QMessageBox::information(0, "Attention", "Selected edges don't belong to the same vertex");
            return;
        }

        int n = 3;
        PFP2::VEC3 pos0 = p.positionAttribute[d0] + p.positionAttribute[map->phi1(d0)];
        Dart d = map->phi_1(d0);
        while( d != map->phi2(d1))
        {
            pos0 += p.positionAttribute[d];
            d = map->phi_1(map->phi2(d));
            n++;
        }
        pos0 += p.positionAttribute[d];
        pos0 = pos0/n;

        n = 3;
        PFP2::VEC3 pos1 = p.positionAttribute[d1] + p.positionAttribute[map->phi1(d1)];
        d = map->phi_1(d1);
        while( d != map->phi2(d0))
        {
            pos1 += p.positionAttribute[d];
            d = map->phi_1(map->phi2(d));
            n++;
        }
        pos1 += p.positionAttribute[d];
        pos1 = pos1/n;

        map->splitVertex(d0, d1);
        p.positionAttribute[d0] = pos0;
        p.positionAttribute[d1] = pos1;

        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
}

void Surface_Modelisation_Plugin::deleteVertex(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.vertexSelector && !p.vertexSelector->getSelectedCells().empty())
    {
503
		const std::vector<Vertex>& darts = p.vertexSelector->getSelectedCells();
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        map->deleteVertex(darts[0]);
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
}

/* ------------------- operations with edges ----------------------------*/

void Surface_Modelisation_Plugin::cutEdge(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
520
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
521 522
        map->cutEdge(edge[0]);

523
		p.positionAttribute[map->phi1(edge[0].dart)] = (p.positionAttribute[edge[0].dart] + p.positionAttribute[map->phi1(map->phi1(edge[0].dart))]) / 2;
524 525 526 527 528 529 530 531 532 533 534 535 536
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
}

void Surface_Modelisation_Plugin::uncutEdge(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
537
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        if (!map->uncutEdge(edge[0]))
            map->uncutEdge(map->phi2(edge[0]));
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
}

void Surface_Modelisation_Plugin::collapseEdge(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
553
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
554 555 556 557
        map->collapseEdge(edge[0]);
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
558 559 560 561 562 563 564 565 566 567
}

void Surface_Modelisation_Plugin::flipEdge(MapHandlerGen *mhg)
{
	MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
	PFP2::MAP* map = mh->getMap();

	const MapParameters& p = h_parameterSet[mhg];
	if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
	{
568
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
569 570
        map->flipEdge(edge[0]);
        mh->notifyConnectivityModification();
571
	}
572 573 574 575 576 577 578 579 580 581
}

void Surface_Modelisation_Plugin::flipBackEdge(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
582
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
        map->flipBackEdge(edge[0]);
        mh->notifyConnectivityModification();
    }
}

/* ------------------- operations with faces ----------------------------*/

void Surface_Modelisation_Plugin::splitFace(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.vertexSelector && !p.vertexSelector->getSelectedCells().empty() && (p.vertexSelector->getSelectedCells().size() == 2))
    {
598
		const std::vector<Vertex>& selectedDarts = p.vertexSelector->getSelectedCells();
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        bool sameFace = false;

        Dart d = selectedDarts[0];
        Dart it = d;
        Dart it2;
        do
        {
            Dart e = selectedDarts[1];
            it2 = e;
            do
            {
                if (map->sameFace(it,it2))
                {
                    sameFace = true;
                    break;
                }
                it2 = map->phi2(map->phi_1(it2));
            } while (it2 != e);
            if (sameFace) break;
            it = map->phi2(map->phi_1(it));
        } while (it != d);

        if (sameFace)
        {
            map->splitFace(it, it2);
            mh->notifyConnectivityModification();
        }
        else QMessageBox::information(0, "Attention", "Selected points don't belong to the same face");
    }
    else QMessageBox::information(0, "Attention", "To split a face select 2 points of the same face");
}

void Surface_Modelisation_Plugin::mergeFaces(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
639
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
640 641 642 643
        map->mergeFaces(edge[0]);
        mh->notifyConnectivityModification();
    }
}
644

645 646 647 648 649 650 651 652
void Surface_Modelisation_Plugin::deleteFace(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.faceSelector && !p.faceSelector->getSelectedCells().empty())
    {
653
		const std::vector<Face>& dart = p.faceSelector->getSelectedCells();
654 655 656
        map->deleteFace(dart[0]);
        mh->notifyConnectivityModification();
    }
657 658
}

659 660 661 662 663 664 665 666
void Surface_Modelisation_Plugin::sewFaces(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    const MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty() && (p.edgeSelector->getSelectedCells().size() == 2))
    {
667
		const std::vector<Edge>& selectedDarts = p.edgeSelector->getSelectedCells();
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        Dart d = selectedDarts[0];
        Dart e = selectedDarts[1];

        if (map->isBoundaryEdge(d) && map->isBoundaryEdge(e))
        {
            map->sewFaces(d,e);
            mh->notifyConnectivityModification();
        }
        else QMessageBox::information(0, "Attention", "One or two of selcted faces are already sewed with another face");
    }
    else QMessageBox::information(0, "Attention", "To sew faces select 2 edges which you want to connect");
}

void Surface_Modelisation_Plugin::unsewFaces(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
689
		const std::vector<Edge>& dart = p.edgeSelector->getSelectedCells();
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
        if (!map->isBoundaryEdge(dart[0]))
        {
            Dart d = dart[0];
            PFP2::VEC3 pos1 = p.positionAttribute[d];
            PFP2::VEC3 pos2 = p.positionAttribute[map->phi1(d)];
            Dart e = map->phi2(d);

            map->unsewFaces(dart[0]);

            p.positionAttribute[d] = pos1;
            p.positionAttribute[map->phi1(d)] = pos2;
            p.positionAttribute[e] = pos2;
            p.positionAttribute[map->phi1(e)] = pos1;

            mh->notifyConnectivityModification();
            mh->notifyAttributeModification(p.positionAttribute);
        }
        else QMessageBox::information(0, "Attention", "One or two of selcted faces are already unsewed");
    }
}

void Surface_Modelisation_Plugin::extrudeFace(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
    if(p.faceSelector && !p.faceSelector->getSelectedCells().empty())
    {
719
		const std::vector<Face>& selectedDart = p.faceSelector->getSelectedCells();
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

        Dart d = selectedDart[0];
        PFP2::REAL dist = 0;
        int n = 0;
        Dart it = d;
        do
        {
            dist = dist + (p.positionAttribute[it] - p.positionAttribute[map->phi1(it)]).norm();
            it = map->phi1(it);
            n++;
        } while (it != d);
        dist = dist/n;

        Algo::Surface::Modelisation::extrudeFace<PFP2>(*map, p.positionAttribute, d, dist);
        mh->notifyConnectivityModification();
        mh->notifyAttributeModification(p.positionAttribute);
        mh->updateBB(p.positionAttribute);
    }
}

void Surface_Modelisation_Plugin::pathExtrudeFace(MapHandlerGen *mhg)
{
    MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);
    PFP2::MAP* map = mh->getMap();

    MapParameters& p = h_parameterSet[mhg];
Sylvain Thery's avatar
Sylvain Thery committed
746
	VertexAttribute<PFP2::VEC3, PFP2::MAP>& position = p.positionAttribute;
747 748
    if(p.faceSelector && !p.faceSelector->getSelectedCells().empty())
    {
749
		const std::vector<Face>& selectedDart = p.faceSelector->getSelectedCells();
750 751
        Dart d = selectedDart[0];
        for (unsigned int i = 0; i < collectedVertices.size(); i++)
752
        {
753 754 755 756 757 758 759 760 761 762
            Dart it = d;
            PFP2::VEC3 center;
            int n = 0;
            do
            {
                center += position[it];
                it = map->phi1(it);
                n++;
            } while (it != d);
            center = center / n;
763

764 765 766 767 768 769
            std::vector<PFP2::VEC3> norms;
            do
            {
                norms.push_back((center - position[it]));
                it = map->phi1(it);
            } while (it != d);
770

771 772 773
            PFP2::REAL dist = 0;
            dist = (collectedVertices[i] - center).norm();
            d = Algo::Surface::Modelisation::extrudeFace<PFP2>(*map, position, d, dist);
774

775 776 777 778 779 780 781 782 783
            it = d;
            n = 0;
            do
            {
                position[it] = collectedVertices[i] - norms[n];
                it = map->phi1(it);
                n++;
            } while (it != d);
        }
784 785 786 787 788 789 790 791 792

        mh->notifyConnectivityModification();
        mh->notifyAttributeModification(position);
        mh->updateBB(position);
        collectedVertices.clear();
    }
}


793 794 795 796 797 798 799 800 801
#ifndef DEBUG
Q_EXPORT_PLUGIN2(Surface_Modelisation_Plugin, Surface_Modelisation_Plugin)
#else
Q_EXPORT_PLUGIN2(Surface_Modelisation_PluginD, Surface_Modelisation_Plugin)
#endif

} // namespace SCHNApps

} // namespace CGoGN