surface_modelisation.cpp 25.8 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

bool Surface_Modelisation_Plugin::enable()
{
27 28 29
	//	magic line that init static variables of GenericMap in the plugins
		GenericMap::copyAllStatics(m_schnapps->getStaticPointers());

30 31 32 33 34 35 36 37 38 39 40 41
	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();

42 43 44
    m_drawer = new Utils::Drawer();
    registerShader(m_drawer->getShader());

45 46
    mapNumber = 1;

47 48 49 50 51 52
	return true;
}

void Surface_Modelisation_Plugin::disable()
{
	disconnect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    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();
        }
72 73
}

74 75
void Surface_Modelisation_Plugin::drawMap(View* view, MapHandlerGen* map)
{
76

77
}
78

79 80
void Surface_Modelisation_Plugin::mousePress(View* view, QMouseEvent* event)
{
81
    if(collect && (event->button() == Qt::LeftButton))
82 83 84 85 86 87 88
    {
        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();
    }
89

90
}
91 92 93 94 95 96 97 98 99

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&)));
100 101
	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&)));
102 103 104 105 106
}

void Surface_Modelisation_Plugin::mapRemoved(MapHandlerGen* map)
{
	disconnect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
107 108
    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&)));
109 110 111 112 113 114 115 116 117
}

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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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;
		}
	}
}

160 161 162 163 164
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
165 166
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(name);
167 168 169 170 171 172 173 174 175 176
		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
177 178
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].vertexSelector = mh->getCellSelector<VERTEX>(name);
179 180 181 182 183 184 185 186 187 188
		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
189 190
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].edgeSelector = mh->getCellSelector<EDGE>(name);
191 192 193 194 195 196 197 198 199 200
		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
201 202
		MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
		h_parameterSet[m].faceSelector = mh->getCellSelector<FACE>(name);
203 204 205 206 207
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

208 209 210
/*-------------------------------------------------------------------------------------*/
/* --------------------------------- OPERATIONS ---------------------------------------*/
/*-------------------------------------------------------------------------------------*/
211

212
/* --------------------------- general operations -------------------------------*/
213

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

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

230 231 232 233 234 235
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
236
		VertexAttribute<PFP2::VEC3, PFP2::MAP>& position = h_parameterSet[mhg].positionAttribute;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        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");
    }

}

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

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

269 270 271 272 273 274
        mh->notifyAttributeModification(position);
        mh->notifyConnectivityModification();
        // compute map bounding box
        mh->updateBB(position);
    }
}
275

276 277 278 279 280 281
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];
282 283
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty() && (p.edgeSelector->getSelectedCells().size() == 2) &&
       p.faceSelector && !p.faceSelector->getSelectedCells().empty() && (p.faceSelector->getSelectedCells().size() == 2))
284
    {
285 286
		const std::vector<Edge>& selectedDarts = p.edgeSelector->getSelectedCells();
		const std::vector<Face>& selectedFaces = p.faceSelector->getSelectedCells();
287

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

295
        if (!(map->sameFace(d1,f1) && (map->sameFace(d2,f2))))
296
        {
297 298 299 300 301 302 303 304 305 306 307 308 309
            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;
            }
310 311
        }

312
        if(isWellSelected)
313
        {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
            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");
336 337 338 339 340 341 342 343 344
        }
    }
}

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

345
    MapParameters& p = h_parameterSet[mhg];
346 347
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
348 349 350 351 352
		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);

353 354 355 356 357 358 359 360 361 362 363 364
        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
365 366
		CellMarker<PFP2::MAP, VERTEX> vm(*map);

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

377
					std::vector<Dart>::iterator next;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                    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;
            }
        }
393

394
        if(isPath)
395
        {
396
            map->splitSurface(path);
397
            mh->notifyConnectivityModification();
398
            mh->notifyAttributeModification(p.positionAttribute);
399
        }
400
        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");
401 402 403 404 405 406 407 408 409 410 411
    }
}

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())
    {
412
		const std::vector<Face>& selectedDarts = p.faceSelector->getSelectedCells();
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        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))
    {
431
		const std::vector<Edge>& selectedEdges = p.edgeSelector->getSelectedCells();
432 433

        Dart d0, d1;
434 435 436 437
		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));
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

        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())
    {
506
		const std::vector<Vertex>& darts = p.vertexSelector->getSelectedCells();
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
        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())
    {
523
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
524 525
        map->cutEdge(edge[0]);

526
		p.positionAttribute[map->phi1(edge[0].dart)] = (p.positionAttribute[edge[0].dart] + p.positionAttribute[map->phi1(map->phi1(edge[0].dart))]) / 2;
527 528 529 530 531 532 533 534 535 536 537 538 539
        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())
    {
540
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        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())
    {
556
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
557 558 559 560
        map->collapseEdge(edge[0]);
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
561 562 563 564 565 566 567 568 569 570
}

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())
	{
571
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
572 573
        map->flipEdge(edge[0]);
        mh->notifyConnectivityModification();
574
	}
575 576 577 578 579 580 581 582 583 584
}

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())
    {
585
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
        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))
    {
601
		const std::vector<Vertex>& selectedDarts = p.vertexSelector->getSelectedCells();
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 639 640 641
        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())
    {
642
		const std::vector<Edge>& edge = p.edgeSelector->getSelectedCells();
643 644 645 646
        map->mergeFaces(edge[0]);
        mh->notifyConnectivityModification();
    }
}
647

648 649 650 651 652 653 654 655
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())
    {
656
		const std::vector<Face>& dart = p.faceSelector->getSelectedCells();
657 658 659
        map->deleteFace(dart[0]);
        mh->notifyConnectivityModification();
    }
660 661
}

662 663 664 665 666 667 668 669
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))
    {
670
		const std::vector<Edge>& selectedDarts = p.edgeSelector->getSelectedCells();
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
        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())
    {
692
		const std::vector<Edge>& dart = p.edgeSelector->getSelectedCells();
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 719 720 721
        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())
    {
722
		const std::vector<Face>& selectedDart = p.faceSelector->getSelectedCells();
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

        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
749
	VertexAttribute<PFP2::VEC3, PFP2::MAP>& position = p.positionAttribute;
750 751
    if(p.faceSelector && !p.faceSelector->getSelectedCells().empty())
    {
752
		const std::vector<Face>& selectedDart = p.faceSelector->getSelectedCells();
753 754
        Dart d = selectedDart[0];
        for (unsigned int i = 0; i < collectedVertices.size(); i++)
755
        {
756 757 758 759 760 761 762 763 764 765
            Dart it = d;
            PFP2::VEC3 center;
            int n = 0;
            do
            {
                center += position[it];
                it = map->phi1(it);
                n++;
            } while (it != d);
            center = center / n;
766

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

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

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

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


796 797 798 799 800
Q_EXPORT_PLUGIN2(Surface_Modelisation_Plugin, Surface_Modelisation_Plugin)

} // namespace SCHNApps

} // namespace CGoGN