From 3ed68b80ae7b5badbafe540ece9cccb0a74bf9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bartoletti?= Date: Tue, 10 Oct 2023 10:13:08 +0200 Subject: [PATCH] refacto: query visibility parts was almost the same. refactor this and fix test accordingly --- src/algorithm/visibility.cpp | 71 +++++++++++------------ test/unit/SFCGAL/algorithm/Visibility.cpp | 6 +- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/algorithm/visibility.cpp b/src/algorithm/visibility.cpp index 352c1dc3..db12901b 100644 --- a/src/algorithm/visibility.cpp +++ b/src/algorithm/visibility.cpp @@ -27,6 +27,38 @@ using Halfedge_const_handle = Arrangement_2::Halfedge_const_handle; using TEV = CGAL::Triangular_expansion_visibility_2; using PolygonWithHoles = CGAL::Polygon_with_holes_2; + +static auto +query_visibility(Face_handle fh, Halfedge_const_handle he) + -> std::unique_ptr +{ + + std::unique_ptr extRing{new LineString()}; + // Make sure the visibility polygon we find has an outer boundary + if (fh->has_outer_ccb()) { + Arrangement_2::Ccb_halfedge_circulator curr = fh->outer_ccb(); + + // find the right halfedge first + if (he != Halfedge_const_handle()) + while (++curr != fh->outer_ccb()) + if (curr->source()->point() == he->source()->point()) + break; + + Arrangement_2::Ccb_halfedge_circulator first = curr; + extRing->addPoint(Point(curr->source()->point())); + + // Save the points from the visibility polygon + while (++curr != first) { + extRing->addPoint(Point(curr->source()->point())); + } + } + + extRing->closes(); + std::unique_ptr result{new Polygon(extRing.release())}; + + return result; +} + /// /// /// @@ -103,29 +135,8 @@ visibility(const Geometry &polygon, const Geometry &point, NoValidityCheck) // Use the half edge to compute the visibility fh = tev.compute_visibility(queryPoint, he, output_arr); } - // Make sure the visibility polygon we find has an outer boundary - if (fh->has_outer_ccb()) { - Arrangement_2::Ccb_halfedge_circulator curr = fh->outer_ccb(); - - // find the right halfedge first - if (he != Halfedge_const_handle()) - while (++curr != fh->outer_ccb()) - if (curr->source()->point() == he->source()->point()) - break; - - Arrangement_2::Ccb_halfedge_circulator first = curr; - extRing->addPoint(Point(curr->source()->point())); - - // Save the points from the visibility polygon - while (++curr != first) { - extRing->addPoint(Point(curr->source()->point())); - } - } - - extRing->closes(); - std::unique_ptr result{new Polygon(extRing.release())}; - return result; + return query_visibility(fh, he); } /// /// @@ -176,21 +187,7 @@ visibility(const Geometry &polygon, const Geometry &pointA, TEV tev(arr); Face_handle fh = tev.compute_visibility(endPoint, he, output_arr); - // export the visibility region. - Arrangement_2::Ccb_halfedge_circulator curr = fh->outer_ccb(); - std::unique_ptr extRing{new LineString()}; - - // Make sure the visibility polygon we find has an outer boundary - if (fh->has_outer_ccb()) { - extRing->addPoint(Point(curr->target()->point())); - while (++curr != fh->outer_ccb()) { - extRing->addPoint(Point(curr->target()->point())); - } - } - extRing->closes(); - std::unique_ptr result{new Polygon(extRing.release())}; - - return result; + return query_visibility(fh, he); } } // namespace algorithm diff --git a/test/unit/SFCGAL/algorithm/Visibility.cpp b/test/unit/SFCGAL/algorithm/Visibility.cpp index fcd8a2dc..28351616 100644 --- a/test/unit/SFCGAL/algorithm/Visibility.cpp +++ b/test/unit/SFCGAL/algorithm/Visibility.cpp @@ -147,9 +147,9 @@ BOOST_AUTO_TEST_CASE(testVisibility_SegmentInPolygonHole) std::unique_ptr result( algorithm::visibility(poly, startPoint, endPoint)); std::string expectedWkt = - "POLYGON((12.0 6.0,14.0 14.0,10.4 7.6,11.0 " - "7.0,11.0 6.0,10.0 6.0,9.6 6.0,9.0 5.0,1.0 2.0,4.7 " - "2.3,8.0 4.0,10.0 3.0,9.9 2.8,12.0 3.0,19.0 -2.0,12.0 6.0))"; + "POLYGON((19.0 -2.0,12.0 6.0,14.0 14.0,10.4 7.6,11.0 7.0,11.0 6.0,10.0 " + "6.0,9.6 6.0,9.0 5.0,1.0 2.0,4.7 2.3,8.0 4.0,10.0 3.0,9.9 2.8,12.0 " + "3.0,19.0 -2.0))"; BOOST_CHECK_EQUAL(result->asText(1), expectedWkt); } BOOST_AUTO_TEST_SUITE_END()