o
    oiL                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ 	dded	ed
ede	de
defddZ	dded	ed
ede	de
defddZded	ed
edefddZded	ed
edefddZdS )z:Module including useful metrics for Structure from Motion.    )Tensor)KORNIA_CHECK_IS_TENSOR)convert_points_to_homogeneous)point_line_distanceT:0yE>pts1pts2Fmsquaredepsreturnc                 C   s  t |tstdt| t|jdk s|jdd dks&td|j | jd dkr1t| } |jd dkr<t|}|jddd	}| | }|| }|| j	dd

d}|dddf jddd

d|dddf jddd

d }	||	 }
|r~|
S |
|  S )a  Return Sampson distance for correspondences given the fundamental matrix.

    Args:
        pts1: correspondences from the left images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
              converted automatically.
        pts2: correspondences from the right images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
              converted automatically.
        Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to avoid ambiguity with torch.nn.functional.
        squared: if True (default), the squared distance is returned.
        eps: Small constant for safe sqrt.

    Returns:
        the computed Sampson distance with shape :math:`(*, N)`.

    #Fm type is not a torch.Tensor. Got    Nr   r   #Fm must be a (*, 3, 3) tensor. Got    dim0dim1dim.
isinstancer   	TypeErrortypelenshape
ValueErrorr   	transposesumpownormsqrt)r   r   r	   r
   r   F_t
line1_in_2
line2_in_1	numeratordenominatorout r+   U/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/epipolar/_metrics.pysampson_epipolar_distance   s"   
 	@r-   c                 C   s  t |tstdt| t|jdk s|jdd dks&td|j | jd dkr1t| } |jd dkr<t|}|jddd	}| | }|| }|| j	dd

d}d|dddf jddd

d d|dddf jddd

d  }	||	 }
|r|
S |
|  S )a  Return symmetrical epipolar distance for correspondences given the fundamental matrix.

    Args:
       pts1: correspondences from the left images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
             converted automatically.
       pts2: correspondences from the right images with shape :math:`(*, N, (2|3))`. If they are not homogeneous,
             converted automatically.
       Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to avoid ambiguity with torch.nn.functional.
       squared: if True (default), the squared distance is returned.
       eps: Small constant for safe sqrt.

    Returns:
        the computed Symmetrical distance with shape :math:`(*, N)`.

    r   r   r   Nr   r   r   r   r   r   g      ?.r   )r   r   r	   r
   r   r%   r&   r'   r(   denominator_invr*   r+   r+   r,   symmetrical_epipolar_distanceO   s&   
 	$r/   c                 C   s~   t |  t | t | t|jdk s|jdd dks$td|j | jd dkr/t| } |jddd}| | }t||S )	a  Return one-sided epipolar distance for correspondences given the fundamental matrix.

    This method measures the distance from points in the right images to the epilines
    of the corresponding points in the left images as they reflect in the right images.

    Args:
       pts1: correspondences from the left images with shape
         :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
       pts2: correspondences from the right images with shape
         :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
       Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to
         avoid ambiguity with torch.nn.functional.

    Returns:
        the computed Symmetrical distance with shape :math:`(*, N)`.

    r   r   Nr   r   r   r   r   )r   r   r   r   r   r    r   )r   r   r	   r%   r&   r+   r+   r,   left_to_right_epipolar_distance   s    
r0   c                 C   sp   t |  t | t | t|jdk s|jdd dks$td|j |jd dkr/t|}|| }t| |S )a  Return one-sided epipolar distance for correspondences given the fundamental matrix.

    This method measures the distance from points in the left images to the epilines
    of the corresponding points in the right images as they reflect in the left images.

    Args:
       pts1: correspondences from the left images with shape
         :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
       pts2: correspondences from the right images with shape
         :math:`(*, N, 2 or 3)`. If they are not homogeneous, converted automatically.
       Fm: Fundamental matrices with shape :math:`(*, 3, 3)`. Called Fm to
         avoid ambiguity with torch.nn.functional.

    Returns:
        the computed Symmetrical distance with shape :math:`(*, N)`.

    r   r   Nr   r   r   r   )r   r   r   r   r   r   )r   r   r	   r'   r+   r+   r,   right_to_left_epipolar_distance   s    
r1   N)Tr   )__doc__torchr   kornia.core.checkr   kornia.geometry.conversionsr   kornia.geometry.linalgr   boolfloatr-   r/   r0   r1   r+   r+   r+   r,   <module>   sF   
5
6"