0

I have a 3D array of data which shows one concave figure. I want to find all points which don't belong to the figure, but are inside the convex hull. How can I do it?

I know I should use the convhull function, but I'm not sure how (the input is an array of size m by n, where m is the number of points and n is the number of dimensions, so 3 in my case). Also, I'm not sure how to process the output of this function - it has the same size as the input matrix.

I'd appreciate any examples.

  • Wait so you have a figure of data lets say file1.txt of points making up a convexhull and lets say file2.txt of points (inside or outside) of that hull? – Jay Oct 13 '15 at 20:32

1 Answers1

1
[K, V] = convhull(YourArray);

K are the indices of your points corresponding to the points on the convex hull, V is just the volume spanned by that convex hull. Thus, you can use this row index to find your points back in YourArray.

Using the below example:

YourArray= rand(1e3,3);
[K, V] = convhull(YourArray);

K gave me a 140x3 'double' containing the indices of my points and V=0.9291, the volume spanned by my convex hull. You can get the points on the convex hull back from YourArray by simply calling

YourArray(K)

Documentation link

Unsurprisingly more people have struggled with obtaining the points within the convex hull and have actually written code for this, see the MathWorks Central, code by John D'Errico.

Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • According to the documentation, my array "is of size mpts-by-ndim, where mpts is the number of points and ndim is the dimension of the space where the points reside". Then why would it work for an array which is, let's say, 32 by 32 by 32? And I need to find all points which are inside the convex hull, but are outside my figure. How can I do it? – DailyInformation Oct 13 '15 at 20:55
  • @DailyInformation - This may help: http://stackoverflow.com/questions/33110501/reshaping-arrays-in-matlab/33110725#33110725 – rayryeng Oct 13 '15 at 20:57
  • The array of size [32x32x32] basically is an evenly distributed 3D grid, i.e. 32 grid points in each direction and can thus be reshaped to an Nx3 array using what @rayryeng told you – Adriaan Oct 13 '15 at 20:58
  • @DailyInformation the other part of your question is alas not built-in into MATLAB yet. for the 2D case there's [`inpolygon`](http://mathworks.com/help/matlab/ref/inpolygon.html), but in 3D you'd have to mess around yourself with [`delaunay`](http://mathworks.com/help/matlab/ref/delaunay.html) and checking whether a point is on the correct side of each triangle. The reason MATLAB has no built-in for this yet, is because it is very difficult to do so, especially in an efficient way. – Adriaan Oct 13 '15 at 21:14