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 1 commit
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
30 changes: 21 additions & 9 deletions deebot_client/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from PIL import Image, ImageColor, ImageOps, ImagePalette
import svg

from deebot_client import util
from deebot_client.events.map import CachedMapInfoEvent, MapChangedEvent

from .commands.json import GetMinorMap
Expand All @@ -39,6 +38,7 @@
from .util import (
OnChangedDict,
OnChangedList,
decompress_7z_base64_data,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -163,6 +163,18 @@
image: bytes


class ViewBoxFloat:
"""ViewBox where all values are converted to float."""

def __init__(self, view_box: svg.ViewBoxSpec) -> None:
self.min_x = float(view_box.min_x)
self.min_y = float(view_box.min_y)
self.width = float(view_box.width)
self.height = float(view_box.height)
self.max_x = self.min_x + self.width
self.max_y = self.min_y + self.height


# SVG definitions referred by map elements
_SVG_DEFS = svg.Defs(
elements=[
Expand Down Expand Up @@ -221,16 +233,16 @@
)


def _calc_point_in_viewbox(x: float, y: float, viewbox: svg.ViewBoxSpec) -> Point:
def _calc_point_in_viewbox(x: float, y: float, view_box: ViewBoxFloat) -> Point:
point = _calc_point(x, y)
return Point(
min(
max(point.x, float(viewbox.min_x)),
float(viewbox.min_x) + float(viewbox.width),
max(point.x, view_box.min_x),
view_box.max_x,
),
min(
max(point.y, float(viewbox.min_y)),
float(viewbox.min_y) + float(viewbox.height),
max(point.y, view_box.min_y),
view_box.max_y,
),
)

Expand Down Expand Up @@ -263,11 +275,11 @@


def _get_svg_positions(
positions: list[Position], viewbox: svg.ViewBoxSpec
positions: list[Position], view_box: ViewBoxFloat
) -> list[svg.Element]:
svg_positions: list[svg.Element] = []
for position in sorted(positions, key=lambda x: _POSITIONS_SVG[x.type].order):
pos = _calc_point_in_viewbox(position.x, position.y, viewbox)
pos = _calc_point_in_viewbox(position.x, position.y, view_box)
svg_positions.append(
svg.Use(href=f"#{_POSITIONS_SVG[position.type].svg_id}", x=pos.x, y=pos.y)
)
Expand Down Expand Up @@ -555,11 +567,11 @@

# Traces (if any)
if svg_traces_path := self._get_svg_traces_path():
svg_map.elements.append(svg_traces_path)

Check warning on line 570 in deebot_client/map.py

View check run for this annotation

Codecov / codecov/patch

deebot_client/map.py#L570

Added line #L570 was not covered by tests

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

self._last_image = str(svg_map)
Expand Down
15 changes: 8 additions & 7 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Path,
Point,
TracePoint,
ViewBoxFloat,
_calc_point,
_calc_point_in_viewbox,
_get_svg_positions,
Expand Down Expand Up @@ -81,15 +82,15 @@ def test_calc_point(


@pytest.mark.parametrize(
("x", "y", "viewbox", "expected"), _test_calc_point_in_viewbox_data
("x", "y", "view_box", "expected"), _test_calc_point_in_viewbox_data
)
def test_calc_point_in_viewbox(
x: int,
y: int,
viewbox: ViewBoxSpec,
view_box: ViewBoxSpec,
expected: Point,
) -> None:
result = _calc_point_in_viewbox(x, y, viewbox)
result = _calc_point_in_viewbox(x, y, ViewBoxFloat(view_box))
assert result == expected


Expand Down Expand Up @@ -157,7 +158,7 @@ async def on_change() -> None:


@patch(
"deebot_client.util.decompress_7z_base64_data",
"deebot_client.map.decompress_7z_base64_data",
philek marked this conversation as resolved.
Show resolved Hide resolved
Mock(return_value=b"\x10\x00\x00\x01\x00"),
)
async def test_Map_svg_traces_path(
Expand Down Expand Up @@ -309,12 +310,12 @@ def test_get_svg_subset(subset: MapSubsetEvent, expected: Path | Polygon) -> Non


@pytest.mark.parametrize(
("positions", "viewbox", "expected"), _test_get_svg_positions_data
("positions", "view_box", "expected"), _test_get_svg_positions_data
)
def test_get_svg_positions(
positions: list[Position],
viewbox: ViewBoxSpec,
view_box: ViewBoxSpec,
expected: list[Use],
) -> None:
result = _get_svg_positions(positions, viewbox)
result = _get_svg_positions(positions, ViewBoxFloat(view_box))
assert result == expected