Skip to content
Open
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
9 changes: 5 additions & 4 deletions roborock/map/b01_q10_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import math
from collections.abc import Sequence
from dataclasses import dataclass
from functools import lru_cache

from PIL import Image
from vacuum_map_parser_base.config.color import ColorsPalette, SupportedColor
Expand Down Expand Up @@ -480,9 +481,8 @@ def _render_base_grid(packet: Q10MapPacket, *, config: MapParserConfig) -> Image
show_walls=config.show_walls,
show_rooms=config.show_rooms,
)
flat_palette = [channel for color in palette for channel in color]
p_img = Image.frombytes("P", (packet.width, packet.height), packet.grid)
p_img.putpalette(flat_palette, rawmode="RGBA")
p_img.putpalette(palette, rawmode="RGBA")
img = p_img.convert("RGBA")
# The ss07 grid is stored top-down (row 0 = top of the home), so it is
# rendered as-is -- unlike the V1/Q7 convention, no vertical flip.
Expand All @@ -499,13 +499,14 @@ def _opaque(color: tuple[int, ...]) -> tuple[int, int, int, int]:
return (color[0], color[1], color[2], color[3] if len(color) == 4 else 255)


@lru_cache(maxsize=16)
def _build_palette(
grid: bytes,
width: int,
*,
show_walls: bool = True,
show_rooms: bool = True,
) -> list[tuple[int, int, int, int]]:
) -> bytes:
"""Map Q10 cells onto the same colors used by the V1 map renderer."""

def room_id(value: int) -> int | None:
Expand All @@ -529,4 +530,4 @@ def room_id(value: int) -> int | None:
for value in range(Q10_WALL_THRESHOLD, 256):
if value not in (Q10_UNSEGMENTED_FLOOR_VALUE, Q10_BACKGROUND_VALUE):
palette[value] = wall
return palette
return bytes(channel for color in palette for channel in color)
22 changes: 22 additions & 0 deletions tests/map/test_b01_q10_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,25 @@ def fail_decomposition(*args: object, **kwargs: object) -> None:

monkeypatch.setattr("roborock.map.b01_q10_map_parser.decompose_grid", fail_decomposition)
assert resolve_q10_current_room(packet, trace) == packet.rooms[1]


def test_render_memoizes_palette_across_trace_updates(monkeypatch: pytest.MonkeyPatch) -> None:
"""Repeated renders on the same map grid reuse the computed room-color palette."""
from roborock.map.b01_q10_render import _build_palette
from roborock.map.room_colors import adjacency_aware_room_colors

_build_palette.cache_clear()
calls = 0

def counting_room_colors(*args: object, **kwargs: object) -> dict[int, tuple[int, ...]]:
nonlocal calls
calls += 1
return adjacency_aware_room_colors(*args, **kwargs) # type: ignore[arg-type]

monkeypatch.setattr("roborock.map.b01_q10_render.adjacency_aware_room_colors", counting_room_colors)

packet, trace1 = _calibrated_inputs(heading=0)
_, trace2 = _calibrated_inputs(heading=90)
_render(packet, trace=trace1)
_render(packet, trace=trace2)
assert calls == 1
Loading