Repository navigation
refactor: reuse shared grid layers in Q7 parser #882
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
base: main
Are you sure you want to change the base?
Changes from all commits
0886033
3a63163
caf3e72
13a6e00
7cb28f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,38 @@ | |
| from roborock.exceptions import RoborockException | ||
| from roborock.map.proto.b01_scmap_pb2 import RobotMap # type: ignore[attr-defined] | ||
|
|
||
| from .b01_grid_layers import ( | ||
| LAYER_BACKGROUND, | ||
| LAYER_FLOOR, | ||
| LAYER_WALL, | ||
| GridLayers, | ||
| decompose_grid, | ||
| ) | ||
| from .map_parser import ParsedMapData | ||
|
|
||
| _MAP_FILE_FORMAT = "PNG" | ||
|
|
||
|
|
||
| # The Q7 occupancy grid encodes only these classes (no per-room segmentation in | ||
| # the raster -- room ids/names live in the protobuf metadata, not the pixels). | ||
| _Q7_WALL_VALUE = 127 | ||
| _Q7_FLOOR_VALUE = 128 | ||
| _Q7_RENDER_INTENSITY = { | ||
| LAYER_BACKGROUND: 0, | ||
| LAYER_WALL: 180, | ||
| LAYER_FLOOR: 255, | ||
| } | ||
|
|
||
|
|
||
| def classify_q7_cell(value: int) -> str: | ||
| """Map a Q7 SCMap grid cell value to a canonical layer class.""" | ||
| if value == _Q7_WALL_VALUE: | ||
| return LAYER_WALL | ||
| if value == _Q7_FLOOR_VALUE: | ||
| return LAYER_FLOOR | ||
| return LAYER_BACKGROUND # 0 = outside / unknown | ||
|
|
||
|
|
||
| @dataclass | ||
| class B01MapParserConfig: | ||
| """Configuration for the B01/Q7 map parser.""" | ||
|
|
@@ -39,8 +66,9 @@ def parse(self, payload: bytes) -> ParsedMapData: | |
| parsed = _parse_scmap_payload(payload) | ||
| size_x, size_y, grid = _extract_grid(parsed) | ||
| room_names = _extract_room_names(parsed) | ||
| layers = decompose_grid(size_x, size_y, grid, [], classify_q7_cell) | ||
|
Collaborator
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. Internal Design & Conflict with Now that Additionally, note that |
||
|
|
||
| image = _render_occupancy_image(grid, size_x=size_x, size_y=size_y, scale=self._config.map_scale) | ||
| image = _render_occupancy_image(layers, scale=self._config.map_scale) | ||
|
|
||
| map_data = MapData() | ||
| map_data.image = ImageData( | ||
|
|
@@ -102,23 +130,15 @@ def _extract_room_names(parsed: RobotMap) -> dict[int, str]: | |
| return room_names | ||
|
|
||
|
|
||
| def _render_occupancy_image(grid: bytes, *, size_x: int, size_y: int, scale: int) -> Image.Image: | ||
| """Render the B01 occupancy grid into a simple image.""" | ||
|
|
||
| # The observed occupancy grid contains only: | ||
| # - 0: outside/unknown | ||
| # - 127: wall/obstacle | ||
| # - 128: floor/free | ||
| table = bytearray(range(256)) | ||
| table[0] = 0 | ||
| table[127] = 180 | ||
| table[128] = 255 | ||
|
|
||
| mapped = grid.translate(bytes(table)) | ||
| img = Image.frombytes("L", (size_x, size_y), mapped) | ||
| img = img.transpose(Image.Transpose.FLIP_TOP_BOTTOM).convert("RGB") | ||
| def _render_occupancy_image(layers: GridLayers, *, scale: int) -> Image.Image: | ||
| """Render canonical Q7 grid classes into the composed map image.""" | ||
| mapped = bytes(_Q7_RENDER_INTENSITY[layers.cell_class(value)] for value in layers.grid) | ||
| img = Image.frombytes("L", (layers.width, layers.height), mapped) | ||
| if layers.flip: | ||
| img = img.transpose(Image.Transpose.FLIP_TOP_BOTTOM) | ||
| img = img.convert("RGB") | ||
|
|
||
| if scale > 1: | ||
| img = img.resize((size_x * scale, size_y * scale), resample=Image.Resampling.NEAREST) | ||
| img = img.resize((layers.width * scale, layers.height * scale), resample=Image.Resampling.NEAREST) | ||
|
|
||
| return img | ||
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.
Protocol / Parser Correctness (Priority 3):
_Q7_WALL_VALUEand_Q7_FLOOR_VALUEare invertedIn Q7
SCMapoccupancy grids (RobotMap.mapData.mapData), cells are binarized from signedint8occupancy values where:127(0x7F, signed+127) = floor / free space128(0x80, signed-128) = wall / obstacle0= outside / unknownYou can also verify this directly in
tests/map/testdata/raw-mqtt-map301.bin.inflated.bin.gz, where127is the interior floor (25,841cells, and every room'sroomNamePostlabel coordinate lands on a127cell) while128forms the 1–2px perimeter walls (3,784cells). This was updated onmainin #911 as_FLOOR = 127and_WALL = 128.