pub fn knn_within(
positions: &[[f32; 3]],
query: &AtomSelection,
targets: &AtomSelection,
k: usize,
options: KnnOptions,
backend: SpatialBackend,
periodic: Option<&PeriodicBox>,
context: &ExecutionContext,
) -> Result<KnnResult, SpatialError>Expand description
Finds the k nearest target atoms of every query atom.
Rows follow the query selection’s order. Within a row neighbours increase in
distance, equal distances ordering by atom index, and unfilled slots hold
NO_NEIGHBOR with an infinite squared distance. Periodic queries report
exact minimum-image distances; an unbounded periodic search looks through
two lattice shells of images, which covers every wrapped target of a cell
that is not extremely skewed, and a max_distance widens it exactly.
BruteForce scans every target serially and exists as a reference; every
other backend uses the parallel k-d tree, since a cell list has no
radius-free nearest query. Distances are f32; a target whose f64 distance
ties the k-th at the f32 rounding boundary may be ranked either way.
§Errors
Returns an error for an invalid max_distance, out-of-range atom indices,
memory admission failure or cancellation.