
    3jL                        S r SSKrSSKJrJrJr  SSKrSSKJrJ	r	  SSK
JrJr  SSKJrJr  SSKJr  S.S\R$                  S	\S
\\R$                  \R$                  4   4S jjrS.S\R$                  S	\S
\R$                  4S jjrS\R$                  S
\R$                  4S jrS\R$                  4S jrS/S\R$                  S	\S
\R$                  4S jjr\R2                  R4                  S/S\R$                  S	\S
\R$                  4S jj5       r\R2                  R4                  S\R$                  S\R$                  S
\R$                  4S j5       r\R2                  R4                    S0S\R$                  S\R$                  S\\R$                     S\S
\R$                  4
S jj5       r  S1S\R$                  S\R$                  S\\R$                     S\S   S
\R$                  4
S jjrS\R$                  S\R$                  S
\R$                  4S  jr S!\R$                  S\R$                  S
\R$                  4S" jr!S#\R$                  S$\R$                  S%\R$                  S
\R$                  4S& jr"S'\R$                  S(\R$                  S)\R$                  S
\R$                  4S* jr#S+\R$                  S,\R$                  S
\R$                  4S- jr$g)2zKModule containing the functionalities for computing the Fundamental Matrix.    N)LiteralOptionalTuple)KORNIA_CHECK_SAME_SHAPEKORNIA_CHECK_SHAPE)_torch_svd_castsafe_inverse_with_mask)convert_points_from_homogeneousconvert_points_to_homogeneous)solve_cubicpointsepsreturnc                 *   U R                   S:w  a  [        U R                  5      eU R                  S   S:w  a  [        U R                  5      eU R                  u  p#nU R                  U R                  peU R                  SSS9nX-
  nUR                  SSS9R                  SS9n	[        R                  " S	5      X-   -  n
XR                  USS5      -  n[        R                  " USS4XVS
9nXS'   XS'   U
* US   -  US'   U
* US   -  US'   SUS'   X4$ )a  Normalize points (isotropic).

Computes the transformation matrix such that the two principal moments of the set of points
are equal to unity, forming an approximately symmetric circular cloud of points of radius 1
about the origin. Reference: Hartley/Zisserman 4.4.4 pag.107

This operation is an essential step before applying the DLT algorithm in order to consider
the result as optimal.

Args:
   points: Tensor containing the points to be normalized with shape :math:`(B, N, 2)`.
   eps: epsilon value to avoid numerical instabilities.

Returns:
   tuple containing the normalized points in the shape :math:`(B, N, 2)` and the transformation matrix
   in the shape :math:`(B, 3, 3)`.

         T)dimkeepdim)r   pr   g       @devicedtype).r   r   ).r   r   ).r   r   ).r   r   ).r   r         ?).r   r   )ndimAssertionErrorshaper   r   meannormmathsqrtviewtorchzeros)r   r   B_N_r   r   x_meancenteredmean_radiusscalepoints_norm	transforms                ^/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/epipolar/fundamental.pynormalize_pointsr1      s-   & {{aV\\**||B1V\\**||HA1MM6<<E [[Q[-FH --B!-,11b19K YYs^ 12E ZZ1a00K Q1IfBI i i!6F9$55Ii!6F9$55IiIi!!    Mc                     [        U R                  5      S:  a  [        U R                  5      eU SSS2SS24   n[        R                  " UR                  5       U:  XU-   -  U 5      $ )a  Normalize a given transformation matrix.

The function trakes the transformation matrix and normalize so that the value in
the last row and column is one.

Args:
    M: The transformation to be normalized of any shape with a minimum size of 2x2.
    eps: small value to avoid unstabilities during the backpropagation.

Returns:
    the normalized transformation matrix with same shape as the input.

r   .r   N)lenr   r   r%   whereabs)r3   r   norm_vals      r0   normalize_transformationr9   U   s\     177|aQWW%%sBC}-H;;x||~+QS.-A1EEr2   Ac                     U R                  SS5      nX-  n[        R                  R                  U5      u  p4USSS2SS24   nU$ )zCompute the nullspace of a matrix A using the eigh method.

Args:
    A: (..., 7, 9)

Returns:
    N: (..., 9, 2) where columns span the right nullspace of A
r   .Nr   )	transposer%   linalgeigh)r:   ATG_evalsevecsNs         r0   _nullspace_via_eighrE   i   sJ     
R	B
ALL%%a(MFc1bqbjAHr2   rD   c                 `    U S   R                  SSS5      nU S   R                  SSS5      nX4$ )zCompute the F1 and F2 matrices from the nullspace of a matrix A.

Args:
    N: (..., 9, 2) where columns span the right nullspace of A
Returns:
    F1: (..., 3, 3)
    F2: (..., 3, 3)
.r   r   r   ).r   )r$   )rD   F1F2s      r0   _F1F2_from_nullspacerJ   y   s7     
6Aq	!B	
6Aq	!B6Mr2   Fc                 J    U R                  SSSS9R                  U5      nX-  $ )zFrobenius-normalize each 3x3 (keeps cubic coefficients well-scaled).

Args:
    F: (..., 3, 3)
    eps: small value to avoid unstabilities.

Returns:
    F: (..., 3, 3)
)r<   r   r   T)r   r   r   )r!   	clamp_min)rK   r   nrms      r0   _normalize_FrO      s*     &&XD&
1
;
;C
@C7Nr2   xc                 (    U R                  5       U:*  $ )N)r7   )rP   r   s     r0   	_isclose0rR      s     557c>r2   points1points2c                    [        U / SQ5        [        U/ SQ5        U R                  S   nU R                  nU R                  n[	        U 5      u  pV[	        U5      u  px[
        R                  " USSS9u  p[
        R                  " USSS9u  p[
        R                  " U	5      n[
        R                  " X-  X-  XU	-  X-  XX/	SS9n[        [        U5      5      u  nn[        U5      n[        U5      n[
        R                  " US4X4S9n[
        R                  R                  U5      n[
        R                  R                  U5      n[        U5      u  nn[        U5      u  nnUUS	S	2S4'   [
        R                   " S
UU-  5      U-  US	S	2S4'   [
        R                   " S
UU-  5      U-  US	S	2S4'   UUS	S	2S4'   [#        U5      n[
        R$                  " USS9nUS:  US:  -  nUR'                  5       n[
        R                  " U5      nUS	S	2SS4   R)                  S5      nUS	S	2SS4   R)                  S5      nUU-  U-   n[+        US5      ) n[
        R,                  " USU-  U5      n [
        R,                  " UUU -  U5      n!U nU!nUR)                  S5      R/                  USSS5      n"UR)                  S5      R/                  USSS5      n#U"US	S	2S	S	2S	S	4   -  U#US	S	2S	S	2S	S	4   -  -   n$[
        R,                  " U[
        R                  " UUS9[
        R0                  " UUS95      n%U%U$S	S	2S	S	2SS4'   [
        R,                  " UR3                  USSS5      U$[
        R0                  " U$5      5      n$UR)                  S5      R5                  SS5      U$-  UR)                  S5      -  n$[7        U$5      $ )a  Compute the fundamental matrix using the 7-point algorithm.

The 7-point algorithm computes the fundamental matrix from exactly 7 point correspondences.
Unlike the 8-point algorithm, this method can return up to 3 possible fundamental matrices
as solutions to the rank-2 constraint, which is formulated as a cubic equation.

Reference: Hartley/Zisserman 11.1.2 pag.281

Args:
    points1: A set of 7 points in the first image with shape :math:`(B, 7, 2)`.
    points2: A set of 7 points in the second image with shape :math:`(B, 7, 2)`.

Returns:
    The computed fundamental matrices with shape :math:`(B, 3, 3, 3)`, containing up to 3
    candidate solutions per batch. Invalid solutions are zeroed out.

)r'   72r   r   r   r   chunksr      r   Nzbii->br   r   -q=r   )r   r<   )r   r   r   r   r1   r%   chunk	ones_likecatrJ   rE   rO   r&   r>   detr	   einsumr   count_nonzeroclone	unsqueezerR   r6   expand
zeros_liker$   r=   r9   )&rS   rT   r'   r   r   points1_norm
transform1points2_norm
transform2x1y1x2y2onesXf1f2coeffsf1_detf2_detinv_f1r)   inv_f2rootscnzvalid_root_mask_lambda_muf1_22f2_22_s_s_non_zero_maskmu_newlam_newf1_expandedf2_expandedfmatrixf22s&                                         r0   
run_7pointr      s   & w0w0aA^^FMME/8L/8L[[2a8FB[[2a8FB??2D 			27BGRb"'22LRTUA ""5a"89FB	b	B	b	B [[!Q<F\\b!F\\b!F&r*IFA&r*IFAF1a4L<<"v+6?F1a4L<<"v+6?F1a4LF1a4LE 

e
+CQw37+O kkmG
//'
"C q!QwK!!!$Eq!QwK!!!$E		B ""e,, [[)38S9Fkk*Gf,<gFG
CG ,,q/((Aq!4K,,q/((Aq!4KGAq$$455c!QPTVZJZF[8[[G
 ++&%(H%JZJZ[]ejJk
lCGAq!QJ kk/..q!Q:GUEUEUV]E^_G ##A&00R87BjFZFZ[\F]]G#G,,r2   weightsuse_einsum_at_more_than_pointsc                 P   [        U / SQ5        [        U/ SQ5        [        X5        U R                  S   S:  a  [        U R                  5      eUbC  [        USS/5        UR                  S   U R                  S   :w  a  [        UR                  5      e[	        U 5      u  pE[	        U5      u  pg[
        R                  " USSS	9u  p[
        R                  " USSS	9u  p[
        R                  " U5      n[
        R                  " X-  X-  XU-  X-  XX/	SS
9R                  S5      nUR                  u  pnUcA  X:  a$  UR                  SS5      R                  5       U-  nO[
        R                  " SX5      nOtUR                  S5      nX:  aF  UUR                  S5      R                  5       -  nUR                  SS5      R                  5       U-  nO[
        R                  " SXU5      n[
        R                   R#                  U5      u  nnUS   nUR%                  USS5      n['        U5      u  nnn[
        R(                  " U5      nUSSS24   USSS24'   U[
        R*                  " U5      -  UR,                  -  nUR                  SS5      UU-  -  n[/        U5      $ )a>  Compute the fundamental matrix using (weighted) 8-point DLT, optimized.

Args:
    points1: (B, N, 2), N >= 8
    points2: (B, N, 2), N >= 8
    weights: optional (B, N) nonnegative weights
    use_einsum_at_more_than_points: threshold for using einsum vs GEMM for large N

Returns:
    (B, 3, 3) fundamental matrices
)r'   rD   rW   r      Nr'   rD   r   r   rX   r   r<   zbni,bnj->bijr   zbni,bnj,bn->bijrG   r   .)r   r   r   r   r1   r%   r\   r]   r^   squeezer=   
contiguousr`   rM   rc   r#   r>   r?   r$   r   re   
diag_embedmHr9   )rS   rT   r   r   pts1nT1pts2nT2rj   rk   rl   rm   rn   r:   r'   rD   r)   r3   wAwrB   rC   hF_hatUSVS_newF_rank2rK   s                                 r0   
run_8pointr     sd   $ w0w0G-}}Q!W]]++7S#J/==w}}Q// // !)IE )IE[[Bq1FB[[Bq1FB??2D 			27BGRb"'22LRTU]]^`aAggGA! -B#..014A ^Q2Aa -Q[[_))++BR$//1B6A .a8ALL%%a(MFEfAFF1aOE e$GAq!QESbSkE#ss(O%""5))ADD0G
R"-A#A&&r2   method)8POINT7POINTc                     UR                  5       S:X  a  [        X5      nU$ UR                  5       S:X  a  [        XU5      nU$ [        SU S35      e)af  Find the fundamental matrix.

Args:
    points1: A set of points in the first image with a tensor shape :math:`(B, N, 2), N>=8`.
    points2: A set of points in the second image with a tensor shape :math:`(B, N, 2), N>=8`.
    weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
    method: The method to use for computing the fundamental matrix. Supported methods are "7POINT" and "8POINT".

Returns:
    the computed fundamental matrix with shape :math:`(B, 3*m, 3)`, where `m` number of fundamental matrix.

Raises:
    ValueError: If an invalid method is provided.

r   r   zInvalid method: z.. Supported methods are '7POINT' and '8POINT'.)upperr   r   
ValueError)rS   rT   r   r   results        r0   find_fundamentalr   M  s^    * ||~!G-
 M	 
8	#Gg6 M +F83abccr2   F_matc                 6   [        U / SQ5        U R                  S   S:X  a  [        U 5      nO+U R                  S   S:X  a  U nO[        U R                  5      e[        U/ SQ5        [        R
                  " USSS9n[        R                  " X-  SSS9u  p4nX3-  XD-  -   n[        R                  " US	:  S
[        R                  " U5      -  [        R                  " U5      5      n[        R                  " X6-  XF-  XV-  /SS9n[        R
                  " USSS9$ )a  Compute the corresponding epipolar line for a given set of points.

Args:
    points: tensor containing the set of points to project in the shape of :math:`(*, N, 2)` or :math:`(*, N, 3)`.
    F_mat: the fundamental to use for projection the points in the shape of :math:`(*, 3, 3)`.

Returns:
    a tensor with shape :math:`(*, N, 3)` containing a vector of the epipolar
    lines corresponding to the points to the other image. Each line is described as
    :math:`ax + by + c = 0` and encoding the vectors as :math:`(a, b, c)`.

)*rD   DIMr   r   r   r   3r   r<   )dim0dim1rX   g        r   r   )r   r   r   r   r%   r=   r\   r6   r#   r]   r^   )r   r   points_habcnulines           r0   compute_correspond_epilinesr   k  s     v01||B1!>v!F	b	Q	V\\**uo.xbr:Hkk%*1=GA! uqu}B	R#XsUZZ^3U__R5H	IB99afafaf-26D??4br22r2   linesc                    [        U / SQ5        [        U/ SQ5        UR                  S   S:X  a  [        U5      nO+UR                  S   S:X  a  UnO[        UR                  5      eU [        R
                  " / SQU R                  U R                  S9R                  SSS5      -  n[        R                  R                  X#SS9nU$ )	a  Compute the perpendicular to a line, through the point.

Args:
    lines: tensor containing the set of lines :math:`(*, N, 3)`.
    points:  tensor containing the set of points :math:`(*, N, 2)`.

Returns:
    a tensor with shape :math:`(*, N, 3)` containing a vector of the epipolar
    perpendicular lines. Each line is described as
    :math:`ax + by + c = 0` and encoding the vectors as :math:`(a, b, c)`.

)r   rD   r   )r   rD   twor   r   )r   r   r   )r   r   r   r   )r   r   r   r   r%   tensorr   r   r$   r>   cross)r   r   r   infinity_pointperps        r0   get_perpendicularr     s     uo.v01||A!!>v!F	aA	V\\**U\\)5;;u||\aabcefhijjN++H!+LDKr2   pts1pts2Fmc                    [        U[        R                  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 R                  S   S:X  a  [        U 5      n UR                  S   S:X  a  [        U5      n[        X5      n[        X15      n[        [        R                  R                  X4SS	95      nU$ )
a4  Return closest point on the epipolar line to the correspondence, 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.

Returns:
    point on epipolar line :math:`(*, N, 2)`.

z#Fm type is not a torch.Tensor. Got r   r<   N)r   r   z#Fm must be a (*, 3, 3) tensor. Got r   r   r   )
isinstancer%   Tensor	TypeErrortyper5   r   r   r   r   r   r
   r>   r   )r   r   r   line1in2r   points1_in_2s         r0   "get_closest_point_on_epipolar_liner     s     b%,,''=d2hZHIIBHH"((23-6"9>rxxjIJJzz"~,T2zz"~,T2*44HX,D25<<3E3EhZ[3E3\]Lr2   E_matK1K2c                 p   [        U / SQ5        [        U/ SQ5        [        U/ SQ5        [        U R                  SS 5      [        UR                  SS 5      s=:X  a"  [        UR                  SS 5      :X  d   [        e  [        e[	        U5      S   R                  SS5      U -  [	        U5      S   -  $ )a  Get the Fundamental matrix from Essential and camera matrices.

Uses the method from Hartley/Zisserman 9.6 pag 257 (formula 9.12).

Args:
    E_mat: The essential matrix with shape of :math:`(*, 3, 3)`.
    K1: The camera matrix from first camera with shape :math:`(*, 3, 3)`.
    K2: The camera matrix from second camera with shape :math:`(*, 3, 3)`.

Returns:
    The fundamental matrix with shape :math:`(*, 3, 3)`.

r   Nr<   r   r   )r   r5   r   r   r	   r=   )r   r   r   s      r0   fundamental_from_essentialr     s     uo.r?+r?+u{{3B C"$6L#bhhsm:LL M"2&q)44R<uDH^_aHbcdHeffr2   P1P2c                    [        U / SQ5        [        U/ SQ5        U R                  SS UR                  SS :w  a  [        eS[        R                  S[        R                  S[        R                  4S jnU R
                  nU[        R                  [        R                  4;  a>  U R                  [        R                  5      n UR                  [        R                  5      nU SS	S2SS24   nU" U SS
S2SS24   U SSS	2SS24   5      nU SSS
2SS24   nUSS	S2SS24   nU" USS
S2SS24   USSS	2SS24   5      nUSSS
2SS24   n	U" XG5      U" XW5      U" Xg5      pn
U" XH5      U" XX5      U" Xh5      pnU" XI5      U" XY5      U" Xi5      nnn[        R                  " U
R                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      UR                  5       R                  SS	5      /	S	S9nUR                  " / U R                  SS QSPSP76 R                  U5      $ )a  Get the Fundamental matrix from Projection matrices.

Args:
    P1: The projection matrix from first camera with shape :math:`(*, 3, 4)`.
    P2: The projection matrix from second camera with shape :math:`(*, 3, 4)`.

Returns:
     The fundamental matrix with shape :math:`(*, 3, 3)`.
)r   r   4Nr<   rP   yr   c                 .    [         R                  " X/SS9$ )Nr<   r   )r%   r^   )rP   r   s     r0   vstack,fundamental_from_projections.<locals>.vstack  s    yy!R((r2   .r   r   r   r   r   r   )r   r   r   r%   r   r   float32float64tor^   r_   reshaper$   )r   r   r   input_dtypeX1X2X3Y1Y2Y3X1Y1X2Y1X3Y1X1Y2X2Y2X3Y2X1Y3X2Y3X3Y3F_vecs                       r0   fundamental_from_projectionsr     s    r?+r?+	xx}"%)%,, )5<< )ELL ) ((K5==%--88UU5==!UU5==!	CQJB	3!Q;C1aK	1B	C!QJB	CQJB	3!Q;C1aK	1B	C!QJBb~vb~vb~Db~vb~vb~Db~vb~vb~$DIIHHJr1%HHJr1%HHJr1%HHJr1%HHJr1%HHJr1%HHJr1%HHJr1%HHJr1%
	
 E ::+rxx}+a++..{;;r2   )g:0yE>)r[   )Ni   )Nr   )%__doc__r"   typingr   r   r   r%   kornia.core.checkr   r   kornia.core.utilsr   r	   kornia.geometry.conversionsr
   r   kornia.geometry.solversr   r   floatr1   r9   rE   rJ   rO   jitscriptrR   r   intr   r   r   r   r   r   r    r2   r0   <module>r      s  $ R  + +  I E f /3"U\\ 3" 3"u||UZUaUaGaAb 3"lF F5 FELL F(5<< ELL  ELL ELL u     E ell  
 b- b-u|| b- b- b-J  '+*-	F'\\F'\\F' ell#F' %(	F'
 \\F' F'X '+*2	\\\\ ell# &'	
 \\<3 3U\\ 3ell 3BU\\ 5<< ELL 4U\\  SXS_S_ didpdp :gell g g%,, g[`[g[g g82<U\\ 2<u|| 2< 2<r2   