o
    }oi                     @   s   d dl mZmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZmZmZ edgedged gddd	d
fdejdejdejdedededejdejfddZG dd deZdS )    )DictUnionN)Dataset)PinholeCamera)compute_look_at_vectorsconstruct_posesget_raysget_view_direction皙	@<      Fcudaradiusthetaphiangle_overheadangle_frontreturn_dirsdevicereturnc                 C   s   |d t j }|d t j }|d t j }|d t j }tj| t| t| | t| | t| t| gdd}t||d\}}	}
t||
|	||d}|rZt||||nd}||fS )a  
    Generate camera poses based on a circular arrangement.

    Parameters:
        radius: torch.Tensor - Radii for the camera positions.
        theta: torch.Tensor - Theta angles for the camera positions.
        phi: torch.Tensor - Phi angles for the camera positions.
        angle_overhead: float - Angle range of the overhead view.
        angle_front: float - Angle range of the front view.
        return_dirs: bool - Whether to return the view directions.
        device: str - The device to allocate the tensor on (e.g., 'cuda' or 'cpu').

    Returns:
        Tuple: Contains the following:
            - poses (torch.Tensor): Generated poses, shape [size, 4, 4].
            - dirs (torch.Tensor, optional): View directions, if requested.
       dim)centersr   )r   right_vector	up_vectorforward_vectorr   N)	nppitorchstacksincosr   r   r	   )r   r   r   r   r   r   r   r   r   r   r   posesdirs r&   f/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/data/nerf/circle_poses.pycircle_poses   s"   

r(   c                       s   e Zd ZdZ												
		d&dededededededededededededejddf fddZdefddZ	d ede
eeeejf f fd!d"Zd#ede
eeeejf f fd$d%Z  ZS )'CirclePosesDatasetz3
    A dataset class to generate circle poses.
    d            4@r
        V@              >@      N@{Gz?     @@cpusizeheightwidthdefault_fovxdefault_fovydefault_radiusdefault_polardefault_azimuthr   r   nearfarr   r   Nc                    sz   t    || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _t| j| j| j| j| jd| _dS )a6  
        Initializes a new CirclePosesDataset instance.

        Parameters:
            size (int): Number of samples in the dataset.
            height (int): Height of the image.
            width (int): Width of the image.
            default_fovx (float): Default field of view in x-direction.
            default_fovy (float): Default field of view in y-direction.
            default_radius (float): Default radius of the circle.
            default_polar (float): Default polar angle.
            default_azimuth (float): Default azimuth angle.
            angle_overhead (float): Overhead angle.
            angle_front (float): Frontal angle.
            near (float): Near clipping distance.
            far (float): Far clipping distance.
            device (torch.device): Device to generate data on.
        )r6   r5   r<   r=   r   N)super__init__r4   r5   r6   r7   r8   r9   r:   r;   r   r   r<   r=   r   r   camera)selfr4   r5   r6   r7   r8   r9   r:   r;   r   r   r<   r=   r   	__class__r&   r'   r?   Z   s"   
"zCirclePosesDataset.__init__c                 C   s   | j S )z-Returns the number of samples in the dataset.)r4   )rA   r&   r&   r'   __len__   s   zCirclePosesDataset.__len__idxc              	   C   s   t | jg| j}t || j d g| j}t | jg| j}t|||| j| j	d| jd\}}| j
j| j| jd}| j
j|d |d d}|t | }	t||| j| j|jd\}
}|| j }||d	k  d8  < | j| j|
|||	|d
}|S )a  Get an item from the dataset.

        Args:
            idx (int): Index of the item to retrieve.

        Returns:
            dict: Data dictionary containing the following:
            - height (int): Height of the image.
            - width (int): Width of the image.
            - rays_o (torch.Tensor): Ray origins, shape [height, width, 3].
            - rays_d (torch.Tensor): Ray directions, shape [height, width, 3].
            - dir (torch.Tensor): View direction, shape [3].
            - mvp (torch.Tensor): Model-view-projection matrix, shape [4, 4].
            - azimuth (torch.Tensor): Azimuth angle, shape [1].
        ih  T)r   r   r   r   r   r   r   )fovxfovyr      )focal_xfocal_y)r$   
intrinsicsr5   r6   r   r   )r5   r6   rays_orays_ddirmvpazimuth)r    FloatTensorr:   tor   r4   r9   r(   r   r   r@   compute_intrinsicsr7   r8   compute_projection_matrixinverser   r5   r6   r;   )rA   rE   thetasphisr   r$   r%   rK   
projectionrO   rL   rM   delta_azimuthdatar&   r&   r'   __getitem__   s:   



zCirclePosesDataset.__getitem__batchc              
   C   s|   | j | jtjdd |D ddtjdd |D ddtjdd |D ddtjdd |D ddtjdd |D ddd	S )
zCollate function to combine multiple data points into batches.

        Args:
            batch (list): List of data dictionaries.

        Returns:
            dict: Collated data.
        c                 S      g | ]}|d  qS )rL   r&   .0itemr&   r&   r'   
<listcomp>       z1CirclePosesDataset.collate_fn.<locals>.<listcomp>r   r   c                 S   r]   )rM   r&   r^   r&   r&   r'   ra      rb   c                 S   r]   )rO   r&   r^   r&   r&   r'   ra      rb   c                 S   r]   )rN   r&   r^   r&   r&   r'   ra      rb   c                 S   r]   )rP   r&   r^   r&   r&   r'   ra      rb   )r5   r6   rL   rM   rO   rN   rP   )r5   r6   r    cat)rA   r\   r&   r&   r'   
collate_fn   s   
zCirclePosesDataset.collate_fn)r*   r+   r+   r,   r,   r
   r-   r.   r/   r0   r1   r2   r3   )__name__
__module____qualname____doc__intfloatr    r   r?   rD   r   strr   Tensorr[   listrd   __classcell__r&   r&   rB   r'   r)   U   s^    	
9$,<r)   )typingr   r   numpyr   r    torch.utils.datar   -nemo.collections.multimodal.data.nerf.camerasr   +nemo.collections.multimodal.data.nerf.utilsr   r   r   r	   tensorrl   rj   boolr   r(   r)   r&   r&   r&   r'   <module>   s>   
	


7