def test_properties(polygon_with_segment: PolygonWithSegment ) -> None: polygon, segment = polygon_with_segment result = complete_intersect_segment_with_polygon(segment, polygon) result_points, result_segments = pack_non_shaped(result) assert all(point_in_segment(point, segment) is Location.BOUNDARY for point in result_points) assert all(point_in_polygon(point, polygon) is Location.BOUNDARY for point in result_points) assert (not (segment_in_polygon(segment, polygon) is Relation.TOUCH and all(segments_relation(segment, contour_segment) in (Relation.CROSS, Relation.DISJOINT, Relation.TOUCH) for contour in to_polygon_contours(polygon) for contour_segment in to_contour_segments(contour))) or any(point_in_segment(point, segment) is Location.BOUNDARY for point in result_points) or any(segments_relation(segment, result_segment) is Relation.TOUCH for result_segment in result_segments)) assert all(segment_in_segment(result_segment, segment) in (Relation.EQUAL, Relation.COMPONENT) for result_segment in result_segments) assert all(segment_in_polygon(result_segment, polygon) in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN) for result_segment in result_segments) assert (segment_in_polygon(segment, polygon) not in (Relation.CROSS, Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN) or to_sorted_segment(segment) in result_segments # in case of cross or any(segment_in_segment(result_segment, segment) is Relation.COMPONENT for result_segment in result_segments))
def test_properties( polygon_with_multisegment: PolygonWithMultisegment) -> None: polygon, multisegment = polygon_with_multisegment result = intersect_multisegment_with_polygon(multisegment, polygon) result_points, result_segments = pack_non_shaped(result) assert not result_points assert all( segment_in_multisegment(result_segment, multisegment) in ( Relation.EQUAL, Relation.COMPONENT) for result_segment in result_segments) assert all( segment_in_polygon(result_segment, polygon) in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN) for result_segment in result_segments) assert all( to_sorted_segment(segment) in result_segments # in case of cross or any( segment_in_segment(result_segment, segment) is Relation.COMPONENT for result_segment in result_segments) for segment in multisegment.segments if (segment_in_polygon(segment, polygon) in (Relation.CROSS, Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN)))
def test_properties( polygon_with_multisegment: PolygonWithMultisegment) -> None: polygon, multisegment = polygon_with_multisegment result = subtract_polygon_from_multisegment(multisegment, polygon) result_points, result_segments = pack_non_shaped(result) assert not result_points assert all( segment_in_multisegment(result_segment, multisegment) in ( Relation.EQUAL, Relation.COMPONENT) for result_segment in result_segments) assert all( segment_in_polygon(result_segment, polygon) in (Relation.DISJOINT, Relation.TOUCH) for result_segment in result_segments) assert all( segment in result_segments or reverse_segment(segment) in result_segments # in case of cross or any( segment_in_segment(result_segment, segment) is Relation.COMPONENT for result_segment in result_segments) for segment in multisegment.segments if (segment_in_polygon(segment, polygon) in (Relation.DISJOINT, Relation.CROSS)))
def test_separators(polygon: Polygon) -> None: assert all(segment_in_polygon(segment, polygon) in (Relation.TOUCH, Relation.CROSS, Relation.ENCLOSED) for segment in to_contour_separators(polygon.border)) assert all(segment_in_polygon(segment, polygon) in (Relation.TOUCH, Relation.CROSS, Relation.ENCLOSED) for hole in polygon.holes for segment in to_contour_separators(hole))
def test_basic(polygon_with_segment: Tuple[Polygon, Segment]) -> None: polygon, segment = polygon_with_segment result = segment_in_polygon(segment, polygon) assert isinstance(result, Relation) assert result in LINEAR_COMPOUND_RELATIONS
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)))
def test_step(multipolygon_with_polygon: Tuple[Multipolygon, Segment]) -> None: multipolygon, segment = multipolygon_with_polygon first_polygon, rest_multipolygon = multipolygon_pop_left(multipolygon) result = segment_in_multipolygon(segment, rest_multipolygon) next_result = segment_in_multipolygon(segment, multipolygon) relation_with_first_polygon = segment_in_polygon(segment, first_polygon) assert equivalence( next_result is Relation.DISJOINT, result is Relation.DISJOINT and relation_with_first_polygon is Relation.DISJOINT) assert equivalence( next_result is Relation.WITHIN, result is Relation.WITHIN or relation_with_first_polygon is Relation.WITHIN) assert equivalence( next_result is Relation.COMPONENT, result is Relation.COMPONENT or relation_with_first_polygon is Relation.COMPONENT) assert equivalence( next_result is Relation.CROSS, result is Relation.CROSS or relation_with_first_polygon is Relation.CROSS) assert equivalence( next_result is Relation.TOUCH, result is Relation.TOUCH and (relation_with_first_polygon is Relation.DISJOINT or relation_with_first_polygon is Relation.TOUCH) or result is Relation.DISJOINT and relation_with_first_polygon is Relation.TOUCH)
def test_convex_polygon(polygon: Polygon) -> None: polygon_with_convex_border = to_polygon_with_convex_border(polygon) assert (bool(polygon.holes) or implication( are_polygons_equivalent(polygon, polygon_with_convex_border), all(segment_in_polygon(segment, polygon) is Relation.ENCLOSED for segment in to_contour_separators(polygon.border))))
def test_connection_with_point_in_polygon(polygon_with_segment : Tuple[Polygon, Segment]) -> None: polygon, segment = polygon_with_segment result = segment_in_polygon(segment, polygon) assert implication(result is Relation.DISJOINT, point_in_polygon(segment.start, polygon) is point_in_polygon(segment.end, polygon) is Location.EXTERIOR)
def test_properties(polygon_with_segment: PolygonWithSegment) -> None: polygon, segment = polygon_with_segment result = symmetric_subtract_polygon_from_segment(segment, polygon) relation = segment_in_polygon(segment, polygon) assert (not is_mix(result) or (relation in (Relation.DISJOINT, Relation.TOUCH, Relation.CROSS) and (is_empty(result.discrete) and not is_empty(result.linear) and are_compounds_similar(result.shaped, polygon)))) assert (not is_polygon(result) or relation in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN))
def test_step(polygon_with_multisegment: Tuple[Polygon, Multisegment]) -> None: polygon, multisegment = polygon_with_multisegment first_segment, rest_multisegment = multisegment_pop_left(multisegment) result = multisegment_in_polygon(rest_multisegment, polygon) next_result = multisegment_in_polygon(multisegment, polygon) relation_with_first_segment = segment_in_polygon(first_segment, polygon) 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 not Relation.CROSS or result is Relation.DISJOINT and relation_with_first_segment is Relation.TOUCH) assert implication( result is Relation.DISJOINT and relation_with_first_segment is Relation.TOUCH or result is Relation.TOUCH and relation_with_first_segment is Relation.DISJOINT, next_result is Relation.TOUCH) assert equivalence( next_result is Relation.CROSS, result is Relation.CROSS or relation_with_first_segment is Relation.CROSS or (bool(rest_multisegment.segments) and result is Relation.DISJOINT or result is Relation.TOUCH) and (relation_with_first_segment is Relation.ENCLOSED or relation_with_first_segment is Relation.WITHIN) or (result is Relation.ENCLOSED or result is Relation.WITHIN) and (relation_with_first_segment is Relation.DISJOINT or relation_with_first_segment is Relation.TOUCH)) assert equivalence( next_result is Relation.COMPONENT, (not rest_multisegment.segments or result is Relation.COMPONENT) and relation_with_first_segment is Relation.COMPONENT) assert equivalence( next_result is Relation.ENCLOSED, not rest_multisegment.segments and relation_with_first_segment is Relation.ENCLOSED or (result is Relation.COMPONENT or result is Relation.ENCLOSED) and (relation_with_first_segment is Relation.ENCLOSED or relation_with_first_segment is Relation.WITHIN) or (result is Relation.ENCLOSED or result is Relation.WITHIN) and relation_with_first_segment is Relation.COMPONENT or result is Relation.WITHIN and relation_with_first_segment is Relation.ENCLOSED) assert equivalence( next_result is Relation.WITHIN, (not rest_multisegment.segments or result is Relation.WITHIN) and relation_with_first_segment is Relation.WITHIN)
def test_connection_with_segment_in_polygon( polygon_with_contour: Tuple[Polygon, Contour]) -> None: polygon, contour = polygon_with_contour result = contour_in_polygon(contour, polygon) contour_segments_relations = [ segment_in_polygon(segment, polygon) for segment in to_contour_segments(contour) ] assert equivalence( result is Relation.DISJOINT, all(relation is Relation.DISJOINT for relation in contour_segments_relations)) assert equivalence( result is Relation.TOUCH, all(relation is not Relation.CROSS and relation is not Relation.ENCLOSED and relation is not Relation.WITHIN for relation in contour_segments_relations) and any(relation is Relation.TOUCH for relation in contour_segments_relations)) assert equivalence( result is Relation.CROSS, any(relation is Relation.CROSS for relation in contour_segments_relations) or any(relation is Relation.TOUCH or relation is Relation.DISJOINT for relation in contour_segments_relations) and any(relation is Relation.ENCLOSED or relation is Relation.WITHIN for relation in contour_segments_relations)) assert equivalence( result is Relation.COMPONENT, all(relation is Relation.COMPONENT for relation in contour_segments_relations)) assert equivalence( result is Relation.ENCLOSED, all(relation is Relation.COMPONENT or relation is Relation.ENCLOSED or relation is Relation.WITHIN for relation in contour_segments_relations) and any(relation is Relation.COMPONENT or relation is Relation.ENCLOSED for relation in contour_segments_relations) and any(relation is Relation.ENCLOSED or relation is Relation.WITHIN for relation in contour_segments_relations)) assert equivalence( result is Relation.WITHIN, all(relation is Relation.WITHIN for relation in contour_segments_relations))
def test_reversals(polygon_with_segment: Tuple[Polygon, Segment]) -> None: polygon, segment = polygon_with_segment result = segment_in_polygon(segment, polygon) assert result is segment_in_polygon(reverse_segment(segment), polygon) assert result is segment_in_polygon(segment, reverse_polygon_border(polygon)) assert result is segment_in_polygon(segment, reverse_polygon_holes(polygon)) assert result is segment_in_polygon( segment, reverse_polygon_holes_contours(polygon)) assert result is segment_in_polygon(reverse_segment_coordinates(segment), reverse_polygon_coordinates(polygon))
def test_self(polygon: Polygon) -> None: assert all(segment_in_polygon(edge, polygon) is Relation.COMPONENT for edge in to_polygon_edges(polygon))