function [P2D, ij_res] = p2d_fit(xy_mark, ij_mark)
	% P2D, res = p2d_fit(xy_mark, ij_mark)
	%
	% fit a simple 2D pinhole model to map the x-y plane in object space
	% to the i-j plane in image space
	%
	% input:
	%    xy_mark		2 x N array of marker locations in object space
	%                 which lie on the x-y plane
	%    ij_mark		2 x N array of marker locations in image space
	%                 which lie on the i-j plane
	%
	% output:
	%    P2D				a matrix representing the pinhole model
	%    ij_res			residual displacement between model and marker
	%						locations in image space
	
	% fit pinhole
	n_mark				= size(xy_mark, 2);
	xy_tilde				= [xy_mark;ones(1,n_mark)];
	P2D					= dlt33([ij_mark;ones(1,n_mark)], xy_tilde);
	% calculate residuals
	if nargout > 1
		ij_model			= P2D * xy_tilde;
		ij_model			= [ij_model(1,:) ./ ij_model(3,:);
								ij_model(2,:) ./ ij_model(3,:)];
		ij_res			= ij_mark - ij_model;
	end
end
