4.3

4.3 | 11 ratings Rate this file 91 downloads (last 30 days) File Size: 22.5 KB File ID: #10391

Fast points-in-polygon test

by Darren Engwirda

 

16 Mar 2006 (Updated 03 Dec 2007)

No BSD License  

Fast test to determine points located inside general polygon regions. Should be significantly faster

Download Now | Watch this File

File Information
Description

Test a set of points in the 2D plane to determine which are located inside or on the edges of a polygon.

The polygon geometry can be non-convex and multiply-connected.

Similar to INPOLYGON, but generally much faster, more memory efficient and less prone to numerical rounding error.

INPOLY also displays superior scaling in terms of problem size (number of points, number of polygon edges) and hence the speedup when compared to INPOLYGON is significant for large problems and can easily be a factor of several hundred.

% UPDATE 31/03/2007
New algorithm! Massive speed improvements for large problems.

Untested on MATLAB pre-R6.5. These older releases lack JIT acceleration and may suffer speed penalties as a result.

Acknowledgements
This submission has inspired the following:
inpoly mex file
MATLAB release MATLAB 7 (R14)
Zip File Content  
Other Files poly_stuff/lake.m,
poly_stuff/polydemo.m,
poly_stuff/inpoly.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (14)
17 Mar 2006 Urs (us) Schwarz

very nice (and indeed fast) snippet with a good help section and economic implementation of the crossing number test
minor comments:
- the third arg CNECT should be computed automatically (if not defined at run-time) on the assumption that the user-defined points are connected consecutively; this behavior could/should be mentioned in the help section
- unfortunately, unlike INPOLYGON it does not (yet) distinguish between IN and ON the polygon
- the help section should give a pointer to INPOLYGON
us

17 Mar 2006 John D'Errico

While I prefer the edge list implementation this code uses as opposed to Matlab's polygon, it would be easy enough to generate the connectivity assuming consecutive points on the polygon as us points out.
Regardless, this code is indeed fast and nice.

17 Mar 2006 Darren Engwirda (The author)

The reason that cnect is defined on its own is so that the domain can be multiply connected (polygon with "islands").

If you assume that cnect can be built by taking consecutive nodes this is no longer possible.

Thanks though, I will update it to flag boundary points as us mentioned.

19 Mar 2006 Urs (us) Schwarz

QUOTE
The reason that cnect is defined on its own is so that the domain can be multiply connected (polygon with "islands").
QUOTE
please, note that i said at run-time, i still feel that you should make the third arg optional
us

12 Feb 2007 Matt K.

Perfectly suited for my needs of finding points on the boundary of a polygon

08 Apr 2007 Michael M

It's an incredible speed up compared with the poor inpolygon function delivered with MATLAB! Must be O(M*log(N)) ;-)

12 Apr 2007 Alex Storer

Very fast, but fails on some cases. Users beware! Perhaps older versions are more robust?

16 Apr 2007 Darren Engwirda

Small bug (as noted below) fixed.

Users don't beware, inpoly should work in all cases.

Further bugs can be emailed if necessary...

19 Mar 2008 Joseph Marks

inpoly provides a very large speed increase for large polygon problems!

Unfortunately, I too noticed a bug.

I am checking a very large rectangle grid's points to see if they are in a "massively concave polygon" -- think the outline of a "robot with arms, legs, etc."

The inpoly algorithm "incorrectly" *ADDS* a "shock of hair" to the "robot" -- obviously I am speaking metaphorically here -- and hence I am stuck using the *MUCH SLOWER* "inpolygon".

Has anyone reported an error like this to you?

Is there anything I can do?

Is there some middle ground between the two -- for example, by and large, I am dealing with concave polygons that just have an outside boundary -- no hole or anything.

25 Mar 2008 Joseph Marks

My review below is too harsh.

After examining several runs on many different "ultra-concave" polygons, I have found inpoly to be very good.

If the error problem I reported earlier is real, it is very rare. It could have been due to some other factor in my software including my own bug.

inpoly is very much faster than inpolygon for large test vectors.

03 Apr 2008 Armin Müller

Very fast, very accurate. Good jobb, Darren!

18 Sep 2008 Lili Wan

It is a good job, but I still find some problems when detecting a point of a polygon lies on the polygon edge. My test run in Matlab R2006a. Suppose node be the points of a polygon,
"[in,on] = inpoly(node,node);" can get right results, but after running
"[in,on] = inpoly(node(1,:),node);", on is false.

07 Nov 2008 Dag Lindbo

Very nice routine!

Is it reasonable to look for more efficient method for the particular case when the points to query at can be assumed to lie on a cartesian grid? E.g. imagine

in = inpoly(p,node);

with p = {X, Y}, where [X Y] = meshgrid(x,y)

Thanks!

23 Nov 2008 Luigi Giaccari

Thank You,
 it was very helpfull, impressive code.

If I can suggest an improvement:
a mex version will be the top of performance.

Please login to add a comment or rating.
Updates
20 Mar 2006

Detect points on boundaries

06 Dec 2006

Faster

01 Apr 2007

New algorithm

02 Apr 2007

Error checking added

13 Apr 2007

Bug fix (floating point roundoff)

21 May 2007

Binary search added, bit faster

03 Dec 2007

Floating point error reduced

Tag Activity for this File
Tag Applied By Date/Time
points inside polygon Darren Engwirda 22 Oct 2008 08:18:45
image processing Darren Engwirda 22 Oct 2008 08:18:45
inpolygon Darren Engwirda 22 Oct 2008 08:18:45
polygon Darren Engwirda 22 Oct 2008 08:18:45
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com