//fixed mirror placement calculator
//uses parameters from "Fixed tilted mirrors calc.sce"
if(~exists("mos"))
    error("Run Fixed tilted mirrors calc.sce to load parameters");
end
//msl=0.5;//[cm] mirror side length
//lbd=0.3;//[cm] laser beam diameter
//lbw=0.2; //[cm] laser beam width
mplot2d=%t;
mplace=%f;
mirror_r=mos(1,:);//[cm] mirror distance from central axis, from running "Fixed tilted mirrors calc.sce"
num_mirrors=length(mirror_r);
//mirror_t=(1:num_mirrors)/num_mirrors*2*%pi;//[rad] theta locations of mirrors
dtml=(2*%pi/num_mirrors)/4; //[rad] +-theta of mirror for each laser to cover an equal share of full circle
//mirror_ar=zeros(1,num_mirrors);//no angular change in reflection
mirror_ar=ones(1,num_mirrors)*dtml;//[rad] +-theta acceptance angle of each mirror
//mirror_ar=mirror_ar./exp((1:num_mirrors)./num_mirrors);// (reduce for ones closer to center)
nqt=floor(num_mirrors/4);
/*
mirror_ar(1:nqt)=mirror_ar(1:nqt)*(3/4);//reduce resolution based on 4 pixels per laser
mirror_ar(nqt+(1:nqt))=mirror_ar(nqt+(1:nqt))*(2/3);//3 pixels
mirror_ar(2*nqt+(1:nqt))=mirror_ar(2*nqt+(1:nqt))*(1/2);//2 pixels
mirror_ar((3*nqt+1):$)=mirror_ar((3*nqt+1):$)*(0);//1 pixels
mirror_ar=mirror_ar+dtml/10;//a little bit of extra room
*/
nmp=0;

while(nmp<num_mirrors)
if(mplot2d)
clf();
end

plo=zeros(2,num_mirrors*2);
plx=plo;

num_thetas=10*num_mirrors;
mirror_t=zeros(1,num_mirrors);
mirror_tt=zeros(1,num_mirrors);
mirror_ts_used=zeros(1,num_thetas);

ti=1;
nmp=0;
for(i=num_mirrors:-1:1)
    nt=1;
    ti=mirror_ttv1(i);
    //ti=ceil(rand()*num_thetas);
    //tdir=1;
    tdir=round(rand())*2-1;
    if(nmp==0)
        ti=1;
    end
    if(nmp==1)
        ti=floor(num_thetas/2);
    end
    if(mplace)
    while(nt<=num_thetas)
        if(mirror_ts_used(ti)==0)
            mt=ti/num_thetas*2*%pi;
            intf=%f;
            if(nmp>0)
                til=ti;
                tiq=1;
                while(mirror_ts_used(til)==0 && tiq<num_thetas)
                    til=til-1;
                    if(til<1)
                        til=til+num_thetas;
                    end
                    tiq=tiq+1;
                end
                if(tiq>num_thetas)
                    error("Could not find lines to intersect L");
                end
                tir=ti;
                tiq=1;
                while(mirror_ts_used(tir)==0 && tiq<num_thetas)
                    tir=tir+1;
                    if(tir>num_thetas)
                        tir=tir-num_thetas;
                    end
                    tiq=tiq+1;
                end
                if(tiq>num_thetas)
                    error("Could not find lines to intersect R");
                end
                mr=[cos(mt),-sin(mt);sin(mt),cos(mt)];
                mo=(mirror_r(i)-mth)*(mr*[1;0]);
                mx=(msl/2)*mr*[0;1];
                tir=mirror_ts_used(tir);
                til=mirror_ts_used(til);
                plt=[plo(:,tir*2-1),plx(:,tir*2-1),plo(:,til*2),plx(:,til*2)];
                for(h=[1,3])
                    tu=pl_intersect(mo,mx,plt(:,h),plt(:,h+1));
                    if(tu(1)>-1 && tu(1)<1 && tu(2)>-mth)
                        intf=%t;//interference
                        break;
                    else
                        //clear
                    end
                end
            end
            if(~intf)
                mirror_t(i)=mt;
                mirror_tt(i)=ti;
                mirror_ts_used(ti)=i;
                nmp=nmp+1;
                break;
            end
        end
        ti=ti+tdir;
        if(ti>num_thetas)
            ti=ti-num_thetas;
        end
        if(ti<1)
            ti=ti+num_thetas;
        end
        nt=nt+1;
    end
    if(nt>num_thetas)
        disp(sprintf('Could not place mirror %d',i));
        break;
    end
    else
    mirror_t(i)=ti/num_thetas*2*%pi;
    nmp=nmp+1;
    end
    mt=mirror_t(i);
    mr=[cos(mt),-sin(mt);sin(mt),cos(mt)];
    mo=mirror_r(i)*(mr*[1;0]);
    mx=(msl/2)*mr*[0;1];
    mp=[mo+mx,mo-mx];
    if(mplot2d)
    plot(mp(1,:),mp(2,:));
    end
    mt=mirror_ar(i);
    lr=[cos(mt),-sin(mt);sin(mt),cos(mt)];
    lo=mr*(lr*[mirror_r(i);lbw/2]);
    lw=mr*(lr*[laser_offset(1);lbw/2]);
    if(mplot2d)
    plot([lo(1),lw(1)],[lo(2),lw(2)],'r');
    end
    plo(:,2*i)=lo;
    plx(:,2*i)=(lw-lo)/norm(lw-lo);
    mt=-mt;
    lr=[cos(mt),-sin(mt);sin(mt),cos(mt)];
    lo=mr*(lr*[mirror_r(i);-lbw/2]);
    lw=mr*(lr*[laser_offset(1);-lbw/2]);
    if(mplot2d)
    plot([lo(1),lw(1)],[lo(2),lw(2)],'r');
    end
    plo(:,2*i-1)=lo;
    plx(:,2*i-1)=(lw-lo)/norm(lw-lo);
end

end

if(mplot2d)
isoview on
end
