% plotTopology
%
% Usage: plotTopology (nodesPlaceMatrix,edgeTable,type, topologyTitle, showNrEdgesPerPair)
%
% Abstract: this function draws a topology of nodes and edges when it is done
% the positions of nodes, the matrix of edges information and the kind of
% representation. The kind of representation can be physical or virtual(lightpath network). In
% the physical representation it is assumed bidirectional edges between
% each node pair (graph theory).The lightpath or virtual topology is represented by means of arrows.
%
% Arguments:
% o In:
% nodesPlaceMatrix(Nx2): XY coordinates of nodes, where N is the number of
% nodes. They are contained in the first and second column respectively.
% . edgeTable(M,2): M-by-2 integer matrix. Each row is an edge (physical link
% or lightpath) 'm', where the first and second columns of each row are the
% origin node 'x' and destination node 'y' of this edge 'm' respectively and
% M is the number of physical links or lightpaths if the network topology
% is physical or virtual respectively.
% type(1x1): Indicates the kind of representation.
% If type=0 (physical reprensentation), the edges are drawn according to graph theory.
% If type=1 (virtual representation), the edges are drawn as arrows.
% topologyTitle(string): title of figure
% showNrEdgesPerPair(1x1) boolean that indicates if the number of edges per a pair of nodes are shown
% If type=0 they aren't shown.
% If type=1 they are shown
%
function plotTopology (nodesPlaceMatrix, edgeTable, type, topologyTitle, showNrEdgesPerPair)
if (nargin==0), help plotTopology;return, end %help calling
if (nargin~=5), error('1: Incorrect number of arguments.'),end %Number of input arguments different of 5
if (type~=1&&type~=0), error('2: Argument "type" must be either 0 or 1'),end %type must be only 0 or 1
if(size(nodesPlaceMatrix,2)~=2), error('3: Argument "nodePlaceMatrix" must have two columns'), end
%Compte the Nr Of Edges Per Node Pair
[uniqueSorted_edgeTable rowIndeces] = unique(sortrows(edgeTable),'rows');
NrOfEdgesPerPair = [rowIndeces(1) diff(transpose(rowIndeces))];
backgroundScaleX = abs(max(nodesPlaceMatrix(:,1)) - min(nodesPlaceMatrix(:,1)));
backgroundScaleY = abs(max(nodesPlaceMatrix(:,2)) - min(nodesPlaceMatrix(:,2)));
textOffset = 0.03;
%All nodes are drawn
plot(nodesPlaceMatrix(:,1),nodesPlaceMatrix(:,2),'bo','MarkerSize',10,'Color',[1 0.41 0],'LineWidth',2.0,'MarkerFaceColor',[1 0.41 0]);
%The axis are fixed in both dimensions according to minimum and maximum values of nodesPlaceMatrix
axis([min(min(nodesPlaceMatrix(:,1)))-backgroundScaleX*0.01 max(max(nodesPlaceMatrix(:,1)))+backgroundScaleX*0.01 min(min(nodesPlaceMatrix(:,2)))-backgroundScaleY*0.01 max(max(nodesPlaceMatrix(:,2)))+backgroundScaleY*0.1],'manual','off');
%Arrow and Line Features
if type==0, %When type=0 we are drawing a physical topology
lineColor = [1 0.41 0];
lineWidth = 0.5;
arrowLength = 15;
arrowBaseAngle = 45;
arrowTipAngle = 5;
arrowWidth = 0;
arrowFaceColor = [1 0.41 0];
else %When type=1 we are drawing a virtual topology
lineColor = [0 0 0.52];
lineWidth = 1;
arrowLength = 19;
arrowBaseAngle = 90;
arrowTipAngle = 5;
arrowWidth = 0;
arrowFaceColor = [0 0 0.52];
end
title(topologyTitle,'FontSize',7,'Fontweight','light','FontName','Verdana');
% f=statusbar('Plotting Physical Topology in progress ...');
for m=1:size(uniqueSorted_edgeTable,1),
% statusbar(m/size(uniqueSorted_edgeTable,1),f);
arrow(line([nodesPlaceMatrix(uniqueSorted_edgeTable(m,1),1) nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),1)],[nodesPlaceMatrix(uniqueSorted_edgeTable(m,1),2) nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),2)],'color',lineColor,'lineWidth',lineWidth),'Length',arrowLength,'BaseAngle', arrowBaseAngle,'TipAngle', arrowTipAngle,'Width',arrowWidth,'FaceColor',arrowFaceColor);
%The text labels are the number of edges established between a pair
%of nodes. They are shown if showNrEdgesPerPair = 1.
if showNrEdgesPerPair==1,
textLabel=num2str(NrOfEdgesPerPair(m));
%We calculate the writing position
distanceX=nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),1) - nodesPlaceMatrix(uniqueSorted_edgeTable(m,1),1);
distanceY=nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),2) - nodesPlaceMatrix(uniqueSorted_edgeTable(m,1),2);
%We write the current the number of edges per pair
distanceVector = [distanceX distanceY 0];
normalVectorToDistanceVector = cross(distanceVector/norm(distanceVector) , [0 0 1]);
text(nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),1)-(distanceX/3)+ textOffset*backgroundScaleX*normalVectorToDistanceVector(1) , nodesPlaceMatrix(uniqueSorted_edgeTable(m,2),2)-(distanceY/3) + textOffset*backgroundScaleY*normalVectorToDistanceVector(2) , textLabel,'FontSize',8,'FontWeight','bold','Color',[1 0.41 0]);
end
end
%At the end, text label of each node is set
for i=1:length(nodesPlaceMatrix),
text(nodesPlaceMatrix(i,1),nodesPlaceMatrix(i,2),{int2str(i)},'FontSize',6,'Color',[1 1 1],'HorizontalAlignment','Center');
end
hold off