a
    d"                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ deje	eejejf ddd	Z
deje	ejd
ddZejejejejdddZejejejdddZejejejejdddZejejejdddZdS )zKModule containing the functionalities for computing the Fundamental Matrix.    )TupleN)convert_points_to_homogeneous)transform_points:0yE>)pointsepsreturnc              
   C   s   t | jdkrt| j| jd dkr0t| jtj| ddd}| | jdddjdd}ttd	||  }t|t	| }}tj
||| |d
  ||| |d  |||g	dd}|ddd}t|| }||fS )a  Normalizes 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)dimZkeepdim)r   pr   g       @).r   r   ).r   r   )lenshapeAssertionErrortorchmeanZnormsqrttensor	ones_likeZ
zeros_likestackviewr   )r   r   Zx_meanZscaleoneszerosZ	transformZpoints_norm r   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/epipolar/fundamental.pynormalize_points   s    

*
r   )Mr   r   c                 C   sL   t | jdk rt| j| dddddf }t| |k| ||  | S )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)r   r   r   r   whereabs)r   r   Znorm_valr   r   r   normalize_transformation3   s    
r"   )points1points2weightsr   c              
   C   sb  | j |j krt| j |j t|j dkr<|j d | j d ksFt|j t| \}}t|\}}tj|ddd\}}tj|ddd\}	}
t|}tj|	| |	| |	|
| |
| |
|||g	dd}t|}|	dd| | }t
|\}}}|d ddd}t
|\}}}tjg d	|j|jd
}|t|| |	dd  }|	dd||  }t|S )a&  Compute the fundamental matrix using the DLT formulation.

    The linear system is solved by using the Weighted Least Squares Solution for the 8 Points algorithm.

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

    Returns:
        the computed fundamental matrix with shape :math:`(B, 3, 3)`.
    r   r   r
   r   chunksr   ).r
   r	   )      ?r)           )devicedtype)r   r   r   r   r   chunkr   catZ
diag_embed	transposeZsvdr   r   r+   r,   r"   )r#   r$   r%   Zpoints1_normZ
transform1Zpoints2_normZ
transform2x1y1Zx2y2r   XZw_diag_VF_matUSZ	rank_maskZF_projectedZF_estr   r   r   find_fundamentalF   s&    "

0
r9   )r   r6   r   c                 C   s   t | jdkr| jd dks&t| jt |jdkrF|jdd dksPt|jt| }tj|ddd}tj|| ddd\}}}|| ||  }t|d	kd
t| t	|}tj
|| || || gdd}tj|dddS )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)`.
        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)`.
    r   r
   r(   Nr	   r	   )Zdim0Zdim1r	   r&   r*   r)   r   )r   r   r   r   r   r/   r-   r    r   r   r.   )r   r6   Zpoints_habcnuliner   r   r   compute_correspond_epilinesx   s    
 
" r@   )E_matK1K2r   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d t |jdd   krt |jdd ksn t| dd|  |  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   r(   Nr:   r
   )r   r   r   Zinverser/   )rA   rB   rC   r   r   r   fundamental_from_essential   s     
 
 
@rD   )P1P2r   c                 C   sD  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| jdd |jdd krttdd }| dddddf }|| ddd	ddf | dd
dddf }| dddddf }|dddddf }||ddd	ddf |dd
dddf }|dddddf }|||||||||  }	}
}|||||||||  }}}|||||||||  }}}tj|	 dd|
 dd| dd| dd| dd| dd| dd| dd| ddg	dd}|jg | jdd d	d	R  S )a5  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(   N)r	      c                 S   s   t j| |gddS )Nr(   r   )r   r.   )xyr   r   r   vstack   s    z,fundamental_from_projections.<locals>.vstack.r   r	   r   r
   r   )r   r   r   r   r.   ZdetZreshaper   )rE   rF   rJ   ZX1ZX2ZX3ZY1ZY2ZY3ZX1Y1ZX2Y1ZX3Y1ZX1Y2ZX2Y2ZX3Y2ZX1Y3ZX2Y3ZX3Y3ZF_vecr   r   r   fundamental_from_projections   s<    
 
 
.."""rK   )r   )r   )__doc__typingr   r   Zkornia.geometry.conversionsr   Zkornia.geometry.linalgr   ZTensorfloatr   r"   r9   r@   rD   rK   r   r   r   r   <module>   s   "(2