
    3j#              
       <   S r SSKJrJr  SSKrSSKJr  SSKJ	r	  SSK
JrJr  SSKJr  SS	KJr  S
\S\R$                  S\R$                  4S jrS\\\R$                  4   S\\\R$                  4   S\R$                  4S jrS\R$                  S\\\R$                  4   S\R$                  4S jrS\R$                  S\R$                  S\R$                  S\R$                  4S jrS!S\R$                  S\S\\R$                  \R$                  \R$                  4   4S jjrS\R$                  S\R$                  S\R$                  S\R$                  4S jrS\R$                  S\\R$                  \R$                  4   4S jrS\R$                  S\R$                  4S  jrg)"zModule for image projections.    )TupleUnionN)qr)KORNIA_CHECK_SHAPE)eye_likevec_like_torch_svd_cast   )cross_product_matrixfocalinputreturnc                 <   [        UR                  5      S:w  a  [        UR                  5      eU S::  a  [        U 5      eUR                  u    p#n[        SU5      nUS==   U -  ss'   US==   U -  ss'   US==   SU-  S-  -  ss'   US	==   SU-  S-  -  ss'   U$ )
a  Return a 3x3 intrinsics matrix, with same size as the input.

The center of projection will be based in the input image size.

Args:
    focal: the focal length for the camera matrix.
    input: image tensor that will determine the batch size and image height
      and width. It is assumed to be a tensor in the shape of :math:`(B, C, H, W)`.

Returns:
    The camera matrix with the shape of :math:`(B, 3, 3)`.

   r      .r   r   .r   r   .r            ?r   .r   r   )lenshapeAssertionErrorr   )r   r   _HW
intrinsicss         ]/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/epipolar/projection.pyintrinsics_liker!       s     5;;1U[[))zU##JAqQ!U#JyU"yU"yS1Wq[(yS1Wq[(    lowhighc                    [         R                  R                  X5      nUR                  S5      nUS   US   US   US   4u  pEpg[         R                  " USU/SXW// SQ/UR
                  UR                  S9nUR                  S5      $ )	zGenerate a random camera matrix based on a given uniform distribution.

Args:
    low: lower range (inclusive).
    high: upper range (exclusive).

Returns:
    the random camera matrix with the shape of :math:`(1, 3, 3)`.

)r   r   r   r   r           )r&   r&   r   )dtypedevice)torchdistributionsUniformsampletensorr'   r(   	unsqueeze)	r#   r$   samplerparamsfxfycxcycamera_matrixs	            r    random_intrinsicsr6   =   s     !!))#4G^^D!FAYq	6!9fQi?NBBLL2sB-#r!PXZX`X`ikirirsM""1%%r"   r5   scale_factorc                     U R                  5       nUS==   U-  ss'   US==   U-  ss'   US==   U-  ss'   US==   U-  ss'   U$ )a  Scale a camera matrix containing the intrinsics.

Applies the scaling factor to the focal length and center of projection.

Args:
    camera_matrix: the camera calibration matrix containing the intrinsic
      parameters. The expected shape for the tensor is :math:`(B, 3, 3)`.
    scale_factor: the scaling factor to be applied.

Returns:
    The scaled camera matrix with shame shape as input :math:`(B, 3, 3)`.

r   r   r   r   )clone)r5   r7   K_scales      r    scale_intrinsicsr;   O   sW     !!#GI,&I,&I,&I,&Nr"   KRtc                    [        U / SQ5        [        U/ SQ5        [        U/ SQ5        [        U R                  5      [        UR                  5      s=:X  a  [        UR                  5      :X  d   [        e  [        e[        R
                  " X/SS9n[        R                  R                  R                  U/ SQSS5      nUS==   S	-  ss'   [        R                  R                  R                  U / S
QSS5      nUS==   S	-  ss'   X-  $ )a  Get the projection matrix P from K, R and t.

This function estimate the projection matrix by solving the following equation: :math:`P = K * [R|t]`.

Args:
   K: the camera matrix with the intrinsics with shape :math:`(B, 3, 3)`.
   R: The rotation matrix with shape :math:`(B, 3, 3)`.
   t: The translation vector with shape :math:`(B, 3, 1)`.

Returns:
   The projection matrix P with shape :math:`(B, 4, 4)`.

*3rB   )rA   rB   1dim)r   r   r   r   constantr&   ).rD   rD   r   )r   r   r   r   )	r   r   r   r   r)   catnn
functionalpad)r<   r=   r>   RtRt_hK_hs         r    projection_from_KRtrO   e   s     q/*q/*q/*qww<3qww<73qww<7 8	A6r	"B88""2|ZED
((


!
!!\:s
CC6Mr"   Pepsc           
      L   [        U / SQ5        U SS2SS2SS24   nU SS2SS2S4   R                  S5      n[        R                  " / SQ/ SQ/ SQ/U R                  U R
                  S	9R                  S5      n[        R                  " XB5      R                  SS
S5      n[        U5      u  pV[        R                  " XER                  SS
S5      5      n[        R                  " U[        R                  " UR                  SS
S5      U5      5      n[        R                  " USSS9U-   n[        R                  " U5      n[        R                  " U5      n	[        R                  " Xi5      n
[        R                  " X5      n[        R                  R                  X5      nXU4$ )ay  Decompose the Projection matrix into Camera-Matrix, Rotation Matrix and Translation vector.

Args:
    P: the projection matrix with shape :math:`(B, 3, 4)`.
    eps: epsilon for numerical stability.

Returns:
    - The Camera matrix with shape :math:`(B, 3, 3)`.
    - The Rotation matrix with shape :math:`(B, 3, 3)`.
    - The Translation vector with shape :math:`(B, 3)`.

)rA   rB   4Nr   r   rD   )r   r   r   )r   r   r   )r   r   r   )r(   r'   r   r   )dim1dim2)r   r.   r)   r-   r(   r'   matmulpermute	linalg_qrdiagonalsign
diag_embedlinalgsolve)rP   rQ   
submat_3x3last_columnreverse	ortho_mat	upper_mat	diagonalssigns	signs_matr<   r=   r>   s                r    KRt_from_projectionrg      sV    q/*1ac1Q3;JAqsAI,((,K llIy)<QXXUVU\U\]gghijGg2::1aCJ$Z0IW&7&71a&@AIWell93D3DQ13Mw&WXI yr;cAIJJy!E  'IY*AY*A1*A7Nr"   Xc                 X    XR                  SS5      -  nUSSSS24   USSSS24   -   nU$ )a*  Return the depth of a point transformed by a rigid transform.

Args:
   R: The rotation matrix with shape :math:`(*, 3, 3)`.
   t: The translation vector with shape :math:`(*, 3, 1)`.
   X: The 3d points with shape :math:`(*, 3)`.

Returns:
   The depth value per point with shape :math:`(*, 1)`.

rT   rD   .r   N)	transpose)r=   r>   rh   X_tmpX_outs        r    depth_from_pointrm      s;     B##E#q!)qa|+ELr"   Ac                 2    [        U 5      u  pnUS   US   4$ )z_Compute the null space of A.

Return the smallest singular value and the corresponding vector.
).rD   r	   )rn   r   svs       r    
_nullspacerr      s%    
 a GA!W:qz!!r"   F_matc                 @   [        U / SQ5        [        SU 5      n[        SU 5      nU R                  SS5      n[	        U5      u  pE[        U5      U -  nUSSS2S4   n[        R                  " X/SS9n[        R                  " Xg/SS9n	[        R                  " X/SS9$ )zGet the projection matrices from the Fundamental Matrix.

Args:
   F_mat: the fundamental matrix with the shape :math:`(B, 3, 3)`.

Returns:
    The projection matrices with shape :math:`(B, 3, 4, 2)`.

r@   r   rT   rD   .NrE   )	r   r   r   rj   rr   r   r)   rH   stack)
rs   R1t1Ft_matr   e2R2t2P1P2s
             r    projections_from_fundamentalr~      s     uo.	!U	B	!U	B__R$FvEA	b	!E	)B	CDL	B	B8	$B	B8	$B;;xR((r"   )gư>)__doc__typingr   r   r)   torch.linalgr   rY   kornia.core.checkr   kornia.core.opsr   r   kornia.core.utilsr
   numericr   floatTensorr!   r6   r;   rO   rg   rm   rr   r~    r"   r    <module>r      s  $ $   ( 0 . - )5  %,, :&5!45 &U5%,,CV=W &\a\h\h &$ELL eU\\FY@Z _d_k_k ,5<< ELL U\\ ell <!5<< !e !uU\\SXS_S_afamamEm?n !H  %,, 5<< ("%,, "5u||)C#D ") ) )r"   