
    #
3j0                     r    S SK rS SKJrJr  \" SSSS9S 5       r\" SSSSS9S	 5       r " S
 S\5      rS r	g)    N)njitprangezNi8(i8[:], i8[:], i8[:], i8[:], i8[:], f4[:, :, :], f4[:], f4[:, :], i8[:], i8)T)cachenogilc
                 B   UR                   S   n
[        R                  " S[        R                  5      nSnSnSU	-  nSX'   US-  nUR                   S   X'   US-  nXU'   US-  nUS-  nUS:  Ga-  US-  nX   nUS-  nX   nUS-  nX   nX_S4   nX_S4   n[	        U
5       H  nUUU4   UU'   UUU4   UU'   M     [	        US-   U5       HC  n[	        U
5       H1  n[        UU   UUU4   5      UU'   [        UU   UUU4   5      UU'   M3     ME     UU-
  U::  a  UX'   UX'   SX/'   SX?'   SXO'   SXo'   GO^UU-
  n[        R                  " U5      nUU   SUU   -  -   nUnUS-
  nUU:  a  UU:  a#  UUU4   U:  a  US-  nUU:  a  UUU4   U:  a  M  UU:  a#  UUU4   U:  a  US-  nUU:  a  UUU4   U:  a  M  UU:  a>  [	        U
5       H  nUUU4   nUUU4   UUU4'   UUUU4'   M     UU   nUU   UU'   UUU'   UU:  a  M  UUU4   U:  a  US-  nUn[        UU	-   [        UU	-
  U5      5      nUnUS-  nUnUS-  nUX'   US-  nUX'   US-  nUX'   US-  nUX'   US-  nUX'   US-  nUX'   US-  nUX'   UX'   UX/'   UX?'   UXO'   UXo'   US:  a  GM-  U$ )N     r              g      ?)shapenpemptyint64rangeminmaxargmax)i0_indsi1_inds	less_inds	more_inds
split_dimsboundssplit_valuespointsindicesmin_leaf_size	dimensionstack
stack_sizen_nodesmax_leaf_sizei_nodei1i0lohidilengths	split_dimsplit_valuejtemptemp_i_nodei_splitlessmores                                  O/home/wildlama/miniconda3/lib/python3.13/site-packages/pymatting/util/kdtree.py
_make_treer5      s    QIHHT288$EJG%M E!OJQE!OJ*qLG!OJ q.a
"a
a
AIAIy!A2q5MBqE2q5MBqE "
 rAvr"A9%BqE6!Q$<01BqE6!Q$<01 & # 7m# GO GO "I "I!#J#&L  2gG		'*IY-#	0B*BBK AQAa%!eq)| 4{ BFA !eq)| 4{ B!eq)| 4 CFA !eq)| 4 C q5"9-%ad|'-ad|q!t'+q!t .
 #*!*K!(GAJ!,GAJ a%" al#k1QG "},c"}2Dg.NOGDqLGDqLG !#E!OJ 'E!OJ $E!OJ !(E!OJ "E!OJ $E!OJ GO GO $I $I!*J#.L } q.@ N    zgvoid(i8[:], i8[:], i8[:], i8[:], i8[:], f4[:, :, :], f4[:], f4[:, :], f4[:, :], i8[:, :], f4[:, :], i8))r   r   parallelc                    UR                   S   n[        UR                   S   5       GH  nX   nX   nX   n[        R                  " S[        R                  5      nSnSUS'   SnUS:  Ga{  US-  nUU   nUU:  aT  Sn[        U5       H5  nUU   nU[        UUSU4   [        UUSU4   U5      5      -
  nUUU-  -  nM7     UUUS-
     :  a  Mk  UU   S:X  a  [        U U   UU   5       H  nSn[        U5       H  nUU   UUU4   -
  nUUU-  -  nM     Un[        US-
  SS5       H  nUU   U:  d  M  UnM     UU:  d  MZ  [        [        UUS-
  5      US5       H  nUUS-
     UU'   UUS-
     UU'   M     UUU'   UUU'   [        US-   U5      nM     ODUU   nUU   nUU   n UU    UU   :  a  UUU'   US-  nUUU'   US-  nOUUU'   US-  nUUU'   US-  nUS:  a  GM{  US-  nGM     g )Nr   r   r	   r   r   )r   r   r   r   r   r   r   r   )!r   r   r   r   r   r   r   r   query_pointsout_indicesout_distanceskr   i_queryquery_point	distancesr   r    n_neighborsr!   r$   distr)   pdpr*   distancedd
insert_posr.   r2   r3   r,   s!                                    r4   	_find_knnrG      s    QI ,,,Q/0"+!*	& rxx(a
 1n!OJ:&F ay)A#AAS1!5s6&!Q,;OQR7STTBBGOD * )K!O44 &!R'wv@A"H"9-(^fQTl: BG+ .
 "-J";?B;$Q<(2)*J <
 "A~!&s;A'>
B!OA)0QGAJ+4QU+;IaL "P
 /0
+08	*-&)+/1&=/ A4 !( (&v.	 y)L,@@(,E*%!OJ(,E*%!OJ )-E*%!OJ(,E*%!OJ} 1nB 	a
_ 1r6   c                   (    \ rS rSrSrSS jrS rSrg)KDTree   zKDTree implementationc                 L   UR                   [        R                  :X  d   eUR                  u  p4SX2-   S-
  U-  -  n[        R                  " U[        R
                  5      U l        [        R                  " U[        R
                  5      U l        [        R                  " U[        R
                  5      U l        [        R                  " U[        R
                  5      U l	        [        R                  " U[        R
                  5      U l
        [        R                  " USU4[        R                  5      U l        [        R                  " U[        R                  5      U l        UR                  5       U l        [        R                  " U5      R!                  [        R
                  5      U l        [%        U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R"                  U5
      U l        g)a3  Constructs a KDTree for given data points. The implementation currently only supports data type `np.float32`.

Parameters
----------
data_points: numpy.ndarray (of type `np.float32`)
    Dataset with shape :math:`n \times d`, where :math:`n` is the number of data points in the data set and :math:`d` is the dimension of each data point
min_leaf_size: int
    Minimum number of nodes in a leaf, defaults to 8

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> data_set = np.random.randn(100, 2)
>>> tree = KDTree(data_set.astype(np.float32))
r
   r   N)dtyper   float32r   r   r   r   r   r   r   r   r   r   copyshuffled_data_pointsarangeastypeshuffled_indicesr5   r"   )selfdata_pointsr   n_datar   	max_nodess         r4   __init__KDTree.__init__   sh   "   BJJ...'--&014FG	xx	2884xx	2884)RXX6)RXX6((9bhh7hh	1i8"**EHHY

;$/$4$4$6! "		& 1 8 8 B!LLLLNNNNOOKK%%!!
r6   c                    UR                   [        R                  :X  d   eU R                  R                  S   U:  a)  [        SU R                  R                  S    SU S35      eUR                  S   n[        R                  " X24[        R                  5      n[        R                  " X24[        R                  5      n[        U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  UUUU5        U R                   U   n[        R"                  " U5      nXe4$ )a  Query the tree

Parameters
----------
query_points: numpy.ndarray (of type `np.float32`)
    Data points for which the next neighbours should be calculated
k: int
    Number of neighbors to find

Returns
-------
distances: numpy.ndarray
    Distances to the neighbors
indices: numpy.ndarray
    Indices of the k nearest neighbors in original data array

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> data_set = np.random.randn(100, 2)
>>> tree = KDTree(data_set.astype(np.float32))
>>> tree.query(np.array([[0.5,0.5]], dtype=np.float32), k=3)
(array([[0.14234178, 0.15879704, 0.26760164]], dtype=float32), array([[29, 21, 20]]))
r   zNumber of data points (z4) is less than the number of neighbors requested (k=z<). Please provide a larger dataset or reduce the value of k.)rL   r   rM   rO   r   
ValueErrorr   r   rG   r   r   r   r   r   r   r   rR   sqrt)rS   r9   r<   n_querysquared_distancesr   r?   s          r4   queryKDTree.query  s6   4 !!RZZ///$$**1-1)$*C*C*I*I!*L)M  NB  CD  BE EM M 
 $$Q'HHg\2::>((G<2LLLLNNNNOOKK%%	
 ''0GG-.	!!r6   )
r   r   r   r   r   r"   rO   rR   r   r   N)   )__name__
__module____qualname____firstlineno____doc__rW   r^   __static_attributes__ r6   r4   rI   rI      s    ,
\9"r6   rI   c                 :    [        U 5      nUR                  X5      $ )a  Find k nearest neighbors in a data set. The implementation currently only supports data type `np.float32`.

Parameters
----------
data_points: numpy.ndarray (of type `np.float32`)
    Dataset with shape :math:`n \times d`, where :math:`n` is the number of data points in the data set and :math:`d` is the dimension of each data point
query_points: numpy.ndarray (of type `np.float32`)
    Data points for which the next neighbours should be calculated
k: int
    Number of neighbors to find

Returns
-------
distances: numpy.ndarray
    Distances to the neighbors
indices: numpy.ndarray
    Indices of the k nearest neighbors in original data array

Example
-------
>>> from pymatting import *
>>> import numpy as np
>>> data_set = np.random.randn(100, 2)
>>> knn(data_set.astype(np.float32), np.array([[0.5,0.5]], dtype=np.float32), k=2)
(array([[0.16233477, 0.25393516]], dtype=float32), array([[25, 17]]))
)rI   r^   )rT   r9   r<   trees       r4   knnrj   Y  s    6 +D::l&&r6   )
numpyr   numbar   r   r5   rG   objectrI   rj   rg   r6   r4   <module>rn      su     
 V^bjno~ p~B ow{  DH  SW  X` X`Dj"V j"Z'r6   