irsim.lib.algorithm.ray_casting_2d#
Vectorized 2D ray casting against simulation objects.
Shared by the lidar sensors (Lidar2D and
FMCWLidar2D). The high-level
cast_rays() function flattens already-detected object boundaries and
returns the nearest object hit by every beam. The lower-level
cast_ray_segments() function performs the vectorized numerical kernel.
For a sensor origin in free space, this reproduces a GEOS difference scan
to floating-point precision while avoiding the expensive overlay. FMCW exit
hits also remain compatible when the origin is inside an obstacle.
Typical use:
ranges, hit_object_indices, origin, directions = cast_rays(
lidar_geometry, detected_objects, range_max
)
Attributes#
Functions#
|
Flatten a geometry's boundary into |
|
Nearest ray-segment hit distance and segment index per ray (vectorized). |
|
Cast a 2D lidar geometry against already-detected objects. |
Module Contents#
- irsim.lib.algorithm.ray_casting_2d.ORIGIN_EPS = 1e-09#
- irsim.lib.algorithm.ray_casting_2d.SEGMENT_CHUNK_SIZE = 1024#
- irsim.lib.algorithm.ray_casting_2d.boundary_segments(geometry)[源代码]#
Flatten a geometry's boundary into
(start, end)segment arrays.Handles every obstacle geometry a lidar may encounter: polygons (with holes), rectangles, circles (buffer polygons), linestrings, map segments, and compound
MultiPolygon/GeometryCollectionshapes. Points and empty geometries contribute no edges.Polygonal parts are reduced to their ring linestrings with
shapely.boundary()(which covers the outer ring and any holes); linestrings are used directly. Noteshapely.boundaryof a linestring returns its endpoints, not its segments, so it must not be applied to line obstacles.- 参数:
geometry -- Any Shapely geometry.
- 返回:
(start, end)endpoint arrays, each of shape(M, 2)((0, 2)when the geometry has no edges).- 返回类型:
tuple[np.ndarray, np.ndarray]
- irsim.lib.algorithm.ray_casting_2d.cast_ray_segments(origin: numpy.ndarray, directions: numpy.ndarray, seg_start: numpy.ndarray, seg_end: numpy.ndarray, max_range: float) tuple[numpy.ndarray, numpy.ndarray][源代码]#
Nearest ray-segment hit distance and segment index per ray (vectorized).
Solves, for every ray
origin + t * directionagainst every segmentseg_start + u * (seg_end - seg_start), the intersection in vectorized segment blocks and keeps the nearest valid hit (0 < t <= max_range,0 <= u <= 1) per ray. Blocking bounds peak matrix memory bySEGMENT_CHUNK_SIZE * number_of_rays. Collinear overlaps are handled separately because their standard intersection denominator is zero.- 参数:
origin (np.ndarray) -- Shared ray origin
(2,).directions (np.ndarray) -- Unit ray directions
(N, 2).seg_start (np.ndarray) -- Segment start points
(M, 2).seg_end (np.ndarray) -- Segment end points
(M, 2).max_range (float) -- Maximum ray length; misses return this.
- 返回:
ranges(N,)clamped tomax_range, andhit_index(N,)giving the index into the segment arrays that each ray hit (-1on a miss).- 返回类型:
tuple[np.ndarray, np.ndarray]
- irsim.lib.algorithm.ray_casting_2d.cast_rays(lidar_geometry, detected_objects, max_range: float) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray][源代码]#
Cast a 2D lidar geometry against already-detected objects.
This geometry-only operation derives ray parameters, gathers boundary segments from the supplied objects, runs the numerical kernel, and maps segment hits back to indices in
detected_objects. Scene lookup remains the sensor's responsibility.- 参数:
lidar_geometry -- Max-range beams in world coordinates as a Shapely multiline geometry.
detected_objects -- Objects selected by the sensor's scene query.
max_range -- Maximum ray length; misses return this value.
- 返回:
Ranges, indices into
detected_objects, origin, and directions.- 返回类型:
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]