function [Mean_Square_Residual,Cmax,Grid2D_recalc] = DLT_Recalc(DLT_result,R,Grid)

%Rekalkulace bodu v prostoru do roviny obrazu pri znamych parametrech kamery, 
%vypocet prumerne odchylky, je zanedbano zkresleni, nebo musi byt
%odstraneno

[m,n] = size(Grid);                                                         %matice obsahujici souradice bodu v prostoru a v obrazu
R = -R;                                                                     %matice zrcadleni, jedna se o matici vypocitanou DLT nebo prostorovou resekci
T = DLT_result(2:4);                                                        %vektor souradnic stredu promitani v globalnim s. s.
x0_shift = DLT_result(8);                                                   %principalni bod snimku
y0_shift = DLT_result(9);
cx = DLT_result(10);                                                        %ohniskove vzdalenosti ve smeru osy x a y
cy = DLT_result(11);

for i=1:m                                                                   %vypocet souradnic bodu v s. s. obrazu pomoci zakladnich rovnic fotogrammetrie
    x(i) = x0_shift-cx*...
      ((R(1,1)*(Grid(i,2)-T(1))+R(2,1)*(Grid(i,3)-T(2))+R(3,1)*(Grid(i,4)-T(3)))/...
       (R(1,3)*(Grid(i,2)-T(1))+R(2,3)*(Grid(i,3)-T(2))+R(3,3)*(Grid(i,4)-T(3))));
    y(i) = y0_shift-cy*...
      ((R(1,2)*(Grid(i,2)-T(1))+R(2,2)*(Grid(i,3)-T(2))+R(3,2)*(Grid(i,4)-T(3)))/...
       (R(1,3)*(Grid(i,2)-T(1))+R(2,3)*(Grid(i,3)-T(2))+R(3,3)*(Grid(i,4)-T(3))));
   
   Residual(i,1) = x(i)-Grid(i,5);                                          %vypocet odchylek
   Residual(i,2) = y(i)-Grid(i,6);
   Square_residual(i,1) = sqrt(Residual(i,1)^2+Residual(i,2)^2);
end

Cmax = max(Square_residual);                                                %prumerna a maximalni odchylka
Mean_Square_Residual = (sum(Square_residual))/m;

end