Skip to main content

spatialrust_gpu/kernels/
euclidean_cluster.rs

1use spatialrust_core::{SpatialError, SpatialResult, TransferStats};
2
3pub use spatialrust_search::euclidean_cluster_roots as euclidean_cluster_roots_grid;
4
5/// Builds sparse cells and labels minimum-index components on the GPU.
6/// Returns roots on the host with explicit upload/readback accounting.
7/// Nonempty inputs require finite XYZ and a positive tolerance with a finite
8/// reciprocal. Positive infinity initializes one component on the device.
9pub fn euclidean_cluster_roots_gpu_with_receipt(
10    runtime: &crate::runtime::WgpuRuntime,
11    x: &[f32],
12    y: &[f32],
13    z: &[f32],
14    cluster_tolerance: f32,
15) -> SpatialResult<(Vec<u32>, TransferStats)> {
16    if x.len() != y.len() || x.len() != z.len() {
17        return Err(SpatialError::InvalidArgument("xyz arrays must have equal length".to_owned()));
18    }
19    if cluster_tolerance <= 0.0 || cluster_tolerance.is_nan() {
20        return Err(SpatialError::InvalidArgument("cluster_tolerance must be positive".to_owned()));
21    }
22    if x.is_empty() {
23        return Ok((Vec::new(), TransferStats::default()));
24    }
25
26    super::cluster_union::run(runtime, x, y, z, cluster_tolerance)
27}
28
29/// Connected-component roots via uniform-grid union-find.
30/// Sparse-grid construction and component union/find run on the GPU.
31pub fn euclidean_cluster_roots_gpu(
32    runtime: &crate::runtime::WgpuRuntime,
33    x: &[f32],
34    y: &[f32],
35    z: &[f32],
36    cluster_tolerance: f32,
37) -> SpatialResult<Vec<u32>> {
38    euclidean_cluster_roots_gpu_with_receipt(runtime, x, y, z, cluster_tolerance)
39        .map(|(roots, _)| roots)
40}