larvaworld.lib.util.shapely_aux

Methods for managing shapely-based metrics

Functions

segments_intersection(→ shapely.geometry.Point | None)

Compute intersection point of two line segments.

detect_nearest_obstacle(→ tuple[float | None, Any])

Find nearest obstacle intersected by a sensor ray.

Module Contents

larvaworld.lib.util.shapely_aux.segments_intersection(segment_1: tuple[shapely.geometry.Point, shapely.geometry.Point], segment_2: tuple[shapely.geometry.Point, shapely.geometry.Point]) shapely.geometry.Point | None

Compute intersection point of two line segments.

Determines if two line segments intersect and returns the intersection point if they do, using coordinate-based calculation.

Args:

segment_1: First segment as (start_point, end_point) segment_2: Second segment as (start_point, end_point)

Returns:

Intersection Point if segments intersect, None otherwise

Example:
>>> from shapely.geometry import Point
>>> seg1 = (Point(0, 0), Point(2, 2))
>>> seg2 = (Point(0, 2), Point(2, 0))
>>> p = segments_intersection(seg1, seg2)
>>> p  # Point at (1, 1)
larvaworld.lib.util.shapely_aux.detect_nearest_obstacle(obstacles: list[Any], sensor_ray: tuple[shapely.geometry.Point, shapely.geometry.Point], p0: shapely.geometry.Point) tuple[float | None, Any]

Find nearest obstacle intersected by a sensor ray.

Checks all obstacle edges for intersections with sensor ray and returns the closest obstacle and its distance from the sensor origin point.

Args:

obstacles: List of obstacle objects with .edges attribute sensor_ray: Ray segment as (start_point, end_point) p0: Sensor origin point for distance calculation

Returns:

Tuple of (distance, obstacle) where distance is to nearest intersection, or (None, None) if no intersections found

Example:
>>> from shapely.geometry import Point
>>> ray = (Point(0, 0), Point(10, 0))
>>> dist, obs = detect_nearest_obstacle(obstacles, ray, Point(0, 0))