function h = dispLS(ls, vs, plotType, aspectRatio)
% dispLS(ls, vs, plotType)
%
% does the actual display of a level set function to the screen
% in some cases, a handle to the displayed object is returned
%
% this code used to be part of plotLS, but has now been split off
%    to allow other callers
%
% Ian Mitchell, 2/21/01

% in case h is never set
h = [];

if(nargin < 4)
  aspectRatio = [ 1 1 1 ];
end

  switch(length(vs))
  case 1
    plot(vs{1}, ls, '-', vs{1}([ 1 end ]), [ 0 0 ], '--');
    xlabel('x');  ylabel('\psi(x)');

  case 2
    switch(plotType)
      case 'surf'
        surf(vs{1}, vs{2}, ls);
        shading interp;
        view(2); daspect([ 1 1 1/25 ]);
      otherwise
        contour(vs{1}, vs{2}, ls, [ 0 0 ], 'k');
        %contour(vs{1}, vs{2}, ls, 0:max(ls(:))/10:max(ls(:)));
        %contour(vs{1}, vs{2}, ls, -2.0:0.2:0);
        %contour(vs{1}, vs{2}, ls, -1:0.2:1);
        %contour(vs{1}, vs{2}, ls, -0.03:0.03:0.03);
        axis equal;
        axis([ min(vs{1}) max(vs{1}) min(vs{2}) max(vs{2}) ]);
        grid on;
    end
    xlabel('x');  ylabel('y');

  case 3
    level = 0;
    switch(plotType)
      case 'slice'
        [ gx gy gz ] = meshgrid(vs{1}, vs{2}, vs{3});
        nInfVal = find(~isnan(ls));
        avgx = mean(gx(nInfVal));
        avgy = mean(gy(nInfVal));
        avgz = mean(gz(nInfVal));
        slice(gx, gy, gz, ls, avgx, avgy, avgz);
        [ min(ls(nInfVal)) max(ls(nInfVal)) ]

      case 'solid'
        h = patch(isosurface(vs{1}, vs{2}, vs{3}, ls, level));
        isonormals(vs{1}, vs{2}, vs{3}, ls, h);
        set(h, 'FaceColor', 'red', 'EdgeColor', 'none');
        camlight left;  camlight right;
        lighting phong;

      otherwise
        h = patch(isosurface(vs{1}, vs{2}, vs{3}, ls, level));
        %reducepatch(h, 0.25);
        set(h, 'FaceColor', 'none', 'EdgeColor', 'black');
    end
    axis([ min(vs{1}) max(vs{1}) min(vs{2}) max(vs{2}) ...
           min(vs{3}) max(vs{3}) ]);

    daspect(aspectRatio);
    xlabel('x');  ylabel('y');  zlabel('z');
    %view(80, 15);

  otherwise
    error([ 'Unable to display ' num2str(length(vs)) ' dimensional data.' ]);
  end

