function p = gencam(f,ku,kv,angles,T,u0,v0)
%
% gencam: generate a perspective matrix modeling a pin-hole camera
% On input:
%      f:   focal length
%     ku:   CCD array inter-pixel x distance
%     kv:   CCD array inter-pixel y distance
% angles:   three X-Y-Z rotation angles 
%      T:   3D translation matrix from world frame to optical center
%     u0:   x axis center of image plane
%     v0:   y axis center of image plane
% On output:
%     p:   3x4 perspective matrix:
%
%         +-                                -+
%         | au*R(1,:)+u0*R(3,:)  au*Tx+u0*Tz |
%         | av*R(2,:)+v0*R(3,:)  av*Ty+v0*Tz |
%         |          R(3,:)           Tz     |
%         +-                                -+
% Call:
%     p = gencam(f,ku,kv,angles,T,u0,v0)
%

au = -f*ku;
av = -f*kv;
angles = (pi/180)*angles;
s1 = sin(angles(1)); c1 = cos(angles(1));
s2 = sin(angles(2)); c2 = cos(angles(2));
s3 = sin(angles(3)); c3 = cos(angles(3));

R(1,1) = c2*c3;
R(1,2) = -c2*s3;
R(1,3) = s2;
R(2,1) = s1*s2*c3+c1*s3;
R(2,2) = -s1*s2*s3+c1*c3;
R(2,3) = -s1*c2;
R(3,1) = -c1*s2*c3+s1*s3;
R(3,2) = c1*s2*s3+s1*c3;
R(3,3) = c1*c2;
     
p = [ au*R(1,:)+u0*R(3,:), au*T(1)+u0*T(3);
      av*R(2,:)+v0*R(3,:), av*T(2)+v0*T(3);
	           R(3,:)    ,       T(3)];
