a
    d.                     @   s   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 eejejdd	d
Zeeejf eeejf ejdddZejeeejf ejdddZejejejejdddZd ejeeejejejf dddZejejejejdddZdd ZejejdddZdS )!zModule for image projections.    )TupleUnionN)eye_likevec_like)	linalg_qr   )cross_product_matrix)focalinputreturnc                 C   s   t |jdkrt|j| dkr(t| |j\}}}}td|}|d  | 9  < |d  | 9  < |d  d| d 7  < |d	  d| d 7  < |S )
a  Return a 3x3 instrinsics 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WZ
intrinsics r   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/epipolar/projection.pyintrinsics_like   s    

r   )lowhighr   c           	         sl   t j| |  fddtdD \}}}}t |t | }}t |||||||||g	}|dddS )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)`.
    c                 3   s   | ]}  d V  qdS ))r   N)sample).0r   Zsamplerr   r   	<genexpr>3       z$random_intrinsics.<locals>.<genexpr>r   r   r   )torchdistributionsZUniformrangeZ
zeros_likeZ	ones_likecatview)	r   r   ZfxZfyZcxcyzerosZonescamera_matrixr   r!   r   random_intrinsics(   s
    
r,   )r+   scale_factorr   c                 C   sL   |   }|d  |9  < |d  |9  < |d  |9  < |d  |9  < |S )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)r+   r-   ZK_scaler   r   r   scale_intrinsics9   s    r/   )KRtr   c                 C   s   | j dd dkrt| j |j dd dkr8t|j |j dd dkrTt|j t| j t|j   krzt|j ksn ttj||gdd}tjj|g ddd	}|d
  d7  < tjj| g ddd	}|d
  d7  < | | S )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 instrinsics 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)`.
    Nr   r   )r   r   Zdim)r   r   r   r   Zconstantg        ).r5   r5   r   )r   r   r   r   )r   r   r   r$   r'   nnZ
functionalpad)r0   r1   r2   ZRtZRt_hZK_hr   r   r   projection_from_KRtN   s    


(r9   ư>)Pepsr   c              
   C   s@  | j dd dkrtdt| j dkr,t| ddddddf }| dddddf d}tjg dg d	g d
g| j| jdd}t||	ddd}t
|\}}t||	ddd}t|t|	ddd|}tj|ddd| }t|}t|}	t||	}
t|	|}tt|
|}|
||fS )aj  Decompose the Projection matrix into Camera-Matrix, Rotation Matrix and Translation vector.

    Args:
        P: the projection matrix with shape :math:`(B, 3, 4)`.

    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)`.
    r3   N)r   r   zP must be of shape [B, 3, 4]r   r   r5   )r   r   r   )r   r   r   )r   r   r   )devicedtyper   r   )Zdim1Zdim2)r   r   r   Z	unsqueezer$   Ztensorr=   r>   matmulZpermuter   ZdiagonalsignZ
diag_embedZinverse)r;   r<   Z
submat_3x3Zlast_columnreverseZ	ortho_matZ	upper_matZ	diagonalsZsignsZ	signs_matr0   r1   r2   r   r   r   KRt_from_projectionn   s$    ,

rB   )r1   r2   Xr   c                 C   s8   | | dd }|ddddf |ddddf  }|S )aE  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)`.
    r3   r5   .r   N)	transpose)r1   r2   rC   ZX_tmpZX_outr   r   r   depth_from_point   s    $rE   c                 C   s    t | \}}}|d |d fS )zgCompute the null space of A.

    Return the smallest singular value and the corresponding vector.
    ).r5   )r$   Zsvd)Ar   sZvhr   r   r   
_nullspace   s    rH   )F_matr   c           
      C   s   t | jdkrt| j| jdd dkr4t| jtd| }td| }| dd}t|\}}t||  }|ddddf }tj	||gdd}tj	||gdd}	tj
||	gddS )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   r3   Nr4   r5   .r6   )r   r   r   r   r   rD   rH   r   r$   r'   stack)
rI   ZR1t1ZFt_matr   e2ZR2t2ZP1ZP2r   r   r   projections_from_fundamental   s    	



rN   )r:   )__doc__typingr   r   r$   Zkornia.utilsr   r   Zkornia.utils._compatr   numericr   floatZTensorr   r,   r/   r9   rB   rE   rH   rN   r   r   r   r   <module>   s   (  &&	