-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsweep-line.go
More file actions
210 lines (180 loc) · 5.69 KB
/
Copy pathsweep-line.go
File metadata and controls
210 lines (180 loc) · 5.69 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
package polygol
import (
"fmt"
splaytree "github.com/engelsjk/splay-tree"
)
/**
* NOTE: We must be careful not to change any segments while
* they are in the SplayTree. AFAIK, there's no way to tell
* the tree to rebalance itself - thus before splitting
* a segment that's in the tree, we remove it from the tree,
* do the split, then re-insert it. (Even though splitting a
* segment *shouldn't* change its correct position in the
* sweep line tree, the reality is because of rounding errors,
* it sometimes does.)
*/
type sweepLine struct {
tree *splaytree.SplayTree
queue *splaytree.SplayTree
segments []*segment
}
func newSweepLine(
queue *splaytree.SplayTree,
comparator func(a, b interface{}) int,
) *sweepLine {
sl := &sweepLine{}
if comparator == nil {
comparator = segmentCompare
}
sl.queue = queue
sl.tree = splaytree.New(comparator)
sl.segments = []*segment{}
return sl
}
func (sl *sweepLine) process(event *sweepEvent) ([]*sweepEvent, error) {
seg := event.segment
newEvents := []*sweepEvent{}
// if we've already been consumed by another segment,
// clean up our body parts and get out
if event.consumedBy != nil {
if event.isLeft {
sl.queue.Remove(event.otherSE)
} else {
sl.tree.Remove(seg)
}
return newEvents, nil
}
var node *splaytree.Node
if event.isLeft {
node = sl.tree.Insert(seg)
} else {
node = sl.tree.Find(seg)
}
if node == nil {
return nil, fmt.Errorf(
`Unable to find segment #%d [%f, %f] -> [%f, %f] in SweepLine tree.
Please submit a bug report.`,
seg.id,
seg.leftSE.point.x, seg.leftSE.point.y,
seg.rightSE.point.x, seg.rightSE.point.y,
)
}
prevNode := node
nextNode := node
var prevSeg *segment
var nextSeg *segment
// skip consumed segments still in tree
for prevSeg == nil {
prevNode = sl.tree.Prev(prevNode)
if prevNode == nil {
prevSeg = nil
break
} else if prevNode.Item().(*segment).consumedBy == nil {
prevSeg = prevNode.Item().(*segment)
}
}
// skip consumed segments still in tree
for nextSeg == nil {
nextNode = sl.tree.Next(nextNode)
if nextNode == nil {
break
} else if nextNode.Item().(*segment).consumedBy == nil {
nextSeg = nextNode.Item().(*segment)
}
}
if event.isLeft {
var prevSplitter, nextSplitter *point
// Check for intersections against the previous segment in the sweep line
prevSplitter, newEvents = sl.getSplitterFromIntersections(seg, prevSeg, newEvents)
// Check for intersections against the next segment in the sweep line
nextSplitter, newEvents = sl.getSplitterFromIntersections(seg, nextSeg, newEvents)
// For simplicity, even if we find more than one intersection we only
// spilt on the 'earliest' (sweep-line style) of the intersections.
// The other intersection will be handled in a future process().
if prevSplitter != nil || nextSplitter != nil {
var splitter *point
if prevSplitter == nil {
splitter = nextSplitter
} else if nextSplitter == nil {
splitter = prevSplitter
} else {
cmpSplitters := sweepEventComparePoints(prevSplitter, nextSplitter)
splitter = nextSplitter
if cmpSplitters <= 0 {
splitter = prevSplitter
}
}
// Rounding errors can cause changes in ordering,
// so remove affected segments and right sweep events before splitting
sl.queue.Remove(seg.rightSE)
newEvents = append(newEvents, seg.rightSE)
newEventsFromSplit := seg.split(splitter)
newEvents = append(newEvents, newEventsFromSplit...)
}
if len(newEvents) > 0 {
// We found some intersections, so re-do the current event to
// make sure sweep line ordering is totally consistent for later
// use with the segment 'prev' pointers
sl.tree.Remove(seg)
newEvents = append(newEvents, event)
} else {
// done with left event
sl.segments = append(sl.segments, seg)
seg.prev = prevSeg
}
} else {
// event.isRight
// since we're about to be removed from the sweep line, check for
// intersections between our previous and next segments
if prevSeg != nil && nextSeg != nil {
inter := prevSeg.getIntersection(nextSeg)
if inter != nil {
if !prevSeg.isAnEndpoint(inter) {
newEventsFromSplit := sl.splitSafely(prevSeg, inter)
newEvents = append(newEvents, newEventsFromSplit...)
}
if !nextSeg.isAnEndpoint(inter) {
newEventsFromSplit := sl.splitSafely(nextSeg, inter)
newEvents = append(newEvents, newEventsFromSplit...)
}
}
}
sl.tree.Remove(seg)
}
return newEvents, nil
}
// splitSafely splits a segment that is currently in the datastructures
// IE - a segment other than the one that is currently being processed.
func (sl *sweepLine) splitSafely(segment *segment, point *point) []*sweepEvent {
// Rounding errors can cause changes in ordering,
// so remove affected segments and right sweep events before splitting
// removeNode() doesn't work, so have re-find the seg
// https://github.com/w8r/splay-tree/pull/5
sl.tree.Remove(segment)
rightSE := segment.rightSE
sl.queue.Remove(rightSE)
newEvents := segment.split(point)
newEvents = append(newEvents, rightSE)
// splitting can trigger consumption
if segment.consumedBy == nil {
sl.tree.Insert(segment)
}
return newEvents
}
func (sl *sweepLine) getSplitterFromIntersections(seg, otherSeg *segment, events []*sweepEvent) (*point, []*sweepEvent) {
if otherSeg == nil {
return nil, events
}
var otherSplitter *point
otherInter := otherSeg.getIntersection(seg)
if otherInter != nil {
if !seg.isAnEndpoint(otherInter) {
otherSplitter = otherInter
}
if !otherSeg.isAnEndpoint(otherInter) {
newEventsFromSplit := sl.splitSafely(otherSeg, otherInter)
events = append(events, newEventsFromSplit...)
}
}
return otherSplitter, events
}