Skip to content
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

Align viewbox center with vacuum map center #433

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
135 changes: 40 additions & 95 deletions deebot_client/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from PIL import Image, ImageColor, ImageOps, ImagePalette
import svg

from deebot_client import util
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
from deebot_client.events.map import CachedMapInfoEvent, MapChangedEvent

from .commands.json import GetMinorMap
Expand All @@ -38,7 +39,6 @@
from .util import (
OnChangedDict,
OnChangedList,
decompress_7z_base64_data,
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -155,32 +155,6 @@ class TracePoint(Point):
connected: bool


@dataclasses.dataclass
class AxisManipulation:
"""Map manipulation."""

map_shift: float
svg_max: float
_transform: Callable[[float, float], float] | None = None

def __post_init__(self) -> None:
self._svg_center = self.svg_max / 2

def transform(self, value: float) -> float:
"""Transform value."""
if self._transform is None:
return value
return self._transform(self._svg_center, value)


@dataclasses.dataclass
class MapManipulation:
"""Map manipulation."""

x: AxisManipulation
y: AxisManipulation


@dataclasses.dataclass
class BackgroundImage:
"""Background image."""
Expand Down Expand Up @@ -237,33 +211,27 @@ class BackgroundImage:
)


def _calc_value(value: float, axis_manipulation: AxisManipulation) -> float:
try:
if value is not None:
# SVG allows sub-pixel precision, so we use floating point coordinates for better placement.
new_value = (
(float(value) / _PIXEL_WIDTH) + _OFFSET - axis_manipulation.map_shift
)
new_value = axis_manipulation.transform(new_value)
# return value inside min and max
return round(
min(axis_manipulation.svg_max, max(0, new_value)), _ROUND_TO_DIGITS
)

except (ZeroDivisionError, ValueError):
pass

return 0


def _calc_point(
x: float,
y: float,
map_manipulation: MapManipulation,
) -> Point:
return Point(
_calc_value(x, map_manipulation.x),
_calc_value(y, map_manipulation.y),
0 if x is None else round(x / _PIXEL_WIDTH, _ROUND_TO_DIGITS),
0 if y is None else round(-y / _PIXEL_WIDTH, _ROUND_TO_DIGITS),
)


def _calc_point_in_viewbox(x: float, y: float, viewbox: svg.ViewBoxSpec) -> Point:
point = _calc_point(x, y)
return Point(
min(
max(point.x, float(viewbox.min_x)),
float(viewbox.min_x) + float(viewbox.width),
),
min(
max(point.y, float(viewbox.min_y)),
float(viewbox.min_y) + float(viewbox.height),
),
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
)


Expand Down Expand Up @@ -295,22 +263,24 @@ def _points_to_svg_path(


def _get_svg_positions(
positions: list[Position],
map_manipulation: MapManipulation,
positions: list[Position], viewbox: svg.ViewBoxSpec
) -> list[svg.Element]:
svg_positions: list[svg.Element] = []
for position in sorted(positions, key=lambda x: _POSITIONS_SVG[x.type].order):
pos = _calc_point(position.x, position.y, map_manipulation)
pos = _calc_point_in_viewbox(position.x, position.y, viewbox)
svg_positions.append(
svg.Use(href=f"#{_POSITIONS_SVG[position.type].svg_id}", x=pos.x, y=pos.y)
svg.Use(
href=f"#{_POSITIONS_SVG[position.type].svg_id}",
x=pos.x,
y=pos.y,
)
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
)

return svg_positions


def _get_svg_subset(
subset: MapSubsetEvent,
map_manipulation: MapManipulation,
) -> Path | svg.Polygon:
_LOGGER.debug("Creating svg subset for %s", subset)

Expand All @@ -319,7 +289,6 @@ def _get_svg_subset(
_calc_point(
subset_coordinates[i],
subset_coordinates[i + 1],
map_manipulation,
)
for i in range(0, len(subset_coordinates), 2)
]
Expand Down Expand Up @@ -416,7 +385,7 @@ async def on_map_subset(event: MapSubsetEvent) -> None:

def _update_trace_points(self, data: str) -> None:
_LOGGER.debug("[_update_trace_points] Begin")
trace_points = decompress_7z_base64_data(data)
trace_points = util.decompress_7z_base64_data(data)
edenhaus marked this conversation as resolved.
Show resolved Hide resolved

for i in range(0, len(trace_points), 5):
position_x, position_y = struct.unpack("<hh", trace_points[i : i + 4])
Expand Down Expand Up @@ -448,10 +417,7 @@ def _draw_map_pieces(self, image: Image.Image) -> None:
if current_piece.in_use:
image.paste(current_piece.image, (image_x, image_y))

def _get_svg_traces_path(
self,
map_manipulation: MapManipulation,
) -> Path | None:
def _get_svg_traces_path(self) -> Path | None:
if len(self._map_data.trace_values) > 0:
_LOGGER.debug("[get_svg_map] Draw Trace")
return Path(
Expand All @@ -461,11 +427,7 @@ def _get_svg_traces_path(
stroke_linejoin="round",
vector_effect="non-scaling-stroke",
transform=[
svg.Translate(
_OFFSET - map_manipulation.x.map_shift,
_OFFSET - map_manipulation.y.map_shift,
),
svg.Scale(0.2, 0.2),
svg.Scale(0.2, -0.2),
],
d=_points_to_svg_path(self._map_data.trace_values),
)
Expand Down Expand Up @@ -569,56 +531,39 @@ def get_svg_map(self) -> str | None:
# Build the SVG elements
svg_map = svg.SVG()
svg_map.elements = [_SVG_DEFS]
manipulation = MapManipulation(
AxisManipulation(
map_shift=background.bounding_box[0],
svg_max=background.bounding_box[2] - background.bounding_box[0],
),
AxisManipulation(
map_shift=background.bounding_box[1],
svg_max=background.bounding_box[3] - background.bounding_box[1],
_transform=lambda c, v: 2 * c - v,
),
)

# Set map viewBox based on background map bounding box.
svg_map.viewBox = svg.ViewBoxSpec(
0,
0,
manipulation.x.svg_max,
manipulation.y.svg_max,
background.bounding_box[0] - _OFFSET,
_OFFSET - background.bounding_box[3],
(background.bounding_box[2] - background.bounding_box[0]),
(background.bounding_box[3] - background.bounding_box[1]),
)

# Map background.
svg_map.elements.append(
svg.Image(
x=svg_map.viewBox.min_x,
y=svg_map.viewBox.min_y,
width=svg_map.viewBox.width,
height=svg_map.viewBox.height,
style="image-rendering: pixelated",
href=f"data:image/png;base64,{base64.b64encode(background.image).decode('ascii')}",
)
)

# Additional subsets (VirtualWalls and NoMopZones)
svg_map.elements.extend(
[
_get_svg_subset(subset, manipulation)
for subset in self._map_data.map_subsets.values()
]
[_get_svg_subset(subset) for subset in self._map_data.map_subsets.values()]
)

# Traces (if any)
if svg_traces_path := self._get_svg_traces_path(manipulation):
svg_map.elements.append(
# Elements to vertically flip
svg.G(
transform_origin=r"50% 50%",
transform=[svg.Scale(1, -1)],
elements=[svg_traces_path],
)
)
if svg_traces_path := self._get_svg_traces_path():
svg_map.elements.append(svg_traces_path)

# Bot and Charge stations
svg_map.elements.extend(
_get_svg_positions(self._map_data.positions, manipulation)
_get_svg_positions(self._map_data.positions, svg_map.viewBox)
)

self._last_image = str(svg_map)
Expand Down Expand Up @@ -667,7 +612,7 @@ def image(self) -> Image.Image:

def update_points(self, base64_data: str) -> None:
"""Add map piece points."""
decoded = decompress_7z_base64_data(base64_data)
decoded = util.decompress_7z_base64_data(base64_data)
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
old_crc32 = self._crc32
self._crc32 = zlib.crc32(decoded)

Expand Down
Loading
Loading