o
    }oi                     @   sp   d dl mZmZ d dlmZ d dlZd dl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dS )    )ABCabstractmethod)ListNc                	   @   sJ   e Zd ZdZddededejddfdd	Zedd
dZ	edddZ
dS )Cameraz0
    Abstract base class for Camera models.
    cudawidthheightdevicereturnNc                 C   s   || _ || _|| _dS )a-  
        Initializes the Camera instance with given dimensions and device.

        Parameters:
            width: int - Width of the camera frame.
            height: int - Height of the camera frame.
            device: torch.device - The device where tensor computations will be performed.
        N)r   r   r	   )selfr   r   r	    r   a/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/data/nerf/cameras.py__init__   s   	
zCamera.__init__c                 C      dS )z?
        Abstract method to compute camera intrinsics.
        Nr   r   r   r   r   compute_intrinsics(      zCamera.compute_intrinsicsc                 C   r   )zC
        Abstract method to compute the projection matrix.
        Nr   r   r   r   r   compute_projection_matrix/   r   z Camera.compute_projection_matrixr   )r
   N)__name__
__module____qualname____doc__inttorchr	   r   r   r   r   r   r   r   r   r      s    r   c                   @   s    e Zd ZdZdejfddZdS )OrthographicCameraz/
    Class for Orthographic Camera models.
    r
   c                 C   sJ   t jd| j dddgdd| j ddgg dg dgt j| jdd}|S )z
        Computes the projection matrix for an Orthographic camera.

        Returns:
            torch.Tensor: The projection matrix.
           r   r   r   r   )r   r   r      dtyper	   )r   tensorr   r   float32r	   	unsqueeze)r   
projectionr   r   r   r   <   s   .z,OrthographicCamera.compute_projection_matrixN)r   r   r   r   r   Tensorr   r   r   r   r   r   7   s    r   c                       sn   e Zd ZdZddededededejdd	f fd
dZdedede	j
fddZdededejfddZ  ZS )PinholeCameraz*
    Class for Pinhole Camera models.
    r   r   r   nearfarr	   r
   Nc                    s    t  ||| || _|| _dS )a  
        Initializes the Pinhole Camera instance with given parameters.

        Parameters:
            width: int - Width of the camera frame.
            height: int - Height of the camera frame.
            near: float - Near clipping plane.
            far: float - Far clipping plane.
            device: torch.device - The device where tensor computations will be performed.
        N)superr   r)   r*   r   r   r   r)   r*   r	   	__class__r   r   r   P   s   
zPinholeCamera.__init__fovxfovyc                 C   sd   | j dtt|d   }| jdtt|d   }| j d | jd }}t||||gS )a  
        Computes the intrinsic matrix for the camera based on field of views.

        Parameters:
            fovx: float - Field of view in X direction.
            fovy: float - Field of view in Y direction.

        Returns:
            np.ndarray: The intrinsic matrix.
        r   )r   nptandeg2radr   array)r   r/   r0   focal_xfocal_ycxcyr   r   r   r   _   s   z PinholeCamera.compute_intrinsicsr5   r6   c              	   C   s   t jd| | j dddgdd| | j ddgdd| j| j  | j| j  d| j | j  | j| j  gg dgt j| jdd}|S )a  
        Computes the projection matrix for the camera.

        Parameters:
            focal_x: float - Focal length in X direction.
            focal_y: float - Focal length in Y direction.

        Returns:
            torch.Tensor: The projection matrix.
        r   r   r   r   r!   )	r   r#   r   r   r*   r)   r$   r	   r%   )r   r5   r6   r&   r   r   r   r   o   s    z'PinholeCamera.compute_projection_matrixr   )r   r   r   r   r   floatr   r	   r   r1   ndarrayr   r'   r   __classcell__r   r   r-   r   r(   K   s
    * r(   c                   @   s`   e Zd ZdZ	ddedededed	ejd
dfddZd
e	e
j fddZd
e	ej fddZdS )
CubeCamerazQ
    Class for Cube Camera models, which is essentially six pinhole cameras.
    {Gz?  r   r   r   r)   r*   r	   r
   Nc                 C   s"   || _ || _|| _|| _|| _dS )a  
        Initializes the Cube Camera instance with given parameters.

        Parameters:
            width: int - Width of each camera face.
            height: int - Height of each camera face.
            near: float - Near clipping plane.
            far: float - Far clipping plane.
            device: torch.device - The device where tensor computations will be performed.
        Nr   r   r)   r*   r	   r,   r   r   r   r      s
   
zCubeCamera.__init__c                        fddt dD S )z
        Computes the intrinsic matrices for the six faces of the cube using a Pinhole camera model.

        Returns:
            List[np.ndarray]: List of 6 intrinsic matrices, one for each face.
        c              	      0   g | ]}t  j j j j jd ddqS )r?   Z   )r(   r   r   r)   r*   r	   r   .0_r   r   r   
<listcomp>       z1CubeCamera.compute_intrinsics.<locals>.<listcomp>   ranger   r   r   r   r         
zCubeCamera.compute_intrinsicsc                    r@   )z
        Computes the projection matrices for the six faces of the cube using a Pinhole camera model.

        Returns:
            List[torch.Tensor]: List of 6 projection matrices, one for each face.
        c              	      rA   )r?   r    )r(   r   r   r)   r*   r	   r   rC   r   r   r   rF      rG   z8CubeCamera.compute_projection_matrix.<locals>.<listcomp>rH   rI   r   r   r   r   r      rK   z$CubeCamera.compute_projection_matrix)r=   r>   r   )r   r   r   r   r   r9   r   r	   r   r   r1   r:   r   r'   r   r   r   r   r   r<      s$    
r<   )abcr   r   typingr   numpyr1   r   r   r   r(   r<   r   r   r   r   <module>   s   !A