项目作者: eadf

项目描述 :
Line intersection sweep-line algorithm
高级语言: Rust
项目地址: git://github.com/eadf/intersect2d.rs.git
创建时间: 2021-02-04T21:48:58Z
项目社区:https://github.com/eadf/intersect2d.rs

开源协议:GNU Affero General Public License v3.0

下载


crates.io
Documentation
Workflow
Workflow
dependency status
license

intersect2d

After watching Philipp Kindermann’s excellent sweep-line
videos I think I finally understand how this algorithm works.

This is my humble take on an implementation of the segment line
intersection sweep-line algorithm.

The library crate also contains a line intersection function.

This crate is educational, not ready for any production purpose. It has been adopted as a glam and cgmath library here: Linestring.

Rusty voronoi

Interactive step-by-step example:

  1. cargo run --example fltk_gui --features console_trace

Intersection function API example:

  1. use intersect2d::{intersect, Intersection};
  2. let line1 = geo::Line::<f64>::from([(100.0,150.),(150.0,100.)]);
  3. let line2 = geo::Line::<f64>::from([(100.0,150.),(150.0,100.)]);
  4. let rv = intersect(&line1, &line2);
  5. match rv {
  6. Some(Intersection::Intersection(_a)) => panic!("expected an overlap"),
  7. Some(Intersection::OverLap(a)) => println!("{:?}", a),
  8. None => panic!("expected an overlap"),
  9. }
  10. // you can also get a single intersection point from the Intersection enum.
  11. // Albeit geometrically incorrect, it makes things easy
  12. println!("{:?}", rv.unwrap().single());

Sweep-line API example:

  1. let lines = vec![
  2. geo::Line::<f64>::from([(200.0,200.),(350.0,300.)]),
  3. geo::Line::<f64>::from([(400.0,200.),(250.0,300.)]),
  4. ];
  5. let results = intersect2d::algorithm::AlgorithmData::<f64>::default()
  6. .with_ignore_end_point_intersections(false)?
  7. .with_lines(lines.into_iter())?
  8. .compute()?;
  9. for (point, line) in results {
  10. println!("Intersection @{:?} Involved lines:{:?}", point, line);
  11. }

Detection of self-intersecting geo::LineString:

  1. let coordinates = vec![(200., 200.), (300., 300.), (400., 200.), (200., 300.)];
  2. let line_string = geo::LineString::<f32>::from(coordinates);
  3. // Obviously this example only makes sense for LinesStrings with many points.
  4. // A simple brute force O(n²) intersection test will be faster than this O(nlog(n)+k)
  5. // sweep-line algorithm if n is small enough.
  6. let result = intersect2d::algorithm::AlgorithmData::<f32>::default()
  7. .with_ignore_end_point_intersections(true)?
  8. .with_stop_at_first_intersection(true)?
  9. .with_lines(line_string.lines())?
  10. .compute()?;
  11. for (p, l) in result {
  12. println!("Intersection detected @{:?} Involved lines:{:?}", p, l);
  13. }

or using the SelfIntersectingExclusive trait:

  1. // SelfIntersectingExclusive does not report endpoint intersections
  2. use intersect2d::SelfIntersectingExclusive;
  3. let coordinates = vec![(200., 200.), (300., 300.), (400., 200.), (200., 300.)];
  4. let line_string = geo::LineString::from(coordinates);
  5. if line_string.is_self_intersecting()? {
  6. println!("Intersection detected");
  7. }
  8. for intersections in line_string.self_intersections()? {
  9. println!("Intersection: {:?}", intersections);
  10. }

You can also check a bunch of geo::Line for self intersections using the SelfIntersectingInclusive trait:

  1. // SelfIntersectingInclusive reports endpoint intersections
  2. use intersect2d::SelfIntersectingInclusive;
  3. let lines = vec![
  4. geo::Line::<f64>::from([(200.0,200.),(350.0,300.)]),
  5. geo::Line::<f64>::from([(400.0,200.),(250.0,300.)]),
  6. ];
  7. if lines.is_self_intersecting_inclusive()? {
  8. println!("Intersection detected");
  9. }
  10. for intersections in lines.self_intersections_inclusive()? {
  11. println!("Intersection: {:?}", intersections);
  12. }

Todo

  • Benchmark and optimize
  • Stable overlapping co-linear line detection