Exemplo n.º 1
0
def test_rotated(region_with_point: Tuple[Region, Point]) -> None:
    region, point = region_with_point

    result = point_in_region(point, region)

    assert all(result is point_in_region(point, rotated)
               for rotated in region_rotations(region))
Exemplo n.º 2
0
def test_reversals(region_with_point: Tuple[Region, Point]) -> None:
    region, point = region_with_point

    result = point_in_region(point, region)

    assert result is point_in_region(point, reverse_contour(region))
    assert result is point_in_region(reverse_point_coordinates(point),
                                     reverse_contour_coordinates(region))
Exemplo n.º 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)))
Exemplo n.º 4
0
def intersects_with_polygon(box: Box, polygon: Polygon,
                            context: Context) -> bool:
    """
    Checks if the box intersects the polygon.
    """
    border = polygon.border
    polygon_box = context.contour_box(border)
    if not intersects_with(polygon_box, box):
        return False
    elif (is_subset_of(polygon_box, box)
          or any(contains_point(box, vertex) for vertex in border.vertices)):
        return True
    locations = [
        point_in_region(vertex, border)
        for vertex in to_vertices(box, context)
    ]
    if (within_of(box, polygon_box)
            and all(location is Location.INTERIOR for location in locations)
            and all(
                context.segments_relation(edge, border_edge) is
                Relation.DISJOINT for edge in to_edges(box, context)
                for border_edge in context.contour_segments(border))):
        return not any(
            within_of(box, context.contour_box(hole))
            and within_of_region(box, hole, context) for hole in polygon.holes)
    else:
        return (any(location is not Location.EXTERIOR
                    for location in locations) or any(
                        intersects_with_segment(box, edge, context)
                        for edge in context.contour_segments(border)))
Exemplo n.º 5
0
def test_basic(region_with_point: Tuple[Region, Point]) -> None:
    region, point = region_with_point

    result = point_in_region(point, region)

    assert isinstance(result, Location)
    assert result in SHAPED_LOCATIONS
Exemplo n.º 6
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)))
Exemplo n.º 7
0
def test_connection_with_point_in_region(regions_pair: Tuple[Region, Region]
                                         ) -> None:
    left_region, right_region = regions_pair

    assert implication(region_in_region(left_region, right_region)
                       in (Relation.EQUAL, Relation.COMPONENT,
                           Relation.ENCLOSED, Relation.WITHIN),
                       all(point_in_region(vertex, right_region)
                           is not Location.EXTERIOR
                           for vertex in left_region.vertices))
Exemplo n.º 8
0
def within_of_region(box: Box, border: Contour, context: Context) -> bool:
    """
    Checks if the box is contained in an interior of the region.
    """
    return (all(
        point_in_region(vertex, border) is Location.INTERIOR
        for vertex in to_vertices(box, context)) and all(
            context.segments_relation(edge, border_edge) is Relation.DISJOINT
            for edge in to_edges(box, context)
            for border_edge in context.contour_segments(border)))
Exemplo n.º 9
0
def test_connection_with_point_in_region(
        region_with_segment: Tuple[Region, Segment]) -> None:
    region, segment = region_with_segment

    result = segment_in_region(segment, region)

    start_location, end_location = (point_in_region(segment.start, region),
                                    point_in_region(segment.end, region))
    assert implication(result is Relation.DISJOINT,
                       start_location is end_location is Location.EXTERIOR)
    assert implication(result is Relation.COMPONENT,
                       start_location is end_location is Location.BOUNDARY)
    assert implication(result is Relation.WITHIN,
                       start_location is end_location is Location.INTERIOR)
    assert implication(
        start_location is Location.EXTERIOR
        and end_location is Location.INTERIOR
        or start_location is Location.INTERIOR
        and end_location is Location.EXTERIOR, result is Relation.CROSS)
Exemplo n.º 10
0
def intersects_with_region(box: Box, region: Region, context: Context) -> bool:
    """
    Checks if the box intersects the region.
    """
    region_box = context.contour_box(region)
    return (intersects_with(region_box, box)
            and (is_subset_of(region_box, box) or any(
                contains_point(box, vertex)
                for vertex in region.vertices) or any(
                    point_in_region(vertex, region) is not Location.EXTERIOR
                    for vertex in to_vertices(box, context)) or any(
                        intersects_with_segment(box, edge, context)
                        for edge in context.contour_segments(region))))
Exemplo n.º 11
0
def within_of_region(box: Box,
                     border: Contour,
                     *,
                     context: Context) -> bool:
    """
    Checks if the box is contained in an interior of the region.
    """
    return (all(point_in_region(vertex, border) is Relation.WITHIN
                for vertex in to_vertices(box,
                                          context=context))
            and all(segments_relation(edge_start, edge_end, border_edge_start,
                                      border_edge_end) is Relation.DISJOINT
                    for edge_start, edge_end
                    in to_edges_endpoints(box,
                                          context=context)
                    for border_edge_start, border_edge_end
                    in contour_to_edges_endpoints(border)))
Exemplo n.º 12
0
def test_step(multiregion_with_region: Tuple[Multiregion, Point]) -> None:
    multiregion, point = multiregion_with_region
    first_region, *rest_multiregion = multiregion

    result = point_in_multiregion(point, rest_multiregion)
    next_result = point_in_multiregion(point, multiregion)

    relation_with_first_region = point_in_region(point, first_region)
    assert equivalence(
        next_result is Location.EXTERIOR, result is Location.EXTERIOR
        and relation_with_first_region is Location.EXTERIOR)
    assert equivalence(
        next_result is Location.INTERIOR, result is Location.INTERIOR
        or relation_with_first_region is Location.INTERIOR)
    assert equivalence(
        next_result is Location.BOUNDARY, result is Location.BOUNDARY
        or relation_with_first_region is Location.BOUNDARY)
Exemplo n.º 13
0
def test_connection_with_point_in_region(
        region_with_contour: Tuple[Region, Contour]) -> None:
    region, contour = region_with_contour

    result = contour_in_region(contour, region)

    vertices_relations = [
        point_in_region(vertex, region) for vertex in contour.vertices
    ]
    assert implication(
        result is Relation.DISJOINT,
        all(vertex_location is Location.EXTERIOR
            for vertex_location in vertices_relations))
    assert implication(
        result is Relation.TOUCH,
        all(vertex_location is not Location.INTERIOR
            for vertex_location in vertices_relations))
    assert implication(
        result is Relation.COMPONENT,
        all(vertex_location is Location.BOUNDARY
            for vertex_location in vertices_relations))
    assert implication(
        result is Relation.ENCLOSED,
        all(vertex_location is not Location.EXTERIOR
            for vertex_location in vertices_relations))
    assert implication(
        result is Relation.WITHIN,
        all(vertex_location is Location.INTERIOR
            for vertex_location in vertices_relations))
    assert implication(
        all(vertex_location is Location.EXTERIOR
            for vertex_location in vertices_relations),
        result is Relation.DISJOINT or result is Relation.TOUCH
        or result is Relation.CROSS)
    assert implication(
        all(vertex_location is Location.INTERIOR
            for vertex_location in vertices_relations),
        result is Relation.CROSS or result is Relation.ENCLOSED
        or result is Relation.WITHIN)
    assert implication(
        any(vertex_location is Location.EXTERIOR
            for vertex_location in vertices_relations)
        and any(vertex_location is Location.INTERIOR
                for vertex_location in vertices_relations),
        result is Relation.CROSS)
Exemplo n.º 14
0
def intersects_with_polygon(box: Box,
                            polygon: Polygon,
                            *,
                            context: Context) -> bool:
    """
    Checks if the box intersects the polygon.
    """
    border = polygon.border
    polygon_box = from_contour(border,
                               context=context)
    if not intersects_with(polygon_box, box):
        return False
    elif (is_subset_of(polygon_box, box)
          or any(contains_point(box, vertex) for vertex in border.vertices)):
        return True
    relations = [point_in_region(vertex, border)
                 for vertex in to_vertices(box,
                                           context=context)]
    if (within_of(box, polygon_box)
            and all(relation is Relation.WITHIN for relation in relations)
            and all(segments_relation(edge_start, edge_end, border_edge_start,
                                      border_edge_end) is Relation.DISJOINT
                    for edge_start, edge_end
                    in to_edges_endpoints(box,
                                          context=context)
                    for border_edge_start, border_edge_end
                    in contour_to_edges_endpoints(border))):
        return not any(within_of(box, from_contour(hole,
                                                   context=context))
                       and within_of_region(box, hole,
                                            context=context)
                       for hole in polygon.holes)
    else:
        return (any(relation is not Relation.DISJOINT
                    for relation in relations)
                or any(intersects_with_segment(box, border_edge_start,
                                               border_edge_end,
                                               context=context)
                       for border_edge_start, border_edge_end
                       in contour_to_edges_endpoints(border)))
Exemplo n.º 15
0
def intersects_with_region(box: Box,
                           region: Region,
                           *,
                           context: Context) -> bool:
    """
    Checks if the box intersects the region.
    """
    region_box = from_contour(region,
                              context=context)
    return (intersects_with(region_box, box)
            and (is_subset_of(region_box, box)
                 or any(contains_point(box, vertex)
                        for vertex in region.vertices)
                 or any(point_in_region(vertex, region)
                        is not Relation.DISJOINT
                        for vertex in to_vertices(box,
                                                  context=context))
                 or any(intersects_with_segment(box, border_edge_start,
                                                border_edge_end,
                                                context=context)
                        for border_edge_start, border_edge_end in
                        contour_to_edges_endpoints(region))))
Exemplo n.º 16
0
def test_self(region: Region) -> None:
    assert all(
        point_in_region(vertex, region) is Location.BOUNDARY
        for vertex in region.vertices)