3434 solve_calibration ,
3535)
3636from .b01_q10_map_parser import (
37- _BACKGROUND_VALUE ,
38- _UNSEGMENTED_FLOOR_VALUE ,
39- _WALL_THRESHOLD ,
40- B01Q10MapParserConfig ,
37+ Q10_BACKGROUND_VALUE ,
38+ Q10_UNSEGMENTED_FLOOR_VALUE ,
39+ Q10_WALL_THRESHOLD ,
4140 Q10EraseZone ,
4241 Q10HistoricalTracePacket ,
4342 Q10MapPacket ,
4847 erased_packet ,
4948)
5049from .b01_q10_overlays import ZONE_TYPE_NO_GO , ZONE_TYPE_NO_MOP , Q10Zone
51- from .map_parser import DEFAULT_DRAWABLES , MapParserConfig , _create_image_generator
50+ from .map_parser import MapParserConfig , _create_image_generator
5251from .room_colors import adjacency_aware_room_colors
5352
5453# Path-units-per-pixel candidates for calibration. A dense ss07 path lands a
7978 Drawable .VACUUM_POSITION ,
8079 Drawable .VIRTUAL_WALLS ,
8180}
82- _Q10_DRAWABLES = [drawable for drawable in DEFAULT_DRAWABLES if drawable in _Q10_DRAWABLE_TYPES ]
8381
8482
8583@dataclass (frozen = True )
@@ -95,7 +93,7 @@ def render_q10_map(
9593 trace : Q10TracePacket | Q10HistoricalTracePacket | None ,
9694 overlays : Q10MapOverlays ,
9795 * ,
98- config : B01Q10MapParserConfig ,
96+ config : MapParserConfig ,
9997 robot_at_dock : bool = False ,
10098) -> bytes :
10199 """Compose the latest map, trace and DPS inputs into one PNG image.
@@ -120,19 +118,21 @@ def render_q10_map(
120118 if map_data .image is None :
121119 raise RoborockException ("Failed to render Q10 map image" )
122120
123- has_drawables = _place_obstacles (map_data , packet )
124- if trace_calibration is not None and trace is not None :
125- charger_heading = packet .header_calibration .charger_phi if packet .header_calibration is not None else None
126- _place_trace (map_data , trace_calibration , trace , charger_heading = charger_heading )
127- has_drawables = True
128- has_drawables = _place_charger_from_header (map_data , packet ) or has_drawables
129- if robot_at_dock :
130- has_drawables = _place_docked_robot (map_data ) or has_drawables
131- if vector_calibration is not None :
132- _place_overlays (map_data , vector_calibration , overlays )
133- has_drawables = has_drawables or bool (map_data .no_go_areas or map_data .no_mopping_areas or map_data .walls )
134- if has_drawables :
135- return _draw_map_content (map_data , config = config )
121+ drawables = [drawable for drawable in config .drawables if drawable in _Q10_DRAWABLE_TYPES ]
122+ if drawables :
123+ has_drawables = _place_obstacles (map_data , packet )
124+ if trace_calibration is not None and trace is not None :
125+ charger_heading = packet .header_calibration .charger_phi if packet .header_calibration is not None else None
126+ _place_trace (map_data , trace_calibration , trace , charger_heading = charger_heading )
127+ has_drawables = True
128+ has_drawables = _place_charger_from_header (map_data , packet ) or has_drawables
129+ if robot_at_dock :
130+ has_drawables = _place_docked_robot (map_data ) or has_drawables
131+ if vector_calibration is not None :
132+ _place_overlays (map_data , vector_calibration , overlays )
133+ has_drawables = has_drawables or bool (map_data .no_go_areas or map_data .no_mopping_areas or map_data .walls )
134+ if has_drawables :
135+ return _draw_map_content (map_data , config = config , drawables = drawables )
136136
137137 buffer = io .BytesIO ()
138138 map_data .image .data .save (buffer , format = "PNG" )
@@ -432,17 +432,13 @@ def to_area(zone: Q10Zone) -> Area | None:
432432def _draw_map_content (
433433 map_data : MapData ,
434434 * ,
435- config : B01Q10MapParserConfig ,
435+ config : MapParserConfig ,
436+ drawables : list [Drawable ],
436437) -> bytes :
437438 """Draw Q10 content with the shared V1 image generator."""
438439 if map_data .image is None :
439440 raise RoborockException ("Failed to render Q10 map image" )
440- drawables = (
441- _Q10_DRAWABLES
442- if config .drawables is None
443- else [drawable for drawable in config .drawables if drawable in _Q10_DRAWABLE_TYPES ]
444- )
445- generator = _create_image_generator (MapParserConfig (map_scale = config .map_scale ), drawables = drawables )
441+ generator = _create_image_generator (config , drawables = drawables )
446442 generator .draw_map (map_data )
447443 buffer = io .BytesIO ()
448444 map_data .image .data .save (buffer , format = "PNG" )
@@ -452,10 +448,10 @@ def _draw_map_content(
452448def _map_data_from_packet (
453449 packet : Q10MapPacket ,
454450 * ,
455- config : B01Q10MapParserConfig ,
451+ config : MapParserConfig ,
456452) -> MapData :
457453 """Build MapData with a rendered base image, without PNG serialization."""
458- image = _render_base_grid (packet , scale = config . map_scale )
454+ image = _render_base_grid (packet , config = config )
459455
460456 map_data = MapData ()
461457 map_data .image = ImageData (
@@ -473,29 +469,28 @@ def _map_data_from_packet(
473469 # overlays vertically.
474470 img_transformation = lambda p : Point (p .x , packet .height - p .y - 1 , p .a ),
475471 )
476- room_names = {room .id : room .name for room in packet .rooms }
477- if room_names :
478- map_data .additional_parameters ["room_names" ] = room_names
479-
480- # Carpet cells are flat grid indices (y*width + x) in the same top-down
481- # pixel space as the rendered raster, so they line up with the image.
482- if packet .carpet_mask is not None :
483- map_data .carpet_map = {i for i , value in enumerate (packet .carpet_mask ) if value }
484-
485472 return map_data
486473
487474
488- def _render_base_grid (packet : Q10MapPacket , * , scale : int ) -> Image .Image :
475+ def _render_base_grid (packet : Q10MapPacket , * , config : MapParserConfig ) -> Image .Image :
489476 """Render the Q10 grid with the V1 map palette."""
490- palette = _build_palette (packet .grid , packet .width )
477+ palette = _build_palette (
478+ packet .grid ,
479+ packet .width ,
480+ show_walls = config .show_walls ,
481+ show_rooms = config .show_rooms ,
482+ )
491483 flat_palette = [channel for color in palette for channel in color ]
492484 p_img = Image .frombytes ("P" , (packet .width , packet .height ), packet .grid )
493485 p_img .putpalette (flat_palette , rawmode = "RGBA" )
494486 img = p_img .convert ("RGBA" )
495487 # The ss07 grid is stored top-down (row 0 = top of the home), so it is
496488 # rendered as-is -- unlike the V1/Q7 convention, no vertical flip.
497- if scale > 1 :
498- img = img .resize ((packet .width * scale , packet .height * scale ), resample = Image .Resampling .NEAREST )
489+ if config .map_scale > 1 :
490+ img = img .resize (
491+ (packet .width * config .map_scale , packet .height * config .map_scale ),
492+ resample = Image .Resampling .NEAREST ,
493+ )
499494 return img
500495
501496
@@ -504,27 +499,34 @@ def _opaque(color: tuple[int, ...]) -> tuple[int, int, int, int]:
504499 return (color [0 ], color [1 ], color [2 ], color [3 ] if len (color ) == 4 else 255 )
505500
506501
507- def _build_palette (grid : bytes , width : int ) -> list [tuple [int , int , int , int ]]:
502+ def _build_palette (
503+ grid : bytes ,
504+ width : int ,
505+ * ,
506+ show_walls : bool = True ,
507+ show_rooms : bool = True ,
508+ ) -> list [tuple [int , int , int , int ]]:
508509 """Map Q10 cells onto the same colors used by the V1 map renderer."""
509510
510511 def room_id (value : int ) -> int | None :
511- return max (1 , value // 4 ) if 0 < value < _WALL_THRESHOLD else None
512+ return max (1 , value // 4 ) if 0 < value < Q10_WALL_THRESHOLD else None
512513
513514 colors = ColorsPalette ()
514- room_colors = adjacency_aware_room_colors (
515- grid ,
516- width ,
517- colors ,
518- room_id ,
519- )
520515 outside = (0 , 0 , 0 , 0 )
521516 palette = [outside ] * 256
522- for value in {value for value in grid if 0 < value < _WALL_THRESHOLD }:
523- palette [value ] = _opaque (room_colors [max (1 , value // 4 )])
524- wall = _opaque (colors .get_color (SupportedColor .GREY_WALL ))
525- for value in range (_WALL_THRESHOLD , 256 ):
526- palette [value ] = wall
527- palette [_UNSEGMENTED_FLOOR_VALUE ] = _opaque (colors .get_color (SupportedColor .MAP_INSIDE ))
528- palette [_BACKGROUND_VALUE ] = outside
529- palette [0 ] = outside
517+ if show_rooms :
518+ room_colors = adjacency_aware_room_colors (
519+ grid ,
520+ width ,
521+ colors ,
522+ room_id ,
523+ )
524+ for value in {value for value in grid if 0 < value < Q10_WALL_THRESHOLD }:
525+ palette [value ] = _opaque (room_colors [max (1 , value // 4 )])
526+ palette [Q10_UNSEGMENTED_FLOOR_VALUE ] = _opaque (colors .get_color (SupportedColor .MAP_INSIDE ))
527+ if show_walls :
528+ wall = _opaque (colors .get_color (SupportedColor .GREY_WALL ))
529+ for value in range (Q10_WALL_THRESHOLD , 256 ):
530+ if value not in (Q10_UNSEGMENTED_FLOOR_VALUE , Q10_BACKGROUND_VALUE ):
531+ palette [value ] = wall
530532 return palette
0 commit comments