Closest pair of features between two 2d concave polygons - algorithm

The problem is to find the closest features between two 2d concave polygons. The features can be vertex, edge. So result can be any combination of features. Is there any simple solution with complexity better than O(m*n)? where m, n - number of edges of polygons respectively. The polygons are coplanar.

An algorithm in O(n.log(m)) seems to exists, please see this paper, and this question.
An optimization of mine you could try: (not tested)
If your polygons are most of the time far apart enough, you can build the two convex hull and fall back on the easiest problem of finding the Hausdorff distance between two convex polygons (solution in O(n+m)). If the distance is 0, you have to fall back to the O(m.log(n)) case, but it's worth it if you are most of the time in the "convex hull" case with a positive distance.
Post-Scriptum. I just realized that in order of the postulate to work, you also need to check that the closest features from the convex hulls belongs to the original concave polygon. If not, it's easy to find a counter-example (imagine a polygon in shape of the letter C with another round just nearby: CO).
The updated postulate is then: The Hausdorff distance d between two concave polygons is the Hausdorff distance between their convex hulls, if d > 0, and both closest features are part of the original polygons.
The proof of this is left as an exercice to the reader.

Related

Is there any algorithm for covering a concave polygon(contains holes) with convex polygons

This can be approached in two ways :
i) by partitioning the given polygon into convex polygons such that there is no
overlap between the convex polygons
ii) by covering the given polygon using convex polygons such that their union
gives the original polygon. In this case there can be overlap between
the convex polygons
Although partitioning covers the entire polygon, number of convex polygons can be reduced by second approach. It is also known that covering a concave polygon(second approach) with minimal number of convex polygons is NP-Hard.
I'm specifically looking for algorithms based on second approach mentioned above,but number of convex polygons may not be minimal.
As already mentioned by MBo and Yves Daoust in the comments to your questions. Polygon decomposition into convex polygons can be done by triangulation (or trapezoidal decomposition). This will result for an n vertex simple polygon P with n-2 (interior) triangles, i.e., is linear in the number of vertices.
Another way to construct a convex decomposition would be to use a generalized motorcycle graph. I would assume there must be a simpler way though!
The main idea is to start a motorcycle m for every reflex vertex r in P. Every motorcycle m drives with a given speed in a given direction and leaves a trace behind. If another motorcycle meets such a trace it crashes, i.e., stops but leaves the trace.
Generalized refers to the embedding in P and that the polygon boundaries function as walls where the motorcycles also crash. Furthermore if two motorcycles meet at the same point we have to start another one, or in this case just continue with one and stop the other. After all motorcycles have crashed there is the graph of the traces which in fact is a convex tessellation of P. There are several papers (one here) on this but implementation would be tough. This results in O(r) convex polygons that cover the interior of P.
I think the easiest way is to go with the triangulation or trapezoidal decomposition. These are well studied and available as implementation in many libraries.
Also mentioned in the comments: Input can be produced that will force O(n) polygons. Just think of a star shaped polygon that has n/2 reflex vertices (interior angle > pi).

Determine which points maximize area of a polygon if chosen as vertices

I have a polygon whose vertices are the center points of other 4 polygons. For these 4 polygons I also have the coordinates of their vertices. I would like to determine for each "corner polygon" the vertex that if chosen as vertex of the bigger polygon would maximize it's area. The polygon is a rectangle to which has been applied a perspective transformation, so I was thinking that it's a trapezoid.
I have tried calculating a rough center by summing the (x,y)s of the corners and diving by 4. I then chose each vertex based on the one that had farthest distance from this center point among it's peers. (something like distance = (Xc - X)^2 + (Yc - Y)^2, I avoided square rooting the result for performance purposes).
This unfortunately does not give the intended results. Usually just one vertex is substituted by the outer most "corner polygon" vertex, while the others are substituted by one of the other two "corner polygon" vertices excluding the nearest one.
What could be a way to create a better algorithm?
The method I posted actually does work, as #MBo suggested there were implementation mistakes. To specify for future readers, this algorithm probably works only because the polygon is convex and/or a trapezoid, although that remains pure speculation based on the fact that my algorithm was produced heuristically.
A first approach is just by brute force: compare the areas of the 4^4 = 256 polygons obtained by combinations.
Slightly better, I guess that the vertices must belong to the convex hull of the point set (needs to be confirmed). Then discard the inner points and brute-force the remainig ones. As between one and three vertices of the corner quadrilaterals are on the convex hull, there are 3^4 = 81 combinations at worst (and a single at best; four in the given example; 2^4 = 16 is the most likely in practice).
You will need an efficient convex hull algorithm for the savings to be effective.

Confusion on Delaunay Triangulation and Largest inscribed circle

I need to find a largest inscribed circle of a convex polygon, I've searched many sites and I get that this can be done by using Delaunay triangulation. I found a thread in CGAL discussion with an algorithm using CGAL:
You can compute this easily with CGAL:
First, compute the Delaunay triangulation of the points.
Then, iterate on all the finite faces of the triangulation.
For each finite face f
compute its circumcenter c
locate c in the triangulation (to speed up things, you can give one
vertex of f as starting hint for the point location)
if the face returned by locate(c,hint) is finite, then the circumcenter
c lies in the convex hull of the points, so, f is a candidate
if f is such a candidate face, compute its squared circumradius
keep only the face with minimum squared circumradius
The CGAL manual (chapter 2D triangulation, together with a few things
from the kernel) shows every basic function to do this.
I was a bit confused with the last part of this algorithm. When I read it what I understand from it is that the minimum circumradius of the triangulation face is the radius for the largest inscibed circle. But from examples of polygon with Delaunay triangulation, it seems that even the smallest circumcircle sometimes cannot fit inside the polygon, so how can this has the same radius as the largest inscribed circle?
Maximum inscribed circle in polygons.
The classical computational-geometry solution to the maximum inscribed circle problem for polygons is to use the generalized Voronoi diagram of the polygon's faces resp. the medial axis of the polygon. This approach works in a more general setting like polygons with holes, see this stackoverflow answer to a similar question.
Convex input.
The convexity of your input polygon, however, gives the problem more structure, which I would like to comment on. Consider the following convex input polygon (black), the Voronoi diagram (blue), and the maximum inscribed circle (green) centered on a Voronoi node.
The classical Voronoi-based solution is to (i) compute the Voronoi diagram and (ii) take the Voronoi node with largest clearance (i.e., distance to its defining faces).
The Voronoi diagram of a polygon with holes (i.e., the set of vertices and edges) can be computed in O(n log n) time, c.f. Fortune's algorithm (1986). Later Chin et alii (1999) gave an O(n) algorithm for the medial axis of a simple polygon.
For convex polygons, however, a time-optimal algorithm for Voronoi diagram that runs in O(n) time was already known in 1989 due to Aggarwal et alii. This algorithm follows basically the following idea: Consider the grey offset curves moving inwards at unit speed. If you project this movement into three-space where the z-axis is time you get a unit-slop roof over the polygon:
This roof model could also be characterized as follows: Put a half-space on each polygon edge at 45° slope with polygon (such that they contain the polygon) and intersect them all. So if you can quickly compute the intersect of half-spaces then you can also quickly compute Voronoi diagrams of convex polygons. Actually, for the maximum inscribed circle problem we do not need to go back to the Voronoi diagram but take the one peak of the roof, which marks the center of the maximum inscribed circle.
Now the half-spaces are dualized to points, and then the intersection of half-spaces corresponds the convex hull of its dual points. Aggarwal et al. now found an O(n) algorithm for the convex hull of points that stem from this setting.
A summary of this construction that leads to a Voronoi diagram algorithm for convex polyhedra in any dimension can be found in a blog article of mine.
Simple & fast implementation. A simpler algorithm to compute the Voronoi diagram is motivated by straight skeletons. For convex polygons the Voronoi diagram and the straight skeleton are the same.
The algorithm behind the straight-skeleton implementation Stalgo basically simulates the evolution of the wavefront structure (the grey offset curves). For convex polygons this reduces to finding the sequence of edges that collapse.
So a simple O(n log n) algorithm could look like this:
Construct a circular list of the polygon edges. Compute the collapse time of each edge during wavefront propagation, and insert this event into a priority queue.
Until the queue is empty: Take out the next edge-collapse event: Remove the edge from the circular structure and update the collapse times of the neighboring edges of the removed edge.
Actually, you can simplify the above algorithm further: You do not need to update edge collapses in the priority queue but simply insert new ones: Since the new collapse time of edges are strictly lower, you always get the right event first and dismiss the others and the queue is not growing larger than 2n. Hence, you do not compromise the O(n log n) time complexity.
For the maximum inscribed circle problem you can simplify the above algorithm even further: The Voronoi node (resp. straight skeleton node) you look for is due to the collapse of the final triangle at the end of the loop over the priority queue.
This algorithm should be quick in practice and only a few lines of code.
The last step can mean to select the minimum face of the triangle. Then rinse and repeat.

How to find convex hull in a 3 dimensional space

Given a set of points S (x, y, z). How to find the convex hull of those points ?
I tried understanding the algorithm from here, but could not get much.
It says:
First project all of the points onto the xy-plane, and find an edge that is definitely on the hull by selecting the point with highest y-coordinate and then doing one iteration of gift wrapping to determine the other endpoint of the edge. This is the first part of the incomplete hull. We then build the hull iteratively. Consider this first edge; now find another point in order to form the first triangular face of the hull. We do this by picking the point such that all the other points lie to the right of this triangle, when viewed appropriately (just as in the gift-wrapping algorithm, in which we picked an edge such that all other points lay to the right of that edge). Now there are three edges in the hull; to continue, we pick one of them arbitrarily, and again scan through all the points to find another point to build a new triangle with this edge, and repeat this until there are no edges left. (When we create a new triangular face, we add two edges to the pool; however, we have to first check if they have already been added to the hull, in which case we ignore them.) There are O(n) faces, and each iteration takes O(n) time since we must scan all of the remaining points, giving O(n2).
Can anyone explain it in a more clearer way or suggest a simpler alternative approach.
Implementing the 3D convex hull is not easy, but many algorithms have been implemented, and code is widely available. At the high end of quality and time investment to use is CGAL. At the lower end on both measures is my own C code:
In between there is code all over the web, including this implementation of QuickHull.
I would suggest first try an easier approach like quick hull. (Btw, the order for gift wrapping is O(nh) not O(n2), where h is points on hull and order of quick hull is O(n log n)).
Under average circumstances quick hull works quite well, but processing usually becomes slow in cases of high symmetry or points lying on the circumference of a circle. Quick hull can be broken down to the following steps:
Find the points with minimum and maximum x coordinates, those are
bound to be part of the convex.
Use the line formed by the two points to divide the set in two
subsets of points, which will be processed recursively.
Determine the point, on one side of the line, with the maximum
distance from the line. The two points found before along with this
one form a triangle.
The points lying inside of that triangle cannot be part of the
convex hull and can therefore be ignored in the next steps.
Repeat the previous two steps on the two lines formed by the
triangle (not the initial line).
Keep on doing so on until no more points are left, the recursion has
come to an end and the points selected constitute the convex hull.
See this impementaion and explanation for 3d convex hull using quick hull algorithm.
Gift wrapping algorithm:
Jarvis's match algorithm is like wrapping a piece of string around the points. It starts by computing the leftmost point l, since we know that the left most point must be a convex hull vertex.This process will take linear time.Then the algorithm does a series of pivoting steps to find each successive convex hull vertex untill the next vertex is the original leftmost point again.
The algorithm find the successive convex hull vertex like this: the vertex immediately following a point p is the point that appears to be furthest to the right to someone standing at p and looking at the other points. In other words, if q is the vertex following p, and r is any other input point, then the triple p, q, r is in counter-clockwise order. We can find each successive vertex in linear time by performing a series of O(n) counter-clockwise tests.
Since the algorithm spends O(n) time for each convex hull vertex, the worst-case running time is O(n2). However, if the convex hull has very few vertices, Jarvis's march is extremely fast. A better way to write the running time is O(nh), where h is the number of convex hull vertices. In the worst case, h = n, and we get our old O(n2) time bound, but in the best case h = 3, and the algorithm only needs O(n) time. This is a so called output-sensitive algorithm, the smaller the output, the faster the algorithm.
The following image should give you more idea
GPL C++ code for finding 3D convex hulls is available at http://www.newtonapples.net/code/NewtonAppleWrapper_11Feb2016.tar.gz and a description of the O(n log(n)) algorithm at http://www.newtonapples.net/NewtonAppleWrapper.html
One of the simplest algorithms for convex hull computation in 3D was presented in the paper The QuickHull algorithm for Convex Hulls by Barber, etc from 1995. Unfortunately the original paper lacks any figures to simplify its understanding.
The algorithm works iteratively by storing boundary faces of some convex set with the vertices from the subset of original points. The remaining points are divided on the ones already inside the current convex set and the points outside it. And each step consists in enlarging the convex set by including one of outside points in it until no one remains.
The authors propose to start the algorithm in 3D from any tetrahedron with 4 vertices in original points. If these vertices are selected so that they are on the boundary of convex hull then it will accelerate the algorithm (they will not be removed from boundary during the following steps). Also the algorithm can start from the boundary surface containing just 2 oppositely oriented triangles with 3 vertices in original points. Such points can be selected as follows.
The first point has with the minimal (x,y,z) coordinates, if compare coordinates lexicographically.
The second point is the most distant from the first one.
The third point is the most distant from the line through the first two points.
The next figure presents initial points and the starting 2 oppositely oriented triangles:
The remaining points are subdivided in two sets:
Black points - above the plane containing the triangles - are associated with the triangle having normal oriented upward.
Red points - below the plane containing the triangles - are associated with the triangle having normal oriented downward.
On the following steps, the algorithm always associates each point currently outside the convex set with one of the boundary triangles that is "visible" from the point (point is within positive half-space of that triangle). More precisely each outside point is associated with the triangle, for which the distance between the point and the plane containing the triangle is the largest.
On each step of algorithm the furthest outside point is selected, then all faces of the current convex set visible from it are identified, these faces are removed from the convex set and replaced with the triangles having one vertex in furthest point and two other points on the horizon ridge (boundary of removed visible faces).
On the next figure the furthest point is pointed by green arrow and three visible triangles are highlighted in red:
Visible triangles deleted, back faces and inside points can be seen in the hole, horizon ridge is shown with red color:
5 new triangles (joining at the added point) patch the hole in the surface:
The points previously associated with the removed triangles are either become inner for the updated convex set or redistributed among new triangles.
The last figure also presents the final result of convex hull computation without any remaining outside points. (The figures were prepared in MeshInspector application, having this algorithm implemented.)

Minkowski sum for shapes with arcs

There is a problem: I wanna compute Minkowski sum for two almost convex polygons, where almost convex polygon - polygon, obtained by replacing some edges with arcs with 0 to PI radians in convex polygon.
I hope there is O(n + m) solution, where n, m - numbers of verteces in almost convex polygons.
For convex shapes the problem is trivial, but this problem puzzles me. Could anyone help me with any advice/idea/solution. Thanks in advance!
First, visualize the Minkowski sum (help with that here). Next, understand the area between an arc and a chord (that's the semi-hard part here). If your polygons are convex, and the arc is in the convex direction, then it will only add area to the Minkowski sum. To be specific, it will add exactly that area described by the arc and chord. If and only if you are dealing with convex polygons and arcs in the convex direction, you can simply substitute the exact same arc you used on the polygon as the corresponding edge of the Minkowski sum. Note that each edge of the Minkowski sum corresponds exactly to an edge of one of the relevant polygons.
I made a quick screen cap of a slide from the Minkowski link to illustrate my point. Forgive me that it's inexact, but I think you will get the idea. The purple area would be added to the area of the Minkowski sum.
If you are using this for motion planning or similar, you can adapt traditional algorithms for point containment almost trivially.
Edit:
I think if the arcs are in the concave direction, it's simply a matter of area subtraction rather than addition. The important thing to maintain simplicity is that one of the polygons is convex and arc substitution happens on the convex polygon or an edge in the convex hull of the other.

Resources