Skip to content

Commit a2db721

Browse files
add feature to render specific static frame (#43)
* feat(animation): add fram_idx support * feat(frame): add funciton * fixed(frame):fixed unasync frame render bug due to is_active function remove unrelated changes from the pull request --------- Co-authored-by: Alexey Skrynnik <Tviskaron@gmail.com>
1 parent 4ca698a commit a2db721

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

pogema/svg_animation/animation_drawer.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AnimationConfig:
1212
static: bool = False
1313
show_agents: bool = True
1414
egocentric_idx: typing.Optional[int] = None
15+
frame_idx: typing.Optional[int] = None
1516
uid: typing.Optional[str] = None
1617
save_every_idx_episode: typing.Optional[int] = 1
1718
show_grid_lines: bool = True
@@ -116,10 +117,15 @@ def create_animation(self, grid_holder: GridHolder):
116117
if gh.config.show_agents:
117118
agents = self.create_agents(gh)
118119
targets = self.create_targets(gh)
119-
120-
if not gh.config.static:
120+
if gh.config.static:
121+
agents = self.create_static_agents(gh)
122+
if gh.config.egocentric_idx is not None:
123+
obstacles = self.create_static_obstacles(obstacles= obstacles, grid_holder=gh)
124+
self.create_frame_view(gh)
125+
else:
121126
self.animate_agents(agents, gh)
122127
self.animate_targets(targets, gh)
128+
123129
if gh.config.show_grid_lines:
124130
grid_lines = self.create_grid_lines(gh, render_width, render_height)
125131
for line in grid_lines:
@@ -143,7 +149,7 @@ def fix_point(x, y, length):
143149
@staticmethod
144150
def check_in_radius(x1, y1, x2, y2, r) -> bool:
145151
return x2 - r <= x1 <= x2 + r and y2 - r <= y1 <= y2 + r
146-
152+
147153
@staticmethod
148154
def create_grid_lines(grid_holder: GridHolder, render_width, render_height):
149155
gh = grid_holder
@@ -177,6 +183,11 @@ def create_field_of_view(grid_holder):
177183
)
178184

179185
return result
186+
187+
def create_frame_view(self, grid_holder):
188+
gh: GridHolder = grid_holder
189+
frame_idx = gh.config.frame_idx
190+
gh.history = [[agent_states[gh.config.frame_idx]] for agent_states in gh.history]
180191

181192
def animate_field_of_view(self, view, grid_holder):
182193
gh: GridHolder = grid_holder
@@ -316,6 +327,39 @@ def create_obstacles(self, grid_holder):
316327

317328
return result
318329

330+
def create_static_obstacles(self, obstacles, grid_holder):
331+
gh: GridHolder = grid_holder
332+
frame_idx = gh.config.frame_idx
333+
result = []
334+
seen = set()
335+
336+
for step_idx, agent_state in enumerate(gh.history[gh.config.egocentric_idx][:frame_idx + 1]):
337+
ego_x, ego_y = agent_state.get_xy()
338+
for i in range(gh.height):
339+
for j in range(gh.width):
340+
x, y = self.fix_point(i, j, gh.width)
341+
if gh.obstacles[x][y]:
342+
if self.check_in_radius(x, y, ego_x, ego_y, gh.obs_radius):
343+
seen.add((x, y))
344+
for i in range(gh.height):
345+
for j in range(gh.width):
346+
x, y = self.fix_point(i, j, gh.width)
347+
348+
if gh.obstacles[x][y]:
349+
obs_settings = {
350+
'x': gh.svg_settings.draw_start + i * gh.svg_settings.scale_size - gh.svg_settings.r,
351+
'y': gh.svg_settings.draw_start + j * gh.svg_settings.scale_size - gh.svg_settings.r,
352+
'height': gh.svg_settings.r * 2,
353+
}
354+
if (x, y) in seen:
355+
obs_settings.update(opacity=1.0)
356+
else:
357+
obs_settings.update(opacity=gh.svg_settings.shaded_opacity)
358+
359+
result.append(RectangleHref(**obs_settings))
360+
361+
return result
362+
319363
def animate_obstacles(self, obstacles, grid_holder):
320364
gh: GridHolder = grid_holder
321365
obstacle_idx = 0
@@ -367,6 +411,33 @@ def create_agents(self, grid_holder):
367411

368412
return agents
369413

414+
def create_static_agents(self, grid_holder):
415+
agents = []
416+
gh: GridHolder = grid_holder
417+
ego_idx = grid_holder.config.egocentric_idx
418+
frame_idx = grid_holder.config.frame_idx
419+
420+
static_positions = [state[frame_idx].get_xy() for state in grid_holder.history]
421+
for idx, (x, y) in enumerate(static_positions):
422+
circle_settings = {
423+
'cx': gh.svg_settings.draw_start + y * gh.svg_settings.scale_size,
424+
'cy': gh.svg_settings.draw_start + (grid_holder.width - x - 1) * gh.svg_settings.scale_size,
425+
'r': gh.svg_settings.r, 'fill': grid_holder.colors[idx], 'class': 'agent',
426+
}
427+
428+
if ego_idx is not None:
429+
ego_x, ego_y = static_positions[ego_idx]
430+
is_out_of_radius = not self.check_in_radius(x, y, ego_x, ego_y, grid_holder.obs_radius)
431+
circle_settings['fill'] = gh.svg_settings.ego_other_color
432+
if idx == ego_idx:
433+
circle_settings['fill'] = gh.svg_settings.ego_color
434+
elif is_out_of_radius and gh.svg_settings.egocentric_shaded:
435+
circle_settings['opacity'] = gh.svg_settings.shaded_opacity
436+
437+
agents.append(Circle(**circle_settings))
438+
439+
return agents
440+
370441
@staticmethod
371442
def create_targets(grid_holder):
372443
gh: GridHolder = grid_holder

0 commit comments

Comments
 (0)