surface_modelisation.cpp 25.6 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
void Surface_Modelisation_Plugin::changePositionAttribute(const QString& view, const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		h_parameterSet[m].positionAttribute = m->getAttribute<PFP2::VEC3, VERTEX>(name);
		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)
	{
		MapParameters& p = h_parameterSet[m];
		p.vertexSelector = m->getCellSelector<VERTEX>(name);
		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)
	{
		MapParameters& p = h_parameterSet[m];
		p.edgeSelector = m->getCellSelector<EDGE>(name);
		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)
	{
		MapParameters& p = h_parameterSet[m];
		p.faceSelector = m->getCellSelector<FACE>(name);
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

204
205
206
/*-------------------------------------------------------------------------------------*/
/* --------------------------------- OPERATIONS ---------------------------------------*/
/*-------------------------------------------------------------------------------------*/
207

208
/* --------------------------- general operations -------------------------------*/
209

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

		// add vertex position attribute
		VertexAttribute<PFP2::VEC3> position = mh->addAttribute<PFP2::VEC3, VERTEX>("position");
		// update corresponding VBO & emit attribute update signal
		mh->notifyAttributeModification(position);
222
        mapNumber++;
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
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())
    {
        VertexAttribute<PFP2::VEC3>& position = h_parameterSet[mhg].positionAttribute;
        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");
    }

}

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

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

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

272
273
274
275
276
277
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];
278
279
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty() && (p.edgeSelector->getSelectedCells().size() == 2) &&
       p.faceSelector && !p.faceSelector->getSelectedCells().empty() && (p.faceSelector->getSelectedCells().size() == 2))
280
281
    {
        const std::vector<Dart>& selectedDarts = p.edgeSelector->getSelectedCells();
282
        const std::vector<Dart>& selectedFaces = p.faceSelector->getSelectedCells();
283

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

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

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

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

341
    MapParameters& p = h_parameterSet[mhg];
342
343
344
    if(p.edgeSelector && !p.edgeSelector->getSelectedCells().empty())
    {
        std::vector<Dart> selectedDarts = p.edgeSelector->getSelectedCells();
345
346
347
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
382
383
384
        std::vector<Dart> path = selectedDarts;
        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;
        }

        if (isPath)
        {
            for(std::vector<Dart>::iterator it = path.begin() ; it != path.end()-1; ++it)
            {
                if (isPath)
                {
                    CellMarker<VERTEX>* vm = new CellMarker<VERTEX>(*map);
                    if(vm->isMarked(*it))
                        isPath = false;
                    vm->mark(*it);

                    std::vector<Dart>::iterator next;
                    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;
            }
        }
385

386
        if(isPath)
387
        {
388
            map->splitSurface(path);
389
            mh->notifyConnectivityModification();
390
            mh->notifyAttributeModification(p.positionAttribute);
391
        }
392
        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");
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    }
}

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())
    {
        const std::vector<Dart>& selectedDarts = p.faceSelector->getSelectedCells();
        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))
    {
        const std::vector<Dart>& selectedEdges = p.edgeSelector->getSelectedCells();

        Dart d0, d1;
        unsigned int v00 =  map->getEmbedding<VERTEX>(selectedEdges[0]);
        unsigned int v01 =  map->getEmbedding<VERTEX>(map->phi2(selectedEdges[0]));
        unsigned int v10 =  map->getEmbedding<VERTEX>(selectedEdges[1]);
        unsigned int v11 =  map->getEmbedding<VERTEX>(map->phi2(selectedEdges[1]));

        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())
    {
        const std::vector<Dart>& darts = p.vertexSelector->getSelectedCells();
        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())
    {
        const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
        map->cutEdge(edge[0]);

        p.positionAttribute[map->phi1(edge[0])] = (p.positionAttribute[edge[0]] + p.positionAttribute[map->phi1(map->phi1(edge[0]))]) / 2;
        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())
    {
        const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
        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())
    {
        const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
        map->collapseEdge(edge[0]);
        mh->notifyAttributeModification(p.positionAttribute);
        mh->notifyConnectivityModification();
    }
553
554
555
556
557
558
559
560
561
562
563
}

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())
	{
		const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
564
565
        map->flipEdge(edge[0]);
        mh->notifyConnectivityModification();
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
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
}

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())
    {
        const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
        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))
    {
        const std::vector<Dart>& selectedDarts = p.vertexSelector->getSelectedCells();
        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())
    {
        const std::vector<Dart>& edge = p.edgeSelector->getSelectedCells();
        map->mergeFaces(edge[0]);
        mh->notifyConnectivityModification();
    }
}
639

640
641
642
643
644
645
646
647
648
649
650
651
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())
    {
        const std::vector<Dart>& dart = p.faceSelector->getSelectedCells();
        map->deleteFace(dart[0]);
        mh->notifyConnectivityModification();
    }
652
653
}

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
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
719
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
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))
    {
        const std::vector<Dart>& selectedDarts = p.edgeSelector->getSelectedCells();
        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())
    {
        const std::vector<Dart>& dart = p.edgeSelector->getSelectedCells();
        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())
    {
        const std::vector<Dart>& selectedDart = p.faceSelector->getSelectedCells();

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

759
760
761
762
763
764
            std::vector<PFP2::VEC3> norms;
            do
            {
                norms.push_back((center - position[it]));
                it = map->phi1(it);
            } while (it != d);
765

766
767
768
            PFP2::REAL dist = 0;
            dist = (collectedVertices[i] - center).norm();
            d = Algo::Surface::Modelisation::extrudeFace<PFP2>(*map, position, d, dist);
769

770
771
772
773
774
775
776
777
778
            it = d;
            n = 0;
            do
            {
                position[it] = collectedVertices[i] - norms[n];
                it = map->phi1(it);
                n++;
            } while (it != d);
        }
779
780
781
782
783
784
785
786
787

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


788
789
790
791
792
793
794
795
796
#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