o
    ߥi,                     @   s:   d dl Z d dlZd dlZ	d
ddZdd Zddd	ZdS )    Ncudac                 C   s  d}t | d |d ||}t | d |d ||}t | d |d ||}tj|||gtjd}	t   t|D ]{\}
}t|D ]r\}}t|D ]i\}}t |||\}}}|	dd}|	dd}|	dd}t j
|||gdd}||}||	t|t|t|   }||	|
| |
| t| || || t| || || t| f< qPqHq@W d    |	S 1 sw   Y  |	S )N@   r         )dtype)dim)torchlinspacesplitnpzerosfloat32no_grad	enumeratemeshgridreshapecattolendetachcpunumpy)	bound_min	bound_max
resolution
query_funcdeviceNXYZuxixsyiyszizsxxyyzzptsval r.   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/surface_recon_common/utils.pyextract_fields   s>   

.
r0   c                 C   s   t d| t| ||||}t||\}}|   }	|    }
||d  |	|
 d d d f  |
d d d f  }||fS )Nzthreshold: {}      ?)printformatr0   mcubesmarching_cubesr   r   r   )r   r   r   	thresholdr   r   r"   vertices	trianglesb_max_npb_min_npr.   r.   r/   extract_geometry(   s   
r;   Fc                 C   s  |d }|t j|ddd }t |d}t t |dd df |gd}|rJt jdd|  d	d|  |d
|}|t|j	d d |g }nt 
t|j	d d |g |}| }t j||dd}t t |d |d }	t |j	d d t | |}
t |	|
gd}|j	d |j	d |j	d g}t |d|d|}t | d|d|}|d |d  }t |dk t ||}||d  | }|d ||d |d    }|S )Ngh㈵>r   T)keepdim.r   g        g      ?r1   )steps)rightr   r   ).r   ).r   )r	   sumcumsumr   
zeros_liker
   r   expandlistshaperand
contiguoussearchsortedmaxmin	ones_likestackgather	unsqueezewhere)binsweights	n_samplesdetr   pdfcdfr"   indsbelowaboveinds_gmatched_shapecdf_gbins_gdenomtsamplesr.   r.   r/   
sample_pdf5   s4   " $ r_   )r   )Fr   )r4   r   r   r	   r0   r;   r_   r.   r.   r.   r/   <module>   s   
