# Path: pyparticles/geometry/intersection.py
# def box_intersects_sphere( b_min , b_max , c , r ):
#     """
#     return True if the box defined by the opposite vertices *n_max*, *b max* intersect the sphere centred in *c* with a radius *r*
#     """
#     
#     r2 = r**2.0
#     dmin = 0.0
#     
#     if c[0] < b_min[0] :
#         dmin += ( c[0] - b_min[0] )**2.0
#     elif c[0] > b_max[0]:
#         dmin += ( c[0] - b_max[0] )**2.0
#     
#     if c[1] < b_min[1] :
#         dmin += ( c[1] - b_min[1] )**2.0
#     elif c[1] > b_max[1]:
#         dmin += ( c[1] - b_max[1] )**2.0
#     
#     if c[2] < b_min[2] :
#         dmin += ( c[2] - b_min[2] )**2.0
#     elif c[2] > b_max[2]:
#         dmin += ( c[2] - b_max[2] )**2.0
#     
#     return dmin <= r2
# 
# Path: pyparticles/geometry/dist.py
# def distance( x , y ):
#     """
#     return the euclideian distance between *x* and *y*
#     """
#     return np.sqrt( np.sum( (x-y)**2.0 ) )


import pyparticles.pset.particles_set as ps
import multiprocessing as mpr
import numpy as np
from pyparticles.geometry.intersection import box_intersects_sphere
from pyparticles.geometry.dist import distance
from collections import deque
            # down
            indx[:] = indx * self.__down
        elif pset.X[i,2] >= self.__ref_vertex[2] + self.__edge_len/2.0 and pset.X[i,2] < self.__ref_vertex[2] + self.__edge_len :
            # up
            indx[:] = indx * self.__up
        
        ix = np.sum( indx )
        self.__tree[ix].insert_particle( pset , i )
            
        #print ( indx )
        #print("")
        #print ( "ix %d" % ix )
        #print ( "jj %d" % jj )
        #print ( "----------" )
        
        #if jj != ix :
        #    print ("Fatal!!!!")
        #    exit()
        #
        return


    def search_neighbour( self , cand_queue , res_list , pset , X , r ):
        """
        Search the elements included in the volume centred in *X* with the radius *r* and append the results in the list *res_list*.
            *res_list* contains the indicies of the particles included in the the sphere.
        """
        while len(cand_queue) :
            tree = cand_queue.pop()
            

# Path: pyparticles/geometry/intersection.py
# def box_intersects_sphere( b_min , b_max , c , r ):
#     """
#     return True if the box defined by the opposite vertices *n_max*, *b max* intersect the sphere centred in *c* with a radius *r*
#     """
#     
#     r2 = r**2.0
#     dmin = 0.0
#     
#     if c[0] < b_min[0] :
#         dmin += ( c[0] - b_min[0] )**2.0
#     elif c[0] > b_max[0]:
#         dmin += ( c[0] - b_max[0] )**2.0
#     
#     if c[1] < b_min[1] :
#         dmin += ( c[1] - b_min[1] )**2.0
#     elif c[1] > b_max[1]:
#         dmin += ( c[1] - b_max[1] )**2.0
#     
#     if c[2] < b_min[2] :
#         dmin += ( c[2] - b_min[2] )**2.0
#     elif c[2] > b_max[2]:
#         dmin += ( c[2] - b_max[2] )**2.0
#     
#     return dmin <= r2
# 
# Path: pyparticles/geometry/dist.py
# def distance( x , y ):
#     """
#     return the euclideian distance between *x* and *y*
#     """
#     return np.sqrt( np.sum( (x-y)**2.0 ) )


# Path: pyparticles/pset/octree.py
import pyparticles.pset.particles_set as ps
import multiprocessing as mpr
import numpy as np
from pyparticles.geometry.intersection import box_intersects_sphere
from pyparticles.geometry.dist import distance
from collections import deque

            # down
            indx[:] = indx * self.__down
        elif pset.X[i,2] >= self.__ref_vertex[2] + self.__edge_len/2.0 and pset.X[i,2] < self.__ref_vertex[2] + self.__edge_len :
            # up
            indx[:] = indx * self.__up
        
        ix = np.sum( indx )
        self.__tree[ix].insert_particle( pset , i )
            
        #print ( indx )
        #print("")
        #print ( "ix %d" % ix )
        #print ( "jj %d" % jj )
        #print ( "----------" )
        
        #if jj != ix :
        #    print ("Fatal!!!!")
        #    exit()
        #
        return


    def search_neighbour( self , cand_queue , res_list , pset , X , r ):
        """
        Search the elements included in the volume centred in *X* with the radius *r* and append the results in the list *res_list*.
            *res_list* contains the indicies of the particles included in the the sphere.
        """
        while len(cand_queue) :
            tree = cand_queue.pop()
            

            if distance( pset.X[tree.particle,:] , X ) <= r :