tuto_oper3.cpp 25.6 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery 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
/*******************************************************************************
* 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 "tuto_oper3.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Modelisation/polyhedron.h"
28
#include "Algo/Modelisation/tetrahedralization.h"
untereiner's avatar
untereiner committed
29
#include "Algo/Tiling/Volume/cubic.h"
Sylvain Thery's avatar
Sylvain Thery committed
30
#include "Algo/Geometry/centroid.h"
untereiner's avatar
untereiner committed
31
#include "Algo/Geometry/normal.h"
Sylvain Thery's avatar
Sylvain Thery committed
32
33
34
35
36
37
38
39
#include "Algo/Import/import.h"
#include "Algo/Export/export.h"

using namespace CGoGN ;


int main(int argc, char **argv)
{
untereiner's avatar
untereiner committed
40
41
42
    //	// interface
    QApplication app(argc, argv);
    MyQT sqt;
Sylvain Thery's avatar
Sylvain Thery committed
43
44
45
46
47

    sqt.setDock(& sqt.dock);
    sqt.setCallBack( sqt.dock.listOper, SIGNAL(currentRowChanged(int)), SLOT(operation(int)) );
    sqt.setCallBack( sqt.dock.svg, SIGNAL(clicked()), SLOT(svg()) );
    sqt.setCallBack( sqt.dock.widthSlider, SIGNAL(valueChanged(int)), SLOT(width(int)) );
untereiner's avatar
untereiner committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    sqt.setCallBack( sqt.dock.checkBox_hide, SIGNAL(toggled(bool)), SLOT(hide_onoff(bool)) );
    sqt.setCallBack( sqt.dock.checkBox_plane, SIGNAL(toggled(bool)), SLOT(clipping_onoff(bool)) );
    int n=3;
    if (argc==2)
        n = atoi(argv[1]);

    // example code itself
    sqt.createMap(n);

    sqt.width(5);

    // set help message in menu
    sqt.setHelpMsg("First Tuto: \nCreate two faces\nsew them\nand affect positions");
    // final show for redraw
    sqt.show();
    // and wait for the end
Sylvain Thery's avatar
Sylvain Thery committed
64
	sqt.clipping_onoff(true);
untereiner's avatar
untereiner committed
65
    return app.exec();
Sylvain Thery's avatar
Sylvain Thery committed
66
67
}

68
69
void MyQT::clipping_onoff(bool x)
{
untereiner's avatar
untereiner committed
70
71
72
73
74
75
76
    clip_volume = x;

    if (clip_volume)
    {
        Geom::Vec3f pos = m_PlanePick->getPosition();
        float pipo;
        Geom::Vec3f normal = m_PlanePick->getAxisScale(2, pipo); // 2 = Z axis = plane normal
Sylvain Thery's avatar
Sylvain Thery committed
77
78
79
		float d = -(pos*normal);
		m_render_topo->setClippingPlane(Geom::Vec4f(normal[0], normal[1], normal[2], d));
		m_render_topo_boundary->setClippingPlane(Geom::Vec4f(normal[0], normal[1], normal[2], d));
untereiner's avatar
untereiner committed
80
81
    }
    else
Sylvain Thery's avatar
Sylvain Thery committed
82
83
84
	{
		m_render_topo->setNoClippingPlane();
	}
untereiner's avatar
untereiner committed
85
86
    updateMap();
    updateGL();
87
88
}

89
void MyQT::hide_onoff(bool /*x*/)
90
{
untereiner's avatar
untereiner committed
91
92
93
    hide_clipping = !hide_clipping;
    updateMap();
    updateGL();
94
95
}

Sylvain Thery's avatar
Sylvain Thery committed
96
97
void MyQT::operation(int x)
{
untereiner's avatar
untereiner committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
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
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
236
237
238
239
240
241
    switch(x)
    {
        case 0:
            CGoGNout <<"delete vertex"<<CGoGNendl;
            if (m_selected != NIL)
            {
                m_selected=myMap.deleteVertex(m_selected);
                m_selected = NIL;
                updateMap();
            }
            break;
        case 1:
            CGoGNout <<"cut edge"<<CGoGNendl;
            if (m_selected != NIL)
            {
                PFP::VEC3 Q = position[myMap.phi1(m_selected)];
                myMap.cutEdge(m_selected);
                position[myMap.phi1(m_selected)] = (position[m_selected] + Q)/2.0f;
                updateMap();
            }
            break;
        case 2:
            CGoGNout <<"uncut edge"<<CGoGNendl;
            if (m_selected != NIL)
            {
                myMap.uncutEdge(m_selected);
                updateMap();
            }
            break;
        case 3:
            CGoGNout <<"delete edge"<<CGoGNendl;
            if (m_selected != NIL)
            {
                if (myMap.deleteEdgePreCond(m_selected))
                {
                    myMap.deleteEdge(m_selected);
                    m_selected = NIL;
                    updateMap();
                }
                else
                {
                    CGoGNerr << "Could not delete this edge" << CGoGNendl;
                }
            }
            break;
        case 4:
            CGoGNout <<"collapse edge"<<CGoGNendl;
            if (m_selected != NIL)
            {
                PFP::VEC3 Q = (position[myMap.phi1(m_selected)] + position[m_selected])/2.0f;
                Dart x = myMap.collapseEdge(m_selected);
                position[x]= Q;
                m_selected = NIL;
                updateMap();
            }
            break;
        case 5:
            CGoGNout <<"split face"<<CGoGNendl;
            if (m_selected != NIL)
            {
                myMap.splitFace(m_selected,m_selected2);
                updateMap();
            }
            break;
        case 6:
            CGoGNout <<"merge volume"<<CGoGNendl;
            if (m_selected != NIL)
            {
                myMap.mergeVolumes(m_selected);
                updateMap();
            }
            break;

        case 7:
            CGoGNout <<"split volume"<<CGoGNendl;
            if (!m_selecteds.empty())
            {
                std::cout << "start" << std::endl;
                for(std::vector<Dart>::iterator it = m_selecteds.begin() ; it != m_selecteds.end() ; ++it)
                    std::cout << *it << " et phi2() = " << myMap.phi2(*it) <<  std::endl;
                std::cout << "end" << std::endl;

                myMap.splitVolume(m_selecteds);
                m_selecteds.clear();
                updateMap();
            }
            break;
        case 8:
            CGoGNout <<"collapse face"<<CGoGNendl;
            if (m_selected != NIL)
            {
                PFP::VEC3 Q = Algo::Surface::Geometry::faceCentroid<PFP>(myMap,m_selected,position);
                Dart x = myMap.collapseFace(m_selected);
                position[x]= Q;
                m_selected = NIL;
                updateMap();
            }
            break;
        case 9:
            CGoGNout <<"collapse volume"<<CGoGNendl;
            if (m_selected != NIL)
            {
                PFP::VEC3 Q = Algo::Surface::Geometry::volumeCentroid<PFP>(myMap,m_selected,position);
                Dart x = myMap.collapseVolume(m_selected);
                position[x]= Q;
                m_selected = NIL;
                updateMap();
            }
            break;
        case 10:
            CGoGNout <<"split vertex"<<CGoGNendl;
            if (!m_selecteds.empty() && m_selected != NIL)
            {
                Dart dit = m_selecteds.front();
                PFP::VEC3 Q = (position[myMap.phi1(m_selected)] + position[m_selected])/2.0f;
                //PFP::VEC3 c1 = Algo::Geometry::volumeCentroid<PFP>(myMap, dit, position);
                Dart dres = myMap.splitVertex(m_selecteds);
                //Dart dres = Algo::Modelisation::Tetrahedralization::splitVertex<PFP>(myMap, m_selecteds);
                position[dres] = position[dit] + Q*0.25f;
                //position[dit] = position[dit] - c1*0.5f;
                m_selecteds.clear();
                m_selected = NIL;
                updateMap();
                std::cout << "nb darts after = " << myMap.getNbDarts() << std::endl;
            }
            break;
        case 11 :
            CGoGNout <<"unsew volumes"<<CGoGNendl;
            if (m_selected != NIL)
            {
                myMap.unsewVolumes(m_selected);
                m_selected = NIL;
                updateMap();
            }
            break;
        case 12 :
            CGoGNout <<"delete volume"<<CGoGNendl;
            if (m_selected != NIL)
            {
                myMap.deleteVolume(m_selected);
                m_selected = NIL;
                updateMap();
            }
            break;
Lionel Untereiner's avatar
Lionel Untereiner committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
		case 13 :
			CGoGNout <<"split volume"<<CGoGNendl;
			if (!m_selecteds.empty())
			{
				myMap.splitVolume(m_selecteds);
				m_selecteds.clear();
				updateMap();
			}
			break;
		case 14 :
			CGoGNout <<"cut volume"<<CGoGNendl;
			if (!m_selecteds.empty())
			{
				myMap.cutVolume(m_selecteds);
				m_selecteds.clear();
				updateMap();
			}
			break;
untereiner's avatar
untereiner committed
260
261
262
263
264
265
        default:
            break;
    }

    updateGL();
    dock.listOper->setCurrentRow(-1);
Sylvain Thery's avatar
Sylvain Thery committed
266
267
}

268
void MyQT::createMap(int n)
Sylvain Thery's avatar
Sylvain Thery committed
269
{
untereiner's avatar
untereiner committed
270
    myMap.clear(true);
271
	position = myMap.getAttribute<VEC3, VERTEX, MAP>("position");
untereiner's avatar
untereiner committed
272
    if (!position.isValid())
273
		position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
untereiner's avatar
untereiner committed
274
275
    Algo::Volume::Tilings::Cubic::Grid<PFP> cubic(myMap, n, n, n);
    cubic.embedIntoGrid(position, 1.0f, 1.0f, 1.0f);
untereiner's avatar
untereiner committed
276

untereiner's avatar
untereiner committed
277
278
    //	Dart d = Algo::Surface::Modelisation::embedPrism<PFP>(myMap, position, 5, true,6.0,6.0,5.0);
    //	Dart d2 = Algo::Surface::Modelisation::embedPyramid<PFP>(myMap, position, 4, true,6.0,5.0);
279

untereiner's avatar
untereiner committed
280
281
    //	myMap.sewVolumes(myMap.phi2(d),d2);
    //	position[myMap.phi_1(myMap.phi2(d2))] += VEC3(9.0,-5.0,-2.5);
Sylvain Thery's avatar
Sylvain Thery committed
282

untereiner's avatar
untereiner committed
283
    myMap.check();
284

Sylvain Thery's avatar
Sylvain Thery committed
285
    //  bounding box of scene
untereiner's avatar
untereiner committed
286
287
288
    bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
    setParamObject(bb.maxSize(), bb.center().data()) ;
    m_shift = bb.maxSize()/200.0f;
Sylvain Thery's avatar
Sylvain Thery committed
289

untereiner's avatar
untereiner committed
290
291
    // first show for be sure that GL context is binded
    show();
Sylvain Thery's avatar
Sylvain Thery committed
292

untereiner's avatar
untereiner committed
293
    // render the topo of the map without boundary darts
Sylvain Thery's avatar
Sylvain Thery committed
294

untereiner's avatar
untereiner committed
295
296
297
    m_render_topo->setDartWidth(3.0f);
    m_render_topo->setInitialDartsColor(0.0f,0.0f,0.0f);
    updateMap();
Sylvain Thery's avatar
Sylvain Thery committed
298
299
}

untereiner's avatar
untereiner committed
300
301
302
void MyQT::createMapTets()
{
    myMap.clear(true);
303
	position = myMap.getAttribute<VEC3, VERTEX, MAP>("position");
untereiner's avatar
untereiner committed
304
    if (!position.isValid())
305
		position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
untereiner's avatar
untereiner committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

    Dart dd = Algo::Surface::Modelisation::createTetrahedron<PFP>(myMap);
    myMap.closeMap();
    Dart ee =Algo::Surface::Modelisation::createTetrahedron<PFP>(myMap);
    myMap.closeMap();
    myMap.sewVolumes(dd,ee);

    Dart t1 = dd;
    position[t1] = VEC3(0.0f, 0.0f, 0.0f);
    t1 = myMap.phi1(t1);
    position[t1] = VEC3(0.0f, 1.0f, 0.0f);
    t1 = myMap.phi1(t1);
    position[t1] = VEC3(1.0f, 0.5f, 0.0f);
    t1 = myMap.phi_1(myMap.phi2(dd));
    position[t1] = VEC3(0.f, 0.5f, 1.0f);

    Dart t2 = ee;
    position[t2] = VEC3(0.0f, 1.0f, 0.0f);
    t2 = myMap.phi1(t2);
    position[t2] = VEC3(0.0f, 0.0f, 0.0f);
    t2 = myMap.phi1(t2);
    position[t2] = VEC3(1.0f, 0.5f, 0.0f);
    t2 = myMap.phi_1(myMap.phi2(ee));
    position[t2] = VEC3(0.f, 0.5f, -1.0f);

    myMap.check();

    //  bounding box of scene
    bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
    setParamObject(bb.maxSize(), bb.center().data()) ;
    m_shift = bb.maxSize()/200.0f;

    // first show for be sure that GL context is binded
    show();

    // render the topo of the map without boundary darts

    m_render_topo->setDartWidth(3.0f);
    m_render_topo->setInitialDartsColor(0.0f,0.0f,0.0f);
    updateMap();
}

Sylvain Thery's avatar
Sylvain Thery committed
348
349
void MyQT::updateMap()
{
untereiner's avatar
untereiner committed
350
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
351
	m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
352
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
353
	m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
354
#endif
355
	m_render_topo_boundary->updateDataBoundary<PFP>(myMap,position,m_ex1,m_ex2,m_shift);
Sylvain Thery's avatar
Sylvain Thery committed
356
357
358
359
360
}

// initialization GL callback
void MyQT::cb_initGL()
{
untereiner's avatar
untereiner committed
361
    glClearColor(1.0f,1.0f,1.0f,1.0f);
362

untereiner's avatar
untereiner committed
363
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	m_render_topo = new Algo::Render::GL2::Topo3PrimalRender<PFP>() ;
untereiner's avatar
untereiner committed
365
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
366
	m_render_topo = new Algo::Render::GL2::Topo3RenderMap<PFP>() ;
untereiner's avatar
untereiner committed
367
#endif
368
	m_render_topo_boundary = new Algo::Render::GL2::TopoRender();
untereiner's avatar
untereiner committed
369
    m_render_topo_boundary->setInitialDartsColor(0.0f,0.9f,0.0f);
370

371
    m_PlanePick = new Utils::Pickable(Utils::Pickable::GRID,1);
untereiner's avatar
untereiner committed
372
373
    m_frame = new Utils::FrameManipulator();
    m_frame->setSize(bb.maxSize());
Sylvain Thery's avatar
Sylvain Thery committed
374
375
376
377
378
}

// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{
untereiner's avatar
untereiner committed
379
380
    glEnable( GL_POLYGON_OFFSET_FILL );
    glPolygonOffset( 1.0f, 1.0f );
Sylvain Thery's avatar
Sylvain Thery committed
381

untereiner's avatar
untereiner committed
382
    m_render_topo->drawTopo();
Sylvain Thery's avatar
Sylvain Thery committed
383

untereiner's avatar
untereiner committed
384
    m_render_topo_boundary->drawTopo();
385

untereiner's avatar
untereiner committed
386
    glDisable( GL_POLYGON_OFFSET_FILL );
Sylvain Thery's avatar
Sylvain Thery committed
387

untereiner's avatar
untereiner committed
388
389
    if (m_selected != NIL)
        m_render_topo->overdrawDart(m_selected, 11, 1.0f, 0.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
390

untereiner's avatar
untereiner committed
391
392
    if (m_selected2 != NIL)
        m_render_topo->overdrawDart(m_selected2, 11, 0.0f, 1.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
393

untereiner's avatar
untereiner committed
394
395
396
397
    for (std::vector<Dart>::iterator it = m_selecteds.begin(); it != m_selecteds.end(); ++it)
    {
        m_render_topo->overdrawDart(*it, 11, 0.0f, 0.0f, 1.0f);
    }
398

untereiner's avatar
untereiner committed
399
400
401
402
403
    if (clip_volume && !hide_clipping)
    {
        m_frame->draw();
        m_PlanePick->draw();
    }
Sylvain Thery's avatar
Sylvain Thery committed
404
405
406
407
}

void MyQT::cb_mousePress(int button, int x, int y)
{
untereiner's avatar
untereiner committed
408
409
    if (Shift())
    {
Pierre Kraemer's avatar
Pierre Kraemer committed
410
		Dart d = m_render_topo->picking(myMap, x,y/*,nb*/);
untereiner's avatar
untereiner committed
411
412
413
        if (button == Qt::LeftButton)
        {
            if (d != Dart::nil())
untereiner's avatar
untereiner committed
414
            {
untereiner's avatar
untereiner committed
415
                m_selected = d;
untereiner's avatar
untereiner committed
416
417
418
419
420

                std::cout << "m_selected = " << m_selected << std::endl;


            }
untereiner's avatar
untereiner committed
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
        }
        if (button == Qt::RightButton)
        {
            if (d != Dart::nil())
                m_selected2 = d;
        }
        updateGL();

        if (hide_clipping || !clip_volume)
            return;

        m_begX = x;
        m_begY = y;

        // get ray of selection
        Geom::Vec3f rayA,rayB;
        float dist = getOrthoScreenRay(x,y,rayA,rayB);
        Geom::Vec3f AB = rayB-rayA;

        unsigned int fr_picked =0;
        // picking the frame -> axis
        fr_picked = m_frame->pick(rayA,AB,dist);

        if (fr_picked != 0)
        {
            m_pickedAxis=fr_picked;
            m_frame->highlight(m_pickedAxis);
            m_frame->storeProjection(m_pickedAxis);
            updateGL();
        }
    }

    if (Control())
    {
        if (button == Qt::LeftButton)
        {
Lionel Untereiner's avatar
Lionel Untereiner committed
457
458
459
460
461
462
			m_selecteds.push_back(Dart(173));
			m_selecteds.push_back(Dart(186));
			m_selecteds.push_back(Dart(185));
			m_selecteds.push_back(Dart(171));
			m_selecteds.push_back(Dart(170));
			m_selecteds.push_back(Dart(174));
untereiner's avatar
untereiner committed
463
464
465
        }
        if (button == Qt::RightButton)
        {
Pierre Kraemer's avatar
Pierre Kraemer committed
466
			Dart d = m_render_topo->picking(myMap, x,y/*, nb*/);
untereiner's avatar
untereiner committed
467
468
469
470
471
472
473
474
            if (d != Dart::nil())
            {
                m_selecteds.push_back(d);
            }
        }

        updateGL();
    }
Sylvain Thery's avatar
Sylvain Thery committed
475
476
}

477
void  MyQT::cb_mouseRelease(int /*button*/, int /*x*/, int /*y*/)
478
479
{

untereiner's avatar
untereiner committed
480
481
    if (hide_clipping || !clip_volume)
        return;
482

untereiner's avatar
untereiner committed
483
484
485
    m_pickedAxis=0;
    m_frame->highlight(m_pickedAxis);
    updateGL();
486
487
488
489
490

}

void  MyQT::cb_mouseMove(int buttons, int x, int y)
{
untereiner's avatar
untereiner committed
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
    if (!Shift())
        return;

    if (hide_clipping || !clip_volume)
        return;

    // rotation selected ?
    if (Utils::FrameManipulator::rotationAxis(m_pickedAxis))
    {
        if (buttons&1)
        {
            float angle = m_frame->angleFromMouse(x,y,x-m_begX, y-m_begY);
            m_frame->rotate(m_pickedAxis, angle);
        }
        else if (buttons&2)
            m_frame->rotateInScreen(x-m_begX, y-m_begY);

        m_PlanePick->transfo() = m_frame->transfo();
    }
    // translation selected
    else if (Utils::FrameManipulator::translationAxis(m_pickedAxis))
    {
        if (buttons&1)
        {
            float dist =  m_frame->distanceFromMouse(x-m_begX, y-m_begY);
            m_frame->translate(m_pickedAxis, dist);
        }
        else if (buttons&2)
            m_frame->translateInScreen(x-m_begX, y-m_begY);

        m_PlanePick->transfo() = m_frame->transfo();
    }
    // scale selected
    else if (Utils::FrameManipulator::scaleAxis(m_pickedAxis) )
    {
        float scale = m_frame->scaleFromMouse(x-m_begX, y-m_begY);
        m_frame->scale(m_pickedAxis, scale );
        m_PlanePick->transfo() = m_frame->transfo();
    }

    Geom::Vec3f pos = m_PlanePick->getPosition();
    float pipo;
    Geom::Vec3f normal = m_PlanePick->getAxisScale(2, pipo); // 2 = Z axis = plane normal
Sylvain Thery's avatar
Sylvain Thery committed
534
535
536
	float d = -(pos*normal);
	m_render_topo->setClippingPlane(Geom::Vec4f(normal[0], normal[1], normal[2], d));
	m_render_topo_boundary->setClippingPlane(Geom::Vec4f(normal[0], normal[1], normal[2], d));
untereiner's avatar
untereiner committed
537
538
539
540
541

    m_begX = x;
    m_begY = y;
    updateGL();
    return;
542
543
544

}

Sylvain Thery's avatar
Sylvain Thery committed
545
546
void MyQT::cb_keyPress(int keycode)
{
untereiner's avatar
untereiner committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    switch(keycode)
    {

        case ' ':
            m_selecteds.clear();
            m_selected=NIL;
            m_selected2=NIL;
            updateGL();
            break;
        case '2':
            createMap(2);
            updateMap();
            updateGL();
            break;
        case '3':
            createMap(3);
            updateMap();
            updateGL();
            break;
        case '4':
            createMap(4);
            updateMap();
            updateGL();
            break;
untereiner's avatar
untereiner committed
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
        case '9':
            createMapTets();
            updateMap();
            updateGL();
            break;
        case '8':
        {
            if (m_selected != NIL)
            {
                std::vector<Dart> edges;

                Dart d2_1 = myMap.phi_1(myMap.phi2(m_selected));
                myMap.mergeVolumes(m_selected,false);

                Dart d2 = myMap.phi1(d2_1);
                Dart d3 = myMap.phi3(d2);

                myMap.flipEdge(d2);
                myMap.flipBackEdge(d3);

                Dart e = myMap.phi2(d2) ;
                myMap.copyDartEmbedding<VERTEX>(d2, myMap.phi1(e)) ;
                myMap.copyDartEmbedding<VERTEX>(e, myMap.phi1(d2)) ;

                Dart e3 = myMap.phi2(d3);
                myMap.copyDartEmbedding<VERTEX>(d3, myMap.phi1(e3)) ;
                myMap.copyDartEmbedding<VERTEX>(e3, myMap.phi1(d3)) ;

                Dart stop = myMap.phi_1(d2_1);
                Dart dit = stop;
                do
                {
                    edges.push_back(dit);
                    dit = myMap.phi1(myMap.phi2(myMap.phi1(dit)));
                }
                while(dit != stop);

                myMap.splitVolumeWithFace(edges,myMap.phi_1(myMap.phi3(m_selected)));

                m_selected = NIL;
                updateMap();
                updateGL();
            }
            break;
        }
untereiner's avatar
untereiner committed
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
642
643
644
645
646
647
648
649
650
651
652
653
654
        case 'c':
            myMap.check();
            break;
        case 'a':
            m_selected = myMap.phi1(m_selected);
            updateGL();
            break;
        case 'z':
            m_selected = myMap.phi_1(m_selected);
            updateGL();
            break;
        case 'e':
            m_selected = myMap.phi2(m_selected);
            updateGL();
            break;
        case 'r':
            m_selected = myMap.phi3(m_selected);
            updateGL();
            break;
        case 'q':
            m_selected2 = myMap.phi1(m_selected2);
            updateGL();
            break;
        case 's':
            m_selected2 = myMap.phi_1(m_selected2);
            updateGL();
            break;
        case 'd':
            m_selected2 = myMap.phi2(m_selected2);
            updateGL();
            break;
        case 'f':
            m_selected2 = myMap.phi3(m_selected2);
            updateGL();
            break;
        case 'w':
            m_ex1 = 0.99f;
            m_ex2 = 0.99f;
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
655
			m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
656
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
657
			m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
658
659
660
661
662
663
664
#endif
            updateGL();
            break;
        case 'W':
            m_ex1 = 0.95f;
            m_ex2 = 0.95f;
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
665
			m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
666
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
667
			m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
668
669
670
671
672
673
674
675
676
#endif
            updateGL();
            break;
        case '-':
            if(Control())
                m_ex2 -= 0.05f;
            else
                m_ex1 -= 0.05f;
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
677
			m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
678
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
679
			m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
680
681
682
683
684
685
686
687
688
#endif
            updateGL();
            break;
        case '+':
            if(Control())
                m_ex2 += 0.05f;
            else
                m_ex1 += 0.05f;
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
689
			m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
690
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
691
			m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#endif
            updateGL();
            break;
        case '*':
        {
            m_selecteds.clear();
            Dart d2 = myMap.phi_1(myMap.phi_1(m_selected));
            myMap.splitFace(m_selected,d2);
            Dart d3 = myMap.phi2(myMap.phi1(myMap.phi1(myMap.phi2(m_selected))));
            myMap.splitFace(myMap.phi1(d3),myMap.phi_1(d3));

            Dart dit = myMap.phi_1(m_selected);
            m_selecteds.push_back(dit);
            dit =  myMap.phi1(myMap.phi2(myMap.phi1(dit)));
            m_selecteds.push_back(dit);
            dit =  myMap.phi1(myMap.phi2(myMap.phi1(dit)));
            m_selecteds.push_back(dit);
            dit =  myMap.phi1(myMap.phi2(myMap.phi1(dit)));
            m_selecteds.push_back(dit);
            myMap.splitVolume(m_selecteds);

#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
714
			m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
715
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
716
			m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#endif
            updateGL();
        }
            break;
            //	case 'c':
            //		for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
            //		{
            //			if (!myMap.isBoundaryMarked3(d))
            //			{
            //				int n = random();
            //				float r = float(n&0x7f)/255.0f + 0.25f;
            //				float g = float((n>>8)&0x7f)/255.0f + 0.25f;
            //				float b = float((n>>16)&0x7f)/255.0 + 0.25f;
            //				colorDarts[d] =  Geom::Vec3f(r,g,b);
            //				m_render_topo->setDartColor(d,r,g,b);
            //			}
            //		}
            //		break;
            //	case 'g':
            //		for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
            //		{
            //			if (!myMap.isBoundaryMarked3(d))
            //			{
            //				colorDarts[d] =  Geom::Vec3f(0.5f,0.5f,0.5f);
            //				m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
            //			}
            //		}
            //		break;
            //
            //	case 'b':
            //		for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
            //		{
            //			if (!myMap.isBoundaryMarked3(d))
            //			{
            //				colorDarts[d] =  Geom::Vec3f(0.0f,0.0f,0.0f);
            //				m_render_topo->setDartColor(d,0.0f,0.0f,0.0f);
            //			}
            //		}
            //		break;
        case Qt::Key_Up:
            if (m_selected!=NIL)
                position[m_selected][1] += m_shift;
            updateMap();
            updateGL();
            break;
        case Qt::Key_Down:
            if (m_selected!=NIL)
                position[m_selected][1] -= m_shift;
            updateMap();
            updateGL();
            break;
        case Qt::Key_Left:
            if (m_selected!=NIL)
                position[m_selected][0] -= m_shift;
            updateMap();
            updateGL();
            break;
        case Qt::Key_Right:
            if (m_selected!=NIL)
                position[m_selected][0] += m_shift;
            updateMap();
            updateGL();
            break;
    }
    updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
782
783
784
785
}

void MyQT::svg()
{
untereiner's avatar
untereiner committed
786
787
788
789
    if (m_selected != NIL)
        m_render_topo->setDartColor(m_selected, 0.8f, 0.0f, 0.0f);
    if (m_selected2 != NIL)
        m_render_topo->setDartColor(m_selected2, 0.0f, 0.8f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
790

untereiner's avatar
untereiner committed
791
792
    std::string filename = selectFileSave("snapshot file", ".", "(*.svg)");
    m_render_topo->svgout2D(filename, modelViewMatrix(), projectionMatrix());
Sylvain Thery's avatar
Sylvain Thery committed
793
794
795
796
}

void MyQT::cb_Open()
{
untereiner's avatar
untereiner committed
797
798
799
800
801
    std::string filters("all (*.*);; map (*.map)") ;
    std::string filename = selectFile("Open Mesh", "", filters) ;
    if (!filename.empty())
        importMesh(filename);
    updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
802
803
804
805
}

void MyQT::cb_Save()
{
untereiner's avatar
untereiner committed
806
807
808
809
    std::string filename = selectFileSave("Export MAP file ",".","(*.map)");
    //Algo::Surface::Export::exportOFF<PFP>(myMap, position, filename.c_str()); // ???
    if(!myMap.saveMapBin(filename))
        std::cout << "could not save file : " << filename << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
810
811
812
813
}

void MyQT::importMesh(std::string& filename)
{
untereiner's avatar
untereiner committed
814
815
816
817
818
819
820
821
822
    myMap.clear(true) ;

    size_t pos = filename.rfind(".");    // position of "." in filename
    std::string extension = filename.substr(pos);
    std::vector<std::string> attrNames ;

    if (extension == std::string(".map"))
    {
        myMap.loadMapBin(filename);
823
		position = myMap.getAttribute<VEC3, VERTEX, MAP>("position") ;
untereiner's avatar
untereiner committed
824
825
826
827
828
829
    }
    else if(!Algo::Volume::Import::importMesh<PFP>(myMap, filename, attrNames))
    {
        std::cerr << "could not import " << filename << std::endl ;
        return ;
    }
830
	else
831
		position = myMap.getAttribute<VEC3, VERTEX, MAP>(attrNames[0]) ;
untereiner's avatar
untereiner committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846

    m_selected  = NIL;
    m_selected2 = NIL;

    //	m_render_topo->updateData<PFP>(myMap, position, m_ex1,m_ex2,m_ex3/*, nb*/);
    //	m_render_topo_boundary->updateDataBoundary<PFP>(myMap,position,m_ex1,m_ex2,m_shift);

    bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
    setParamObject(bb.maxSize(), bb.center().data()) ;
    m_shift = bb.maxSize()/200.0f;

    m_frame->setSize(bb.maxSize());

    updateMap();
    updateGLMatrices() ;
Sylvain Thery's avatar
Sylvain Thery committed
847
848
849
850
}

void MyQT::width(int w)
{
untereiner's avatar
untereiner committed
851
852
    m_ex3 = 0.9f - 0.025f*w;
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
853
	m_render_topo->updateData(myMap, position, m_ex1, m_ex2);
untereiner's avatar
untereiner committed
854
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
855
	m_render_topo->updateData(myMap, position, m_ex1,m_ex2,m_ex3);
untereiner's avatar
untereiner committed
856
857
#endif
    updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
858
}