125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
import pytest
|
|
import numpy
|
|
from numpy.testing import assert_equal
|
|
|
|
|
|
from ..shapes import Polygon
|
|
from ..utils import R90
|
|
from ..error import PatternError
|
|
|
|
|
|
@pytest.fixture
|
|
def polygon() -> Polygon:
|
|
return Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])
|
|
|
|
|
|
def test_vertices(polygon: Polygon) -> None:
|
|
assert_equal(polygon.vertices, [[0, 0], [1, 0], [1, 1], [0, 1]])
|
|
|
|
|
|
def test_xs(polygon: Polygon) -> None:
|
|
assert_equal(polygon.xs, [0, 1, 1, 0])
|
|
|
|
|
|
def test_ys(polygon: Polygon) -> None:
|
|
assert_equal(polygon.ys, [0, 0, 1, 1])
|
|
|
|
|
|
def test_offset(polygon: Polygon) -> None:
|
|
assert_equal(polygon.offset, [0, 0])
|
|
|
|
|
|
def test_square() -> None:
|
|
square = Polygon.square(1)
|
|
assert_equal(square.vertices, [[-0.5, -0.5], [-0.5, 0.5], [0.5, 0.5], [0.5, -0.5]])
|
|
|
|
|
|
def test_rectangle() -> None:
|
|
rectangle = Polygon.rectangle(1, 2)
|
|
assert_equal(rectangle.vertices, [[-0.5, -1], [-0.5, 1], [0.5, 1], [0.5, -1]])
|
|
|
|
|
|
def test_rect() -> None:
|
|
rect1 = Polygon.rect(xmin=0, xmax=1, ymin=-1, ymax=1)
|
|
assert_equal(rect1.vertices, [[0, -1], [0, 1], [1, 1], [1, -1]])
|
|
|
|
rect2 = Polygon.rect(xmin=0, lx=1, ymin=-1, ly=2)
|
|
assert_equal(rect2.vertices, [[0, -1], [0, 1], [1, 1], [1, -1]])
|
|
|
|
rect3 = Polygon.rect(xctr=0, lx=1, yctr=-2, ly=2)
|
|
assert_equal(rect3.vertices, [[-0.5, -3], [-0.5, -1], [0.5, -1], [0.5, -3]])
|
|
|
|
rect4 = Polygon.rect(xctr=0, xmax=1, yctr=-2, ymax=0)
|
|
assert_equal(rect4.vertices, [[-1, -4], [-1, 0], [1, 0], [1, -4]])
|
|
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(xctr=0, yctr=-2, ymax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(xmin=0, yctr=-2, ymax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(xmax=0, yctr=-2, ymax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(lx=0, yctr=-2, ymax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(yctr=0, xctr=-2, xmax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(ymin=0, xctr=-2, xmax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(ymax=0, xctr=-2, xmax=0)
|
|
with pytest.raises(PatternError):
|
|
Polygon.rect(ly=0, xctr=-2, xmax=0)
|
|
|
|
|
|
def test_octagon() -> None:
|
|
octagon = Polygon.octagon(side_length=1) # regular=True
|
|
assert_equal(octagon.vertices.shape, (8, 2))
|
|
diff = octagon.vertices - numpy.roll(octagon.vertices, -1, axis=0)
|
|
side_len = numpy.sqrt((diff * diff).sum(axis=1))
|
|
assert numpy.allclose(side_len, 1)
|
|
|
|
|
|
def test_to_polygons(polygon: Polygon) -> None:
|
|
assert polygon.to_polygons() == [polygon]
|
|
|
|
|
|
def test_get_bounds_single(polygon: Polygon) -> None:
|
|
assert_equal(polygon.get_bounds_single(), [[0, 0], [1, 1]])
|
|
|
|
|
|
def test_rotate(polygon: Polygon) -> None:
|
|
rotated_polygon = polygon.rotate(R90)
|
|
assert_equal(rotated_polygon.vertices, [[0, 0], [0, 1], [-1, 1], [-1, 0]])
|
|
|
|
|
|
def test_mirror(polygon: Polygon) -> None:
|
|
mirrored_by_y = polygon.deepcopy().mirror(1)
|
|
assert_equal(mirrored_by_y.vertices, [[0, 0], [-1, 0], [-1, 1], [0, 1]])
|
|
print(polygon.vertices)
|
|
mirrored_by_x = polygon.deepcopy().mirror(0)
|
|
assert_equal(mirrored_by_x.vertices, [[0, 0], [1, 0], [1, -1], [0, -1]])
|
|
|
|
|
|
def test_scale_by(polygon: Polygon) -> None:
|
|
scaled_polygon = polygon.scale_by(2)
|
|
assert_equal(scaled_polygon.vertices, [[0, 0], [2, 0], [2, 2], [0, 2]])
|
|
|
|
|
|
def test_clean_vertices(polygon: Polygon) -> None:
|
|
polygon = Polygon([[0, 0], [1, 1], [2, 2], [2, 2], [2, -4], [2, 0], [0, 0]]).clean_vertices()
|
|
assert_equal(polygon.vertices, [[0, 0], [2, 2], [2, 0]])
|
|
|
|
|
|
def test_remove_duplicate_vertices() -> None:
|
|
polygon = Polygon([[0, 0], [1, 1], [2, 2], [2, 2], [2, 0], [0, 0]]).remove_duplicate_vertices()
|
|
assert_equal(polygon.vertices, [[0, 0], [1, 1], [2, 2], [2, 0]])
|
|
|
|
|
|
def test_remove_colinear_vertices() -> None:
|
|
polygon = Polygon([[0, 0], [1, 1], [2, 2], [2, 2], [2, 0], [0, 0]]).remove_colinear_vertices()
|
|
assert_equal(polygon.vertices, [[0, 0], [2, 2], [2, 0]])
|
|
|
|
|
|
def test_vertices_dtype() -> None:
|
|
polygon = Polygon(numpy.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]], dtype=numpy.int32))
|
|
polygon.scale_by(0.5)
|
|
assert_equal(polygon.vertices, [[0, 0], [0.5, 0], [0.5, 0.5], [0, 0.5], [0, 0]])
|