Skip to main content

knearest

Function knearest 

Source
pub fn knearest(
    xyz: &[[f64; 3]],
    simbox: &Cell,
    k: usize,
    mask: Option<&[bool]>,
    cell_hint: Option<f64>,
) -> Result<Vec<Neighbors>, Error>
Expand description

k-nearest neighbours of every point (or of the masked subset).

mask[i] == false drops point i from both sources and candidates. mask is None or length n (Error::MaskLen otherwise). cell_hint is the target cell edge; None uses 3.0 in the same units as the box. Each row has min(k, n_active - 1) entries.

Fold, bin, then grow a rectangular index box. An orthorhombic cell is used as stored. A restricted triclinic cell is tilt-reduced first. A general orientation is Minkowski-reduced. Distances are Cell::dist2_shifted plus Cell::lattice_shift. The walk does not stamp unique cells: occupants of one bin can need different images. A full heap stops when its worst distance is at most the nearest unvisited face.

use linkcell::{knearest, Cell};

let sim = Cell::ortho(10.0, 10.0, 10.0)?;
let xyz = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let mask = [true, false, true];
let rows = knearest(&xyz, &sim, 1, Some(&mask), None)?;
assert!(rows[1].indices.is_empty());
assert_eq!(rows[0].indices, vec![2]);
assert_eq!(rows[2].indices, vec![0]);