% Archana Gupta
% Project

function [newpic,face]=cloud3d(dat)

%horizontal and vertical field of view angles 
fovx=21;  %horizontal fov
fovy=23;  %vertical fov 
fl=8;     %focal length 

%translate x and y coordinates of image such that 
%origin of coord-axis and ccd matrix coincide
transx=(size(dat,1)-1)/2; 
transy=(size(dat,2)-1)/2;

%convert angle to radians
radx = (pi*fovx)/180.0;
rady = (pi*fovy)/180.0;

%find actual length of x and y
xlen = (tan(radx))/transx;
ylen = (tan(rady))/(cos(radx)*transy);

face = [];
row = size(dat, 1);
col = size(dat, 2);

%define faces of mesh
for y = 1:col-1
    for x = 1:row-1
        f1 = (y - 1)* row + x;
        f2 = f1 + 1;
        f3 = (y) * row + x;
        f4 = f3 + 1;
        face = [face; f3 f1 f2; f3 f2 f4];
    end
end
   
newpic=[];

for y = 1:size(dat,2)
    for x = 1:size(dat,1)
        
        z = dat(x,y);
        trsx = -x + transx; 
        trsy = y - transy;
                 
        newfovx = atan(xlen * trsx);
        newfovy = atan(ylen * trsy * cos(newfovx));
                  
        finx = sin(newfovx) * z;
        finy = sin(newfovy) * z * cos(radx);
        finz = cos(newfovx) * cos(newfovy) * z;
        
        newpic= [newpic;finx finy finz];
    end
end
    
figure;
plot3(newpic(:,1),newpic(:,2),newpic(:,3),'.');


   
   
   
   
  