MeshObjectSelectionPlugin.cc 87.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 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 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
/*===========================================================================*\
 *                                                                           *
 *                              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$                   *
 *                                                                           *
\*===========================================================================*/

#include "MeshObjectSelectionPlugin.hh"

#include <set>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
#include <MeshTools/MeshSelectionT.hh>

// Primitive type icons
#define VERTEX_TYPE         "selection_vertex.png"
#define EDGE_TYPE           "selection_edge.png"
#define HEDGE_TYPE          "selection_halfedge.png"
#define FACE_TYPE           "selection_face.png"
// =======================================
// Define operations
// =======================================
// General:
#define G_CLEAR_HANDLE  "Clear Handle Region"
#define G_CLEAR_MODEL   "Clear Modeling Region"
#define G_CONVERT       "Convert Selection"
// Vertices:
#define V_SELECT_ALL    "Select All Vertices"
#define V_CLEAR         "Clear Vertex Selection"
#define V_INVERT        "Invert Vertex Selection"
#define V_BOUNDARY      "Select Boundary Vertices"
#define V_SHRINK        "Shrink Vertex Selection"
#define V_GROW          "Grow Vertex Selection"
#define V_DELETE        "Delete selected Vertices"
#define V_COLORIZE      "Colorize selected Vertices"
#define V_COPYSELECTION "Create mesh from Vertex Selection"
#define V_HANDLE        "Set to Handle Region"
#define V_MODELING      "Set to Modeling Region"
#define V_LOAD_FLIPPER  "Load Flipper Selection"
// Edges
#define E_SELECT_ALL    "Select All Edges"
#define E_CLEAR         "Clear Edge Selection"
#define E_INVERT        "Invert Edge Selection"
#define E_DELETE        "Delete selected Edges"
#define E_BOUNDARY      "Select Boundary Edges"
#define E_COLORIZE      "Colorize selected Edges"
#define E_COPYSELECTION "Create mesh from Edge Selection"
// Halfedges
#define HE_SELECT_ALL   "Select All Halfedges"
#define HE_CLEAR        "Clear Halfedge Selection"
#define HE_INVERT       "Invert Halfedge Selection"
#define HE_BOUNDARY     "Select Boundary Halfedges"
#define HE_COLORIZE     "Colorize selected Halfedges"
// Faces
#define F_SELECT_ALL    "Select All Faces"
#define F_CLEAR         "Clear Face Selection"
#define F_INVERT        "Invert Face Selection"
#define F_DELETE        "Delete selected Faces"
#define F_BOUNDARY      "Select Boundary Faces"
#define F_SHRINK        "Shrink Face Selection"
#define F_GROW          "Grow Face Selection"
#define F_COLORIZE      "Colorize selected Faces"
#define F_COPYSELECTION "Create mesh from Face Selection"

/// Default constructor
MeshObjectSelectionPlugin::MeshObjectSelectionPlugin() :
vertexType_(0),
edgeType_(0),
halfedgeType_(0),
faceType_(0),
allSupportedTypes_(0u),
conversionDialog_(0) {
}
      
MeshObjectSelectionPlugin::~MeshObjectSelectionPlugin() {
    
    delete conversionDialog_;
}

void MeshObjectSelectionPlugin::initializePlugin() {

    // Tell core about all scriptable slots
    updateSlotDescriptions();
    
    // Create conversion dialog
    if(!OpenFlipper::Options::nogui()) {
        conversionDialog_ = new ConversionDialog(0);
        QRect scr = QApplication::desktop()->screenGeometry();
        conversionDialog_->setGeometry((int)scr.width()/2-(int)conversionDialog_->width()/2,
                                       (int)scr.height()/2-(int)conversionDialog_->height()/2,
                                       conversionDialog_->width(), conversionDialog_->height());
        conversionDialog_->hide();
        connect(conversionDialog_->convertButton, SIGNAL(clicked()), this, SLOT(conversionRequested()));
        // Fill in combo boxes
        conversionDialog_->convertFromBox->addItems(
            QString("Vertex Selection;Edge Selection;Halfedge Selection;Face Selection;" \
                    "Feature Vertices;Feature Edges;Feature Faces;Handle Region;Modeling Region").split(";"));
        conversionDialog_->convertToBox->addItems(
            QString("Vertex Selection;Edge Selection;Halfedge Selection;Face Selection;" \
                    "Feature Vertices;Feature Edges;Feature Faces;Handle Region;Modeling Region").split(";"));
    }
}

void MeshObjectSelectionPlugin::pluginsInitialized() {
    // Create new selection environment for mesh objects
    // and register mesh types tri- and polymeshes for
    // the environment.
    
    QString iconPath = OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator();
    
    emit addSelectionEnvironment("Mesh Object Selections", "Select mesh object primitives such as vertices, (half-)edges and faces.",
Jan Möbius's avatar
Jan Möbius committed
148
                                 iconPath + "selections.png", environmentHandle_);
149 150 151 152 153 154

    // Register mesh object types
    emit registerType(environmentHandle_, DATA_POLY_MESH);
    emit registerType(environmentHandle_, DATA_TRIANGLE_MESH);
    
    // Register mesh primitive types
Jan Möbius's avatar
Jan Möbius committed
155 156 157 158
    emit addPrimitiveType(environmentHandle_, "Select Vertices",     iconPath + VERTEX_TYPE, vertexType_);
    emit addPrimitiveType(environmentHandle_, "Select Edges",        iconPath + EDGE_TYPE,   edgeType_);
    emit addPrimitiveType(environmentHandle_, "Select Halfedges",    iconPath + HEDGE_TYPE,  halfedgeType_);
    emit addPrimitiveType(environmentHandle_, "Select Faces",        iconPath + FACE_TYPE,   faceType_);
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 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 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 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 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    
    // Combine all supported types
    allSupportedTypes_ = (vertexType_ | edgeType_ | halfedgeType_ | faceType_);
    
    // Determine, which selection modes are requested
    emit showToggleSelectionMode(environmentHandle_, true, allSupportedTypes_);
    emit showSphereSelectionMode(environmentHandle_, true, allSupportedTypes_);

    emit showLassoSelectionMode(environmentHandle_, true, allSupportedTypes_);
    emit showVolumeLassoSelectionMode(environmentHandle_, true, allSupportedTypes_);

    emit showFloodFillSelectionMode(environmentHandle_, true, allSupportedTypes_);
    emit showComponentsSelectionMode(environmentHandle_, true, allSupportedTypes_);
    emit showClosestBoundarySelectionMode(environmentHandle_, true, allSupportedTypes_);
    
    // Define general operations
    QStringList generalOperations;
    generalOperations.append(G_CLEAR_HANDLE);
    generalOperations.append(G_CLEAR_MODEL);
    generalOperations.append(G_CONVERT);
    
    // Define vertex operations
    QStringList vertexOperations;
    vertexOperations.append(V_SELECT_ALL);
    vertexOperations.append(V_CLEAR);
    vertexOperations.append(V_INVERT);
    vertexOperations.append(V_BOUNDARY);
    vertexOperations.append(V_SHRINK);
    vertexOperations.append(V_GROW);
    vertexOperations.append(V_DELETE);
    vertexOperations.append(V_COLORIZE);
    vertexOperations.append(V_COPYSELECTION);
    vertexOperations.append(V_HANDLE);
    vertexOperations.append(V_MODELING);
    vertexOperations.append(V_LOAD_FLIPPER);
    
    // Define edge operations
    QStringList edgeOperations;
    edgeOperations.append(E_SELECT_ALL);
    edgeOperations.append(E_CLEAR);
    edgeOperations.append(E_INVERT);
    edgeOperations.append(E_DELETE);
    edgeOperations.append(E_BOUNDARY);
    edgeOperations.append(E_COLORIZE);
    edgeOperations.append(E_COPYSELECTION);
    
    // Define halfedge operations
    QStringList hedgeOperations;
    hedgeOperations.append(HE_SELECT_ALL);
    hedgeOperations.append(HE_CLEAR);
    hedgeOperations.append(HE_INVERT);
    hedgeOperations.append(HE_BOUNDARY);
    hedgeOperations.append(HE_COLORIZE);
    
    // Define face operations
    QStringList faceOperations;
    faceOperations.append(F_SELECT_ALL);
    faceOperations.append(F_CLEAR);
    faceOperations.append(F_INVERT);
    faceOperations.append(F_DELETE);
    faceOperations.append(F_BOUNDARY);
    faceOperations.append(F_SHRINK);
    faceOperations.append(F_GROW);
    faceOperations.append(F_COLORIZE);
    faceOperations.append(F_COPYSELECTION);
    
    emit addSelectionOperations(environmentHandle_, generalOperations, "Selection Operations");
    emit addSelectionOperations(environmentHandle_, vertexOperations,  "Vertex Operations",   vertexType_);
    emit addSelectionOperations(environmentHandle_, edgeOperations,    "Edge Operations",     edgeType_);
    emit addSelectionOperations(environmentHandle_, hedgeOperations,   "Halfedge Operations", halfedgeType_);
    emit addSelectionOperations(environmentHandle_, faceOperations,    "Face Operations",     faceType_);
    
    // 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);
    // Delete selected entities
    emit registerKeyShortcut(Qt::Key_Delete, Qt::NoModifier);
}

void MeshObjectSelectionPlugin::updateSlotDescriptions() {
    
    emit setSlotDescription("selectVertices(int,IdList)", tr("Select the specified vertices"),
                            QString("objectId,vertexList").split(","), QString("Id of object,List of vertices").split(","));
    emit setSlotDescription("unselectVertices(int,IdList)", tr("Unselect the specified vertices"),
                            QString("objectId,vertexList").split(","), QString("Id of object,List of vertices").split(","));
    emit setSlotDescription("selectAllVertices(int)", tr("Select all vertices of an object"),
                            QStringList("objectId"), QStringList("Id of object"));
    emit setSlotDescription("clearVertexSelection(int)", tr("Clear vertex selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("invertVertexSelection(int)", tr("Invert vertex selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectBoundaryVertices(int)", tr("Select all boundary vertices of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectClosestBoundaryVertices(int,int)", tr("Select boundary vertices closest to a specific vertex"),
                            QString("objectId,vertexId").split(","), QString("Id of an object,Id of closest vertex").split(","));
    emit setSlotDescription("shrinkVertexSelection(int)", tr("Shrink vertex selection by outer selection ring"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("growVertexSelection(int)", tr("Grow vertex selection by an-ring of selection"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("deleteVertexSelection(int)", tr("Delete selected vertices"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("createMeshFromVertexSelection(int)", tr("Take vertex selection and create a new mesh from it"),
                            QString("objectId").split(","), QString("Id of an object where the selection should be used to create a new mesh").split(","));

    emit setSlotDescription("selectVerticesByValue(int,QString,bool,double)", tr("Select vertices based on the value of their component"),
                                QString("objectId,component,greater,value").split(","),
                                QString("Id of an object where the selection should be used to create a new mesh,component specification: \"x\" or \"y\" or \"z\" ,true: select vertex if component greater than value; false: select if component less than value ,value to test").split(","));

    emit setSlotDescription("colorizeVertexSelection(int,int,int,int)", tr("Colorize the selected vertices"),
                            QString("objectId,r,g,b").split(","), QString("Id of an object,Red,Green,Blue").split(","));
    emit setSlotDescription("selectHandleVertices(int,IdList)", tr("Add specified vertices to handle area"),
                            QString("objectId,vertexList").split(","), QString("Id of an object,List of vertices").split(","));
    emit setSlotDescription("unselectHandleVertices(int,IdList)", tr("Remove specified vertices from handle area"),
                            QString("objectId,vertexList").split(","), QString("Id of an object,List of vertices").split(","));
    emit setSlotDescription("clearHandleVertices(int)", tr("Clear handle area"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("setAllHandleVertices(int)", tr("Add all vertices of an object to handle area"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectModelingVertices(int,IdList)", tr("Add specified vertices to modeling area"),
                            QString("objectId,vertexList").split(","), QString("Id of an object,List of vertices").split(","));
    emit setSlotDescription("unselectModelingVertices(int,IdList)", tr("Remove specified vertices to modeling area"),
                            QString("objectId,vertexList").split(","), QString("Id of an object,List of vertices").split(","));
    emit setSlotDescription("clearModelingVertices(int)", tr("Clear modeling area"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("setAllModelingVertices(int)", tr("Add al vertices of an object to modeling area"),
                            QStringList("objectId"), QStringList("Id of an object"));
    
    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("loadFlipperModelingSelection(int,QString)", tr("Load selection from Flipper selection file"),
                            QString("objectId,filename").split(","), QString("Id of an object,Flipper selection file").split(","));
    emit setSlotDescription("saveFlipperModelingSelection(int,QString)", tr("Save selection into Flipper selection file"),
                            QString("objectId,filename").split(","), QString("Id of an object,Flipper selection file").split(","));
    
    emit setSlotDescription("selectEdges(int,IdList)", tr("Select the specified edges"),
                            QString("objectId,edgeList").split(","), QString("Id of an object,List of edges").split(","));
    emit setSlotDescription("unselectEdges(int,IdList)", tr("Unselect the specified edges"),
                            QString("objectId,edgeList").split(","), QString("Id of an object,List of edges").split(","));
    emit setSlotDescription("selectAllEdges(int)", tr("Select all edges of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("invertEdgeSelection(int)", tr("Invert edge selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("clearEdgeSelection(int)", tr("Clear edge selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectBoundaryEdges(int)", tr("Select all boundary edges of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    
    emit setSlotDescription("colorizeEdgeSelection(int,int,int,int)", tr("Colorize the selected edges"),
                            QString("objectId,r,g,b").split(","), QString("Id of an object,Red,Green,Blue").split(","));
    emit setSlotDescription("createMeshFromEdgeSelection(int)", tr("Take edge selection and create a new mesh from it"),
                            QString("objectId").split(","), QString("Id of an object where the selection should be used to create a new mesh").split(","));
    
    emit setSlotDescription("selectHalfedges(int,IdList)", tr("Select the specified halfedges"),
                            QString("objectId,halfedgeList").split(","), QString("Id of an object,List of halfedges").split(","));
    emit setSlotDescription("unselectHalfedges(int,IdList)", tr("Unselect the specified halfedges"),
                            QString("objectId,halfedgeList").split(","), QString("Id of an object,List of halfedges").split(","));
    emit setSlotDescription("selectAllHalfedges(int)", tr("Select all halfedges of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("invertHalfedgeSelection(int)", tr("Invert halfedge selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("clearHalfedgeSelection(int)", tr("Clear halfedge selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectBoundaryHalfedges(int)", tr("Select all boundary halfedges of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    
    emit setSlotDescription("colorizeHalfedgeSelection(int,int,int,int)", tr("Colorize the selected halfedges"),
                            QString("objectId,r,g,b").split(","), QString("Id of an object,Red,Green,Blue").split(","));
                            
    emit setSlotDescription("selectFaces(int,IdList)", tr("Select the specified faces"),
                            QString("objectId,faceList").split(","), QString("Id of an object,List of faces").split(","));
    emit setSlotDescription("unselectFaces(int,IdList)", tr("Unselect the specified faces"),
                            QString("objectId,faceList").split(","), QString("Id of an object,List of faces").split(","));
    emit setSlotDescription("selectAllFaces(int)", tr("Select all vertices of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("clearFaceSelection(int)", tr("Clear face selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("invertFaceSelection(int)", tr("Invert face selection of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("selectBoundaryFaces(int)", tr("Select all boundary faces of an object"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("shrinkFaceSelection(int)", tr("Shrink face selection by outer face ring of selection"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("growFaceSelection(int)", tr("Grow face selection by an-ring of faces around selection"),
                            QStringList("objectId"), QStringList("Id of an object"));
    emit setSlotDescription("colorizeFaceSelection(int,int,int,int)", tr("Colorize the selected faces"),
                            QString("objectId,r,g,b").split(","), QString("Id of an object,Red,Green,Blue").split(","));

    emit setSlotDescription("createMeshFromFaceSelection(int)", tr("Take face selection and create a new mesh from it"),
                            QString("objectId").split(","), QString("Id of an object where the selection should be used to create a new mesh").split(","));

}

void MeshObjectSelectionPlugin::slotSelectionOperation(QString _operation) {
    
    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_CLEAR_HANDLE) {
        // Clear handle region
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearHandleVertices(o_it->id());
        }
    } else if(_operation == G_CLEAR_MODEL) {
        // Clear modeling region
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearModelingVertices(o_it->id());
        }
    } else if(_operation == G_CONVERT) {
        // Convert vertex selection
        if(!OpenFlipper::Options::nogui())
            conversionDialog_->show();
    } else if(_operation == V_SELECT_ALL) {
        // Select all vertices
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectAllVertices(o_it->id());
        }
    } else if(_operation == V_CLEAR) {
        // Clear vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearVertexSelection(o_it->id());
        }
    } else if(_operation == V_INVERT) {
        // Invert vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                invertVertexSelection(o_it->id());
        }
    } else if(_operation == V_BOUNDARY) {
        // Select boundary vertices
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectBoundaryVertices(o_it->id());
        }
    } else if(_operation == V_SHRINK) {
        // Shrink vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                shrinkVertexSelection(o_it->id());
        }
    } else if(_operation == V_GROW) {
        // Grow vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                growVertexSelection(o_it->id());
        }
    } else if(_operation == V_DELETE) {
        // Delete vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()){
              deleteVertexSelection(o_it->id());
              emit updatedObject(o_it->id(), UPDATE_GEOMETRY);
              emit createBackup(o_it->id(), "Delete Vertices", UPDATE_GEOMETRY);
            }
        }
    } else if(_operation == V_COLORIZE) {
        // Colorize vertex selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                setColorForSelection(o_it->id(), vertexType_);
            }
        }
    } else if(_operation == V_COPYSELECTION) {
        // Copy vertex selection
        std::vector<int> objects;
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
          if (o_it->visible()) {
            objects.push_back(o_it->id());
          }
        }

        for ( unsigned int i = 0 ; i < objects.size() ; ++i)
          createMeshFromSelection(objects[i],vertexType_);

    } else if(_operation == V_HANDLE) {
        // Set vertex selection to be handle region
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                std::vector<int> ids = getVertexSelection(o_it->id());
                selectHandleVertices(o_it->id(), ids);
                clearVertexSelection(o_it->id());
            }
        }
    } else if(_operation == V_MODELING) {
        // Set vertex selection to be modeling
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                std::vector<int> ids = getVertexSelection(o_it->id());
                selectModelingVertices(o_it->id(), ids);
                clearVertexSelection(o_it->id());
            }
        }
    } else if(_operation == V_LOAD_FLIPPER) {
        // Load Flipper selection file
        QString fileName = QFileDialog::getOpenFileName(0,
                tr("Open Flipper Selection File"), OpenFlipper::Options::dataDirStr(),
                tr("Flipper Selection Files (*.sel)"));
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                loadFlipperModelingSelection(o_it->id(), fileName);
            }
        }
    } else if(_operation == E_SELECT_ALL) {
        // Select all edges
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectAllEdges(o_it->id());
        }
    } else if(_operation == E_CLEAR) {
        // Clear edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearEdgeSelection(o_it->id());
        }
    } else if(_operation == E_INVERT) {
        // Invert edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                invertEdgeSelection(o_it->id());
        }
    } else if(_operation == E_DELETE) {
        // Delete edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                deleteEdgeSelection(o_it->id());
        }
    } else if(_operation == E_BOUNDARY) {
        // Select boundary edges
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectBoundaryEdges(o_it->id());
        }
    } else if(_operation == E_COLORIZE) {
        // Colorize edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                setColorForSelection(o_it->id(), edgeType_);
            }
        }
    } else if(_operation == E_COPYSELECTION) {
      // Copy edge selection
      std::vector<int> objects;
      for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
          o_it != PluginFunctions::objectsEnd(); ++o_it) {
        if (o_it->visible()) {
          objects.push_back(o_it->id());
        }
      }

      for ( unsigned int i = 0 ; i < objects.size() ; ++i)
        createMeshFromSelection(objects[i],edgeType_);

    } else if(_operation == HE_SELECT_ALL) {
        // Select all edges
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectAllHalfedges(o_it->id());
        }
    } else if(_operation == HE_CLEAR) {
        // Clear edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearHalfedgeSelection(o_it->id());
        }
    } else if(_operation == HE_INVERT) {
        // Invert edge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                invertHalfedgeSelection(o_it->id());
        }
    } else if(_operation == HE_BOUNDARY) {
        // Select boundary edges
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectBoundaryHalfedges(o_it->id());
        }
    } else if(_operation == HE_COLORIZE) {
        // Colorize halfedge selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                setColorForSelection(o_it->id(), halfedgeType_);
            }
        }
    } else if(_operation == F_SELECT_ALL) {
        // Select all faces
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectAllFaces(o_it->id());
        }
    } else if(_operation == F_CLEAR) {
        // Clear face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                clearFaceSelection(o_it->id());
        }
    } else if(_operation == F_INVERT) {
        // Invert face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                invertFaceSelection(o_it->id());
        }
    } else if(_operation == F_DELETE) {
        // Delete face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                deleteFaceSelection(o_it->id());
        }
    } else if(_operation == F_BOUNDARY) {
        // Select boundary faces
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                selectBoundaryFaces(o_it->id());
        }
    } else if(_operation == F_SHRINK) {
        // Shrink face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                shrinkFaceSelection(o_it->id());
        }
    } else if(_operation == F_GROW) {
        // Grow face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible())
                growFaceSelection(o_it->id());
        }
    } else if(_operation == F_COLORIZE) {
        // Colorize face selection
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                setColorForSelection(o_it->id(), faceType_);
            }
        }
    } else if(_operation == F_COPYSELECTION) {
      // Copy face selection
      std::vector<int> objects;
      for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_ALL));
          o_it != PluginFunctions::objectsEnd(); ++o_it) {
        if (o_it->visible()) {
          objects.push_back(o_it->id());
        }
      }

      for ( unsigned int i = 0 ; i < objects.size() ; ++i)
        createMeshFromSelection(objects[i],faceType_);
    }
}

void MeshObjectSelectionPlugin::setColorForSelection(const int _objectId, const PrimitiveType _primitiveTypes) {
    
    QColor c = QColorDialog::getColor(Qt::red, 0, tr("Choose color"),QColorDialog::ShowAlphaChannel);
    
    if(c.isValid()) {
        if(_primitiveTypes & vertexType_) {
            // Vertex colorization
            colorizeVertexSelection(_objectId, c.red(), c.green(), c.blue(), c.alpha());
        } else if (_primitiveTypes & edgeType_) {
            // Edge colorization
            colorizeEdgeSelection(_objectId, c.red(), c.green(), c.blue(), c.alpha());
        } else if (_primitiveTypes & halfedgeType_) {
            // Edge colorization
            colorizeHalfedgeSelection(_objectId, c.red(), c.green(), c.blue(), c.alpha());
        } else if (_primitiveTypes & faceType_) {
            // Edge colorization
            colorizeFaceSelection(_objectId, c.red(), c.green(), c.blue(), c.alpha());
        }
    }
}

void MeshObjectSelectionPlugin::conversionRequested() {
    
    conversionDialog_->hide();
    
    QString from = conversionDialog_->convertFromBox->currentText();
    QString to = conversionDialog_->convertToBox->currentText();
    
    if(from == to) return;
    
    bool deselect = true;
    if(!OpenFlipper::Options::nogui()) {
        deselect = conversionDialog_->deselect->isChecked();
    }
    
    conversion(from, to, deselect);
}

void MeshObjectSelectionPlugin::conversion(const QString& _from, const QString& _to, bool _deselect) {

  for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH));
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
    
        if(_from == "Vertex Selection") {
            if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Halfedge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Feature Vertices") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexSelectionToFeatureVertices(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexSelectionToFeatureVertices(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Handle Region") {
                selectHandleVertices(o_it->id(), getVertexSelection(o_it->id()));
            } else if (_to == "Modeling Region") {
                selectModelingVertices(o_it->id(), getVertexSelection(o_it->id()));
            }
            
            if(_deselect) {
                clearVertexSelection(o_it->id());
            }
            
        } else if (_from == "Edge Selection") {
            if(_to == "Vertex Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertEdgeToVertexSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertEdgeToVertexSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Halfedge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertEdgeToHalfedgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertEdgeToHalfedgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertEdgeToFaceSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertEdgeToFaceSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Feature Edges") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertEdgeSelectionToFeatureEdges(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertEdgeSelectionToFeatureEdges(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Handle Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::EdgeIter e_it = mesh->edges_begin(); e_it != mesh->edges_end(); ++e_it) {
                        if(mesh->status(e_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 0)).idx());
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 1)).idx());
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::EdgeIter e_it = mesh->edges_begin(); e_it != mesh->edges_end(); ++e_it) {
                        if(mesh->status(e_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 0)).idx());
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 1)).idx());
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                }
            } else if (_to == "Modeling Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::EdgeIter e_it = mesh->edges_begin(); e_it != mesh->edges_end(); ++e_it) {
                        if(mesh->status(e_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 0)).idx());
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 1)).idx());
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::EdgeIter e_it = mesh->edges_begin(); e_it != mesh->edges_end(); ++e_it) {
                        if(mesh->status(e_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 0)).idx());
                            ids.push_back(mesh->to_vertex_handle(mesh->halfedge_handle(e_it, 1)).idx());
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                }
            }
            
            if(_deselect) {
                clearEdgeSelection(o_it->id());
            }
        } else if (_from == "Halfedge Selection") {
            if(_to == "Vertex Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertHalfedgeToVertexSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertHalfedgeToVertexSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertHalfedgeToEdgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertHalfedgeToEdgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertHalfedgeToFaceSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertHalfedgeToFaceSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Handle Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::HalfedgeIter h_it = mesh->halfedges_begin(); h_it != mesh->halfedges_end(); ++h_it) {
                        if(mesh->status(h_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(h_it).idx());
                            ids.push_back(mesh->from_vertex_handle(h_it).idx());
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::HalfedgeIter h_it = mesh->halfedges_begin(); h_it != mesh->halfedges_end(); ++h_it) {
                        if(mesh->status(h_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(h_it).idx());
                            ids.push_back(mesh->from_vertex_handle(h_it).idx());
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                }
            } else if (_to == "Modeling Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::HalfedgeIter h_it = mesh->halfedges_begin(); h_it != mesh->halfedges_end(); ++h_it) {
                        if(mesh->status(h_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(h_it).idx());
                            ids.push_back(mesh->from_vertex_handle(h_it).idx());
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::HalfedgeIter h_it = mesh->halfedges_begin(); h_it != mesh->halfedges_end(); ++h_it) {
                        if(mesh->status(h_it).selected()) {
                            ids.push_back(mesh->to_vertex_handle(h_it).idx());
                            ids.push_back(mesh->from_vertex_handle(h_it).idx());
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                }
            }
            
            if(_deselect) {
                clearHalfedgeSelection(o_it->id());
            }
        } else if (_from == "Face Selection") {
            if(_to == "Vertex Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertFaceToVertexSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertFaceToVertexSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertFaceToEdgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertFaceToEdgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Feature Faces") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertFaceSelectionToFeatureFaces(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertFaceSelectionToFeatureFaces(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Halfedge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertFaceToHalfedgeSelection(PluginFunctions::triMesh(o_it));
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertFaceToHalfedgeSelection(PluginFunctions::polyMesh(o_it));
            } else if (_to == "Handle Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::FaceIter f_it = mesh->faces_begin(); f_it != mesh->faces_end(); ++f_it) {
                        if(mesh->status(f_it).selected()) {
                            for(TriMesh::FaceVertexIter fv_it = mesh->fv_iter(f_it); fv_it; ++fv_it) {
                                ids.push_back(fv_it.handle().idx());
                            }
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::FaceIter f_it = mesh->faces_begin(); f_it != mesh->faces_end(); ++f_it) {
                        if(mesh->status(f_it).selected()) {
                            for(PolyMesh::FaceVertexIter fv_it = mesh->fv_iter(f_it); fv_it; ++fv_it) {
                                ids.push_back(fv_it.handle().idx());
                            }
                        }
                    }
                    selectHandleVertices(o_it->id(), ids);
                }
            } else if (_to == "Modeling Region") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    TriMesh* mesh = PluginFunctions::triMesh(o_it);
                    std::vector<int> ids;
                    for(TriMesh::FaceIter f_it = mesh->faces_begin(); f_it != mesh->faces_end(); ++f_it) {
                        if(mesh->status(f_it).selected()) {
                            for(TriMesh::FaceVertexIter fv_it = mesh->fv_iter(f_it); fv_it; ++fv_it) {
                                ids.push_back(fv_it.handle().idx());
                            }
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    PolyMesh* mesh = PluginFunctions::polyMesh(o_it);
                    std::vector<int> ids;
                    for(PolyMesh::FaceIter f_it = mesh->faces_begin(); f_it != mesh->faces_end(); ++f_it) {
                        if(mesh->status(f_it).selected()) {
                            for(PolyMesh::FaceVertexIter fv_it = mesh->fv_iter(f_it); fv_it; ++fv_it) {
                                ids.push_back(fv_it.handle().idx());
                            }
                        }
                    }
                    selectModelingVertices(o_it->id(), ids);
                }
            }
            
            if(_deselect) {
                clearFaceSelection(o_it->id());
            }
        } else if (_from == "Feature Vertices") {

            if (_to == "Vertex Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    MeshSelection::convertFeatureVerticesToVertexSelection(PluginFunctions::triMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureVertices(PluginFunctions::triMesh(o_it));
                    }
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    MeshSelection::convertFeatureVerticesToVertexSelection(PluginFunctions::polyMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureVertices(PluginFunctions::polyMesh(o_it));
                    }
                }
            }
        } else if (_from == "Feature Edges") {

            if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    MeshSelection::convertFeatureEdgesToEdgeSelection(PluginFunctions::triMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureEdges(PluginFunctions::triMesh(o_it));
                    }
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    MeshSelection::convertFeatureEdgesToEdgeSelection(PluginFunctions::polyMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureEdges(PluginFunctions::polyMesh(o_it));
                    }
                }
            }
        } else if (_from == "Feature Faces") {

            if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH) {
                    MeshSelection::convertFeatureFacesToFaceSelection(PluginFunctions::triMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureFaces(PluginFunctions::triMesh(o_it));
                    }
                } else if(o_it->dataType() == DATA_POLY_MESH) {
                    MeshSelection::convertFeatureFacesToFaceSelection(PluginFunctions::polyMesh(o_it));
                    if (_deselect) {
                        MeshSelection::clearFeatureFaces(PluginFunctions::polyMesh(o_it));
                    }
                }
            }
        } else if (_from == "Handle Region") {
            std::vector<int> ids = getHandleVertices(o_it->id());
            if(_to == "Vertex Selection") {
                selectVertices(o_it->id(), ids);
            } else if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Halfedge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Modeling Region") {
                selectModelingVertices(o_it->id(), ids);
            }
            
            if(_deselect) {
                clearHandleVertices(o_it->id());
            }
            
        } else if (_from == "Modeling Region") {
            std::vector<int> ids = getModelingVertices(o_it->id());
            if(_to == "Vertex Selection") {
                selectVertices(o_it->id(), ids);
            } else if (_to == "Edge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToEdgeSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Halfedge Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToHalfedgeSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Face Selection") {
                if(o_it->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::triMesh(o_it), ids);
                else if(o_it->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertVertexToFaceSelection(PluginFunctions::polyMesh(o_it), ids);
            } else if (_to == "Handle Region") {
                selectHandleVertices(o_it->id(), ids);
            }
            
            if(_deselect) {
                clearModelingVertices(o_it->id());
            }
        }
        
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
        emit createBackup(o_it->id(), "Selection Conversion", UPDATE_SELECTION);
    }
}

void MeshObjectSelectionPlugin::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;
    
    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;

    // First of all, pick anything to find all possible objects
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING,
        _event->pos(), node_idx, target_idx, &hit_point)) {

        BaseObjectData* object(0);
        PluginFunctions::getPickedObject(node_idx, object);
        if(!object) return;
    
        if (object->dataType() == DATA_TRIANGLE_MESH) {
            // Pick triangle meshes
            if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(),node_idx, target_idx, &hit_point)) {

                if (object->dataType(DATA_TRIANGLE_MESH)) {
1066
                    toggleMeshSelection(object->id(), PluginFunctions::triMesh(object), target_idx, hit_point, _currentType);
1067 1068 1069 1070 1071 1072 1073
                }
            }
        } else if (object->dataType() == DATA_POLY_MESH) {
            // Pick poly meshes
            if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(),node_idx, target_idx, &hit_point)) {

                if (object->dataType(DATA_POLY_MESH)) {
1074
                    toggleMeshSelection(object->id(), PluginFunctions::polyMesh(object), target_idx, hit_point, _currentType);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
                }
            }
        }
        emit updatedObject(object->id(), UPDATE_SELECTION);
        emit createBackup(object->id(), "Toggle Selection", UPDATE_SELECTION);
    }
}

void MeshObjectSelectionPlugin::slotLassoSelection(QMouseEvent* _event, PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
   
    if(_event->type() == QEvent::MouseButtonPress) {
 
        // Add picked point
        lasso_2Dpoints_.push_back(_event->pos());
        
        return;

    } else if(_event->type() == QEvent::MouseButtonDblClick) {
        
        // Finish surface lasso selection
        if (lasso_2Dpoints_.size() > 2) {

            QRegion region(lasso_2Dpoints_);
            
            lassoSelect(region, _currentType, _deselect);
        }
        
        // Clear points
        lasso_2Dpoints_.clear();
    }
}

void MeshObjectSelectionPlugin::slotVolumeLassoSelection(QMouseEvent* _event, PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
    
    if(_event->type() == QEvent::MouseButtonPress) {

        // Add point on viewing plane to selection polygon
        volumeLassoPoints_.append(_event->pos());
        
        return;

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

        ACG::GLState &state = PluginFunctions::viewerProperties().glState();
        bool updateGL = state.updateGL();
        state.set_updateGL (false);

        QPolygon p(volumeLassoPoints_);
        QRegion region = QRegion(p);

        SelectVolumeAction action(region, this, _currentType, _deselect, state);
        ACG::SceneGraph::traverse (PluginFunctions::getRootNode(), action);

        state.set_updateGL(updateGL);

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

void MeshObjectSelectionPlugin::slotSphereSelection(QMouseEvent* _event, double _radius, PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
    
    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;
    
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(), node_idx, target_idx, &hit_point)) {
        
        BaseObjectData* object = 0;

        if (PluginFunctions::getPickedObject(node_idx, object)) {

            if (object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH)) {
                paintSphereSelection(PluginFunctions::triMesh(object), target_idx, hit_point, _radius, _currentType, _deselect);
            } else if (object->picked(node_idx) && object->dataType(DATA_POLY_MESH)) {
                paintSphereSelection(PluginFunctions::polyMesh(object), target_idx, hit_point, _radius, _currentType, _deselect);
            }

            emit updatedObject(object->id(), UPDATE_SELECTION);
            if ( _event->type() == QEvent::MouseButtonRelease )
              emit  createBackup(object->id(), "Sphere Selection", UPDATE_SELECTION);
        }
    }
}

void MeshObjectSelectionPlugin::slotClosestBoundarySelection(QMouseEvent* _event, PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;

    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;

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

        if(PluginFunctions::getPickedObject(node_idx, object)) {

            if(object->dataType(DATA_TRIANGLE_MESH)) {

                TriMesh* m = PluginFunctions::triMesh(object);
                TriMesh::VertexHandle vh = m->fv_iter(m->face_handle(target_idx)).handle();

                closestBoundarySelection(m, vh.idx(), _currentType, _deselect);

                emit updatedObject(object->id(), UPDATE_SELECTION);
                emit  createBackup(object->id(), "Boundary Selection", UPDATE_SELECTION);

            } else if(object->dataType(DATA_POLY_MESH)) {

                PolyMesh* m = PluginFunctions::polyMesh(object);
                PolyMesh::VertexHandle vh = m->fv_iter(m->face_handle(target_idx)).handle();

                closestBoundarySelection(m, vh.idx(), _currentType, _deselect);

                emit updatedObject(object->id(), UPDATE_SELECTION);
                emit  createBackup(object->id(), "Boundary Selection", UPDATE_SELECTION);
            }
            
            emit updateView();
        }
    }
}

void MeshObjectSelectionPlugin::slotFloodFillSelection(QMouseEvent* _event, double _maxAngle, PrimitiveType _currentType, bool _deselect) {
    
    // Return if none of the currently active types is handled by this plugin
    if((_currentType & allSupportedTypes_) == 0) return;
    
    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;

    // pick Anything to find all possible objects
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING,
                                        _event->pos(), node_idx, target_idx, &hit_point)) {

        BaseObjectData* object = 0;
        
        if(PluginFunctions::getPickedObject(node_idx, object)) {

            // TRIANGLE MESHES
            if(object->dataType() == DATA_TRIANGLE_MESH) {

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

                    if(PluginFunctions::getPickedObject(node_idx, object)) {

                        if(object->dataType(DATA_TRIANGLE_MESH)) {
                            floodFillSelection(PluginFunctions::triMesh(object), target_idx, _maxAngle, _currentType, _deselect);
                            emit updatedObject(object->id(), UPDATE_SELECTION);
                            emit  createBackup(object->id(), "FloodFill Selection", UPDATE_SELECTION);
                        }
                    }
                }

            // POLY MESHES
            } else if(object->dataType() == DATA_POLY_MESH) {

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

                    if(PluginFunctions::getPickedObject(node_idx, object) ) {

                        if(object->dataType(DATA_POLY_MESH)) {
                            floodFillSelection(PluginFunctions::polyMesh(object), target_idx, _maxAngle, _currentType, _deselect);
                            emit updatedObject(object->id(), UPDATE_SELECTION);
                            emit  createBackup(object->id(), "FloodFill Selection", UPDATE_SELECTION);
                        }
                    }
                }
            }
            else {
                emit log(LOGERR,tr("floodFillSelection: Unsupported dataType"));
            }
        }
    }
}

void MeshObjectSelectionPlugin::slotComponentsSelection(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;

    unsigned int node_idx, target_idx;
    ACG::Vec3d hit_point;

    // First of all, pick anything to find all possible objects
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING,
        _event->pos(), node_idx, target_idx, &hit_point)) {

        BaseObjectData* object(0);
        PluginFunctions::getPickedObject(node_idx, object);
        if(!object) return;

        if (object->dataType() == DATA_TRIANGLE_MESH) {
            // Pick triangle meshes
            if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(),node_idx, target_idx, &hit_point)) {

                if (object->dataType(DATA_TRIANGLE_MESH)) {
                    componentsMeshSelection(PluginFunctions::triMesh(object), target_idx, hit_point, _currentType);
                }
            }
        } else if (object->dataType() == DATA_POLY_MESH) {
            // Pick poly meshes
            if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(),node_idx, target_idx, &hit_point)) {

                if (object->dataType(DATA_POLY_MESH)) {
                    componentsMeshSelection(PluginFunctions::polyMesh(object), target_idx, hit_point, _currentType);
                }
            }
        }
        emit updatedObject(object->id(), UPDATE_SELECTION);
        emit createBackup(object->id(), "Connected Components Selection", UPDATE_SELECTION);
    }
}

void MeshObjectSelectionPlugin::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 MeshObjectSelectionPlugin::loadIniFile(INIFile& _ini, int _id) {

    BaseObjectData* object = 0;
    if (!PluginFunctions::getObject(_id,object)) {
        emit log(LOGERR,tr("Cannot find object for id ") + QString::number(_id) + tr(" in saveFile") );
        return;
    }

    std::vector<int> ids;
    bool invert = false;

    bool updated_selection = false;
    bool updated_modeling_regions = false;

    QString sectionName = object->name();

    if (_ini.get_entry(ids, sectionName ,"ModelingRegion")) {
        invert = false;
        _ini.get_entry(invert, sectionName, "InvertModeling");

        if (invert) {
            setAllModelingVertices(object->id());
            unselectModelingVertices(object->id() , ids);
        } else {
            clearModelingVertices(object->id());
            selectModelingVertices(object->id(), ids);
        }

        if(object->dataType(DATA_TRIANGLE_MESH)) {
            update_regions(PluginFunctions::triMesh(object));
        }

        if(object->dataType(DATA_POLY_MESH)) {
            update_regions(PluginFunctions::polyMesh(object));
        }

        updated_modeling_regions = true;
    }

    if(_ini.get_entry(ids, sectionName, "HandleRegion")) {
        invert = false;
        _ini.get_entry(invert, sectionName, "InvertHandle");

        if(invert) {
            setAllHandleVertices(object->id());
            unselectHandleVertices(object->id(), ids);
        } else {
            clearHandleVertices(object->id());
            selectHandleVertices(object->id(), ids);
        }

        if (object->dataType(DATA_TRIANGLE_MESH)) {
            update_regions(PluginFunctions::triMesh(object));
        }

        if(object->dataType(DATA_POLY_MESH)) {
            update_regions(PluginFunctions::polyMesh(object));
        }
        
        updated_modeling_regions = true;
    }

    if(_ini.get_entry(ids, sectionName, "VertexSelection")) {
        invert = false;
        _ini.get_entry(invert, sectionName, "InvertVertexSelection");

        if(invert) {
            selectAllVertices(object->id());
            unselectVertices(object->id(),ids);
        } else {
            clearVertexSelection(object->id());
            selectVertices(object->id(),ids);
        }

        updated_selection = true;
    }

    if(_ini.get_entry(ids, sectionName, "EdgeSelection")) {
        invert = false;
        _ini.get_entry(invert, sectionName, "InvertEdgeSelection");

        ids = convertVertexPairsToEdges(_id, ids);

        if(invert) {
            selectAllEdges(object->id());
            unselectEdges(object->id(),ids);
        } else {
            clearEdgeSelection(object->id());
            selectEdges(object->id(),ids);
        }

        updated_selection = true;
    }

    if(_ini.get_entry(ids, sectionName, "FaceSelection")) {
        invert = false;
        _ini.get_entry(invert, sectionName, "InvertFaceSelection");

        if(invert) {
            selectAllFaces(object->id());
            unselectFaces(object->id(),ids);
        } else {
            clearFaceSelection(object->id());
            selectFaces(object->id(),ids);
        }

        updated_selection = true;
    }

    if(updated_modeling_regions) {

        emit updatedObject(object->id(), UPDATE_ALL);
        emit updateView();

    } else if(updated_selection) {

        emit updatedObject(object->id(), UPDATE_SELECTION);
        emit updateView();
    }

    if ( updated_modeling_regions || updated_selection )
      emit  createBackup(object->id(), "Load Selection", UPDATE_SELECTION);
}

void MeshObjectSelectionPlugin::saveIniFile(INIFile& _ini, int _id) {
    
    BaseObjectData* object = 0;
    if ( !PluginFunctions::getObject(_id,object) ) {
        emit log(LOGERR,tr("Cannot find object for id ") + QString::number(_id) + tr(" in saveFile") );
        return;
    }

    std::vector<int> ids;

    // The objects section should already exist
    QString sectionName = object->name();
    if(!_ini.section_exists(sectionName)) {
        emit log(LOGERR,tr("Cannot find object section id ") + QString::number(_id) + tr(" in saveFile") );
        return;
    }

    _ini.add_entry(sectionName, "VertexSelection", getVertexSelection(object->id()));
    _ini.add_entry(sectionName, "EdgeSelection", convertEdgesToVertexPairs(_id, getEdgeSelection(object->id())));

    if(object->dataType(DATA_POLY_MESH ) || object->dataType( DATA_TRIANGLE_MESH)) {
        _ini.add_entry(sectionName, "FaceSelection", getFaceSelection(object->id()));
        _ini.add_entry(sectionName, "HandleRegion", getHandleVertices(object->id()));
        _ini.add_entry(sectionName, "ModelingRegion", getModelingVertices(object->id()));
    }
}

void MeshObjectSelectionPlugin::slotLoadSelection(const INIFile& _file) {
    
    // Iterate over all mesh objects in the scene and save
    // the selections for all supported entity types
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
        
        // Read section for each object
        // Append object name to section identifier
        QString section = QString("MeshObjectSelection") + "//" + o_it->name();
        if(!_file.section_exists(section)) {
            continue;
        }
        
        std::vector<int> ids;
        // Load vertex selection:
        _file.get_entry(ids, section, "VertexSelection");
        selectVertices(o_it->id(), ids);
        ids.clear();
        // Load edge selection:
        _file.get_entry(ids, section, "EdgeSelection");
        selectEdges(o_it->id(), convertVertexPairsToEdges(o_it->id(), ids));
        ids.clear();
        // Load halfedge selection:
        _file.get_entry(ids, section, "HalfedgeSelection");
        selectHalfedges(o_it->id(), convertVertexPairsToHalfedges(o_it->id(), ids));
        ids.clear();
        // Load face selection:
        _file.get_entry(ids, section, "FaceSelection");
        selectFaces(o_it->id(), ids);
        ids.clear();
        // Load handle region:
        _file.get_entry(ids, section, "HandleRegion");
        selectHandleVertices(o_it->id(), ids);
        ids.clear();
        // Load modeling region:
        _file.get_entry(ids, section, "ModelingRegion");
        selectModelingVertices(o_it->id(), ids);
        ids.clear();
        
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
        emit  createBackup(o_it->id(), "Load Selection", UPDATE_SELECTION);
    }
}

void MeshObjectSelectionPlugin::slotSaveSelection(INIFile& _file) {
    
    // Iterate over all mesh objects in the scene and save
    // the selections for all supported entity types
    for (PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
         o_it != PluginFunctions::objectsEnd(); ++o_it) {
        
        // Create section for each object
        // Append object name to section identifier
        QString section = QString("MeshObjectSelection") + "//" + o_it->name();

        // Store vertex selection:
        _file.add_entry(section, "VertexSelection", getVertexSelection(o_it->id()));
        // Store edge selection:
        _file.add_entry(section, "EdgeSelection", convertEdgesToVertexPairs(o_it->id(), getEdgeSelection(o_it->id())));
        // Store halfedge selection:
        _file.add_entry(section, "HalfedgeSelection", convertHalfedgesToVertexPairs(o_it->id(), getHalfedgeSelection(o_it->id())));
        // Store face selection:
        _file.add_entry(section, "FaceSelection", getFaceSelection(o_it->id()));
        // Store handle region:
        _file.add_entry(section, "HandleRegion", getHandleVertices(o_it->id()));
        // Store modeling region:
        _file.add_entry(section, "ModelingRegion", getModelingVertices(o_it->id()));
    }
}

void MeshObjectSelectionPlugin::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) {
        
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                
                if(type & vertexType_)
                    selectAllVertices(o_it->id());
                if(type & edgeType_)
                    selectAllEdges(o_it->id());
                if(type & halfedgeType_)
                    selectAllHalfedges(o_it->id());
                if(type & faceType_)
                    selectAllFaces(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) {
        
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                
                if(type & vertexType_)
                    clearVertexSelection(o_it->id());
                if(type & edgeType_)
                    clearEdgeSelection(o_it->id());
                if(type & halfedgeType_)
                    clearHalfedgeSelection(o_it->id());
                if(type & faceType_)
                    clearFaceSelection(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) {
        
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                
                if(type & vertexType_)
                    invertVertexSelection(o_it->id());
                if(type & edgeType_)
                    invertEdgeSelection(o_it->id());
                if(type & halfedgeType_)
                    invertHalfedgeSelection(o_it->id());
                if(type & faceType_)
                    invertFaceSelection(o_it->id());
            }
            emit updatedObject(o_it->id(), UPDATE_SELECTION);
            emit  createBackup(o_it->id(), "Invert Selection", UPDATE_SELECTION);
        }
    } else if (_key == Qt::Key_Delete && _modifiers == Qt::NoModifier) {
        
        for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)); 
            o_it != PluginFunctions::objectsEnd(); ++o_it) {
            if (o_it->visible()) {
                // Delete all selected primitives
                if(type & vertexType_)
                    deleteVertexSelection(o_it->id());
                if(type & edgeType_)
                    deleteEdgeSelection(o_it->id());
                if(type & faceType_)
                    deleteFaceSelection(o_it->id());
            }
            emit updatedObject(o_it->id(), UPDATE_TOPOLOGY);
            emit  createBackup(o_it->id(), "Delete Selection", UPDATE_TOPOLOGY);
        }
    }
}

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
void MeshObjectSelectionPlugin::slotMouseWheelEvent(QWheelEvent* event, std::string const& mode) {

  // Get currently active primitive type
  SelectionInterface::PrimitiveType type = 0u;
  emit getActivePrimitiveType(type);

  // Only handle supported primitive types
  if((type & allSupportedTypes_) == 0) {
    // No supported type is active
    return;
  }

  // Decide, if all or only target objects should be handled
  bool targetsOnly = false;
  emit targetObjectsOnly(targetsOnly);
  PluginFunctions::IteratorRestriction restriction =
      (targetsOnly ? PluginFunctions::TARGET_OBJECTS : PluginFunctions::ALL_OBJECTS);

  if(event->modifiers() == Qt::ShiftModifier) {

    if (event->delta() > 0) {
      for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH));
          o_it != PluginFunctions::objectsEnd(); ++o_it) {
        if (o_it->visible()) {

          if(type & vertexType_)
            growVertexSelection(o_it->id());
          if(type & faceType_)
            growFaceSelection(o_it->id());
        }
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
        emit createBackup(o_it->id(), "Grow Selection", UPDATE_SELECTION);
      }
    } else {

      for (PluginFunctions::ObjectIterator o_it(restriction, DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH));
          o_it != PluginFunctions::objectsEnd(); ++o_it) {
        if (o_it->visible()) {

          if(type & vertexType_)
            shrinkVertexSelection(o_it->id());
          if(type & faceType_)
            shrinkFaceSelection(o_it->id());
        }
        emit updatedObject(o_it->id(), UPDATE_SELECTION);
        emit createBackup(o_it->id(), "Shrink Selection", UPDATE_SELECTION);
      }
    }
  }
}

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
void MeshObjectSelectionPlugin::lassoSelect(QRegion&      _region,
                                            PrimitiveType _primitiveType,
                                            bool          _deselection) {

    // <object id, primitive id>
    QList <QPair<unsigned int, unsigned int> > list;
    
    if(_primitiveType & vertexType_) {
        PluginFunctions::scenegraphRegionPick(ACG::SceneGraph::PICK_FRONT_VERTEX, _region, list);
        
        std::set<int> alreadySelectedObjects;
        
        for(int i = 0; i < list.size(); ++i) {
            
            if(alreadySelectedObjects.count(list[i].first) != 0)
                continue;
            
            BaseObjectData* bod = 0;
            PluginFunctions::getPickedObject(list[i].first, bod);
            
            if(bod && (bod->dataType() == DATA_TRIANGLE_MESH || bod->dataType() == DATA_POLY_MESH)) {
                IdList elements;
                for(int j = 0; j < list.size(); ++j) {
                    if(list[j].first == list[i].first) {
                        
                        elements.push_back(list[j].second);
                    }
                }
                selectVertices(bod->id(), elements);
                alreadySelectedObjects.insert(list[i].first);
                emit updatedObject(bod->id(), UPDATE_SELECTION);
                emit  createBackup(bod->id(), "Lasso Selection", UPDATE_SELECTION);
            }
        }
    }
    if(_primitiveType & edgeType_) {
        PluginFunctions::scenegraphRegionPick(ACG::SceneGraph::PICK_FRONT_EDGE, _region, list);
        
        std::set<int> alreadySelectedObjects;
        
        for(int i = 0; i < list.size(); ++i) {
            
            if(alreadySelectedObjects.count(list[i].first) != 0)
                continue;
            
            BaseObjectData* bod = 0;
            PluginFunctions::getPickedObject(list[i].first, bod);
            
            if(bod && (bod->dataType() == DATA_TRIANGLE_MESH || bod->dataType() == DATA_POLY_MESH)) {
                IdList elements;
                for(int j = 0; j < list.size(); ++j) {
                    if(list[j].first == list[i].first) {
                        
                        elements.push_back(list[j].second);
                    }
                }
                selectEdges(bod->id(), elements);
                alreadySelectedObjects.insert(list[i].first);
                emit updatedObject(bod->id(), UPDATE_SELECTION);
                emit  createBackup(bod->id(), "Lasso Selection", UPDATE_SELECTION);
            }
        }
    }
    if(_primitiveType & halfedgeType_) {
        PluginFunctions::scenegraphRegionPick(ACG::SceneGraph::PICK_FRONT_EDGE, _region, list);
        
        std::set<int> alreadySelectedObjects;
        
        for(int i = 0; i < list.size(); ++i) {
            
            if(alreadySelectedObjects.count(list[i].first) != 0)
                continue;
            
            BaseObjectData* bod = 0;
            PluginFunctions::getPickedObject(list[i].first, bod);
            
            if(bod && (bod->dataType() == DATA_TRIANGLE_MESH || bod->dataType() == DATA_POLY_MESH)) {
                IdList elements;
                for(int j = 0; j < list.size(); ++j) {
                    if(list[j].first == list[i].first) {
                        
                        elements.push_back(list[j].second);
                    }
                }
                IdList oldEdgeSelection = getEdgeSelection(bod->id());
                clearEdgeSelection(bod->id());
                selectEdges(bod->id(), elements);
                
                if(bod->dataType() == DATA_TRIANGLE_MESH)
                    MeshSelection::convertEdgeToHalfedgeSelection(PluginFunctions::triMesh(bod));
                else if(bod->dataType() == DATA_POLY_MESH)
                    MeshSelection::convertEdgeToHalfedgeSelection(PluginFunctions::polyMesh(bod));
                
                clearEdgeSelection(bod->id());
                selectEdges(bod->id(), oldEdgeSelection);
                
                alreadySelectedObjects.insert(list[i].first);
                emit updatedObject(bod->id(), UPDATE_SELECTION);
                emit  createBackup(bod->id(), "Lasso Selection", UPDATE_SELECTION);
            }
        }
    }
    if(_primitiveType & faceType_) {
        PluginFunctions::scenegraphRegionPick(ACG::SceneGraph::PICK_FACE, _region, list);
        
        std::set<int> alreadySelectedObjects;
        
        for(int i = 0; i < list.size(); ++i) {
            
            if(alreadySelectedObjects.count(list[i].first) != 0)
                continue;
            
            BaseObjectData* bod = 0;
            PluginFunctions::getPickedObject(list[i].first, bod);
            
            if(bod && (bod->dataType() == DATA_TRIANGLE_MESH || bod->dataType() == DATA_POLY_MESH)) {
                IdList elements;
                for(int j = 0; j < list.size(); ++j) {
                    if(list[j].first == list[i].first) {
                        
                        elements.push_back(list[j].second);
                    }
                }
                selectFaces(bod->id(), elements);
                alreadySelectedObjects.insert(list[i].first);
                emit updatedObject(bod->id(), UPDATE_SELECTION);
                emit  createBackup(bod->id(), "Lasso Selection", UPDATE_SELECTION);
            }
        }
    }
}

/// Traverse the scenegraph and call the selection function for all mesh nodes
bool SelectVolumeAction::operator()(BaseNode* _node) {
    
    BaseObjectData* object = 0;
    if(PluginFunctions::getPickedObject(_node->id(), object)) {
        
        bool selected = false;
        if (object->dataType(DATA_TRIANGLE_MESH)) {

            TriMesh* m = PluginFunctions::triMesh(object);
            selected = plugin_->volumeSelection(m, state_, &region_, type_, deselection_);

        } else if(object->dataType(DATA_POLY_MESH)) {

            PolyMesh* m = PluginFunctions::polyMesh(object);
            selected = plugin_->volumeSelection(m, state_, &region_, type_, deselection_);
        }

        if (selected){
            emit plugin_->updatedObject(object->id(), UPDATE_SELECTION);
            emit plugin_->createBackup( object->id(), "Lasso Selection", UPDATE_SELECTION);
        }
    }
    return true;
}

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

  // get object
  BaseObjectData *obj = 0;
  PluginFunctions::getObject(_objectId, obj);

  if (obj == 0) {
    emit log(LOGERR, tr("Unable to get object"));
    return -1;
  }

  if (obj->dataType(DATA_TRIANGLE_MESH)) {
    TriMesh* mesh = PluginFunctions::triMesh(obj);

    if (mesh == 0) {
      emit log(LOGERR, tr("Unable to get mesh"));
      return -1;
    }

    //add an empty mesh
    int id = -1;
    emit addEmptyObject(DATA_TRIANGLE_MESH, id);

    if (id == -1) {
      emit log(LOGERR, tr("Unable to add empty object"));
      return -1;
    }

    BaseObjectData *newObj;
    PluginFunctions::getObject(id, newObj);

    TriMesh* newMesh = PluginFunctions::triMesh(newObj);

    if (newMesh == 0) {
      emit log(LOGERR, tr("Unable to get mesh"));
      return -1;
    }

    //fill the empty mesh with the selection
    createMeshFromSelection(*mesh, *newMesh,_primitiveType);

    emit updatedObject(id, UPDATE_ALL);

    return id;

  } else if (obj->dataType(DATA_POLY_MESH)) {
    PolyMesh* mesh = PluginFunctions::polyMesh(obj);

    if (mesh == 0) {
      emit log(LOGERR, tr("Unable to get mesh"));
      return -1;
    }

    //add an empty mesh
    int id;
    emit addEmptyObject(DATA_POLY_MESH, id);

    if (id == -1) {
      emit log(LOGERR, tr("Unable to add empty object"));
      return -1;
    }

    BaseObjectData *newObj;
    PluginFunctions::getObject(id, newObj);

    PolyMesh* newMesh = PluginFunctions::polyMesh(newObj);

    if (newMesh == 0) {
      emit log(LOGERR, tr("Unable to get mesh"));
      return -1;
    }

    //fill the empty mesh with the selection
    createMeshFromSelection(*mesh, *newMesh,_primitiveType);

    emit updatedObject(id, UPDATE_ALL);

    return id;
  } else {
    emit log(LOGERR, tr("DataType not supported"));
    return -1;
  }
}


Q_EXPORT_PLUGIN2(meshobjectselectionplugin, MeshObjectSelectionPlugin);