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

[Feature] Boundary visualization for limited-size environments #142

Merged
merged 19 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b4752db
add boundary visualization for limited-size environments
Giovannibriglia Sep 17, 2024
9a6a890
add boundary visualization for limited-size environments
Giovannibriglia Sep 18, 2024
1177611
add boundary visualization for limited-size environments
Giovannibriglia Sep 18, 2024
15c244f
introduce "visualize_semidims" to display boundaries
Giovannibriglia Sep 18, 2024
5ca1125
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 18, 2024
d328cc8
Merge remote-tracking branch 'origin/boundaries_rendering' into bound…
Giovannibriglia Sep 18, 2024
255e577
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 19, 2024
2119cbd
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 19, 2024
beeba6b
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 19, 2024
010e99c
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 19, 2024
e1afd29
Update vmas/simulator/scenario.py
Giovannibriglia Sep 19, 2024
a17dea1
add boundary visualization for limited-size environments
Giovannibriglia Sep 20, 2024
e57ca48
disabled "visualize_semidims" as boundaries are already being plotted…
Giovannibriglia Sep 20, 2024
0f7626c
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 20, 2024
411fd8f
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 20, 2024
b370fa8
disabled "visualize_semidims" as boundaries are already being plotted…
Giovannibriglia Sep 20, 2024
a4a0af9
add boundary visualization for limited-size environments
Giovannibriglia Sep 20, 2024
a6526da
Update vmas/simulator/environment/environment.py
Giovannibriglia Sep 20, 2024
6f4e1e2
add boundary visualization for limited-size environments
Giovannibriglia Sep 20, 2024
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
50 changes: 50 additions & 0 deletions vmas/simulator/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,9 @@ def render(
)

# Render
if self.scenario.visualize_semidims:
self.plot_boundary()

self._set_agent_comm_messages(env_index)

if plot_position_function is not None:
Expand Down Expand Up @@ -770,6 +773,53 @@ def render(
# render to display or array
return self.viewer.render(return_rgb_array=mode == "rgb_array")

def plot_boundary(self):
# Include boundaries in the rendering
from vmas.simulator import rendering
Giovannibriglia marked this conversation as resolved.
Show resolved Hide resolved
from vmas.simulator.rendering import Line
from vmas.simulator.utils import Color

# Check boundary limits
if self.world.x_semidim is not None or self.world.y_semidim is not None:

infinite_value = 1e7
Giovannibriglia marked this conversation as resolved.
Show resolved Hide resolved

# set semidim variables
x_semi = (
self.world.x_semidim
if self.world.x_semidim is not None
else infinite_value
)
y_semi = (
self.world.y_semidim
if self.world.y_semidim is not None
else infinite_value
)

# define edges
boundary_edges = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of only one boundary set we need just 2 lines not 4

Copy link
Contributor Author

@Giovannibriglia Giovannibriglia Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of only one boundary set we need just 2 lines not 4

Sorry, are you sure?

Eventhough are just two lines, in my opinion, we should specify 4 points. Anyway, I agree that the previous implementation drew a square/rectangle.

I think the point regards the for-loop for drawing lines, for this reason I suggest this new implementation. But I'm not understanding why it draws nothing in the case of just "two parallel lines".

if x_semi is not None and y_semi is not None: # drawing square/rectangle

    boundary_edges = [
        (-x_semi, y_semi),  # top_left
        (x_semi, y_semi),  # top_right
        (x_semi, -y_semi),  # bottom_right
        (-x_semi, -y_semi)  # bottom_left
    ]

    # Create lines to form the boundary by connecting each corner to the next
    for i in range(len(boundary_edges)):
        start = boundary_edges[i]  # Current corner point
        end = boundary_edges[
            (i + 1) % len(boundary_edges)]  # Next corner point, wrap around to the first point
        print(start, end)
        line = Line(start, end, width=0.7)  # Create a line between two corners
        line.set_color(*color)  # Set the line color
        self.viewer.add_onetime(line)  # Add the line to the viewer for rendering

else: # drawing two parallel lines

    # if only x_semi is provided, draw horizontal lines
    if x_semi is not None:
        boundary_edges = [
            (-x_semi, infinite_value),  # upper_left
            (x_semi, infinite_value),  # upper_right
            (-x_semi, -infinite_value),  # lower_left
            (x_semi, -infinite_value)  # lower_right
        ]

    # if only y_semi is provided, draw vertical lines
    elif y_semi is not None:
        boundary_edges = [
            (infinite_value, y_semi),  # right_top
            (infinite_value, -y_semi),  # right_bottom
            (-infinite_value, y_semi),  # left_top
            (-infinite_value, -y_semi)  # left_bottom
        ]

    else:
        boundary_edges = []

    # create the two parallel lines by connecting the points
    for i in range(0, len(boundary_edges), 2):
        start = boundary_edges[i]
        end = boundary_edges[i + 1]
        print(start, end)
        line = Line(start, end, width=0.7)
        line.set_color(*color)
        self.viewer.add_onetime(line)

What do you think?

Copy link
Contributor Author

@Giovannibriglia Giovannibriglia Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong in labelling boundary_edges, the correct name should be boundary_points or something like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes what you did is what i was thinking, still 4 points, but only draw 2 lines when only one semidim is available.

I do think we can write it a little more compactly tho:

  • define the 4 points like before
  • write only once the line creation loop making it work for 2 or 4 lines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is too difficult also as is it works, but more compact would look better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what we want to avoid is drawing 4 lines when only one semidim is available

(-x_semi, y_semi), # top_left
(x_semi, y_semi), # top_right
(x_semi, -y_semi), # bottom_right
(-x_semi, -y_semi), # bottom_left
]

# Set the color for the boundary lines
color = Color.GRAY.value
# Initialize a transformation
xform = rendering.Transform()
Giovannibriglia marked this conversation as resolved.
Show resolved Hide resolved

# Create lines to form the boundary by connecting each corner to the next
for i in range(len(boundary_edges)):
start = boundary_edges[i] # Current corner point
end = boundary_edges[
(i + 1) % len(boundary_edges)
] # Next corner point, wraps around to the first point
line = Line(start, end, width=0.7) # Create a line between two corners
line.add_attr(xform) # Apply transformation to the line
Giovannibriglia marked this conversation as resolved.
Show resolved Hide resolved
line.set_color(*color) # Set the line color
self.viewer.add_onetime(line) # Add the line to the viewer for rendering

def plot_function(
self, f, precision, plot_range, cmap_range, cmap_alpha, cmap_name
):
Expand Down
2 changes: 2 additions & 0 deletions vmas/simulator/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(self):
"""Whether to plot a grid in the scenario rendering background. This can be changed in the :class:`~make_world` function. """
self.grid_spacing = 0.1
"""If :class:`~plot_grid`, the distance between lines in the background grid. This can be changed in the :class:`~make_world` function. """
self.visualize_semidims = True
"""If :class:`~plot_grid`, boundaries are displayed if dimension-limited environment. This can be changed in the :class:`~make_world` function. """
Giovannibriglia marked this conversation as resolved.
Show resolved Hide resolved

@property
def world(self):
Expand Down