function pdecirc(xc,yc,radius,label)
%PDECIRC Draw circle, update Geometry Description matrix.
%
%	PDECIRC(XC,YC,RADIUS,LABEL) draws a circle
%	with center in (XC,YC), RADIUS radius,
%	and label LABEL. Label is optional. If omitted,
%	a default label will be used.
%
%	See also: PDEELLIP PDERECT PDEPOLY

%	Magnus Ringh 9-21-94, MR 7-20-95.
%	Copyright (c) 1994-95 by The MathWorks, Inc.
%	$Revision: 1.9.1.2 $  $Date: 1995/08/31 08:41:57 $

if nargin<3
  error('Too few input arguments')
  return
end

if ~(all(size(xc)==1) & all(size(yc)==1) & all(size(radius)==1))
  error('Input data must be scalars')
end

if ~(isreal(xc) & isreal(yc) & isreal(radius))
  error('Input data must be real')
end

if radius<=0
  error('Radius must be > 0')
end

if nargin>3
  if ~isstr(label)
    error('Label must be a string')
  end
end

pde_fig=findobj(get(0,'Children'),'flat','Tag','PDETool');

% If PDETOOL not started, create PDETOOL interface
if isempty(pde_fig),
  pdetool
  pde_fig=findobj(get(0,'Children'),'flat','Tag','PDETool');
end

if nargin>3
  if pdeisusd(label)
    error('Label must be unique')
  end
end

if nargin==3
  % add label:
  numcirc=1;
  label='C1';
  while pdeisusd(label),		% ensure unique labels
    numcirc=numcirc+1;
    label=['C' int2str(numcirc)];
  end
end

pde_circ=1;

h=findobj(get(pde_fig,'Children'),'flat','Tag','PDEMeshMenu');
pdegd=get(h,'UserData');
% update PDEGD matrix
m=size(pdegd,2)+1;
pdegd(1,m)=pde_circ;
pdegd(2,m)=xc;
pdegd(3,m)=yc;
pdegd(4,m)=radius;

% Check data before proceeding:
stat=csgchk(pdegd);
err=0;
if stat(m)==1,
  pdetool('error','  Circle must have positive radius')
  err=1;
elseif stat(m)==2,
  pdetool('error','  Circles must be unique')
  err=1;
end
if err
  set(pde_fig,'Pointer','arrow');
  return;
end

set(h,'UserData',pdegd)

set(pde_fig,'Pointer','watch');

% First call DECSG to decompose geometry;
err=0;
eval('[dl1,bt1,pdedl,bt,msb]=decsg(pdegd);','err=1;');
if err,
  set(h,'UserData',pdegd(:,1:m-1))
  pdetool('error',lasterr)
  set(pde_fig,'Pointer','arrow');
  return;
end

pdepatch(pdedl,bt,msb,label,m);
set(findobj(get(pde_fig,'Children'),'flat',...
    'Tag','PDEBoundMenu'),'UserData',pdedl);

setuprop(pde_fig,'dl1',dl1)
setuprop(pde_fig,'bt1',bt1)

ax=findobj(get(pde_fig,'Children'),'flat','Tag','PDEAxes');
h=findobj(get(ax,'Children'),'flat','Tag','PDELabel','UserData',m);
set(h,'Tag','PDELblSel')

pdeframe(pdegd,m)

set(pde_fig,'Pointer','arrow');
