After converting a sphere into a patch, I plotted the first vertex of the patch, and realized that the vertex is not on the sphere.
Code
[faces, vertices] = surf2patch(sphere(),'triangle');
sphere();
hold on;
plot3(vertices(1,1), vertices(1,2), vertices(1,3),'*r');
The resulting figure looks like this
How can the vertex not be on the surface of the sphere? Am I misunderstanding something?
Thanks
Explanation
When a function is an argument to another function, Matlab treats this as a function call with a single return value. In the context of our function call above,
[faces, vertices] = surf2patch(sphere(),'triangle');is equivalent to:
Now recall from the documentation:
[x, y, z] = sphere()calculates a set of 3D coordinates, [x, y, z].z = sphere()calculates the same [x, y, z] and only returns the z.sphere()with (nargout = 0) calculates the same [x, y, z], and plots it. This is the color sphere shown in the diagram above.Examining the source for surf2patch.m shows that when called with a single z argument, it assumes values for x and y automatically. These values are different than the [x, y, z] plotted by calling
sphere()with no arguments.Testing
To see for yourself, trace through the following code with a breakpoint enabled just inside surf2patch.m, and examine the input arguments:
You see that the output [facesA, verticesA] matches the plot from
sphere(), whereas the second and third call tosurf2patch()have no input [x, y] values, so they get auto-generated to something different than the original [x,y,z].Lesson
If you expect multiple return values, assign them explicitly.