o
    oi	0                     @  s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZ d dlmZmZ G d	d
 d
eZd!ddZeeeef Zeeef ZG dd dZG dd deZG dd deZG dd deZG dd deZeeeeef Z G dd dZ!d S )"    )annotations)Enum)AnyUnion)Tensorstack
zeros_like)Vector2Vector3)	ImageSize)AffineTransformBrownConradyTransformKannalaBrandtK3Transform)OrthographicProjectionZ1Projectionc                   @  s   e Zd ZdZdZdZdZdS )CameraModelTyper            N)__name__
__module____qualname__PINHOLEBROWN_CONRADYKANNALA_BRANDT_K3ORTHOGRAPHIC r   r   V/home/ubuntu/.local/lib/python3.10/site-packages/kornia/sensors/camera/camera_model.pyr      s
    r   
model_type
image_sizer   paramsr   returnCameraModelVariantsc                 C  sX   | t jkr
t||S | t jkrt||S | t jkrt||S | t jkr(t||S t	d)z!Get camera model from model type.zInvalid Camera Model Type)
r   r   PinholeModelr   BrownConradyModelr   KannalaBrandtK3r   Orthographic
ValueError)r   r   r    r   r   r   get_model_from_type&   s   







r(   c                   @  s   e Zd ZdZd,ddZed-ddZed.ddZed.ddZed/ddZ	ed/ddZ
ed/ddZed/ddZed/ddZd/dd Zd/d!d"Zd0d&d'Zd1d)d*Zd+S )2CameraModelBasea  Base class to represent camera models based on distortion and projection types.

    Distortion is of 3 types:
        - Affine
        - Brown Conrady
        - Kannala Brandt K3
    Projection is of 2 types:
        - Z1
        - Orthographic

    Example:
        >>> params = torch.Tensor([328., 328., 320., 240.])
        >>> cam = CameraModelBase(BrownConradyTransform(), Z1Projection(), ImageSize(480, 640), params)
        >>> cam.params
        tensor([328., 328., 320., 240.])

    
distortionCameraDistortionType
projectionCameraProjectionTyper   r   r    r   r!   Nonec                 C  s,   || _ || _|| _|j| _|j| _|| _dS )a  Construct CameraModelBase class.

        Args:
            distortion: Distortion type
            projection: Projection type
            image_size: Image size
            params: Camera parameters of shape :math:`(B, 4)`
                    for PINHOLE Camera, :math:`(B, 12)`
                    for Brown Conrady, :math:`(B, 8)`
                    for Kannala Brandt K3.

        N)r*   r,   _image_sizeheight_heightwidth_width_params)selfr*   r,   r   r    r   r   r   __init__K   s   
zCameraModelBase.__init__c                 C     | j S )z+Returns the image size of the camera model.)r/   r5   r   r   r   r   a      zCameraModelBase.image_sizeint | Tensorc                 C  r7   )z Returns the height of the image.)r1   r8   r   r   r   r0   f   r9   zCameraModelBase.heightc                 C  r7   )zReturns the width of the image.)r3   r8   r   r   r   r2   k   r9   zCameraModelBase.widthc                 C  r7   )zReturns the camera parameters.r4   r8   r   r   r   r    p   r9   zCameraModelBase.paramsc                 C  
   | j d S )z(Returns the focal length in x direction.).r   r;   r8   r   r   r   fxu      
zCameraModelBase.fxc                 C  r<   )z(Returns the focal length in y direction.).r   r;   r8   r   r   r   fyz   r>   zCameraModelBase.fyc                 C  r<   )z+Returns the principal point in x direction.).r   r;   r8   r   r   r   cx   r>   zCameraModelBase.cxc                 C  r<   )z+Returns the principal point in y direction.).r   r;   r8   r   r   r   cy   r>   zCameraModelBase.cyc                 C  s   t zReturn the camera matrix.)NotImplementedErrorr8   r   r   r   matrix   s   zCameraModelBase.matrixc                 C  s   |   S rB   )rD   r8   r   r   r   K   s   zCameraModelBase.Kpointsr
   r	   c                 C  s   | j | j| j|S )a  Projects 3D points to 2D camera plane.

        Args:
            points: Vector3 representing 3D points.

        Returns:
            Vector2 representing the projected 2D points.

        Example:
            >>> points = Vector3(torch.Tensor([1.0, 1.0, 1.0]))
            >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.]))
            >>> cam.project(points)
            x: 648.0
            y: 568.0

        )r*   distortr    r,   project)r5   rF   r   r   r   rH      s   zCameraModelBase.projectdepthc                 C  s   | j | j| j||S )aN  Unprojects 2D points from camera plane to 3D.

        Args:
            points: Vector2 representing 2D points.
            depth: Depth of the points.

        Returns:
            Vector3 representing the unprojected 3D points.

        Example:
            >>> points = Vector2(torch.Tensor([1.0, 1.0]))
            >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.]))
            >>> cam.unproject(points, torch.Tensor([1.0]))
            x: tensor([-0.9726])
            y: tensor([-0.7287])
            z: tensor([1.])

        )r,   	unprojectr*   	undistortr    )r5   rF   rI   r   r   r   rJ      s   zCameraModelBase.unprojectN)
r*   r+   r,   r-   r   r   r    r   r!   r.   )r!   r   )r!   r:   r!   r   )rF   r
   r!   r	   )rF   r	   rI   r   r!   r
   )r   r   r   __doc__r6   propertyr   r0   r2   r    r=   r?   r@   rA   rD   rE   rH   rJ   r   r   r   r   r)   8   s.    



r)   c                      s6   e Zd ZdZd fdd	Zdd
dZdddZ  ZS )r#   a  Class to represent Pinhole Camera Model.

    The pinhole camera model describes the mathematical relationship between
    the coordinates of a point in three-dimensional space and its projection
    onto the image plane of an ideal pinhole camera,
    where the camera aperture is described as a point and no lenses are used to focus light.
    See more: https://en.wikipedia.org/wiki/Pinhole_camera_model

    Example:
        >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.]))
        >>> cam
        CameraModel(ImageSize(height=480, width=640), PinholeModel, tensor([328., 328., 320., 240.]))

    r   r   r    r   r!   r.   c                   >   |j d dkst|j dkrtdt t t || dS )zConstruct PinholeModel class.

        Args:
            image_size: Image size
            params: Camera parameters of shape :math:`(B, 4)` of the form :math:`(fx, fy, cx, cy)`.

           r   z1params must be of shape (B, 4) for PINHOLE CameraN)shapelenr'   superr6   r   r   r5   r   r    	__class__r   r   r6         zPinholeModel.__init__c                 C  s^   t | j}t| j|| jfd}t|| j| jfd}t|||fd}t|||fd}d|d< |S )a  Return the camera matrix.

        The matrix is of the form:

        .. math::
            \begin{bmatrix} fx & 0 & cx \\
            0 & fy & cy \\
            0 & 0 & 1\end{bmatrix}

        Example:
            >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([1.0, 2.0, 3.0, 4.0]))
            >>> cam.matrix()
            tensor([[1., 0., 3.],
                    [0., 2., 4.],
                    [0., 0., 1.]])

        rP   g      ?).rP   rP   )r   r=   r   r@   r?   rA   )r5   zrow1row2row3rE   r   r   r   rD      s   
zPinholeModel.matrixscale_factorc                 C  s^   | j | }| j| }| j| }| j| }t||||fd}t| jj| | jj| }t	||S )a  Scales the camera model by a scale factor.

        Args:
            scale_factor: Scale factor to scale the camera model.

        Returns:
            Scaled camera model.

        Example:
            >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.]))
            >>> cam_scaled = cam.scale(2)
            >>> cam_scaled.params
            tensor([656., 656., 640., 480.])

        rP   )
r=   r?   r@   rA   r   r   r   r0   r2   r#   )r5   r^   r=   r?   r@   rA   r    r   r   r   r   scale   s   




zPinholeModel.scaler   r   r    r   r!   r.   rL   )r^   r   r!   r#   )r   r   r   rM   r6   rD   r_   __classcell__r   r   rV   r   r#      s
    
r#   c                      "   e Zd ZdZd
 fdd	Z  ZS )r$   zBrown Conrady Camera Model.r   r   r    r   r!   r.   c                   rO   )zConstruct BrownConradyModel class.

        Args:
            image_size: Image size
            params: Camera parameters of shape :math:`(B, 12)` of the form :math:`(fx, fy, cx, cy, kb0, kb1, kb2, kb3,
                    k1, k2, k3, k4)`.

        rP      r   z8params must be of shape (B, 12) for BROWN_CONRADY CameraN)rR   rS   r'   rT   r6   r   r   rU   rV   r   r   r6     s   	zBrownConradyModel.__init__r`   r   r   r   rM   r6   ra   r   r   rV   r   r$   	      r$   c                      rb   )r%   zKannala Brandt K3 Camera Model.r   r   r    r   r!   r.   c                   rO   )zConstruct KannalaBrandtK3 class.

        Args:
            image_size: Image size
            params: Camera parameters of shape :math:`(B, 8)` of the form :math:`(fx, fy, cx, cy, kb0, kb1, kb2, kb3)`.

        rP      r   z9params must be of shape B, 8 for KANNALA_BRANDT_K3 CameraN)rR   rS   r'   rT   r6   r   r   rU   rV   r   r   r6     rX   zKannalaBrandtK3.__init__r`   rd   r   r   rV   r   r%     re   r%   c                      rb   )r&   zOrthographic Camera Model.r   r   r    r   r!   r.   c                   s>   t  t t || |jd dkst|jdkrtddS )zConstruct Orthographic class.

        Args:
            image_size: Image size
            params: Camera parameters of shape :math:`(B, 4)` of the form :math:`(fx, fy, cx, cy)`.

        rP   rQ   r   z4params must be of shape B, 4 for ORTHOGRAPHIC CameraN)rT   r6   r   r   rR   rS   r'   rU   rV   r   r   r6   -  s   zOrthographic.__init__r`   rd   r   r   rV   r   r&   *  re   r&   c                   @  s.   e Zd ZdZdd
dZdddZdddZdS )CameraModelaG  Class to represent camera models.

    Example:
        >>> # Pinhole Camera Model
        >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.PINHOLE, torch.Tensor([328., 328., 320., 240.]))
        >>> # Brown Conrady Camera Model
        >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.BROWN_CONRADY, torch.Tensor([1.0, 1.0, 1.0, 1.0,
        ... 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]))
        >>> # Kannala Brandt K3 Camera Model
        >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.KANNALA_BRANDT_K3, torch.Tensor([1.0, 1.0, 1.0,
        ... 1.0, 1.0, 1.0, 1.0, 1.0]))
        >>> # Orthographic Camera Model
        >>> cam = CameraModel(ImageSize(480, 640), CameraModelType.ORTHOGRAPHIC, torch.Tensor([328., 328., 320., 240.]))
        >>> cam.params
        tensor([328., 328., 320., 240.])

    r   r   r   r   r    r   r!   r.   c                 C  s   t |||| _dS )zConstruct CameraModel class.

        Args:
            image_size: Image size
            model_type: Camera model type
            params: Camera parameters of shape :math:`(B, N)`.

        N)r(   _model)r5   r   r   r    r   r   r   r6   P  s   	zCameraModel.__init__namestrr   c                 C  s   t | j|S )N)getattrrh   )r5   ri   r   r   r   __getattr__[  s   zCameraModel.__getattr__c                 C  s"   d| j  d| jjj d| j dS )NzCameraModel(z, ))r   rh   rW   r   r    r8   r   r   r   __repr__^  s   "zCameraModel.__repr__N)r   r   r   r   r    r   r!   r.   )ri   rj   r!   r   )r!   rj   )r   r   r   rM   r6   rl   rn   r   r   r   r   rg   =  s
    

rg   N)r   r   r   r   r    r   r!   r"   )"
__future__r   enumr   typingr   r   kornia.corer   r   r   kornia.geometry.vectorr	   r
   kornia.imager   &kornia.sensors.camera.distortion_modelr   r   r   &kornia.sensors.camera.projection_modelr   r   r   r(   r+   r-   r)   r#   r$   r%   r&   r"   rg   r   r   r   r   <module>   s(   
 O