コード例 #1
0
def test_relations(polygons_pair: Tuple[Polygon, Polygon]) -> None:
    left, right = polygons_pair

    result = polygon_in_polygon(left, right)

    complement = polygon_in_polygon(right, left)
    assert equivalence(result is complement, result
                       in SYMMETRIC_COMPOUND_RELATIONS)
    assert equivalence(
        result is not complement, result.complement is complement
        and result in ASYMMETRIC_UNIFORM_COMPOUND_RELATIONS
        and complement in ASYMMETRIC_UNIFORM_COMPOUND_RELATIONS)
コード例 #2
0
ファイル: polygon.py プロジェクト: lycantropos/gon
    def relate(self, other: Compound) -> Relation:
        """
        Finds relation between the polygon and the other geometry.

        Time complexity:
            ``O(vertices_count * log vertices_count)``
        Memory complexity:
            ``O(vertices_count)``

        where

            .. code-block:: python

                vertices_count = (len(self.border.vertices)
                                  + sum(len(hole.vertices)\
 for hole in self.holes))

        >>> from gon.base import Contour, Point, Polygon
        >>> polygon = Polygon(Contour([Point(0, 0), Point(6, 0), Point(6, 6),
        ...                            Point(0, 6)]),
        ...                   [Contour([Point(2, 2), Point(2, 4), Point(4, 4),
        ...                             Point(4, 2)])])
        >>> polygon.relate(polygon) is Relation.EQUAL
        True
        """
        return (segment_in_polygon(other, self)
                if isinstance(other, Segment)
                else (multisegment_in_polygon(other, self)
                      if isinstance(other, Linear)
                      else (polygon_in_polygon(other, self)
                            if isinstance(other, Polygon)
                            else other.relate(self).complement)))
コード例 #3
0
def test_basic(polygons_pair: Tuple[Polygon, Polygon]) -> None:
    left, right = polygons_pair

    result = polygon_in_polygon(left, right)

    assert isinstance(result, Relation)
    assert result in UNIFORM_COMPOUND_RELATIONS
コード例 #4
0
def test_connection_with_point_in_polygon(
        polygons_pair: Tuple[Polygon, Polygon]) -> None:
    left, right = polygons_pair

    assert implication(
        polygon_in_polygon(left, right)
        in (Relation.EQUAL, Relation.COMPONENT, Relation.ENCLOSED,
            Relation.WITHIN),
        all(
            point_in_polygon(vertex, right) is not Location.EXTERIOR
            for vertex in to_polygon_vertices(left)))
コード例 #5
0
def test_reversals(polygons_pair: Tuple[Polygon, Polygon]) -> None:
    left, right = polygons_pair

    result = polygon_in_polygon(left, right)

    assert result is polygon_in_polygon(reverse_polygon_border(left), right)
    assert result is polygon_in_polygon(left, reverse_polygon_border(right))
    assert result is polygon_in_polygon(reverse_polygon_holes(left), right)
    assert result is polygon_in_polygon(left, reverse_polygon_holes(right))
    assert result is polygon_in_polygon(reverse_polygon_holes_contours(left),
                                        right)
    assert result is polygon_in_polygon(left,
                                        reverse_polygon_holes_contours(right))
    assert result is polygon_in_polygon(reverse_polygon_coordinates(left),
                                        reverse_polygon_coordinates(right))
コード例 #6
0
def test_step(multipolygon_with_polygon: Tuple[Multipolygon, Polygon]) -> None:
    multipolygon, polygon = multipolygon_with_polygon
    first_polygon, rest_multipolygon = multipolygon_pop_left(multipolygon)

    result = polygon_in_multipolygon(polygon, rest_multipolygon)
    next_result = polygon_in_multipolygon(polygon, multipolygon)

    relation_with_first_polygon = polygon_in_polygon(polygon, first_polygon)
    assert equivalence(
        next_result is Relation.DISJOINT,
        result is relation_with_first_polygon is Relation.DISJOINT)
    assert equivalence(
        next_result is Relation.TOUCH, result is Relation.DISJOINT
        and relation_with_first_polygon is Relation.TOUCH
        or result is Relation.TOUCH
        and relation_with_first_polygon in (Relation.DISJOINT, Relation.TOUCH))
    assert equivalence(
        next_result is Relation.COMPONENT, result is Relation.COMPONENT
        or bool(rest_multipolygon.polygons)
        and relation_with_first_polygon is Relation.EQUAL)
    assert equivalence(
        next_result is Relation.OVERLAP, result is Relation.OVERLAP
        or relation_with_first_polygon is Relation.OVERLAP
        or (bool(rest_multipolygon.polygons) and result is Relation.DISJOINT
            or result is Relation.TOUCH)
        and relation_with_first_polygon in (Relation.COVER, Relation.ENCLOSES)
        or result in (Relation.COVER, Relation.ENCLOSES)
        and relation_with_first_polygon is Relation.DISJOINT)
    assert equivalence(
        next_result is Relation.COVER,
        (not rest_multipolygon.polygons or result is Relation.COVER)
        and relation_with_first_polygon is Relation.COVER)
    assert equivalence(
        next_result is Relation.ENCLOSES, result is Relation.ENCLOSES
        and relation_with_first_polygon in (Relation.ENCLOSES, Relation.COVER)
        or (not rest_multipolygon.polygons or result is Relation.COVER)
        and relation_with_first_polygon is Relation.ENCLOSES)
    assert equivalence(
        next_result is Relation.EQUAL, not rest_multipolygon.polygons
        and relation_with_first_polygon is Relation.EQUAL)
    assert equivalence(
        next_result is Relation.ENCLOSED, result is Relation.ENCLOSED
        or relation_with_first_polygon is Relation.ENCLOSED)
    assert equivalence(
        next_result is Relation.WITHIN, result is Relation.WITHIN
        or relation_with_first_polygon is Relation.WITHIN)
コード例 #7
0
def test_without_holes(polygon: Polygon) -> None:
    solid_polygon = to_solid_polygon(polygon)
    assert (polygon_in_polygon(polygon, solid_polygon) is
            (Relation.ENCLOSED if polygon.holes else Relation.EQUAL))
    assert (polygon_in_polygon(solid_polygon, polygon) is
            (Relation.ENCLOSES if polygon.holes else Relation.EQUAL))
コード例 #8
0
def test_border_convex_hull(polygon: Polygon) -> None:
    assert (polygon_in_polygon(polygon, to_polygon_with_convex_border(polygon))
            in (Relation.EQUAL, Relation.ENCLOSED))
コード例 #9
0
def test_self(polygon: Polygon) -> None:
    assert polygon_in_polygon(polygon, polygon) is Relation.EQUAL