def test_self(multisegment: Multisegment) -> None:
    assert (multisegment_in_multisegment(multisegment, multisegment) is
            Relation.EQUAL)
    assert all(
        multisegment_in_multisegment(segment_to_multisegment(segment),
                                     multisegment) is Relation.COMPONENT
        for segment in multisegment.segments)
def test_rotations(
        multisegments_pair: Tuple[Multisegment, Multisegment]) -> None:
    left, right = multisegments_pair

    result = multisegment_in_multisegment(left, right)

    assert all(result is multisegment_in_multisegment(rotated, right)
               for rotated in multisegment_rotations(left))
    assert all(result is multisegment_in_multisegment(left, rotated)
               for rotated in multisegment_rotations(right))
def test_relations(
        multisegments_pair: Tuple[Multisegment, Multisegment]) -> None:
    left, right = multisegments_pair

    result = multisegment_in_multisegment(left, right)

    complement = multisegment_in_multisegment(right, left)
    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_reversals(
        multisegments_pair: Tuple[Multisegment, Multisegment]) -> None:
    left, right = multisegments_pair

    result = multisegment_in_multisegment(left, right)

    assert result is multisegment_in_multisegment(reverse_multisegment(left),
                                                  right)
    assert result is multisegment_in_multisegment(left,
                                                  reverse_multisegment(right))
    assert result is multisegment_in_multisegment(
        reverse_multisegment_coordinates(left),
        reverse_multisegment_coordinates(right))
def test_basic(multisegments_pair: Tuple[Multisegment, Multisegment]) -> None:
    left, right = multisegments_pair

    result = multisegment_in_multisegment(left, right)

    assert isinstance(result, Relation)
    assert result in SAME_LINEAR_RELATIONS
Example #6
0
    def relate(self, other: Compound[Coordinate]) -> Relation:
        """
        Finds relation between the contour and the other geometry.

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

        where ``vertices_count = len(self.vertices)``.

        >>> from gon.base import Contour, Point, Relation
        >>> contour = Contour([Point(0, 0), Point(1, 0), Point(0, 1)])
        >>> contour.relate(contour) is Relation.EQUAL
        True
        """
        return (relate_multipoint_to_linear_compound(other, self)
                if isinstance(other, Multipoint) else
                (segment_in_multisegment(other, self) if isinstance(
                    other, Segment) else
                 (multisegment_in_multisegment(other, self) if isinstance(
                     other, Linear) else other.relate(self).complement)))
Example #7
0
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))
Example #8
0
def test_properties(multisegments_pair: MultisegmentsPair) -> None:
    first, second = multisegments_pair

    result = complete_intersect_multisegments(first, second)

    result_points, result_segments = pack_non_shaped(result)
    assert all(
        point_in_multisegment(point, first) is point_in_multisegment(
            point, second) is Location.BOUNDARY for point in result_points)
    assert (multisegment_in_multisegment(first, second) is not Relation.TOUCH
            or bool(result_points))
    assert all(
        all(point in result_points or any(
            point == result_segment.start or point == result_segment.end
            for result_segment in result_segments)
            for second_segment in second.segments
            for point in segments_intersections(first_segment, second_segment))
        for first_segment in first.segments
        if (segment_in_multisegment(first_segment, second) in (
            Relation.TOUCH, Relation.CROSS)))
    assert all(
        segment_in_multisegment(result_segment, first) in (Relation.EQUAL,
                                                           Relation.COMPONENT)
        for result_segment in result_segments)
    assert all(
        segment_in_multisegment(result_segment, second) in (Relation.EQUAL,
                                                            Relation.COMPONENT)
        for result_segment in result_segments)
    assert all(
        to_sorted_segment(first_segment) in result_segments or any(
            segment_in_segment(result_segment, first_segment) is
            Relation.COMPONENT for result_segment in result_segments)
        for first_segment in first.segments if any(
            segments_relation(first_segment, second_segment) not in (
                Relation.CROSS, Relation.DISJOINT, Relation.TOUCH)
            for second_segment in second.segments))
Example #9
0
    def relate(self, other: Compound[Coordinate]) -> Relation:
        """
        Finds relation between the multisegment and the other geometry.

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

        where ``segments_count = len(self.segments)``.

        >>> from gon.base import Multisegment, Point, Segment
        >>> multisegment = Multisegment([Segment(Point(0, 0), Point(1, 0)),
        ...                              Segment(Point(0, 1), Point(1, 1))])
        >>> multisegment.relate(multisegment) is Relation.EQUAL
        True
        """
        return (relate_multipoint_to_linear_compound(other, self)
                if isinstance(other, Multipoint)
                else (segment_in_multisegment(other, self)
                      if isinstance(other, Segment)
                      else (multisegment_in_multisegment(other, self)
                            if isinstance(other, Multisegment)
                            else other.relate(self).complement)))
def test_step(multisegments_pair: Tuple[Multisegment, Multisegment]) -> None:
    left, right = multisegments_pair
    first_segment, rest_left = multisegment_pop_left(left)

    result = multisegment_in_multisegment(rest_left, right)
    next_result = multisegment_in_multisegment(left, right)

    relation_with_first_segment = segment_in_multisegment(first_segment, right)
    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_left.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 result is Relation.OVERLAP and
        (relation_with_first_segment is Relation.COMPONENT
         or relation_with_first_segment is Relation.OVERLAP)
        or bool(rest_left.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)
    assert implication(
        result is Relation.COMPOSITE
        or relation_with_first_segment is Relation.COMPOSITE
        or bool(rest_left.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_left.segments
        and relation_with_first_segment is Relation.EQUAL
        or result is relation_with_first_segment is Relation.COMPONENT)
    assert implication(
        not rest_left.segments
        and relation_with_first_segment is Relation.EQUAL,
        next_result is Relation.EQUAL)
    assert implication(next_result is Relation.COMPONENT,
                       (not rest_left.segments or result is Relation.COMPONENT)
                       and relation_with_first_segment is Relation.COMPONENT)
    assert implication(
        not rest_left.segments
        and relation_with_first_segment is Relation.COMPONENT,
        next_result is Relation.COMPONENT)
Example #11
0
def are_multisegments_equivalent(left: Multisegment,
                                 right: Multisegment) -> bool:
    return (not (left.segments or right.segments)
            or multisegment_in_multisegment(left, right) is Relation.EQUAL)