Skip to content

Commit

Permalink
Minor Tweaks to PolygonalRegion (#290)
Browse files Browse the repository at this point in the history
* Minor tweaks to PolygonalRegion.

* Fixed PolygonalRegion points normalization

* Deprecated PolygonalRegion points method.

* Added deprecation test for points.

* Minor tweaks.

* Clarified deprecationTest wrapper message and param.
  • Loading branch information
Eric-Vin authored Jul 29, 2024
1 parent df5595c commit 5f134ad
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
33 changes: 21 additions & 12 deletions src/scenic/core/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2748,7 +2748,7 @@ class PolygonalRegion(Region):

def __init__(
self,
points=None,
points=(),
polygon=None,
z=0,
orientation=None,
Expand All @@ -2759,8 +2759,8 @@ def __init__(
name, points, polygon, z, *additionalDeps, orientation=orientation
)

# Store main parameter
self._points = points
# Normalize and store main parameters
self._points = () if points is None else tuple(points)
self._polygon = polygon
self.z = z

Expand All @@ -2774,7 +2774,6 @@ def __init__(
points = tuple(pt[:2] for pt in points)
if len(points) == 0:
raise ValueError("tried to create PolygonalRegion from empty point list!")
self.points = points
polygon = shapely.geometry.Polygon(points)

if isinstance(polygon, shapely.geometry.Polygon):
Expand All @@ -2791,13 +2790,6 @@ def __init__(
"tried to create PolygonalRegion with " f"invalid polygon {self.polygons}"
)

if (
points is None
and len(self.polygons.geoms) == 1
and len(self.polygons.geoms[0].interiors) == 0
):
self.points = tuple(self.polygons.geoms[0].exterior.coords[:-1])

if self.polygons.is_empty:
raise ValueError("tried to create empty PolygonalRegion")
shapely.prepare(self.polygons)
Expand Down Expand Up @@ -2972,6 +2964,16 @@ def unionAll(regions, buf=0):
z = 0 if z is None else z
return PolygonalRegion(polygon=union, orientation=orientation, z=z)

@property
@distributionFunction
def points(self):
warnings.warn(
"The `points` method is deprecated and will be removed in Scenic 3.3.0."
"Users should use the `boundary` method instead.",
DeprecationWarning,
)
return self.boundary.points

@property
@distributionFunction
def boundary(self) -> "PolylineRegion":
Expand Down Expand Up @@ -3049,7 +3051,14 @@ def __eq__(self, other):

@cached
def __hash__(self):
return hash((self.polygons, self.orientation, self.z))
return hash(
(
self._points,
self._polygon,
self.orientation,
self.z,
)
)


class CircularRegion(PolygonalRegion):
Expand Down
11 changes: 7 additions & 4 deletions src/scenic/simulators/webots/road/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ def computeGeometry(self, crossroads, snapTolerance=0.05):

def show(self, plt):
if self.hasLeftSidewalk:
x, y = zip(*self.leftSidewalk.points)
x, y = zip(*[p[:2] for p in self.leftSidewalk.boundary.points])
plt.fill(x, y, "#A0A0FF")
if self.hasRightSidewalk:
x, y = zip(*self.rightSidewalk.points)
x, y = zip(*[p[:2] for p in self.rightSidewalk.boundary.points])
plt.fill(x, y, "#A0A0FF")
self.region.show(plt, style="r:")
x, y = zip(*self.lanes[0].points)
x, y = zip(*[p[:2] for p in self.lanes[0].boundary.points])
plt.fill(x, y, color=(0.8, 1.0, 0.8))
for lane, markers in enumerate(self.laneMarkers):
x, y = zip(*markers)
Expand Down Expand Up @@ -296,7 +296,10 @@ def __init__(self, world):
allCells = []
drivableAreas = []
for road in self.roads:
assert road.region.polygons.is_valid, (road.waypoints, road.region.points)
assert road.region.polygons.is_valid, (
road.waypoints,
road.region.boundary.points,
)
allCells.extend(road.cells)
for crossroad in self.crossroads:
if crossroad.region is not None:
Expand Down
19 changes: 18 additions & 1 deletion tests/core/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import shapely.geometry
import trimesh.voxel

from scenic.core.distributions import RandomControlFlowError, Range
from scenic.core.object_types import Object, OrientedPoint
from scenic.core.regions import *
from scenic.core.vectors import VectorField
from tests.utils import sampleSceneFrom
from tests.utils import deprecationTest, sampleSceneFrom


def sample_ignoring_rejections(region, num_samples):
Expand Down Expand Up @@ -222,6 +223,14 @@ def test_polygon_region():
PolygonalRegion([(1, 1), (3, 1), (2, 2), (1.3, 1.15)], z=3).uniformPointInner().z
== 3
)
assert i != d
hash(i)
e = CircularRegion((0, 0), Range(1, 3))
with pytest.raises(RandomControlFlowError):
i == e
with pytest.raises(RandomControlFlowError):
e == i
hash(e)


def test_polygon_unionAll():
Expand Down Expand Up @@ -808,3 +817,11 @@ def test_region_combinations(A, B):
# difference()
difference_out = region_a.difference(region_b)
assert isinstance(difference_out, Region)


## Deprecation Tests
@deprecationTest("3.3.0")
def test_polygons_points():
points = ((1, 0, 0), (1, 1, 0), (2, 1, 0), (2, 0, 0))
poly = PolygonalRegion(points)
assert set(poly.points) == set(points)
21 changes: 21 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Utilities used throughout the test suite."""

import functools
from importlib import metadata
import importlib.metadata
import inspect
import math
import multiprocessing
import re
import sys
import types
import weakref
Expand Down Expand Up @@ -576,3 +579,21 @@ def ignorable(attr):
fail()
return False
return True


def deprecationTest(removalVersion):
def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
m_ver = tuple(re.split(r"\D+", removalVersion)[:3])
c_ver = tuple(re.split(r"\D+", importlib.metadata.version("scenic"))[:3])
assert (
m_ver > c_ver
), "Maximum version exceeded. The tested functionality and the test itself should be removed."

with pytest.deprecated_call():
return function(*args, **kwargs)

return wrapper

return decorator

0 comments on commit 5f134ad

Please sign in to comment.