Skip to content

Commit

Permalink
Merge pull request #1693 from AisenGinn/bubble_shape_new
Browse files Browse the repository at this point in the history
* changing state_unpacker.js to display bubble, added ConfigurableZone into bubble

* changed roadmap argument to optional.

* added check for parallel line and number of points check.

* added changelog for ConfigurableZone.

* slightly modified changelog.

* modified descriptions of ConfigurableZone and simplied polygon check.

* added traffic_singals into state_unpacker.js and modified condition of valid polygon check.

* added additional len check for configurable bubble.

* modified changelog.

* added detail description of bubble that does not have a valid closed loop zone.
  • Loading branch information
Gamenot authored Nov 23, 2022
2 parents 6e4d571 + 87d438d commit 4688ba3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Copy and pasting the git commit messages is __NOT__ enough.
### Added
- Added single vehicle `Trip` into type.
- Added new video record ultility using moviepy.
- Added `ConfigurableZone` for `Zone` object to types which enable users to build bubble by providing coordinates of the polygon.
### Deprecated
### Changed
### Removed
Expand Down
7 changes: 4 additions & 3 deletions envision/web/src/helpers/state_unpacker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const WorldState = Object.freeze({
SCENARIO_ID: 1,
SCENARIO_NAME: 2,
TRAFFIC: 3,
BUBBLES: 4,
SCORES: 5,
EGO_AGENT_IDS: 6,
TRAFFIC_SIGNALS: 4,
BUBBLES: 5,
SCORES: 6,
EGO_AGENT_IDS: 7,
});

const Traffic = Object.freeze({
Expand Down
68 changes: 66 additions & 2 deletions smarts/sstudio/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from dataclasses import dataclass, field
from enum import IntEnum
from sys import maxsize
from typing import Any, Callable, Dict, NewType, Optional, Sequence, Tuple, Union
from typing import Any, Callable, Dict, NewType, Optional, Sequence, Tuple, Union, List

import numpy as np
from shapely.affinity import rotate as shapely_rotate
Expand All @@ -38,6 +38,7 @@
MultiPolygon,
Point,
Polygon,
box,
)
from shapely.ops import split, unary_union

Expand Down Expand Up @@ -691,7 +692,7 @@ class GroupedLapMission:
class Zone:
"""The base for a descriptor that defines a capture area."""

def to_geometry(self, road_map: RoadMap) -> Polygon:
def to_geometry(self, road_map: Optional[RoadMap] = None) -> Polygon:
"""Generates the geometry from this zone."""
raise NotImplementedError

Expand Down Expand Up @@ -852,6 +853,55 @@ def to_geometry(self, road_map: Optional[RoadMap] = None) -> Polygon:
return shapely_translate(poly, xoff=x, yoff=y)


@dataclass(frozen=True)
class ConfigurableZone(Zone):
"""A descriptor for a zone with user-defined geometry."""

ext_coordinates: List[Tuple[float, float]]
"""external coordinates of the polygon
< 2 points provided: error
= 2 points provided: generates a box using these two points as diagonal
> 2 points provided: generates a polygon according to the coordinates"""
rotation: Optional[float] = None
"""The heading direction of the bubble(radians, clock-wise rotation)"""

def __post_init__(self):
if (
not self.ext_coordinates
or len(self.ext_coordinates) < 2
or not isinstance(self.ext_coordinates[0], tuple)
):
raise ValueError(
"Two points or more are needed to create a polygon. (less than two points are provided)"
)

x_set = set(point[0] for point in self.ext_coordinates)
y_set = set(point[1] for point in self.ext_coordinates)
if len(x_set) == 1 or len(y_set) == 1:
raise ValueError(
"Parallel line cannot form a polygon. (points provided form a parallel line)"
)

def to_geometry(self, road_map: Optional[RoadMap] = None) -> Polygon:
"""Generate a polygon according to given coordinates"""
poly = None
if (
len(self.ext_coordinates) == 2
): # if user only specified two points, create a box
x_min = min(self.ext_coordinates[0][0], self.ext_coordinates[1][0])
x_max = max(self.ext_coordinates[0][0], self.ext_coordinates[1][0])
y_min = min(self.ext_coordinates[0][1], self.ext_coordinates[1][1])
y_max = max(self.ext_coordinates[0][1], self.ext_coordinates[1][1])
poly = box(x_min, y_min, x_max, y_max)

else: # else create a polygon according to the coordinates
poly = Polygon(self.ext_coordinates)

if self.rotation is not None:
poly = shapely_rotate(poly, self.rotation, use_radians=True)
return poly


@dataclass(frozen=True)
class BubbleLimits:
"""Defines the capture limits of a bubble."""
Expand Down Expand Up @@ -928,6 +978,20 @@ def __post_init__(self):
"Only boids can have keep_alive enabled (for persistent boids)"
)

if not isinstance(self.zone, MapZone):
poly = self.zone.to_geometry(road_map=None)
if not poly.is_valid:
follow_id = (
self.follow_actor_id
if self.follow_actor_id
else self.follow_vehicle_id
)
raise ValueError(
f"The zone polygon of {type(self.zone).__name__} of moving {self.id} which following {follow_id} is not a valid closed loop"
if follow_id
else f"The zone polygon of {type(self.zone).__name__} of fixed position {self.id} is not a valid closed loop"
)

@staticmethod
def to_actor_id(actor, mission_group):
"""Mashes the actor id and mission group to create what needs to be a unique id."""
Expand Down

0 comments on commit 4688ba3

Please sign in to comment.