a
    þd+  ã                   @   sB  d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZ g d	¢Zejejejejd
œdd„Zejeejejejf dœdd„Zejejejejejdœdd„Zejeejejf dœdd„Zdejejejejejeej eejejejf dœdd„Zejejejejeejejf dœdd„ZdS )z;Module containing functionalities for the Essential matrix.é    )ÚOptionalÚTupleN)Úeye_likeÚvec_likeé   )Úcross_product_matrix)Údepth_from_pointÚprojection_from_KRt)Útriangulate_points)Úessential_from_fundamentalÚdecompose_essential_matrixÚessential_from_RtÚmotion_from_essentialÚ%motion_from_essential_choose_solutionÚrelative_camera_motion)ÚF_matÚK1ÚK2Úreturnc                 C   sÖ   t | jƒdkr | jdd… dks*t| jƒ‚t |jƒdkrJ|jdd… dksTt|jƒ‚t |jƒdkrt|jdd… dks~t|jƒ‚t | jdd… ƒt |jdd… ƒ  kr¼t |jdd… ƒksÂn t‚| dd¡|  | S )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)`.
    é   éþÿÿÿN©é   r   éÿÿÿÿ©ÚlenÚshapeÚAssertionErrorÚ	transpose)r   r   r   © r   úk/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/epipolar/essential.pyr      s     
 
 
@r   )ÚE_matr   c                 C   s  t | jƒdkr| jdd… s&t| jƒ‚t | ¡\}}}| dd¡}t | ¡}|ddd…f  d9  < | dd¡}t t |¡dk d || |¡}t t |¡dk d || |¡}t	t 
g d	¢g¡ | ¡ƒ}|d
  d7  < || | }|| dd¡ | }	|}
|	}|ddd…f }|
||fS )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   Nr   .g      ð¿ç        ).NN)r"   r"   ç      ð?).r   r   r#   )r   r   r   ÚtorchZsvdr   Z	ones_likeÚwhereZdetr   ZtensorZtype_as)r!   ÚUÚ_ÚVZVtÚmaskZmasktÚWZU_W_VtZU_Wt_VtÚR1ÚR2ÚTr   r   r    r   /   s"    

  r   )r+   Út1r,   Út2r   c                 C   sÎ   t | jƒdkr | jdd… dks*t| jƒ‚t |jƒdkrJ|jdd… dksTt|jƒ‚t |jƒdkrt|jdd… dks~t|jƒ‚t |jƒdkrž|jdd… dks¨t|jƒ‚t| |||ƒ\}}t|d ƒ}|| S )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   Nr   ©r   r   ).r   )r   r   r   r   r   )r+   r.   r,   r/   ÚRÚtZTxr   r   r    r   Z   s     
 
 
 
r   c                 C   sp   t | jƒdkr | jdd… dks*t| jƒ‚t| ƒ\}}}tj||||gdd}tj|| || gdd}||fS )uó  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   Nr   éýÿÿÿ)Údim)r   r   r   r   r$   Ústack)r!   r+   r,   r2   ÚRsZTsr   r   r    r   z   s     
r   )r!   r   r   Úx1Úx2r)   r   c                 C   sv  t | jƒdkr | jdd… dks*t| jƒ‚t |jƒdkrJ|jdd… dksTt|jƒ‚t |jƒdkrt|jdd… dks~t|jƒ‚t |jƒdkrš|jd dks¤t|jƒ‚t |jƒdkrÀ|jd dksÊt|jƒ‚t | jdd… ƒt |jdd… ƒ  krt |jdd… ƒksn t‚|durVt |jƒdk r6t|jƒ‚|j|jdd… krVt|jƒ‚t | jƒdk}|r¤| d } |d }|d }|d }|d }|dur¤|d }t| ƒ\}}td| ƒ}	td| ƒ}
|	dd…df  dddd¡}	|
dd…df  dddd¡}
|dd…df  dddd¡}t||	|
ƒ}|}|}|dd…df  dddd¡}t|||ƒ}|dd…df  dddd¡}|dd…df  dddd¡}t||||ƒ}t	|	|
|ƒ}t	|||ƒ}|d	k|d	k@ }|durÚ|| 
d¡M }tj| d¡dd
dd }|dd…|f dd…ddf }|dd…|f dd…ddf }|dd…|f dd…ddf }|rl|d }|d }|d }|||fS )aº  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   Nr   r   r   r   é   r"   T)r4   Zkeepdimr   )r   r   r   r   r   r   Úexpandr	   r
   r   Z	unsqueezer$   ÚmaxÚsum)r!   r   r   r7   r8   r)   Z	unbatchedr6   Útsr+   r.   ZP1r,   r/   ZP2ÚXÚd1Zd2Z
depth_maskZmask_indicesZR_outZt_outZpoints3d_outr   r   r    r   ”   sl      
 
 


D






r   c                 C   sÌ   t | jƒdkr | jdd… dks*t| jƒ‚t |jƒdkrJ|jdd… dksTt|jƒ‚t |jƒdkrt|jdd… dks~t|jƒ‚t |jƒdkrž|jdd… dks¨t|jƒ‚||  dd¡ }|||  }||fS )u  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   r   Nr   r0   r   r   )r+   r.   r,   r/   r1   r2   r   r   r    r     s     
 
 
 
r   )N)Ú__doc__Útypingr   r   r$   Zkornia.utilsr   r   Únumericr   Z
projectionr   r	   Ztriangulationr
   Ú__all__ZTensorr   r   r   r   r   r   r   r   r   r    Ú<module>   s0   
"+    úùpþ