From 370ad4a5bffb94b3c787b037834919ad99edf266 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Wed, 18 Nov 2020 15:25:12 +0000 Subject: [PATCH 1/2] Fix test of split_graph_into_polylines() --- BGL/test/BGL/CMakeLists.txt | 2 + BGL/test/BGL/split.cin | 6 -- BGL/test/BGL/split.cpp | 113 ------------------------------------ BGL/test/BGL/test_split.cpp | 111 +++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 119 deletions(-) delete mode 100644 BGL/test/BGL/split.cin delete mode 100644 BGL/test/BGL/split.cpp create mode 100644 BGL/test/BGL/test_split.cpp diff --git a/BGL/test/BGL/CMakeLists.txt b/BGL/test/BGL/CMakeLists.txt index 42f84501ba2..8d5b0e7b652 100644 --- a/BGL/test/BGL/CMakeLists.txt +++ b/BGL/test/BGL/CMakeLists.txt @@ -35,6 +35,8 @@ if(OpenMesh_FOUND) target_link_libraries(graph_concept_OpenMesh PRIVATE ${OPENMESH_LIBRARIES}) endif() +create_single_source_cgal_program("test_split.cpp") + create_single_source_cgal_program("next.cpp") create_single_source_cgal_program("test_circulator.cpp") diff --git a/BGL/test/BGL/split.cin b/BGL/test/BGL/split.cin deleted file mode 100644 index 484dc3db9ac..00000000000 --- a/BGL/test/BGL/split.cin +++ /dev/null @@ -1,6 +0,0 @@ -5 -2 1 1 1 -1 1 1 0 -1 0 2 0 -2 1 2 0 -3 1 2 1 \ No newline at end of file diff --git a/BGL/test/BGL/split.cpp b/BGL/test/BGL/split.cpp deleted file mode 100644 index 7df61a985a9..00000000000 --- a/BGL/test/BGL/split.cpp +++ /dev/null @@ -1,113 +0,0 @@ - -#include -#include -#include -#include -#include - -typedef CGAL::Simple_cartesian K; -typedef K::Point_2 Point_2; - -typedef boost::adjacency_list < boost::listS, - boost::vecS, - boost::undirectedS, - Point_2 > G; - -typedef boost::graph_traits::vertex_descriptor vertex_descriptor; - -typedef std::vector Polyline_2; - -struct Is_terminal -{ - template - bool operator ()(VertexDescriptor vd , const Graph& g ) - { - return false; // degree(vd,g) != 2; is a bad test in case of parallel edges - } -}; - - -template -struct Polyline_visitor -{ - std::list& polylines; - const Graph& points_pmap; - - Polyline_visitor(std::list& lines, - const Graph& points_property_map) - : polylines(lines), - points_pmap(points_property_map) - {} - - void start_new_polyline() - { - Polyline_2 V; - polylines.push_back(V); - } - - void add_node(typename boost::graph_traits::vertex_descriptor vd) - { - Polyline_2& polyline = polylines.back(); - polyline.push_back(points_pmap[vd]); - } -}; - - -int main() -{ - G g; - - std::list polylines; - Polyline_visitor polyline_visitor(polylines, g); - std::map p2vd; - - int n; - std::cin >> n; // number of segments - - - Point_2 p, q; - vertex_descriptor vdp, vdq; - for(int i=0; i < n; i++){ - std::cin >> p >> q; - - if(p2vd.find(p) == p2vd.end()){ - vdp = add_vertex(g); - g[vdp] = p; - p2vd[p] = vdp; - } else { - vdp = p2vd[p]; - } - if(p2vd.find(q) == p2vd.end()){ - vdq = add_vertex(g); - g[vdq] = q; - p2vd[q] = vdq; - } else { - vdq = p2vd[q]; - } - boost::add_edge(vdp, vdq, g); - } - - CGAL::split_graph_into_polylines( g, - polyline_visitor, - Is_terminal() ); - std::cout.precision(17); - - - for(std::list::iterator it = polylines.begin(); it!= polylines.end(); ++it){ - Polyline_2& poly = *it; - std::size_t n; - if(poly.front() == poly.back()){ - std::cout << "POLYGON" << std::endl; - n = poly.size() -1; - }else{ - std::cout << "POLYLINE" << std::endl; - n = poly.size(); - } - for(std::size_t j=0; j < n; j++){ - std::cout << poly[j] << std::endl; - } - std::cout << std::endl; - } - - return 0; -} diff --git a/BGL/test/BGL/test_split.cpp b/BGL/test/BGL/test_split.cpp new file mode 100644 index 00000000000..cddb2130250 --- /dev/null +++ b/BGL/test/BGL/test_split.cpp @@ -0,0 +1,111 @@ + +#include +#include + +#include + +#include +#include + + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef K::Point_2 Point_2; + + +typedef boost::adjacency_list Graph; + +typedef boost::graph_traits::vertex_descriptor vertex_descriptor; + +typedef std::map Point_vertex_map; + +typedef std::vector Polyline_2; + + +// inserts a polyline into a graph +void insert(const std::vector& poly, Graph& graph, Point_vertex_map& pvmap) +{ + vertex_descriptor u, v; + for (int i = 0; i < poly.size(); i++) { + // check if the point is not yet in the graph + if (pvmap.find(poly[i]) == pvmap.end()) { + v = add_vertex(graph); + pvmap[poly[i]] = v; + } + else { + v = pvmap[poly[i]]; + } + graph[v] = poly[i]; // associate the point to the vertex + if (i != 0) { + add_edge(u, v, graph); + } + u = v; + } +} + +template +struct Polyline_visitor +{ + std::list& polylines; + const Graph& points_pmap; + + Polyline_visitor(std::list& lines, + const Graph& points_property_map) + : polylines(lines), + points_pmap(points_property_map) + {} + + void start_new_polyline() + { + Polyline_2 V; + polylines.push_back(V); + } + + void add_node(typename boost::graph_traits::vertex_descriptor vd) + { + Polyline_2& polyline = polylines.back(); + polyline.push_back(points_pmap[vd]); + } + + void end_polyline() + {} + +}; + + +int main() +{ + Polyline_2 polyA = { Point_2(0,0), Point_2(1,0), Point_2(2,0), Point_2(3,0), Point_2(4,0)}; + Polyline_2 polyB = { Point_2(1,-1), Point_2(1,0), Point_2(2,0), Point_2(2,1), Point_2(2,2) }; + + Graph graph; + Point_vertex_map pvmap; + + insert(polyA, graph, pvmap); + insert(polyB, graph, pvmap); + + std::list polylines; + Polyline_visitor polyline_visitor(polylines, graph); + + CGAL::split_graph_into_polylines( graph, + polyline_visitor); + + + for(std::list::iterator it = polylines.begin(); it!= polylines.end(); ++it){ + Polyline_2& poly = *it; + std::size_t n; + if(poly.front() == poly.back()){ + std::cout << "POLYGON" << std::endl; + n = poly.size() -1; + }else{ + std::cout << "POLYLINE" << std::endl; + n = poly.size(); + } + for(std::size_t j=0; j < n; j++){ + std::cout << poly[j] << std::endl; + } + std::cout << std::endl; + } + + return 0; + +} From 6f50e81bff15fb3efb378116ec76e927882cbe04 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Tue, 24 Nov 2020 15:24:02 +0000 Subject: [PATCH 2/2] Fix warning --- BGL/test/BGL/test_split.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BGL/test/BGL/test_split.cpp b/BGL/test/BGL/test_split.cpp index cddb2130250..f9b6fff3ced 100644 --- a/BGL/test/BGL/test_split.cpp +++ b/BGL/test/BGL/test_split.cpp @@ -25,7 +25,7 @@ typedef std::vector Polyline_2; void insert(const std::vector& poly, Graph& graph, Point_vertex_map& pvmap) { vertex_descriptor u, v; - for (int i = 0; i < poly.size(); i++) { + for (std::size_t i = 0; i < poly.size(); i++) { // check if the point is not yet in the graph if (pvmap.find(poly[i]) == pvmap.end()) { v = add_vertex(graph);