function lims=objbounds(h)
%OBJBOUNDS 3D object limits.
%
%   LIMS=OBJBOUNDS(H)  limits of the objects in vector H.
%   LIMS=OBJBOUNDS(AX) limits of the objects in which are children 
%                      of axes AX.
%   LIMS=OBJBOUNDS     limits of the objects in which are children.
%                      of the current axes.
%
%   OBJBOUNDS calculates the 3D limits of the objects specified. The
%   limits are returned in the form [xmin xmax ymin ymax zmin zmax].
%   This is a utility function used by CAMLOOKAT.
%
%   See also CAMLOOKAT.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.6 $  $Date: 2000/06/01 02:53:45 $

%
%  This might be called on the HG objects of a user object when its
%  bounds are requested.
%

if nargin==0
  h = gca;
end

xmin = nan;
xmax = nan;
ymin = nan;
ymax = nan;
zmin = nan;
zmax = nan;

if length(h)==1 & ishandle(h) & strcmp(get(h, 'type'), 'axes')
  ch = get(h, 'children');
  if isempty(ch)
    xl = xlim(h);
    yl = ylim(h);
    zl = zlim(h);
    xmin = xl(1);  xmax = xl(2); 
    ymin = yl(1);  ymax = yl(2); 
    zmin = zl(1);  zmax = zl(2); 
  else
    h = ch;
  end
end

for i=1:length(h)
  type = get(h(i),'type');
  validtype = 0;
  switch type
    case {'surface','line', 'image'}
      validtype = 1;
      xd = get(h(i), 'xdata');
      yd = get(h(i), 'ydata');
      if strcmp(type, 'image')
	zd = 0;
      else
	zd = get(h(i), 'zdata');
	if isempty(zd)
	  zd = 0;
	end
      end
      
    case {'patch'}
      validtype = 1;
      v = get(h(i), 'vertices');
      f = get(h(i), 'faces');
      v = v(f(isfinite(f)),:);
      xd = v(:,1);
      yd = v(:,2);
      if size(v,2)==2
	zd = 0;
      else
	zd = v(:,3);
      end
  end
  
  if validtype
    xmin = min(xmin,min(xd(:)));
    xmax = max(xmax,max(xd(:)));
    ymin = min(ymin,min(yd(:)));
    ymax = max(ymax,max(yd(:)));
    zmin = min(zmin,min(zd(:)));
    zmax = max(zmax,max(zd(:)));
  end
end

if ~isnan(xmin)
  lims = [xmin xmax ymin ymax zmin zmax];
else
  lims = [];
end
