linestring 0.16.0

line string toolbox
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2021,2023 lacklustr@protonmail.com https://github.com/eadf

// This file is part of the linestring crate.

/*
Copyright (c) 2021,2023 lacklustr@protonmail.com https://github.com/eadf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

or

Copyright 2021,2023 lacklustr@protonmail.com https://github.com/eadf

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#[cfg(test)]
mod tests;

use crate::{LinestringError, linestring_2d::Line2};
use rustc_hash::FxHashSet;
use smallvec::{SmallVec, smallvec};
use std::{cmp::Ordering, collections::BTreeMap, convert::identity, fmt, fmt::Debug};
use vector_traits::{approx::*, num_traits::Float, prelude::*};

#[derive(Clone, Copy)]
struct SiteEventKey<T: GenericVector2> {
    pub pos: T,
    pub index: Option<u32>,
}

impl<T: GenericVector2> Debug for SiteEventKey<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("")
            .field(&self.pos.x())
            .field(&self.pos.y())
            .field(&self.index)
            .finish()
    }
}

impl<T: GenericVector2> Ord for SiteEventKey<T> {
    /// It should be impossible for a !is_finite() number to be added to SiteEventKey
    #[inline(always)]
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl<T: GenericVector2> PartialOrd for SiteEventKey<T>
where
    T::Scalar: UlpsEq,
{
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if ulps_eq!(&self.pos.y(), &other.pos.y()) {
            if ulps_eq!(&self.pos.x(), &other.pos.x()) {
                Some(Ordering::Equal)
            } else {
                self.pos.x().partial_cmp(&other.pos.x())
            }
        } else {
            self.pos.y().partial_cmp(&other.pos.y())
        }
    }
}

impl<T: GenericVector2> Eq for SiteEventKey<T> {}

impl<T: GenericVector2> PartialEq for SiteEventKey<T> {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        self.pos.is_ulps_eq(
            other.pos,
            T::Scalar::default_epsilon(),
            T::Scalar::default_max_ulps(),
        )
    }
}

/// A container struct that keeps track of the lines around a pivot point.
/// It only stores the lines with highest x value left of pivot point, and lines with lowest x
/// value right of the point. Secondarily it prioritizes according to slope of the line, lines
/// leaning towards pivot point have priority.
struct MinMax<T: GenericVector2> {
    best_left: Option<T::Scalar>,
    slope: MinMaxSlope<T>,
    best_right: Option<T::Scalar>,
}

impl<T: GenericVector2> MinMax<T>
where
    T::Scalar: UlpsEq,
{
    fn new() -> Self {
        Self {
            best_left: None,
            best_right: None,
            slope: MinMaxSlope::new(),
        }
    }

    /// keep track of the candidates closest (on both sides) to pivot_x
    fn update(
        &mut self,
        pivot_x: T::Scalar,
        candidate_x: T::Scalar,
        candidate_slope: T::Scalar,
        candidate_index: u32,
    ) {
        if candidate_x < pivot_x {
            /*println!(
                "Left:looking at {} candidate_x:{} candidate_slope:{}",
                candidate_index, candidate_x, candidate_slope
            );*/
            // handle left side
            if let Some(current_min) = self.best_left {
                if ulps_eq!(&current_min, &candidate_x) {
                    self.slope
                        .update_left(false, candidate_slope, candidate_index);
                } else if current_min < candidate_x {
                    // this candidate is better.
                    self.slope
                        .update_left(true, candidate_slope, candidate_index);
                    self.best_left = Some(candidate_x);
                } else {
                    /*println!(
                        "Left2:rejecting {} candidate_x:{} current_x:{} candidate_slope:{}, current_slope:{}",
                        candidate_index, candidate_x, current_min, candidate_slope, self.slope.best_left.unwrap()
                    )*/
                }
            } else {
                // First sample
                self.best_left = Some(candidate_x);
                self.slope
                    .update_left(false, candidate_slope, candidate_index);
            }
        } else if candidate_x > pivot_x {
            /*println!(
                "Right:looking at {} candidate_x:{} candidate_slope:{}",
                candidate_index, candidate_x, candidate_slope
            );*/
            // handle right side
            if let Some(current_max) = self.best_right {
                if ulps_eq!(&current_max, &candidate_x) {
                    self.slope
                        .update_right(false, candidate_slope, candidate_index);
                } else if current_max > candidate_x {
                    // this candidate is better.
                    self.slope
                        .update_right(true, candidate_slope, candidate_index);
                    self.best_right = Some(candidate_x);
                } else {
                    /*println!(
                        "Right2:rejecting {} candidate_x:{} current_x:{} candidate_slope:{}, current_slope:{}",
                        candidate_index, candidate_x, current_max, candidate_slope, self.slope.best_right.unwrap()
                    )*/
                }
            } else {
                // First sample
                self.best_right = Some(candidate_x);
                self.slope
                    .update_right(false, candidate_slope, candidate_index);
            }
        }
    }

    /// clear all data
    fn clear(&mut self) {
        self.best_left = None;
        self.best_right = None;
        self.slope.clear();
    }
}

struct MinMaxSlope<T: GenericVector2> {
    best_left: Option<T::Scalar>, // slope
    candidates_left: Vec<u32>,

    best_right: Option<T::Scalar>, // slope
    candidates_right: Vec<u32>,
}

impl<T: GenericVector2> MinMaxSlope<T>
where
    T::Scalar: UlpsEq,
{
    fn new() -> Self {
        Self {
            best_left: None,
            candidates_left: Vec::<_>::new(),
            best_right: None,
            candidates_right: Vec::<_>::new(),
        }
    }

    /// sort candidates based on slope, keep only the ones with 'flattest' angle to the left and right
    fn update_both(&mut self, vertices: &[T], candidate_index: u32, lines: &[(u32, u32)]) {
        let line = lines[candidate_index as usize];
        let line_start = vertices[line.0 as usize];
        let line_end = vertices[line.1 as usize];

        let candidate_slope = if ulps_eq!(&line_end.y(), &line_start.y()) {
            T::Scalar::INFINITY
        } else {
            (line_end.x() - line_start.x()) / (line_end.y() - line_start.y())
        };

        self.update_left(false, candidate_slope, candidate_index);
        self.update_right(false, candidate_slope, candidate_index);
    }

    /// sort candidates based on slope, keep only the best to the left -> lowest abs(negative slope value)
    fn update_left(&mut self, clear: bool, candidate_slope: T::Scalar, candidate_index: u32) {
        if clear {
            self.candidates_left.clear();
            self.best_left = None;
        }
        /*println!(
            "Left:looking at {} candidate_slope:{}",
            candidate_index, candidate_slope
        );*/
        // handle left side
        if let Some(current_slope) = self.best_left {
            if ulps_eq!(&current_slope, &candidate_slope) {
                // this candidate is just as good as the others already found
                self.candidates_left.push(candidate_index);
            } else if candidate_slope < current_slope {
                // this candidate is better.
                self.candidates_left.clear();
                self.candidates_left.push(candidate_index);
                self.best_left = Some(candidate_slope);
            } else {
                /*println!(
                    "Left1:rejecting {} candidate_slope:{}, current_slope:{}",
                    candidate_index, candidate_slope, current_slope
                )*/
            }
        } else {
            // First sample
            self.best_left = Some(candidate_slope);
            self.candidates_left.push(candidate_index);
        }
        /*println!(
            "Left: best slope is {:?} ,best_left:{}",
            self.candidates_left,
            self.best_left.unwrap()
        )*/
    }

    /// sort candidates based on slope, keep only the best to the left -> lowest positive slope value
    fn update_right(&mut self, clear: bool, candidate_slope: T::Scalar, candidate_index: u32) {
        /*println!(
            "Right:looking at {} candidate_slope:{}",
            candidate_index, candidate_slope
        );*/
        if clear {
            self.candidates_right.clear();
            self.best_right = None;
        }
        // handle right side
        if let Some(current_slope) = self.best_right {
            if ulps_eq!(&current_slope, &candidate_slope) {
                // this candidate is just as good as the others already found
                self.candidates_right.push(candidate_index);
            } else if candidate_slope > current_slope {
                // this candidate is better.
                self.candidates_right.clear();
                self.candidates_right.push(candidate_index);
                self.best_right = Some(candidate_slope);
            } else {
                /*println!(
                    "Right1:rejecting {} candidate_slope:{}, current_slope:{}",
                    candidate_index, candidate_slope, current_slope
                )*/
            }
        } else {
            // First sample
            self.best_right = Some(candidate_slope);
            self.candidates_right.push(candidate_index);
        }
        /*println!(
            "Right: best slope is {:?} ,best_right:{}",
            self.candidates_right,
            self.best_right.unwrap()
        )*/
    }

    /// clear all data
    fn clear(&mut self) {
        self.best_left = None;
        self.candidates_left.clear();

        self.best_right = None;
        self.candidates_right.clear();
    }
}

/// SiteEvents contains the events happening at a specific point.
/// Line segments have their start and end positions arranged so that line.start.y < line.end.y
/// Sorting is based on their Y-coordinate, secondary the X-coordinate. (line.start.x < line.end.x)
///
/// The 'drop' list contains the line segments that ends in the event point.
/// The 'add' list contains the line segments that starts in the event point.
/// The 'intersection' list contains the line segments that intersects at the event point.
///
struct SiteEvent {
    drop: Option<Vec<u32>>,
    add: Option<Vec<u32>>,
    intersection: Option<SmallVec<[u32; 1]>>,
}

impl SiteEvent {
    pub(crate) fn with_intersection(i: &[u32]) -> Self {
        Self {
            drop: None,
            add: None,
            intersection: Some(i.into()),
        }
    }

    pub(crate) fn with_drop(l: &[u32]) -> Self {
        Self {
            drop: Some(l.to_vec()),
            add: None,
            intersection: None,
        }
    }

    pub(crate) fn with_add(l: &[u32]) -> Self {
        Self {
            drop: None,
            add: Some(l.to_vec()),
            intersection: None,
        }
    }

    #[allow(dead_code)]
    pub fn get_intersections(&self) -> &Option<SmallVec<[u32; 1]>> {
        &self.intersection
    }
}

/// Returns *one* point of intersection between the `sweepline` and `other`
/// Second return value is the slope of the line
fn sweepline_intersection<T: GenericVector2>(
    vertices: &[T],
    sweepline: T,
    other: (u32, u32),
) -> Option<(T::Scalar, T::Scalar)> {
    // line equation: y=slope*x+d => d=y-slope*x => x = (y-d)/slope
    let other_start = vertices[other.0 as usize];
    let other_end = vertices[other.1 as usize];

    let y1 = other_start.y();
    let y2 = other_end.y();
    let x1 = other_start.x();
    let x2 = other_end.x();
    if ulps_eq!(&y1, &y2) {
        // horizontal line: return to the point right of sweepline.x, if any
        // Any point to the left are supposedly already handled.
        return (sweepline.x() < x2).then_some((x2, T::Scalar::ZERO));
    }

    if ulps_eq!(&x1, &x2) {
        return Some((x1, T::Scalar::INFINITY));
    }

    let slope = (y2 - y1) / (x2 - x1);

    let d = y1 - slope * x1;
    Some(((sweepline.y() - d) / slope, slope))
}

/// Contains the data the sweep-line intersection algorithm needs to operate.
/// Some of these containers are stored inside an Option. This makes it possible
/// to take() them and make the borrow-checker happy.
/// ```
/// use itertools::Itertools;
/// use linestring::linestring_2d::indexed_intersection::IntersectionTester;
/// use vector_traits::glam::{self, vec2}; // or use the crate glam directly
///
/// let vertices: Vec<glam::Vec2> = vec![
///     vec2(651.3134, 410.21536),
///     vec2(335.7384, 544.54614),
///     vec2(154.29922, 363.10654),
///     vec2(425.06284, 255.50153),
///     vec2(651.1434, 387.16595),
///     vec2(250.0, 300.0),
/// ];
/// // edges are counted in the order they are presented to IntersectionTester
/// let example_edges: Vec<(u32, u32)> = (0..vertices.len() as u32)
///         .tuples()
///         .collect();
/// let input_vertices_len = vertices.len();
/// let (output_vertices, result_iter) = IntersectionTester::new(vertices)
///     .with_ignore_end_point_intersections(true)?
///     .with_stop_at_first_intersection(true)?
///     .with_edges( example_edges.iter())?
///     .compute().unwrap();
///
/// assert_eq!(output_vertices.len(),input_vertices_len+1);
/// assert_eq!(1, result_iter.len());
/// for (intersection_vertex_id, affected_edges) in result_iter {
///     println!(
///       "Found an intersection at {:?}, affected edges:{:?}",
///        output_vertices[intersection_vertex_id as usize], affected_edges
///     );
/// }
/// # Ok::<(), linestring::LinestringError>(())
/// ```
pub struct IntersectionTester<T: GenericVector2> {
    // sweep-line position
    sweepline_pos: T,
    // sweep-line position: an index into self.vertices. Is usually a value, only at end and start
    // of the algorithm it will be set to None
    sweepline_pos_index: Option<u32>,
    // Stop when first intersection is found
    stop_at_first_intersection: bool,
    // Allow start&end points to intersect
    // i.e. don't report them as an intersections.
    // An endpoint intersecting any other point of another line will still be
    // counted as an intersection.
    pub ignore_end_point_intersections: bool,
    // The unhandled events
    site_events: Option<BTreeMap<SiteEventKey<T>, SiteEvent>>,
    // The input geometry. These lines are re-arranged so that Line.start.y <= Line.end.y
    // These are never changed while the algorithm is running.
    lines: Vec<(u32, u32)>,
    // The array containing the actual vertices
    vertices: Vec<T>,
}

impl<T: GenericVector2> IntersectionTester<T> {
    pub fn new(vertices: Vec<T>) -> Self {
        Self {
            vertices,
            sweepline_pos: T::new_2d(-T::Scalar::max_value(), -T::Scalar::max_value()),
            sweepline_pos_index: None,
            stop_at_first_intersection: false,
            ignore_end_point_intersections: false,
            site_events: Some(BTreeMap::new()),
            lines: Vec::<(u32, u32)>::new(),
        }
    }

    #[inline]
    pub fn get_sweepline_pos(&self) -> (T, Option<u32>) {
        (self.sweepline_pos, self.sweepline_pos_index)
    }

    #[inline]
    pub fn get_lines(&self) -> &[(u32, u32)] {
        &self.lines
    }

    pub fn with_stop_at_first_intersection(mut self, value: bool) -> Result<Self, LinestringError> {
        self.stop_at_first_intersection = value;
        Ok(self)
    }

    pub fn with_ignore_end_point_intersections(
        mut self,
        value: bool,
    ) -> Result<Self, LinestringError> {
        self.ignore_end_point_intersections = value;
        Ok(self)
    }

    /// Add data to the input lines.
    /// Sort the end point according to the order of SiteEventKey.
    /// Populate the event queue
    pub fn with_edges<'a, IT>(mut self, input_iter: IT) -> Result<Self, LinestringError>
    where
        IT: IntoIterator<Item = &'a (u32, u32)>,
    {
        let mut site_events = self.site_events.take().ok_or_else(|| {
            LinestringError::InternalError("with_lines() could not take 'site_events'".to_string())
        })?;

        for (index, aline) in input_iter.into_iter().enumerate() {
            let index = index as u32;
            let mut start_index = aline.0;
            let mut start = self.vertices[start_index as usize];
            let mut end_index = aline.1;
            let mut end = self.vertices[end_index as usize];

            if !(start.x().is_finite()
                && start.y().is_finite()
                && end.x().is_finite()
                && end.y().is_finite())
            {
                return Err(LinestringError::InvalidData(
                    "Some of the points are infinite".to_string(),
                ));
            }

            // Re-arrange so that:
            // SiteEvent.pos.start < SiteEvent.pos.end (primary ordering: pos.y, secondary: pos.x)
            if !(SiteEventKey {
                pos: start,
                index: Some(start_index),
            })
            .lt(&(SiteEventKey {
                pos: end,
                index: Some(end_index),
            })) {
                std::mem::swap(&mut start_index, &mut end_index);
                std::mem::swap(&mut start, &mut end);
            };

            self.lines.push(*aline);

            let key_start = SiteEventKey {
                pos: start,
                index: Some(start_index),
            };
            let key_end = SiteEventKey {
                pos: end,
                index: Some(end_index),
            };

            // start points goes into the site_event::add list
            if let Some(event) = site_events.get_mut(&key_start) {
                let mut lower = event.add.take().map_or(Vec::<u32>::default(), identity);
                lower.push(index);
                event.add = Some(lower);
            } else {
                let event = SiteEvent::with_add(&[index]);
                let _ = site_events.insert(key_start, event);
            }

            // end points goes into the site_event::drop list
            if let Some(event) = site_events.get_mut(&key_end) {
                let mut upper = event.drop.take().map_or(Vec::<u32>::default(), identity);
                upper.push(index);
                event.drop = Some(upper);
            } else {
                let event = SiteEvent::with_drop(&[index]);
                let _ = site_events.insert(key_end, event);
            }
        }
        self.site_events = Some(site_events);
        Ok(self)
    }

    ///
    /// Add a new intersection event to the event queue
    ///
    #[inline]
    fn add_intersection_event(
        &self,
        site_events: &mut BTreeMap<SiteEventKey<T>, SiteEvent>,
        position: &SiteEventKey<T>,
        intersecting_lines: &[u32],
    ) {
        if let Some(event) = site_events.get_mut(position) {
            // there were already events at this point
            let mut intersections_added = 0;
            for new_intersection in intersecting_lines.iter() {
                // only add this line as an intersection if the intersection lies
                // at the interior of the line (no end point)
                let i_line = self.lines[*new_intersection as usize];
                if position.pos.is_ulps_eq(
                    self.vertices[i_line.0 as usize],
                    T::Scalar::default_epsilon(),
                    T::Scalar::default_max_ulps(),
                ) || position.pos.is_ulps_eq(
                    self.vertices[i_line.1 as usize],
                    T::Scalar::default_epsilon(),
                    T::Scalar::default_max_ulps(),
                ) {
                    continue;
                }

                if let Some(prev_intersections) = &mut event.intersection {
                    prev_intersections.push(*new_intersection);
                } else {
                    let new_vec = smallvec![*new_intersection];
                    event.intersection = Some(new_vec);
                }
                intersections_added += 1;
            }
            if intersections_added > 0 {
                let mut intersections = event.intersection.take().unwrap();
                intersections.sort_unstable();
                intersections.dedup();
                event.intersection = Some(intersections);
            }
        } else {
            // this is the first event at this point
            let event = SiteEvent::with_intersection(intersecting_lines);
            let _ = site_events.insert(*position, event);
        }
    }

    //#[allow(unused_assignments)]
    #[allow(clippy::type_complexity)]
    /// Handles input events, returns an iterator containing the results when done.
    /// Return a new vertex list, an iterator of all the detected intersections:
    /// (intersection point index, a Vec of affected edges)
    pub fn compute(
        mut self,
    ) -> Result<(Vec<T>, impl ExactSizeIterator<Item = (u32, Vec<u32>)>), LinestringError> {
        // make the borrow checker happy by breaking the link between self and all the
        // containers and their iterators.
        let mut site_events = self.site_events.take().ok_or_else(|| {
            LinestringError::InternalError(
                "compute() could not take ownership of 'site_events'".to_string(),
            )
        })?;
        let mut active_lines =
            FxHashSet::<u32>::with_capacity_and_hasher(site_events.len(), Default::default());
        // A list of intersection points and the line segments involved in each intersection
        let mut result: BTreeMap<SiteEventKey<T>, Vec<u32>> = BTreeMap::default();
        // The 'best' lines surrounding the event point but not directly connected to the point.
        let mut neighbour_priority = MinMax::new();

        // The 'best' lines directly connected to the event point.
        let mut connected_priority = MinMaxSlope::new();

        loop {
            if let Some((key, event)) = {
                //#[cfg(feature = "map_first_last")]
                //{
                site_events.pop_first()
                //}
                /*#[cfg(not(feature = "map_first_last"))]
                {
                    // emulate pop_first
                    if let Some((first_key, _)) = site_events.iter().next() {
                        let first_key = *first_key;
                        let v = site_events.remove(&first_key).ok_or_else(|| {
                            LinestringError::InternalError(format!(
                                "Found a key to pop but could not remove the value. {}:{}",
                                file!(),
                                line!()
                            ))
                        })?;
                        Some((first_key, v))
                    } else {
                        None
                    }
                }*/
            } {
                self.handle_event(
                    &key,
                    &event,
                    &mut active_lines,
                    &mut neighbour_priority,
                    &mut connected_priority,
                    &mut site_events,
                    &mut result,
                )?;
                if !result.is_empty() && self.stop_at_first_intersection {
                    break;
                }
            } else {
                self.sweepline_pos = T::new_2d(T::Scalar::max_value(), T::Scalar::max_value());
                self.sweepline_pos_index = None;
                break;
            }
        }
        // no need to put the borrowed containers back
        Ok((
            self.vertices,
            result.into_iter().map(|x| (x.0.index.unwrap(), x.1)),
        ))
    }

    #[inline(always)]
    #[allow(clippy::too_many_arguments)]
    fn handle_event(
        &mut self,
        key: &SiteEventKey<T>,
        event: &SiteEvent,
        active_lines: &mut FxHashSet<u32>,
        neighbour_priority: &mut MinMax<T>,
        connected_priority: &mut MinMaxSlope<T>,
        site_events: &mut BTreeMap<SiteEventKey<T>, SiteEvent>,
        result: &mut BTreeMap<SiteEventKey<T>, Vec<u32>>,
    ) -> Result<(), LinestringError> {
        self.sweepline_pos = key.pos;
        self.sweepline_pos_index = key.index;

        let removed_active_lines = event.drop.iter().flatten().count();
        let added_active_lines = event.add.iter().flatten().count();
        let intersections_found = event.intersection.iter().flatten().count();

        // Handle points converging at this point:
        // If sum of number of items in 'add' + 'drop' > 1 they must intersect at this point
        if self.ignore_end_point_intersections {
            // only report end point intersections if they collide with
            // a calculated intersection point
            if intersections_found > 0 {
                if removed_active_lines > 0 {
                    self.report_intersections_to_result(
                        result,
                        self.sweepline_pos_index.unwrap(),
                        event.drop.iter().flatten(),
                    );
                }
                if added_active_lines > 0 {
                    self.report_intersections_to_result(
                        result,
                        self.sweepline_pos_index.unwrap(),
                        event.add.iter().flatten(),
                    );
                }
                self.report_intersections_to_result(
                    result,
                    self.sweepline_pos_index.unwrap_or_else(|| {
                        println!("self.sweepline_pos:{:?}", self.sweepline_pos);
                        panic!();
                    }),
                    event.intersection.iter().flatten(),
                );
            }
        } else if removed_active_lines + added_active_lines + intersections_found > 1 {
            // report *all* intersections, including the end-to-end intersections
            if removed_active_lines > 0 {
                self.report_intersections_to_result(
                    result,
                    self.sweepline_pos_index.unwrap(),
                    event.drop.iter().flatten(),
                );
            }
            if added_active_lines > 0 {
                self.report_intersections_to_result(
                    result,
                    self.sweepline_pos_index.unwrap(),
                    event.add.iter().flatten(),
                );
            }
            if intersections_found > 0 {
                self.report_intersections_to_result(
                    result,
                    self.sweepline_pos_index.unwrap(),
                    event.intersection.iter().flatten(),
                );
            }
        }

        // remove 'drop' lines from the active lines
        for line_index in event.drop.iter().flatten() {
            let _ = active_lines.remove(line_index);
        }

        //
        // calculate the sweep-line status for this point (aka the 'leftright' struct)
        //
        neighbour_priority.clear();

        'active_lines: for line_index in active_lines.iter() {
            for i in event.intersection.iter().flatten() {
                // lines intersecting at this point can never be left/right candidates
                if i == line_index {
                    continue 'active_lines;
                }
            }

            //print!("(sweepline_intersection id={:?}", line_index);
            if let Some((intersection_x, intersection_slope)) = sweepline_intersection(
                &self.vertices,
                self.sweepline_pos,
                *self.lines.get(*line_index as usize).unwrap(),
            ) {
                //println!(" @{}^{})", intersection_x, intersection_slope);
                neighbour_priority.update(
                    self.sweepline_pos.x(),
                    intersection_x,
                    intersection_slope,
                    *line_index,
                );
            }
        }

        // add the newly found lines
        for l in event.add.iter().flatten() {
            let _ = active_lines.insert(*l);
        }

        if intersections_found + added_active_lines == 0 {
            // this event didn't spawn off new events, check neighbours
            if !neighbour_priority.slope.candidates_left.is_empty()
                && !neighbour_priority.slope.candidates_right.is_empty()
            {
                self.find_new_events(
                    &neighbour_priority.slope.candidates_left,
                    &neighbour_priority.slope.candidates_right,
                    site_events,
                )?;
            }
        } else {
            connected_priority.clear();
            for l in event.add.iter().flatten() {
                connected_priority.update_both(&self.vertices, *l, &self.lines);
            }
            for l in event.intersection.iter().flatten() {
                connected_priority.update_both(&self.vertices, *l, &self.lines);
            }

            if !neighbour_priority.slope.candidates_left.is_empty() {
                self.find_new_events(
                    &neighbour_priority.slope.candidates_left,
                    &connected_priority.candidates_left,
                    site_events,
                )?;
            }
            if !neighbour_priority.slope.candidates_right.is_empty() {
                self.find_new_events(
                    &neighbour_priority.slope.candidates_right,
                    &connected_priority.candidates_right,
                    site_events,
                )?;
            }
        }
        Ok(())
    }

    fn find_new_events(
        &mut self,
        left: &[u32],
        right: &[u32],
        site_events: &mut BTreeMap<SiteEventKey<T>, SiteEvent>,
    ) -> Result<(), LinestringError> {
        for left_i in left.iter() {
            for right_i in right.iter() {
                let left_l = self.lines[*left_i as usize];
                let right_l = self.lines[*right_i as usize];
                let left_l_end = self.vertices[left_l.1 as usize];
                let right_l_end = self.vertices[right_l.1 as usize];

                if left_l_end.is_ulps_eq(
                    right_l_end,
                    T::Scalar::default_epsilon(),
                    T::Scalar::default_max_ulps(),
                ) {
                    // if endpoints are equal they will already be in the event queue
                    continue;
                }

                let left_l_as_line = Line2::new(
                    self.vertices[left_l.0 as usize],
                    self.vertices[left_l.1 as usize],
                );
                let right_l_as_line = Line2::new(
                    self.vertices[right_l.0 as usize],
                    self.vertices[right_l.1 as usize],
                );
                if let Some(intersection_p) = left_l_as_line.intersection_point(right_l_as_line) {
                    let intersection_p = intersection_p.single();
                    if !intersection_p.x().is_finite() || !intersection_p.y().is_finite() {
                        return Err(LinestringError::InvalidData(format!(
                            "Input data has intersection at invalid position x:{:?}, y:{:?}",
                            intersection_p.x(),
                            intersection_p.y()
                        )));
                    }
                    // don't allow intersection 'behind' or 'at' current sweep-line position
                    if intersection_p.y() >= self.sweepline_pos.y()
                        && !(intersection_p.y() == self.sweepline_pos.y()
                            && intersection_p.x() < self.sweepline_pos.x())
                        && !intersection_p.is_ulps_eq(
                            self.sweepline_pos,
                            T::Scalar::default_epsilon(),
                            T::Scalar::default_max_ulps(),
                        )
                    {
                        let intersection_p_index = self.vertices.len() as u32;
                        self.vertices.push(intersection_p);
                        self.add_intersection_event(
                            site_events,
                            &SiteEventKey {
                                pos: intersection_p,
                                index: Some(intersection_p_index),
                            },
                            &[*left_i, *right_i],
                        )
                    }
                }
            }
        }
        Ok(())
    }

    fn report_intersections_to_result<'b, I: Iterator<Item = &'b u32>>(
        &mut self,
        result: &mut BTreeMap<SiteEventKey<T>, Vec<u32>>,
        pos_index: u32,
        intersecting_lines: I,
    ) {
        let pos = self.vertices[pos_index as usize];
        let key = SiteEventKey {
            pos,
            index: Some(pos_index),
        };

        let value = if let Some(value) = result.get_mut(&key) {
            value
        } else {
            let _ = result.insert(key, Vec::default());
            result.get_mut(&key).unwrap()
        };

        for line in intersecting_lines {
            value.push(*line);
        }
        value.sort_unstable();
    }
}