BSplineSurfaceSelectionPlugin.cc 33.7 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
8
9
 *                                                                           *
 *---------------------------------------------------------------------------*
Martin Schultz's avatar
Martin Schultz committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                      *
 *   $Author$                                                       *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/

#include "BSplineSurfaceSelectionPlugin.hh"

#include <OpenFlipper/common/GlobalOptions.hh>

// Primitive type icons
#define CONTROL_POINT_TYPE  "bscontrolpointselection.png"
#define KNOT_TYPE           "bsknotselection.png"
// =======================================
// Define operations
// =======================================
// General Operations:
#define G_CP_MODE          "Control Point Selection View"
#define G_K_MODE           "Knot Selection View"

// Control points:
#define CP_SELECT_ALL      "Select All Control Points"
#define CP_DESELECT_ALL    "Deselect All Control Points"
#define CP_INVERT          "Invert Control Point Selection"
//#define CP_DELETE_U        "Delete Selected Control Points in U"
//#define CP_DELETE_V        "Delete Selected Control Points in V"

// Knots:
#define K_SELECT_ALL      "Select All Knots"
#define K_DESELECT_ALL    "Deselect All Knots"
#define K_INVERT          "Invert Knot Selection"
//#define K_DELETE_U        "Delete Selected Knots in U"
//#define K_DELETE_V        "Delete Selected Knots in V"

/// Default constructor
BSplineSurfaceSelectionPlugin::BSplineSurfaceSelectionPlugin() :
controlPointType_(0),
knotType_(0),
allSupportedTypes_(0) {
}

//==============================================================================================
      
BSplineSurfaceSelectionPlugin::~BSplineSurfaceSelectionPlugin() {
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::initializePlugin() {

    // Tell core about all scriptable slots
    updateSlotDescriptions();
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::pluginsInitialized() {
    // Create new selection environment for skeletons
    // and register skeleton data type for the environment.
    
    QString iconPath = OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator();
    
    emit addSelectionEnvironment("B-Spline Surface Selections", "Select B-Spline surface primitives.",
Jan Möbius's avatar
Jan Möbius committed
107
                                 iconPath + "bsplinesurface.png", environmentHandle_);
108
109
110
111

    // Register mesh object types
    emit registerType(environmentHandle_, DATA_BSPLINE_SURFACE);
    
Jan Möbius's avatar
Jan Möbius committed
112
113
    emit addPrimitiveType(environmentHandle_, "Select Control Points", iconPath + CONTROL_POINT_TYPE, controlPointType_);
    emit addPrimitiveType(environmentHandle_, "Select Knots", iconPath + KNOT_TYPE, knotType_);
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
    
    allSupportedTypes_ = controlPointType_ | knotType_;
    
    // Determine, which selection modes are requested
    emit showToggleSelectionMode(environmentHandle_, true, allSupportedTypes_);
    emit showVolumeLassoSelectionMode(environmentHandle_, true, allSupportedTypes_);
    
    QStringList generalOperations;
    generalOperations.append(G_CP_MODE);
    generalOperations.append(G_K_MODE);
    
    // Define control point and knot operations
    QStringList controlPointOperations;
    controlPointOperations.append(CP_SELECT_ALL);
    controlPointOperations.append(CP_DESELECT_ALL);
//     controlPointOperations.append(CP_DELETE_U);
//     controlPointOperations.append(CP_DELETE_V);
    controlPointOperations.append(CP_INVERT);
    
    QStringList knotOperations;
    knotOperations.append(K_SELECT_ALL);
    knotOperations.append(K_DESELECT_ALL);
//     knotOperations.append(K_DELETE_U);
//     knotOperations.append(K_DELETE_V);
    knotOperations.append(K_INVERT);
    
    emit addSelectionOperations(environmentHandle_, generalOperations, "Selection Operations");
    emit addSelectionOperations(environmentHandle_, controlPointOperations,  "Control Point Operations",   controlPointType_);
    emit addSelectionOperations(environmentHandle_, knotOperations,  "Knot Operations",   knotType_);
    
    // Register key shortcuts:
    
    // Select (a)ll
    emit registerKeyShortcut(Qt::Key_A, Qt::ControlModifier);
    // (C)lear selection
    emit registerKeyShortcut(Qt::Key_C,      Qt::NoModifier);
    // (I)nvert selection
    emit registerKeyShortcut(Qt::Key_I,      Qt::NoModifier);
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::updateSlotDescriptions() {
    
    emit setSlotDescription("selectAllControlPoints(int)", tr("Select all control points of a B-spline surface"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("deselectAllControlPoints(int)", tr("Deselect all control points of a B-spline surface"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("invertControlPointSelection(int)", tr("Invert control point selection"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("deleteSelectedControlPoints(int)", tr("Delete selected control points"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("selectControlPoints(int,IdList)", tr("Select the specified control points"),
                            QString("objectId,control pointList").split(","), QString("Id of object,List of control points").split(","));

    emit setSlotDescription("loadSelection(int,QString)", tr("Load selection from selection file"),
                            QString("objectId,filename").split(","), QString("Id of an object,Selection file").split(","));

    emit setSlotDescription("selectAllKnots(int)", tr("Select all knots of a B-spline surface"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("deselectAllKnots(int)", tr("Deselect all knots of a B-spline surface"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("invertKnotSelection(int)", tr("Invert knot selection"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("deleteSelectedKnots(int)", tr("Delete selected knots"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("selectKnots(int,IdList)", tr("Select the specified knots"),
                            QString("objectId,knotList").split(","), QString("Id of object,List of knots").split(","));
}

//==============================================================================================

Jan Möbius's avatar
Jan Möbius committed
186
void BSplineSurfaceSelectionPlugin::slotSelectionOperation(const QString& _operation) {
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    
    SelectionInterface::PrimitiveType type = 0u;
    emit getActivePrimitiveType(type);
    
    if((type & allSupportedTypes_) == 0)
        return;
    
    // Test if operation should be applied to target objects only
    bool targetsOnly = false;
    emit targetObjectsOnly(targetsOnly);
    PluginFunctions::IteratorRestriction restriction =
            (targetsOnly ? PluginFunctions::TARGET_OBJECTS : PluginFunctions::ALL_OBJECTS);

    if(_operation == G_CP_MODE) {
        // Set selection view mode
        setSelectionViewMode(CP);
    } else if(_operation == G_K_MODE) {
        // Set selection view mode
        setSelectionViewMode(K);
    } else if(_operation == CP_SELECT_ALL) {
        // Select all control points
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                selectAllControlPoints(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Select All Controlpoints", UPDATE_SELECTION);
        }
    } else if (_operation == CP_DESELECT_ALL) {
        // Deselect all control points
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
               deselectAllControlPoints(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);    
            emit  createBackup(o_it->id(), "Deselect All Controlpoints", UPDATE_SELECTION);
        }
    } else if (_operation == CP_INVERT) {
        // Invert control point selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
               invertControlPointSelection(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Invert Controlpoint Selection", UPDATE_SELECTION);
        }
//     } else if (_operation == CP_DELETE_U) {
//         // Delete control point selection
//         for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
//             o_it != PluginFunctions::objectsEnd(); ++o_it) {
//             if (o_it->visible()) {
//                deleteSelectedControlPointsU(o_it->id());
//             }
//             
//             emit updatedObject(o_it->id(), UPDATE_SELECTION);    
//         }
//     } else if (_operation == CP_DELETE_V) {
//         // Delete control point selection
//         for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
//             o_it != PluginFunctions::objectsEnd(); ++o_it) {
//             if (o_it->visible()) {
//                deleteSelectedControlPointsV(o_it->id());
//             }
//             
//             emit updatedObject(o_it->id(), UPDATE_SELECTION);    
//         }
    } else if(_operation == K_SELECT_ALL) {
        // Select all control points
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                selectAllKnots(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Select All Knots", UPDATE_SELECTION);
        }
    } else if (_operation == K_DESELECT_ALL) {
        // Deselect all control points
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
               deselectAllKnots(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Deselect All Knots", UPDATE_SELECTION);
        }
    } else if (_operation == K_INVERT) {
        // Invert knot selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
               invertKnotSelection(o_it->id());
            }
            
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Invert Knot Selection", UPDATE_SELECTION);
        }
//     } else if (_operation == K_DELETE_U) {
//         // Delete knot selection
//         for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
//             o_it != PluginFunctions::objectsEnd(); ++o_it) {
//             if (o_it->visible()) {
//                deleteSelectedKnotsU(o_it->id());
//             }
//             
//             emit updatedObject(o_it->id(), UPDATE_SELECTION);    
//         }
//     } else if (_operation == K_DELETE_V) {
//         // Delete knot selection
//         for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
//             o_it != PluginFunctions::objectsEnd(); ++o_it) {
//             if (o_it->visible()) {
//                deleteSelectedKnotsV(o_it->id());
//             }
//             
//             emit updatedObject(o_it->id(), UPDATE_SELECTION);    
//         }
    }
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::slotToggleSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect) {

    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
    
    if (_currentType & controlPointType_) {

324
        size_t       node_idx, target_idx;
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
        ACG::Vec3d   hit_point;

        if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_VERTEX, _event->pos(), node_idx, target_idx, &hit_point)) {

            BaseObjectData* object = 0;

            if (PluginFunctions::getPickedObject(node_idx, object)) {
                
                BSplineSurfaceObject* bsso = PluginFunctions::bsplineSurfaceObject(object);
                
                // toggle selection
                if (bsso->splineSurface()->controlpoint_selections_available()) {
                    
                    unsigned int max = bsso->splineSurface()->n_control_points_m() * bsso->splineSurface()->n_control_points_n();

                    if ( target_idx < max) {
                        
                        int idx_m = target_idx / bsso->splineSurface()->n_control_points_n();
                        int idx_n = target_idx % bsso->splineSurface()->n_control_points_n();
                        
                        if (bsso->splineSurface()->controlpoint_selected(idx_m, idx_n) || _deselect) {
                            bsso->splineSurface()->deselect_controlpoint(idx_m, idx_n);
                        } else {
                            bsso->splineSurface()->select_controlpoint(idx_m, idx_n);
                        }
                    }
                }
                bsso->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::CONTROLPOINT);
                emit updatedObject(bsso->id(), UPDATE_SELECTION);
                emit  createBackup(bsso->id(), "Toggle Selection", UPDATE_SELECTION);
            }
        }
    }
    
    if (_currentType & knotType_) {

361
        size_t       node_idx, target_idx;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
553
554
555
556
557
558
559
560
561
562
563
564
565
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
639
640
641
642
643
644
645
646
647
648
649
650
651
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
        ACG::Vec3d   hit_point;

        if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_SPLINE, _event->pos(),node_idx, target_idx, &hit_point)) {
            
            BaseObjectData* object = 0;

            if (PluginFunctions::getPickedObject(node_idx, object)) {
                
                BSplineSurfaceObject * bsso = PluginFunctions::bsplineSurfaceObject(object);
                BSplineSurface *       bss  = bsso->splineSurface();

                // toggle selection
                if(bss->get_knotvector_m_ref()->selections_available() &&
                        bss->get_knotvector_n_ref()->selections_available()) {
                    
                    // the target index we are getting here refers to the respective texel in the picking-texture
                    // hence, we have to compute the actual u,v coordinates from this texel
                    // given the uv coord, we finally compute the closes knot in u and v direction, respectively

                    int numKnots_m = bss->n_knots_m();
                    int numKnots_n = bss->n_knots_n();
                    int order_m    = bss->degree_m() + 1;
                    int order_n    = bss->degree_n() + 1;

                    Knotvector * knotvec_u = bss->get_knotvector_m_ref();
                    Knotvector * knotvec_v = bss->get_knotvector_n_ref();

                    double minu = knotvec_u->getKnot(bss->degree_m());
                    double minv = knotvec_v->getKnot(bss->degree_n());
                    double maxu = knotvec_u->getKnot(numKnots_m - order_m);
                    double maxv = knotvec_v->getKnot(numKnots_n - order_n);

                    double udiff = maxu - minu;
                    double vdiff = maxv - minv;

                    int texres = bsso->splineSurfaceNode()->pick_texture_res();
                    double curu = double (target_idx / texres) / double(texres) * udiff + minu;
                    double curv = double (target_idx % texres) / double(texres) * vdiff + minv;

                    ACG::Vec2d found_params(curu, curv);

                    // knots closest to parameters of hitpoint on the surface
                    ACG::Vec2i interval_m = bss->interval_m(curu);
                    ACG::Vec2i interval_n = bss->interval_n(curv);
            
                    int knotIdx_m = interval_m[0];
                    int knotIdx_n = interval_n[0];

                    if ( !bss->get_knotvector_m_ref()->selected( knotIdx_m ) ||
                         !bss->get_knotvector_n_ref()->selected( knotIdx_n )) {
                        
                        bss->get_knotvector_m_ref()->select( knotIdx_m );
                        bss->get_knotvector_n_ref()->select( knotIdx_n );
                    }
                    else {
                        bss->get_knotvector_m_ref()->deselect( knotIdx_m );
                        bss->get_knotvector_n_ref()->deselect( knotIdx_n );
                    }

                } // end of if selections available
                
                bsso->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::KNOTVECTOR);
                emit updatedObject(bsso->id(), UPDATE_SELECTION);
                emit  createBackup(bsso->id(), "Toggle Selection", UPDATE_SELECTION);
            }
        }
    }
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::slotVolumeLassoSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
    
    ACG::Vec3d hit_point;
    
    if(_event->type() == QEvent::MouseButtonPress) {
        
        ACG::GLState &state = PluginFunctions::viewerProperties().glState();
        QPoint p(_event->pos().x(), state.viewport_height() - _event->pos().y());
        
        volumeLassoPoints_.append(p);
        
        return;

    } else if(_event->type() == QEvent::MouseButtonDblClick) {

        ACG::GLState &state = PluginFunctions::viewerProperties().glState();
        
        QPolygon polygon(volumeLassoPoints_);
        
        bool targetsOnly = false;
        emit targetObjectsOnly(targetsOnly);
        PluginFunctions::IteratorRestriction restriction =
            (targetsOnly ? PluginFunctions::TARGET_OBJECTS : PluginFunctions::ALL_OBJECTS);
        
        // Select all primitives that lie in this region
        for(PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            
            if (o_it->visible()) {
                
                BSplineSurface* surface = PluginFunctions::splineSurface(o_it);
                BSplineSurfaceObject* so = PluginFunctions::bsplineSurfaceObject(o_it);
                
                if(surface) {
                    
                    if(_currentType & controlPointType_) {
                        // Select control points
                        if(!surface->controlpoint_selections_available()) return;
                        
                        // Enable control point texture mode
                        so->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::CONTROLPOINT);
                        
                        for(unsigned int i = 0; i < surface->n_control_points_m(); ++i) {
                            for(unsigned int j = 0; j < surface->n_control_points_n(); ++j) {
                                
                                ACG::Vec3d c = surface->get_control_point(i, j);
                                ACG::Vec3d pc = state.project(c);
                                QPoint p((int)pc[0], (int)pc[1]);
                                if(polygon.containsPoint(p, Qt::OddEvenFill)) {
                                    if(_deselect)
                                        surface->deselect_controlpoint(i,j);
                                    else
                                        surface->select_controlpoint(i,j);
                                }
                            }
                        }
            
                    }
                    
                    if(_currentType & knotType_) {
                        // Select knots
                        if(!surface->get_knotvector_m_ref()->selections_available()) return;
                        if(!surface->get_knotvector_n_ref()->selections_available()) return;
                        
                        // Enable knot texture mode
                        so->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::KNOTVECTOR);
                        
                        int numKnots_m = surface->n_knots_m();
                        int numKnots_n = surface->n_knots_n();
                        
                        int order_m    = surface->degree_m() + 1;
                        int order_n    = surface->degree_n() + 1;

                        Knotvector* knotvec_m = surface->get_knotvector_m_ref();
                        Knotvector* knotvec_n = surface->get_knotvector_n_ref();

                        // Go over all B-spline surface knot midpoints and test
                        // if computed position on surface lies within the polygon
                        if(surface->degree_m() == 0) return;
                        if(surface->degree_n() == 0) return;
                        
                        if((int)surface->n_knots_m() <= ((numKnots_m - order_m) + 1)) return;
                        if((int)surface->n_knots_n() <= ((numKnots_n - order_n) + 1)) return;
                        
                        for(int i = surface->degree_m()-1; i < (numKnots_m - order_m)-1; ++i) {
                            for(int j = surface->degree_n()-1; j < (numKnots_n - order_n)-1; ++j) {
                            
                                double baseKnot_m = knotvec_m->getKnot(i+1);
                                double baseKnot_n = knotvec_n->getKnot(j+1);
                                
                                double dhalfu = 0.5*(knotvec_m->getKnot(i+2) - baseKnot_m);
                                double dhalfv = 0.5*(knotvec_n->getKnot(j+2) - baseKnot_n);
                                
                                double t_u = baseKnot_m + dhalfu;
                                double t_v = baseKnot_n + dhalfv;
                                
                                // Project surface point
                                ACG::Vec3d pcp = state.project(surface->surfacePoint(t_u, t_v));
                                
                                QPoint p((int)pcp[0], (int)pcp[1]);
                                
                                if(polygon.containsPoint(p, Qt::OddEvenFill)) {
                                    if(_deselect) {
                                        knotvec_m->deselect(i+1);
                                        knotvec_n->deselect(j+1);
                                    } else {
                                        knotvec_m->select(i+1);
                                        knotvec_n->select(j+1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Lasso Selection", UPDATE_SELECTION);
        }

        // Clear lasso points
        volumeLassoPoints_.clear();
    }
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::loadSelection(int _objId, const QString& _filename) {

    // Load ini file
    INIFile file;

    if(!file.connect(_filename, false)) {
        emit log(LOGERR, QString("Could not read file '%1'!").arg(_filename));
        return;
    }

    // Load selection from file
    loadIniFile(file, _objId);
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::loadIniFile(INIFile& _ini, int _id) {
    // From INI Interface
    // Load plugin specific settings
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::saveIniFile(INIFile& _ini, int _id) {
    // From INI Interface
    // Save plugin specific settings
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::slotLoadSelection(const INIFile& _file) {
    
    // Iterate over all B-spline surfaces in the scene and save
    // the selections for all supported entity types
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_BSPLINE_SURFACE)); 
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
        
        // Read section for each object
        // Append object name to section identifier
        QString section = QString("BSplineSurfaceSelection") + "//" + o_it->name();
        if(!_file.section_exists(section)) {
            continue;
        }
        
        std::vector<int> ids;
        // Read control point selection:
        _file.get_entry(ids, section, "ControlPointSelection");
        selectControlPoints(o_it->id(), ids);
        ids.clear();
        // Read knot selection:
        std::vector<int> ids_u;
        std::vector<int> ids_v;
        
        _file.get_entry(ids_u, section, "KnotSelection_u");
        _file.get_entry(ids_v, section, "KnotSelection_v");
        
        selectKnots(o_it->id(), ids_u, ids_v);
        
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
        emit  createBackup(o_it->id(), "Load Selection", UPDATE_SELECTION);
    }
    
    setSelectionViewMode(CP);
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::slotSaveSelection(INIFile& _file) {
    
    // Iterate over all B-spline surfaces in the scene and save
    // the selections for all entity types
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_BSPLINE_SURFACE)); 
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
        
        // Create section for each object
        // Append object name to section identifier
        QString section = QString("BSplineSurfaceSelection") + "//" + o_it->name();
        
        // Store control point selection
        _file.add_entry(section, "ControlPointSelection", getControlPointSelection(o_it->id()));
        // Store knot selection
        _file.add_entry(section, "KnotSelection_u", getKnotSelectionU(o_it->id()));
        _file.add_entry(section, "KnotSelection_v", getKnotSelectionV(o_it->id()));
    }
}

//==============================================================================================

void BSplineSurfaceSelectionPlugin::slotKeyShortcutEvent(int _key, Qt::KeyboardModifiers _modifiers) {
    
    SelectionInterface::PrimitiveType type = 0u;
    emit getActivePrimitiveType(type);
    
    if((type & allSupportedTypes_) == 0) {
        // No supported type is active
        return;
    }
    
    bool targetsOnly = false;
    emit targetObjectsOnly(targetsOnly);
    PluginFunctions::IteratorRestriction restriction =
            (targetsOnly ? PluginFunctions::TARGET_OBJECTS : PluginFunctions::ALL_OBJECTS);
    
    if(_key == Qt::Key_A && _modifiers == Qt::ControlModifier) {
        // Select all
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                if(type & controlPointType_) selectAllControlPoints(o_it->id());
                if(type & knotType_) selectAllKnots(o_it->id());
            }
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Select All", UPDATE_SELECTION);
        }
    } else if(_key == Qt::Key_C && _modifiers == Qt::NoModifier) {
        // Deselect all
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                if(type & controlPointType_) deselectAllControlPoints(o_it->id());
                if(type & knotType_) deselectAllKnots(o_it->id());
            }
            emit updatedObject(o_it->id(), UPDATE_SELECTION);    
            emit  createBackup(o_it->id(), "Clear Selection", UPDATE_SELECTION);
        }
    } else if(_key == Qt::Key_I && _modifiers == Qt::NoModifier) {
        // Invert selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_BSPLINE_SURFACE)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                if(type & controlPointType_) invertControlPointSelection(o_it->id());
                if(type & knotType_) invertKnotSelection(o_it->id());
            }
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Invert Selection", UPDATE_SELECTION);
        }
    }
}

void BSplineSurfaceSelectionPlugin::setSelectionViewMode(const SelectionViewMode _mode) {
    
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_BSPLINE_SURFACE)); 
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
        
        BSplineSurfaceObject* so = PluginFunctions::bsplineSurfaceObject(o_it);
        if(_mode == CP) {
            so->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::CONTROLPOINT);
        }
        else {
            so->splineSurfaceNode()->set_selection_draw_mode(ACG::SceneGraph::BSplineSurfaceNodeT<BSplineSurface>::KNOTVECTOR);
        }
        
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
    }
}

//==============================================================================================

Matthias Möller's avatar
Matthias Möller committed
721
722