/ src / sweep / mod.rs
mod.rs
 1  //! The sweep-line implementation.
 2  //!
 3  //! The details of this implementation are described in the `docs` directory.
 4  //! The main entry point is [`Sweeper`], which computes all the intersection
 5  //! points between a collection of line segments, and makes them available
 6  //! sweep-line by sweep-line.
 7  
 8  mod output_event;
 9  mod range;
10  mod sweep_line;
11  
12  pub use output_event::OutputEvent;
13  pub use range::{SegmentsConnectedAtX, SweepLineRange, SweepLineRangeBuffers};
14  pub use sweep_line::{ChangedInterval, SweepLine, SweepLineBuffers, Sweeper};
15  
16  use crate::Segments;
17  
18  /// Runs the sweep-line algorithm, calling the provided callback on every output point.
19  pub fn sweep<C: FnMut(f64, OutputEvent)>(segments: &Segments, eps: f64, mut callback: C) {
20      let mut state = Sweeper::new(segments, eps);
21      let mut range_bufs = SweepLineRangeBuffers::default();
22      let mut line_bufs = SweepLineBuffers::default();
23      while let Some(mut line) = state.next_line(&mut line_bufs) {
24          let y = line.y();
25          while let Some(mut range) = line.next_range(&mut range_bufs, segments) {
26              while let Some(events) = range.events() {
27                  for ev in events {
28                      callback(y, ev);
29                  }
30              }
31          }
32      }
33  }