% kalman_predict( cur) - compute predictions for next step
%
function kalman_predict( cur)

global K;
global M;
global L;
global A;
global kxv;

% stuff for precomputed incremental costs
%
global Winc;
global log2pi;
global Kv;
global MM;
global XY;
global XY_offset;
global HUGE_VAL;

MK = M*K;

i1 = (cur-1)*MK + [1:MK];		% index for current kxv values
i2 = i1 + MK;				% index for next kxv values

for el=0:L-1
  el12 = [1:2] + el*4;
  el34 = el12 + 2;
  kxv(el12,i2) = A*kxv(el12,i1);	% x's and xv's
  kxv(el34,i2) = A*kxv(el34,i1);	% y's and yv's
end


% precompute incremental costs
%
% By precomputing all possible incremental costs we achieve some execution
% speedup in get_perm() because the computations here are vectorized, and
% get_perm() can simply index the Winc array to get costs.  This also
% eliminates some redundancy in the computations over permutations of the
% measurement-track associations which get_perm() used to do, e.g. costs
% for permutations [1 2 3] and [1 3 2] used to involve computing the cost
% of using measurement 1 for track 1 twice, once for each permutation.
% 
next = cur + 1;

if( (K > 1) & (MM(next) > 0))

  extra = log2pi + log(Kv);

  el1 = 1 + 4*[0:L-1];		% el offsets for kxv
  el3 = 2 + el1;

  for j = 1:MM(next)		% for each measurement

    ii2 = XY_offset(next) + j;	% index for XY

    if( XY(1,ii2) == HUGE_VAL) % for testing with false detections never choosen

      Winc(:,j) = HUGE_VAL;

    else

      rx = XY(1,ii2) - kxv(el1,i2)';
      ry = XY(2,ii2) - kxv(el3,i2)';

      Winc(:,j) = reshape( (rx .* rx + ry .* ry) / (2*Kv) + extra, size(Winc,1), 1);

    end % if

  end % j

end % if
