// Recursively checks the leaves of the head node until the best node is found
Recursively checks the leaves of the head node until the best node is found
Recursively checks the leaves of the head node until the best node is found
fn nearest_search_impl(
        &self,
        p: &KdtreePoint,
        searched_index: usize,
        best_distance_squared: &mut f64,
        best_leaf_found: &mut usize,
    ) {
        let node = &self.nodes[searched_index];
        let splitting_value = node.split_on;
        let point_splitting_dim_value = p.dims()[node.dimension];
        let (closer_node, farther_node) = if point_splitting_dim_value <= splitting_value {
            (node.left_node, node.right_node)
        } else {
            (node.right_node, node.left_node)
        };
        // Check the children of the closer node
        if let Some(closer_node) = closer_node {
            self.nearest_search_impl(p, closer_node, best_distance_squared, best_leaf_found);
        }
        let distance = squared_euclidean(p.dims(), node.point.dims());
        if distance < *best_distance_squared {
            *best_distance_squared = distance;
            *best_leaf_found = searched_index;
        }
        // If there's a farther node, check that one too just in case
        if let Some(farther_node) = farther_node {
            let distance_on_single_dimension =
                squared_euclidean(&[splitting_value], &[point_splitting_dim_value]);
            if distance_on_single_dimension <= *best_distance_squared {
                self.nearest_search_impl(p, farther_node, best_distance_squared, best_leaf_found);
            }
        }
    }
fn nearest_search_impl ( & self , p : & KdtreePoint , searched_index : usize , best_distance_squared : & mut f64 , best_leaf_found : & mut usize , ) { let node = & self . nodes [ searched_index ] ; let splitting_value = node . split_on ; let point_splitting_dim_value = p . dims ( ) [ node . dimension ] ; let ( closer_node , farther_node ) = if point_splitting_dim_value <= splitting_value { ( node . left_node , node . right_node ) } else { ( node . right_node , node . left_node ) } ; // Check the children of the closer node if let Some ( closer_node ) = closer_node { self . nearest_search_impl ( p , closer_node , best_distance_squared , best_leaf_found ) ; } let distance = squared_euclidean ( p . dims ( ) , node . point . dims ( ) ) ; if distance < * best_distance_squared { * best_distance_squared = distance ; * best_leaf_found = searched_index ; } // If there's a farther node, check that one too just in case if let Some ( farther_node ) = farther_node { let distance_on_single_dimension = squared_euclidean ( & [ splitting_value ] , & [ point_splitting_dim_value ] ) ; if distance_on_single_dimension <= * best_distance_squared { self . nearest_search_impl ( p , farther_node , best_distance_squared , best_leaf_found ) ; } } }
Recursively checks the leaves of the head node until the best node is found
Recursively checks the leaves of the head node until the best node is found
&self
p
&KdtreePoint
searched_index
usize
best_distance_squared
&mut f64
best_leaf_found
&mut usize