a
    d                     @   sd   d dl Z d dlmZ ddlmZ e  dd Ze  dd Zd	d
 Ze  dd Z	dd Z
dS )    N)create_meshgrid   )	warp_kptsc                 C   s>   |j \}}}|||| dddd}d| |  < | S )z5For megadepth dataset, zero-padding exists in images.r      r   )shapereshapeZ	unsqueezerepeatbool)Zgrid_ptmasknhw r   o/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/feature/loftr/utils/supervision.pymask_pts_at_padded_regions
   s     r   c           !         s,  | d j }| d j\}}}}| d j\}}}}|d d d d| v r^| d dddf  n}	d| v r| d dddf  n}
tfd	d
||||g\}}}}t||d|d|| d|dd}|	| }t||d|d|| d|dd}|
| }d| v r*t|| d }t|| d }t|| d | d | d | d | d \}}t|| d | d | d | d | d \}}||
 }||	 }|ddddddf  	 }|d |d |  }|ddddddf  	 }|d |d |   dd }d|||||< d ||||< t
j fddt|D dd}|t
j|| |dd |dk}d|dddf< t
j||| || |d}t
|dk\}}|||f } d|||| f< | d|i t|dkrt
jdg|d}t
jdg|d}t
jdg|d} | ||| d | ||d  dS )!a  
    Update:
        data (dict): {
            "conf_matrix_gt": [N, hw0, hw1],
            'spv_b_ids': [M]
            'spv_i_ids': [M]
            'spv_j_ids': [M]
            'spv_w_pt0_i': [N, hw0, 2], in original image resolution
            'spv_pt1_i': [N, hw1, 2], in original image resolution
        }

    NOTE:
        - for scannet dataset, there're 3 kinds of resolution {i, c, f}
        - for megadepth dataset, there're 4 kinds of resolution {i, i_resize, c, f}
    Zimage0Zimage1LOFTR
RESOLUTIONr   scale0Nscale1c                    s   |   S )Nr   )x)scaler   r   <lambda>-       zspvs_coarse.<locals>.<lambda>Fr   r   Zmask0Zmask1Zdepth0Zdepth1ZT_0to1ZK0ZK1ZT_1to0.r   .r   c                 S   s0   | d dk | d |k | d dk  | d |k S )Nr   r   r   r   )ptr   r   r   r   r   out_bound_maskJ   s    z#spvs_coarse.<locals>.out_bound_maskc                    s   g | ]\}} | | qS r   r   ).0_bZ_i)nearest_index0r   r   
<listcomp>P   r   zspvs_coarse.<locals>.<listcomp>)Zdim)deviceconf_matrix_gt)Z	spv_b_idsZ	spv_i_idsZ	spv_j_ids)spv_w_pt0_i	spv_pt1_i)r"   r   mapr   r   r	   r   r   roundlongtorchstack	enumerateZarangezeroswhereupdatelenZtensor)!dataconfigr"   N_ZH0ZW0ZH1ZW1r   r   Zh0Zw0h1Zw1Z
grid_pt0_cZ
grid_pt0_iZ
grid_pt1_cZ
grid_pt1_iw_pt0_iZw_pt1_iZw_pt0_cZw_pt1_cZw_pt0_c_roundZnearest_index1Zw_pt1_c_roundr   Z	loop_backZcorrect_0to1r#   b_idsi_idsj_idsr   )r    r   r   spvs_coarse   sN    
$$"&&
**"" "r9   c                 C   sR   t t| d dkrtd| d d }| dv r@t| | ntd| d S )Ndataset_namer   z'Do not support mixed datasets training!r   ZscannetZ	megadepthzUnknown data source: )r/   set
ValueErrorlowerr9   r0   r1   Zdata_sourcer   r   r   compute_supervision_coarsei   s    r@   c           
      C   s   | d | d  }}|d d d }|d d d }| d | d	 | d
   }}}d| v rf|| d |  n|}|||f |||f  | | }	|  d|	i dS )zI
    Update:
        data (dict):{
            "expec_f_gt": [M, 2]}
    r$   r%   r   r   r   ZFINE_WINDOW_SIZEr   r6   r7   r8   r   r   
expec_f_gtN)r.   )
r0   r1   r5   Zpt1_ir   Zradiusr6   r7   r8   rA   r   r   r   	spvs_finev   s    	 rB   c                 C   s,   | d d }|  dv r$t| | ntd S )Nr:   r   r;   )r>   rB   NotImplementedErrorr?   r   r   r   compute_supervision_fine   s    rD   )r)   Zkornia.utilsr   Zgeometryr   Zno_gradr   r9   r@   rB   rD   r   r   r   r   <module>   s   


S
