-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathBentleyOttmann.cs
397 lines (304 loc) · 13.2 KB
/
BentleyOttmann.cs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;
namespace Advanced.Algorithms.Geometry;
/// <summary>
/// Bentley-Ottmann sweep line algorithm to find line intersections.
/// </summary>
public class BentleyOttmann
{
private readonly PointComparer pointComparer;
internal readonly double Tolerance;
private RedBlackTree<Event> currentlyTrackedLines;
private BHeap<Event> eventQueue;
private HashSet<Event> eventQueueLookUp;
private Dictionary<Point, HashSet<Tuple<Event, Event>>> intersectionEvents;
private HashSet<Event> otherLines;
private Dictionary<Event, Event> rightLeftEventLookUp;
internal Line SweepLine;
private HashSet<Event> verticalAndHorizontalLines;
public BentleyOttmann(int precision = 5)
{
pointComparer = new PointComparer();
Tolerance = Math.Round(Math.Pow(0.1, precision), precision);
}
private void Initialize(IEnumerable<Line> lineSegments)
{
SweepLine = new Line(new Point(0, 0), new Point(0, int.MaxValue), Tolerance);
currentlyTrackedLines = new RedBlackTree<Event>(true, pointComparer);
intersectionEvents = new Dictionary<Point, HashSet<Tuple<Event, Event>>>(pointComparer);
verticalAndHorizontalLines = new HashSet<Event>();
otherLines = new HashSet<Event>();
rightLeftEventLookUp = lineSegments
.Select(x =>
{
if (x.Left.X < 0 || x.Left.Y < 0 || x.Right.X < 0 || x.Right.Y < 0)
throw new Exception("Negative coordinates are not supported.");
return new KeyValuePair<Event, Event>(
new Event(x.Left, pointComparer, EventType.Start, x, this),
new Event(x.Right, pointComparer, EventType.End, x, this)
);
}).ToDictionary(x => x.Value, x => x.Key);
eventQueueLookUp = new HashSet<Event>(rightLeftEventLookUp.SelectMany(x => new[]
{
x.Key,
x.Value
}));
eventQueue = new BHeap<Event>(SortDirection.Ascending, eventQueueLookUp, new EventQueueComparer());
}
public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSegments)
{
Initialize(lineSegments);
while (eventQueue.Count > 0)
{
var currentEvent = eventQueue.Extract();
eventQueueLookUp.Remove(currentEvent);
SweepTo(currentEvent);
switch (currentEvent.Type)
{
case EventType.Start:
//special case
if (verticalAndHorizontalLines.Count > 0)
foreach (var line in verticalAndHorizontalLines)
{
var intersection = FindIntersection(currentEvent, line);
RecordIntersection(currentEvent, line, intersection);
}
//special case
if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal)
{
verticalAndHorizontalLines.Add(currentEvent);
foreach (var line in otherLines)
{
var intersection = FindIntersection(currentEvent, line);
RecordIntersection(currentEvent, line, intersection);
}
break;
}
otherLines.Add(currentEvent);
currentlyTrackedLines.Insert(currentEvent);
var lower = currentlyTrackedLines.NextLower(currentEvent);
var upper = currentlyTrackedLines.NextHigher(currentEvent);
var lowerIntersection = FindIntersection(currentEvent, lower);
RecordIntersection(currentEvent, lower, lowerIntersection);
EnqueueIntersectionEvent(currentEvent, lowerIntersection);
var upperIntersection = FindIntersection(currentEvent, upper);
RecordIntersection(currentEvent, upper, upperIntersection);
EnqueueIntersectionEvent(currentEvent, upperIntersection);
break;
case EventType.End:
currentEvent = rightLeftEventLookUp[currentEvent];
//special case
if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal)
{
verticalAndHorizontalLines.Remove(currentEvent);
break;
}
otherLines.Remove(currentEvent);
lower = currentlyTrackedLines.NextLower(currentEvent);
upper = currentlyTrackedLines.NextHigher(currentEvent);
currentlyTrackedLines.Delete(currentEvent);
var upperLowerIntersection = FindIntersection(lower, upper);
RecordIntersection(lower, upper, upperLowerIntersection);
EnqueueIntersectionEvent(currentEvent, upperLowerIntersection);
break;
case EventType.Intersection:
var intersectionLines = intersectionEvents[currentEvent];
foreach (var lines in intersectionLines)
{
//special case
if (lines.Item1.Segment.IsHorizontal || lines.Item1.Segment.IsVertical
|| lines.Item2.Segment.IsHorizontal ||
lines.Item2.Segment.IsVertical)
continue;
SwapBstNodes(currentlyTrackedLines, lines.Item1, lines.Item2);
var upperLine = lines.Item1;
var upperUpper = currentlyTrackedLines.NextHigher(upperLine);
var newUpperIntersection = FindIntersection(upperLine, upperUpper);
RecordIntersection(upperLine, upperUpper, newUpperIntersection);
EnqueueIntersectionEvent(currentEvent, newUpperIntersection);
var lowerLine = lines.Item2;
var lowerLower = currentlyTrackedLines.NextLower(lowerLine);
var newLowerIntersection = FindIntersection(lowerLine, lowerLower);
RecordIntersection(lowerLine, lowerLower, newLowerIntersection);
EnqueueIntersectionEvent(currentEvent, newLowerIntersection);
}
break;
}
}
return intersectionEvents.ToDictionary(x => x.Key,
x => x.Value.SelectMany(y => new[] { y.Item1.Segment, y.Item2.Segment })
.Distinct().ToList());
}
private void SweepTo(Event currentEvent)
{
SweepLine = new Line(new Point(currentEvent.X, 0), new Point(currentEvent.X, int.MaxValue), Tolerance);
}
internal void SwapBstNodes(RedBlackTree<Event> currentlyTrackedLines, Event value1, Event value2)
{
var node1 = currentlyTrackedLines.Find(value1).Item1;
var node2 = currentlyTrackedLines.Find(value2).Item1;
if (node1 == null || node2 == null) throw new Exception("Value1, Value2 or both was not found in this BST.");
var tmp = node1.Value;
node1.Value = node2.Value;
node2.Value = tmp;
currentlyTrackedLines.NodeLookUp[node1.Value] = node1;
currentlyTrackedLines.NodeLookUp[node2.Value] = node2;
}
private void EnqueueIntersectionEvent(Event currentEvent, Point intersection)
{
if (intersection == null) return;
var intersectionEvent = new Event(intersection, pointComparer, EventType.Intersection, null, this);
if (intersectionEvent.X > SweepLine.Left.X
|| intersectionEvent.X == SweepLine.Left.X
&& intersectionEvent.Y > currentEvent.Y)
if (!eventQueueLookUp.Contains(intersectionEvent))
{
eventQueue.Insert(intersectionEvent);
eventQueueLookUp.Add(intersectionEvent);
}
}
private Point FindIntersection(Event a, Event b)
{
if (a == null || b == null
|| a.Type == EventType.Intersection
|| b.Type == EventType.Intersection)
return null;
return a.Segment.Intersection(b.Segment, Tolerance);
}
private void RecordIntersection(Event line1, Event line2, Point intersection)
{
if (intersection == null) return;
var existing = intersectionEvents.ContainsKey(intersection)
? intersectionEvents[intersection]
: new HashSet<Tuple<Event, Event>>();
if (line1.Segment.Slope.CompareTo(line2.Segment.Slope) > 0)
existing.Add(new Tuple<Event, Event>(line1, line2));
else
existing.Add(new Tuple<Event, Event>(line2, line1));
intersectionEvents[intersection] = existing;
}
}
//point type
internal enum EventType
{
Start = 0,
Intersection = 1,
End = 2
}
/// <summary>
/// A custom object representing start/end/intersection point.
/// </summary>
internal class Event : Point, IComparable
{
private readonly PointComparer pointComparer;
private readonly double tolerance;
internal BentleyOttmann Algorithm;
internal Point LastIntersection;
internal Line LastSweepLine;
//The full line only if not an intersection event
internal Line Segment;
internal EventType Type;
internal Event(Point eventPoint, PointComparer pointComparer, EventType eventType,
Line lineSegment, BentleyOttmann algorithm)
: base(eventPoint.X, eventPoint.Y)
{
tolerance = algorithm.Tolerance;
this.pointComparer = pointComparer;
Type = eventType;
Segment = lineSegment;
Algorithm = algorithm;
}
public int CompareTo(object that)
{
if (Equals(that)) return 0;
var thatEvent = that as Event;
var line1 = Segment;
var line2 = thatEvent.Segment;
Point intersectionA;
if (Type == EventType.Intersection)
{
intersectionA = this;
}
else
{
if (LastSweepLine == Algorithm.SweepLine)
{
intersectionA = LastIntersection;
}
else
{
intersectionA = LineIntersection.FindIntersection(line1, Algorithm.SweepLine, tolerance);
LastSweepLine = Algorithm.SweepLine;
LastIntersection = intersectionA;
}
}
Point intersectionB;
if (Type == EventType.Intersection)
{
intersectionB = thatEvent;
}
else
{
if (thatEvent.LastSweepLine == thatEvent.Algorithm.SweepLine)
{
intersectionB = thatEvent.LastIntersection;
}
else
{
intersectionB = LineIntersection.FindIntersection(line2, thatEvent.Algorithm.SweepLine, tolerance);
thatEvent.LastSweepLine = thatEvent.Algorithm.SweepLine;
thatEvent.LastIntersection = intersectionB;
}
}
var result = intersectionA.Y.CompareTo(intersectionB.Y);
if (result != 0) return result;
//if Y is same use slope as comparison
var slope1 = line1.Slope;
//if Y is same use slope as comparison
var slope2 = line2.Slope;
result = slope1.CompareTo(slope2);
if (result != 0) return result;
//if slope is the same use diff of X co-ordinate
result = line1.Left.X.CompareTo(line2.Left.X);
if (result != 0) return result;
//if diff of X co-ordinate is same use diff of Y co-ordinate
result = line1.Left.Y.CompareTo(line2.Left.Y);
//at this point this is guaranteed to be not same.
//since we don't let duplicate lines with input HashSet of lines.
//see line equals override in Line class.
return result;
}
public override bool Equals(object that)
{
if (that == this) return true;
var thatEvent = that as Event;
if (Type != EventType.Intersection && thatEvent.Type == EventType.Intersection
|| Type == EventType.Intersection && thatEvent.Type != EventType.Intersection)
return false;
if (Type == EventType.Intersection && thatEvent.Type == EventType.Intersection)
return pointComparer.Equals(this, thatEvent);
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
//Used to override event comparison when using BMinHeap for Event queue.
internal class EventQueueComparer : Comparer<Event>
{
public override int Compare(Event a, Event b)
{
//same object
if (a == b) return 0;
//compare X
var result = a.X.CompareTo(b.X);
if (result != 0) return result;
//Left event first, then intersection and finally right.
result = a.Type.CompareTo(b.Type);
if (result != 0) return result;
return a.Y.CompareTo(b.Y);
}
}