Skip to content

Commit

Permalink
Complete 'segments_intersections' function
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Mar 25, 2021
1 parent 33f2da1 commit c1026a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
7 changes: 0 additions & 7 deletions bentley_ottmann/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from typing import (Iterable,
Sequence,
Tuple,
TypeVar)

from .hints import Ids

_T = TypeVar('_T')


Expand All @@ -16,9 +13,5 @@ def pairwise(ids: Iterable[_T]) -> Iterable[Tuple[_T, _T]]:
value = next_value


def to_ids_pairs(id_: int, other_ids: Ids) -> Sequence[Tuple[int, int]]:
return [to_sorted_pair(id_, other_id) for other_id in other_ids]


def to_sorted_pair(first: _T, second: _T) -> Tuple[_T, _T]:
return (first, second) if first < second else (second, first)
35 changes: 18 additions & 17 deletions bentley_ottmann/planar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from itertools import chain
from itertools import (chain,
product)
from typing import (Callable,
Dict,
Hashable,
Expand All @@ -16,7 +17,7 @@
from .core import (bentley_ottmann as _bentley_ottmann,
shamos_hoey as _shamos_hoey)
from .core.utils import (pairwise as _pairwise,
to_ids_pairs as _to_ids_pairs)
to_sorted_pair as _to_sorted_pair)


def edges_intersect(contour: Contour) -> bool:
Expand Down Expand Up @@ -217,15 +218,14 @@ def segments_intersections(segments: Sequence[Segment]
result = {}
context = _get_context()

def segments_intersector(first_start: Point,
first_end: Point,
second_start: Point,
second_end: Point,
relation: _Relation,
intersector
: Callable[[Point, Point, Point, Point], Point]
= context.segments_intersection
) -> Tuple[Point, ...]:
def to_intersections(first_start: Point,
first_end: Point,
second_start: Point,
second_end: Point,
relation: _Relation,
intersector: Callable[[Point, Point, Point, Point],
Point]
= context.segments_intersection) -> Tuple[Point, ...]:
if relation is _Relation.TOUCH or relation is _Relation.CROSS:
return intersector(first_start, first_end, second_start,
second_end),
Expand All @@ -240,12 +240,13 @@ def segments_intersector(first_start: Point,
segments_ids = event.segments_ids
for relation, other_segments_ids in event.relations.items():
for other_segment_ids in other_segments_ids:
other_segment_id = other_segment_ids[0]
ids_pairs = _to_ids_pairs(other_segment_id, segments_ids)
other_segment = segments[other_segment_id]
for point in segments_intersector(segment_start, segment_end,
other_segment.start,
other_segment.end, relation):
ids_pairs = {_to_sorted_pair(segment_id, other_segment_id)
for segment_id, other_segment_id
in product(segments_ids, other_segment_ids)}
other_segment = segments[other_segment_ids[0]]
for point in to_intersections(segment_start, segment_end,
other_segment.start,
other_segment.end, relation):
result.setdefault(point, set()).update(ids_pairs)
for ids_pair in _pairwise(segments_ids):
result.setdefault(segment_start, set()).add(ids_pair)
Expand Down

0 comments on commit c1026a4

Please sign in to comment.