MeshObjectSelectionPlugin.hh 21.7 KB
Newer Older
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper 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 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper 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 LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

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

#ifndef MESHOBJECTSELECTIONPLUGIN_HH
#define MESHOBJECTSELECTIONPLUGIN_HH

#include <QObject>

#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/KeyInterface.hh>
50
#include <OpenFlipper/BasePlugin/MouseInterface.hh>
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>
#include <OpenFlipper/BasePlugin/ToolboxInterface.hh>
#include <OpenFlipper/BasePlugin/ToolbarInterface.hh>
#include <OpenFlipper/BasePlugin/LoadSaveInterface.hh>
#include <OpenFlipper/BasePlugin/BackupInterface.hh>
#include <OpenFlipper/BasePlugin/INIInterface.hh>
#include <OpenFlipper/BasePlugin/ScriptInterface.hh>
#include <OpenFlipper/BasePlugin/SelectionInterface.hh>

#include <OpenFlipper/common/Types.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/INIFile/INIFile.hh>

#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>

#include "ConversionDialog.hh"

class MeshObjectSelectionPlugin : public QObject, BaseInterface, KeyInterface, LoadSaveInterface,
70
71
            INIInterface, BackupInterface, ScriptInterface, LoggingInterface, SelectionInterface,
            MouseInterface
72
73
74
75
{
    Q_OBJECT
    Q_INTERFACES(BaseInterface)
    Q_INTERFACES(KeyInterface)
76
    Q_INTERFACES(MouseInterface)
77
78
79
80
81
82
83
    Q_INTERFACES(INIInterface)
    Q_INTERFACES(BackupInterface)
    Q_INTERFACES(ScriptInterface)
    Q_INTERFACES(LoggingInterface)
    Q_INTERFACES(LoadSaveInterface)
    Q_INTERFACES(SelectionInterface)

Matthias Möller's avatar
Matthias Möller committed
84
85
86
87
#if QT_VERSION >= 0x050000
  Q_PLUGIN_METADATA(IID "org.OpenFlipper.Plugins.Plugin-SelectionMeshObject")
#endif

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public:
    /// Default constructor
    MeshObjectSelectionPlugin();

    /// Default destructor
    ~MeshObjectSelectionPlugin();
    
    friend class SelectVolumeAction;

signals:

    // BaseInterface
    void updateView();
    void updatedObject(int, const UpdateType&);
    void nodeVisibilityChanged(int _identifier);
    void setSlotDescription(QString     _slotName,   QString     _slotDescription,
                            QStringList _parameters, QStringList _descriptions);

    // BackupInterface
    void createBackup( int _objectid, QString _name, UpdateType _type = UPDATE_ALL);

    // LoggingInterface
    void log(Logtype _type, QString _message);
    void log(QString _message);

    // SelectionInterface
Jan Möbius's avatar
Jan Möbius committed
114
    void addSelectionEnvironment(QString _modeName, QString _description, QString _icon, QString& _handleName);
115
    void registerType(QString _handleName, DataType _type);
Jan Möbius's avatar
Jan Möbius committed
116
    void addPrimitiveType(QString _handleName, QString _name, QString _icon, SelectionInterface::PrimitiveType& _typeHandle);
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
    void addSelectionOperations(QString _handleName, QStringList _operationsList, QString _category, SelectionInterface::PrimitiveType _type = 0u);
    
    void showToggleSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);

    void showLassoSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    void showVolumeLassoSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    void showSphereSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    void showClosestBoundarySelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    void showFloodFillSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    void showComponentsSelectionMode(QString _handleName, bool _show, SelectionInterface::PrimitiveType _associatedTypes);
    
    void getActiveDataTypes(SelectionInterface::TypeList& _types);
    void getActivePrimitiveType(SelectionInterface::PrimitiveType& _type);
    void targetObjectsOnly(bool& _targetsOnly);
    
    void registerKeyShortcut(int _key, Qt::KeyboardModifiers _modifiers = Qt::NoModifier);
    
    // LoadSaveInterface
    void deleteObject(int _objectId);
    void addEmptyObject( DataType _type, int& _id);
    
    // ScriptInterface
    void scriptInfo(QString _functionName);

public slots:

    // SelectionInterface
    void loadSelection(int _objId, const QString& _filename);

private slots:

    // INIInterface
    void loadIniFile(INIFile& _ini, int _id);
    void saveIniFile(INIFile& _ini, int _id);

    // BaseInterface
    void initializePlugin();
    void pluginsInitialized();
    void noguiSupported() {};

    // SelectionInterface
    void slotSelectionOperation(QString _operation);
    void slotToggleSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect);

    void slotLassoSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect);
    void slotVolumeLassoSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect);
    void slotSphereSelection(QMouseEvent* _event, double _radius, SelectionInterface::PrimitiveType _currentType, bool _deselect);
    void slotClosestBoundarySelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect);
    void slotFloodFillSelection(QMouseEvent* _event, double _maxAngle, SelectionInterface::PrimitiveType _currentType, bool _deselect);
    void slotComponentsSelection(QMouseEvent* _event, SelectionInterface::PrimitiveType _currentType, bool _deselect);

    void slotLoadSelection(const INIFile& _file);
    void slotSaveSelection(INIFile& _file);
    
    void slotKeyShortcutEvent(int _key, Qt::KeyboardModifiers _modifiers);

173
174
175
    // MouseInterface
    void slotMouseWheelEvent(QWheelEvent* event, std::string const& mode);

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
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
324
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
361
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
public:

    // BaseInterface
    QString name() {
        return (QString(tr("Mesh Object Selection")));
    };
    
    QString description() {
        return (QString(tr("Allows to select parts of Mesh Objects")));
    };

    //===========================================================================
    /** @name Private methods
    * @{ */
    //===========================================================================
private:
    
    /// Set descriptions for local public slots
    void updateSlotDescriptions();
    
    /// Set color for selection
    void setColorForSelection(const int _objectId, const PrimitiveType _primitiveType);
    
    /** @} */
    
    //===========================================================================
    /** @name Private slots
    * @{ */
    //===========================================================================
private slots:
    
    /// Show selection conversion dialog
    void conversionRequested();

    /** @} */
    
public slots:

    QString version() {
        return QString("1.0");
    };

    // Is vertex type active? (for use in plugins that need mesh selection)
    bool vertexTypeActive() {
        SelectionInterface::PrimitiveType t = 0u;
        emit getActivePrimitiveType(t);
        return (t & vertexType_) > 0;
    }

    // Is vertex type active? (for use in plugins that need mesh selection)
    bool edgeTypeActive() {
        SelectionInterface::PrimitiveType t = 0u;
        emit getActivePrimitiveType(t);
        return (t & edgeType_) > 0;
    }

    // Is face type active? (for use in plugins that need mesh selection)
    bool faceTypeActive() {
        SelectionInterface::PrimitiveType t = 0u;
        emit getActivePrimitiveType(t);
        return (t & faceType_) > 0;
    }

    //===========================================================================
    /** @name Scriptable slots
    * @{ */
    //===========================================================================
public slots:

    //==========================================
    // VERTEX OPERATIONS
    //==========================================

    /// select given vertices
    void selectVertices(int objectId, IdList _vertexList);

    /// unselect given vertices
    void unselectVertices(int objectId, IdList _vertexList);

    /// Select all Vertices
    void selectAllVertices(int _objectId);

    /// Unselect all vertices
    void clearVertexSelection(int _objectId);

    /// Invert the current vertex selection
    void invertVertexSelection(int _objectId);

    /// Select all boundary vertices of the given object
    void selectBoundaryVertices(int _objectId);

    /// Select all vertices of the boundary close to the given vertex
    void selectClosestBoundaryVertices(int _objectId, int _vertexId);

    /// Shrink the current vertex selection
    void shrinkVertexSelection(int _objectId);

    /// Grow the current vertex selection
    void growVertexSelection(int _objectId);

    /// Return a list of all selected vertices
    IdList getVertexSelection(int _objectId);

    /// Delete vertices and faces that are currently selected
    void deleteVertexSelection(int _objectId);

    int createMeshFromVertexSelection( int _objectId);

    /// Colorize the vertex selection
    void colorizeVertexSelection(int _objectId, int _r, int _g, int _b, int a);

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

    /// Set vertices to be part of the handle area
    void selectHandleVertices(int objectId, IdList _vertexList);

    /// Remove vertices from handle area
    void unselectHandleVertices(int objectId, IdList _vertexList);

    /// Clear handle Area
    void clearHandleVertices(int objectId);

    /// Set all vertices to be part of the handle area
    void setAllHandleVertices(int objectId);

    /// Get a list of all handle vertices
    IdList getHandleVertices(int objectId);

    /// Load a selection from an Flipper selection file for the given object
    void loadFlipperModelingSelection(int _objectId, QString _filename);

    /// Save a selection in Flipper Selection Format
    void saveFlipperModelingSelection(int _objectId, QString _filename);

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

    /// Set vertices to be part of the modeling area
    void selectModelingVertices(int objectId, IdList _vertexList);

    /// Remove vertices from modeling area
    void unselectModelingVertices(int objectId, IdList _vertexList);

    /// Select vertices by their value
    void selectVerticesByValue(int _objectId, QString _component, bool _greater, double _value );

    /// Clear Modeling Area
    void clearModelingVertices(int objectId);

    /// Set all vertices to be part of the modeling area
    void setAllModelingVertices(int objectId);

    /// Get a list of all modeling vertices
    IdList getModelingVertices(int objectId);

    //==========================================
    // EDGE OPERATIONS
    //==========================================

    /// Select given Edges
    void selectEdges(int objectId, IdList _vertexList);

    /// Unselect given Edges
    void unselectEdges(int objectId, IdList _vertexList);

    /// Select all Edges
    void selectAllEdges(int objectId);

    /// Unselect all Edges
    void invertEdgeSelection(int objectId);

    /// Invert the current edge selection
    void clearEdgeSelection(int objectId);

    /// select boundary edges
    void selectBoundaryEdges(int objectId);

    /// Delete edges that are currently selected
    void deleteEdgeSelection(int _objectId);

    /// Return a list of all selected edges
    IdList getEdgeSelection(int objectId);

    /// Convert edge ids to vertex pairs
    IdList convertEdgesToVertexPairs(int _id, const IdList& _edges);

    /// Inverse of function above
    IdList convertVertexPairsToEdges(int _id, const IdList& _vertices);

    /// Create a mesh containing the face selection of the given mesh
    int createMeshFromEdgeSelection( int _objectId);

    /// Colorize the edge selection
    void colorizeEdgeSelection(int objectId, int r, int g, int b, int a);

    //==========================================
    // HALFEDGE OPERATIONS
    //==========================================

    /// Select given Halfedges
    void selectHalfedges(int objectId, IdList _vertexList);

    /// Unselect given Halfedges
    void unselectHalfedges(int objectId, IdList _vertexList);

    /// Select all Halfedges
    void selectAllHalfedges(int objectId);

    /// Unselect all Halfedges
    void invertHalfedgeSelection(int objectId);

    /// Invert the current edge selection
    void clearHalfedgeSelection(int objectId);

    /// Select boundary edges
    void selectBoundaryHalfedges(int objectId);

    /// Return a list of all selected edges
    IdList getHalfedgeSelection(int objectId);

    /// Convert halfedge ids to vertex pairs
    IdList convertHalfedgesToVertexPairs(int _id, const IdList& _halfedges);

    /// Inverse of function above
    IdList convertVertexPairsToHalfedges(int _id, const IdList& _vertices);

    /// Colorize the edge selection
    void colorizeHalfedgeSelection(int objectId, int r, int g, int b, int a);

    //==========================================
    // FACE OPERATIONS
    //==========================================

    /// Select given faces
    void selectFaces(int objectId, IdList _facesList);

    /// Unselect given faces
    void unselectFaces(int objectId, IdList _facesList);

    /// Select all faces
    void selectAllFaces(int objectId);

    /// Unselect all faces
    void clearFaceSelection(int objectId);

    /// Invert the current face selection
    void invertFaceSelection(int objectId);

    /// Delete face that are currently selected
    void deleteFaceSelection(int _objectId);

    /// Select all boundary faces of the given object
    void selectBoundaryFaces(int objectId);

    /// Shrink the current face selection
    void shrinkFaceSelection(int objectId);

    /// Grow the current face selection
    void growFaceSelection(int objectId);

    /// Return a list of all selected faces
    IdList getFaceSelection(int objectId);

    /// Create a mesh containing the face selection of the given mesh
    int createMeshFromFaceSelection( int _objectId);

    /// Colorize the face selection
    void colorizeFaceSelection(int objectId, int r, int g, int b, int a);
    
    //===========================================================================

    /// Lasso selection tool
    void lassoSelect(QRegion& _region, PrimitiveType _primitiveType, bool _deselection);

    /** @} */

    void conversion(const QString& _from, const QString& _to, bool _deselect);

    //===========================================================================
    /** @name Template Functions
    * @{ */
    //===========================================================================

private:

    /// Delete all selected elements of a mesh
    template<typename MeshT>
    bool deleteSelection(MeshT* _mesh, PrimitiveType _primitiveType);

    /// Update face selection to correspond to the vertex selection
    template<typename MeshT>
    void update_regions(MeshT* _mesh);

    /// Toggle mesh selection
    template<typename MeshT>
470
    void toggleMeshSelection(int _objectId, MeshT* _mesh, uint _fh, ACG::Vec3d& _hit_point, PrimitiveType _primitiveType);
471
472
473

    /// Use the event to paint selection with a sphere
    template<typename MeshT>
474
475
476
    void paintSphereSelection(MeshT* _mesh, int _objectId, int _target_idx,
            typename MeshT::Point _hitpoint, double _radius,
            PrimitiveType _primitiveTypes, bool _deselection);
477
478
479
    
    /// Surface volume selection tool
    template<class MeshT>
480
481
    bool volumeSelection(MeshT* _mesh, int _objectId, ACG::GLState& _state,
            QRegion *_region, PrimitiveType _primitiveTypes, bool _deselection);
482
483
484

    /// Select all entities that are incident to closest boundary
    template<class MeshT>
485
486
    void closestBoundarySelection(MeshT* _mesh, int _vh,
            PrimitiveType _primitiveTypes, bool _deselection);
487
488
489
    
    /// Select all entities that are connected (and do not exceed the maximum dihedral angle)
    template<class MeshT>
490
491
    void floodFillSelection(MeshT* _mesh, int _objectId, uint _fh,
            double _maxAngle, PrimitiveType _primitiveTypes, bool _deselection);
492
493
494

    /// Connected component mesh selection
    template<typename MeshT>
495
496
    void componentsMeshSelection(MeshT* _mesh, int _objectId, uint _fh,
            ACG::Vec3d& _hit_point, PrimitiveType _primitiveType);
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
                            
    /// Colorize the selection
    template<class MeshT>
    void colorizeSelection(MeshT* _mesh, PrimitiveType _primitiveTypes, int _red, int _green, int _blue, int _alpha);

    /// Create a new mesh from the selection
    template< class MeshT >
    void createMeshFromSelection( MeshT& _mesh, MeshT& _newMesh, PrimitiveType _primitiveType);

    /// Select vertices by value
    template< class MeshT >
    void selectVerticesByValue(MeshT* _mesh, QString _component, bool _greater, double _value);

    /** @} */

    //===========================================================================
    /** @name Private helper function
     * @{ */
    //===========================================================================
private:

    /// Create a mesh containing the selection of the given mesh
    int createMeshFromSelection( int _objectId , PrimitiveType _primitiveType);

/** @} */

    //===========================================================================
    /** @name Member variables
    * @{ */
    //===========================================================================

private:

    /// Handle to selection environment
    QString environmentHandle_;

    /// Primitive type handles:
    SelectionInterface::PrimitiveType vertexType_;
    SelectionInterface::PrimitiveType edgeType_;
    SelectionInterface::PrimitiveType halfedgeType_;
    SelectionInterface::PrimitiveType faceType_;
    
    SelectionInterface::PrimitiveType allSupportedTypes_;

    /// Used for lasso selection tool
    QPolygon lasso_2Dpoints_;
    
    /// Used for volume lasso tool
    QVector<QPoint> volumeLassoPoints_;

    ConversionDialog* conversionDialog_;

    /** @} */

};

/// Traverse the scenegraph and call the selection function for all mesh nodes
class SelectVolumeAction
{
public:

    SelectVolumeAction(QRegion&_region, MeshObjectSelectionPlugin* _plugin,
                       unsigned int _type, bool _deselection, ACG::GLState& _state) :
            state_(_state), region_(_region), plugin_(_plugin), type_(_type), deselection_(_deselection) {};

    void enter(BaseNode* /*_node*/) {};

    void leave(BaseNode* /*_node*/) {};

    bool operator()(BaseNode* _node);

private:
    ACG::GLState&               state_;
    QRegion&                    region_;
    MeshObjectSelectionPlugin*  plugin_;
    unsigned int                type_;
    bool                        deselection_;
};
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(MESHOBJECTSELECTIONPLUGINT_CC)
#define MESHOBJECTSELECTIONPLUGINT_TEMPLATES
#include "MeshObjectSelectionPluginT.cc"
#endif

//=============================================================================
#endif // MESHOBJECTSELECTIONPLUGIN_HH defined
//=============================================================================