Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/roboticstoolbox/mobile/OccGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,15 @@ def set(self, region: ArrayLike4, value: float | bool) -> None:
:param value: value to set cells to

The cells that contain the corners of the region, and all those between
them, are set.
them, are set. Only cells inside the grid are changed; a region
entirely outside the grid has no effect.
"""
bl = self.w2g([region[0], region[2]])
tr = self.w2g([region[1], region[3]])
self.grid[bl[1] : tr[1] + 1, bl[0] : tr[0] + 1] = value
bl = np.maximum(bl, 0)
tr = np.minimum(tr, np.array(self.grid.shape[::-1]) - 1)
if np.all(bl <= tr):
self.grid[bl[1] : tr[1] + 1, bl[0] : tr[0] + 1] = value

def grid_to_world(self, p: ArrayLike2) -> R2:
"""
Expand Down
31 changes: 31 additions & 0 deletions tests/test_mobile_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,42 @@
import matplotlib.pyplot as plt
import numpy as np
import numpy.testing as nt
import pytest
from spatialmath import Polygon2

from roboticstoolbox import BinaryOccupancyGrid, LandmarkMap, PolygonMap


@pytest.mark.parametrize("cellsize, origin", [(1.0, (0.0, 0.0)), (0.5, (10.0, -3.0))])
@pytest.mark.parametrize(
"bounds",
[
(-2, 2, -1, 1),
(4, 8, 2, 5),
(-5, -3, 1, 2),
(7, 9, 1, 2),
(1, 2, -4, -2),
(1, 2, 5, 7),
],
)
def test_set_region_clips_to_grid(
cellsize: float, origin: tuple[float, float], bounds: tuple[int, int, int, int]
) -> None:
grid = BinaryOccupancyGrid(np.zeros((4, 6)), cellsize=cellsize, origin=origin)
region = np.array(bounds) * cellsize + np.repeat(origin, 2)
columns, rows = np.meshgrid(np.arange(6), np.arange(4))
expected = (
(columns >= bounds[0])
& (columns <= bounds[1])
& (rows >= bounds[2])
& (rows <= bounds[3])
)

grid.set(region, True)

nt.assert_array_equal(grid.grid, expected)


class TestOccupancyGrid(unittest.TestCase):
def test_unknown_argument_is_an_error(self):
# ``size`` used to be silently ignored, the argument is ``cellsize``
Expand Down
Loading