From c40581042c8bb3f03adaf15f98e7392ff48922d7 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 20 Nov 2026 10:32:35 +0100 Subject: [PATCH] refactor(navigator): name the piece start in the fence distance check pieceTouchesEdge() passed the start of the piece as three unnamed zero vectors. It is now one named point, with a comment that says what a to d are. That also takes 32 B off the stack frame of the function (168 to 136 B for fmu-v6c with GCC 9). isCircleInside() builds its path in one line. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: jonas --- src/modules/navigator/geofence.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/modules/navigator/geofence.cpp b/src/modules/navigator/geofence.cpp index 4b8da9e121a..92361c1cc99 100644 --- a/src/modules/navigator/geofence.cpp +++ b/src/modules/navigator/geofence.cpp @@ -125,15 +125,17 @@ static bool pieceTouchesEdge(const matrix::Vector2d &piece_start, const matrix:: const double north_scale = math::radians(1.0) * CONSTANTS_RADIUS_OF_EARTH; const double max_lat = math::radians(math::max(fabs(piece_start(0)), fabs(piece_end(0)))); const double east_scale = north_scale * cos(math::min(max_lat + margin / CONSTANTS_RADIUS_OF_EARTH, kHalfPi)); + // In metres north and east of the piece start: the piece runs from a to b, the edge from c to d. + const matrix::Vector2d a{}; const matrix::Vector2d b{(piece_end(0) - piece_start(0)) *north_scale, (piece_end(1) - piece_start(1)) *east_scale}; const matrix::Vector2d c{(edge_start(0) - piece_start(0)) *north_scale, (edge_start(1) - piece_start(1)) *east_scale}; const matrix::Vector2d d{(edge_end(0) - piece_start(0)) *north_scale, (edge_end(1) - piece_start(1)) *east_scale}; const double margin_squared = margin * margin; - return geofence_utils::pointToSegmentDistanceSquared(matrix::Vector2d {}, c, d) <= margin_squared + return geofence_utils::pointToSegmentDistanceSquared(a, c, d) <= margin_squared || geofence_utils::pointToSegmentDistanceSquared(b, c, d) <= margin_squared - || geofence_utils::pointToSegmentDistanceSquared(c, matrix::Vector2d {}, b) <= margin_squared - || geofence_utils::pointToSegmentDistanceSquared(d, matrix::Vector2d {}, b) <= margin_squared; + || geofence_utils::pointToSegmentDistanceSquared(c, a, b) <= margin_squared + || geofence_utils::pointToSegmentDistanceSquared(d, a, b) <= margin_squared; } // Whether a path crosses a fence edge or comes within its margin of it. @@ -957,10 +959,7 @@ bool Geofence::isCircleInside(double lat, double lon, float radius) } // A path without length that keeps the radius as its margin. - PathCheck path{}; - path.start = matrix::Vector2d{lat, lon}; - path.end = path.start; - path.margin = radius; + const PathCheck path{{lat, lon}, {lat, lon}, 0.f, radius}; bool clear = false; return checkPaths(&path, 1, &clear) && clear; }