
    3j3                        S r SSKrSSKJrJr  SSKJr  SSKJr  SSKJ	r	   SS\S\S	\S
\
S\S\4S jjr SS\S\S	\S
\
S\S\4S jjr\R                  R                     SS\S\S	\S
\
S\S\S\4S jj5       r SS\S\S	\S
\
S\S\4S jjr SS\S\S	\S
\
S\S\4S jjr SS\S\S	\S
\
S\S\4S jjrS\S\S	\S\4S jrS\S\S	\S\4S jrg)z:Module including useful metrics for Structure from Motion.    N)Tensor	ones_like)KORNIA_CHECK_IS_TENSOR)convert_points_to_homogeneous)point_line_distancepts1pts2Fmsquaredepsreturnc                    [        U[        5      (       d  [        S[        U5       35      e[	        UR
                  5      S:  d  UR
                  SS S:X  d  [        SUR
                   35      eU SSS2S4   nU SSS2S	4   nUSSS2S4   nUSSS2S	4   nU R
                  S
   S:X  a
  U SSS2S4   O
[        U5      n	UR
                  S
   S:X  a
  USSS2S4   O
[        U5      n
US   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nX-  X-  -   X-  -   nX-  X-  -   UU	-  -   nUU-  UU-  -   UU	-  -   nX-  X-  -   UU
-  -   nX-  X-  -   UU
-  -   nUU-  UU-  -   U
U-  -   S-  nUU-  UU-  -   UU-  -   UU-  -   U-   nUU-  nU(       a  U$ UU-   R                  5       $ )  Return Sampson distance for correspondences given the fundamental matrix.

Args:
    pts1: correspondences from the left images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
          converted automatically.
    pts2: correspondences from the right images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
          converted automatically.
    Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to avoid ambiguity with torch.nn.functional.
    squared: if True (default), the squared distance is returned.
    eps: Small constant for safe sqrt.

Returns:
    the computed Sampson distance with shape :math:`(*, N)`.

#Fm type is not a torch.Tensor. Got    Nr   r   #Fm must be a (*, 3, 3) tensor. Got .r         .r   r   .N.r   r   .r   r   .r   r   .r   r   .r   r   .r   r   .r   r   .r   r   )	
isinstancer   	TypeErrortypelenshape
ValueErrorr   sqrt)r   r	   r
   r   r   xyuvw1w2f00f01f02f10f11f12f20f21f22Fx0Fx1Fx2Ft0Ft1numdenouts                               [/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/epipolar/_metrics.py'_sampson_epipolar_distance_manual_impl_rA      sN   $ b&!!=d2hZHIIBHH"((23-6"9>rxxjIJJ 	S!QYAS!QYAS!QYAS!QYA JJrNa/c1aiYq\B JJrNa/c1aiYq\B Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C 'CG
ch
&C
'CG
cBh
&C
'C!G
cBh
&C 'CG
cBh
&C
'CG
cBh
&C s7QWrCx'A
-C
)cCi
#)
+cCi
7#
=C)C
#I    c                    U R                   S   S:X  a  [        U 5      n UR                   S   S:X  a  [        U5      nUR                  SSS9nX-  nX-  nX-  R                  SS9R	                  S5      nUSSS24   R                  SSS9R	                  S5      USSS24   R                  SSS9R	                  S5      -   n	X-  n
U(       a  U
$ X-   R                  5       $ )r   r   r   r   dim0dim1dim.Nr&   r   	transposesumpownormr(   )r   r	   r
   r   r   F_t
line1_in_2
line2_in_1	numeratordenominatorr?   s              r@   '_sampson_epipolar_distance_matmul_impl_rS   \   s   $ zz"~,T2zz"~,T2 ,,BR,0CJJ *//B/7;;A>I %S"1"W-221"2=AA!DzRUWYXYWYRYGZG_G_`agiG_GjGnGnopGqqK)C
IrB   use_matmul_at_less_than_pointsc                     UR                   R                  S:X  a!  U R                  S   nXe:  a  [        XX#U5      $ [	        XX#U5      $ )a  Return Sampson distance for correspondences given the fundamental matrix.

Args:
    pts1: correspondences from the left images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
          converted automatically.
    pts2: correspondences from the right images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
          converted automatically.
    Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to avoid ambiguity with torch.nn.functional.
    squared: if True (default), the squared distance is returned.
    eps: Small constant for safe sqrt.
    use_matmul_at_less_than_points: If the number of points is less than this value, use the matmul implementation.

Returns:
    the computed Sampson distance with shape :math:`(*, N)`.

cudar   )devicer$   r&   rS   rA   )r   r	   r
   r   r   rT   
num_pointss          r@   sampson_epipolar_distancerY      sF    2 
yy~~ZZ^
6:4rTWXX24rCPPrB   c                 *   [        U[        5      (       d  [        S[        U5       35      e[	        UR
                  5      S:  d  UR
                  SS S:w  a  [        SUR
                   35      eU SSS2S4   nU SSS2S	4   nUSSS2S4   nUSSS2S	4   nU R
                  S
   S:X  a
  U SSS2S4   O
[        U5      n	UR
                  S
   S:X  a
  USSS2S4   O
[        U5      n
US   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nX-  X-  -   X-  -   nX-  X-  -   UU	-  -   nUU-  UU-  -   UU	-  -   nX-  X-  -   UU
-  -   nX-  X-  -   UU
-  -   nUU-  UU-  -   U
U-  -   R                  S5      nSUR                  S5      UR                  S5      -   U-   -  nSUR                  S5      UR                  S5      -   U-   -  nUU-   nUU-  nU(       a  U$ UU-   R                  5       $ )zdReturn symmetric epipolar distance for correspondences given the fundamental matrix (CPU-optimized).r   r   r   Nr   r   .r   r   r   r   r   r   r   r   r   r   r   r   r    r!         ?)
r"   r   r#   r$   r%   r&   r'   r   rL   r(   )r   r	   r
   r   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   inv1inv2den_invr?   s                                 r@   +_symmetrical_epipolar_distance_manual_impl_r_      s    b&!!=d2hZHIIBHHbhhrsmv5>rxxjIJJ 	S!QYAS!QYAS!QYAS!QYA !JJrNa/c1aiYq\B JJrNa/c1aiYq\B Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C
Y-	
"C 'CG
ch
&C
'CG
cBh
&C
'C!G
cBh
&C 'CG
cBh
&C
'CG
cBh
&C s7QWrCx'
,
,Q
/C #''!*swwqz)C/0D#''!*swwqz)C/0DTkG-C
#IrB   c                    U R                   S   S:X  a  [        U 5      n UR                   S   S:X  a  [        U5      nUR                  SSS9nX-  nX-  nX-  R                  SS9R	                  S5      nSUSS S24   R                  SSS9R	                  S5      U-   -  SUSS S24   R                  SSS9R	                  S5      U-   -  -   n	X-  n
U(       a  U
$ X-   R                  5       $ )Nr   r   r   rD   rG   r[   .rI   )r   r	   r
   r   r   rN   rO   rP   rQ   denominator_invr?   s              r@   +_symmetrical_epipolar_distance_matmul_impl_rb      s    zz"~,T2zz"~,T2,,BR,0CJJ*//B/7;;A>I!ZRaR%8%=%=aR%=%H%L%LQ%ORU%UVY\37   +//2S8Z O -C
IrB   c                     UR                   R                  S:X  a"  U R                  S   nUS:  a  [        XX#U5      $ [	        XX#U5      $ )a  Return symmetrical epipolar distance for correspondences given the fundamental matrix.

Args:
   pts1: correspondences from the left images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
         converted automatically.
   pts2: correspondences from the right images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
         converted automatically.
   Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to avoid ambiguity with torch.nn.functional.
   squared: if True (default), the squared distance is returned.
   eps: Small constant for safe sqrt.

Returns:
    the computed Symmetrical distance with shape :math:`(*, N)`.

rV   r   '  )rW   r$   r&   rb   r_   )r   r	   r
   r   r   rX   s         r@   symmetrical_epipolar_distancere      sI    $ 
yy~~ZZ^
>t2X[\\6t2PSTTrB   c                 F   [        U 5        [        U5        [        U5        [        UR                  5      S:  d  UR                  SS S:X  d  [        SUR                   35      eU R                  S   S:X  a  [	        U 5      n UR                  SSS9nX-  n[        X5      $ )	a  Return one-sided epipolar distance for correspondences given the fundamental matrix.

This method measures the distance from points in the right images to the epilines
of the corresponding points in the left images as they reflect in the right images.

Args:
   pts1: correspondences from the left images with shape
     :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
   pts2: correspondences from the right images with shape
     :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
   Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to
     avoid ambiguity with torch.nn.functional.

Returns:
    the computed Symmetrical distance with shape :math:`(*, N)`.

r   r   Nr   r   r   r   rD   )r   r%   r&   r'   r   rJ   r   )r   r	   r
   rN   rO   s        r@   left_to_right_epipolar_distancerg     s    $ 4 4 2BHH"((23-6"9>rxxjIJJzz"~,T2,,BR,0CJt00rB   c                 &   [        U 5        [        U5        [        U5        [        UR                  5      S:  d  UR                  SS S:X  d  [        SUR                   35      eUR                  S   S:X  a  [	        U5      nX-  n[        X5      $ )a  Return one-sided epipolar distance for correspondences given the fundamental matrix.

This method measures the distance from points in the left images to the epilines
of the corresponding points in the right images as they reflect in the left images.

Args:
   pts1: correspondences from the left images with shape
     :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
   pts2: correspondences from the right images with shape
     :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
   Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to
     avoid ambiguity with torch.nn.functional.

Returns:
    the computed Symmetrical distance with shape :math:`(*, N)`.

r   r   Nr   r   r   r   )r   r%   r&   r'   r   r   )r   r	   r
   rP   s       r@   right_to_left_epipolar_distanceri   2  s    $ 4 4 2BHH"((23-6"9>rxxjIJJzz"~,T2Jt00rB   )T:0yE>)Trj   rd   )__doc__torchr   r   kornia.core.checkr   kornia.geometry.conversionsr   kornia.geometry.linalgr   boolfloatrA   rS   jitscriptintrY   r_   rb   re   rg   ri    rB   r@   <module>rv      s  $ A  # 4 E 6 PT=
==$*=59=GL==B PT+
++$*+59+GL++\ 
 */Q
Q
Q 	Q 	Q
 
Q %(Q Q Q@ PT3
33$*3593GL33n PT
$*59GL0 PTU
UU$*U59UGLUU21& 1 1F 1v 1D1& 1 1F 1v 1rB   