function mdPlot(azimuth,speed,varargin)
%mdPlot()  Plots motion data for an arbitary number of motion data sets.
%   Complementary colours are used to display consecutive pairs of motion
%   data sets.
%
%   azimuth (real):  rotational point of view.
%   speed (real):  display speed.
%   varargin (md):  one or more motion data (md) sets or motion move
%       structures, which are normally part of a session structure.
%
%   Example:  mdPlot(0,60,session1.move{1}.mdData,session2.move{1});
%
%   Also see mdShift().
%
%   This function and the MAStools 3.x (part of the BMLtoolbox) are
%   copyright (C) 2007 Biomotion Lab, Queen's University (Psychology).
%   For support contact Shawn Leclaire (leclaire@cs.queensu.ca).

% Check function arguments.
if nargin < 3
    error('Too few arguments.');
end

figgy = figure;

% Define complementary colour pairs.
clr{1} = [1.0 0.0 0.0];   % red
clr{2} = [0.0 1.0 0.0];   % green
clr{3} = [1.0 0.5 0.0];   % orange
clr{4} = [0.0 0.0 1.0];   % blue
clr{5} = [1.0 1.0 0.0];   % yellow
clr{6} = [0.5 0.0 1.0];   % purple

for d = 1:numel(varargin)
    var_data_in = varargin{d};
    if(~isstruct(var_data_in))
        md = var_data_in;
    else
        if isfield(var_data_in,'mdData')
            md = var_data_in.mdData;
        else
            error('Structure does not contain mdData.');
        end
    end

    if numel(size(md)) == 3
        clear data;
        f = size(md,1);        % f for frames
        nSpeedAdjustment = 120; % Why 120?  120 is a common sampling rate.
        lsVec = linspace(1,f,round(f * speed / nSpeedAdjustment));
        data(:,:,1) = interp1(1:f,squeeze(md(:,:,1)),lsVec);
        data(:,:,2) = interp1(1:f,squeeze(md(:,:,2)),lsVec);
        data(:,:,3) = interp1(1:f,squeeze(md(:,:,3)),lsVec);
        mdi{d} = data;

        % Map complementary colours to pairs of moves.
        c = mod(d,numel(clr));
        if c == 0
            c = numel(clr);
        end
        mdiclr{d} = clr{c};
    else
        error(['Motion data has wrong number of coordinates: ' ...
            int2str(d)]);
    end
end

azimuth = azimuth + 90;   % Rotate walker

% Replace with motion data specific limits.
axis([-600 600 -850 850 0 2100]);
% Set the aspect ratio so that the units are the same in every direction.
axis equal;

% Specify the view point using azimuth and elevation.
view([azimuth,0]);

% Retain current plot so further graphics command add to picture.
hold on;

% Set current figure (cf) properties.
set(figgy,'Position',[64,64,512,512]);
set(figgy,'doublebuffer','on','color','white');
cameratoolbar(figgy);
global bCloseNow;
bCloseNow = false;

% Create Close button and link it to global variable 'bCloseNow'.
btnClose = uicontrol('style','pushbutton',...
    'string','Close',...
    'units','normalized',...
    'position',[0 0 0.3 0.06],...
    'callback',['global bCloseNow;' 'bCloseNow = true;'],...
    'fontsize',10.0);

% Establish plot array with first frame of each motion data.
for d = 1:numel(varargin)
    data = mdi{d};
    for m = 1:size(varargin{d},2)
        p(m) = plot3(...
            squeeze(data(1,m,1)),...
            squeeze(data(1,m,2)),...
            squeeze(data(1,m,3)),...
            'b.',...
            'EraseMode','background',...
            'MarkerEdgeColor',mdiclr{d},...
            'MarkerSize',15);
        pi{d} = p;
    end
end
drawnow;

maxlength = 0;
for d = 1:numel(mdi)
    maxlength = max(maxlength,size(mdi{d},1));
end

for f = 1:maxlength
    if(bCloseNow)
        break
    end

    for d = 1:numel(mdi)
        clear data;
        data = mdi{d};
        p = pi{d};

        if f < size(data,1)
            for m = 1:size(data,2)
                set(p(m),...
                    'XData',data(f,m,1),...
                    'YData',data(f,m,2),...
                    'ZData',data(f,m,3));
            end
        end
    end
    drawnow;
end

clear global bCloseNow figgy;
close;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
