simplefeatures icon indicating copy to clipboard operation
simplefeatures copied to clipboard

Use Bently-Ottmann algorithm for LineString's IsSimple

Open peterstace opened this issue 5 years ago • 3 comments

We're currently using a line sweep intersection algorithm, but it's not the most efficient. This is because we're just using a slice as the status data structure (which has linear lookup and insert time complexity).

For very particularly constructed inputs, this could result in long runtimes.

Tasks:

  • Add a benchmark test to exhibit the bad runtime for specially constructed inputs. This will basically be a linestring that is a zigzag 'stacked' vertically.
  • Implement the Bently-Ottmann algorithm for LineString's IsSimple method.
  • Confirm using the benchmark the improved result.

Resources:

  • https://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm
  • https://www.cs.tufts.edu/comp/163/notes05/seg_intersection_handout.pdf

peterstace avatar Apr 03 '20 20:04 peterstace

The original Bently Ottmann paper is http://www.itseng.org/research/papers/topics/VLSI_Physical_Design_Automation/Physical_Verification/DRC/Geometric_Intersection_Problems/1979-Bentley.pdf

peterstace avatar Jun 09 '20 21:06 peterstace

Note that we're now using an R-Tree for LineString's IsSimple method. This isn't as good as using the Bently-Ottmann algorithm, but does avoid the nasty O(n^2) behaviour we were seeing before (this change was made some time ago).

peterstace avatar Nov 02 '20 18:11 peterstace

It would actually make more sense to use the Shamos Hoey algorithm here, rather than the Bently-Ottmann algorithm.

Some details on the Shamos Hoey algorithm can be found here:

  • https://github.com/rowanwins/shamos-hoey/blob/master/ShamosHoey.pdf
  • https://github.com/rowanwins/shamos-hoey
  • http://geomalgorithms.com/a09-_intersect-3.html#Shamos-Hoey-Algorithm
  • https://www.npmjs.com/package/shamos-hoey
  • https://codereview.stackexchange.com/questions/69/shamos-hoey-algorithm-for-checking-the-self-intersection-of-a-closed-shape

We don't actually care where the intersections are, we only care if they exist or not. The Shamos Hoey algorithm is much simpler than the Bently-Ottmann algorithm.

peterstace avatar Dec 31 '20 23:12 peterstace