Skip to content

Commit

Permalink
Merge pull request #466 from eyal0/single_path_finder
Browse files Browse the repository at this point in the history
Single path finder
  • Loading branch information
eyal0 committed Jul 3, 2020
2 parents a3a2475 + 0172b81 commit c0995cf
Show file tree
Hide file tree
Showing 29 changed files with 3,439 additions and 3,352 deletions.
26 changes: 23 additions & 3 deletions bg_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,33 @@ multi_polygon_type_fp buffer(multi_polygon_type_fp const & geometry_in, coordina
}
}

multi_polygon_type_fp buffer_miter(multi_polygon_type_fp const & geometry_in, coordinate_type_fp expand_by) {
if (expand_by == 0) {
return geometry_in;
} else {
multi_polygon_type_fp geometry_out;
bg::buffer(geometry_in, geometry_out,
bg::strategy::buffer::distance_symmetric<coordinate_type_fp>(expand_by),
bg::strategy::buffer::side_straight(),
bg::strategy::buffer::join_miter(expand_by),
bg::strategy::buffer::end_round(points_per_circle),
bg::strategy::buffer::point_circle(points_per_circle));
return geometry_out;
}
}

template<typename CoordinateType>
multi_polygon_type_fp buffer(polygon_type_fp const & geometry_in, CoordinateType expand_by) {
return buffer(multi_polygon_type_fp{geometry_in}, expand_by);
}

template multi_polygon_type_fp buffer(polygon_type_fp const&, double);

template<typename CoordinateType>
multi_polygon_type_fp buffer_miter(polygon_type_fp const & geometry_in, CoordinateType expand_by) {
return buffer_miter(multi_polygon_type_fp{geometry_in}, expand_by);
}

template<typename CoordinateType>
multi_polygon_type_fp buffer(linestring_type_fp const & geometry_in, CoordinateType expand_by) {
if (expand_by == 0) {
Expand Down Expand Up @@ -93,10 +113,10 @@ multi_polygon_type_fp buffer(multi_linestring_type_fp const & geometry_in, Coord
template multi_polygon_type_fp buffer(const multi_linestring_type_fp&, double expand_by);

template<typename CoordinateType>
multi_polygon_type_fp buffer(ring_type_fp const & geometry_in, CoordinateType expand_by) {
return buffer(polygon_type_fp{geometry_in}, expand_by);
multi_polygon_type_fp buffer_miter(ring_type_fp const & geometry_in, CoordinateType expand_by) {
return buffer_miter(polygon_type_fp{geometry_in}, expand_by);
}

template multi_polygon_type_fp buffer(ring_type_fp const&, double);
template multi_polygon_type_fp buffer_miter(ring_type_fp const&, double);

} // namespace bg_helpers
5 changes: 5 additions & 0 deletions bg_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace bg_helpers {
// 0, unlike bg::buffer.
multi_polygon_type_fp buffer(multi_polygon_type_fp const & geometry_in, coordinate_type_fp expand_by);

multi_polygon_type_fp buffer_miter(multi_polygon_type_fp const & geometry_in, coordinate_type_fp expand_by);

template<typename CoordinateType>
multi_polygon_type_fp buffer(polygon_type_fp const & geometry_in, CoordinateType expand_by);

Expand All @@ -27,6 +29,9 @@ multi_polygon_type_fp buffer(multi_linestring_type_fp const & geometry_in, Coord
template<typename CoordinateType>
multi_polygon_type_fp buffer(ring_type_fp const & geometry_in, CoordinateType expand_by);

template<typename CoordinateType>
multi_polygon_type_fp buffer_miter(ring_type_fp const & geometry_in, CoordinateType expand_by);

} // namespace bg_helpers

#endif //BG_HELPERS_HPP
22 changes: 8 additions & 14 deletions path_finding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ using std::make_shared;
class PathFindingSurface {
public:
PathFindingSurface(const multi_polygon_type_fp& keep_in,
const optional<multi_polygon_type_fp>& keep_out,
const coordinate_type_fp tolerance) {
const multi_polygon_type_fp& keep_out,
coordinate_type_fp tolerance) {
multi_polygon_type_fp total_keep_in;
total_keep_in = keep_in;
// By here, total_keep_in_grown is ready.
if (keep_out) {
total_keep_in = total_keep_in - *keep_out;
}
total_keep_in = total_keep_in - keep_out;

all_vertices.clear();
for (const auto& poly : total_keep_in) {
for (const auto& point : poly.outer()) {
all_vertices.push_back(point);
Expand All @@ -46,6 +42,7 @@ class PathFindingSurface {
}
}
}
total_keep_in_grown = bg_helpers::buffer_miter(total_keep_in, tolerance);

sort(all_vertices.begin(),
all_vertices.end(),
Expand All @@ -55,12 +52,10 @@ class PathFindingSurface {
all_vertices.erase(
std::unique(all_vertices.begin(),
all_vertices.end(),
[](const point_type_fp& a, const point_type_fp& b) {
return std::tie(a.x(), a.y()) == std::tie(b.x(), b.y());
}),
[](const point_type_fp& a, const point_type_fp& b) {
return std::tie(a.x(), a.y()) == std::tie(b.x(), b.y());
}),
all_vertices.end());

total_keep_in_grown = bg_helpers::buffer(total_keep_in, tolerance);
}
multi_polygon_type_fp total_keep_in_grown;
vector<point_type_fp> all_vertices;
Expand Down Expand Up @@ -102,7 +97,6 @@ class PathSurface {
PathSurface(const std::shared_ptr<const PathFindingSurface>& base, const point_type_fp begin, const point_type_fp end,
PathLimiter path_limiter)
: path_limiter(path_limiter) {
total_keep_in_grown.clear();
for (const auto& poly : base->total_keep_in_grown) {
if (bg::covered_by(begin, poly) && bg::covered_by(end, poly)) {
total_keep_in_grown.push_back(poly);
Expand Down Expand Up @@ -362,7 +356,7 @@ struct AstarGoalVisitor : public boost::default_astar_visitor {

const std::shared_ptr<const PathFindingSurface> create_path_finding_surface(
const multi_polygon_type_fp& keep_in,
const boost::optional<multi_polygon_type_fp>& keep_out,
const multi_polygon_type_fp& keep_out,
const coordinate_type_fp tolerance) {
boost::function_requires<boost::VertexListGraphConcept<PathSurface>>();
boost::function_requires<boost::IncidenceGraphConcept<PathSurface>>();
Expand Down
2 changes: 1 addition & 1 deletion path_finding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef std::function<bool(const point_type_fp& target, const coordinate_type_fp
// small epsilon value.
const std::shared_ptr<const PathFindingSurface> create_path_finding_surface(
const multi_polygon_type_fp& keep_in,
const boost::optional<multi_polygon_type_fp>& keep_out,
const multi_polygon_type_fp& keep_out,
const coordinate_type_fp tolerance);


Expand Down
10 changes: 5 additions & 5 deletions path_finding_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BOOST_AUTO_TEST_CASE(simple) {
bg::expand(bounding_box, point_type_fp(100, 100));
multi_polygon_type_fp keep_in;
bg::convert(bounding_box, keep_in);
auto surface = create_path_finding_surface(keep_in, boost::none, 0.1);
auto surface = create_path_finding_surface(keep_in, multi_polygon_type_fp(), 0.1);
auto ret = find_path(surface, point_type_fp(0,0), point_type_fp(1,1), nullptr);
linestring_type_fp expected;
expected.push_back(point_type_fp(0, 0));
Expand All @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(box) {
bg::expand(bounding_box, point_type_fp(100, 100));
multi_polygon_type_fp keep_in;
bg::convert(bounding_box, keep_in);
auto surface = create_path_finding_surface(keep_in, boost::make_optional(keep_out), 0.1);
auto surface = create_path_finding_surface(keep_in, keep_out, 0.1);
auto ret = find_path(surface, point_type_fp(0,0), point_type_fp(10,10), nullptr);

linestring_type_fp expected;
Expand All @@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(unreachable_box) {
bg::expand(bounding_box, point_type_fp(100, 100));
multi_polygon_type_fp keep_in;
bg::convert(bounding_box, keep_in);
auto surface = create_path_finding_surface(keep_in, boost::make_optional(keep_out), 0.1);
auto surface = create_path_finding_surface(keep_in, keep_out, 0.1);
auto ret = find_path(surface, point_type_fp(0,0), point_type_fp(5,5), nullptr);

BOOST_CHECK_EQUAL(ret, boost::none);
Expand All @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(reuse_surface) {
bg::expand(bounding_box, point_type_fp(100, 100));
multi_polygon_type_fp keep_in;
bg::convert(bounding_box, keep_in);
auto surface = create_path_finding_surface(keep_in, boost::make_optional(keep_out), 0.1);
auto surface = create_path_finding_surface(keep_in, keep_out, 0.1);
auto ret = find_path(surface, point_type_fp(0,0), point_type_fp(5,5), nullptr);
BOOST_CHECK_EQUAL(ret, boost::none);

Expand All @@ -119,7 +119,7 @@ BOOST_AUTO_TEST_CASE(u_shape) {
poly.outer() = u_shape;
multi_polygon_type_fp keep_in;
keep_in.push_back(poly);
auto surface = create_path_finding_surface(keep_in, boost::none, 0.1);
auto surface = create_path_finding_surface(keep_in, multi_polygon_type_fp(), 0.1);
auto ret = find_path(surface, point_type_fp(1,9), point_type_fp(9,9), nullptr);

linestring_type_fp expected;
Expand Down
Loading

0 comments on commit c0995cf

Please sign in to comment.