-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_intersection.py
More file actions
330 lines (258 loc) · 10.2 KB
/
Copy path_intersection.py
File metadata and controls
330 lines (258 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""Line segment intersection using Bentley-Ottmann sweep line and brute force."""
import heapq
from enum import IntEnum
import numpy as np
# ---------------------------------------------------------------------------
# Event types — ordering matters for the heap (left < intersection < right)
# ---------------------------------------------------------------------------
class _EventType(IntEnum):
LEFT = 0
INTERSECTION = 1
RIGHT = 2
# ---------------------------------------------------------------------------
# Geometric helpers
# ---------------------------------------------------------------------------
_EPS = 1e-9
def _segment_intersection(s1, s2):
"""Parametric intersection of two segments. Returns [x, y] or None."""
x1, y1 = s1[0]
x2, y2 = s1[1]
x3, y3 = s2[0]
x4, y4 = s2[1]
denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if abs(denom) < _EPS:
# Parallel or collinear — check for overlap
return _collinear_intersection(s1, s2)
t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom
u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom
if -_EPS <= t <= 1 + _EPS and -_EPS <= u <= 1 + _EPS:
ix = x1 + t * (x2 - x1)
iy = y1 + t * (y2 - y1)
return [ix, iy]
return None
def _collinear_intersection(s1, s2):
"""Check if two collinear segments overlap, return one endpoint of overlap or None."""
x1, y1 = s1[0]
x2, y2 = s1[1]
x3, y3 = s2[0]
x4, y4 = s2[1]
# Check if segments are on the same line
cross = (x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)
if abs(cross) > _EPS:
return None
# Project onto the longer axis
dx = abs(x2 - x1)
dy = abs(y2 - y1)
if dx >= dy:
a_lo, a_hi = min(x1, x2), max(x1, x2)
b_lo, b_hi = min(x3, x4), max(x3, x4)
else:
a_lo, a_hi = min(y1, y2), max(y1, y2)
b_lo, b_hi = min(y3, y4), max(y3, y4)
lo = max(a_lo, b_lo)
hi = min(a_hi, b_hi)
if lo > hi + _EPS:
return None
# Return the start of overlap as the intersection point
if dx >= dy:
t = (lo - x1) / (x2 - x1) if abs(x2 - x1) > _EPS else 0
return [x1 + t * (x2 - x1), y1 + t * (y2 - y1)]
else:
t = (lo - y1) / (y2 - y1) if abs(y2 - y1) > _EPS else 0
return [x1 + t * (x2 - x1), y1 + t * (y2 - y1)]
def _y_at_x(seg, x):
"""Y-coordinate where segment crosses the sweep line at x."""
x1, y1 = seg[0]
x2, y2 = seg[1]
if abs(x2 - x1) < _EPS:
return min(y1, y2)
t = (x - x1) / (x2 - x1)
return y1 + t * (y2 - y1)
# ---------------------------------------------------------------------------
# Sweep-line status structure
# ---------------------------------------------------------------------------
class _SweepLineStatus:
"""Maintains sorted list of active segments ordered by y at current sweep x."""
def __init__(self):
self._segments = [] # list of segment indices
self._sweep_x = 0.0
self._seg_data = None # reference to segments array
def set_context(self, segments):
self._seg_data = segments
def set_sweep_x(self, x):
self._sweep_x = x
def _sort_key(self, seg_idx):
return _y_at_x(self._seg_data[seg_idx], self._sweep_x)
def insert(self, seg_idx):
key = self._sort_key(seg_idx)
# Binary search for insertion point
lo, hi = 0, len(self._segments)
while lo < hi:
mid = (lo + hi) // 2
if self._sort_key(self._segments[mid]) < key - _EPS:
lo = mid + 1
else:
hi = mid
self._segments.insert(lo, seg_idx)
def remove(self, seg_idx):
try:
self._segments.remove(seg_idx)
except ValueError:
pass
def _find_pos(self, seg_idx):
for i, s in enumerate(self._segments):
if s == seg_idx:
return i
return -1
def swap(self, seg_a, seg_b):
pos_a = self._find_pos(seg_a)
pos_b = self._find_pos(seg_b)
if pos_a >= 0 and pos_b >= 0:
self._segments[pos_a] = seg_b
self._segments[pos_b] = seg_a
def neighbors(self, seg_idx):
"""Return (above, below) neighbor indices or None."""
pos = self._find_pos(seg_idx)
if pos < 0:
return None, None
above = self._segments[pos - 1] if pos > 0 else None
below = self._segments[pos + 1] if pos < len(self._segments) - 1 else None
return above, below
# ---------------------------------------------------------------------------
# Main class
# ---------------------------------------------------------------------------
class SegmentIntersection:
"""Find intersection points among a set of 2D line segments.
Provides both a Bentley-Ottmann sweep line method and a brute-force
O(n^2) pairwise check.
Attributes:
segments (np.ndarray): Array of segments with shape (n, 2, 2).
"""
def __init__(self, segments):
"""Initialize with a set of 2D line segments.
Args:
segments: Collection of segments as a list, tuple, or numpy array.
Each segment is [[x1, y1], [x2, y2]].
Raises:
pydantic.ValidationError: If fewer than 2 segments, zero-length
segment, non-numeric, or wrong shape.
"""
from cgeom.elements.models import SegmentIntersectionInput
validated = SegmentIntersectionInput(segments=segments)
self.segments = np.array(validated.segments)
def find_intersections(self):
"""Find all intersection points using Bentley-Ottmann sweep line.
Returns:
list[list[float]]: Unique intersection points as [[x, y], ...].
"""
segs = self._normalize_segments()
n = len(segs)
if n < 2:
return []
# Build event queue
events = []
for i in range(n):
lx, ly = segs[i][0]
rx, ry = segs[i][1]
heapq.heappush(events, (lx, _EventType.LEFT, ly, i, -1))
heapq.heappush(events, (rx, _EventType.RIGHT, ry, i, -1))
status = _SweepLineStatus()
status.set_context(segs)
found_points = {} # (round_x, round_y) -> [x, y]
found_pairs = set() # frozenset(i, j) to avoid duplicate events
def _check_and_add(seg_a, seg_b):
if seg_a is None or seg_b is None:
return
pair = frozenset((seg_a, seg_b))
if pair in found_pairs:
return
pt = _segment_intersection(segs[seg_a], segs[seg_b])
if pt is not None:
rkey = (round(pt[0], 9), round(pt[1], 9))
if rkey not in found_points:
found_points[rkey] = pt
found_pairs.add(pair)
heapq.heappush(
events, (pt[0], _EventType.INTERSECTION, pt[1], seg_a, seg_b)
)
while events:
x, etype, y, s1, s2 = heapq.heappop(events)
status.set_sweep_x(x)
if etype == _EventType.LEFT:
status.insert(s1)
above, below = status.neighbors(s1)
_check_and_add(s1, above)
_check_and_add(s1, below)
elif etype == _EventType.RIGHT:
above, below = status.neighbors(s1)
_check_and_add(above, below)
status.remove(s1)
elif etype == _EventType.INTERSECTION:
status.swap(s1, s2)
# After swap, check new neighbors
above_s1, below_s1 = status.neighbors(s1)
above_s2, below_s2 = status.neighbors(s2)
_check_and_add(s1, above_s1)
_check_and_add(s1, below_s1)
_check_and_add(s2, above_s2)
_check_and_add(s2, below_s2)
return list(found_points.values())
def find_intersections_brute_force(self):
"""Find all intersection points using O(n^2) pairwise check.
Returns:
list[list[float]]: Unique intersection points as [[x, y], ...].
"""
segs = self.segments.tolist()
n = len(segs)
found = {}
for i in range(n):
for j in range(i + 1, n):
pt = _segment_intersection(segs[i], segs[j])
if pt is not None:
rkey = (round(pt[0], 9), round(pt[1], 9))
if rkey not in found:
found[rkey] = pt
return list(found.values())
def get_intersection_pairs(self):
"""Return intersection details with segment indices.
Returns:
list[tuple[int, int, list[float]]]: Each entry is
(seg_i, seg_j, [x, y]).
"""
segs = self.segments.tolist()
n = len(segs)
pairs = []
for i in range(n):
for j in range(i + 1, n):
pt = _segment_intersection(segs[i], segs[j])
if pt is not None:
pairs.append((i, j, pt))
return pairs
def get_segments(self):
"""Return segments as a plain list.
Returns:
list[list[list[float]]]: Segments as [[[x1,y1],[x2,y2]], ...].
"""
return self.segments.tolist()
def plot(self, title="Segment Intersections"):
"""Deprecated: use cgeom.visualization.plot_intersections() instead."""
import warnings
warnings.warn(
"SegmentIntersection.plot() is deprecated. "
"Use cgeom.visualization.plot_intersections(si_obj, title) instead.",
DeprecationWarning,
stacklevel=2,
)
from cgeom.visualization import plot_intersections
plot_intersections(self, title)
def _normalize_segments(self):
"""Return segments normalized left-to-right (or bottom-to-top for vertical)."""
segs = self.segments.tolist()
normalized = []
for seg in segs:
(x1, y1), (x2, y2) = seg
if x1 > x2 + _EPS or (abs(x1 - x2) < _EPS and y1 > y2):
normalized.append([[x2, y2], [x1, y1]])
else:
normalized.append([[x1, y1], [x2, y2]])
return normalized