function plotCameraImagePlane(cam, zplane, Pw, scale)
% Function that draws the camera and the virtual image plane located at 
% zC = zplane. Draws the points Pw in the world frame and its projection in
% the virtual image plane.
% The scale parameter scales the plot of the camera

    % plot image plane (drawn at distance focus=zplane)
    % Xi = (cam.f/cam.rho(1))*X + cam.u0, with X=Pcx/Pcz
    % Yi = (cam.f/cam.rho(2))*Y + cam.v0, with Y=Pcy/Pcz
    % sx is the value of X that results in the maximum pixel value, cam.npix(1)
    % sy is the value of Y that results in the maximum pixel value, cam.npix(2)
    % The image plane is the square Cc=[[-sx,-sy],[-sx,sy],[sx,sy],[sx,-sy]]
    % Cw are the coordinates of Cc in the world frame
    
    sx=zplane*0.5*cam.npix(1)*cam.rho(1)/cam.f;
    sy=zplane*0.5*cam.npix(2)*cam.rho(2)/cam.f;
    Cc=[[-sx -sy zplane]', [-sx sy zplane]', [sx sy zplane]', [sx -sy zplane]'];
    Cw=cam.T.T*cat(1,Cc,ones(1,size(Cc,2))); %cam.T is SE3 object; cam.T.T is 4x4 matrix
    h = fill3( Cw(1,:), Cw(2,:), Cw(3,:),'r'); 
    set(h,'facealpha',.3);%transparency
    
    %plots supporting plane
    Cc=[[-5*scale -5*scale 1.01*zplane]', [-5*scale 5*scale 1.01*zplane]', [5*scale 5*scale 1.01*zplane]', [5*scale -5*scale 1.01*zplane]'];
    Cw=cam.T.T*cat(1,Cc,ones(1,size(Cc,2)));
    h = fill3( Cw(1,:), Cw(2,:), Cw(3,:),'g'); 
    set(h,'facealpha',.2);%transparency

    % draw point P in the image plane 
    p = cam.project(Pw);
    ray=cam.ray(p);
    for i = 1:size(p,2)
        dC = t2r(inv(cam.T))*ray(i).d; %d vecot in camera coordinates
        lambda = zplane/dC(3);%lambda corresponding to plane zC=zplane
        Pp=ray(i).P0+lambda*ray(i).d;
        plot3(Pp(1),Pp(2),Pp(3),'*k')%plot the points on the image plane
    end
    % plot line from camera origin to Pw
    for i = 1:size(Pw,2)
        plot3([Pw(1,i),cam.T.t(1)],[Pw(2,i),cam.T.t(2)],[Pw(3,i),cam.T.t(3)],'r')
    end
    cam.plot_camera('scale',scale)
    display('cam.T = ');
    cam.T
end