Skip to content

refactor: reuse shared grid layers in Q7 parser - #882

Draft
tubededentifrice wants to merge 5 commits into
Python-roborock:mainfrom
tubededentifrice:agent/q7-map-layers
Draft

tubededentifrice wants to merge 5 commits into
Python-roborock:mainfrom
tubededentifrice:agent/q7-map-layers

Conversation

@tubededentifrice

@tubededentifrice tubededentifrice commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Keeps the Q7 shared-grid reuse from #848 as a small parser-internal refactor. The draft no longer expands the Q7 trait API and remains independent of the Q10 map stack.

Changes

  • Classify Q7 SCMap cells into the shared background, wall, and floor classes.
  • Build GridLayers once inside B01MapParser.parse from the already-parsed protobuf payload.
  • Render the existing composed Q7 image from those internal canonical layers.
  • Keep MapContentTrait unchanged: callers continue to receive only image_content, map_data, and raw_api_response.
  • Remove speculative Q7 calibration; no current Q7 renderer or caller consumes it.

Review feedback addressed

  • Layers and calibration are no longer exposed as public trait fields.
  • The trait no longer reparses the raw map payload.
  • Shared decomposition stays behind the existing parser interface and is used directly by rendering.

Validation

  • focused Q7 parser and trait suite — 39 passed
  • pytest -q — 792 passed, 86 snapshots passed
  • pre-commit run --show-diff-on-failure --color=always --all-files — all hooks passed, including Ruff and mypy

@allenporter allenporter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is draft still and not ready but giving early feedback in the name of quick iteration

map_data: MapData | None = None
"""Parsed map data (metadata for points on the map)."""

layers: GridLayers | None = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't seem like data points we want or need to expose to callers, and are more like internal map rendering details.

self.image_content = parsed_data.image_content
self.map_data = parsed_data.map_data
self.raw_api_response = raw_payload
try:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once parsed, these don't appear to be used by anything.

is there a specifc reason we're parsing the raw payload again? I would expect this to all be behind the existing self._map_parser.parse call above where the map parser parses the raw content and pulls out useful data points.

Is it because in q10 these come as a separate channel, therefore we need to change the map interface from working in raw payload to be raw payload + layers + calibration data?

@tubededentifrice tubededentifrice changed the title feat: expose shared map layers for Q7 refactor: reuse shared grid layers in Q7 parser Jul 19, 2026
@Lash-L Lash-L added Small PR Small pull request (<200 lines) Q7 B01 Q7 platform labels Nov 15, 2026

@Lash-L Lash-L left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated code review by Gemini 4 Argon in Antigravity using our /review (/code-review) skill (.agents/skills/code-review/SKILL.md / AGENTS.md).

Summary by Architectural Priority

  • Priority 1 — Public API & Trait Surfaces: Clean. Earlier review feedback to avoid exposing internal GridLayers and calibration fields on MapContentTrait / MapContent has been addressed.
  • Priority 2 — Internal Abstractions & main Evolution:
    • Now that GridLayers is no longer returned from B01MapParser.parse(), calling decompose_grid(size_x, size_y, grid, [], classify_q7_cell) builds an intermediate GridLayers object solely to pass into _render_occupancy_image(), replacing a single C-level bytes.translate() call with two full Python passes over grid (decompose_grid + layers.cell_class(value) per pixel) while discarding class_counts, rooms, and all GridLayers helper methods.
    • In addition, main has since evolved in #911 to render Q7 maps using room-outline flood-fill (roomDataInfo[*].roomOutline + roomNamePost), carpet stippling (carpetInfo), the shared V1 RGBA palette (ColorsPalette), and vacuum_map_parser_base overlay generators (_create_image_generator). Because Q7 SCMap grids do not encode per-room IDs in cell values (unlike Q10, which b01_grid_layers.py targets), this greyscale GridLayers path is superseded by the flood-fill renderer on main.
  • Priority 3 — Protocol & Parser Correctness:
    • _Q7_WALL_VALUE = 127 and _Q7_FLOOR_VALUE = 128 are inverted: in Q7 SCMap (RobotMap.mapData.mapData), cell byte 127 (0x7F, signed +127) encodes floor / free space and 128 (0x80, signed -128) encodes wall / obstacle (with 0 for unknown/outside). In the real capture fixture (tests/map/testdata/raw-mqtt-map301.bin.inflated.bin.gz), 127 makes up the interior room area (25,841 cells, containing all 10 roomNamePost room label coordinates) and 128 forms the surrounding walls (3,784 cells). This was also corrected on main in #911 (_FLOOR = 127, _WALL = 128).

# 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

Copy link
Copy Markdown
Collaborator

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_VALUE and _Q7_FLOOR_VALUE are inverted

In Q7 SCMap occupancy grids (RobotMap.mapData.mapData), cells are binarized from signed int8 occupancy values where:

  • 127 (0x7F, signed +127) = floor / free space
  • 128 (0x80, signed -128) = wall / obstacle
  • 0 = outside / unknown

You can also verify this directly in tests/map/testdata/raw-mqtt-map301.bin.inflated.bin.gz, where 127 is the interior floor (25,841 cells, and every room's roomNamePost label coordinate lands on a 127 cell) while 128 forms the 1–2px perimeter walls (3,784 cells). This was updated on main in #911 as _FLOOR = 127 and _WALL = 128.

Suggested change
_Q7_FLOOR_VALUE = 128
_Q7_FLOOR_VALUE = 127
_Q7_WALL_VALUE = 128

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internal Design & Conflict with main (Priority 2): decompose_grid / GridLayers is constructed and immediately discarded

Now that layers is no longer exposed on ParsedMapData / MapContent, decompose_grid(size_x, size_y, grid, [], classify_q7_cell) allocates a GridLayers instance with rooms=[] and iterates over every byte in grid in Python to compute class_counts (which are never read), only for _render_occupancy_image() to iterate over layers.grid in Python a second time (bytes(_Q7_RENDER_INTENSITY[layers.cell_class(value)] for value in layers.grid)). On a typical 340×300 map (102,000 cells), this replaces a single C-level grid.translate(...) lookup with two Python loops per frame without using any GridLayers capabilities.

Additionally, note that main (#911) has since replaced _render_occupancy_image in roborock/map/b01_map_parser.py with full room-outline flood-fill (_assign_room_pixels), carpet stippling, V1 palette coloring, and overlay rendering (_create_image_generator). Because Q7 SCMap rasters do not encode room IDs in cell bytes (unlike Q10), b01_grid_layers.py does not fit the room-outline flood-fill pipeline now on main.

@Lash-L Lash-L added the /review run Code review skill (/review) has been run label Nov 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Q7 B01 Q7 platform /review run Code review skill (/review) has been run Small PR Small pull request (<200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants