function [p, res] = proj(b, wx, wy)
% Created by Yuandong Tian, Oct. 5, 2009
% Email: yuandong@andrew.cmu.edu
% project the warping onto bases b.

if isstruct(wx) 
    if exist('wy', 'var')
        index = wy;
    else
        index = 1:wx.nFrame;
    end;
    data = [reshape(wx.Xs(:, :, index), b.h*b.w, length(index)); reshape(wx.Ys(:, :, index), b.h*b.w, length(index))];
    p = [b.bx; b.by]' * data;
else
    data = [reshape(wx, b.h*b.w, 1); reshape(wy, b.h*b.w, 1)];
    p = [b.bx; b.by]' * data;
end;

if nargout >= 2
    res = [b.bx; b.by] * p - data;
    res = sqrt(sum(res.^2, 1));
end;