forked from gisalgs/geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_seg_eventqueue.py
78 lines (70 loc) · 2.13 KB
/
line_seg_eventqueue.py
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
"""
Python classes for Event and EventQueue for the Bentley-Ottmann algorithm.
Contact:
Ningchuan Xiao
The Ohio State University
Columbus, OH
"""
__author__ = "Ningchuan Xiao <[email protected]>"
from .point import *
class Event:
"""
An event in the sweep line algorithm. Each Event object
stores the event point and the line segments associated with the
point.
"""
def __init__(self, p=None):
self.edges = [] # line segments associated with the event
self.p = p # event point
def __repr__(self):
return "{0}{1}".format(self.p,self.edges)
class EventQueue:
"""
An event queue in the sweep line algorithm.
"""
def __init__(self, lset):
"""
Constructor of EventQueue.
Input
lset: a list of Segment objects. The left point of
each segment is used to create an event
Output
A sorted list of events as a member of this class
"""
if lset == None:
return
self.events = []
for l in lset:
e0 = Event(l.lp)
inx = self.find(e0)
if inx == -1:
e0.edges.append(l)
self.events.append(e0)
else:
self.events[inx].edges.append(l)
e1 = Event(l.rp)
if self.find(e1) == -1:
self.events.append(e1)
self.events.sort(key=lambda e: e.p)
def add(self, e):
"""
Adds event e to the queue, updates the list of events
"""
self.events.append(e)
self.events.sort(key=lambda e: e.p)
def find(self, t):
"""
Returns the index of event t if it is in the queue.
Otherwise, returns -1.
"""
if isinstance(t, Event):
p = t.p
elif isinstance(t, Point):
p = t
else: return -1
for e in self.events:
if p == e.p:
return self.events.index(e)
return -1
def is_empty(self):
return len(self.events) == 0