3.33333

3.3 | 3 ratings Rate this file 8 Downloads (last 30 days) File Size: 7.56 KB File ID: #20754
image thumbnail

Fast InPolygon detection MEX

by Guillaume JACQUENOT

 

17 Jul 2008 (Updated 21 Aug 2008)

Fast detection points inside a polygonal region

| Watch this File

File Information
Description

Fast detection points inside a polygonal region.
To my knowlegde, the code is faster than all the inpolygon functions found on MatLab File Exchange

[IN,ON,IN_strict] = InPolygon(px,py,cx,cy);

input: px - nPM x nPN (Matrix) X-coordinates of the points to be tested
       py - nPM x nPN (Matrix) Y-coordinates of the points to be tested
       cx - nC x 1 (Vector) X-coordinates of the polygon
       cy - nC x 1 (Vector) Y-coordinates of the polygon
output:
   IN - boolean matrix of 1 if px(i,j),py(i,j) is
in or on the polygon defined by cx,cy
   ON - boolean matrix of 1 if px(i,j),py(i,j) is
on the polygon defined by cx,cy
   IN_strict - boolean matrix of 1 if px(i,j),py(i,j) is
exclusively in the polygon defined by cx,cy

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
MClust Spike sorting toolbox

MATLAB release MATLAB 7 (R14)
Other requirements A C compiler to compile mex file for different plateform than win32
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
19 Jul 2008 Darren Engwirda

Generally good, but not the fastest available on the tests I've run.

Offers similar functionality to the MATLAB inpolygon routine although not much help provided unless you want to read the C code.

It appears that only polygons with one bounding contour can be tested, rather than general multiply connected regions.

Based on a polygonal map of Lake Superior with 225 vertices, this code compared with my code "inpoly" as follows:

10,000 test points:
inpoly: 0.016 sec
InPolygon: 0.032 sec

100,000 test points:
inpoly: 0.06 sec
InPolygon: 0.33 sec

1,000,000 test points:
inpoly: 0.71 sec
InPolygon: 3.39 sec

These times are the average of 3 successive runs using MATLAB R14 on an Intel Core2 Duo laptop. The performance may vary for other benchmarks.

19 Jul 2008 Guillaume Jacquenot

I agree that Daren's code is faster on this example.
My function presents the advantage of directly being able to substiture with MatLab inpolygon function.

19 Jul 2008 Guillaume Jacquenot

I correct my previous comment: Daren's code is faster than mine.

21 Jul 2008 Mex Dll

m-file please. Why do you put a file on MCFE if one can't peer into your code? That's not the purpose of this all...

22 Jul 2008 Guillaume Jacquenot

There is no M-file, this a MEX file, however the function is called like a standard M-function (like the standard inpolygon). The function is a C -code interfaced with MatLab. I share this C-code interfaced with MatLab, because compiled code runs much faster than interpreted ones.
To have it work on your computer, you just have to compile it with MEX utility, and call it like the function inpolygon.

Moreover, MCFE has been developped to promote exchange between MATLAB users. And experimented users do not satisfy themselves with only M-files, they also use C-codes or Fortran codes that they interface with MatLab.

22 Jul 2008 Samy Makarsky

That's exactly what I was looking for!

Please login to add a comment or rating.
Updates
21 Aug 2008

Two bugs fixed

Tag Activity for this File
Tag Applied By Date/Time
inpolygon Guillaume JACQUENOT 22 Oct 2008 10:10:34
mex Guillaume JACQUENOT 22 Oct 2008 10:10:34
polygon Guillaume JACQUENOT 22 Oct 2008 10:10:34
points Guillaume JACQUENOT 22 Oct 2008 10:10:35
inside Guillaume JACQUENOT 22 Oct 2008 10:10:35
inpolygon Matt 05 Nov 2008 17:30:07
polygon SU 23 Dec 2009 13:54:45
inpolygon Ole Buus 19 Jul 2011 09:54:48

Contact us at files@mathworks.com