function render(Constr)
%RENDER sets the vertices, X and Y data properties of the patch and markers.
%Note: This is a default renderer, it always renders rectangular patches
%      for the chosen axes uints. May have to overload for specific axes
%      types, e.g., bode.

%   Author(s): A. Stothert
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/10 19:32:19 $

%Check that we have a valid constraint to render
if ~Constr.isValid
   return
else
   %Make sure we've a valid selected edge
   nConstr = size(Constr.getData('xCoords'),1);
   EdgeColor  = Constr.EdgeColor;
   PatchColor = Constr.PatchColor;
   SE = Constr.getData('SelectedEdge');
   SE = SE(SE <= nConstr);
   Constr.setData('SelectedEdge', SE);
end

numP = 50;  %Number of point to use along each edge

HostAx  = handle(Constr.Parent);
HostFig = double(HostAx.Parent);

if ~Constr.Activated
    % Initialize when constraint is not activated yet (preset for Activated=1)
    % Construct the constraint patch and edge
    % RE: The edge is the only object visible to the limit picker
    hPatch = patch( ...
       'Parent',double(Constr),...
       'XlimInclude', 'off',...
       'YlimInclude', 'off',...
       'ZlimInclude', 'on', ...
       'LineWidth', 0.5, ...
       'LineStyle', 'none',...
       'FaceAlpha', 0.75, ...
       'CDataMapping','Direct', ...
       'HelpTopicKey','timeresponseconstraint',...
       'UIContextMenu',Constr.addmenu(HostFig),...
       'ButtonDownFcn',Constr.ButtonDownFcn,...
       'HitTest','on', ...
       'Visible','off',...
       'Tag','ConstraintPatch');

    % Edge line to delimit extent for limit picker
    hEdge = line(...
       'Parent',double(Constr),...
       'LineWidth', 2, ...
       'LineStyle', 'none', ...
       'HitTest', 'on',...
       'UIContextMenu', Constr.addmenu(HostFig),...
       'ButtonDownFcn', Constr.ButtonDownFcn,...
       'Tag', 'ConstraintInfeasibleEdge', ...
       'Visible', 'off',...
       'XlimInclude', 'on',...
       'YlimInclude', 'on', ...
       'ZlimInclude', 'on');
    
    % Construct the resize markers
    hMarker = line(...
       'Parent',double(Constr),...
       'LineStyle','none', ...
       'Marker','s', ...
       'MarkerSize',4, ...
       'MarkerFaceColor','k', ...
       'MarkerEdgeColor','k', ...
       'HitTest', 'on', ...
       'ButtonDownFcn',Constr.ButtonDownFcn,...
       'Tag','ConstraintMarkers',...
       'Visible','off',...
       'XlimInclude','off',...
       'YlimInclude','off',...
       'ZlimInclude','on');
end

%Find the bounding polygon with extened bounds marking the infeasible
%region
[vertX, vertY, XData, YData] = Constr.getConstrPoly(numP);
%Get and set Z coordinates for layering
Zlevel = Constr.Zlevel;
vertZ = Zlevel*ones(size(vertX));
ZData = Zlevel*ones(size(XData))+0.1;

%Draw the polygon
hChildren = Constr.Children;
Tags = get(hChildren,'Tag');
idx = strcmp(Tags,'ConstraintPatch');
Vertices = [vertX(:), vertY(:), vertZ(:)];
Faces = 1:numel(vertX);
set(hChildren(idx),...
   'Vertices',  Vertices,...
   'Faces',     Faces, ...
   'visible',   'on',...
   'FaceColor', PatchColor, ...
   'EdgeColor', EdgeColor)

% Position 'edge' line
idx = strcmp(Tags,'ConstraintInfeasibleEdge');
set(hChildren(idx),...
   'XData',   XData(:),...
   'YData',   YData(:),...
   'ZData',   ZData(:),...
   'Color',   PatchColor, ...
   'Visible', 'on')

% Position 'edge piece lines
idx = strcmp(Tags,'ConstraintPieceEdge');
while sum(idx) < nConstr
   %Not enough edge pieces
   localCreateEdgePiece(Constr)
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   idx = strcmp(Tags,'ConstraintPieceEdge');
end
while sum(idx) > nConstr
   %Too many edge pieces
   delete(hChildren(find(idx,1,'first')))
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   idx = strcmp(Tags,'ConstraintPieceEdge');
end
ind = find(idx);
Weights = Constr.getData('Weight');
for ct = 1:nConstr
   if Weights(ct) > 0
      isVisible = 'on';
   else
      isVisible = 'off';
   end
   set(hChildren(ind(ct)),...
      'XData',     XData(:,ct),...
      'YData',     YData(:,ct),...
      'ZData',     ZData(:,ct),...
      'Color',     EdgeColor, ...
      'LineWidth', Weights(ct)*2+eps, ...
      'Visible',   isVisible)
end

% Position resize markers
XData = Constr.getData('xCoords');
XData = XData(SE,:);
XData = unitconv(XData(:),Constr.getData('xUnits'),Constr.xDisplayUnits);
YData = Constr.getData('yCoords');
YData = YData(SE,:);
YData = unitconv(YData(:),Constr.getData('yUnits'),Constr.yDisplayUnits);
ZData = Zlevel*ones(size(XData));
idx = strcmp(Tags,'ConstraintMarkers');
set(hChildren(idx),...
   'XData', XData,...
   'YData', YData,...
   'Zdata', ZData );
% Marker visibility
set(hChildren(idx),'Visible',Constr.Selected)

%--------------------------------------------------------------------------
function localCreateEdgePiece(Constr)

hPiece = line(...
   'Parent',double(Constr),...
   'LineWidth', 1, ...
   'HitTest','off',...
   'UIContextMenu','',...
   'ButtonDownFcn','',...
   'Tag','ConstraintPieceEdge', ...
   'Visible','off',...
   'XlimInclude','off',...
   'YlimInclude','off',...
   'ZlimInclude','on');
