% compute extrema of boundingboxes of multiple p-vectors in a mat.
%
% finds low and high limits of indices normalized to [0,0]..[1,1]
%
% Examples: P = pintegrate(p) % from est_pairwise to est_cumulative
%           boundingboxextrema(P)
%           BB = boundingboxextrema(P);
%           [Ml,Nl,Mh,Nh] = boundingboxextrema(P);

function [Ml_outarglist,Nl,Mh,Nh] = relativeto00011011(P);

% check that input is the correct dimension
[M,N] = size(P);
if N ~= 8
  error( 'boundingbox: input should be row vector(s) of 8 elements')
end%if
if M == N
  disp('boundingbox: input is square matrix: assuming each row is a parameter')
end%if

Ml=[];  Nl=[];  Mh=[];  Nh=[];
for m=1:M
  p = P(m,:);
  mdomain=[0 0 1 1];
  ndomain=[0 1 0 1];
  X=[mdomain;ndomain];                % X is [x;y] colvec coords
  pinver=pinverse(p);
  A=[pinver(1) pinver(2); pinver(4) pinver(5)];           % linear part
  b=[pinver(3);pinver(6)];                      % trans. colvec
  c=[pinver(7) pinver(8)];                      % chirpiness rowvec
  Y=(A*X+b*ones(1,4))./(ones(2,1)*(c*X+ones(1,4)));
  mrange=Y(1,:);
  nrange=Y(2,:);
  Ml = [min(mrange);Ml];
  Mh = [max(mrange);Mh];
  Nl = [min(nrange);Nl];
  Nh = [max(nrange);Nh];
end%for

% compute extrema
Ml = min(Ml);
Mh = max(Mh);
Nl = min(Nl);
Nh = max(Nh);

dis=sprintf('boundingboxextrema:of %d;  Ml=%g, Nl=%g  Mh=%g, Nh=%g',M,Ml,Nl,Mh,Nh);
disp(dis)

if nargout > 0
  Ml_outarglist = Ml;
end%if

if nargout == 1  % overwrite first arg with all the data
  Ml_outarglist=[Ml_outarglist,Nl,Mh,Nh];
end%if

