%Ricostruisco i punti con MINIMI QUADRATI ELIMINANDO RAGGI 

%Costrusco le telecamere

T1=[a{1}' b{1}' c{1}' x_f_as(1)*ones(length(a{1}),1) y_f_as(1)*ones(length(a{1}),1)];
T2=[a{2}' b{2}' c{2}' x_f_as(2)*ones(length(a{2}),1) y_f_as(2)*ones(length(a{2}),1)];
T3=[a{3}' b{3}' c{3}' x_f_as(3)*ones(length(a{3}),1) y_f_as(3)*ones(length(a{3}),1)];
T4=[a{4}' b{4}' c{4}' x_f_as(4)*ones(length(a{4}),1) y_f_as(4)*ones(length(a{4}),1)];
T5=[a{5}' b{5}' c{5}' x_f_as(5)*ones(length(a{5}),1) y_f_as(5)*ones(length(a{5}),1)];
T6=[a{6}' b{6}' c{6}' x_f_as(6)*ones(length(a{6}),1) y_f_as(6)*ones(length(a{6}),1)];
T7=[a{7}' b{7}' c{7}' x_f_as(7)*ones(length(a{7}),1) y_f_as(7)*ones(length(a{7}),1)];
T8=[a{8}' b{8}' c{8}' x_f_as(8)*ones(length(a{8}),1) y_f_as(8)*ones(length(a{8}),1)];
T9=[a{9}' b{9}' c{9}' x_f_as(9)*ones(length(a{9}),1) y_f_as(9)*ones(length(a{9}),1)];
T10=[a{10}' b{10}' c{10}' x_f_as(10)*ones(length(a{10}),1) y_f_as(10)*ones(length(a{10}),1)];
T11=[a{11}' b{11}' c{11}' x_f_as(11)*ones(length(a{11}),1) y_f_as(11)*ones(length(a{11}),1)];
T12=[a{12}' b{12}' c{12}' x_f_as(12)*ones(length(a{12}),1) y_f_as(12)*ones(length(a{12}),1)];
T13=[a{13}' b{13}' c{13}' x_f_as(13)*ones(length(a{13}),1) y_f_as(13)*ones(length(a{13}),1)];
T14=[a{14}' b{14}' c{14}' x_f_as(14)*ones(length(a{14}),1) y_f_as(14)*ones(length(a{14}),1)];
T15=[a{15}' b{15}' c{15}' x_f_as(15)*ones(length(a{15}),1) y_f_as(15)*ones(length(a{15}),1)];
T16=[a{16}' b{16}' c{16}' x_f_as(16)*ones(length(a{16}),1) y_f_as(16)*ones(length(a{16}),1)];

%Inizializzo

R11a2=cell(1,2);
R21a2=cell(1,2);
R31a2=cell(1,2);
R41a2=cell(1,2);
R51a2=cell(1,2);
R61a2=cell(1,2);
R71a2=cell(1,2);
R81a2=cell(1,2);

%Associo le telecamere ricordando che al primo passo nn vi sono associazioni

R11a2{1}=T1;
R11a2{2}=T2;
R21a2{1}=T3;
R21a2{2}=T4;
R31a2{1}=T5;
R31a2{2}=T6;
R41a2{1}=T7;
R41a2{2}=T8;
R51a2{1}=T9;
R51a2{2}=T10;
R61a2{1}=T11;
R61a2{2}=T12;
R71a2{1}=T13;
R71a2{2}=T14;
R81a2{1}=T15;
R81a2{2}=T16;

%Inizializzo i punti
P11a2=[-1 -10 -1];

%dimensione stanza
dim_stanza_x=10000;
dim_stanza_y=10000;

%secondo passo dell'albero
tic
[R12a2 P12a2]=associa2(R11a2, R21a2, P11a2, P11a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t1=toc; %Valuto tempo esecuzione
set(S.tP1R4,'string',t1,'visible','on')
tic
[R22a2 P22a2]=associa2(R31a2, R41a2, P11a2, P11a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t2=toc;
set(S.tP1R3,'string',t2,'visible','on')
tic
[R32a2 P32a2]=associa2(R51a2, R61a2, P11a2, P11a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t3=toc;
set(S.tP1R2,'string',t3,'visible','on')
tic
[R42a2 P42a2]=associa2(R71a2, R81a2, P11a2, P11a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t4=toc;
set(S.tP1R1,'string',t4,'visible','on')

tmp=max([t1 t2 t3 t4]);
%terzo passo dell'albero
tic
[R13a2 P13a2]=associa2(R12a2, R22a2, P12a2, P22a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t2=toc;
set(S.tP2R2,'string',t2,'visible','on')
tic
[R23a2 P23a2]=associa2(R32a2, R42a2, P32a2, P42a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
t1=toc;
set(S.tP2R1,'string',t1,'visible','on')
tmp = tmp + max([t1 t2]);

%quarto passo dell'albero

tic
[R14a2 P14a2]=associa2(R13a2, R23a2, P13a2, P23a2,epsilon,dim_stanza_x,dim_stanza_y,d_max);
tmpo=toc;
set(S.tP3R1,'string',tmpo,'visible','on')
tmp=tmp+tmpo;
set(S.Ttot,'string',tmp,'visible','on') %Calcolo tempo esecuzione massimo considerando approccio decentralizzato ad albero