function showmesh(node,elem,varargin)
% SHOWMESH displays a mesh.
%
%    showmesh(node,elem) displays a topological 2-dimensional mesh given by
%    node and elem matrices; see <a href="matlab:ifemdoc meshdoc">iFEMdoc meshdoc</a> 
%    for the data structure node and elem.
%
%    showmesh(node,elem,viewangle) changes the display angle. The
%    deault view angle on planar meshes is view(2) and view(3) for surface
%    meshes. 
%        
%    showmesh(node,elem,'param','value','param','value'...) allows
%    additional patch param/value pairs to be used when displaying the
%    mesh.  For example, the default transparency parameter is set to 0.75.
%    You can overwrite this value by using the param pair ('FaceAlpha',
%    value). The value has to be a number between 0 and 1. Other parameters
%    include: 'Facecolor', 'Edgecolor' etc. These parameters
%    are mostly used when displaying a surface mesh.
% 
%   Example:
%     % A mesh for a L-shaped domain
%     node = [1,0; 1,1; 0,1; -1,1; -1,0; -1,-1; 0,-1; 0,0]; 
%     elem = [1,2,8; 3,8,2; 8,3,5; 4,5,3; 7,8,6; 5,6,8];    
%     figure(1)
%     showmesh(node,elem);
%
%     % A mesh for a unit sphere
%     node = [1,0,0; 0,1,0; -1,0,0; 0,-1,0; 0,0,1; 0,0,-1];
%     elem = [6,1,2; 6,2,3; 6,3,4; 6,4,1; 5,1,4; 5,3,4; 5,3,2; 5,2,1];
%     for i = 1:3
%         [node,elem] = uniformrefine(node,elem);
%     end
%     r = sqrt(node(:,1).^2 + node(:,2).^2 + node(:,3).^2);
%     node = node./[r r r];
%     figure(2)
%     subplot(1,2,1);
%     showmesh(node,elem);
%     subplot(1,2,2);
%     showmesh(node,elem,'Facecolor','y','Facealpha',0.5);
%
%   See also showmesh3, showsolution.

% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

dim = size(node,2);
if (dim==2) % planar meshes
    trisurf(elem,node(:,1),node(:,2),zeros(size(node,1),1));
    view(2); axis equal; axis tight; axis off;
end
if (dim==3) % surface meshes
    h = trisurf(elem,node(:,1),node(:,2),node(:,3));    
    if nargin==2
        set(h,'facecolor',[0.45 1 0.45],'edgecolor','k','FaceAlpha',0.75);    
    else
        set(h,'FaceAlpha',0.75);
        set(h,varargin{1:end});
    end
    view(3); axis equal; axis off; axis tight;    
end
if nargin>2
    if isnumeric(varargin{1})
        view(varargin{1});
        if nargin>3
            set(h,varargin{2:end});
        end
    else
        set(h,varargin{1:end});        
    end
end
