o
    oi                     @   sd   d dl Z d dlmZ d dlmZ d dlmZ G dd de jjZ	G dd de	Z
G d	d
 d
e	ZdS )    N)Tensor)KORNIA_CHECK_SHAPE)calc_ray_t_valsc                       sd   e Zd ZdZdZdZddeddf fdd	Zd
ededefddZ	dedededefddZ
  ZS )VolumeRendererzBase class for volume rendering.

    Implementation follows Ben Mildenhall et el. (2020) at https://arxiv.org/abs/2003.08934.
    g    _Bg|=   shiftreturnNc                    s   t    || _dS )zaInitialize the renderer.

        Args:
            shift: Size of far-field layer: int

        N)super__init___shift)selfr   	__class__ O/home/ubuntu/.local/lib/python3.10/site-packages/kornia/nerf/volume_renderer.pyr
   "   s   

zVolumeRenderer.__init__alphargbsc                 C   s`   t jd| | j dd}t j|| jdd}d|dd | jd d f< || }t j|| dd}|S )Nr   dim)shiftsdims.)torchcumprod_epsrollr   sum)r   r   r   transweightsrgbs_renderedr   r   r   _render,   s   zVolumeRenderer._render	densities	points_3dc                 C   s   t )N)NotImplementedError)r   r   r!   r"   r   r   r   forward7   s   zVolumeRenderer.forward)r   )__name__
__module____qualname____doc___huger   intr
   r   r    r$   __classcell__r   r   r   r   r      s    
"r   c                   @   *   e Zd ZdZdedededefddZdS )	IrregularRendererz1Renders 3D irregularly sampled points along rays.r   r!   r"   r   c                 C   s   t |}|dddf |dddf  }tj|jdd |j|jd| j}tj||d gdd}dt	d| |d   }| 
||S )	ae  Render 3D irregularly sampled points along rays.

        Args:
            rgbs: RGB values of points along rays :math:`(*, N, 3)`
            densities: Volume densities of points along rays :math:`(*, N)`
            points_3d: 3D points along rays :math:`(*, N, 3)`

        Returns:
            Rendered RGB values for each ray :math:`(*, 3)`

        .r   N)sizedtypedevice).Nr         )r   r   emptyshaper0   r1   fill_r)   catexpr    )r   r   r!   r"   t_valsdeltasfarr   r   r   r   r$   >   s    &zIrregularRenderer.forwardNr%   r&   r'   r(   r   r$   r   r   r   r   r-   ;       r-   c                   @   r,   )	RegularRendererz/Renders 3D regularly sampled points along rays.r   r!   r"   r   c                 C   s   t |g d t |ddg t |g d |jd }|d|d}|ddd	d	f |ddd	d	f  }tjj|dd
}dtd| |  }| ||S )ac  Render 3D regularly sampled points along rays.

        Args:
            rgbs: RGB values of points along rays :math:`(*, N, 3)`
            densities: Volume densities of points along rays :math:`(*, N)`
            points_3d: 3D points along rays :math:`(*, N, 3)`

        Returns:
            Rendered RGB values for each ray :math:`(*, 3)`

        )*N3r>   r?   r   r.      r   r   Nr   r2   )r   r4   reshaper   linalgnormr7   r    )r   r   r!   r"   num_ray_pointsdelta_3ddeltar   r   r   r   r$   W   s   
$zRegularRenderer.forwardNr;   r   r   r   r   r=   T   r<   r=   )r   kornia.corer   kornia.core.checkr   kornia.nerf.samplersr   nnModuler   r-   r=   r   r   r   r   <module>   s   "