LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make VI to check if a polygon is inside of another?

Hello,

 

I need to create a program which checks if a 4-vertex polygon is entirely within a larger 5-vertex polygon. I am following the logic behind the PIP (Point in polygon) VI which is included in LabVIEW, though I need this to be expanded to check for a whole geometry rather than a single point. 

 

Would an 'easy' solution be to quickly cycle between all four points of the smaller polygon as the input for the PIP tool? 

 

Thank you,

LB

0 Kudos
Message 1 of 10
(806 Views)

I doubt there is an easy way to do that, not in any language.

 

Polygon Collision - GPWiki (archive.org)

 

Of course someone has probably done this before. Not sure about this: Polygon Clipping Download - NI

 

Any solution is very specific to the details of your problem. For instance, it makes a huge difference if one or both polygons are known to be convex.

 

I'm pretty sure that if all points of any polygon A are in a convex polygon B, the entire polygon A is in B. But if B isn't convex, it can be much harder, although it might help to know you only have 4 vertexes.

0 Kudos
Message 2 of 10
(790 Views)

As Wiebe already said this can be a hard problem, depending on the details.

 

In the most general case, you have and ordered set of four points for polygon 1 and five points for polygon 2. The problem is simple if these are regular polygons but it can get tricky of e.g. crossovers are allowed.

 

Have a look at the examples here. Are all these inputs for four or five points valid?

 

altenbach_0-1698246843426.png

 

Let's have a look at the following 5 point polygon? Which locations (A,B, C, D) would be "inside"?

 

altenbach_0-1698247436882.png

 

If you just want to distinguish between "completely inside" and "not completely inside" (assuming the "completely outside" case does not occur, but you could test for it later), you could just iterate over all pairwise sides and see if there is at last one crossover, a mathematically simple problem.

 

0 Kudos
Message 3 of 10
(760 Views)

Draw them with different colors to a picture and see if any pixels have the wrong color? It's not 100%, but might be close enough. 🙂
(Some vectors might be just outside, but not much enough to become a pixel and so on)

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 10
(757 Views)

You raise an interesting point, though my case would be much simpler than the examples you provided. Both polygons would have their vertices connected through exclusively straight lines with no cross-over.

 

The overall objective of the program would be to determine if there is a 'collision' while moving the smaller-internal polygon around the larger-fixed one. 

SpacePlasma_0-1698667502059.png

 

0 Kudos
Message 5 of 10
(674 Views)

Windows API provide a set of functions for Regions.

You can use the vertex of the outside polygon to create a region, and then use the vertex of the inside polygon to check if a point is in a region.

https://learn.microsoft.com/en-us/windows/win32/gdi/region-functions 

 

 

George Zou
0 Kudos
Message 6 of 10
(658 Views)

@SpacePlasma wrote:

The overall objective of the program would be to determine if there is a 'collision' while moving the smaller-internal polygon around the larger-fixed one. 


As I said, just iterate pairwise over all segments of each polygon (i.e. adjacent points and closure) and stop once a collision is detected. I already gave you the link for the math earlier.

 

So you have sides ABCD and sides VWXYZ, so do A?V, A?W, A?X, ..... D?Y, D?Z.

0 Kudos
Message 7 of 10
(646 Views)

@SpacePlasma wrote:

You raise an interesting point, though my case would be much simpler than the examples you provided. Both polygons would have their vertices connected through exclusively straight lines with no cross-over.


This only means something to you.

 

Exclusively straight lines? I'd think if the lines where bend (e.g. Bezier curves), you'd have mentioned it.

 

If you mean all polygons are concave, than that does (as mentioned) make a huge difference.

 

If one of the polygons is a square, I bed you can optimize more.

 

Or use one of the algorithms in the first reply. We can help translate that to LabVIEW, but we need to know your the exact input types.

 

To make matters more complex, it can be faster to hit test with a rough algorithm, before testing details. For instance, if the x min and x max don't overlap, you wouldn't have to test using a complex algorithm. If this happens a lot, it can be much faster to do a fast check first. 

 

I need to create a program which checks if a 4-vertex polygon is entirely within a larger 5-vertex polygon.


Yet you're showing a 4 and 6 vertex polygon?

0 Kudos
Message 8 of 10
(640 Views)

Also be aware if one vertex is exactly on a side of the other polygon, you might get false results due to the limitations of floating point math, You probably need to decide on a threshold for closeness.

0 Kudos
Message 9 of 10
(617 Views)

Shrink the polygon by 1 pixel, touching becomes collision.

Shrink more pixels to predict a collision.

 

George Zou
0 Kudos
Message 10 of 10
(597 Views)