Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast method to find distance from point to closest edge of polygon

Setup

  • Function will need to provide the distance from a point to the closest edge of a polygon
  • Point is known to be inside of the polygon
  • Polygon can be convex or concave
  • Many points (millions) will need to be tested
  • Many separate polygons (dozens) will need to be ran through the function per point
  • Precalculated and persistently stored data structures are an option.
  • The final search function will be in C++

For the function implementation, I know a simple method would be to test the distance to all segments of the polygon using standard distance to line segment formulas. This option would be fairly slow at scale and I am confident there should be a better option.

My gut instinct is that there should be some very fast known algorithms for this type of function that would have been implemented in a game engine, but I'm not sure where to look.

I've found a reference for storing line segments in a quadtree, which would provide for very rapid searching and I think it could be used for my purpose to quickly narrow down which segment to look at as the closest segment and then would only need to calculate the distance to one line segment. https://people.cs.vt.edu/~shaffer/Papers/SametCVPR85.pdf

I've not been able to locate any code examples for how this would work. I don't mind implementing algorithms from scratch, but don't see the point in doing so if a working, tested code base exists.

I've been looking at a couple quadtree implementations and I think the way it would work is to create a quadtree per polygon and insert each polygon's line segments with a bounding box into the quadtree for that polygon.

The "query" portion of the function I would be making would then consist of creating a point as a very small bounding box, which would then be used to search against the quadtree structure, which would then find only the very closest portions of the polygon.

http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C

and

https://github.com/Esri/geometry-api-java/blob/master/src/main/java/com/esri/core/geometry/QuadTree.java

My real question would be, does this seem like a sound approach for a fast search time function?

Is there something that would work faster?

EDIT: I've been looking around and found some issues with using a quadtree. The way quadtrees work is good for collision detection, but isn't setup to allow for efficient nearest neighbor searching. https://gamedev.stackexchange.com/questions/14373/in-2d-how-do-i-efficiently-find-the-nearest-object-to-a-point

R-Trees look to be a better option. https://en.wikipedia.org/wiki/R-tree

and

efficient way to handle 2d line segments

Based on those posts, R-trees look like the winner. Also handy to see that C++ Boost already has them implemented. This looks close enough to what I was planning on doing that I'll go ahead and implement it and verify the results.

like image 498
thaspius Avatar asked Oct 23 '15 14:10

thaspius


People also ask

How can I find closest point on a polygon from a point?

The basic algorithm is to check every segment of the polygon and find the closest point for it. This will either be the perpedicular point (if it is on the segment) or one of the endpoints. After doing this for all segments, pick the point with the smallest total difference.

How proximity tools calculate distance?

The distance between any two features is calculated as the shortest separation between them, that is, where the two features are closest to each other.

What is the distance polygon?

Because a polygon is an area enclosed by an ordered collection of line segments, calculating the distance from a point to a polygon involves identifying the closest line segment to the point, and then Rule 2 is applied to get the distance.


1 Answers

EDIT: Since i have implemented an PMR quadtree, I see now, that the nearest neighbour search is a bit more complex than I described. If the quad search result for the search point would be empty then it gets more complex. I remeber a description somewhere in Hannan Sammets:Multidimensional search structure. Giving the answer below I had in mind searching for all objects withing a specified distance. This is easy for the PMR quadtree, but just finding the closest is more complex. Edit End

I would not use a R-Tree.
The weak point (and the strong point!) on R-trees is the separation of the space into rectangles.
There are three algorithms known to do that separation but none is well suited for all situations. R-trees are really complex to implement. Why then do it? Just because R-Trees can be twice fast than a quad tree when perfectly implemented. The speed difference between a quadtree and a R-Tree is not relevant. The monetary difference is. (If you have working code for both I would use the PMR quadtree, if you have only code for the R-Tree then use that, If you have none use the PMR Quadtree)

Quad trees (PMR) always work, and they are simple to implement.

Using the PMR quad tree, you just find all segments related to the search point. The result will be a few segments, then you just check them and ready you are.

People that tell quad trees are not suited or neighbour search, do not know that there are hundreds of different quad trees. The non suitability is just true for a point quad tree, not for the PMR one, which stores bounding boxes.

I once remebered the compelx description of finding the neighbour points in a POINT-Quadtree. For the PMR-quadtree I had nothing to do (for a search within a specified rectangular interval), no code change, Just iterate the result and find the closest.

I think that there are even better solutions than Quad tree or R-Tree for your spefic questions, but the point is that the PMR always work. Just implement it one time and use if for all spatial searches.

like image 138
AlexWien Avatar answered Oct 10 '22 05:10

AlexWien