4.38462

4.4 | 14 ratings Rate this file 164 downloads (last 30 days) File Size: 2.9 KB File ID: #10226

Inhull

by John D'Errico

 

04 Mar 2006 (Updated 30 Oct 2006)

Code covered by BSD License  

Efficient test for points inside a convex hull in n dimensions

Download Now | Watch this File

File Information
Description

Testing if a point is inside a convex hull can be done in a variety of ways. Inhull converts the problem into a dot product. If not supplied, it also computes the convex hull too. Inhull also attempts to avoid memory problems, doing the computation in smaller blocks when appropriate.

Here is a comparison of inhull to tsearchn:
n = 500;
m = 100;
p = 5;
xyz = rand(m,p);
testpts = rand(n,p)-.1;

tic
tess = delaunayn(xyz);
in0 = ~isnan(tsearchn(xyz,tess,testpts));
toc
tic
in1 = inhull(testpts,xyz);
toc

tsearchn: Elapsed time is 641.046321 seconds.
inhull: Elapsed time is 0.610503 seconds.

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
26 Apr 2006 Liketo Stayanonymous

No, sorry! I think you lost me with this one. What (again) is this software about??
Clear example will be much appreciated!

01 May 2006 Mia Ginsburg

Just what I wanted. Much faster than tsearchn if you just want to test if a point is inside a convex hull.

23 Oct 2006 Quan Wen

I agree with what Mia Ginsburg said. This code is very helpful. Thanks a lot.

06 Mar 2007 John Galt

Ever heard of commenting your code?

28 Mar 2007 Jon KoS

Great Job! Works really well and does what it says. I'll be using this function a lot. :) Not sure what the other reviewer was looking at but the code is well commented. Thanks!

31 Oct 2007 Yonas T. Weldeselassie

Excellent! Great job. Well documented, easy to understand, very similar to inpolygon and what more? Thanks a lot.

08 Nov 2007 Lee Shunn

Fast and easy to use. Thanks for the great code.

22 Feb 2008 Jayveer Thakoor

huge time saver! thank you :)

23 Jun 2008 Titus Edelhofer

Very nice. Just for ease of use: the example given in the help uses different variable names then the call to the function (and should call convhulln instead of convhull).

02 Dec 2008 Luigi Giaccari

Very good code, readable, uderstandable and even fast.
The n-dimensions also make-it flexible.

The thing I like the most is a wonderfull example of dot product vectorization with memory management. It helps a lot to understdand how to make m-code more efficient.

The only thing i disagree is the comparison with tsearchn, I thing its purpose is to find where a point is located inside the hull and whether he is inside or not. The nan output values are just a secondary objective which occurs in the worst case when a point lies outside.

Anyway a five stars, very good work!

02 Dec 2008 Luigi Giaccari

Very good code, readable, uderstandable and even fast.
The n-dimensions also make-it flexible.
 
The thing I like the most is a wonderfull example of dot product vectorization with memory management. It helps a lot to understdand how to make m-code more efficient.
 
The only thing i disagree is the comparison with tsearchn.
 I thing its purpose is to find where a point is located inside the hull and not whether he is inside or not. The nan output values are just a secondary objective which occurs in the worst case when a point lies outside.
 
Anyway a five stars, very good work!

05 Dec 2008 Nagarjuna

very nice work

19 Feb 2009 Florian

Thank you very much for this code. Particularly thanks to John for explaining me a problem I had with the code:

I have tried the following:
 tms=rand(1000,5);
in = inhull(tms,tms);
disp(length(find(in==0)))

which gave roughly ~319

as John points out:
"those 319 points were almost certainly on the surface of the convex hull.

Try this slight modification of your code instead.

tms=rand(1000,5);
in = inhull(tms,tms,[],1.e-13);
disp(length(find(in==0)))
     0

See that I've used a tolerance this time. The linear algebra used in the test is done in floating point arithmetic in matlab, as it must be. Without a tolerance, any test done in floating point arithmetic is suspect. By default that tolerance is zero, but this is also why I allowed the user a tolerance at all. "

Thanks John!!!!!

02 Mar 2009 Navid Samavati

Excellent Code!

28 May 2009 David Gingras

Another very usefull code provided by John! Thanks.

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

Added a tolerance on the tests

17 Mar 2006

Version 2.0: Repaired a problem when the hull has degenerate facets.

05 Apr 2006

Fix a problem in 2-d or 3-d, catching degenerate facets

30 Oct 2006

Sped up by removing a spare repmat from a loop, also changed the memblock size to 1e6, for an additional 3x speed enhancement. Thanks to Paul Jackson for these ideas.

Tag Activity for this File
Tag Applied By Date/Time
interior John D'Errico 22 Oct 2008 08:17:22
convex John D'Errico 22 Oct 2008 08:17:22
hull John D'Errico 22 Oct 2008 08:17:22
convhull John D'Errico 22 Oct 2008 08:17:22
convhulln John D'Errico 22 Oct 2008 08:17:22
tsearchn John D'Errico 22 Oct 2008 08:17:22
tsearch John D'Errico 22 Oct 2008 08:17:22
computational geometry John D'Errico 31 Oct 2008 06:24:07
convex hull John D'Errico 31 Oct 2008 06:24:07
computational geometry Adan Levy 12 Dec 2008 11:27:06
 

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