diff --git a/src/lib/geofence/geofence_utils.cpp b/src/lib/geofence/geofence_utils.cpp index 87bb94f740..3e8b6efbee 100644 --- a/src/lib/geofence/geofence_utils.cpp +++ b/src/lib/geofence/geofence_utils.cpp @@ -104,14 +104,9 @@ bool segmentsIntersect(const matrix::Vector2f &p1, const matrix::Vector2f &p2, } bool lineSegmentIntersectsPolygon(const matrix::Vector2f &start, const matrix::Vector2f &end, - const matrix::Vector2f *vertices, int num_vertices, bool flipped) + const matrix::Vector2f *vertices, int num_vertices, bool is_inclusion_zone) { - // If flipped, consider the outside to be the interior of the polygon. - - // For exclusion zones, set !flipped. Results in lines completely outside being non-intersecting (allowed) and inside being intersecting (disallowed). - // For inclusion zones, set flipped. Results in lines completely ouside being intersecting (disallowed) and inside being non-intersecting (allowed). - // First pass: If there is a proper intersection (open line segment // start-end with closed line segment between vertices), it is // intersecting. @@ -127,7 +122,9 @@ bool lineSegmentIntersectsPolygon(const matrix::Vector2f &start, const matrix::V // Remaining cases: The line is either completely outside or completely inside. const bool midpoint_inside = insidePolygon(vertices, num_vertices, 0.5f * (start + end)); - return midpoint_inside == flipped; + // Inclusion zone: inside -> no intersection, outside -> intersection + // Exclusion zone: inside -> intersection, outside -> no intersection + return midpoint_inside != is_inclusion_zone; } bool lineSegmentIntersectsCircle(const matrix::Vector2f &start, const matrix::Vector2f &end, diff --git a/src/lib/geofence/geofence_utils.h b/src/lib/geofence/geofence_utils.h index 6a490d5eda..54ccae8dc7 100644 --- a/src/lib/geofence/geofence_utils.h +++ b/src/lib/geofence/geofence_utils.h @@ -84,9 +84,11 @@ bool insideCircle(const matrix::Vector2 ¢er, float radius, const matrix::Vector2 &point); /** - * Check if two line segments intersect (excluding endpoints). + * Check if two line segments intersect (excluding endpoints*). * Works in local Cartesian coordinates (meters). * + * * TODO write correctly - see inside + * * @param p1 first segment start in local frame * @param p2 first segment end in local frame * @param v1 second segment start in local frame @@ -97,17 +99,19 @@ bool segmentsIntersect(const matrix::Vector2f &p1, const matrix::Vector2f &p2, const matrix::Vector2f &v1, const matrix::Vector2f &v2); /** - * Check if a line segment intersects any edge of a polygon (excluding endpoints). + * Check if a line segment and a polygon have non-empty intersection. * Works in local Cartesian coordinates (meters). * - * @param start segment start in local frame - * @param end segment end in local frame - * @param vertices polygon vertices in local frame - * @param num_vertices number of vertices - * @return true if the segment intersects any polygon edge + * @param start segment start in local frame + * @param end segment end in local frame + * @param vertices polygon vertices in local frame + * @param num_vertices number of vertices + * @param is_inclusion_zone If true, consider completely outside lines intersecting, inside non-intersecting. If false the other way around. + * + * @return true if the segment intersects the disallowed part of the polygon */ bool lineSegmentIntersectsPolygon(const matrix::Vector2f &start, const matrix::Vector2f &end, - const matrix::Vector2f *vertices, int num_vertices); + const matrix::Vector2f *vertices, int num_vertices, bool is_inclusion_zone); /** * Check if a line segment intersects a circle.