Skip to content

Commit

Permalink
Fix case when we have just one intersection while raycasting outside …
Browse files Browse the repository at this point in the history
…map (#98)
  • Loading branch information
renan028 authored Sep 23, 2024
1 parent f40aa05 commit 2fd1968
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions costmap_2d/plugins/obstacle_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ void ObstacleLayer::raytraceFreespace(const Observation& clearing_observation, d
bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation, double& ox, double& oy, double wx,
double wy) const
{
// copy original sensor origin
const double original_ox = ox;
const double original_oy = oy;

// Define the sensor ray as a linestring (from the sensor origin to the endpoint)
Linestring sensor_ray;
bg::append(sensor_ray, Point(ox, oy));
Expand All @@ -606,22 +610,34 @@ bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation,
// find the intersection between the map and the line defined by the sensor ray
bg::intersection(sensor_ray, map_boundary_, intersection_points);

// the map is a rectangle, so there should be two intersection points
// otherwise, sensor's ray is completely outside the map
if (intersection_points.size() != 2)
// the map is a rectangle, so there should be at least one intersection point
// and at maximum 2 intersection points
if (intersection_points.size() > 2 || intersection_points.size() == 0)
{
return false;
}

// if there is just one intersection point, an object is inside the map
if (intersection_points.size() == 1)
{
const auto& intersection = intersection_points[0];
ox = intersection.x();
oy = intersection.y();

// the obstacle must be farther than the intersection
if (std::hypot(original_ox - wx, original_oy - wy) <= std::hypot(ox - wx, oy - wy))
{
return false;
}
return true;
}

// if there are two intersection points, the raytrace passes through the map
const auto& intersection1 = intersection_points[0];
const auto& intersection2 = intersection_points[1];
double distance1 = bg::distance(Point(ox, oy), intersection1);
double distance2 = bg::distance(Point(ox, oy), intersection2);

// copy original sensor origin
const double original_ox = ox;
const double original_oy = oy;

// Choose the closest intersection point as origin
if (distance1 < distance2)
{
Expand Down

0 comments on commit 2fd1968

Please sign in to comment.