Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bubble rendering issue, added ConfigurableZone #1693

Merged
merged 13 commits into from
Nov 23, 2022
Merged
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
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,
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
});

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]]
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
"""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)"
)
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved

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