function [ext_dist] = calc_extrinsic_dist_withU(n,m,P1,P2, U); 
%Note that U must be obtained by [U S V]=svd(P1)
Q = U'*P1*U;
P = U'*P2*U;
A = Q-P;
%disp('Eigenvalues of Q-P....');
[X Y] =eig(A);
%disp('Eigenvalues Done');
%disp('Forming the Unitary Matrix .... ');
 % Forming the Unitary Matrix
 chek = zeros(1,n);
 cnt = 1;
 for i=1:n
    %i
    if (chek(i) == 0 & (abs(Y(i,i)) > 0.00001)) 
       for k=i:n
          if (chek(k) == 0 & (abs(Y(i,i) + Y(k,k)) < 0.00001))
             idx(cnt) = i;
             idx(cnt+1) = k;
             chek(k) = 1.0;
             chek(i) = 1.0;
             %sprintf('Connected %d and %d',i,k)
             cnt = cnt + 2;
          end
       end
    end
 end
 
 for i=1:n
    if chek(i) == 0
       idx(cnt) = i;
       cnt = cnt + 1;
    end
 end
 
   
 % Rearrange
 Yn = zeros(n,n);
 %  Xn = zeros(n,n);
 for i=1:n
    % Xn(:,i) = X(:,idx(i));
    Yn(i,i) = Y(idx(i),idx(i));
 end
 
%disp('Done');
%disp('Calculating the velocity ... ');
% Zn = zeros(n,n);
 i=1;
 Ome = zeros(n,n);
 while i < n+1
    if abs(Yn(i,i)) > 0.000001
       %Zn(i,i) = sqrt(1-Yn(i,i)^2);
       %Zn(i+1,i+1) = Zn(i,i);
       %Zn(i,i+1) = -Yn(i,i);
       %Zn(i+1,i) = Yn(i,i);
       Ome(i,i) = 0;
       Ome(i,i+1) = -asin(Yn(i,i));
       Ome(i+1,i) = asin(Yn(i,i));
       Ome(i+1,i+1) = 0;
       i = i + 1;
       % else
       %  Zn(i,i) = 1.0;
    end
    i = i+1;
 end
  
%  for i=1:2:n
%     if Zn(i,i) ~= 1.0
%     	t1 = Xn(1,i);
%     	t2 = Xn(1,i+1);
%     	tmp = t1/t2;
%     	tmp = tmp/abs(tmp);
%      Xn(:,i+1) = tmp*Xn(:,i+1);
%     end
% end
  
%disp('Done.');
%Vel = Xn*Ome*Xn';  
ext_dist = norm(Ome,'fro');

return;
% keyboard;
    






