Fast Convex Hull Algorithm

August 24, 2009 by Luigi Giaccari 
Filed under: Algorithms, Computational Geometry 
8 Comments
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 10.0/10 (2 votes cast)

Even if totally m-coded, this routine is particularly fast in computing convex hull of 2D points. In many cases seems to be much faster than the matlab library routine. The main reason is that, differently from convhull, this algorithm jumps the call to unique function which can be very slow for large models .
Algorithm is very simple, it’s based on cross product.

It is brand new, so please let me know if something goes wrong!

Since I received comments about different timings this version includes a speed test to compare ConvHull2D to the native matlab convhull. There is also a graphical explanation on how the algorithm works.

ConvHull2D returns indices into the X and Y vectors of the points on the convex hull.

Example: (convex hull of 1000000 random points)

N=1000000;
x=rand(N,1);
y=rand(N,1);

tic
chull=ConvHull2D(x,y);%ConvHull2D
toc

tic
chull2=convhull(x,y);     %Matlab built-in routine
toc

Output:

Elapsed time is 0.104134 seconds.(ConvHull2D)
Elapsed time is 1.207817 seconds.(Matlab built in)

For any problem, bug, information or suggestion just contact me at:

giaccariluigi@msn.com


Download Now

VN:F [1.8.8_1072]
Rating: 10.0/10 (2 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
Fast Convex Hull Algorithm10.0102

Popularity: 28% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

8 Comments on Fast Convex Hull Algorithm

  1. r0l0kcs on Fri, 18th Sep 2009 23:28
  2. This looks like Graham Scan to me.
    May I ask: Do you have any sources on incremental convex hull algorithms “for dummies” like me? That would be great. :o Or is Graham Scan the same as incremental convex hull?

    [Reply]


    Luigi Giaccari Reply:

    Umh,

    You should really study :-)
    In Graham scan there is a sorting operation based on the angle others points forms with the first one. Here sort is performed on y (or x ) coordinate, plus there is a filtering preprocessor step.
    I actually thing this algorithm is not something new since it seems just too easy. Anyway it works, it is very fast.

    Please report comments on http://www.advancedmcode.org/
    I don’t usually go on Youtube.

    Last question, no I don’t have no code about incremental convex hull, this one was coded for fun, I don’t work in the convex hull field…..Sorry

    For any more question just ask,

    Bye

    Luigi

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  3. Taywin on Fri, 25th Sep 2009 21:03
  4. I’m trying again… Last time it submitted an empty text…

    Have you every tried to compare for the case when all points are vertices of convex hull as well? I mean all 1 million points are actually vertices of a convex hull. Because your algorithm speed seems to rely heavily filtering. If there is a case that your filtering does not benefit the speed at all, I just want to know whether the built-in function is still slower than yours or how much faster it is compared to yours. Of course, this is an extreme case, but I just want to see how it goes. Thank you.

    [Reply]


    Luigi Giaccari Reply:

    My routine is faster if all points are already part of the convex hull.
    Run the following test:

    N=1000000;
    teta=linspace(0,2*pi,N);
    x=cos(teta);
    y=sin(teta);
     
    tic
    chull1=ConvHull2D(x,y);
    fprintf('ConvHull2D took : %4.4f s\n', toc);
     
    tic
    chull2=convhull(x,y);
    fprintf('Matlab Routine: %4.4f s\n', toc);

    it leads to:

    ConvHull2D took : 0.6692 s
    Matlab Routine: 5.5856 s

    The built-in uses the quick hull algorithm this point cofiguration is patological.

    My routine suffers logspace data where generally the convhull is formed by only a few points. On this data the filtering step erases no points. Anywa it should be faster in this case too, since ConvHull2D doesn’t call the unique fucntion which is the main bottlenech for the Matlab convhull.

    We also have to consider that Convhull2D, apart from the sort function, is totally m-coded, no precompiled files. Convhull instead is a MEX routine.

    If you need more infos just ask.

    Luigi

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  5. JackM on Tue, 15th Dec 2009 19:51
  6. Yes, this is nothing new but just a variation of Graham’s scan (FYI, you don’t need to calculate angles in Graham’s scan). If you are looking for fast convex hull methods, there is for example a paper “Optimal Output-Sensitive Convex Hull Algorithms in Two and Three Dimensions” by Timothy Chan, which explains a method which run in O(n*log(h)) time.

    [Reply]


    Luigi Giaccari Reply:

    Oh you seem pretty expert! :-)

    can you please send me the paper, I would like to give it a look!

    giaccariluigi@msn.com

    Thanks

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  7. Shazmin on Fri, 23rd Jul 2010 13:08
  8. Hi, is it possible to do this in 3D? Could you please tell me how? Thank you.

    [Reply]


    Luigi Giaccari Reply:

    Possible, but not convenient, it gets much more complicated.

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


Include MATLAB code in your comment by doing the following:

<pre lang="MATLAB">

%insert code here

</pre>