
    3jRm                        S r SSKJrJr  SSKrSSKJrJrJr  SSK	J
r
Jr  SSKJr  SSKJrJrJrJrJr  SS	KJrJr  SS
KJrJr  SSKJr  / SQrSLS\R:                  S\R:                  S\\R:                     S\R:                  4S jjr\R>                  R@                  S\R:                  S\R:                  S\R:                  S\R:                  4S j5       r!\R>                  R@                  S\R:                  S\R:                  S\R:                  S\R:                  4S j5       r"\R>                  R@                  S\R:                  S\R:                  S\R:                  S\R:                  S\R:                  4
S j5       r#\R>                  R@                  SMS\R:                  S\R:                  S\$S\\R:                  \R:                  4   4S jj5       r%SNS\R:                  S \&S!\&S"\&S\R:                  4
S# jjr'\R>                  R@                  S$\R:                  S%\&S\R:                  S\R:                  S\R:                  S\R:                  S\R:                  S&\R:                  S'\R:                  S(\R:                  S)\R:                  S*\R:                  S+\R:                  S\R:                  4S, j5       r(\RR                  " / S-Q/ S.Q/ S/Q/\RT                  S09r+\RR                  " / S1Q\RT                  S09r,\RR                  " / S2Q\RT                  S09r-\RR                  " / S3Q\RT                  S09r.\RR                  " / S4Q\RT                  S09r/\RR                  " / S5Q\RT                  S09r0SNS6\R:                  S \&S!\&S"\&S\R:                  4
S7 jjr1S$\R:                  S%\&S\R:                  4S8 jr2S9\R:                  S:\R:                  S;\R:                  S\R:                  4S< jr3S=\R:                  S\\R:                  \R:                  \R:                  4   4S> jr4S=\R:                  S\\R:                  \R:                  \R:                  4   4S? jr5S@\R:                  SA\R:                  SB\R:                  SC\R:                  S\R:                  4
SD jr6S=\R:                  S\\R:                  \R:                  4   4SE jr7 SLS=\R:                  S:\R:                  S;\R:                  SF\R:                  SG\R:                  SH\\R:                     S\\R:                  \R:                  \R:                  4   4SI jjr8S@\R:                  SA\R:                  SB\R:                  SC\R:                  S\\R:                  \R:                  4   4
SJ jr9 SLS\R:                  S\R:                  S\\R:                     S\R:                  4SK jjr:g)Oz;Module containing functionalities for the Essential matrix.    )OptionalTupleN)KORNIA_CHECKKORNIA_CHECK_SAME_SHAPEKORNIA_CHECK_SHAPE)eye_likevec_like)_torch_svd_cast)T_deg1T_deg2coefficient_mapmultiplication_indicessigns   )cross_product_matrixmatrix_cofactor_tensor)depth_from_pointprojection_from_KRt)triangulate_points)decompose_essential_matrix!decompose_essential_matrix_no_svdessential_from_Rtessential_from_fundamentalfind_essentialmotion_from_essential%motion_from_essential_choose_solutionrelative_camera_motionpoints1points2weightsreturnc                    [        U / SQ5        [        X5        [        U R                  S   S:  S5        U R                  u  n  nU SSS24   U SSS24   peUSSS24   USSS24   p[        R
                  " U5      n	[        R                  " XW-  XX-  XVU-  Xh-  XgX/	SS	9n
[        X5      n[        R                  " U5      R                  S
S	9R                  SS	9nUR                  5       (       aw  [        R                  " SUR                  UR                  S9R                  SSSS5      R                  USSS5      n[        R                   " UR                  USSS5      X5      nU$ )aI  Compute the essential matrix using the 5-point algorithm from Nister.

The linear system is solved by Nister's 5-point algorithm [@nister2004efficient],
and the solver implemented referred to [@barath2020magsac++][@wei2023generalized][@wang2023vggsfm].

Args:
    points1: A set of carlibrated 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: Not used, kept for compatibility.

Returns:
    the computed essential matrix with shape :math:`(B, 10, 3, 3)`.

)BN2r      zNumber of points should be >=5.r      dim)r(      devicedtype
   )r   r   r   shapetorch	ones_likecatnull_to_Nister_solutionisnanallanyeyer.   r/   viewexpandwhere)r   r   r    
batch_size_x1y1x2y2onesXEbadeye3s                 \/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/epipolar/essential.py
run_5pointrI   -   s[    w0G-q!Q&(HI}}J1S!A#XQqS 1S!A#XQqS 1??2D
 			27BGRb"'22LRTUA.A
++a.



*
.
.2
.
6C
wwyyyy188177;@@Aq!LSST^`bdeghiKKQ15t?H    abr   c                 n    U R                  S5      UR                  S5      -  nUR                  SS9nXB-  $ Nr'   r   r+   )	start_dim	unsqueezeflatten)rK   rL   r   product_basisproduct_vectors        rH   _multiply_deg_one_polyrU   R   :     KKNQ[[^3M"**R*8N""rJ   r   c                 n    U R                  S5      UR                  S5      -  nUR                  SS9nXB-  $ rN   rP   )rK   rL   r   rS   rT   s        rH   _multiply_deg_two_one_polyrX   Z   rV   rJ   Ar   r   r   c                 J   U R                   S   nU R                  US5      nUS S 2U4   n[        R                  " USS9nXr-  n[        R                  " USU R
                  U R                  S9n	UR                  " S5      R                  US5      n
U	R                  SX5        U	$ )Nr   r(   r)      r-   r   )
r1   r:   r2   prodzerosr.   r/   rQ   r;   scatter_add_)rY   r   r   r   r#   A_flatgathered_valuesproductssigned_productscsbatch_coefficient_maps              rH   _determinant_to_polynomial_jitre   b   s     	

AVVAr]FQ 667Ozz/r2H&O	Q188177	;B+55a8??2FOOA,>IrJ   epsc                 >   U S   nU S   nU S   nU S   nX6-  XE-  -
  nUR                  5       nX:*  [        R                  " U5      -  [        R                  " U5      -  n	[        R                  " X:  U[        R
                  " U5      U-  5      n
SU
-  nXk-  nU* U-  nU* U-  nX;-  nXS   -  XS   -  -   nXS   -  XS   -  -   n[        R                  " UU4SS9R                  S5      nX3-  XU-  -   nX4-  XV-  -   nXD-  Xf-  -   nX1S   -  XQS   -  -   nXAS   -  XaS   -  -   nUU-   nUS-  R                  U5      nUU-   nUnUnUU-   nUU-  UU-  -
  nUR                  5       R                  U5      nSU-  n UU -  n!U* U -  n"U* U -  n#UU -  n$U!U-  U"U-  -   n%U#U-  U$U-  -   n&[        R                  " U%U&4SS9R                  S5      n'[        R                  " U	R                  S5      R                  S5      U'U5      n([        R                  " U(5      R                  S	S9[        R                  " U(5      R                  S	S9-  n)U	U)-  n	[        R                  " U	R                  S5      R                  S5      [        R                  " U(5      U(5      n(U(U	4$ )
u  Solve (A)x=b for A (...,2,2), b (...,2,1) using Tikhonov regularization.

Uses the following methods:
  - direct inverse when det is OK
  - otherwise solve normal equations (A^T A + λI)x = A^T b  (λ from trace scale)
Never throws. Returns (x, bad) where bad marks ill-conditioned A.
).r   r   ).r   r   ).r   r   ).r   r         ?r(   r)   :0yE>r+   r(   )absr2   r6   isinfr<   r3   stackrQ   	clamp_minr8   
zeros_like)*rY   rL   rf   rK   bbcddetdet_absrF   det_safeinv_detinv00inv01inv10inv11x0_dirx1_dirx_dirata00ata01ata11atb0atb1trlamm00m01m10m11det_m
det_m_safe	inv_det_minvm00invm01invm10invm11x0_fbx1_fbx_fbx	nonfinites*                                             rH   _solve_2x2_tikhonov_safer   w   s    	
)A	
9B	)A	)A
%"&.CggiG>U[[1
1EKK4H
HC {{7=#us/Cc/IJHHnGKESGOER7NEKEy\!EiL$88Fy\!EiL$88FKK(b1;;B?E EAEMEFQUNEGaeOE|aI,..D)qY<//D 
B9


$C
#+C
C
C
#+C#Ic	!E&&s+Jj I9_FdiFdiF9_FTMFTM)ETMFTM)E;;u~2.88<D 	CMM"%//3T5AA A""x"05;;q>3E3E(3E3SSI
	/CCMM"%//3U5E5Ea5H!LAc6MrJ   matijratioc                     U S S 2X2-  U-   4   $ N )r   r   r   r   s       rH   _fun_selectr      s    q%)a-  rJ   rD   r=   idx_iji_idxj_idxidx3top_idxbot_idxc           	      >   U R                   n[        U 5      u    pUS S 2S S 2SS 24   R                  5       nUR                  SS5      S S 2SS 2S S 24   nUnU R                  nU R                   n[
        R                  " USSUUS9nUR                  USSS5      R                  S	S
5      R                  5       nUS S 2SSS S 24   nUS S 2SS	S S 24   nUS S 2SS
S S 24   nUS S 2S	SS S 24   nUS S 2S	S	S S 24   nUS S 2S	S
S S 24   nUS S 2S
SS S 24   nUS S 2S
S	S S 24   nUS S 2S
S
S S 24   n[        UUU5      n [        UUU5      n![        UUU5      n"[        UUU5      n#[        UUU5      n$[        UUU5      n%[        U U!-
  UU5      [        U"U#-
  UU5      -   [        U$U%-
  UU5      -   US S 2S4'   US S 2US S 2S S 24   n&US S 2U	S S 2S S 24   n'[        U&R                  SS5      U'R                  SS5      U5      R                  USSS5      n(U(R                  S
S9R                  USSS5      R                  5       n)U)S S 2XS S 24   n*SU*R                  S	SS9-  n+U)S S 2XS S 24   U+-
  U)S S 2XS S 24'   U)S S 2US S 2S S 24   n,US S 2S S 2U	S S 24   R                  SS
S	S5      R                  5       n-[        U,R                  SS5      U-R                  SS5      U5      R                  USSS5      n.U.R                  S
S9US S 2S S2S S 24'   US S 2S S 2S S24   n/US S 2S S 2SS 24   n0[
        R                  " SUUS9R                  S5      R                  USS5      n1[
        R                   R#                  U/U05      n2[
        R$                  " U25      R'                  SS9[
        R(                  " U25      R'                  SS9-  n3U3R'                  5       (       a  [
        R*                  " U/SSS9R-                  5       R/                  SS9n4U4S-  S-   R1                  U5      n5U/U1U5R                  US	S	5      -  -   n6[
        R                   R#                  U6U05      n7[
        R2                  " U3R                  US	S	5      U7U25      n2[
        R4                  " U/U24SS9n8[
        R                  " USSUUS9n9U8S S 2USS24   U9S S 2S S 2S	S24'   U9S S 2S S 2SS24   U8S S 2USS24   -
  U9S S 2S S 2SS24'   U8S S 2USS24   U9S S 2S S 2SS24'   U9S S 2S S 2SS24   U8S S 2USS24   -
  U9S S 2S S 2SS24'   U8S S 2USS24   U9S S 2S S 2SS24'   U9S S 2S S 2SS24   U8S S 2USS24   -
  U9S S 2S S 2SS24'   [7        U9XEU5      n:[
        R                  " USSUUS9n;[
        R                  " SUUS9U;S S 2SS2S	S24'   U:S S 2S4   R9                  S5      n<U:S S 2S S24   * U<R                  S5      -  U;S S 2SS S 24'   [
        R                   R;                  U;5      n=[
        R<                  " U=5      n>[
        R,                  " [
        R>                  " U=5      5      S:  n?U>R                  S	5      n@[
        R@                  " U9S S 2S S2S S	24   U@S-  -  U9S S 2S S2S	S
24   U@RC                  5       -  -   U9S S 2S S2S
S24   U@-  -   U9S S 2S S2SS24   -   U9S S 2S S2SS24   U@S-  -  U9S S 2S S2SS24   U@RC                  5       -  -   U9S S 2S S2SS24   U@-  -   U9S S 2S S2SS24   -   4S	S9R                  S	S5      nAU9S S 2S S2SS24   U@S-  -  U9S S 2S S2SS24   U@S-  -  -   U9S S 2S S2SS24   U@RC                  5       -  -   U9S S 2S S2SS24   U@-  -   U9S S 2S S2SS24   -   R                  S	S
5      R                  S5      nBUAS S 2S S 2SS
2SS
24   nCUBS S 2S S 2SS
2S S 24   nD[E        UCUDS5      u  nEnFUERG                  S5      nGUGS S 2S S 2S4   * nHUGS S 2S S 2S	4   * nIU>nJUS S 2SS S 24   R                  S	5      nKUS S 2S	S S 24   R                  S	5      nLUS S 2S
S S 24   R                  S	5      nMUS S 2SS S 24   R                  S	5      nNUHR                  S5      UK-  UIR                  S5      UL-  -   UJR                  S5      UM-  -   UN-   nO[
        RH                  " UHUH-  UIUI-  -   UJUJ-  -   S-   5      nPUOUPR                  S5      -  nOUOR                  USSS5      R                  SS5      nQUFR'                  5       (       a  [
        RJ                  WQWF'   U?RM                  5       R'                  5       (       a  [
        RJ                  WQU?) '   WQR1                  US9$ ) Nr(   r+   r0      r-   r,      r   r'   r   	   r)         ?Tr*   keepdimrj   dim1dim2ri         r&            g|=   r[   -q=rh   r/   )'r/   r
   
contiguous	transposer.   r2   r]   r:   rU   rX   reshapesumpermuter9   rQ   r;   linalgsolver6   r8   rl   diagonalrk   meantor<   r4   re   rn   eigvalsrealimagrm   squarer   squeezersqrtnanlogical_not)RrD   r=   r   r   r   r   r   r   r   r   r   r   r   original_dtyper>   Vnull_	nullSpacer#   r.   r/   coeffsnull_ijn00n01n02n10n11n12n20n21n22p01_12p02_11p02_10p00_12p00_11p01_10a_datab_dataprodsD_blocksdiagtD_for_i
Null_for_jprods2A10b_polyeye10
eliminatedrF   diagAr   A_dampedeliminated_dcoeffs_rY   rc   Ccs_de	roots_eigrootsis_realroots_unsquBsbs_vecA2b2xzsbad2xzs_sqr   yzN0N1N2N3Es_vecinv_normEssR                                                                                     rH   _null_to_Nister_solution_scriptr     s
     WWNa GAqaBCiL##%EB#ArsAI.IAXXFGGE[[B6?F jjAq!$..q!4??AG !Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C
!Q1*
C $Cf5F#Cf5F#Cf5F#Cf5F#Cf5F#Cf5F 	#6F?C@
$Vf_c6
B	C
$Vf_c6
B	C 1a4L Qq!^$FQq!^$F"6>>"a#8&..Q:OQWX]]^_abdegijEyyQy$$Q1b1<<>H At1$%Ddhh1dh++A!)!T*:!;a!?HQA q%A~&GAua(00Aq!<GGIJ'B(?ASASTVXYAZ\bchhijlmoprtuFzzaz(F1bqb!8 Ass
CAq"#IF IIbu5??BII!RQSTE ##C0J ++j
!
%
%(
%
3ekk*6M6Q6QV^6Q6_
_C
wwyy s"599;@@R@Ht|d"&&u-!Q!222||))(F;[[!Q!2L*M
iij)r2G 	Aq"V59A1gr"u,-AaAaCiLQ1Q3Y<'!Wbe*;"<<AaAaCiL1gr"u,-AaAaCiLQ1Q3Y<'!Wbe*;"<<AaAaCiLAw2-.AaAbDjMaAbDjMGAw2,=$>>AaAbDjM 
(+A/	ZBAr2fE:Aii&>Aa1adlOq"uI%Ea"f:+ 33AaQhK$$Q'IJJy!Eii

9-.6G//!$K	a!RaRiLKN+2A2qs
mk002232A2qs
mk)* 2A2qs
m a!QqSjM[!^,2A2qs
mk002232A2qs
mk)* 2A2qs
m		
 
 i2   a!QqSjM[!^,2A2qtnQ/02A2r"uo 2 2 445 2A2r"uo+, 2A2r"uo	 
1a	2  
Aq!A#qsN	B	1ac1	B(R7IC [[_F	1aA	1aAA	1a7		%	%a	(B	1a7		%	%a	(B	1a7		%	%a	(B	1a7		%	%a	(B[[_r!AKKOb$881;;r?R;OORTTF{{1q51q5=1q50367Hh((,,F	QAq	!	+	+B	3Bxxzz994  ""yyG855~5&&rJ   )r   r,   r   )r   r   r   )r'   r&   r   r   )	r   r   r   r   r   r   r'   r'   r'   )	r   r   r'   r   r   r'   r   r   r'   )r   r   r'   )r   r   r   )r&   r   r   null_matc                     U S S 2X2-  U-   4   $ r   r   )r  r   r   r   s       rH   
fun_selectr    s    Auy1}$%%rJ   c                    U R                   nU R                  n[        R                  " X#S9n[        R                  " X#S9n[
        R                  " US9n[        R                  " X#S9n[        R                  " US9n[        R                  US9n	[        R                  US9n
[        R                  US9n[        R                  US9n[        R                  US9n[        R                  US9n[        U UUUUUUUU	U
UUU5      $ )Nr-   )r.   )r.   r/   r   r   r   r   r   r   I_IDXJ_IDXIDX3TOP_IDXBOT_IDXIDX_IJr  )rD   r=   r.   dtype_internalT1T2mult_idxsgns	coeff_map	i_idx_dev	j_idx_devidx3_devtop_idx_devbot_idx_dev
idx_ij_devs                  rH   r5   r5     s    XXFWWN	&	7B	&	7B%((7H8868D""&1I'I'Iwwfw%H**F*+K**F*+K&)J*	

 rJ   F_matK1K2c                     [        U / SQ5        [        U/ SQ5        [        U/ SQ5        UR                  SS5      U -  U-  $ )a  Get Essential matrix from Fundamental and Camera matrices.

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

Args:
    F_mat: The fundamental 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 essential matrix with shape :math:`(*, 3, 3)`.

*3r"  r+   r(   r   r   )r  r  r  s      rH   r   r     s=     uo.r?+r?+<<B%'",,rJ   E_matc                 z   [        U / SQ5        [        U 5      u  pnUR                  SS5      n[        R                  " U 5      nUSSS24==   S-  ss'   UR                  SS5      n[        R
                  " [        R                  " U5      S:  S   X-  U5      n[        R
                  " [        R                  " U5      S:  S   XF-  U5      n[        [        R                  " / S	Q/5      R                  U 5      5      nUS
==   S-  ss'   X-  U-  nXR                  SS5      -  U-  n	Un
U	nUSSS24   nXU4$ )a  Decompose an essential matrix to possible rotations and translation.

This function decomposes the essential matrix E using svd decomposition [96]
and give the possible solutions: :math:`R1, R2, t`.

Args:
   E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.

Returns:
   A tuple containing the first and second possible rotation matrices and the translation vector.
   The shape of the tensors with be same input :math:`[(*, 3, 3), (*, 3, 3), (*, 3, 1)]`.

r   r+   r(   .Ng              ).NN)r&  r&  rh   ).r'   r'   rh   )
r   r
   r   r2   r3   r<   rs   r   tensortype_as)r$  Ur>   r   VtmaskmasktWU_W_VtU_Wt_VtR1R2Ts                rH   r   r     s'    uo. e$GA!	
R	B??5!DbcNdNNN2r"E 	UYYq\C'918QGA	eiimc)?;RZ	LBU\\?*;<DDUKLAiLCL URZF++b"%%*G 
B	B	#rs(AA;rJ   c           
         [        U / SQ5        [        U R                  5      S:w  a  U R                  SSS5      n U R                  S   nU S   U S   U S   pCn[        R
                  " S[        R                  " X R                  SS	5      -  SS	S
9R                  S5      -  5      n[        R                  " [        R                  R                  X#SS9[        R                  R                  X4SS9[        R                  R                  XBSS9/SS9n[        R                  " USSS9n[        R                  " US	S9nUSS2SS4   U-  U-  n	UR                  S5      R                  SSU	R!                  S5      5      n
[        R"                  " U	SU
S9R%                  S5      nU[        R                  " USSS9-  n[        R&                  " USS4U R(                  U R*                  S9nUSS2S4   USS2S4   USS2S4   npU* UsUSS2SS4'   USS2SS4'   X* sUSS2SS4'   USS2SS4'   U* UsUSS2SS4'   USS2SS4'   U* nU* n[-        U 5      X-  -
  X-  R                  5       R                  S5      -  n[-        U 5      UU -  -
  UU-  R                  5       R                  S5      -  nUUUR                  S5      4$ )a  Decompose the essential matrix to rotation and translation.

   Recover rotation and translation from essential matrices without SVD
  reference: Horn, Berthold KP. Recovering baseline and orientation from essential matrix[J].
  J. Opt. Soc. Am, 1990, 110.

Args:
   E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.

Returns:
   A tuple containing the first and second possible rotation matrices and the translation vector.
   The shape of the tensors with be same input :math:`[(*, 3, 3), (*, 3, 3), (*, 3, 1)]`.

r   r,   r(   r   .r   ).r   ).r'   r   r+   r   r)   r   Tr   N)r*   indexr-   r'   )r   lenr1   r:   r2   sqrtr   r   r   rm   r   crossnormargmaxrQ   r;   sizegatherr   r]   r.   r/   r   )r$  r#   e1e2e3scale_factorcross_productsnormslargeste_cross_productsindex_expandedb1b1_B1t0t1t2B2r   r0  r1  s                        rH   r   r     s    uo.
5;;1

2q!$AA vfuV}BB ::cENN5??2r;R3RY[bd$e$i$ijl$mmnL [[			B		+U\\-?-?B-?-OQVQ]Q]QcQcdfprQcQstN JJ~2t<E ll5b)G $AtTM2^CeK &&r*11"b:J:O:OPR:STN 
&A^	D	L	LQ	OB
uzz""d3
3C 
aAYu||5;;	GBAqD2ad8R1XB "sBBq!QwKAq!G!3Bq!QwKAq!G "sBBq!QwKAq!G B
B !
'"*
48Q8QRT8U	UB
 
'"u*
4b8Q8QRT8U	UBCMM"%&&rJ   r0  rJ  r1  rK  c                     [        U / SQ5        [        U/ SQ5        [        U/ SQ5        [        U/ SQ5        [        XX#5      u  pE[        US   5      nXd-  $ )a  Get the Essential matrix from Camera motion (Rs and ts).

Reference: Hartley/Zisserman 9.6 pag 257 (formula 9.12)

Args:
    R1: The first camera rotation matrix with shape :math:`(*, 3, 3)`.
    t1: The first camera translation vector with shape :math:`(*, 3, 1)`.
    R2: The second camera rotation matrix with shape :math:`(*, 3, 3)`.
    t2: The second camera translation vector with shape :math:`(*, 3, 1)`.

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

r   r!  r"  1r4  )r   r   r   )r0  rJ  r1  rK  Rr   Txs          rH   r   r   /  sS     r?+r?+r?+r?+ """1DA 
ai	(B6MrJ   c                     [        U / SQ5        [        U 5      u  pn[        R                  " XX"/SS9n[        R                  " X3* X3* /SS9nXE4$ )a  Get Motion (R's and t's ) from Essential matrix.

Computes and return four possible poses exist for the decomposition of the Essential
matrix. The possible solutions are :math:`[R1,t], [R1,-t], [R2,t], [R2,-t]`.

Args:
    E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.

Returns:
    The rotation and translation containing the four possible combination for the retrieved motion.
    The tuple is as following :math:`[(*, 4, 3, 3), (*, 4, 3, 1)]`.

r   r)   )r   r   r2   rm   )r$  r0  r1  r   RsTss         rH   r   r   L  sT     uo. +51IBA 
bb%2	.B	aQ^	,B6MrJ   r?   rA   r+  c                 x   [        U / SQ5        [        U/ SQ5        [        U/ SQ5        [        U/ SQ5        [        U/ SQ5        [        [        U R                  SS 5      [        UR                  SS 5      s=:H  =(       a    [        UR                  SS 5      :H  Os  5        Ub3  [        USS/5        [        UR                  UR                  SS :H  5        [        U R                  5      S:H  nU(       a!  U S   n US   nUS   nUS   nUS   nUb  US   n[	        U 5      u  px[        S	U 5      n	[        S	U 5      n
U	SS2S4   R                  SS
SS5      n	U
SS2S4   R                  SS
SS5      n
USS2S4   R                  SS
SS5      n[        XU
5      nUnUnUSS2S4   R                  SS
SS5      n[        X,U5      nUSS2S4   R                  SS
SS5      nUSS2S4   R                  SS
SS5      n[        XX45      n[        XU5      n[        XU5      nUS:  US:  -  nUb  UUR                  S5      -  n[        R                  " UR                  S5      SSS9S   nUSS2U4   SS2SS4   nUSS2U4   SS2SS4   nUSS2U4   SS2SS4   nU(       a  US   nUS   nUS   nUUU4$ )ak  Recover the relative camera rotation and the translation from an estimated essential matrix.

The method checks the corresponding points in two images and also returns the triangulated
3d points. Internally uses :py:meth:`~kornia.geometry.epipolar.decompose_essential_matrix` and then chooses
the best solution based on the combination that gives more 3d points in front of the camera plane from
:py:meth:`~kornia.geometry.epipolar.triangulate_points`.

Args:
    E_mat: The essential matrix in the form 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)`.
    x1: The set of points seen from the first camera frame in the camera plane
      coordinates with shape :math:`(*, N, 2)`.
    x2: The set of points seen from the first camera frame in the camera plane
      coordinates with shape :math:`(*, N, 2)`.
    mask: A boolean mask which can be used to exclude some points from choosing
      the best solution. This is useful for using this function with sets of points of
      different cardinality (for instance after filtering with RANSAC) while keeping batch
      semantics. Mask is of shape :math:`(*, N)`.

Returns:
    The rotation and translation plus the 3d triangulated points.
    The tuple is as following :math:`[(*, 3, 3), (*, 3, 1), (*, N, 3)]`.

r   )r!  r$   r%   Nr+   r!  r$   r(   r'   r,   r   r&  r   Tr   r   )r   r   r6  r1   r   r   r	   r;   r   r   r   rQ   r2   maxr   )r$  r  r  r?   rA   r+  	unbatchedrT  tsr0  rJ  P1r1  rK  P2rD   d1d2
depth_maskmask_indicesR_outt_outpoints3d_outs                          rH   r   r   f  s   B uo.r?+r?+r?+r?+U[["%&#bhhsm*<RRBHHSbM@RRS4#s,TZZ288CR=01EKK A%I dXXXX:D #5)FB 
!U	B	!U	B 
AtG		B2r	*B	AtG		B2r	*B	AtG		B2r	*B	RR	(B 
B	B	AtG		B2r	*B	RR	(B 
AtG		B2r	*B	AtG		B2r	*B22*A 
"!	$B	"!	$B s(rCx(JdnnQ''
99Z^^B/RFqIL q,1a(Eq,1a(EQ_%aAg.Laa#A%%%rJ   c                     [        U / SQ5        [        U/ SQ5        [        U/ SQ5        [        U/ SQ5        X R                  SS5      -  nX4U-  -
  nXE4$ )a  Compute the relative camera motion between two cameras.

Given the motion parameters of two cameras, computes the motion parameters of the second
one assuming the first one to be at the origin. If :math:`T1` and :math:`T2` are the camera motions,
the computed relative motion is :math:`T = T_{2}T^{-1}_{1}`.

Args:
    R1: The first camera rotation matrix with shape :math:`(*, 3, 3)`.
    t1: The first camera translation vector with shape :math:`(*, 3, 1)`.
    R2: The second camera rotation matrix with shape :math:`(*, 3, 3)`.
    t2: The second camera translation vector with shape :math:`(*, 3, 1)`.

Returns:
    A tuple with the relative rotation matrix and
    translation vector with the shape of :math:`[(*, 3, 3), (*, 3, 1)]`.

r   rN  r+   r(   r#  )r0  rJ  r1  rK  rP  r   s         rH   r   r     sU    ( r?+r?+r?+r?+ 	\\"b!!A 	VA4KrJ   c                 P    [        XU5      R                  U R                  5      nU$ )aW  Find essential matrices.

Args:
     points1: A set of points in the first image with a tensor shape :math:`(B, N, 2), N>=5`.
     points2: A set of points in the second image with a tensor shape :math:`(B, N, 2), N>=5`.
     weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.

Returns:
     the computed essential matrices with shape :math:`(B, 10, 3, 3)`.
     Note that all possible solutions are returned, i.e., 10 essential matrices for each image pair.
     To choose the best one out of 10, try to check the one with the lowest Sampson distance.

)rI   r   r/   )r   r   r    rE   s       rH   r   r     s$      	7W-00?AHrJ   r   )r   )r,   );__doc__typingr   r   r2   kornia.core.checkr   r   r   kornia.core.opsr   r	   kornia.core.utilsr
   )kornia.geometry.solvers.polynomial_solverr   r   r   r   r   numericr   r   
projectionr   r   triangulationr   __all__TensorrI   jitscriptrU   rX   re   floatr   intr   r  r'  longr  r
  r  r  r  r  r  r5   r   r   r   r   r   r   r   r   r   rJ   rH   <module>ru     s  $ B "  W W . - t t A = -	" "u|| "hu||F\ "hmhtht "J #ell #u|| #U\\ #V[VbVb # # #%,, #5<< # #Z_ZfZf # # ||!LL << \\	
 \\ ( G G GE GV[\a\h\hjojvjv\vVw G GV!U\\ !c !c !# !ell ! q'||q'q' LLq' LL	q'
 "LLq' <<q' \\q' LLq' <<q' <<q' ,,q' \\q' \\q' \\q' q'j 
y)Y7uzz	J0

C0

C||IUZZ0
,,y


3
,,y


3& &# &# &c &%,, &u||   B-ell - -%,, -[`[g[g -((ell (uU\\5<<Y^YeYe=e7f (VB'U\\ B'eELLRWR^R^`e`l`lDl>m B'J%,, ELL ell PUP\P\ afamam : %ell8R2S @ $(f&<<f&f& 	f& 		f&
 	f& 5<<
 f& 5<<u||34f&R,,,1LL>Cll
5<<%&F UY\\$)LL;CELL;Q
\\rJ   