def test_properties(polygon_with_segment: PolygonWithSegment) -> None: polygon, segment = polygon_with_segment result = intersect_segment_with_polygon(segment, polygon) result_points, result_segments = pack_non_shaped(result) assert not result_points 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_reversals(segments_pair: Tuple[Segment, Segment]) -> None: left_segment, right_segment = segments_pair result = segment_in_segment(left_segment, right_segment) assert result is segment_in_segment(reverse_segment(left_segment), right_segment) assert result is segment_in_segment(left_segment, reverse_segment(right_segment)) assert result is segment_in_segment( reverse_segment_coordinates(left_segment), reverse_segment_coordinates(right_segment))
def test_relations(segments_pair: Tuple[Segment, Segment]) -> None: left_segment, right_segment = segments_pair result = segment_in_segment(left_segment, right_segment) complement = segment_in_segment(right_segment, left_segment) assert equivalence(result is complement, result in SYMMETRIC_SAME_LINEAR_RELATIONS) assert equivalence( result is not complement, result.complement is complement and result in ASYMMETRIC_LINEAR_RELATIONS and complement in ASYMMETRIC_LINEAR_RELATIONS)
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_properties( multipolygon_with_multisegment: MultipolygonWithMultisegment) -> None: multipolygon, multisegment = multipolygon_with_multisegment result = intersect_multisegment_with_multipolygon(multisegment, multipolygon) assert all( segment_in_multisegment(segment, multisegment) in (Relation.EQUAL, Relation.COMPONENT) for segment in result.segments) assert all( segment_in_multipolygon(segment, multipolygon) in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN) for 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_multipolygon(segment, multipolygon) in ( Relation.CROSS, Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN)))
def test_properties( multipolygon_with_multisegment: MultipolygonWithMultisegment) -> None: multipolygon, multisegment = multipolygon_with_multisegment result = subtract_multipolygon_from_multisegment(multisegment, multipolygon) assert all( segment_in_multisegment(segment, multisegment) in (Relation.EQUAL, Relation.COMPONENT) for segment in result.segments) assert all( segment_in_multipolygon(segment, multipolygon) in (Relation.DISJOINT, Relation.TOUCH) for 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_multipolygon(segment, multipolygon) in ( Relation.DISJOINT, Relation.CROSS)))
def test_basic(segments_pair: Tuple[Segment, Segment]) -> None: left_segment, right_segment = segments_pair result = segment_in_segment(left_segment, right_segment) assert isinstance(result, Relation) assert result in SAME_LINEAR_RELATIONS
def relate(self, other: Compound[Coordinate]) -> Relation: """ Finds relation between the segment and the other geometry. Time complexity: ``O(1)`` Memory complexity: ``O(1)`` >>> from gon.base import Point, Segment >>> segment = Segment(Point(0, 0), Point(2, 0)) >>> segment.relate(segment) is Relation.EQUAL True """ return ( relate_multipoint_to_linear_compound(other, self) if isinstance( other, Multipoint) else (segment_in_segment(other, self, context=self._context) if isinstance(other, Segment) else other.relate(self).complement))
def test_properties(multisegments_pair: MultisegmentsPair) -> None: left_multisegment, right_multisegment = multisegments_pair result = complete_intersect_multisegments(left_multisegment, right_multisegment) result_multipoint, result_multisegment = result assert all( point_in_multisegment(point, left_multisegment) is point_in_multisegment(point, right_multisegment) is Relation.COMPONENT for point in result_multipoint.points) assert (multisegment_in_multisegment(left_multisegment, right_multisegment) is not Relation.TOUCH or bool(result_multipoint)) assert all( all(point in result_multipoint or any( point in segment for segment in result_multisegment.segments) for right_segment in right_multisegment.segments for point in segments_intersections(left_segment, right_segment)) for left_segment in left_multisegment.segments if (segment_in_multisegment(left_segment, right_multisegment) in ( Relation.TOUCH, Relation.CROSS))) assert all( segment_in_multisegment(segment, left_multisegment) in ( Relation.EQUAL, Relation.COMPONENT) for segment in result_multisegment.segments) assert all( segment_in_multisegment(segment, right_multisegment) in ( Relation.EQUAL, Relation.COMPONENT) for segment in result_multisegment.segments) assert all( to_sorted_segment(left_segment) in result_multisegment.segments or any( segment_in_segment(segment, left_segment) is Relation.COMPONENT for segment in result_multisegment.segments) for left_segment in left_multisegment.segments if any( segments_relation(left_segment.start, left_segment.end, right_segment.start, right_segment.end) not in ( Relation.CROSS, Relation.DISJOINT, Relation.TOUCH) for right_segment in right_multisegment.segments))
def test_self(segment: Segment) -> None: assert segment_in_segment(segment, segment) is Relation.EQUAL