o
    iA                     @   s:  d dl Z d dlZd dlZd dlZddlmZ ee dre jZn		 dd Zdd ZdZ	d	d
 Z
edd3ddZd4ddZejddd5ddZdejfddZdejfddZdd Zd6ddZd d! Zd7d"d#Zejddd$d% Zd7d&d'Zejddd(d) Zd*d+ Zd,d- Zd.d/ Z		d8dddd0d1d2ZdS )9    N   )_get_inttypelcmc                 C   s   t || t| |  S N)absmathgcd)ab r   X/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/scipy/ndimage/_pba_2d.py_lcm   s   r   c                  G   st   t | }tdd | D std|dkrdS t| d }|dkr%t|S td|D ]}t| | }t||}q*|S )Nc                 s   s    | ]	}t |tjV  qd S r   )
isinstancenumbersIntegral).0r	   r   r   r   	<genexpr>       zlcm.<locals>.<genexpr>zall arguments must be integersr   r   )lenall	TypeErrorintr   ranger   )integersnargsresixr   r   r   r      s   a  

// MARKER is used to mark blank pixels in the texture.
// Any uncolored pixels will have x = MARKER.
// Input texture should have x = MARKER for all pixels other than sites
#define MARKER      {marker}
#define BLOCKSIZE   {block_size_2d}
#define pixel_int2_t {pixel_int2_t}                // typically short2 (int2 for images with > 32k pixels per side)
#define make_pixel(x, y)  {make_pixel_func}(x, y)  // typically make_short2 (make_int2 images with > 32k pixels per side

c                 C   s,   | t jkr	d}|S | t jkrd}|S td)z<use a minimum value that is appropriate to the integer dtype i   z8expected int_dtype to be either cupy.int16 or cupy.int32)cupyint16int32
ValueError)	int_dtypemarkerr   r   r   _init_marker,   s   
	
r%   T@   r   short2c                 C   s~   d| }t j| |||d}tjtjtd}ttj|dd}d| }W d    n1 s4w   Y  ||7 }|S )Nmake_)block_size_2dr$   pixel_int2_tmake_pixel_funccudazpba_kernels_2d.hrt
)	pba2d_defines_templateformatospathjoindirname__file__open	readlines)r)   r$   r*   r+   
pba2d_codekernel_directoryfpba2d_kernelsr   r   r   get_pba2d_src<   s   r<   Fc                 C   s.   | rt jj }t jj|}t|d S dS )NwarpSize    )r   r,   runtime	getDevicegetDevicePropertiesr   )check_warp_sizedevdevice_propertiesr   r   r   _get_block_sizeN   s
   rE   )for_each_devicec                 C   s$   d| d| d}t jdd|ddS )a=  Pack coordinates into array of type short2 (or int2).

    This kernel works with 2D input data, `arr` (typically boolean).

    The output array, `out` will be 3D with a signed integer dtype.
    It will have size 2 on the last axis so that it can be viewed as a CUDA
    vector type such as `int2` or `float2`.
    z&
    if (arr[i]) {
        out[2*i] = z;
        out[2*i + 1] = z;
    } else {
        int shape_1 = arr.shape()[1];
        int _i = i;
        int ind_1 = _i % shape_1;
        _i /= shape_1;
        out[2*i] = ind_1;   // out.x
        out[2*i + 1] = _i;  // out.y
    }
    z	raw B arrz	raw I outz--std=c++11	in_params
out_params	operationoptions)r   ElementwiseKernel)int_typer$   coder   r   r   _get_pack_kernelW   s   
rP   c                 C   s   | j dkr	tdtddg|gd d}tj| jd |d}|jd| j ks*J t|tjkr2dnd	|d
}|| || jd t	|
|}|S )N   zonly 2d arr supportedr   ynamesformatsrQ   dtypeshortr   )rN   r$   size)ndimr"   r   rX   zerosshaper[   rP   r    squeezeview)arrr$   r#   
int2_dtypeoutpack_kernelr   r   r   
_pack_int2v   s   
re   c                 C   s&   |  || jd }|r| }|S )NrV   )r`   reshaper^   copy)img	make_copyr#   tempr   r   r   _unpack_int2   s   rk   c                 C   s<   | \}}||ks||krd|| fd|| ff}|S d }|S )Nr   r   )r^   padded_size
block_sizeorig_syorig_sxpadding_widthr   r   r   _determine_padding   s   rq   rc   c              
   C   sB   d}|sd| }t | D ]}|| d| d| d| d7 }q|S )N _raw_z shape_z = z	.shape()[z];
r   )r\   rN   var_nameraw_varrO   r   r   r   r   _generate_shape   s   "rw   c              
   C   sR   | d}t | d ddD ]}|| d| d| d| d7 }q|| d	7 }|S )
Nz	 _i = i;
r   r   z ind_z = _i % shape_z;
_i /= shape_z;
z ind_0 = _i;rt   )r\   rN   rO   jr   r   r   _generate_indices_ops   s
   
"rz   c                 C   s:   t d| d|d}|td| d7 }|d|  d| d7 }|S )NrQ   distr\   rN   ru   rv   r\   rN   z
    z
 tmp;
    z sq_dist;
    tmp = y[i] - ind_0;
    sq_dist = tmp * tmp;
    tmp = x[i] - ind_1;
    sq_dist += tmp * tmp;
    dist[i] = sqrt(static_cast<F>(sq_dist));
    rw   rz   )rN   dist_int_typeraw_out_varrO   r   r   r   _get_distance_kernel_code   s   
	r   c                 C   s    t | |dd}tjdd|ddS )AReturns kernel computing the Euclidean distance from coordinates.Tr   zraw I y, raw I x
raw F distrG   rH   )r   r   rM   )rN   r   rK   r   r   r   _get_distance_kernel   s   r   c                 C   s,   t d| d|d}|td| d7 }|d7 }|S )NrQ   r{   r|   r}   z
    F tmp;
    F sq_dist;
    tmp = static_cast<F>(y[i] - ind_0) * sampling[0];
    sq_dist = tmp * tmp;
    tmp = static_cast<F>(x[i] - ind_1) * sampling[1];
    sq_dist += tmp * tmp;
    dist[i] = sqrt(sq_dist);
    r~   )rN   r   rO   r   r   r   _get_aniso_distance_kernel_code   s   	r   c                 C   s   t | dd}tjdd|ddS )r   Tr   z raw I y, raw I x, raw F samplingr   rG   rH   )r   r   rM   )rN   rK   r   r   r   _get_aniso_distance_kernel   s   r   c                 C   sP   g }|s|s| d | r|s| d |r|s| d |r&td|dS )z1Raise a RuntimeError if the arguments are invalidz<at least one of return_distances/return_indices must be Truez6return_distances must be True if distances is suppliedz2return_indices must be True if indices is suppliedz, N)appendRuntimeErrorr3   )distances_outindices_outreturn_distancesreturn_indices
error_msgsr   r   r   _distance_tranform_arg_check   s   
r   c                 C   s.   | j |kr	td| j|krtd| d S )Nzdistances array has wrong shapez!distances array must have dtype: )r^   r   rX   )	distancesr^   rX   r   r   r   _check_distances   s   

r   c                 C   sD   | j |kr	td| jjdvrtd| jj|k r td| d S )Nzindices array has wrong shapeiuz(indices array must have an integer dtypez#indices dtype must have itemsize > )r^   r   rX   kinditemsize)indicesr^   r   r   r   r   _check_indices   s   
r   )float64_distancesr   r   c          5   	   C   s  t |tj}	t |tj}
t|
|	|| t|}|d u rRtt| j| | }|| }tdt	|| |}dt
t| }|| dkrItdt	t	||d}n2tdd |D r_td|\}}}t|d dkrqtd	t||||}tt| j| | }||| krtd
| ||| krtd| ||| krtd| |||fD ]}|| dkrtd| d|||f dqt| j}|dkrtj}d}n|dkrtdd d| j tj}d}t|}| j\}}t| j||}|d urtj| |ddd} | jd }t| ||d}t|}tddg|gd d}tjd| | f|d}tjt|||dd}|d}|d } |d!}!|d"}"|d#}#|d$}$|d u rp|d%}%|d&}&|d'}'n|d(}%|d)}&|d*}'|ddf}(t||(d  |df})|| }*||)|(||||*f | |)|(||||*f |!|)|(||||*f |"|)|(|||||*f |ddf}(t||(d  |df})|| }+|d u rd+},nttt|}|d |d f},|%|)|(||||+f|,  |#|)|(||||+f |}-|-dkr+t||(d  |-d f})|&|)|(||||||- f|,  |-d }-|-dkst||(d  |f})|$|)|(||||f ||df}(t||(d  ddf})|'|)|(|||f|,  t|d,|d-}|d |d |df }.|d |d |df }/d+}0|r|rtj ntj!}1|
rt"||/j|1 ntj#|/j|1d}t$d.d |/jD }2|2d/k rd0nd1}3|d u rt%t&||3d2}4|4|/|.||j'd3 nt(t&|d4}4tj)||1d}|4|/|.|||j'd3 |0|f }0|r|	rt*|| j+f| j |.jj, |/|d5< |.|d6< n	tj-|/|.fdd7}|0|f }0|0S )8Nr   rQ   r   zerror in setting default m2c                 s   s    | ]}|d k V  qdS )r   Nr   )r   pr   r   r   r   ,  s    z_pba_2d.<locals>.<genexpr>z((m1, m2, m3) in blockparams must be >= 1gh㈵>zm2 must be a power of 2z0m1 too large. must be <= padded arr.shape[0] // z0m2 too large. must be <= padded arr.shape[1] // z0m3 too large. must be <= padded arr.shape[1] // zLargest dimension of image (z<) must be evenly disivible by each element of block_params: .i   r'   i   zmaximum axis size of z  exceeded, for image with shape int2constant)modeconstant_values)r$   r#   r   rR   rS   rW   )r)   r$   r*   )rO   kernelFloodDownkernelFloodUpkernelPropagateInterbandkernelUpdateVerticalkernelCreateForwardPointerskernelDoubleToSingleListkernelProximatePointskernelMergeBandskernelColor kernelProximatePointsWithSpacingkernelMergeBandsWithSpacingkernelColorWithSpacingr   F)ri   r#   c                 s   s    | ]	}|d  d V  qdS )r   rQ   Nr   )r   sr   r   r   r     r   l        r   	ptrdiff_t)rN   r   rZ   )rN   )r   .)r   .)axis).r   r   ndarrayr   rE   r   ceilmaxr^   minfloorlog2r   anyr"   r   r    r!   r%   rq   padre   
zeros_likerX   empty	RawModuler<   get_functiontuplemapfloatrk   float64float32r   r]   sumr   r   r[   r   asarrayr   r\   r   stack)5ra   samplingr   r   block_paramsrB   r   r   r   indices_inplace
dt_inplacerm   rl   m1m2m3multiplem	shape_maxr#   pixel_int2_typer$   rn   ro   rp   r[   	input_arroutputrb   marginpba2dr   r   r   r   r   r   r   r   r   blockgrid	bandSize1	bandSize2sampling_argsnoBandr   rR   vals	dtype_outmax_possible_distr   distance_kernelr   r   r   _pba_2d  sj  































r   )r&   r   r'   )F)r   )rc   T)T)NTFNF)r   r   r1   r   _utilr   hasattrr   r   r/   r%   memoizer<   rE   rP   r    re   rk   rq   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sH    


	
	





