Skip to content

Commit

Permalink
refacto: query visibility parts was almost the same. refactor this an…
Browse files Browse the repository at this point in the history
…d fix test accordingly
  • Loading branch information
lbartoletti committed Oct 10, 2023
1 parent f10e9fa commit 3ed68b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
71 changes: 34 additions & 37 deletions src/algorithm/visibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ using Halfedge_const_handle = Arrangement_2::Halfedge_const_handle;
using TEV =
CGAL::Triangular_expansion_visibility_2<Arrangement_2, CGAL::Tag_true>;
using PolygonWithHoles = CGAL::Polygon_with_holes_2<Kernel>;

static auto
query_visibility(Face_handle fh, Halfedge_const_handle he)
-> std::unique_ptr<Polygon>
{

std::unique_ptr<LineString> 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<Polygon> result{new Polygon(extRing.release())};

return result;
}

///
///
///
Expand Down Expand Up @@ -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<Polygon> result{new Polygon(extRing.release())};

return result;
return query_visibility(fh, he);
}
///
///
Expand Down Expand Up @@ -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<LineString> 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<Polygon> result{new Polygon(extRing.release())};

return result;
return query_visibility(fh, he);
}

} // namespace algorithm
Expand Down
6 changes: 3 additions & 3 deletions test/unit/SFCGAL/algorithm/Visibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ BOOST_AUTO_TEST_CASE(testVisibility_SegmentInPolygonHole)
std::unique_ptr<Polygon> 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()

0 comments on commit 3ed68b8

Please sign in to comment.