TopologyPlugin.cc 25.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
// 
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================
Jan Möbius's avatar
 
Jan Möbius committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

#include <ACG/Geometry/Algorithms.hh>

#define EDGE_FLIP_POPUP "Flip Edge"
#define EDGE_COLLAPSE_POPUP "Collapse Edge"
#define EDGE_SPLIT_POPUP "Split Edge"
#define FACE_ADD_POPUP "Add Face"
#define FACE_SPLIT_POPUP "Split Face"
#define FACE_DELETE_POPUP "Delete Face"

#include <OpenMesh/Core/System/omstream.hh>
#include <float.h>

Dirk Wilden's avatar
Dirk Wilden committed
49 50 51 52 53 54

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

/** \brief initialize the Plugin
 * 
 */
Jan Möbius's avatar
 
Jan Möbius committed
55 56 57 58 59 60 61 62
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
63 64 65

  toolbar_ = new QToolBar("Topology");

Jan Möbius's avatar
 
Jan Möbius committed
66 67 68
  QActionGroup* group = new QActionGroup(0);
  QAction* act;

Dirk Wilden's avatar
Dirk Wilden committed
69
  act = toolbar_->addAction( EDGE_FLIP_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
70 71
  act->setCheckable( true);
  act->setActionGroup(group);
Dirk Wilden's avatar
Dirk Wilden committed
72
  act = toolbar_->addAction( EDGE_SPLIT_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
73 74
  act->setCheckable( true);
  act->setActionGroup(group);
Dirk Wilden's avatar
Dirk Wilden committed
75
  act = toolbar_->addAction( EDGE_COLLAPSE_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
76 77
  act->setCheckable( true);
  act->setActionGroup(group);
Dirk Wilden's avatar
Dirk Wilden committed
78 79
  toolbar_->addSeparator();
  act = toolbar_->addAction( FACE_ADD_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
80 81
  act->setCheckable( true);
  act->setActionGroup(group);
Dirk Wilden's avatar
Dirk Wilden committed
82
  act = toolbar_->addAction( FACE_SPLIT_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
83 84
  act->setCheckable( true);
  act->setActionGroup(group);
Dirk Wilden's avatar
Dirk Wilden committed
85
  act = toolbar_->addAction( FACE_DELETE_POPUP );
Jan Möbius's avatar
 
Jan Möbius committed
86 87 88 89 90
  act->setCheckable( true);
  act->setActionGroup(group);

  group->setExclusive(true);

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

Dirk Wilden's avatar
Dirk Wilden committed
93 94
  emit addToolbar( toolbar_ );
}
Jan Möbius's avatar
 
Jan Möbius committed
95 96


Dirk Wilden's avatar
Dirk Wilden committed
97
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
98 99


Dirk Wilden's avatar
Dirk Wilden committed
100 101 102 103 104
/** \brief Toolbar action was triggered
 * 
 * @param _action the action that was triggered
 */
void TopologyPlugin::toolBarTriggered(QAction* _action){
Jan Möbius's avatar
 
Jan Möbius committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  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);
}


Dirk Wilden's avatar
Dirk Wilden committed
122
//******************************************************************************
Jan Möbius's avatar
 
Jan Möbius committed
123

Dirk Wilden's avatar
Dirk Wilden committed
124 125 126 127
/** \brief this is called when a mouse event occurred
 * 
 * @param _event the event that occurred
 */
Jan Möbius's avatar
 
Jan Möbius committed
128 129 130 131 132 133 134 135 136 137 138 139
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
140 141 142 143 144 145

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

/** \brief Deselect the vertices from AddFace mode
 * 
 */
Jan Möbius's avatar
 
Jan Möbius committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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);
        object->update();
      }
    }

    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);
        object->update();
      }
    }

  }

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

Dirk Wilden's avatar
Dirk Wilden committed
176 177 178 179 180 181 182

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

/** \brief Add a face
 * 
 * @param _event mouse position where one of the vertices is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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();
            float shortest_distance = (m.point(closest) - hit_point).sqrnorm();

            ++fv_it;
            if ( (m.point(fv_it.handle() ) - hit_point).sqrnorm() < shortest_distance ) {
               shortest_distance = (m.point(fv_it.handle() ) - hit_point).sqrnorm();
               closest = fv_it.handle();
            }

            ++fv_it;
            if ( (m.point(fv_it.handle() ) - hit_point).sqrnorm() < shortest_distance ) {
               shortest_distance = (m.point(fv_it.handle() ) - hit_point).sqrnorm();
               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);
                object->update();
                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 ) {
              object->update();
              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);
            }

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

            clearAddFaceVertices();

            if ( fh.is_valid() )
              emit createBackup(object->id(),"Add Face");
            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);
                  object->update();
                  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) ) {
              object->update();
              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();
              }

              fh = m.add_face(rvhs);
            }

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

            clearAddFaceVertices();

            if ( fh.is_valid() )
              emit createBackup(object->id(),"Add Face");
            else
              emit log(LOGERR,"Unable to add face!");

         }
      }
  }
}

Dirk Wilden's avatar
Dirk Wilden committed
409 410 411 412 413 414 415

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

/** \brief Split a face at the given point
 * 
 * @param _event mouse position where the face is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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);
            m.update_normals();

            object->update();
            emit updatedObject(object->id());
            emit updateView();
            emit createBackup(object->id(),"Split Face");
         }

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

Dirk Wilden's avatar
Dirk Wilden committed
453 454 455 456 457 458 459

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

/** \brief Delete a face at the given position
 * 
 * @param _event mouse position where the face is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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();
         }

         object->update();
         emit updatedObject(object->id());
         emit updateView();
         emit createBackup(object->id(),"Delete Face");
      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
503 504 505 506 507 508 509

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

/** \brief Flip an edge at the given position
 * 
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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);

            double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
            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 ) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
               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) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
               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()));

            object->update();
            emit updatedObject(object->id());
            emit updateView();
            emit createBackup(object->id(),"Edge Flip");
         }

         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
567 568 569 570 571 572 573

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

/** \brief Collapse an edge at the given position
 * 
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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);

            double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
            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 ) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
               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) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
               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 );
            } else
              emit log(LOGERR,"Collapse is not allowed here!");

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

            object->update();
            emit updatedObject(object->id());
            emit updateView();
            emit createBackup(object->id(),"Edge Collapse");
         }

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

      } else return;
   }
}

Dirk Wilden's avatar
Dirk Wilden committed
639 640 641 642 643 644 645

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

/** \brief Split an edge at the given position
 * 
 * @param _event mouse position where the edge is picked
 */
Jan Möbius's avatar
 
Jan Möbius committed
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
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);

            double min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e1 )), m.point(m.from_vertex_handle( e1 )));
            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 ) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e2 )),m.point(m.from_vertex_handle( e2 )));
               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) {
               min_dist = ACG::Geometry::distPointLineSquared(hit_point,m.point(m.to_vertex_handle( e3 )),m.point(m.from_vertex_handle( e3 )));
               closest_edge = m.edge_handle(e3);
            }

            m.split(closest_edge,hit_point);

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

            object->update();
            emit updatedObject(object->id());
            emit updateView();
            emit createBackup(object->id(),"Edge Split");
         }

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

      } else return;
   }
}

Q_EXPORT_PLUGIN2( topologyplugin , TopologyPlugin );