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 <jonspero@me.com>
This commit is contained in:
jonas committed 2026-11-20 10:32:35 +01:00
1 parent 200d5ddc18
commit c40581042c
1 file changed
+6 -7
+6 -7
View File
@@ -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;
}