function dummy = plotcomp(Editor)
%PLOTCOMP  Renders compensator poles and zeros.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.11 $ $Date: 2000/08/02 16:06:22 $

% REVISIT
dummy = [];

% Get pole/zero group information
PZGroups = Editor.LoopData.Compensator.PZGroup;
Nf = length(PZGroups);  % number of groups

% Initialize list of PZ group renderers (@PZVIEW objects)
HG = Editor.HG;  
Nc = length(HG.Compensator.Magnitude);  % current number of PZVIEW objects
% Delete extra groups
if Nc>Nf,
    delete(HG.Compensator.Magnitude(Nf+1:Nc));
    HG.Compensator.Magnitude = HG.Compensator.Magnitude(1:Nf,:);
    delete(HG.Compensator.Phase(Nf+1:Nc));
    HG.Compensator.Phase = HG.Compensator.Phase(1:Nf,:);
end
% Add new groups
for ct=1:Nf-Nc,
    HG.Compensator.Magnitude = [HG.Compensator.Magnitude ; sisogui.pzview];
    HG.Compensator.Phase = [HG.Compensator.Phase ; sisogui.pzview];
end

% Render each pole/zero group
Ts = getTs(Editor.LoopData);
for ct=1:Nf
    h(1) = HG.Compensator.Magnitude(ct);  % PZVIEW object for mag plot
    h(2) = HG.Compensator.Phase(ct);
    set(h,'GroupData',PZGroups(ct));
    LocalRender(h,HG.PlotAxes,Editor,Ts);
end

% Update database
Editor.HG = HG;


%----------------- Local functions -----------------


%%%%%%%%%%%%%%%
% LocalRender %
%%%%%%%%%%%%%%%
function LocalRender(hView,PlotAxes,Editor,Ts)
% Renders pole/zero group
% HVIEW = @PZVIEW object rendering a given pole/zero group

% Unit conversion factor
FreqFactor = unitconv(1,'rad/sec',Editor.FrequencyUnits);

% RE: Leave Y coordinate undefined (resolved by one-shot interp)
Style = Editor.LineStyle;
hMagView = hView(1);           % PZVIEW object for mag plot
hPhaseView = hView(2);         % PZVIEW object for phase plot
PZGroup = hMagView.GroupData;  % Rendered PZGROUP

% Render zeros
Zeros = PZGroup.Zero;   % zero values
if ~isempty(Zeros)
	if Ts
        FreqZ = FreqFactor * min(damp(Zeros(1),Ts),pi/Ts);
	else
		FreqZ = FreqFactor * damp(Zeros(1));
	end
    hZ(1,1) = line(FreqZ,NaN,...
        'Parent',PlotAxes(1),'Visible',Editor.MagVisible,...
        'UserData',hMagView,...
        'ButtonDownFcn',{@LocalMovePZ Editor}); 
    hZ(1,2) = line(FreqZ,NaN,...
        'Parent',PlotAxes(2),'Visible',Editor.PhaseVisible,...
        'UserData',hPhaseView,...
        'ButtonDownFcn',{@LocalMovePZ Editor}); 
    set(hZ,'LineStyle','none','Color',Style.Color.Compensator,...
        'HelpTopicKey','sisocompensatorpolezero',...
        'Marker','o','MarkerSize',6);
    if any(strcmp(PZGroup.Type,{'Complex','Notch'}))
        set(hZ,'LineWidth',2)
    end
else
    hZ = zeros(0,2);
end
hMagView.Zero = hZ(ones(1,length(Zeros)),1);
hPhaseView.Zero = hZ(ones(1,length(Zeros)),2);

% Render poles
Poles = PZGroup.Pole;   % zero values
if ~isempty(Poles)
	if Ts
		FreqP = FreqFactor * min(damp(Poles(1),Ts),pi/Ts);
	else
		FreqP = FreqFactor * damp(Poles(1));
	end
	hP(1,1) = line(FreqP,NaN,...
		'Parent',PlotAxes(1),'Visible',Editor.MagVisible,...
		'UserData',hMagView,...
		'ButtonDownFcn',{@LocalMovePZ Editor});  
	hP(1,2) = line(FreqP,NaN,...
		'Parent',PlotAxes(2),'Visible',Editor.PhaseVisible,...
		'UserData',hPhaseView,...
		'ButtonDownFcn',{@LocalMovePZ Editor}); 
	set(hP,'LineStyle','none','Color',Style.Color.Compensator,...
		'HelpTopicKey','sisocompensatorpolezero',...
		'Marker','x','MarkerSize',8);
	if any(strcmp(PZGroup.Type,{'Complex','Notch'}))
		set(hP,'LineWidth',2)
	end
else
	hP = zeros(0,2);
end
hMagView.Pole = hP(ones(1,length(Poles)),1);
hPhaseView.Pole = hP(ones(1,length(Poles)),2);

if strcmp(PZGroup.Type,'Notch')
	% Add notch width markers (mag plot only)
	Xm = FreqFactor * notchwidth(PZGroup,Ts);  % Marker frequencies
	nwm(1,1) = line(Xm(1),NaN,'Parent',PlotAxes(1));
	nwm(2,1) = line(Xm(2),NaN,'Parent',PlotAxes(1));
	set(nwm,'Visible',Editor.MagVisible,...
		'LineStyle','none','Marker','diamond',...
		'MarkerFaceColor',[0 0 0],'MarkerSize',6,'Color',[0 0 0],...
		'Tag','NotchWidthMarker',...
		'HelpTopicKey','sisonotchwidthmarker',...
		'UserData',hMagView,...
		'ButtonDownFcn',{@LocalShapeNotch Editor}); 
	hMagView.Extra = nwm;
else
	hMagView.Extra = zeros(0,1);
end
hMagView.Ruler = zeros(0,1);



%%%%%%%%%%%%%%%%%%%
%%% LocalMovePZ %%%
%%%%%%%%%%%%%%%%%%%
function LocalMovePZ(hSrc,event,Editor)
% Callback for button down on closed-loop poles

if strcmp(Editor.EditMode,'idle')  
	SISOfig = gcbf;
	switch get(SISOfig,'SelectionType')
	case 'open'
		% Double click: edit pole/zero location
		% REVISIT: do it??
	case 'normal'
		% Left click: initialize dragging of closed-loop pole
		% Switch edit mode to drag
		Editor.ModeData = 'pz';
		Editor.EditMode = 'drag';
		% Change pointer
		setptr(SISOfig,'closedhand')
		% Initialize tracking algorithm and notify peers
		Status = trackpz(Editor,'init');
		hgset_status(Editor,Status);
	end
end


%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalShapeNotch %%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalShapeNotch(hSrc,event,Editor)
% Callback for button down on closed-loop poles

if strcmp(Editor.EditMode,'idle')
	% Initialize dragging of closed-loop pole
	% Switch edit mode to drag
	Editor.ModeData = 'notchwidth';
	Editor.EditMode = 'drag';
	% Change pointer
	setptr(gcbf,'closedhand')
	% Initialize tracking algorithm and notify peers
	Status = shapenotch(Editor,'init');
	hgset_status(Editor,Status);
end
