o
    }o™i†  ã                   @   s   d dl Z G dd„ dƒZdS )é    Nc                   @   sŠ   e Zd ZdZdejddfdd„Zdejfdd„Zdejfd	d
„Z	dejfdd„Z
dejdejfdd„Zdejdejdejdefdd„ZdS )ÚDeepMarchingTetrahedraaR  
    Class for Deep Marching Tetrahedra (DMTet).

    Attributes:
        device (torch.device): Device to place the tensors.
        triangle_table (Tensor): Lookup table for the triangles.
        num_triangles_table (Tensor): Table for the number of triangles.
        base_tet_edges (Tensor): The base edges for the tetrahedrons.
    ÚdeviceÚreturnNc                 C   s(   || _ |  ¡ | _|  ¡ | _|  ¡ | _dS )zInitialize DMTet instance with the given device.

        Args:
            device (torch.device): The device to place the tensors on.
        N)r   Ú_create_triangle_tableÚtriangle_tableÚ_create_num_triangles_tableÚnum_triangles_tableÚ_create_base_tet_edgesÚbase_tet_edges)Úselfr   © r   úk/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/modules/nerf/geometry/dmtet.pyÚ__init__   s   

zDeepMarchingTetrahedra.__init__c                 C   st   t jg d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d	¢g d
¢g d¢g d¢g d¢g d¢g d¢g d¢gt j| jdS )zpCreate the lookup table for triangles.

        Returns:
            Tensor: The triangle lookup table.
        )éÿÿÿÿr   r   r   r   r   )é   r   é   r   r   r   )é   r   é   r   r   r   )r   r   r   r   r   r   )r   r   é   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   ©Údtyper   ©ÚtorchÚtensorÚlongr   ©r   r   r   r   r   '   s*   ðìz-DeepMarchingTetrahedra._create_triangle_tablec                 C   ó   t jg d¢t j| jdS )zwCreate the table for number of triangles.

        Returns:
            Tensor: The number of triangles table.
        )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   D   ó   z2DeepMarchingTetrahedra._create_num_triangles_tablec                 C   r   )z{Create the base edges for the tetrahedrons.

        Returns:
            Tensor: The base edges for tetrahedrons.
        )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   L   r   z-DeepMarchingTetrahedra._create_base_tet_edgesÚ	edges_ex2c                 C   sŒ   t  ¡ 2 |dd…df |dd…df k ¡ }|jdd}t j||dd}t j|d| dd}W d  ƒ n1 s9w   Y  t  ||gd¡S )zSort the given edges.

        Args:
            edges_ex2 (Tensor): The edges to be sorted.

        Returns:
            Tensor: The sorted edges.
        Nr   r   ©Údim)ÚinputÚindexr    r   )r   Úno_gradr   Ú	unsqueezeÚgatherÚstack)r   r   ÚorderÚaÚbr   r   r   Ú_sort_edgesT   s   
	$üz"DeepMarchingTetrahedra._sort_edgesÚ	positionsÚsdf_nÚtet_fx4c              	   C   sf  t  ¡ € |dk}|| d¡  dd¡}t  |d¡}|dk|dk @ }|| }|| dd…| jf  dd¡}|  |¡}t j|ddd\}	}
|	 ¡ }	||	 d¡  dd¡ d¡dk}t j|	j	d t j| j
d	d }t j| ¡ t j| j
d	||< ||
 }
|	| }W d  ƒ n1 s‡w   Y  || d¡  ddd
¡}|| d¡  ddd¡}|dd…df  d9  < |jddd}t  |dg¡| }||  d¡}|
 dd¡}
t  dt jdt j| j
d	¡}|| | d¡  d¡}| j| }t jt j|
|dk d| j||dk  dd…dd
…f d dd
¡t j|
|dk d| j||dk  dd…dd…f d dd
¡fdd}||fS )a_  
        Process the provided data to generate vertices and faces.

        Args:
            positions (Tensor): Position tensor with shape [N, 3].
            sdf_n (Tensor): SDF tensor with shape [N].
            tet_fx4 (Tensor): Tetrahedron faces tensor with shape [F, 4].

        Returns:
            tuple: Vertices and faces tensors.
        r   r   r   Nr   T)r    Úreturn_inverser   r   r   )Úkeepdimé   )r!   r    r"   r   )r   r#   ÚreshapeÚsumr
   r*   Úuniquer   ÚonesÚshaper   ÚarangeÚflipÚpowr$   r   Úcatr%   r   )r   r+   r,   r-   Úocc_nÚocc_fx4Úocc_sumÚ
valid_tetsÚ	all_edgesÚunique_edgesÚidx_mapÚ
mask_edgesÚmappingÚinterp_vÚedges_to_interpÚedges_to_interp_sdfÚdenominatorÚvertsÚv_idÚtetindexÚnum_trianglesÚfacesr   r   r   Ú__call__e   sV   

 
î

 ý
 ýöózDeepMarchingTetrahedra.__call__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚTensorr   r   r	   r*   ÚtuplerL   r   r   r   r   r      s    
$r   )r   r   r   r   r   r   Ú<module>   s   