% shows the trajectory traced out by the orbit of a set of parameters
% acting on the reference rectangle
%
% parameters are vectors, differentially expressed
%
% Examples: trajectoryshow(p);     % returns nothing
%         : trajectoryshow(p,3,15);  % plots trajectory frames 3 to 15, mref=3
%         : trajectoryshow(p,3,15,10); % frame 10 is reference frame
%
% Bugs: now only up to 10 frames (quick hack)
%
% See also: cement_pairwise, cement_cumulative

function ilikeusingasingleinput(p,l1,l2,mref)

[M,N] = size(p);

if N~= 8
  error('pintegrate: parameters must be row vectors arranged in 8-wide matrix')
end%if

if M==N
  disp('pintegrate: warning; input is square; double check parameters are across rows')
end%if

if nargin==2
  error('trajectoryshow: need at least 3 input args (p, start and end frame #s)')
end%if
if nargin==3
  disp(sprintf('trajectoryshow: reference frame defaulting to frame %d (lower end of range)',l1))
  mref=l1;
end%if

P=pintegrate(p,mref);

P_part=P(l1+1:l2+1,:);  % select out relevant portion of P

[Ml,Nl,Mh,Nh]=boundingboxextrema([P_part]); % now P includes reference parameter
                                          % which may or may not be the identity

% define bounding rectangle of original frame
ml=0;
nl=0;
mh=1;
nh=1;
m=[ml mh mh ml ml];
n=[nl nl nh nh nl];
one=ones(1,5);

m_all=[]; n_all=[];
for l=l1:l2
  P_now = P(l+1,:);
  P_now = pinverse(P_now);
  % disp(sprintf('plotting frame %d',l))
  % disp(sprintf('P=%g',P_now))
  a=P_now(1); b=P_now(2); c=P_now(3); d=P_now(4); e=P_now(5); f=P_now(6);
    g=P_now(7); h=P_now(8);
  m_now = (a*m + b*n +c)./(g*m + h*n + one);
  n_now = (d*m + e*n +f)./(g*m + h*n + one);
  m_all=[m_all;m_now]; n_all=[n_all;n_now];
  if l==l1  % plot first in sequence
    clg
    plot(n_now,m_now,':') % even with axis('ij'), first axis still across
    hold on
  end%if
  % hak('continue:'); plot(n_now,m_now); drawnow % uncomment to plot all interactively
  axis([Nl,Nh,Ml,Mh])
  axis('ij')
  % axis('equal') % doesnt obey axis limits when set to equal
end%for

  plot(n_now,m_now,'--') % last in sequence (remains after for loop)
  plot(n,m)  % identity frame; solid line
  plot(n_all,m_all); % trajectory
hold off

