//mirror pixel locations calculator
//for spinning mirror at phi=45 degrees
//and lasers arranged in a uniform circle at phi=0 degrees

//for this case we use the numerically established finding that, with -%pi/2 < mirror theta < %pi/2
// laser spot theta = sign(mirror_theta) * (%pi/2) + mirror_theta
// laser spot phi = (%pi/2) - abs(mirror_theta)

num_lasers=16;
laser_acceptance_angle_h=88/180*%pi; //+-[rad]
//npl=360;
//mphi=%pi/4;
//mirror_angles=(-180+linspace(0,360,npl))/180*%pi;
minphi=(%pi/2)-abs(laser_acceptance_angle_h); //[rad] lowest phi of reflected spot

laser_dtheta=2*%pi/num_lasers; //[rad] angle between adjacent lasers

//intersect lasers on (theta,phi)
//on the right side theta>%pi/2 to intersect with left side requires
// dtheta of >%pi and integer multiple of laser_dtheta, where the intersection
// is at the midpoint of the multiple and %pi/2 with phi equal to %pi/2-difference
lho=ceil(%pi/laser_dtheta)*laser_dtheta-%pi/2;
theta1=(lho+%pi/2)/2;//points in circle 1 and lower odd intersections
phi1=%pi/2-(lho-%pi/2)/2;//phi of circle 1
theta2=theta1+laser_dtheta/2;//points in circle 2 and lower even intersections
phi2=phi1-laser_dtheta/2;
theta3=theta1+laser_dtheta/4;
phi3=phi1-laser_dtheta/4;//next higher intersection point is 0

//intersection points
if(%t)
pl=floor((phi1-minphi)/(laser_dtheta/2))+1;
phis=phi1-((1:pl)-1)*(laser_dtheta/2);
ts=linspace(0,2*%pi,num_lasers+1);
ts($)=[];
for i=1:length(phis)
    phi=phis(i);
    if(modulo(i,2)==1)
        theta=theta1;
    else
        theta=theta2;
    end
    //intpts=[intpts,[theta_int+i*laser_dtheta;phi_int]];
    intpts=[theta+ts;phi*ones(1,num_lasers)];
    scatter3(cos(intpts(1,:)).*cos(intpts(2,:)),sin(intpts(1,:)).*cos(intpts(2,:)),sin(intpts(2,:)));
end
end

//quarter-split points
pl=floor((phi3-minphi)/(laser_dtheta/2))+1;
phis=phi3-((1:pl)-1)*(laser_dtheta/2);
ts=linspace(0,2*%pi,2*num_lasers+1);
ts($)=[];
if(%t)
for i=1:length(phis)
    phi=phis(i);
    intpts=[theta3+ts;phi*ones(1,2*num_lasers)];
    param3d(cos(intpts(1,:)).*cos(intpts(2,:)),sin(intpts(1,:)).*cos(intpts(2,:)),sin(intpts(2,:)),"markerEdgeColor",[0,0.5,0]);
    gce().mark_mode = "on";
    gce().line_mode = "off";
    gce().mark_style=1;
end
end

hres=length(ts);
vres=length(phis);
merr=2*%pi/hres*sqrt(2)/2;
mprintf("Resolution H x V = %i x %i, max err = %f\n",hres,vres,merr);

