From 8b6dff6c2a064d0dedc50de9bc7d1c53740e4da3 Mon Sep 17 00:00:00 2001 From: Pierre Kraemer Date: Tue, 12 Dec 2017 17:22:59 +0100 Subject: [PATCH] check that handle vertices are in the free vertices area --- schnapps/core/schnapps.cpp | 2 +- .../surface_deformation/map_parameters.h | 26 +++++- .../surface_deformation.cpp | 85 +++++++++++++------ .../surface_deformation/surface_deformation.h | 5 +- 4 files changed, 86 insertions(+), 32 deletions(-) diff --git a/schnapps/core/schnapps.cpp b/schnapps/core/schnapps.cpp index bc7c64c..59ace09 100644 --- a/schnapps/core/schnapps.cpp +++ b/schnapps/core/schnapps.cpp @@ -233,7 +233,7 @@ Plugin* SCHNApps::enable_plugin(const QString& plugin_name) // set the plugin with correct parameters (name, filepath, SCHNApps) // plugin->set_name(plugin_name); if (plugin_name != plugin->get_name()) - cgogn_log_warning("SCHNApps::enable_plugin") << "plugin name incompatibility: "<< plugin_name.toStdString() << " != " << plugin->get_name().toStdString(); + cgogn_log_warning("SCHNApps::enable_plugin") << "plugin name incompatibility: " << plugin_name.toStdString() << " != " << plugin->get_name().toStdString(); plugin->set_file_path(plugin_file_path); plugin->set_schnapps(this); diff --git a/schnapps/plugins/surface_deformation/map_parameters.h b/schnapps/plugins/surface_deformation/map_parameters.h index a444da5..558c638 100644 --- a/schnapps/plugins/surface_deformation/map_parameters.h +++ b/schnapps/plugins/surface_deformation/map_parameters.h @@ -204,7 +204,7 @@ struct MapParameters solver_ready_ = false; } - void build_solver() + bool build_solver() { if (initialized_ && !solver_ready_ && free_vertex_set_ && free_vertex_set_->get_nb_cells() > 0 && @@ -213,12 +213,32 @@ struct MapParameters CMap2* map2 = map_->get_map(); CMap2::CellMarkerStore working_vertices_marker(*map2); + // check that handle vertices are surrounded only by handle or free vertices + bool handle_ok = true; + map2->foreach_cell([&] (CMap2::Vertex v) -> bool + { + if (handle_vertex_set_->is_selected(v)) + { + map2->foreach_adjacent_vertex_through_edge(v, [&] (CMap2::Vertex av) -> bool + { + if (!handle_vertex_set_->is_selected(av) && !free_vertex_set_->is_selected(av)) + handle_ok = false; + return handle_ok; + }); + } + return handle_ok; + }); + if (!handle_ok) + { + cgogn_log_warning("surface_deformation") << "Handle is not defined in the free area"; + return false; + } + // build the cell cache of working area vertices (and mark them) working_cells_->build([&] (CMap2::Vertex v) -> bool { if (handle_vertex_set_->is_selected(v)) // handle vertices { - // TODO: check that it is surrounded only by handle or free vertices working_vertices_marker.mark(v); return true; } @@ -335,6 +355,8 @@ struct MapParameters solver_ready_ = true; } + + return solver_ready_; } private: diff --git a/schnapps/plugins/surface_deformation/surface_deformation.cpp b/schnapps/plugins/surface_deformation/surface_deformation.cpp index 7e1b75a..f03c56e 100644 --- a/schnapps/plugins/surface_deformation/surface_deformation.cpp +++ b/schnapps/plugins/surface_deformation/surface_deformation.cpp @@ -111,19 +111,9 @@ bool Plugin_SurfaceDeformation::keyPress(View* view, QKeyEvent* event) { const MapParameters& p = get_parameters(mhg); if (!dragging_ && p.get_handle_vertex_set() && p.get_handle_vertex_set()->get_nb_cells() > 0) - { - dragging_ = true; - drag_init_ = false; - view->setMouseTracking(true); - view->get_current_camera()->disable_views_bb_fitting(); - } + start_dragging(view); else - { - dragging_ = false; - drag_init_ = false; - view->setMouseTracking(false); - view->get_current_camera()->enable_views_bb_fitting(); - } + stop_dragging(view); } break; } @@ -151,6 +141,22 @@ bool Plugin_SurfaceDeformation::keyPress(View* view, QKeyEvent* event) return true; } +void Plugin_SurfaceDeformation::start_dragging(View* view) +{ + dragging_ = true; + drag_init_ = false; + view->setMouseTracking(true); + view->get_current_camera()->disable_views_bb_fitting(); +} + +void Plugin_SurfaceDeformation::stop_dragging(View* view) +{ + dragging_ = false; + drag_init_ = false; + view->setMouseTracking(false); + view->get_current_camera()->enable_views_bb_fitting(); +} + bool Plugin_SurfaceDeformation::mouseMove(View* view, QMouseEvent* event) { if (dragging_) @@ -177,25 +183,37 @@ bool Plugin_SurfaceDeformation::mouseMove(View* view, QMouseEvent* event) } else { - qoglviewer::Vec pp(event->x(), event->y(), drag_z_); - qoglviewer::Vec qq = view->camera()->unprojectedCoordinatesOf(pp); - - qoglviewer::Vec vec = qq - drag_previous_; - VEC3 t(vec.x, vec.y, vec.z); - p.handle_vertex_set_->foreach_cell([&] (CMap2::Vertex v) + if (p.initialized_) { - p.position_[v] += t; - }); + qoglviewer::Vec pp(event->x(), event->y(), drag_z_); + qoglviewer::Vec qq = view->camera()->unprojectedCoordinatesOf(pp); - drag_previous_ = qq; + qoglviewer::Vec vec = qq - drag_previous_; + VEC3 t(vec.x, vec.y, vec.z); + p.handle_vertex_set_->foreach_cell([&] (CMap2::Vertex v) + { + p.position_[v] += t; + }); - if (p.initialized_) - as_rigid_as_possible(mhg); + drag_previous_ = qq; - mhg->notify_attribute_change(CMap2::Vertex::ORBIT, p.get_position_attribute_name()); + if (as_rigid_as_possible(mhg)) + { + mhg->notify_attribute_change(CMap2::Vertex::ORBIT, p.get_position_attribute_name()); - for (View* view : mhg->get_linked_views()) - view->update(); + for (View* view : mhg->get_linked_views()) + view->update(); + } + else + { + // undo handle displacement & stop dragging + p.handle_vertex_set_->foreach_cell([&] (CMap2::Vertex v) + { + p.position_[v] -= t; + }); + stop_dragging(view); + } + } } } } @@ -401,7 +419,7 @@ void Plugin_SurfaceDeformation::stop(MapHandlerGen* map, bool update_dock_tab) } } -void Plugin_SurfaceDeformation::as_rigid_as_possible(MapHandlerGen* map) +bool Plugin_SurfaceDeformation::as_rigid_as_possible(MapHandlerGen* map) { if (map && map->dimension() == 2) { @@ -409,7 +427,8 @@ void Plugin_SurfaceDeformation::as_rigid_as_possible(MapHandlerGen* map) if (p.initialized_) { if (!p.solver_ready_) - p.build_solver(); + if (!p.build_solver()) + return false; CMap2* map2 = p.map_->get_map(); @@ -520,8 +539,18 @@ void Plugin_SurfaceDeformation::as_rigid_as_possible(MapHandlerGen* map) }, *p.working_cells_ ); + + return true; + } + else + { + cgogn_log_warning("surface_deformation") << "initial state not initialized"; + return false; } } + + cgogn_log_warning("surface_deformation") << "wrong map dimension"; + return false; } } // namespace plugin_surface_deformation diff --git a/schnapps/plugins/surface_deformation/surface_deformation.h b/schnapps/plugins/surface_deformation/surface_deformation.h index a96baee..15fe8d8 100644 --- a/schnapps/plugins/surface_deformation/surface_deformation.h +++ b/schnapps/plugins/surface_deformation/surface_deformation.h @@ -72,6 +72,9 @@ class SCHNAPPS_PLUGIN_SURFACE_DEFORMATION_API Plugin_SurfaceDeformation : public inline void resizeGL(View*, int, int) override {} + void start_dragging(View* view); + void stop_dragging(View* view); + void view_linked(View*) override; void view_unlinked(View*) override; @@ -102,7 +105,7 @@ public slots: void initialize(MapHandlerGen* map, bool update_dock_tab); void stop(MapHandlerGen* map, bool update_dock_tab); - void as_rigid_as_possible(MapHandlerGen* map); + bool as_rigid_as_possible(MapHandlerGen* map); private: