MATLAB: Does the patch becomes blocky when I set the XData, YData or ZData properties of the patch to the values they already contained in MATLAB

MATLABvertex

I have a patch object. When I display the patch it is smooth but now if I set its XData, YData or ZData properties of the patch to the same exact values that they just contained, the patch becomes blocky. Download the attachment and execute the following commands:
load 5thFemaleHead
ha=patch('Vertices', nodes, 'Faces',elements, 'FaceColor', [0.8 0 0], ...
'EdgeColor',[0.8 0 0], 'FaceLighting', 'phong', 'EdgeLighting', 'phong');
% Surface is smooth as desired
light('Position',[-5 -5 5]); light('Position',[10 5 5]); view(3);
Now if I execute the following commands the patch becomes blocky:
x=get(ha,'XData'); y=get(ha,'YData');z=get(ha,'ZData');
set(ha, 'XData', x, 'YData', y,'ZData', z);

Best Answer

The result that you encounter when you are setting the 'XData', 'YData' and 'ZData' properties to itself is an expected behavior.
There are two different ways to set the coordinates of a Patch object. The first way is to use the Faces and Vertices properties which is what is used when the patch is created. The second way is to use the XData, YData, and ZData properties. These are actually parts of two separate user models.
The XData, YData, and ZData are convenience properties. If you set them, they get mapped to Faces and Vertices. It is not the case that all values of Faces and Vertices can be represented as XData, YData, and ZData. This means that if you set the Faces and Vertices properties, and then do something like the following:
set(ha,'XData',get(ha,'XData')+1)
you may get unexpected results. In this case, the XData, YData and ZData model it is impossible to represent shared vertices. This means that the result has to be flat shaded because the smoothing of the normals comes from averaging the facet normals at shared vertices.
This could be illustrated as follows:
size(get(ha,'vertices'))
ans =
1225 3
set(ha,'xdata',get(ha,'xdata'),'ydata',get(ha,'ydata'),'zdata',get(ha,'zdata'))
size(get(ha,'vertices'))
ans =
4816 3
Notice that there are suddenly 3 times as many vertices.
Since the patch in the code above was created using Faces and Vertices, it is preferred not to use the convenience properties.
For example, the following would work:
v=get(ha,'vertices');
v(:,1) = v(:,1)+1;
set(ha,'vertices',v);
Hence, to summarize if one creates a patch using the 'Vertices' and 'Faces' model then the 'XData', 'YData' or 'ZData' properties should not be used, and vice versa.
Related Question