inire/inire/tests/test_visualization.py

28 lines
1.1 KiB
Python

from matplotlib import pyplot as plt
from numpy import asarray
from shapely import force_3d
from shapely.geometry import Polygon, box
from inire.utils.visualization import plot_routing_results
from scripts.render_documentation_figures import _polygon
def test_polygon_holes_remain_unfilled_in_routes_and_documentation() -> None:
keepout = force_3d(Polygon(box(1, 1, 9, 9).exterior, [box(3, 3, 7, 7).exterior]), z=1)
figure, route_axes = plot_routing_results({}, [keepout], (0, 0, 10, 10))
walkthrough_axes = figure.add_axes((0.6, 0.6, 0.25, 0.25))
_polygon(walkthrough_axes, keepout, fill="gray", edge="black")
walkthrough_axes.set(xlim=(0, 10), ylim=(0, 10))
route_axes.grid(False)
try:
figure.canvas.draw()
pixels = asarray(figure.canvas.buffer_rgba())
for axes in (route_axes, walkthrough_axes):
def pixel(x, y, axes=axes):
xp, yp = axes.transData.transform((x, y))
return tuple(pixels[-1 - round(yp), round(xp)])
assert pixel(2.5, 2.5) != pixel(5, 5)
assert pixel(5, 5) == pixel(0.5, 0.5)
finally:
plt.close(figure)