Skip to content

Commit b76519f

Browse files
committed
[Rendering] More colors and utils to render agent indices and rotation
1 parent 15053bc commit b76519f

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

vmas/scenarios/debug/diff_drive.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from vmas.simulator.dynamics.diff_drive import DiffDrive
1212
from vmas.simulator.dynamics.holonomic_with_rot import HolonomicWithRotation
1313
from vmas.simulator.scenario import BaseScenario
14-
from vmas.simulator.utils import Color, ScenarioUtils
14+
from vmas.simulator.utils import ScenarioUtils
1515

1616
if typing.TYPE_CHECKING:
1717
from vmas.simulator.rendering import Geom
@@ -88,24 +88,14 @@ def observation(self, agent: Agent):
8888
)
8989

9090
def extra_render(self, env_index: int = 0) -> "List[Geom]":
91-
from vmas.simulator import rendering
9291

9392
geoms: List[Geom] = []
9493

9594
# Agent rotation
9695
for agent in self.world.agents:
97-
color = Color.BLACK.value
98-
line = rendering.Line(
99-
(0, 0),
100-
(0.1, 0),
101-
width=1,
96+
geoms.append(
97+
ScenarioUtils.plot_entity_rotation(agent, env_index, length=0.1)
10298
)
103-
xform = rendering.Transform()
104-
xform.set_rotation(agent.state.rot[env_index])
105-
xform.set_translation(*agent.state.pos[env_index])
106-
line.add_attr(xform)
107-
line.set_color(*color)
108-
geoms.append(line)
10999

110100
return geoms
111101

vmas/scenarios/debug/drone.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from vmas.simulator.core import Agent, World
1212
from vmas.simulator.dynamics.drone import Drone
1313
from vmas.simulator.scenario import BaseScenario
14-
from vmas.simulator.utils import Color, ScenarioUtils
14+
from vmas.simulator.utils import ScenarioUtils
1515

1616
if typing.TYPE_CHECKING:
1717
from vmas.simulator.rendering import Geom
@@ -93,24 +93,14 @@ def done(self):
9393
)
9494

9595
def extra_render(self, env_index: int = 0) -> "List[Geom]":
96-
from vmas.simulator import rendering
9796

9897
geoms: List[Geom] = []
9998

10099
# Agent rotation
101100
for agent in self.world.agents:
102-
color = Color.BLACK.value
103-
line = rendering.Line(
104-
(0, 0),
105-
(0.1, 0),
106-
width=1,
101+
geoms.append(
102+
ScenarioUtils.plot_entity_rotation(agent, env_index, length=0.1)
107103
)
108-
xform = rendering.Transform()
109-
xform.set_rotation(agent.state.rot[env_index])
110-
xform.set_translation(*agent.state.pos[env_index])
111-
line.add_attr(xform)
112-
line.set_color(*color)
113-
geoms.append(line)
114104

115105
return geoms
116106

vmas/scenarios/debug/kinematic_bicycle.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from vmas.simulator.dynamics.holonomic_with_rot import HolonomicWithRotation
1313
from vmas.simulator.dynamics.kinematic_bicycle import KinematicBicycle
1414
from vmas.simulator.scenario import BaseScenario
15-
from vmas.simulator.utils import Color, ScenarioUtils
15+
from vmas.simulator.utils import ScenarioUtils
1616

1717
if typing.TYPE_CHECKING:
1818
from vmas.simulator.rendering import Geom
@@ -99,24 +99,14 @@ def observation(self, agent: Agent):
9999
)
100100

101101
def extra_render(self, env_index: int = 0) -> "List[Geom]":
102-
from vmas.simulator import rendering
103102

104103
geoms: List[Geom] = []
105104

106105
# Agent rotation
107106
for agent in self.world.agents:
108-
color = Color.BLACK.value
109-
line = rendering.Line(
110-
(0, 0),
111-
(0.1, 0),
112-
width=1,
107+
geoms.append(
108+
ScenarioUtils.plot_entity_rotation(agent, env_index, length=0.1)
113109
)
114-
xform = rendering.Transform()
115-
xform.set_rotation(agent.state.rot[env_index])
116-
xform.set_translation(*agent.state.pos[env_index])
117-
line.add_attr(xform)
118-
line.set_color(*color)
119-
geoms.append(line)
120110

121111
return geoms
122112

vmas/simulator/utils.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Copyright (c) 2022-2024.
1+
# Copyright (c) 2022-2025.
22
# ProrokLab (https://www.proroklab.org/)
33
# All rights reserved.
44
import importlib
55
import os
6+
import typing
67
import warnings
78
from abc import ABC, abstractmethod
89
from enum import Enum
@@ -12,6 +13,9 @@
1213
import torch
1314
from torch import Tensor
1415

16+
if typing.TYPE_CHECKING:
17+
from vmas.simulator.rendering import Geom
18+
1519
_has_matplotlib = importlib.util.find_spec("matplotlib") is not None
1620

1721
X = 0
@@ -50,6 +54,10 @@ class Color(Enum):
5054
WHITE = (0.75, 0.75, 0.75)
5155
GRAY = (0.25, 0.25, 0.25)
5256
BLACK = (0.15, 0.15, 0.15)
57+
ORANGE = (1.00, 0.50, 0)
58+
PINK = (0.97, 0.51, 0.75)
59+
PURPLE = (0.60, 0.31, 0.64)
60+
YELLOW = (0.87, 0.87, 0)
5361

5462

5563
def override(cls):
@@ -319,3 +327,54 @@ def check_kwargs_consumed(dictionary_of_kwargs: Dict, warn: bool = True):
319327
)
320328
else:
321329
raise ValueError(message)
330+
331+
@staticmethod
332+
def render_agent_indices(
333+
scenario, env_index: int, start_from: int = 0, exclude: List = None
334+
) -> "List[Geom]":
335+
from vmas.simulator import rendering
336+
337+
aspect_r = scenario.viewer_size[X] / scenario.viewer_size[Y]
338+
if aspect_r > 1:
339+
dimensional_ratio = (aspect_r, 1)
340+
else:
341+
dimensional_ratio = (1, 1 / aspect_r)
342+
343+
geoms = []
344+
for i, entity in enumerate(scenario.world.agents):
345+
if exclude is not None and entity in exclude:
346+
continue
347+
i = i + start_from
348+
line = rendering.TextLine(
349+
text=str(i),
350+
font_size=15,
351+
x=(
352+
(entity.state.pos[env_index, X] * scenario.viewer_size[X])
353+
/ (scenario.viewer_zoom**2 * dimensional_ratio[X] * 2)
354+
+ scenario.viewer_size[X] / 2
355+
),
356+
y=(
357+
(entity.state.pos[env_index, Y] * scenario.viewer_size[Y])
358+
/ (scenario.viewer_zoom**2 * dimensional_ratio[Y] * 2)
359+
+ scenario.viewer_size[Y] / 2
360+
),
361+
)
362+
geoms.append(line)
363+
return geoms
364+
365+
@staticmethod
366+
def plot_entity_rotation(entity, env_index: int, length: float = 0.15) -> "Geom":
367+
from vmas.simulator import rendering
368+
369+
color = entity.color
370+
line = rendering.Line(
371+
(0, 0),
372+
(length, 0),
373+
width=2,
374+
)
375+
xform = rendering.Transform()
376+
xform.set_rotation(entity.state.rot[env_index])
377+
xform.set_translation(*entity.state.pos[env_index])
378+
line.add_attr(xform)
379+
line.set_color(*color)
380+
return line

0 commit comments

Comments
 (0)