From 105b93b35add01308e1fbba4c5cd3e0b718c265f Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 14 Jan 2021 08:51:39 +0000 Subject: [PATCH] Now it should work with overlapping polylines --- .../simplify_overlapping_polyline.cpp | 51 +++++++++++++ .../CGAL/Polyline_simplification_2/simplify.h | 42 ++++------ .../Polyline_constraint_hierarchy_2.h | 76 +++++++++++-------- 3 files changed, 111 insertions(+), 58 deletions(-) create mode 100644 Polyline_simplification_2/examples/Polyline_simplification_2/simplify_overlapping_polyline.cpp diff --git a/Polyline_simplification_2/examples/Polyline_simplification_2/simplify_overlapping_polyline.cpp b/Polyline_simplification_2/examples/Polyline_simplification_2/simplify_overlapping_polyline.cpp new file mode 100644 index 00000000000..2df47f53932 --- /dev/null +++ b/Polyline_simplification_2/examples/Polyline_simplification_2/simplify_overlapping_polyline.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace PS = CGAL::Polyline_simplification_2; + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; + +typedef PS::Vertex_base_2 Vb; +typedef CGAL::Constrained_triangulation_face_base_2 Fb; +typedef CGAL::Triangulation_data_structure_2 TDS; +typedef CGAL::Constrained_Delaunay_triangulation_2 CDT; +typedef CGAL::Constrained_triangulation_plus_2 CT; + +typedef CT::Point Point; +typedef CT::Constraint_iterator Constraint_iterator; +typedef CT::Vertices_in_constraint_iterator Vertices_in_constraint_iterator; +typedef CT::Points_in_constraint_iterator Points_in_constraint_iterator; +typedef PS::Stop_below_count_ratio_threshold Stop; +typedef PS::Squared_distance_cost Cost; + +int main() +{ + CT ct; + + std::vector P = { Point(0,1), Point(1,1), Point(2,2), Point(3,1), Point(4,1), Point(5,1) }; + std::vector Q = { Point(5,0), Point(4,1), Point(3,1), Point(2,2), Point(1,1), Point(0,0) }; + std::vector R = { Point(3,1), Point(4,1) }; + + ct.insert_constraint(P); + ct.insert_constraint(Q); + ct.insert_constraint(R); + + PS::simplify(ct, Cost(), Stop(0.5)); + + for(Constraint_iterator cit = ct.constraints_begin(); + cit != ct.constraints_end(); + ++cit) { + std::cout << "simplified polyline" << std::endl; + for(Points_in_constraint_iterator vit = + ct.points_in_constraint_begin(*cit); + vit != ct.points_in_constraint_end(*cit); + ++vit) + std::cout << *vit << std::endl; + } + return 0; +} diff --git a/Polyline_simplification_2/include/CGAL/Polyline_simplification_2/simplify.h b/Polyline_simplification_2/include/CGAL/Polyline_simplification_2/simplify.h index 26416805d72..579bcfc9707 100644 --- a/Polyline_simplification_2/include/CGAL/Polyline_simplification_2/simplify.h +++ b/Polyline_simplification_2/include/CGAL/Polyline_simplification_2/simplify.h @@ -101,7 +101,6 @@ public: typedef boost::readable_property_map_tag category; typedef std::size_t value_type; typedef value_type reference; - // typedef Vertices_in_constraint_iterator key_type; typedef Vertex_handle key_type; Vertex_index_map* vertex_index_map; @@ -112,12 +111,10 @@ public: reference operator[] ( key_type const& x ) const { - // return x.base()->id ; - return (*vertex_index_map)[x]; + return (*vertex_index_map)[x]; } } ; - //typedef CGAL::Modifiable_priority_queue MPQ ; typedef CGAL::Modifiable_priority_queue MPQ ; MPQ* mpq; @@ -189,7 +186,17 @@ public: it != pct.vertices_in_constraint_end(cid); ++it){ if((*it)->is_removable()){ - vertex_to_iterator[*it].push_back(it); + typename std::map >::iterator lit; + lit = vertex_to_iterator.find(*it); + + if(lit != vertex_to_iterator.end()){ + std::list& ilist = lit->second; + if(std::find(ilist.begin(),ilist.end(),it) == ilist.end()){ + ilist.push_back(it); + } + }else{ + vertex_to_iterator[*it].push_back(it); + } } } } @@ -202,26 +209,6 @@ public: } } */ - - /* - // the previous code does mark vertices with more than one constraint on it as unremovable - std::set vertices; - Constraint_iterator cit = pct.constraints_begin(), e = pct.constraints_end(); - for(; cit!=e; ++cit){ - Constraint_id cid = *cit; - Vertices_in_constraint_iterator it = pct.vertices_in_constraint_begin(cid); - (*it)->set_removable(false); - for(; it != pct.vertices_in_constraint_end(cid); ++it){ - if(vertices.find(*it) != vertices.end()){ - (*it)->set_removable(false); - } else { - vertices.insert(*it); - } - } - it = boost::prior(it); - (*it)->set_removable(false); - } - */ } // For all polyline constraints we compute the cost of all unremovable and not removed vertices @@ -236,7 +223,9 @@ public: boost::optional dist = cost(pct, it); if(dist){ (*it)->set_cost(*dist); - (*mpq).push(*it); + if(! (*mpq).contains(*it)){ + (*mpq).push(*it); + } ++n; } else { // no need to set the costs as this vertex is not in the priority queue @@ -342,6 +331,7 @@ operator()() if(stop(pct, v, v->cost(), pct_initial_number_of_vertices, pct.number_of_vertices())){ return false; } + Vertices_in_constraint_iterator vit = vertex_to_iterator[v].front(); if(is_removable(vit)){ Vertices_in_constraint_iterator u = boost::prior(vit), w = boost::next(vit); diff --git a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h index d46c76a3666..4b739c74f2c 100644 --- a/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h +++ b/Triangulation_2/include/CGAL/Triangulation_2/internal/Polyline_constraint_hierarchy_2.h @@ -46,14 +46,13 @@ private: class Node { public: explicit Node(Vertex_handle vh, bool input = false) - : vertex_(vh), /* id(-1),*/ input(input) + : vertex_(vh), input(input) {} const Point& point() const { return vertex_->point(); } Vertex_handle vertex() const { return vertex_; } private: Vertex_handle vertex_; public: - // int id; bool input; }; @@ -585,45 +584,58 @@ void Polyline_constraint_hierarchy_2::simplify(Vertex_it uc, Vertex_it wc) { + // TODO: How do we (want to) deal with u == w ??? Vertex_handle u = *uc, v = *vc, w = *wc; typename Sc_to_c_map::iterator uv_sc_iter = sc_to_c_map.find(make_edge(u, v)); - CGAL_assertion_msg( uv_sc_iter != sc_to_c_map.end(), "not a subconstraint" ); + typename Sc_to_c_map::iterator vw_sc_iter = sc_to_c_map.find(make_edge(v, w)); Context_list* uv_hcl = uv_sc_iter->second; - CGAL_assertion_msg((u == w) || (uv_hcl->size() == 1), "more than one constraint passing through the subconstraint" ); - - if(*(uv_hcl->front().current()) != u) { - std::swap(u,w); - uv_sc_iter = sc_to_c_map.find(make_edge(u, v)); - CGAL_assertion_msg( uv_sc_iter != sc_to_c_map.end(), "not a subconstraint" ); - uv_hcl = (*uv_sc_iter).second; - CGAL_assertion_msg((u == w) || (uv_hcl->size() == 1), "more than one constraint passing through the subconstraint" ); - } - // now u,v, and w are ordered along the polyline constraint + Context_list* vw_hcl = vw_sc_iter->second; + // AF: what is input() about??? if(vc.input()){ uc.input() = true; wc.input() = true; } - typename Sc_to_c_map::iterator vw_sc_iter = sc_to_c_map.find(make_edge(v, w)); - CGAL_assertion_msg( vw_sc_iter != sc_to_c_map.end(), "not a subconstraint" ); - Context_list* vw_hcl = vw_sc_iter->second; - CGAL_assertion_msg((u == w) || (vw_hcl->size() == 1), "more than one constraint passing through the subconstraint" ); - Vertex_list* vertex_list = uv_hcl->front().id().vl_ptr(); - CGAL_assertion_msg(vertex_list == vw_hcl->front().id().vl_ptr(), "subconstraints from different polyline constraints" ); - // Remove the list item which points to v - vertex_list->skip(vc.base()); - - if(u != w){ - // Remove the entries for [u,v] and [v,w] - sc_to_c_map.erase(uv_sc_iter); - sc_to_c_map.erase(vw_sc_iter); - delete vw_hcl; - // reuse other context list - sc_to_c_map[make_edge(u,w)] = uv_hcl; - }else{ - sc_to_c_map.erase(uv_sc_iter); - delete vw_hcl; + // Take contexts from the two context lists depending on the orientation of the constraints + // These are the contexts where current is either u or w + // remove from uv_hcl the contexts where current is not u + // remove from vw_hcl the contexts where current is not w + // splice into uv_hcl + typename Context_list::iterator it = uv_hcl->begin(); + while(it != uv_hcl->end()){ + if((*it->current()) != u){ + it = uv_hcl->erase(it); + }else{ + // Remove the list item which points to v + Vertex_list* vertex_list = it->id().vl_ptr(); + Vertex_it vc_in_context = it->current(); + vc_in_context = boost::next(vc_in_context); + vertex_list->skip(vc_in_context.base()); + ++it; + } } + it = vw_hcl->begin(); + while(it != vw_hcl->end()){ + if((*it->current()) != w){ + it = vw_hcl->erase(it); + }else{ + // Remove the list item which points to v + Vertex_list* vertex_list = it->id().vl_ptr(); + Vertex_it vc_in_context = it->current(); + vc_in_context = boost::next(vc_in_context); + vertex_list->skip(vc_in_context.base()); + ++it; + } + } + + uv_hcl->splice(uv_hcl->end(),*vw_hcl); + delete vw_hcl; + + sc_to_c_map.erase(uv_sc_iter); + sc_to_c_map.erase(vw_sc_iter); + + // reuse other context list + sc_to_c_map[make_edge(u,w)] = uv_hcl; }