% EE  392J
% PROJECT

% Ali Ercan and Ulrich Barnhoefer

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function motion = convert(vectors,flag)
%
% This function converts the motion vector fields provided by the motion estimation
% algorithm to motion trajectory matrices as required by the "qModifier" function
%
% inputs  : vectors : The motion vector field (4 dimensional, vectors(i,j,t,:)) is
%                     the motion vector at point in the scene i,j at time t       
%         : flag    : flsg(i,j) tells which frame is the last reliable frame 
%                     for point (i,j) in the scene
% outputs : motion  : The motion trajectory matrices for each pixel
%                     motion{i,j} is a matrix that describes the motion of a point 
%                     in the scene 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function motion = convert(vectors);

[m n T dummy] = size(vectors);

for i = 1:m
    for j = 1:n
        motion{i,j}=[i j 1];
        for time = 1:T
            cur_point = motion{i,j}(end,1:2);
            v = vectors(cur_point(1),cur_point(2),time,:);
            v=reshape(v,1,2);
            if (v(1)~=0)|(v(2)~=0)
                %cur_point = motion{i,j}(end,1:2);
                nextpoint = cur_point+v;
                nextrow = [nextpoint(1),nextpoint(2),time+2];
                motion{i,j} = [motion{i,j};nextrow];
            end
        end
        lastrow = motion{i,j}(end,:);
        if lastrow(3) ~= (T+2)
            lastrow(3) = T+2;
            motion{i,j}=[motion{i,j};lastrow];
        end
    end
end

return;

