TopologyPlugin.cc 34.3 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36 37 38 39 40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46 47 48 49

#include <QtGui>
#include "TopologyPlugin.hh"
#include <iostream>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

#include <ACG/Geometry/Algorithms.hh>

Dirk Wilden's avatar
Dirk Wilden committed
50 51 52 53 54 55
#define EDGE_FLIP_POPUP "<B>Flip Edge</B><br>Rotate an edge"
#define EDGE_COLLAPSE_POPUP "<B>Collapse Edge</B><br>Collapse an edge into one of its vertices."
#define EDGE_SPLIT_POPUP "<B>Split Edge</B><br>Split an edge at the clicked point."
#define FACE_ADD_POPUP "<B>Add Face</B><br>Insert a face between clicked vertices."
#define FACE_SPLIT_POPUP "<B>Split Face</B><br>Split a face at a clicked point."
#define FACE_DELETE_POPUP "<B>Delete Face</B><br>Remove a clicked face."
Jan Möbius's avatar
 
Jan Möbius committed
56 57

#include <OpenMesh/Core/System/omstream.hh>
58
#include <cfloat>
Jan Möbius's avatar
 
Jan Möbius committed
59

Jan Möbius's avatar
Jan Möbius committed
60 61 62 63 64 65 66 67 68 69 70 71 72
//******************************************************************************

TopologyPlugin::TopologyPlugin() :
        toolbar_(0),
        edgeFlipAction_(0),
        edgeSplitAction_(0),
        edgeCollapseAction_(0),
        faceAddAction_(0),
        faceDeleteAction_(0),
        faceSplitAction_(0)
{

}
Dirk Wilden's avatar
Dirk Wilden committed
73 74 75 76

//******************************************************************************

/** \brief initialize the Plugin
77
 *
Dirk Wilden's avatar
Dirk Wilden committed
78
 */
Jan Möbius's avatar
 
Jan Möbius committed
79 80 81 82 83 84 85 86
void TopologyPlugin::pluginsInitialized() {
   emit addHiddenPickMode(EDGE_FLIP_POPUP);
   emit addHiddenPickMode(EDGE_SPLIT_POPUP);
   emit addHiddenPickMode(EDGE_COLLAPSE_POPUP);
   emit addHiddenPickMode(FACE_ADD_POPUP);
   emit addHiddenPickMode(FACE_SPLIT_POPUP);
   emit addHiddenPickMode(FACE_DELETE_POPUP);

Dirk Wilden's avatar
Dirk Wilden committed
87 88 89

  toolbar_ = new QToolBar("Topology");

Jan Möbius's avatar
 
Jan Möbius committed
90
  QActionGroup* group = new QActionGroup(0);
91 92 93

  QString iconPath = OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator();

94 95 96 97
  const QString baseHelpURL = "<a href='qthelp://org.openflipper.plugin-topology/Plugin-Topology/index.html";
  const QString clickText = tr("Click for more information</a>");


98 99 100
  edgeFlipAction_ = toolbar_->addAction( QIcon(iconPath + "topology-edgeFlip.png"), EDGE_FLIP_POPUP );
  edgeFlipAction_->setCheckable( true);
  edgeFlipAction_->setActionGroup(group);
101 102
  edgeFlipAction_->setWhatsThis(tr("Flip edge. ") + baseHelpURL+ "#flip_edge'>" + clickText);

103 104 105
  edgeSplitAction_ = toolbar_->addAction( QIcon(iconPath + "topology-edgeSplit.png"), EDGE_SPLIT_POPUP );
  edgeSplitAction_->setCheckable( true);
  edgeSplitAction_->setActionGroup(group);
106 107 108
  edgeSplitAction_->setWhatsThis(tr("Split edge. ") + baseHelpURL+ "#split_edge'>" + clickText);


109 110 111
  edgeCollapseAction_ = toolbar_->addAction( QIcon(iconPath + "topology-edgeCollapse.png"), EDGE_COLLAPSE_POPUP );
  edgeCollapseAction_->setCheckable( true);
  edgeCollapseAction_->setActionGroup(group);
112 113 114
  edgeCollapseAction_->setWhatsThis(tr("Collapse edge. ") + baseHelpURL+ "#collapse_edge'>" + clickText);


Dirk Wilden's avatar
Dirk Wilden committed
115
  toolbar_->addSeparator();
116 117 118
  faceAddAction_ = toolbar_->addAction( QIcon(iconPath + "topology-addFace.png"), FACE_ADD_POPUP );
  faceAddAction_->setCheckable( true);
  faceAddAction_->setActionGroup(group);
119 120 121
  faceAddAction_->setWhatsThis(tr("Add face.") + baseHelpURL+ "#add_face'>" + clickText);


122 123 124
  faceDeleteAction_ = toolbar_->addAction( QIcon(iconPath + "topology-deleteFace.png"), FACE_DELETE_POPUP );
  faceDeleteAction_->setCheckable( true);
  faceDeleteAction_->setActionGroup(group);
125 126 127
  faceDeleteAction_->setWhatsThis(tr("Delete face. ") + baseHelpURL+ "#delete_face'>" + clickText);


128 129 130
  faceSplitAction_ = toolbar_->addAction( QIcon(iconPath + "topology-splitFace.png"), FACE_SPLIT_POPUP );
  faceSplitAction_->setCheckable( true);
  faceSplitAction_->setActionGroup(group);
131
  faceSplitAction_->setWhatsThis(tr("Split face. ") + baseHelpURL+ "#split_face'>" + clickText);
Jan Möbius's avatar
 
Jan Möbius committed
132 133 134

  group->setExclusive(true);

Dirk Wilden's avatar
Dirk Wilden committed
135
  connect( toolbar_,  SIGNAL( actionTriggered(QAction*) ), this, SLOT( toolBarTriggered(QAction*) ));
Jan Möbius's avatar
 
Jan Möbius committed
136

Dirk Wilden's avatar
Dirk Wilden committed
137 138
  emit addToolbar( toolbar_ );
}
Jan Möbius's avatar
 
Jan Möbius committed
139 140


Dirk Wilden's avatar
Dirk Wilden committed
141
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
142 143


Dirk Wilden's avatar
Dirk Wilden committed
144
/** \brief Toolbar action was triggered
145
 *
Dirk Wilden's avatar
Dirk Wilden committed
146 147 148
 * @param _action the action that was triggered
 */
void TopologyPlugin::toolBarTriggered(QAction* _action){
Jan Möbius's avatar
 
Jan Möbius committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  if ( _action->text() == EDGE_FLIP_POPUP)
    PluginFunctions::pickMode(EDGE_FLIP_POPUP);
  else if ( _action->text() == EDGE_SPLIT_POPUP)
    PluginFunctions::pickMode(EDGE_SPLIT_POPUP);
  else if ( _action->text() == EDGE_COLLAPSE_POPUP)
    PluginFunctions::pickMode(EDGE_COLLAPSE_POPUP);
  else if ( _action->text() == FACE_ADD_POPUP)
    PluginFunctions::pickMode(FACE_ADD_POPUP);
  else if ( _action->text() == FACE_SPLIT_POPUP)
    PluginFunctions::pickMode(FACE_SPLIT_POPUP);
  else if ( _action->text() == FACE_DELETE_POPUP)
    PluginFunctions::pickMode(FACE_DELETE_POPUP);

  PluginFunctions::actionMode(Viewer::PickingMode);
}


166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
//******************************************************************************

/** \brief Toggle actions when the PickMode changes
 *
 * @param _mode the new PickMode
 */
void TopologyPlugin::slotPickModeChanged( const std::string& _mode) {

  edgeFlipAction_->setChecked(     _mode == EDGE_FLIP_POPUP );
  edgeSplitAction_->setChecked(    _mode == EDGE_SPLIT_POPUP );
  edgeCollapseAction_->setChecked( _mode == EDGE_COLLAPSE_POPUP );
  faceAddAction_->setChecked(      _mode == FACE_ADD_POPUP );
  faceDeleteAction_->setChecked(   _mode == FACE_DELETE_POPUP );
  faceSplitAction_->setChecked(    _mode == FACE_SPLIT_POPUP );
}

Dirk Wilden's avatar
Dirk Wilden committed
182
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
183

Dirk Wilden's avatar
Dirk Wilden committed
184
/** \brief this is called when a mouse event occurred
185
 *
Dirk Wilden's avatar
Dirk Wilden committed
186 187
 * @param _event the event that occurred
 */
Jan Möbius's avatar
 
Jan Möbius committed
188 189 190 191 192 193 194 195 196 197 198 199
void TopologyPlugin::slotMouseEvent( QMouseEvent* _event ) {
    if ( _event->buttons() == Qt::RightButton )
      return;

   if ( PluginFunctions::pickMode() == EDGE_FLIP_POPUP ) { flip_edge(_event); } else
   if ( PluginFunctions::pickMode() == EDGE_COLLAPSE_POPUP ) { collapse_edge(_event); } else
   if ( PluginFunctions::pickMode() == EDGE_SPLIT_POPUP ) { split_edge(_event); } else
   if ( PluginFunctions::pickMode() == FACE_ADD_POPUP ) { add_face(_event); } else
   if ( PluginFunctions::pickMode() == FACE_SPLIT_POPUP ) { split_face(_event); } else
   if ( PluginFunctions::pickMode() == FACE_DELETE_POPUP ) { delete_face(_event); }
}

Dirk Wilden's avatar
Dirk Wilden committed
200 201 202 203

//******************************************************************************

/** \brief Deselect the vertices from AddFace mode
204
 *
Dirk Wilden's avatar
Dirk Wilden committed
205
 */
Jan Möbius's avatar
 
Jan Möbius committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void TopologyPlugin::clearAddFaceVertices() {
  for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
    BaseObjectData* object;
    if ( ! PluginFunctions::getObject( addFaceVertices_[i].first , object ) )
      continue;

    if ( object->dataType(DATA_TRIANGLE_MESH) ) {
      TriMesh* m = PluginFunctions::triMesh(object);
      TriMesh::VertexHandle vh = m->vertex_handle( addFaceVertices_[i].second );
      if ( vh.is_valid()) {
        m->status(vh).set_selected(false);
      }
    }

    if ( object->dataType(DATA_POLY_MESH) ) {
      PolyMesh* m = PluginFunctions::polyMesh(object);
      PolyMesh::VertexHandle vh = m->vertex_handle( addFaceVertices_[i].second );
      if ( vh.is_valid()) {
        m->status(vh).set_selected(false);
      }
    }
Jan Möbius's avatar
Jan Möbius committed
227 228
    
    emit updatedObject(object->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
229 230 231 232 233 234
  }

  addFaceVertices_.clear();
  emit updateView();
}

Dirk Wilden's avatar
Dirk Wilden committed
235 236 237 238

//******************************************************************************

/** \brief Add a face
239
 *
Dirk Wilden's avatar
Dirk Wilden committed
240 241
 * @param _event mouse position where one of the vertices is picked
 */
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
void TopologyPlugin::add_face(QMouseEvent* _event) {
  if (( _event->type() != QEvent::MouseButtonPress) && (_event->type() != QEvent::MouseButtonDblClick))
    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;
      if ( PluginFunctions::getPickedObject(node_idx, object) ) {

         //--- Add Face for TriMesh
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH) ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);

            TriMesh::FaceVertexIter fv_it(m,fh);
            TriMesh::VertexHandle closest = fv_it.handle();
Jan Möbius's avatar
Jan Möbius committed
261
            float shortest_distance = (m.point(closest) - hit_point).sqrnorm();
Jan Möbius's avatar
 
Jan Möbius committed
262 263 264

            ++fv_it;
            if ( (m.point(fv_it.handle() ) - hit_point).sqrnorm() < shortest_distance ) {
Jan Möbius's avatar
Jan Möbius committed
265
              // shortest_distance = (m.point(fv_it.handle() ) - hit_point).sqrnorm();
Jan Möbius's avatar
 
Jan Möbius committed
266 267 268 269 270
               closest = fv_it.handle();
            }

            ++fv_it;
            if ( (m.point(fv_it.handle() ) - hit_point).sqrnorm() < shortest_distance ) {
Jan Möbius's avatar
Jan Möbius committed
271
               // shortest_distance = (m.point(fv_it.handle() ) - hit_point).sqrnorm();
Jan Möbius's avatar
 
Jan Möbius committed
272 273 274 275 276 277 278 279 280 281 282
               closest = fv_it.handle();
            }


            std::pair<int,int> newVertex(object->id(), closest.idx() );

            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
              if ( ( addFaceVertices_[i].first  == newVertex.first  ) &&
                   ( addFaceVertices_[i].second == newVertex.second ) ) {
                addFaceVertices_.erase(addFaceVertices_.begin()+i);
                m.status(closest).set_selected(false);
283
                emit updatedObject(object->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
284 285 286 287 288 289 290 291 292 293 294
                emit updateView();
                return;
              }
            }

            // New Vertex so add it to the list
            addFaceVertices_.push_back( std::pair<int,int>(object->id(), closest.idx() ) );
            m.status(closest).set_selected(true);

            // We need 3 in the list to proceed
            if ( addFaceVertices_.size() < 3 ) {
295
              emit updatedObject(object->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
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
              emit updateView();
              return;
            }

            // check if the objects are of same type
            DataType dt = object->dataType();
            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
              BaseObjectData* tmpObject;
              if ( ! PluginFunctions::getObject( addFaceVertices_[i].first , tmpObject ) ) {
                emit log(LOGERR,"Unable to get object for adding face");
                clearAddFaceVertices();
                return;
              }

              if ( tmpObject->dataType() != dt ) {
                emit log(LOGERR,"Adding faces between different type of meshes is not supported!");
                clearAddFaceVertices();
                return;
              }
            }

            // check if we add a face between multiple objects
            ///@todo: Support adding faces between objects ( and merging them into one )
            int objectId = addFaceVertices_[0].first;
            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
              if ( addFaceVertices_[i].first != objectId ) {
                 emit log(LOGERR,"Adding faces between different objects!");
                 clearAddFaceVertices();
                 return;
              }
            }

            TriMesh::VertexHandle vh0 = m.vertex_handle( addFaceVertices_[0].second );
            TriMesh::VertexHandle vh1 = m.vertex_handle( addFaceVertices_[1].second );
            TriMesh::VertexHandle vh2 = m.vertex_handle( addFaceVertices_[2].second );

            // store state and disable output
            bool errlog = omerr().is_enabled();
            omerr().disable();

            fh = m.add_face(vh0,vh1,vh2);
            if ( !fh.is_valid() ) {
              fh = m.add_face(vh2,vh1,vh0);
            }
340 341 342
            
            emit updatedObject(object->id(),UPDATE_ALL);
            emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
343 344 345 346 347 348 349 350

            // reenable output if it was enabled
            if (errlog)
              omerr().enable();

            clearAddFaceVertices();

            if ( fh.is_valid() )
Dirk Wilden's avatar
Dirk Wilden committed
351
              emit createBackup(object->id(),"Add Face", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
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
            else
              emit log(LOGERR,"Unable to add face!");

         }

        //--- Add Face for PolyMesh
         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH) ) {

            PolyMesh& m = *PluginFunctions::polyMesh(object);
            PolyMesh::FaceHandle fh = m.face_handle(target_idx);

            //find the closest vertex in the picked face
            PolyMesh::VertexHandle closest;
            float shortest_distance = FLT_MAX;

            for (PolyMesh::FaceVertexIter fv_it(m,fh); fv_it; ++fv_it){
              float distance = (m.point( fv_it.handle() ) - hit_point).sqrnorm();

              if (distance < shortest_distance){
                shortest_distance = distance;
                closest = fv_it.handle();
              }
            }

            if (!closest.is_valid())
              return;

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

              std::pair<int,int> newVertex(object->id(), closest.idx() );

              for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
                if ( ( addFaceVertices_[i].first  == newVertex.first  ) &&
                      ( addFaceVertices_[i].second == newVertex.second ) ) {
                  addFaceVertices_.erase(addFaceVertices_.begin()+i);
                  m.status(closest).set_selected(false);
388
                  emit updatedObject(object->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
389 390 391 392 393 394 395 396 397 398 399 400
                  emit updateView();
                  return;
                }
              }

              // New Vertex so add it to the list
              addFaceVertices_.push_back( std::pair<int,int>(object->id(), closest.idx() ) );
              m.status(closest).set_selected(true);
            }

            // We need at least 3 in the list to proceed
            if ( (addFaceVertices_.size() < 3) || (_event->type() != QEvent::MouseButtonDblClick) ) {
401
              emit updatedObject(object->id(),UPDATE_SELECTION);
Jan Möbius's avatar
 
Jan Möbius committed
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
              emit updateView();
              return;
            }

            // check if the objects are of same type
            DataType dt = object->dataType();
            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
              BaseObjectData* tmpObject;
              if ( ! PluginFunctions::getObject( addFaceVertices_[i].first , tmpObject ) ) {
                emit log(LOGERR,"Unable to get object for adding face");
                clearAddFaceVertices();
                return;
              }

              if ( tmpObject->dataType() != dt ) {
                emit log(LOGERR,"Adding faces between different type of meshes is not supported!");
                clearAddFaceVertices();
                return;
              }
            }

            // check if we add a face between multiple objects
            ///@todo: Support adding faces between objects ( and merging them into one )
            int objectId = addFaceVertices_[0].first;
            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i ) {
              if ( addFaceVertices_[i].first != objectId ) {
                  emit log(LOGERR,"Adding faces between different objects!");
                  clearAddFaceVertices();
                  return;
              }
            }

            std::vector< PolyMesh::VertexHandle > vhs;
            for ( uint i = 0 ; i < addFaceVertices_.size() ; ++i )
              vhs.push_back( m.vertex_handle( addFaceVertices_[i].second ) );

            // store state and disable output
            bool errlog = omerr().is_enabled();
            omerr().disable();

            fh = m.add_face(vhs);

            if (!fh.is_valid()){
              std::vector< PolyMesh::VertexHandle > rvhs;
              //reverse vector
              while (!vhs.empty()){
                rvhs.push_back( vhs.back() );
                vhs.pop_back();
              }
451
              
Jan Möbius's avatar
 
Jan Möbius committed
452
              fh = m.add_face(rvhs);
453
              
Jan Möbius's avatar
 
Jan Möbius committed
454
            }
455 456 457
            
            emit updatedObject(object->id(),UPDATE_ALL);
            emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
458 459 460 461 462 463 464 465

            // reenable output if it was enabled
            if (errlog)
              omerr().enable();

            clearAddFaceVertices();

            if ( fh.is_valid() )
Dirk Wilden's avatar
Dirk Wilden committed
466
              emit createBackup(object->id(),"Add Face", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
467 468 469 470 471 472 473 474
            else
              emit log(LOGERR,"Unable to add face!");

         }
      }
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
475 476 477 478

//******************************************************************************

/** \brief Split a face at the given point
479
 *
Dirk Wilden's avatar
Dirk Wilden committed
480 481
 * @param _event mouse position where the face is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
void TopologyPlugin::split_face(QMouseEvent* _event) {
   if ( _event->type() != QEvent::MouseButtonPress )
      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;

      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH) ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);

            emit( log(LOGOUT,"Picked Face " + QString::number(fh.idx()) + ", normal (" +
                                              QString::number(m.normal(fh)[0]) + "," +
                                              QString::number(m.normal(fh)[1]) + "," +
                                              QString::number(m.normal(fh)[2]) + ") ") );

            TriMesh::VertexHandle vh = m.add_vertex(hit_point);
            m.split(fh,vh);
David Bommes's avatar
David Bommes committed
504
            m.garbage_collection();
Jan Möbius's avatar
 
Jan Möbius committed
505 506
            m.update_normals();

507
            emit updatedObject(object->id(), UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
508
            emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
509
            emit createBackup(object->id(),"Split Face", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
510 511 512
         }

         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH)  ) {
David Bommes's avatar
David Bommes committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
           PolyMesh& m = *PluginFunctions::polyMesh(object);
           PolyMesh::FaceHandle fh = m.face_handle(target_idx);

           emit( log(LOGOUT,"Picked Face " + QString::number(fh.idx()) + ", normal (" +
               QString::number(m.normal(fh)[0]) + "," +
               QString::number(m.normal(fh)[1]) + "," +
               QString::number(m.normal(fh)[2]) + ") ") );

           PolyMesh::VertexHandle vh = m.add_vertex(hit_point);
           m.split(fh,vh);
           m.garbage_collection();
           m.update_normals();

           emit updatedObject(object->id(), UPDATE_TOPOLOGY);
           emit updateView();
           emit createBackup(object->id(),"Split Face", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
529 530 531 532 533
         }
      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
534 535 536 537

//******************************************************************************

/** \brief Delete a face at the given position
538
 *
Dirk Wilden's avatar
Dirk Wilden committed
539 540
 * @param _event mouse position where the face is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
void TopologyPlugin::delete_face(QMouseEvent* _event) {
   if ( _event->type() != QEvent::MouseButtonPress )
      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;

      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH)  ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);
            emit( log(LOGOUT,"Picked Face " + QString::number(fh.idx()) + ", normal (" +
                                   QString::number(m.normal(fh)[0]) + "," +
                                   QString::number(m.normal(fh)[1]) + "," +
                                   QString::number(m.normal(fh)[2]) + ") ") );

            m.delete_face(fh);
            m.garbage_collection();
         }

         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH)  ) {
            PolyMesh& m = *PluginFunctions::polyMesh(object);
            PolyMesh::FaceHandle fh = m.face_handle(target_idx);
            emit( log(LOGOUT,"Picked Face " + QString::number(fh.idx()) + ", normal (" +
                       QString::number(m.normal(fh)[0]) + "," +
                       QString::number(m.normal(fh)[1]) + "," +
                       QString::number(m.normal(fh)[2]) + ") ") );

            m.delete_face(fh);
            m.garbage_collection();
         }

576
         emit updatedObject(object->id(), UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
577
         emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
578
         emit createBackup(object->id(),"Delete Face", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
579 580 581 582
      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
583 584 585 586

//******************************************************************************

/** \brief Flip an edge at the given position
587
 *
Dirk Wilden's avatar
Dirk Wilden committed
588 589
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
void TopologyPlugin::flip_edge(QMouseEvent* _event) {
   if ( _event->type() != QEvent::MouseButtonPress )
      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;
      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH)  ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);

            TriMesh::FaceEdgeIter fe_it(m,fh);
            TriMesh::HalfedgeHandle e1 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e2 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e3 = m.halfedge_handle(fe_it.handle(),0);

Jan Möbius's avatar
Jan Möbius committed
613
            const double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
Jan Möbius's avatar
 
Jan Möbius committed
614 615 616
            TriMesh::EdgeHandle closest_edge = m.edge_handle(e1);

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )), m.point(m.from_vertex_handle( e2 ))) < min_dist ) {
Jan Möbius's avatar
Jan Möbius committed
617
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
Jan Möbius's avatar
 
Jan Möbius committed
618 619 620 621
               closest_edge = m.edge_handle(e2);
            }

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 ))) < min_dist) {
Jan Möbius's avatar
Jan Möbius committed
622
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
Jan Möbius's avatar
 
Jan Möbius committed
623 624 625 626 627 628 629 630 631 632
               closest_edge = m.edge_handle(e3);
            }

            if ( m.is_flip_ok(closest_edge) )
               m.flip(closest_edge);
            else
              emit log(LOGERR,"Flip is not allowed here!");

            emit log(LOGOUT,"Picked Edge " + QString::number(closest_edge.idx()));

633
            emit updatedObject(object->id(), UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
634
            emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
635
            emit createBackup(object->id(),"Edge Flip", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
636 637 638 639 640 641 642 643 644 645
         }

         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH)  ) {
            emit log(LOGWARN,"Edge Flips not supported for Poly Meshes");
         }

      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
646 647 648 649

//******************************************************************************

/** \brief Collapse an edge at the given position
650
 *
Dirk Wilden's avatar
Dirk Wilden committed
651 652
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
void TopologyPlugin::collapse_edge(QMouseEvent* _event) {
   if ( _event->type() != QEvent::MouseButtonPress )
      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;
      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH)  ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);

            TriMesh::FaceEdgeIter fe_it(m,fh);
            TriMesh::HalfedgeHandle e1 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e2 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e3 = m.halfedge_handle(fe_it.handle(),0);

Jan Möbius's avatar
Jan Möbius committed
676
            const double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
Jan Möbius's avatar
 
Jan Möbius committed
677 678 679
            TriMesh::HalfedgeHandle closest_edge = e1;

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )), m.point(m.from_vertex_handle( e2 ))) < min_dist ) {
Jan Möbius's avatar
Jan Möbius committed
680
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
Jan Möbius's avatar
 
Jan Möbius committed
681 682 683 684
               closest_edge = e2;
            }

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 ))) < min_dist) {
Jan Möbius's avatar
Jan Möbius committed
685
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
Jan Möbius's avatar
 
Jan Möbius committed
686 687 688 689 690 691 692 693 694 695 696 697 698
               closest_edge = e3;
            }

            // collapse into to point which is closer to hitpoint
            TriMesh::Point to = m.point( m.to_vertex_handle(closest_edge) );
            TriMesh::Point from = m.point( m.from_vertex_handle(closest_edge) );

            if ( (hit_point - to).sqrnorm() > (hit_point - from).sqrnorm() )
               closest_edge = m.opposite_halfedge_handle(closest_edge);

            if ( m.is_collapse_ok(closest_edge) ){
   //             m.point(m.to_vertex_handle(closest_edge)) = hit_point;
               m.collapse(closest_edge );
David Bommes's avatar
David Bommes committed
699
               m.garbage_collection();
Jan Möbius's avatar
 
Jan Möbius committed
700 701 702 703 704
            } else
              emit log(LOGERR,"Collapse is not allowed here!");

            emit log(LOGOUT,"Picked Edge " + QString::number(closest_edge.idx()));

705
            emit updatedObject(object->id(), UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
706
            emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
707
            emit createBackup(object->id(),"Edge Collapse", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
708 709
         }

David Bommes's avatar
David Bommes committed
710
         // Poly Meshes
Jan Möbius's avatar
 
Jan Möbius committed
711
         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH)  ) {
David Bommes's avatar
David Bommes committed
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
           PolyMesh& m = *PluginFunctions::polyMesh(object);
           PolyMesh::FaceHandle fh = m.face_handle(target_idx);

           // find closest edge
           PolyMesh::HalfedgeHandle closest_edge(-1);
           double min_dist = FLT_MAX;

           PolyMesh::FaceEdgeIter fe_it(m,fh);
           for(; fe_it; ++fe_it)
           {
             PolyMesh::HalfedgeHandle heh = m.halfedge_handle(fe_it.handle(),0);
             double dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( heh )), m.point(m.from_vertex_handle( heh )));

             if( dist < min_dist)
             {
               min_dist = dist;
               closest_edge = heh;
             }
           }

           // collapse into to point which is closer to hitpoint
           PolyMesh::Point to = m.point( m.to_vertex_handle(closest_edge) );
           PolyMesh::Point from = m.point( m.from_vertex_handle(closest_edge) );

           if ( (hit_point - to).sqrnorm() > (hit_point - from).sqrnorm() )
              closest_edge = m.opposite_halfedge_handle(closest_edge);

//           if ( m.is_collapse_ok(closest_edge) ){
  //             m.point(m.to_vertex_handle(closest_edge)) = hit_point;
              m.collapse(closest_edge );
              m.garbage_collection();
//           } else
//             emit log(LOGERR,"Collapse is not allowed here!");

           emit log(LOGOUT,"Picked Edge " + QString::number(closest_edge.idx()));

           emit updatedObject(object->id(), UPDATE_TOPOLOGY);
           emit updateView();
           emit createBackup(object->id(),"Edge Collapse", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
751 752 753 754 755 756
         }

      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
757 758 759 760

//******************************************************************************

/** \brief Split an edge at the given position
761
 *
Dirk Wilden's avatar
Dirk Wilden committed
762 763
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
void TopologyPlugin::split_edge(QMouseEvent* _event) {
   if ( _event->type() != QEvent::MouseButtonPress )
      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;
      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH)  ) {
            TriMesh& m = *PluginFunctions::triMesh(object);
            TriMesh::FaceHandle fh = m.face_handle(target_idx);

            TriMesh::FaceEdgeIter fe_it(m,fh);
            TriMesh::HalfedgeHandle e1 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e2 = m.halfedge_handle(fe_it.handle(),0);

            ++fe_it;
            TriMesh::HalfedgeHandle e3 = m.halfedge_handle(fe_it.handle(),0);

Jan Möbius's avatar
Jan Möbius committed
787
            const double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
Jan Möbius's avatar
 
Jan Möbius committed
788 789 790
            TriMesh::EdgeHandle closest_edge = m.edge_handle(e1);

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )), m.point(m.from_vertex_handle( e2 ))) < min_dist ) {
Jan Möbius's avatar
Jan Möbius committed
791
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
Jan Möbius's avatar
 
Jan Möbius committed
792 793 794 795
               closest_edge = m.edge_handle(e2);
            }

            if ( ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 ))) < min_dist) {
Jan Möbius's avatar
Jan Möbius committed
796
               // min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
Jan Möbius's avatar
 
Jan Möbius committed
797 798 799 800 801 802 803
               closest_edge = m.edge_handle(e3);
            }

            m.split(closest_edge,hit_point);

            emit log(LOGOUT,"Picked Edge " + QString::number(closest_edge.idx()));

Mike Kremer's avatar
Mike Kremer committed
804
            emit updatedObject(object->id(), UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
805
            emit updateView();
Dirk Wilden's avatar
Dirk Wilden committed
806
            emit createBackup(object->id(),"Edge Split", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
807 808
         }
         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH)  ) {
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
        	 PolyMesh& m = *PluginFunctions::polyMesh(object);
        	 PolyMesh::FaceHandle fh = m.face_handle(target_idx);

        	 PolyMesh::FaceEdgeIter fe_it(m,fh);

        	 std::vector<PolyMesh::HalfedgeHandle> halfEdgeHandles;
        	 //get all edges which belongs to the picked face
        	 for (;fe_it; ++fe_it)
        	 {
        		 halfEdgeHandles.push_back(fe_it.current_halfedge_handle());
        	 }

        	 //search for the nearest edge
        	 PolyMesh::EdgeHandle closest_edge = m.edge_handle(*halfEdgeHandles.begin());
        	 double min_dist = ACG::Geometry::distPointLineSquared(hit_point, m.point(m.to_vertex_handle( *halfEdgeHandles.begin() )), m.point(m.from_vertex_handle( *halfEdgeHandles.begin() )));

        	 for (std::vector<PolyMesh::HalfedgeHandle>::iterator iter = halfEdgeHandles.begin(); iter != halfEdgeHandles.end(); ++iter)
        	 {
        		 double dist = ACG::Geometry::distPointLineSquared(hit_point, m.point(m.to_vertex_handle( *iter )), m.point(m.from_vertex_handle( *iter )));
        		 if (dist < min_dist)
        		 {
        			 closest_edge = m.edge_handle(*iter);
        			 min_dist = dist;
        		 }
        	 }

        	 m.split(closest_edge,hit_point);

        	 emit log(LOGOUT,"Picked Edge " + QString::number(closest_edge.idx()));

        	 emit updatedObject(object->id(), UPDATE_TOPOLOGY);
        	 emit updateView();
        	 emit createBackup(object->id(),"Edge Split", UPDATE_TOPOLOGY);
Jan Möbius's avatar
 
Jan Möbius committed
842 843 844 845 846 847 848 849
         }

      } else return;
   }
}

Q_EXPORT_PLUGIN2( topologyplugin , TopologyPlugin );