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 (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 1997/12/06 01:03:20 $


if nargin==0
  h = gca;
end

if length(h)==1 & ishandle(h) & strcmp(get(h, 'type'), 'axes')
  h = get(h, 'children');
end

xmin = [];
for i=1:length(h)
  type = get(h(i),'type');
  switch type
    case {'patch','surface','line', 'image'}
      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
      
      if isempty(xmin)
	xmin = min(min(xd));
	xmax = max(max(xd));
	ymin = min(min(yd));
	ymax = max(max(yd));
	zmin = min(min(zd));
	zmax = max(max(zd));
      else
	xmin = min([xmin min(min(xd))]);
	xmax = max([xmax max(max(xd))]);
	ymin = min([ymin min(min(yd))]);
	ymax = max([ymax max(max(yd))]);
	zmin = min([zmin min(min(zd))]);
	zmax = max([zmax max(max(zd))]);
      end
  end
end

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