diff --git a/src/lib/geo/geo.cpp b/src/lib/geo/geo.cpp index 2818742ef27..0e8f16bbd72 100644 --- a/src/lib/geo/geo.cpp +++ b/src/lib/geo/geo.cpp @@ -269,9 +269,7 @@ int get_distance_to_arc(struct crosstrack_error_s *crosstrack_error, double lat_ // Determine if the current position is inside or outside the sector between the line from the center // to the arc start and the line from the center to the arc end - float bearing_sector_start = 0.0f; - float bearing_sector_end = 0.0f; - float bearing_now = get_bearing_to_next_waypoint(lat_now, lon_now, lat_center, lon_center); + const float bearing_now = get_bearing_to_next_waypoint(lat_now, lon_now, lat_center, lon_center); int return_value = -1; // Set error flag, cleared when valid result calculated. crosstrack_error->past_end = false; @@ -283,34 +281,14 @@ int get_distance_to_arc(struct crosstrack_error_s *crosstrack_error, double lat_ return return_value; } - if (arc_sweep >= 0.0f) { - bearing_sector_start = arc_start_bearing; - bearing_sector_end = arc_start_bearing + arc_sweep; - - if (bearing_sector_end > 2.0f * M_PI_F) { bearing_sector_end -= (2 * M_PI_F); } - - } else { - bearing_sector_end = arc_start_bearing; - bearing_sector_start = arc_start_bearing - arc_sweep; - - if (bearing_sector_start < 0.0f) { bearing_sector_start += (2 * M_PI_F); } - } - - bool in_sector = false; - - // Case where sector does not span zero - if (bearing_sector_end >= bearing_sector_start && bearing_now >= bearing_sector_start - && bearing_now <= bearing_sector_end) { - - in_sector = true; - } - - // Case where sector does span zero - if (bearing_sector_end < bearing_sector_start && (bearing_now > bearing_sector_start - || bearing_now < bearing_sector_end)) { - - in_sector = true; - } + // arc_start_bearing and arc_sweep are referenced to the center, so test membership with the + // center->vehicle bearing. Measuring it in the direction of the sweep handles either sign and + // any zero crossing without special cases. + // NOLINTNEXTLINE(readability-suspicious-call-argument) intentional reverse bearing: center -> vehicle + const float bearing_center_to_now = get_bearing_to_next_waypoint(lat_center, lon_center, lat_now, lon_now); + const float bearing_from_start = wrap_2pi((bearing_center_to_now - arc_start_bearing) * (arc_sweep < 0.0f ? -1.0f : + 1.0f)); + const bool in_sector = bearing_from_start <= fabsf(arc_sweep); // If in the sector then calculate distance and bearing to closest point if (in_sector) { @@ -339,10 +317,11 @@ int get_distance_to_arc(struct crosstrack_error_s *crosstrack_error, double lat_ double start_disp_y = (double)radius * cos((double)arc_start_bearing); double end_disp_x = (double)radius * sin((double)wrap_pi(arc_start_bearing + arc_sweep)); double end_disp_y = (double)radius * cos((double)wrap_pi(arc_start_bearing + arc_sweep)); - double lon_start = lon_now + start_disp_x / 111111.0; - double lat_start = lat_now + start_disp_y * cos(lat_now) / 111111.0; - double lon_end = lon_now + end_disp_x / 111111.0; - double lat_end = lat_now + end_disp_y * cos(lat_now) / 111111.0; + const double cos_lat_center = cos(math::radians(lat_center)); + double lon_start = lon_center + start_disp_x / (111111.0 * cos_lat_center); + double lat_start = lat_center + start_disp_y / 111111.0; + double lon_end = lon_center + end_disp_x / (111111.0 * cos_lat_center); + double lat_end = lat_center + end_disp_y / 111111.0; float dist_to_start = get_distance_to_next_waypoint(lat_now, lon_now, lat_start, lon_start); float dist_to_end = get_distance_to_next_waypoint(lat_now, lon_now, lat_end, lon_end); diff --git a/src/lib/geo/test_geo.cpp b/src/lib/geo/test_geo.cpp index 0cd3d835aaf..ec2cc906636 100644 --- a/src/lib/geo/test_geo.cpp +++ b/src/lib/geo/test_geo.cpp @@ -214,3 +214,109 @@ TEST_F(GeoTest, waypoint_from_line_and_negative_distance) EXPECT_FLOAT_EQ(lat_start - lat_offset, lat_target); EXPECT_DOUBLE_EQ(lon_start, lon_target); } + +TEST_F(GeoTest, getDistanceToArcOutsideSector) +{ + // GIVEN: an arc at a mid-latitude (so the longitude cosine scaling matters), sweeping from + // due-North of the center (start) to due-East (end). + const double lat_center = 47.0; + const double lon_center = 8.0; + const float radius = 10000.f; // [m] + const float arc_start_bearing = 0.f; // start point due North of the center + const float arc_sweep = M_PI_F / 2.f; // end point due East of the center + const double mpd = 111111.0; + const double cos_lat = cos(math::radians(lat_center)); + + // The implementation locates the endpoints with an equirectangular approximation; mirror it so we + // can predict which endpoint is nearer and at what distance. + auto endpoint = [&](double bearing, double & lat, double & lon) { + lat = lat_center + static_cast(radius) * cos(bearing) / mpd; + lon = lon_center + static_cast(radius) * sin(bearing) / (mpd * cos_lat); + }; + double lat_start, lon_start, lat_end, lon_end; + endpoint(static_cast(arc_start_bearing), lat_start, lon_start); + endpoint(static_cast(arc_start_bearing + arc_sweep), lat_end, lon_end); + + crosstrack_error_s err{}; + double lat_v, lon_v; + + // WHEN: the vehicle is outside the wedge on the start side (0.5 rad before the start bearing) + waypoint_from_heading_and_distance(lat_center, lon_center, arc_start_bearing - 0.5f, radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: the result is the distance to the nearer (start) endpoint, not past the end + EXPECT_LT(get_distance_to_next_waypoint(lat_v, lon_v, lat_start, lon_start), + get_distance_to_next_waypoint(lat_v, lon_v, lat_end, lon_end)); + EXPECT_NEAR(err.distance, get_distance_to_next_waypoint(lat_v, lon_v, lat_start, lon_start), 0.5f); + EXPECT_FALSE(err.past_end); + + // WHEN: the vehicle is outside the wedge on the end side (0.5 rad past the end bearing) + waypoint_from_heading_and_distance(lat_center, lon_center, arc_start_bearing + arc_sweep + 0.5f, radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: the result is the distance to the nearer (end) endpoint, past the end + EXPECT_LT(get_distance_to_next_waypoint(lat_v, lon_v, lat_end, lon_end), + get_distance_to_next_waypoint(lat_v, lon_v, lat_start, lon_start)); + EXPECT_NEAR(err.distance, get_distance_to_next_waypoint(lat_v, lon_v, lat_end, lon_end), 0.5f); + EXPECT_TRUE(err.past_end); +} + +TEST_F(GeoTest, getDistanceToArcInsideSector) +{ + // GIVEN: an arc sweeping from due-North (start) to due-East (end) of the center + const double lat_center = 47.0; + const double lon_center = 8.0; + const float radius = 10000.f; + const float arc_start_bearing = 0.f; + const float arc_sweep = M_PI_F / 2.f; + const float mid_bearing = arc_start_bearing + arc_sweep / 2.f; // pi/4, inside the wedge + + crosstrack_error_s err{}; + double lat_v, lon_v; + + // WHEN: the vehicle is exactly on the arc (on the circle, inside the wedge). This is the case the + // previous in-sector logic mis-classified and reported kilometers off. + waypoint_from_heading_and_distance(lat_center, lon_center, mid_bearing, radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: distance to the arc is ~zero + EXPECT_NEAR(err.distance, 0.f, 1.f); + EXPECT_FALSE(err.past_end); + + // WHEN: the vehicle is inside the circle, within the wedge (half a radius from the center) + waypoint_from_heading_and_distance(lat_center, lon_center, mid_bearing, radius / 2.f, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: the distance is the radial gap to the arc (~radius/2) + EXPECT_NEAR(err.distance, radius / 2.f, 1.f); + EXPECT_FALSE(err.past_end); + + // WHEN: the vehicle is outside the circle, within the wedge (1.5 radius from the center) + waypoint_from_heading_and_distance(lat_center, lon_center, mid_bearing, 1.5f * radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: the distance is again the radial gap (~radius/2) + EXPECT_NEAR(err.distance, radius / 2.f, 1.f); + EXPECT_FALSE(err.past_end); +} + +TEST_F(GeoTest, getDistanceToArcNegativeSweep) +{ + // GIVEN: the same NE-quadrant arc, but defined from due-East (start) sweeping negatively to North (end) + const double lat_center = 47.0; + const double lon_center = 8.0; + const float radius = 10000.f; + const float arc_start_bearing = M_PI_F / 2.f; + const float arc_sweep = -M_PI_F / 2.f; + + crosstrack_error_s err{}; + double lat_v, lon_v; + + // WHEN: the vehicle is on the arc midpoint (bearing pi/4 from the center) -> in sector + waypoint_from_heading_and_distance(lat_center, lon_center, M_PI_F / 4.f, radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: distance to the arc is ~zero + EXPECT_NEAR(err.distance, 0.f, 1.f); + EXPECT_FALSE(err.past_end); + + // WHEN: the vehicle is due South of the center, far outside the wedge -> out of sector + waypoint_from_heading_and_distance(lat_center, lon_center, M_PI_F, radius, &lat_v, &lon_v); + get_distance_to_arc(&err, lat_v, lon_v, lat_center, lon_center, radius, arc_start_bearing, arc_sweep); + // THEN: it is NOT treated as on the arc (the old negative-sweep sector math returned ~zero here) + EXPECT_GT(err.distance, radius); +}