function varargout = viewBoundaryShape(shapeStruct)
% plot a shape struct
%
% Syntax: objectHandleVector = viewBoundaryShape(shapeStruct)
%   or    [hEdge, hVertex] = viewBoundaryShape(shapeStruct)
%
% Plot edges and vertices of shape struct, if the corresponding fields are
%  present.
%
% See also: fitShapeBnd, viewBoundary, obtainBnd

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>

%% initialize plotting
nextPlotState = get(gca, 'nextPlot');
set(gca, 'nextPlot', 'add');

%% plot edges
if ~isempty(shapeStruct.hrep) && ~isempty(shapeStruct.hrep.A)
    % the incidence matrix tells in every column (=vertex) to which edges it
    %   belongs to (=row):
    incidenceMatrix = abs(bsxfun(@minus, ...
        shapeStruct.hrep.A * shapeStruct.vrep.V', ...
        shapeStruct.hrep.h)) < elkZerotol;
    hEdge = nan(1, size(incidenceMatrix, 1));
     
    for iEdge = 1:size(incidenceMatrix, 1)
        vertexIndex = find(incidenceMatrix(iEdge, :));
        vertexMatrix(1, :) = shapeStruct.vrep.V(vertexIndex(1), :) + ...
                             shapeStruct.lambda * shapeStruct.hrep.A(iEdge, :);
        vertexMatrix(2, :) = shapeStruct.vrep.V(vertexIndex(2), :) + ...
                             shapeStruct.lambda * shapeStruct.hrep.A(iEdge, :);

        hEdge(iEdge) = plot(vertexMatrix(:,1), ...
                            vertexMatrix(:,2), 'r-');         
    end
    set(hEdge, 'linewidth', 3, 'color', 'r');
else
    hEdge = [];
    incidenceMatrix = [];
end

if isfield(shapeStruct, 'lambda') && shapeStruct.lambda > 0
    % we can use the incidence matrix from above from which we extrakt
    %   starting angle and end angle for each vertex
    nVertex = max(size(incidenceMatrix, 2), 1);
    hVertex = nan(1, nVertex);
    for iVertex = 1:nVertex
        if ~isempty(incidenceMatrix)
            edgeIndexVector = find(incidenceMatrix(:, iVertex));
            [edgeAngleVector tmp] = convertCartToPolar(...
                shapeStruct.hrep.A(edgeIndexVector, 1)', ...
                shapeStruct.hrep.A(edgeIndexVector, 2)');
            edgeAngleVector = sort(edgeAngleVector);
            if (edgeAngleVector(2) - edgeAngleVector(1)) > pi
                edgeAngleVector = [edgeAngleVector(2) edgeAngleVector(1)+2*pi];
            end
            phi = edgeAngleVector(1):0.01:edgeAngleVector(2);
        else
            phi = 0:0.01:2*pi;
        end
        
        hVertex(iVertex) = plot(...
            shapeStruct.vrep.V(iVertex, 1) + shapeStruct.lambda*cos(phi), ...
            shapeStruct.vrep.V(iVertex, 2) + shapeStruct.lambda*sin(phi),  'k-');
    end

    set(hVertex, 'linewidth', 3, 'color', 'k')
else
    hVertex = [];
end

%% cleanup
set(gca, 'dataAspectRatio', [1 1 1]);
set(gca, 'nextPlot', nextPlotState);

if nargout == 2
    varargout{1} = hEdge;
    varargout{2} = hVertex;
elseif nargout == 1
    varargout{1} = [hEdge, hVertex];
end