
nprojectors=3;
projectors=cell([nprojectors 1]);

p_radius = 12.5;
p_height = 6;
p_start = 0.25;
p_target=[0 0 0]';
p_im_height_pixels = 256;
p_im_width_pixels = 256;
p_pl_height= 1.0;
p_pl_width = 1.0;
p_bk_color=[0 0  0];


for c_proj=1:nprojectors,
   c_angle=(c_proj-1+p_start)*2*pi/nprojectors;
   projectors{c_proj}.center=[p_radius*cos(c_angle) p_radius*sin(c_angle) p_height]';
   projectors{c_proj}.target=p_target;
   projectors{c_proj}.f=1.50;
   projectors{c_proj}.upvector=[0 0 1]';
   projectors{c_proj}.im_height = p_im_height_pixels;
   projectors{c_proj}.im_width = p_im_width_pixels;
   projectors{c_proj}.pl_height = p_pl_height;
   projectors{c_proj}.pl_width = p_pl_width;
   projectors{c_proj}.bkcolor=p_bk_color;
   projectors{c_proj}.ray_len=4.5;
   projectors{c_proj}.ray_style='b-';
   projectors{c_proj}.drawing=0;
   projectors{c_proj}.draw_scale=0.35;
   projectors{c_proj}.weight = 0.60;
   projectors{c_proj}.imagename='frame_patterns_4.bmp';
   %If the following flag is zero, then the image size will be changed to the size given by
   %the image
   projectors{c_proj}.scaling_image_flag = 0;
end

for i=1:nprojectors,
   [projectors{i}.image, cmap]=imread(projectors{1}.imagename);
   projectors{i}.image=ind2rgb(projectors{i}.image, cmap);
   projectors{i}.image=projectors{i}.image(:,1:1000,:)*255;
   
   if projectors{1}.scaling_image_flag==0,
      projectors{1}.im_height=size(projectors{i}.image,1);
      projectors{1}.im_width = size(projectors{i}.image,2);
   else
      projectors{i}.image=imresize(projectors{i}.image,[projectors{1}.im_height projectors{1}.im_width],'bilinear');
   end
   
   projectors{i}.z=projectors{i}.target-projectors{i}.center;
   projectors{i}.z=projectors{i}.z/norm(projectors{i}.z);
   projectors{i}.y=projectors{i}.upvector-dot(projectors{i}.upvector,projectors{i}.z)*projectors{i}.z;
   projectors{i}.y=projectors{i}.y/norm(projectors{i}.y);
   projectors{i}.x=cross(projectors{i}.y,projectors{i}.z);
   %This is the pose of the projector in the world coordinate
   projectors{i}.Rw=[projectors{i}.x,projectors{i}.y,projectors{i}.z];
   %This is the position of the projector in the world coordinate
   projectors{i}.Tw=projectors{i}.center;
   %Note that given a point Xw in the world coordinate, its coorindate in the projector Xc is given by
   % Xc = projectors{i}.Rw'*Xw-projectors{i}.Rw'*projectors{i}.center
   % The following is the matrix that converts a point in the image plane to the corresponding point
   % in the pixel coordinate
   %
   projectors{i}.world2proj=[projectors{i}.Rw',-projectors{i}.Rw'*projectors{i}.Tw; 0 0 0 1];
   projectors{i}.proj2image=[-projectors{i}.im_width/projectors{i}.pl_width 0 (projectors{i}.im_width+1)/2
      0   -projectors{i}.im_height/projectors{i}.pl_height (projectors{i}.im_height+1)/2
      0                 0                                1];
   projectors{i}.f_K=diag([projectors{i}.f projectors{i}.f 1]);
   projectors{i}.K=projectors{i}.proj2image*projectors{i}.f_K;
   %Given a point in the pixel coordinate xim, we can get the point on the normalized image plane
   %xn = inv(projectors{i}.K) * xim;
   projectors{i}.P=projectors{i}.K*[1 0 0 0;0 1 0 0; 0 0 1 0]*projectors{i}.world2proj;
   %Checking, the target point should be mapped to the center of the image
   target_proj=projectors{i}.P*[projectors{i}.target;1];
   target_proj=target_proj./target_proj(end);
   if norm(target_proj-projectors{i}.proj2image(:,end))>0.00001,
      fprintf(1,'Something is wrong for projector %d. Its target should be mapped to ', i);
      fprintf(1,'%6.2f %6.2f instead of ', projectors{i}.proj2image(1,end),projectors{i}.proj2image(2,end));
      fprintf(1,'%6.2f %6.2f\n',target_proj(1),target_proj(2));
   end
   target_z=projectors{i}.Rw'*[projectors{i}.z];
   if norm(target_z-[0 0 1]')>0.00001,
      fprintf(1,'Something is wrong for projector %d. Its z axis should be mapped to ', i);
      fprintf(1,'[0 0 1] instead of ');
      target_proj=projectors{i}.world2proj*[projectors{i}.z; 1];
      fprintf(1,'[%6.2f %6.2f %6.2f]\n',target_z(1),target_z(2),target_z(3));
   end
   center_proj=projectors{i}.world2proj*[projectors{i}.center;1];
   center_proj=center_proj/center_proj(end);
   center_proj=center_proj(1:3);
   if norm(center_proj-[0 0 0]') > 0.00001,
      fprintf(1,'Something is wrong for projector %d. Its center should be mapped to ', i);
      fprintf(1,'[0 0 0] instead of ');
      fprintf(1,'%6.2f %6.2f %6.2f\n',center_proj(1),center_proj(2),center_proj(3));
   end
   image_plane_center=projectors{i}.Tw+projectors{i}.f*projectors{i}.z;
   projectors{i}.v=[projectors{i}.pl_width/2*projectors{i}.x+projectors{i}.pl_height/2*projectors{i}.y+image_plane_center];
   projectors{i}.v=[projectors{i}.v,cameras{1}.pl_width/2*projectors{i}.x-projectors{i}.pl_height/2*projectors{i}.y+image_plane_center];
   projectors{i}.v=[projectors{i}.v, -cameras{1}.pl_width/2*projectors{i}.x-projectors{i}.pl_height/2*projectors{i}.y+image_plane_center];
   projectors{i}.v=[projectors{i}.v, -cameras{1}.pl_width/2*projectors{i}.x+projectors{i}.pl_height/2*projectors{i}.y+image_plane_center];
   projectors{i}.f=[1 2 4;2 3 4];
   
   if projectors{i}.drawing,
      f_3Dprojector([projectors{i}.Rw,projectors{i}.Tw],'b-',projectors{i}.draw_scale,2,3);
   else
      
      hh_proj=plot_mesh(projectors{i}.v,projectors{i}.f);
      for ii=1:size(projectors{i}.v,2),
         dir_v=projectors{i}.v(:,ii)-projectors{i}.Tw;
         dir_v=dir_v/norm(dir_v);
         plot3arr([projectors{i}.Tw';(projectors{i}.Tw+projectors{i}.ray_len*dir_v)'],projectors{i}.ray_style,2);
      end
      plot3arr([projectors{i}.Tw';(projectors{i}.Tw+projectors{i}.ray_len*projectors{i}.z)'],projectors{i}.ray_style,4);
      hh=plot3arr(projectors{i}.Tw',[projectors{i}.ray_style(1) 'o'],4);
      set(hh,'MarkerSize',[6]);
      hh=plot3arr(projectors{i}.Tw',[projectors{i}.ray_style(1) '+'],4);
      set(hh,'MarkerSize',[6]);
      %warp(projectors{i}.v(1,:)',projectors{i}.v(2,:)',projectors{i}.v(3,:)',projectors{i}.image);
      warp_x=[projectors{i}.v(1,1) (projectors{i}.v(1,1)+projectors{i}.v(1,4))/2 projectors{i}.v(1,4)
      projectors{i}.v(1,2) (projectors{i}.v(1,2)+projectors{i}.v(1,3))/2 projectors{i}.v(1,3)];
   warp_y=[projectors{i}.v(2,1) (projectors{i}.v(2,1)+projectors{i}.v(2,4))/2 projectors{i}.v(2,4)
   projectors{i}.v(2,2) (projectors{i}.v(2,2)+projectors{i}.v(2,3))/2 projectors{i}.v(2,3)];
  warp_z=[projectors{i}.v(3,1) (projectors{i}.v(3,1)+projectors{i}.v(3,4))/2 projectors{i}.v(3,4)
      projectors{i}.v(3,2) (projectors{i}.v(3,2)+projectors{i}.v(3,3))/2 projectors{i}.v(3,3)];
  warp(warp_x,warp_y,warp_z,imresize(projectors{i}.image,[20 20]));
  end

end
