From b5cf23cc9f9237ceb4cc5f1b1df9c9f2c79f0e6d Mon Sep 17 00:00:00 2001 From: Jonas Perolini Date: Wed, 25 Nov 2026 10:33:13 +0100 Subject: [PATCH] perf(navigator): speed up the contingency geofence check (#30005) Assisted-by: Claude:claude-opus-5-5 Signed-off-by: jonas --- src/modules/navigator/geofence.cpp | 39 ++++++++++++++++++++++++------ src/modules/navigator/geofence.h | 14 +++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/modules/navigator/geofence.cpp b/src/modules/navigator/geofence.cpp index 14d06d0359b..22cb395e262 100644 --- a/src/modules/navigator/geofence.cpp +++ b/src/modules/navigator/geofence.cpp @@ -156,14 +156,17 @@ static bool pathTouchesEdge(const Geofence::PathCheck &path, unsigned pieces, co const matrix::Vector2d shifted_end{edge_end(0), edge_end(1) - shift}; const double margin = static_cast(path.margin); - if (pieces == 1) { - return pieceTouchesEdge(path.start, path_end, shifted_start, shifted_end, margin); - } - // The whole arc's bow is at most pieces^2 * kMaxPieceBow. Expand the latitude bounds by that // distance and the margin to skip distant edges cheaply. Longitude stays between the unwrapped // endpoints because pathPieces() excludes arcs that could reach a pole; only the margin widens it. const double margin_lat = math::degrees((pieces * pieces * kMaxPieceBow + margin) / CONSTANTS_RADIUS_OF_EARTH); + + // Latitude first: it skips most edges without computing the cosine below. + if (math::max(path.start(0), path_end(0)) + margin_lat < math::min(shifted_start(0), shifted_end(0)) + || math::min(path.start(0), path_end(0)) - margin_lat > math::max(shifted_start(0), shifted_end(0))) { + return false; + } + double margin_lon = 0.0; if (margin > 0.0) { @@ -172,13 +175,16 @@ static bool pathTouchesEdge(const Geofence::PathCheck &path, unsigned pieces, co margin_lon = max_lat < 90.0 ? math::degrees(margin / (CONSTANTS_RADIUS_OF_EARTH * cos(math::radians(max_lat)))) : 360.0; } - if (math::max(path.start(0), path_end(0)) + margin_lat < math::min(shifted_start(0), shifted_end(0)) - || math::min(path.start(0), path_end(0)) - margin_lat > math::max(shifted_start(0), shifted_end(0)) - || math::max(path.start(1), path_end(1)) + margin_lon < math::min(shifted_start(1), shifted_end(1)) + if (math::max(path.start(1), path_end(1)) + margin_lon < math::min(shifted_start(1), shifted_end(1)) || math::min(path.start(1), path_end(1)) - margin_lon > math::max(shifted_start(1), shifted_end(1))) { return false; } + // After the bounds, so single pieces skip distant edges too, e.g. the contingency geofence point check. + if (pieces == 1) { + return pieceTouchesEdge(path.start, path_end, shifted_start, shifted_end, margin); + } + // Split along the great circle, approximating the local straight line near the EKF origin. const matrix::Vector3d start_unit = unitVector(path.start); const matrix::Vector3d end_unit = unitVector(path_end); @@ -250,6 +256,9 @@ Geofence::Geofence(Navigator *navigator) : Geofence::~Geofence() { +#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS) + perf_free(_cont_check_perf); +#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS _clearFence(); } @@ -965,6 +974,22 @@ bool Geofence::isCircleInside(double lat, double lon, float radius) return checkPaths(&path, 1, &clear) && clear; } +#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS) +bool Geofence::isInsideContGeofence(double lat, double lon) +{ + const float margin = getContGeofencePlanMargin(); + + if (margin <= FLT_EPSILON) { + return true; + } + + perf_begin(_cont_check_perf); + const bool inside = isCircleInside(lat, lon, margin); + perf_end(_cont_check_perf); + return inside; +} +#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS + bool Geofence::checkPointAgainstPolygonCircle(const PolygonInfo &polygon, double lat, double lon, float altitude) { bool checksPass = true; diff --git a/src/modules/navigator/geofence.h b/src/modules/navigator/geofence.h index a8847cd657f..82354ec84c5 100644 --- a/src/modules/navigator/geofence.h +++ b/src/modules/navigator/geofence.h @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -193,11 +194,11 @@ public: float getContGeofencePlanMargin() const { return getContGeofenceMargin(); } /** True if the position keeps GF_CONT_H_MARGIN from the polygons and circles, or the margin is disabled. */ - bool isInsideContGeofence(double lat, double lon) - { - const float margin = getContGeofencePlanMargin(); - return margin <= FLT_EPSILON || isCircleInside(lat, lon, margin); - } +#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS) + bool isInsideContGeofence(double lat, double lon); +#else + bool isInsideContGeofence(double lat, double lon) { return true; } +#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS bool valid(); @@ -278,6 +279,9 @@ private: DatamanState _error_state{DatamanState::UpdateRequestWait}; DatamanCache _dataman_cache{"geofence_dm_cache_miss", 0}; DatamanClient &_dataman_client = _dataman_cache.client(); +#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS) + perf_counter_t _cont_check_perf {perf_alloc(PC_ELAPSED, "navigator: contingency geofence")}; +#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS float _altitude_min{0.0f}; float _altitude_max{0.0f};