HoleFillerPlugin.cc 20.7 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
 
Jan Möbius committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

#include "HoleFillerPlugin.hh"

#include <MeshTools/MeshSelectionT.hh>

54
#if QT_VERSION >= 0x050000
Jan Möbius's avatar
 
Jan Möbius committed
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
#else
#include <QtGui>
#endif

#include "HoleInfoT.hh"

#define HOLEINFO "HoleInfoData"

/// Constructor
HoleFillerPlugin::HoleFillerPlugin() :
       tool_(0)
{

}

/// Initialize the toolbox widget
void HoleFillerPlugin::initializePlugin()
{
   tool_ = new HoleFillerToolbarWidget();
   QSize size(300, 300);
   tool_->resize(size);

   connect(tool_->tableWidget,SIGNAL(itemSelectionChanged()),this,SLOT(slotItemSelectionChanged()));
   
   connect(tool_->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(slotCellDoubleClicked(int,int)));

   connect(tool_->detect,SIGNAL(clicked()),this,SLOT(detectButton()));
   connect(tool_->fillButton, SIGNAL(clicked()), this, SLOT(slotFillSelection()) );

   QIcon* toolIcon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"holefilling.png");

   emit addToolbox( tr("Hole Filler") , tool_ , toolIcon);
} 

/// add PickModes after initialization
void HoleFillerPlugin::pluginsInitialized()
{
   emit addPickMode("Separator");
   emit addPickMode("Hole Filler");

   emit setSlotDescription("fillAllHoles(int)", tr("Fill all holes from a given Mesh"),
                           QString("objectId").split(","), QString("Id of the mesh").split(","));

   emit setSlotDescription("fillHole(int,int)", tr("Fill a holes from a given Mesh where edgeHandle is on the boundary"),
                           QString("objectId,edgeHandle").split(","), QString("Id of the mesh,Handle of one boundary edge of the hole").split(","));

   //build the tableWidget with headers
   update_menu();
}

Matthias Möller's avatar
Matthias Möller committed
105 106 107
void HoleFillerPlugin::getSelectedHoles(std::vector<int>& _holeIds, std::vector<int>& _objIds)
{
  QModelIndexList indices = tool_->tableWidget->selectionModel()->selectedRows();
Jan Möbius's avatar
 
Jan Möbius committed
108

Matthias Möller's avatar
Matthias Möller committed
109 110 111 112
  //get a map from objectID to (selected) holeIDs
  for (int i=0; i < indices.size(); i++){
    int objID  = holeMapping_[ indices[i].row() ].first;
    int holeID = holeMapping_[ indices[i].row() ].second;
Jan Möbius's avatar
 
Jan Möbius committed
113

Matthias Möller's avatar
Matthias Möller committed
114 115 116 117
    _holeIds.push_back  ( holeID );
    _objIds.push_back( objID );
  }
}
Jan Möbius's avatar
 
Jan Möbius committed
118

Matthias Möller's avatar
Matthias Möller committed
119 120
/// Fill all selected holes
void HoleFillerPlugin::slotFillSelection(){
Jan Möbius's avatar
 
Jan Möbius committed
121 122 123 124

  //get a map from objectID to (selected) holeIDs
  std::vector< int > holes;
  std::vector< int > objects;
Matthias Möller's avatar
Matthias Möller committed
125
  getSelectedHoles(holes,objects);
Jan Möbius's avatar
 
Jan Möbius committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

  //init progressDialog
  QProgressDialog progress(tr("Filling holes..."), tr("Abort"), 0, holes.size(), 0);
  progress.setWindowModality(Qt::ApplicationModal);
  progress.setValue(0);

  int counter = 0;

  //iterate over all objects with holes that should be filled
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {

    // TYPE is TRIMESH
    if ( o_it->dataType( DATA_TRIANGLE_MESH ) ) {

      TriMesh* mesh = PluginFunctions::triMesh(o_it);

      //get perObjectData
      HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( o_it->objectData(HOLEINFO) );
    
      if (holeInfo == 0){
        holeInfo = new HoleInfo< TriMesh >( mesh );
        o_it->setObjectData(HOLEINFO, holeInfo);
      }

      //fill the holes
      for (uint i = 0; i < objects.size(); i++)
        if ( objects[i] == o_it->id() ){
          holeInfo->fillHole( holes[i]);

          if (progress.wasCanceled())
            break;
          else
            progress.setValue(++counter);
        }

      //update the object
162
      emit updatedObject(o_it->id(),UPDATE_ALL);
Jan Möbius's avatar
 
Jan Möbius committed
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
    
      holeInfo->getHoles();
    
      update_menu();
      emit createBackup( o_it->id(), "Hole Filling", UPDATE_GEOMETRY | UPDATE_TOPOLOGY | UPDATE_SELECTION);
    }
    // DATATYPE is  POLYMESH
    else if ( o_it->dataType( DATA_POLY_MESH ) ) {
      emit log(LOGWARN, tr("HoleFilling unsupported for poly meshes") );
      continue;
    }
    
    //abort if user wants to
    if (progress.wasCanceled())
      break;
  }

  progress.close();

  emit updateView();
}

/// slot for displaying selected holes
void HoleFillerPlugin::slotItemSelectionChanged() {

  std::vector< int > holes;
  std::vector< int > objects;
Matthias Möller's avatar
Matthias Möller committed
190
  getSelectedHoles(holes,objects);
Jan Möbius's avatar
 
Jan Möbius committed
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

  //iterate over all objects with holes that should be displayed
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {

    // TYPE is TRIMESH
    if ( o_it->dataType( DATA_TRIANGLE_MESH ) ) {

      TriMesh* mesh = PluginFunctions::triMesh(o_it);

      //get perObjectData
      HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( o_it->objectData(HOLEINFO) );
    
      if (holeInfo == 0){
        holeInfo = new HoleInfo< TriMesh >( mesh );
        o_it->setObjectData(HOLEINFO, holeInfo);
      }

      //clear the edge selection
      MeshSelection::clearEdgeSelection(mesh);

      //select the holes
      for (uint i = 0; i < objects.size(); i++)
        if ( objects[i] == o_it->id() )
          holeInfo->selectHole( holes[i] );

      //update the object
217
      emit updatedObject(o_it->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    }
    // DATATYPE is  POLYMESH
    else if ( o_it->dataType( DATA_POLY_MESH ) ) {

      PolyMesh* mesh = PluginFunctions::polyMesh(o_it);

      //get perObjectData
      HoleInfo< PolyMesh >* holeInfo = dynamic_cast< HoleInfo< PolyMesh >* > ( o_it->objectData(HOLEINFO) );
    
      if (holeInfo == 0){
        holeInfo = new HoleInfo< PolyMesh >( mesh );
        o_it->setObjectData(HOLEINFO, holeInfo);
      }

      //clear the edge selection
      MeshSelection::clearEdgeSelection(mesh);

      //select the holes
      for (uint i = 0; i < objects.size(); i++)
        if ( objects[i] == o_it->id() )
          holeInfo->selectHole( holes[i] );

      //update the object
241
      emit updatedObject(o_it->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
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

    }
  }

  emit updateView();
}

/// Slot for filling holes from double-clicked rows
void HoleFillerPlugin::slotCellDoubleClicked(int _row , int /*_col*/) {

  if ( _row > (int)holeMapping_.size() ) {
    emit log(LOGWARN, tr("Error for holeMapping_ vector size") );
    return;
  }

  BaseObjectData* object;
  int objID  = holeMapping_[_row].first;
  int holeID = holeMapping_[_row].second;

  if ( !PluginFunctions::getObject( objID, object ) ) {
    emit log(LOGWARN, tr("Unable to find object for hole (should not happen!!)") );
    return;
  }

  // TYPE is TRIMESH
  if ( object->dataType( DATA_TRIANGLE_MESH ) ) {

    TriMesh* mesh = PluginFunctions::triMesh(object);

    //get perObjectData
    HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( object->objectData(HOLEINFO) );
  
    if (holeInfo == 0){
      holeInfo = new HoleInfo< TriMesh >( mesh );
      object->setObjectData(HOLEINFO, holeInfo);
    }

    //fill the hole
    holeInfo->fillHole( holeID );

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

    holeInfo->getHoles();

    update_menu();

    emit updateView();
  }
  // DATATYPE is  POLYMESH
  else if ( object->dataType( DATA_POLY_MESH ) ) {
    
//     PolyMesh* mesh = PluginFunctions::polyMesh(object);
//     
//     HoleInfo< PolyMesh >* holeInfo = dynamic_cast< HoleInfo< PolyMesh >* > ( object->objectData(HOLEINFO) );
//     
//     if (holeInfo == 0){
//       holeInfo = new HoleInfo< PolyMesh >( mesh );
//       object->setObjectData(HOLEINFO, holeInfo);
//     }
//     
//     holeInfo->getHoles();    
    
    emit log(LOGWARN,tr("HoleFilling unsupported for poly meshes"));
    return;
  }
}

/// detect holes on all objects
void HoleFillerPlugin::detectButton( )
{
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {

    //case TRIMESH
    if ( o_it->dataType( DATA_TRIANGLE_MESH ) ) {

      HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( o_it->objectData(HOLEINFO) );
    
      if ( holeInfo == 0 ){
        TriMesh* mesh = PluginFunctions::triMesh(*o_it);
        holeInfo = new HoleInfo< TriMesh >( mesh );
        o_it->setObjectData(HOLEINFO, holeInfo);
      }

      holeInfo->getHoles();
    }

//     //case POLYMESH
//     if ( o_it->dataType( DATA_POLY_MESH ) ) {
// 
//       HoleInfo< PolyMesh >* holeInfo = dynamic_cast< HoleInfo< PolyMesh >* > ( o_it->objectData(HOLEINFO) );
//     
//       if (holeInfo == 0){
//         PolyMesh* mesh = PluginFunctions::polyMesh(*o_it);
//         holeInfo = new HoleInfo< PolyMesh >( mesh );
//         o_it->setObjectData(HOLEINFO, holeInfo);
//       }
// 
//       holeInfo->getHoles();
//     }
  }

  update_menu();
}

/// check for holes if an object has changed
347
void HoleFillerPlugin::slotObjectUpdated( int _identifier, const UpdateType& _type ) {
Jan Möbius's avatar
 
Jan Möbius committed
348 349 350 351 352 353 354 355 356

  BaseObjectData* object;

  // Check if this is a usable Object
  if ( !PluginFunctions::getObject(_identifier,object) ) 
    return;

  bool updated = false;
  
Jan Möbius's avatar
Jan Möbius committed
357
  if ( _type.contains(UPDATE_TOPOLOGY) ) {
Jan Möbius's avatar
 
Jan Möbius committed
358

Jan Möbius's avatar
Jan Möbius committed
359 360 361 362 363 364 365 366 367
    // get holes for TRIMESH
    if ( object->dataType( DATA_TRIANGLE_MESH ) ) {

      HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( object->objectData(HOLEINFO) );

      if ( holeInfo ) {
        holeInfo->getHoles();
        updated = true;
      }
Jan Möbius's avatar
 
Jan Möbius committed
368 369
    }

Jan Möbius's avatar
Jan Möbius committed
370 371
    // get holes for POLYMESH
    else if ( object->dataType( DATA_POLY_MESH ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
372

Jan Möbius's avatar
Jan Möbius committed
373 374 375 376 377 378
      HoleInfo< PolyMesh >* holeInfo = dynamic_cast< HoleInfo< PolyMesh >* > ( object->objectData(HOLEINFO) );

      if ( holeInfo ) {
        holeInfo->getHoles();
        updated = true;
      }
Jan Möbius's avatar
 
Jan Möbius committed
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
    }
  }

  // Only update if something has changed!
  if ( updated )
    update_menu();
}

///update the entries in the tableWidget
void HoleFillerPlugin::update_menu() {

  holeMapping_.clear();

  tool_->tableWidget->clear();

  tool_->tableWidget->setRowCount ( 0 );
  tool_->tableWidget->setColumnCount ( 4 );

  QStringList headerdata;
  headerdata << "Object" << "Edges" << "Boundary Length" << "BB Diagonal";

  tool_->tableWidget->setHorizontalHeaderLabels(headerdata);
  tool_->updateGeometry();
  
  int elements = 0;
  int count = 0;

  //iterate over all objects
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {

    // DATATYPE is  TRIMESH
    if ( o_it->dataType( DATA_TRIANGLE_MESH ) ) {

      //get perObjectData  
      HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( o_it->objectData(HOLEINFO) );

      if (holeInfo != 0){
        elements += holeInfo->holes()->size();
        tool_->tableWidget->setRowCount ( elements );

        //add holes to the table
        for (uint i = 0 ; i < holeInfo->holes()->size() ; ++i ) {
          // Set Name of the object
          QTableWidgetItem* name = new QTableWidgetItem( o_it->name() );
          name->setFlags( 0 );
          name->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,0,name);
    
          // Set Number of the edges
          QTableWidgetItem* size = new QTableWidgetItem( QString::number( (*holeInfo->holes())[i].size() ) );
          size->setFlags( 0 );
          size->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,1,size);
    
          // Set boundary Length
          std::vector< TriMesh::EdgeHandle >::iterator endIter = (*holeInfo->holes())[i].end();
          double boundaryLength = 0.0;
          TriMesh* mesh = 0;
          PluginFunctions::getMesh(o_it->id(),mesh);
          for (std::vector< TriMesh::EdgeHandle >::iterator edgeIter = (*holeInfo->holes())[i].begin(); edgeIter != endIter; ++edgeIter)
            boundaryLength += mesh->calc_edge_length(*edgeIter);
          QTableWidgetItem* boundaryLengthWidget = new QTableWidgetItem( QString::number(boundaryLength) );
          boundaryLengthWidget->setFlags( 0 );
          boundaryLengthWidget->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,2,boundaryLengthWidget);

          //compute bounding box
          TriMesh::Point minCoord = TriMesh::Point(std::numeric_limits<TriMesh::Scalar>::max(),std::numeric_limits<TriMesh::Scalar>::max(),std::numeric_limits<TriMesh::Scalar>::max());
          TriMesh::Point maxCoord = TriMesh::Point(-std::numeric_limits<TriMesh::Scalar>::max(),-std::numeric_limits<TriMesh::Scalar>::max(),-std::numeric_limits<TriMesh::Scalar>::max());
          for (std::vector< TriMesh::EdgeHandle >::iterator edgeIter = (*holeInfo->holes())[i].begin(); edgeIter != endIter; ++edgeIter)
          {
            TriMesh::Point pos = mesh->point(mesh->from_vertex_handle(mesh->halfedge_handle(*edgeIter,0)));
            minCoord[0] = std::min(minCoord[0],pos[0]);
            minCoord[1] = std::min(minCoord[1],pos[1]);
            minCoord[2] = std::min(minCoord[2],pos[2]);

            maxCoord[0] = std::max(maxCoord[0],pos[0]);
            maxCoord[1] = std::max(maxCoord[1],pos[1]);
            maxCoord[2] = std::max(maxCoord[2],pos[2]);
          }

          TriMesh::Scalar bbDiagonal = (minCoord-maxCoord).length();

          QTableWidgetItem* bbDiagonalWidget = new QTableWidgetItem( QString::number(bbDiagonal) );
          bbDiagonalWidget->setFlags( 0 );
          bbDiagonalWidget->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,3,bbDiagonalWidget);
    
          // remember the id for the hole
          holeMapping_.push_back( std::pair<int , int>( o_it->id() , i ) );
    
          ++count;
        }
      }
    }
    // DATATYPE is  POLYMESH
    else if ( o_it->dataType( DATA_POLY_MESH ) ) {

      //get perObjectData  
      HoleInfo< PolyMesh >* holeInfo = dynamic_cast< HoleInfo< PolyMesh >* > ( o_it->objectData(HOLEINFO) );

      if (holeInfo != 0){
        elements += holeInfo->holes()->size();
        tool_->tableWidget->setRowCount ( elements );

        //add holes to the table
        for (uint i = 0 ; i < holeInfo->holes()->size() ; ++i ) {
          // Set Name of the object
          QTableWidgetItem* name = new QTableWidgetItem( o_it->name() );
          name->setFlags( 0 );
          name->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,0,name);
    
          // Set Number of the edges
          QTableWidgetItem* size = new QTableWidgetItem( QString::number( (*holeInfo->holes())[i].size() ) );
          size->setFlags( 0 );
          size->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,1,size);
    
          // Set radius
          QTableWidgetItem* radius = new QTableWidgetItem( "TODO" );
          radius->setFlags( 0 );
          radius->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled);
          tool_->tableWidget->setItem(count,2,radius);
    
          // remember the id for the hole
          holeMapping_.push_back( std::pair<int , int>( o_it->id() , i ) );
    
          ++count;
        }
      }
    }
  }

  tool_->tableWidget->resizeColumnToContents ( 1 );
}

/// fill all holes from a given object
void HoleFillerPlugin::fillAllHoles(int _objectID){
  BaseObjectData* object = 0;
  PluginFunctions::getObject( _objectID , object );

  if (object == 0){
    emit log(LOGERR, tr("Could not get object from ID.") );
    return;
  }
  
  emit scriptInfo( "fillAllHoles( ObjectId )"  );


  // TYPE is TRIMESH
  if ( object->dataType( DATA_TRIANGLE_MESH ) ) {

    TriMesh* mesh = PluginFunctions::triMesh(object);

    //get perObjectData
    HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( object->objectData(HOLEINFO) );
  
    if (holeInfo == 0){
      holeInfo = new HoleInfo< TriMesh >( mesh );
      object->setObjectData(HOLEINFO, holeInfo);
    }

    //fill the hole
    holeInfo->fillAllHoles();

    //and update everything
    emit updatedObject(object->id(),UPDATE_ALL);

    holeInfo->getHoles();

    update_menu();

    emit updateView();
  }
  // DATATYPE is  POLYMESH
  else if ( object->dataType( DATA_POLY_MESH ) ) {
    emit log(LOGERR, tr("HoleFilling unsopported for poly meshes.") );
    return;
  }
}


/// fill a hole in given object where _edgeHandle is on the boundary
void HoleFillerPlugin::fillHole(int _objectID, int _edgeHandle){
  BaseObjectData* object = 0;
  PluginFunctions::getObject( _objectID , object );

  if (object == 0){
    emit log(LOGERR, tr("Could not get object from ID.") );
    return;
  }
  
  emit scriptInfo( "fillHole( ObjectId , EdgeHandle )"  );


  // TYPE is TRIMESH
  if ( object->dataType( DATA_TRIANGLE_MESH ) ) {

    TriMesh* mesh = PluginFunctions::triMesh(object);

    //get perObjectData
    HoleInfo< TriMesh >* holeInfo = dynamic_cast< HoleInfo< TriMesh >* > ( object->objectData(HOLEINFO) );
  
    if (holeInfo == 0){
      holeInfo = new HoleInfo< TriMesh >( mesh );
      object->setObjectData(HOLEINFO, holeInfo);
    }

    //check edgeHandle
    TriMesh::EdgeHandle eh(_edgeHandle);

    if ( !eh.is_valid() || !mesh->is_boundary(eh) ){
      emit log(LOGERR, tr("Invalid edge handle.") );
      return;
    }

    //fill the hole
    holeInfo->fillHole( eh );

    //and update everything
    emit updatedObject(object->id(),UPDATE_ALL);

    holeInfo->getHoles();

    update_menu();

    emit updateView();
  }
  // DATATYPE is  POLYMESH
  else if ( object->dataType( DATA_POLY_MESH ) ) {
    emit log(LOGERR, tr("HoleFilling unsopported for poly meshes.") );
    return;
  }
}

#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2( holefillerplugin , HoleFillerPlugin );
#endif