예제 #1
0
def test_rotations(contour_with_segment: Tuple[Contour, Segment]) -> None:
    contour, segment = contour_with_segment

    result = segment_in_contour(segment, contour)

    assert all(result is segment_in_contour(segment, rotated)
               for rotated in contour_rotations(contour))
예제 #2
0
def test_reversals(contour_with_segment: Tuple[Contour, Segment]) -> None:
    contour, segment = contour_with_segment

    result = segment_in_contour(segment, contour)

    assert result is segment_in_contour(reverse_segment(segment), contour)
    assert result is segment_in_contour(segment, reverse_contour(contour))
    assert result is segment_in_contour(reverse_segment_coordinates(segment),
                                        reverse_contour_coordinates(contour))
예제 #3
0
def coupled_with_polygon(box: Box, polygon: Polygon, context: Context) -> bool:
    """
    Checks if the box intersects the polygon in continuous points set.
    """
    border = polygon.border
    polygon_box = context.contour_box(border)
    if not coupled_with(polygon_box, box):
        return False
    elif (is_subset_of(polygon_box, box)
          or any(covers_point(box, vertex) for vertex in border.vertices)):
        return True
    locations = [
        point_in_region(vertex, border)
        for vertex in to_vertices(box, context)
    ]
    if any(location is Location.INTERIOR for location in locations):
        return (not all(location is Location.INTERIOR
                        for location in locations)
                or not is_subset_of_multiregion(box, polygon.holes, context))
    else:
        return (not is_subset_of_multiregion(box, polygon.holes, context) if
                (is_subset_of(box, polygon_box)
                 and is_subset_of_region(box, border, context)) else any(
                     segment_in_contour(segment, border) is Relation.OVERLAP
                     or segment_in_region(segment, border) in (
                         Relation.CROSS, Relation.COMPONENT, Relation.ENCLOSED)
                     for segment in to_edges(box, context)))
예제 #4
0
def coupled_with_region(box: Box,
                        region: Region,
                        *,
                        context: Context) -> bool:
    """
    Checks if the box intersects the region in continuous points set.
    """
    region_box = from_contour(region,
                              context=context)
    if not coupled_with(region_box, box):
        return False
    elif (is_subset_of(region_box, box)
          or any(covers_point(box, vertex) for vertex in region.vertices)):
        return True
    return (any(point_in_region(vertex, region) is Relation.WITHIN
                for vertex in to_vertices(box,
                                          context=context))
            or is_subset_of(box, region_box)
            and is_subset_of_region(box, region,
                                    context=context)
            or any(segment_in_contour(segment, region)
                   is Relation.OVERLAP
                   or segment_in_region(segment, region)
                   in (Relation.CROSS, Relation.COMPONENT,
                       Relation.ENCLOSED)
                   for segment in to_edges(box,
                                           context=context)))
예제 #5
0
def test_connection_with_segment_in_contour(
        region_with_segment: Tuple[Region, Segment]) -> None:
    region, segment = region_with_segment

    result = segment_in_region(segment, region)

    relation_with_contour = segment_in_contour(segment, region)
    assert implication(result is Relation.DISJOINT,
                       relation_with_contour is Relation.DISJOINT)
    assert implication(
        result is Relation.TOUCH, relation_with_contour is Relation.TOUCH
        or relation_with_contour is Relation.OVERLAP)
    assert equivalence(result is Relation.CROSS,
                       relation_with_contour is Relation.CROSS)
    assert equivalence(result is Relation.COMPONENT,
                       relation_with_contour is Relation.COMPONENT)
    assert implication(
        result is Relation.ENCLOSED, relation_with_contour is Relation.TOUCH
        or relation_with_contour is Relation.OVERLAP)
    assert implication(result is Relation.WITHIN,
                       relation_with_contour is Relation.DISJOINT)
    assert implication(
        relation_with_contour is Relation.DISJOINT, result is Relation.DISJOINT
        or result is Relation.WITHIN)
    assert implication(relation_with_contour is Relation.TOUCH,
                       result is Relation.TOUCH or result is Relation.ENCLOSED)
    assert implication(relation_with_contour is Relation.OVERLAP,
                       result is Relation.TOUCH or result is Relation.ENCLOSED)
예제 #6
0
def test_basic(contour_with_segment: Tuple[Contour, Segment]) -> None:
    contour, segment = contour_with_segment

    result = segment_in_contour(segment, contour)

    assert isinstance(result, Relation)
    assert result in LINEAR_RELATIONS
예제 #7
0
def test_connection_with_point_in_contour(
        contour_with_segment: Tuple[Contour, Segment]) -> None:
    contour, segment = contour_with_segment

    result = segment_in_contour(segment, contour)

    assert implication(
        result is Relation.DISJOINT,
        point_in_contour(segment.start, contour) is point_in_contour(
            segment.end, contour) is Location.EXTERIOR)
def test_step(contour_with_multisegment: Tuple[Contour, Multisegment]) -> None:
    contour, multisegment = contour_with_multisegment
    first_segment, rest_multisegment = multisegment_pop_left(multisegment)

    result = multisegment_in_contour(rest_multisegment, contour)
    next_result = multisegment_in_contour(multisegment, contour)

    relation_with_first_segment = segment_in_contour(first_segment, contour)
    assert equivalence(
        next_result is Relation.DISJOINT,
        result is relation_with_first_segment is Relation.DISJOINT)
    assert implication(
        next_result is Relation.TOUCH, result is Relation.TOUCH and
        (relation_with_first_segment is Relation.DISJOINT
         or relation_with_first_segment is Relation.TOUCH)
        or result is Relation.DISJOINT
        and relation_with_first_segment is Relation.TOUCH)
    assert implication(
        result is Relation.TOUCH
        and relation_with_first_segment is Relation.DISJOINT
        or result is Relation.DISJOINT
        and relation_with_first_segment is Relation.TOUCH,
        next_result is Relation.TOUCH)
    assert implication(
        next_result is Relation.CROSS, result is Relation.CROSS and
        (relation_with_first_segment is Relation.DISJOINT
         or relation_with_first_segment is Relation.TOUCH
         or relation_with_first_segment is Relation.CROSS)
        or (result is Relation.DISJOINT or result is Relation.TOUCH)
        and relation_with_first_segment is Relation.CROSS
        or result is Relation.TOUCH
        and relation_with_first_segment is Relation.TOUCH)
    assert implication(
        result is Relation.CROSS and
        (relation_with_first_segment is Relation.DISJOINT
         or relation_with_first_segment is Relation.TOUCH
         or relation_with_first_segment is Relation.CROSS)
        or (result is Relation.DISJOINT or result is Relation.TOUCH)
        and relation_with_first_segment is Relation.CROSS,
        next_result is Relation.CROSS)
    assert implication(
        next_result is Relation.OVERLAP, result is Relation.OVERLAP
        or relation_with_first_segment is Relation.OVERLAP
        or (result is Relation.DISJOINT and bool(rest_multisegment.segments)
            or result is Relation.TOUCH or result is Relation.CROSS)
        and relation_with_first_segment is Relation.COMPONENT
        or result is Relation.COMPONENT and
        (relation_with_first_segment is Relation.DISJOINT
         or relation_with_first_segment is Relation.TOUCH
         or relation_with_first_segment is Relation.CROSS))
    assert implication(
        next_result is Relation.COMPOSITE, result is Relation.COMPOSITE
        or relation_with_first_segment is Relation.COMPOSITE
        or bool(rest_multisegment.segments)
        and relation_with_first_segment is Relation.EQUAL
        or result is Relation.EQUAL or result is Relation.COMPONENT
        and relation_with_first_segment is Relation.OVERLAP
        or result is Relation.OVERLAP
        and relation_with_first_segment is Relation.COMPONENT)
    assert implication(
        result is Relation.COMPOSITE
        or relation_with_first_segment is Relation.COMPOSITE
        or bool(rest_multisegment.segments)
        and relation_with_first_segment is Relation.EQUAL
        or result is Relation.EQUAL, next_result is Relation.COMPOSITE)
    assert implication(
        next_result is Relation.EQUAL, not rest_multisegment.segments
        and relation_with_first_segment is Relation.EQUAL
        or result is relation_with_first_segment is Relation.COMPONENT)
    assert implication(
        not rest_multisegment.segments
        and relation_with_first_segment is Relation.EQUAL,
        next_result is Relation.EQUAL)
    assert implication(
        next_result is Relation.COMPONENT,
        (not rest_multisegment.segments or result is Relation.COMPONENT)
        and relation_with_first_segment is Relation.COMPONENT)
    assert implication(
        not rest_multisegment.segments
        and relation_with_first_segment is Relation.COMPONENT,
        next_result is Relation.COMPONENT)
예제 #9
0
def test_convex_contour(contour: Contour) -> None:
    assert implication(
        are_contours_equal(contour, to_contour_convex_hull(contour)),
        all(
            segment_in_contour(segment, contour) is Relation.TOUCH
            for segment in to_contour_separators(contour)))
예제 #10
0
def test_separators(contour: Contour) -> None:
    assert all(
        segment_in_contour(segment, contour) in (Relation.TOUCH,
                                                 Relation.CROSS,
                                                 Relation.OVERLAP)
        for segment in to_contour_separators(contour))
예제 #11
0
def test_self(contour: Contour) -> None:
    assert all(
        segment_in_contour(segment, contour) is Relation.COMPONENT
        for segment in to_contour_segments(contour))