diff --git a/BGL/include/CGAL/boost/graph/Face_filtered_graph.h b/BGL/include/CGAL/boost/graph/Face_filtered_graph.h index fb6036dc89e..8cf3c7884eb 100644 --- a/BGL/include/CGAL/boost/graph/Face_filtered_graph.h +++ b/BGL/include/CGAL/boost/graph/Face_filtered_graph.h @@ -463,64 +463,57 @@ struct Face_filtered_graph /// returns `true` if around any vertex of a selected face, /// there is at most one connected set of selected faces. - bool is_selection_valid() + bool is_selection_valid() const { - for(vertex_descriptor vd : vertices(*this) ) + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor halfedge_descriptor; + + // Non-manifoldness can appear either: + // - if 'pm' is pinched at a vertex. While traversing the incoming halfedges at this vertex, + // we will meet strictly more than one border halfedge. + // - if there are multiple umbrellas around a vertex. In that case, we will find a non-visited + // halfedge that has for target a vertex that is already visited. + + boost::unordered_set vertices_visited; + boost::unordered_set halfedges_handled; + + for(halfedge_descriptor hd : halfedges(*this)) { - face_descriptor first_selected = boost::graph_traits::null_face(); - bool first_unselected_found(false), - second_unselected_found(false); + CGAL_assertion(is_in_cc(hd)); - //find an unselected face, then find the first selected face. - //Find another unselected face, the next selected face must be the first; - //else this is not valid. - halfedge_descriptor hd = halfedge(vd, _graph); - face_descriptor first_tested = boost::graph_traits::null_face(); - while(1) //will break if valid, return false if not valid + if(!halfedges_handled.insert(hd).second) // already treated this halfedge + continue; + + vertex_descriptor vd = target(hd, *this); + CGAL_assertion(is_in_cc(vd)); + + // Check if we have already met this vertex before (necessarily in a different umbrella + // since we have never treated the halfedge 'hd') + if(!vertices_visited.insert(vd).second) + return false; + + std::size_t border_halfedge_counter = 0; + + // Can't simply call halfedges_around_target(vd, *this) because 'halfedge(vd)' is not necessarily 'hd' + halfedge_descriptor ihd = hd; + do { - face_descriptor fd = face(hd, _graph); + halfedges_handled.insert(ihd); + if(is_border(ihd, *this)) + ++border_halfedge_counter; - if(first_tested == boost::graph_traits::null_face()) - first_tested = fd; - else if(fd == first_tested ) + do { - //if there is no unselected face, break - if(selected_faces[get(fimap, fd)] && !first_unselected_found) - break; - //if there is no selected face, break - else if(!selected_faces[get(fimap, fd)] && - first_selected == boost::graph_traits::null_face()) - break; + ihd = prev(opposite(ihd, _graph), _graph); } - - if(fd != boost::graph_traits::null_face()) - { - if(selected_faces[get(fimap, fd)]) - { - if(first_unselected_found && - first_selected == boost::graph_traits::null_face()) - { - first_selected = fd; - } - else if(second_unselected_found) - { - if(fd == first_selected) - break; - else - return false; - } - } - else - { - if(first_selected == boost::graph_traits::null_face()) - first_unselected_found = true; - else - second_unselected_found = true; - } - } - hd = next(opposite(hd, _graph), _graph); + while(!is_in_cc(ihd) && ihd != hd); } + while(ihd != hd); + + if(border_halfedge_counter > 1) + return false; } + return true; }