o
    oi                     @   s   d dl mZ d dlZd dlmZmZmZmZmZ ddej	dej	de
dej	fd	d
Z	ddej	dej	dej	deej	 dej	f
ddZdS )    )OptionalN)cos	ones_likesinstack
zeros_likeFtauxtauyreturn_inversereturnc              
   C   s  | j |j krtd| j  d|j  d|  }| d} |d}t| }t| }t|}t|}t|}t|}	t|	||||||| |g	dddd}
t||| ||	||||g	dddd}||
 }|rd|d  }t|||d | |||d	 | |||	g	dddd}|	dd
| }|dkrt
|}|S t|d ||d  ||d |d	  |||	g	dddd}||	dd
 }|dkrt
|}|S )a  Estimate the tilt projection matrix or the inverse tilt projection matrix.

    Args:
        taux: Rotation angle in radians around the :math:`x`-axis with shape :math:`(*, 1)`.
        tauy: Rotation angle in radians around the :math:`y`-axis with shape :math:`(*, 1)`.
        return_inverse: False to obtain the tilt projection matrix. True for the inverse matrix.

    Returns:
        torch.Tensor: Inverse tilt projection matrix with shape :math:`(*, 3, 3)`.

    zShape of taux z
 and tauy z do not match.      ).   r   ).r   r   ).r   r   r   )shape
ValueErrordimreshaper   r   r   r   r   	transposetorchsqueeze)r   r	   r
   ndimcTxsTxcTysTyzerooneRxRyRinvR22invPzinv_tiltPztilt r'   W/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/calibration/distort.pytilt_projection   s@   

((&

0
r)   pointsKdistnew_Kc                 C   s  |   dk r| jd dkrtd| j d|jdd dkr(td|j d|du r/|}n|jdd dkrAtd	|j d|jd d
vrRtd|jd  |jd dk rhtjj|dd|jd  g}|ddddf }|ddddf }|ddddf }|ddddf }| d | | }| d | | }	|| |	|	  }
d|dddf |
  |dddf |
 |
  |dddf |
d   d|dddf |
  |dddf |
 |
  |dddf |
d    }|| d|dddf  | |	  |dddf |
d| |    |dddf |
  |dddf |
 |
  }|	| |dddf |
d|	 |	    d|dddf  | |	  |dddf |
  |dddf |
 |
  }t|d dkspt|d dkrt|d |d }t	||t
|gd|dd }|d |d  }|d |d  }|ddddf }|ddddf }|ddddf }|ddddf }|| | }|| | }	t	||	gdS )a~  Distortion of a set of 2D points based on the lens distortion model.

    Radial :math:`(k_1, k_2, k_3, k_4, k_4, k_6)`,
    tangential :math:`(p_1, p_2)`, thin prism :math:`(s_1, s_2, s_3, s_4)`, and tilt :math:`(\tau_x, \tau_y)`
    distortion models are considered in this function.

    Args:
        points: Input image points with shape :math:`(*, N, 2)`.
        K: Intrinsic camera matrix with shape :math:`(*, 3, 3)`.
        dist: Distortion coefficients
            :math:`(k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6[,s_1,s_2,s_3,s_4[,\tau_x,\tau_y]]]])`. This is
            a vector with 4, 5, 8, 12 or 14 elements with shape :math:`(*, n)`.
        new_K: Intrinsic camera matrix of the distorted image. By default, it is the same as K but you may additionally
            scale and shift the result by using a different matrix. Shape: :math:`(*, 3, 3)`. Default: None.

    Returns:
        Undistorted 2D points with shape :math:`(*, N, 2)`.

    Example:
        >>> points = torch.rand(1, 1, 2)
        >>> K = torch.eye(3)[None]
        >>> dist_coeff = torch.rand(1, 4)
        >>> points_dist = distort_points(points, K, dist_coeff)

    r   r   zpoints shape is invalid. Got .r   N)r   r   zK matrix shape is invalid. Got z#new_K matrix shape is invalid. Got )               z/Invalid number of distortion coefficients. Got r3   r   .r   ).r   ).r   r/   r0   r         r1   	   
      r2   ).r2   ).   ).r   )r   r   r   r   nn
functionalpadanyr)   r   r   r   )r*   r+   r,   r-   new_cxnew_cynew_fxnew_fyxyr2rad_polyxdydr&   points_untiltcxcyfxfyr'   r'   r(   distort_pointsO   sl   FF(	 rM   )F)N)typingr   r   kornia.corer   r   r   r   r   Tensorboolr)   rM   r'   r'   r'   r(   <module>   s    "6