refactor(sync): improve topdown rendering and make justfile adapt to windows

This commit is contained in:
2026-04-06 19:54:23 +08:00
parent 2c78f4b662
commit eb016385f8
3 changed files with 65 additions and 44 deletions

View File

@@ -1,9 +1,13 @@
from __future__ import annotations
from dataclasses import dataclass, field
from importlib import import_module
from typing import Any, Sequence
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from PIL import Image
@dataclass(frozen=True)
class TopDownSceneElements:
@@ -28,10 +32,26 @@ def render_topdown_scene_map(
elements: TopDownSceneElements,
meters_per_pixel: float,
style: TopDownRenderStyle | None = None,
maps_module: Any | None = None,
plt_module: Any | None = None,
use_matplotlib: bool = True,
) -> Any:
) -> Image.Image:
"""Render a top-down scene map as a PIL Image.
Uses matplotlib Agg backend (non-interactive) to draw room markers and
labels on the habitat top-down occupancy map, then returns the result
as a PIL Image.
Args:
pathfinder: Habitat pathfinder instance.
elements: Scene elements containing room nodes to annotate.
meters_per_pixel: Resolution for the top-down map.
style: Visual style configuration.
Returns:
PIL.Image.Image: Rendered top-down map with annotations.
Raises:
ValueError: If room_nodes is empty or meters_per_pixel <= 0.
NotImplementedError: If object_nodes or edges are provided.
"""
if not elements.room_nodes:
raise ValueError("room_nodes must not be empty")
@@ -47,37 +67,33 @@ def render_topdown_scene_map(
if style is None:
style = TopDownRenderStyle()
if maps_module is None:
maps_module = import_module("habitat.utils.visualizations.maps")
# Import habitat maps internally — treated as required dependency.
from habitat.utils.visualizations import maps
# Generate raw top-down occupancy map.
map_height = float(elements.room_nodes[0].center[1])
top_down_map = maps_module.get_topdown_map(
top_down_map = maps.get_topdown_map(
pathfinder,
height=map_height,
meters_per_pixel=meters_per_pixel,
)
if not use_matplotlib:
return top_down_map
if plt_module is None:
try:
plt_module = import_module("matplotlib.pyplot")
except ImportError:
return top_down_map
plt_module.figure(figsize=style.figure_size)
plt_module.imshow(top_down_map, cmap=style.map_cmap)
# Render with matplotlib Agg backend (no GUI required).
fig = Figure(figsize=style.figure_size)
_ = FigureCanvasAgg(fig)
ax = fig.add_subplot(111)
ax.imshow(top_down_map, cmap=style.map_cmap)
# Annotate room positions and labels.
for room_node in elements.room_nodes:
grid_y, grid_x = maps_module.to_grid(
grid_y, grid_x = maps.to_grid(
float(room_node.center[2]),
float(room_node.center[0]),
top_down_map.shape,
pathfinder=pathfinder,
)
plt_module.scatter(grid_x, grid_y, c=style.room_color, s=style.room_marker_size)
plt_module.text(
ax.scatter(grid_x, grid_y, c=style.room_color, s=style.room_marker_size)
ax.text(
grid_x + style.room_label_offset,
grid_y + style.room_label_offset,
room_node.room_id,
@@ -85,7 +101,12 @@ def render_topdown_scene_map(
fontsize=8,
)
plt_module.title(style.title)
plt_module.axis("off")
plt_module.show()
return top_down_map
ax.set_title(style.title)
ax.axis("off")
fig.canvas.draw()
# Extract RGB pixels from the Agg canvas buffer.
rgba = np.asarray(fig.canvas.buffer_rgba(), dtype=np.uint8)
rgb = rgba[..., :3].copy()
return Image.fromarray(rgb)