% surfi(XYZ[, C][, maxSideLength])
%
% plot a surface interpolated from a set of vertices. XYZ
% should be 3xN. the surface that interpolates these
% vertices is plotted. C specifies vertex colours. if
% unsupplied, C = normalized(Z). if maxSideLength is
% supplied, triangles will be pared before plotting using
% triangulate_pareBySideLength.
%
% to obtain the surface without plotting it, see
% triangulate. to see example usage, try surfi_demo.
%
% NOTE: for correct operation the vertex list must be sorted
% correctly, by x coordinate first, then by y, then z.
% triangulate will do this if it is not already done, but it
% uses a simple sorting algorithm that may take some time.
% therefore, if you can sort the data more intelligently
% before passing in, performance may be improved.
%
% triangulate is ported from Paul Bourke's triangulate code at
% http://astronomy.swin.edu.au/~pbourke/terrain/triangulate/
% AUTHOR ben mitch
% SOURCE http://www.mathworks.com/matlabcentral/fileexchange/6169-surfi
% VERSION 30/08/2010
function surfi(XYZ, C, maxSideLength)
% check args
if nargin < 2 || isempty(C)
C = XYZ(3,:);
C = C - min(C);
C = C / max(C);
end
% run triangulate
v = triangulate(XYZ);
% pare by side length
if nargin > 2
v = triangulate_pareBySideLength(XYZ, v, maxSideLength);
end
% display
X = XYZ(1,:);
Y = XYZ(2,:);
Z = XYZ(3,:);
patch(X(v), Y(v), Z(v), C(v));
% set up axis
view(3)