-
Notifications
You must be signed in to change notification settings - Fork 91
feat: align Q10 map colors with V1 #902
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
Open
tubededentifrice
wants to merge
1
commit into
Python-roborock:main
Choose a base branch
from
tubededentifrice:vc/q10-v1-palette
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,21 @@ | |
|
|
||
| import io | ||
| import logging | ||
| import threading | ||
| from dataclasses import dataclass, field | ||
|
|
||
| from vacuum_map_parser_base.config.color import ColorsPalette, SupportedColor | ||
| from vacuum_map_parser_base.config.drawable import Drawable | ||
| from vacuum_map_parser_base.config.image_config import ImageConfig | ||
| from vacuum_map_parser_base.config.size import Size, Sizes | ||
| from vacuum_map_parser_base.map_data import MapData | ||
| from vacuum_map_parser_roborock.image_parser import RoborockImageParser | ||
| from vacuum_map_parser_roborock.map_data_parser import RoborockMapDataParser | ||
|
|
||
| from roborock.exceptions import RoborockException | ||
|
|
||
| from .room_colors import adjacency_aware_room_colors | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_DRAWABLES = { | ||
|
|
@@ -98,6 +102,49 @@ def parse(self, map_bytes: bytes) -> ParsedMapData | None: | |
| return ParsedMapData(image_content=img_byte_arr.getvalue(), map_data=parsed_map) | ||
|
|
||
|
|
||
| class _AdjacencyAwareRoborockImageParser(RoborockImageParser): | ||
| """Apply the shared adjacency color policy to V1 room cells.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| palette: ColorsPalette, | ||
| image_config: ImageConfig, | ||
| *, | ||
| recolor_rooms: bool = True, | ||
| ) -> None: | ||
| super().__init__(palette, image_config) | ||
| self._room_palette = palette | ||
| self._base_room_colors = palette.cached_room_colors.copy() | ||
| self._recolor_rooms = recolor_rooms | ||
| self._palette_lock = threading.Lock() | ||
|
|
||
| def parse( | ||
| self, | ||
| raw_data: bytes, | ||
| width: int, | ||
| height: int, | ||
| carpet_map: set[int] | None, | ||
| removed_map: set[int] | None = None, | ||
| ): | ||
| """Assign non-conflicting room colors before the V1 image pass.""" | ||
| with self._palette_lock: | ||
| self._room_palette.cached_room_colors.clear() | ||
| self._room_palette.cached_room_colors.update(self._base_room_colors) | ||
|
|
||
| if self._recolor_rooms: | ||
|
|
||
| def room_id(value: int) -> int | None: | ||
| if value in (self.MAP_OUTSIDE, self.MAP_WALL, self.MAP_INSIDE, self.MAP_SCAN): | ||
| return None | ||
| return self._get_room_number(value) if value & 0x07 == 0x07 else None | ||
|
|
||
| room_colors = adjacency_aware_room_colors(raw_data, width, self._room_palette, room_id) | ||
| for number, color in room_colors.items(): | ||
| self._room_palette.cached_room_colors[number] = color | ||
| self._room_palette.cached_room_colors[str(number)] = color | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see any code that uses strings for this. Can you explain what's going on here? otherwise i think tis can be: |
||
| return super().parse(raw_data, width, height, carpet_map, removed_map) | ||
|
|
||
|
|
||
| def _create_map_data_parser(config: MapParserConfig) -> RoborockMapDataParser: | ||
| """Create a RoborockMapDataParser based on the config entry.""" | ||
| color_dicts = {} | ||
|
|
@@ -114,10 +161,18 @@ def _create_map_data_parser(config: MapParserConfig) -> RoborockMapDataParser: | |
| if not config.show_rooms: | ||
| room_colors = {str(x): (0, 0, 0, 0) for x in range(1, 32)} | ||
|
|
||
| return RoborockMapDataParser( | ||
| ColorsPalette(color_dicts, room_colors), | ||
| palette = ColorsPalette(color_dicts, room_colors) | ||
| image_config = ImageConfig(scale=config.map_scale) | ||
| parser = RoborockMapDataParser( | ||
| palette, | ||
| Sizes({k: v * config.map_scale for k, v in Sizes.SIZES.items() if k != Size.MOP_PATH_WIDTH}), | ||
| config.drawables, | ||
| ImageConfig(scale=config.map_scale), | ||
| image_config, | ||
| [], | ||
| ) | ||
| parser._image_parser = _AdjacencyAwareRoborockImageParser( | ||
| palette, | ||
| image_config, | ||
| recolor_rooms=config.show_rooms, | ||
| ) | ||
| return parser | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Deterministic room colors that keep adjacent segments distinguishable.""" | ||
|
|
||
| from collections.abc import Callable, Sequence | ||
|
|
||
| from vacuum_map_parser_base.config.color import Color, ColorsPalette | ||
|
|
||
| RoomIdFromCell = Callable[[int], int | None] | ||
|
|
||
|
|
||
| def adjacency_aware_room_colors( | ||
| grid: Sequence[int], | ||
| width: int, | ||
| palette: ColorsPalette, | ||
| room_id_from_cell: RoomIdFromCell, | ||
| ) -> dict[int, Color]: | ||
| """Return room colors, changing only adjacent same-color conflicts. | ||
|
|
||
| Room IDs remain the stable preference, matching the existing V1 palette. | ||
| When two rooms sharing an edge resolve to the same RGB value, the | ||
| higher-numbered room receives the first palette color not already used by | ||
| one of its colored neighbors. | ||
| """ | ||
| if width <= 0: | ||
| return {} | ||
|
|
||
| room_ids: set[int] = set() | ||
| neighbors: dict[int, set[int]] = {} | ||
| for index, value in enumerate(grid): | ||
| room_id = room_id_from_cell(value) | ||
| if room_id is None: | ||
| continue | ||
| room_ids.add(room_id) | ||
| neighbors.setdefault(room_id, set()) | ||
|
|
||
| for neighbor_index in (index - 1 if index % width else -1, index - width): | ||
| if neighbor_index < 0: | ||
| continue | ||
| neighbor_id = room_id_from_cell(grid[neighbor_index]) | ||
| if neighbor_id is None or neighbor_id == room_id: | ||
| continue | ||
| neighbors[room_id].add(neighbor_id) | ||
| neighbors.setdefault(neighbor_id, set()).add(room_id) | ||
|
|
||
| candidates: list[Color] = [] | ||
| for palette_id in map(int, ColorsPalette.ROOM_COLORS): | ||
| color = palette.get_room_color(palette_id) | ||
| if color not in candidates: | ||
| candidates.append(color) | ||
|
|
||
| assigned: dict[int, Color] = {} | ||
| for room_id in sorted(room_ids): | ||
| preferred = palette.get_room_color(room_id) | ||
| neighbor_colors = {assigned[neighbor] for neighbor in neighbors[room_id] if neighbor in assigned} | ||
| assigned[room_id] = ( | ||
| preferred | ||
| if preferred not in neighbor_colors | ||
| else next((color for color in candidates if color not in neighbor_colors), preferred) | ||
| ) | ||
| return assigned |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work as a one-liner?