File Exchange

## in_polyhedron

version 1.2.0.1 (140 KB) by
Tests if points are inside of triangulated volume.

Updated 20 Aug 2018

Tests if points are inside of triangulated volume. There are NO assumptions about orientation of the face normals. The code uses simple algorithm, is fully documented and optimized for speed.

### Cite As

Jaroslaw Tuszynski (2021). in_polyhedron (https://www.mathworks.com/matlabcentral/fileexchange/48041-in_polyhedron), MATLAB Central File Exchange. Retrieved .

ERAY TUNABOYU

Paul Zhang

This implementation is around 16x SLOWER than the implementation in 'inpolyhedron'. For some reason matlab lists this over 'inpolyhedron' but go for the other version instead. It's far faster.

Sameh Khan

Is there a way to tweak this code to identify which points are 'on the surface', rather than 'inside the surface'? This is very useful, but just trying to understand how to tune it to my liking. Thank to the Author!

Arthur Menikoff

Another small correction: at the bottom of your code
Change from:
if any(abs(max(bary,[], 2)-1)<eps & abs(t)<eps) % test for point being one of the grid points
Change to:
if any(max(bary,[], 2)<1+eps & min(bary,[],2)>-eps & abs(t)<eps)

This is in order not to exclude the case of the point being in the plane through the triangle, but not within the triangle.

Arthur Menikoff

Efficient and works wells.

I had some problems when intersect = all(bary>-eps,2) is an empty array. The subsequent logic doesn't work correctly (I'm using R2019B). I fixed this by adding a check for this situation:

if ~any(intersect)
inside(iPoint) = false;
continue;
end

Arthur Menikoff

Grigory Tagiltsev

Alexander Meynen

Yusuke Hirata

Fantastic!

Leandro Neckel

Reply to Jaroslaw Tuszynski: Thank you very much. I think this will solve my problem. Big hugs

Jaroslaw Tuszynski

Reply to Leandro Neckel: In my function the polyhedron surface has some "thickness", so the points defining it are considered "inside" polyhedron. To get that I define variable "eps". If that "thickness" is on the order of magnitude of the size of the polyhedron, you will run into trouble. I would suggest changing eps value or even better multiply your coordinates by 1e5

Leandro Neckel

Hi,
First of all thanks for your amazing work with this function. It has been very useful in my codes. I'll make sure you'll be properly cited when I publish some work.
However, I'm having problems when I trying to find points inside a very small 3D region (1e-5x1e-5x1e-5 region, for example). I would like to know if this is a limitation of the function or something else.
Thank you very much for your attention and again for your awesome function

##### MATLAB Release Compatibility
Created with R2017b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux