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.
Jaroslaw Tuszynski (2021). in_polyhedron (https://www.mathworks.com/matlabcentral/fileexchange/48041-in_polyhedron), MATLAB Central File Exchange. Retrieved .
Inspired by: Triangle/Ray Intersection, inpolyhedron - are points inside a triangulated volume?
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
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.
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!
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.
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
Fantastic!
Reply to Jaroslaw Tuszynski: Thank you very much. I think this will solve my problem. Big hugs
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
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