MATLAB: Remove sidewalls from surface plots

MATLABsidewallsurfsurface

Hi, the quad mesh algorithm underlying the shaded surface plot 'surf(x,y,z,c)' creates unnatural sidewalls at height discontinuities of natural 3d objects. Is there a way to remove these sidewalls, e.g. by making them transparent?

Best Answer

There are several ways, for example by replacing points with NaN, but they often give mixed results as far as appearance. The code below is another way to do it. This method is not simple, but it should look decent for most discontinuities. It involves looking at each face in turn and if the slope of the face exceeds some threshold, set it's alpha to zero.
I hope someone can come up with something a bit easier.
[X,Y] = ndgrid(linspace(-1,1,51));
Z = atan2(Y,X) + 3*round(atan2(Y,X)/2.5);
% For comparison
figure;
surf(X,Y,Z);
% Convert the "surf" into a "patch"
figure;
h = surf(X,Y,Z);
hp = patch(surf2patch(h));
delete(h);
V = get(hp,'Vertices');
F = get(hp,'Faces');
% Set the Alpha to be zero when the "slope" of a face is beyond a threshold
A = ones(prod(size(Z)-1),1);
thresh = 1.0;
for n = 1:size(F,1)
z = V(F(n,:),3);
dz = max(max(abs(bsxfun(@minus,z,z'))));
if dz > thresh;
A(n) = 0;
end
end
set(hp,'FaceVertexAlphaData',A,'edgealpha','flat');
shading faceted;
alpha flat
Related Question