//calculate and plot 2D mirror locations for angled laser beam
//to be reflected as though from a point source at the origin
laser_offset=[12;0]; //[cm] x;y parametric line offset
laser_angle=(180-59)/180*%pi; //[rad] up from horizontal CCW
lbh=0.4; //[cm] laser beam height
msl=1.5; //[cm] mirror side length
mth=0.1; //[cm] mirror thickness
pll=4; //[cm] projected line render length
lll=40; //[cm] projected laser render length
lx=[cos(laser_angle);sin(laser_angle)]; //dx;dy parametric line slope
lp=[-lx(2);lx(1)]; //perpendicular line to laser
projected_angles=linspace(0,90,16)/180*%pi; //[rad] horizontal from origin CCW
//projected_angles($)=[];
po=[0;0]; //x;y apparent point source location
na=length(projected_angles);
mplot2d=%t;
mplotlp=%f;

function tu=pl_intersect(ao,ax,bo,bx)
    //intersect two 2D parametric lines with offset and slope vectors
    mat=[-ax(1),bx(1);-ax(2),bx(2)];
    val=[ao(1)-bo(1);ao(2)-bo(2)];
    tu=mat\val;
endfunction

if(mplot2d)
    l1=laser_offset;
    l2=laser_offset+lx*lll;
    //plot([l1(1),l2(1)],[l1(2),l2(2)],'r--'); //expected laser beam
    l1=l1-(lbh/2)*lp;
    l2=l2-(lbh/2)*lp;
    plot([l1(1),l2(1)],[l1(2),l2(2)],'r--'); //expected laser beam
    l1=l1+(lbh)*lp;
    l2=l2+(lbh)*lp;
    plot([l1(1),l2(1)],[l1(2),l2(2)],'r--'); //expected laser beam
end

mos=[];//mirror centers
mas=[];//mirror angles

for(i=1:na)//(na-1):na)
    pa=projected_angles(i);
    px=[cos(pa);sin(pa)]; //dx;dy of projected line
    tu=pl_intersect(po,px,laser_offset,lx);
    ipt=po+px*tu(1);//lines intersection
    ia=pa-(pa-(laser_angle-%pi))/2;//angle for mirror normal
    mos(:,i)=ipt;//save mirror centers
    mas(i)=ia;//save mirror angles
    if(mplot2d)
        mx=[cos(ia);sin(ia)];//mirror normal
        mp=[-mx(2);mx(1)];//perpendicular to mirror normal
        m1=ipt+(msl/2)*mp;
        m2=ipt-(msl/2)*mp;
        ppt=ipt+px*pll;
        plot([m1(1),m2(1)],[m1(2),m2(2)],'b'); //expected mirror location
        plot([ipt(1),ppt(1)],[ipt(2),ppt(2)],'k'); //expected projected line
    end
end

if(mplot2d)
    isoview on
end

mes=-msl*cos(mas-laser_angle);//[rad] mirror effective size as seen by laser beam
ald=(mas-laser_angle)/%pi*180+180;//[deg] laser angle away from mirror normal
mld=mas'/%pi*180;//[deg] mirror normal angle
if(mplotlp)
    plot(cos(projected_angles'),mes/lbh);
    plot([0,1],[0,1],'r--');
    isoview on
end
