Skip to content

Commit 86d07f9

Browse files
fix(boundary_departure): invalid swapped points
Signed-off-by: Zulfaqar Azmi <zulfaqar.azmi@tier4.jp>
1 parent d1c1ebe commit 86d07f9

File tree

2 files changed

+21
-36
lines changed
  • common/autoware_boundary_departure_checker

2 files changed

+21
-36
lines changed

common/autoware_boundary_departure_checker/include/autoware/boundary_departure_checker/utils.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,13 @@ UncrossableBoundRTree build_uncrossable_boundaries_rtree(
257257
* If it is within the segment's bounds, it returns the projection point. If it's outside,
258258
* an error is returned indicating the relative position.
259259
*
260-
* The `swap_points` flag controls the order of the returned pair, useful when projection
261-
* direction matters (e.g., from ego to boundary vs. from boundary to ego).
262-
*
263260
* @param p The point to be projected.
264261
* @param segment The line segment to project onto.
265-
* @param swap_points Whether to swap the order of the original and projected point in the result.
266262
* @return A tuple containing the original point, projection point, and the distance between them,
267263
* or an error message if the point lies outside the segment.
268264
*/
269-
tl::expected<std::tuple<Point2d, Point2d, double>, std::string> point_to_segment_projection(
270-
const Point2d & p, const Segment2d & segment, const bool swap_points = false);
265+
tl::expected<std::pair<Point2d, double>, std::string> point_to_segment_projection(
266+
const Point2d & p, const Segment2d & segment);
271267

272268
/**
273269
* @brief Computes the nearest projection between two segments, used to assess lateral distance.

common/autoware_boundary_departure_checker/src/utils.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -418,35 +418,25 @@ UncrossableBoundRTree build_uncrossable_boundaries_rtree(
418418
return {segments.begin(), segments.end()};
419419
}
420420

421-
tl::expected<std::tuple<Point2d, Point2d, double>, std::string> point_to_segment_projection(
422-
const Point2d & p, const Segment2d & segment, const bool swap_points)
421+
tl::expected<std::pair<Point2d, double>, std::string> point_to_segment_projection(
422+
const Point2d & p, const Segment2d & segment)
423423
{
424424
const auto & p1 = segment.first;
425425
const auto & p2 = segment.second;
426426

427427
const Point2d p2_vec = {p2.x() - p1.x(), p2.y() - p1.y()};
428428
const Point2d p_vec = {p.x() - p1.x(), p.y() - p1.y()};
429429

430-
const auto result = [&swap_points](
431-
const Point2d & orig,
432-
const Point2d & proj) -> std::tuple<Point2d, Point2d, double> {
433-
return swap_points ? std::make_tuple(proj, orig, boost::geometry::distance(proj, orig))
434-
: std::make_tuple(orig, proj, boost::geometry::distance(orig, proj));
435-
};
436-
437430
const auto c1 = boost::geometry::dot_product(p_vec, p2_vec);
438431
if (c1 < 0.0) return tl::make_unexpected("Point before segment start");
439-
if (c1 == 0.0) return result(p, p1);
440432

441433
const auto c2 = boost::geometry::dot_product(p2_vec, p2_vec);
442434
if (c1 > c2) return tl::make_unexpected("Point after segment end");
443435

444-
if (c1 == c2) return result(p, p2);
445-
446436
const auto projection = p1 + (p2_vec * c1 / c2);
447437
const auto projection_point = Point2d{projection.x(), projection.y()};
448438

449-
return result(p, projection_point);
439+
return std::make_pair(projection_point, boost::geometry::distance(p, projection_point));
450440
}
451441

452442
tl::expected<ProjectionToBound, std::string> segment_to_segment_nearest_projection(
@@ -465,29 +455,28 @@ tl::expected<ProjectionToBound, std::string> segment_to_segment_nearest_projecti
465455

466456
ProjectionsToBound projections;
467457
projections.reserve(4);
468-
constexpr bool swap_result = true;
469-
if (const auto projection_opt = point_to_segment_projection(ego_f, lane_seg, swap_result)) {
470-
const auto & [pt_ego, pt_lane, dist] = *projection_opt;
471-
const auto lon_offset = boost::geometry::distance(pt_ego, ego_f);
472-
projections.emplace_back(pt_ego, pt_lane, lane_seg, dist, lon_offset, ego_sides_idx);
458+
if (const auto projection_opt = point_to_segment_projection(ego_f, lane_seg)) {
459+
const auto & [proj, dist] = *projection_opt;
460+
constexpr auto lon_offset = 0.0;
461+
projections.emplace_back(ego_f, proj, lane_seg, dist, lon_offset, ego_sides_idx);
473462
}
474463

475-
if (const auto projection_opt = point_to_segment_projection(ego_b, lane_seg, swap_result)) {
476-
const auto & [pt_ego, pt_lane, dist] = *projection_opt;
477-
const auto lon_offset = boost::geometry::distance(pt_ego, ego_f);
478-
projections.emplace_back(pt_ego, pt_lane, lane_seg, dist, lon_offset, ego_sides_idx);
464+
if (const auto projection_opt = point_to_segment_projection(ego_b, lane_seg)) {
465+
const auto & [proj, dist] = *projection_opt;
466+
const auto lon_offset = boost::geometry::distance(ego_b, ego_f);
467+
projections.emplace_back(ego_b, proj, lane_seg, dist, lon_offset, ego_sides_idx);
479468
}
480469

481-
if (const auto projection_opt = point_to_segment_projection(lane_pt1, ego_seg, !swap_result)) {
482-
const auto & [pt_ego, pt_lane, dist] = *projection_opt;
483-
const auto lon_offset = boost::geometry::distance(pt_ego, ego_f);
484-
projections.emplace_back(pt_ego, pt_lane, lane_seg, dist, lon_offset, ego_sides_idx);
470+
if (const auto projection_opt = point_to_segment_projection(lane_pt1, ego_seg)) {
471+
const auto & [proj, dist] = *projection_opt;
472+
const auto lon_offset = boost::geometry::distance(proj, ego_f);
473+
projections.emplace_back(proj, lane_pt1, lane_seg, dist, lon_offset, ego_sides_idx);
485474
}
486475

487-
if (const auto projection_opt = point_to_segment_projection(lane_pt2, ego_seg, !swap_result)) {
488-
const auto & [pt_ego, pt_lane, dist] = *projection_opt;
489-
const auto lon_offset = boost::geometry::distance(pt_ego, ego_f);
490-
projections.emplace_back(pt_ego, pt_lane, lane_seg, dist, lon_offset, ego_sides_idx);
476+
if (const auto projection_opt = point_to_segment_projection(lane_pt2, ego_seg)) {
477+
const auto & [proj, dist] = *projection_opt;
478+
const auto lon_offset = boost::geometry::distance(proj, ego_f);
479+
projections.emplace_back(proj, lane_pt2, lane_seg, dist, lon_offset, ego_sides_idx);
491480
}
492481

493482
if (projections.empty())

0 commit comments

Comments
 (0)