MATLAB: Clipping 3D mesh along boundary, interp1 problem

3d meshclipping meshinterp1MATLAB

Hello everyone,
I'm currently trying to cut off parts of a 3D mesh that lie outside of a certain threshold. To do so, I use interp1 to check if a point on the grid is above or below my threshold. My upper and lower limit is created using the boundary function on a set of points as shown in the first figure (X-Y view).
I set every point that is outside of the boundary to NaN with following code, where xm,ym,zm are the matrices generating the mesh, lower/upper contain the points from the boundary function, so that x(lower),y(lower) forms the lower border.
for i=1:size(zm,1)
for j=1:size(zm,2)
dyl=interp1(x(lower),y(lower),xm(i,j));
dyu=interp1(x(upper),y(upper),xm(i,j));
if ym(i,j)-dyl<0 || ym(i,j)-dyu>0
zm(i,j)=NaN;
end
end
end
After setting all the values to NaN the following mesh remains.
To me it looks like the code worked at least in some places, but I cant figure out what causes the steep cuts along the upper border. My guess is that interp1 has problems giving interpolated values where the boundary has sharp bends. I tried using polyfit to compare the y-values to a fit function, both using boundary and convhull for the borders, but the results were not usable.
Can anyone see the problem here and is there a different/better way to do this?
Thanks for your help!

Best Answer

Check inpolygon ....with this you should be able to tell whether the given point lies inside or outside the given closed region.
Related Question