threecrate-algorithms 0.9.0

Algorithms for 3D point cloud and mesh processing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//! Nearest neighbor search implementations

use std::cmp::Ordering;
use threecrate_core::{NearestNeighborSearch, Point3f, Result};

/// Sentinel used in place of a child index to mean "no child".
const NIL: u32 = u32::MAX;

/// KD-Tree node stored in a flat, contiguous array.
///
/// Children are referenced by index into the owning `Vec<KdNode>` rather than
/// through `Box` pointers. Keeping every node in one allocation makes traversal
/// cache-friendly — neighbour search is the dominant cost in normal estimation
/// and ICP correspondence, so a contiguous layout directly moves those numbers.
#[derive(Debug, Clone)]
struct KdNode {
    point: Point3f,
    original_index: usize, // index into the original input slice
    left: u32,             // child index, or NIL
    right: u32,            // child index, or NIL
    axis: u8,              // splitting axis: 0=x, 1=y, 2=z
}

/// Read-only view of one kd-tree node, for code that walks the tree itself
/// (e.g. the GPU search in `threecrate-gpu`). See [`KdTree::flat_nodes`].
#[derive(Debug, Clone, Copy)]
pub struct FlatKdNode {
    /// The point stored at this node.
    pub point: Point3f,
    /// Index of that point in the slice the tree was built from.
    pub index: usize,
    /// Position of the left child in [`KdTree::flat_nodes`], or [`KdTree::NO_CHILD`].
    pub left: u32,
    /// Position of the right child in [`KdTree::flat_nodes`], or [`KdTree::NO_CHILD`].
    pub right: u32,
    /// Splitting axis: 0 = x, 1 = y, 2 = z.
    pub axis: u8,
}

/// Efficient KD-Tree implementation for nearest neighbor search.
///
/// Nodes live in a single contiguous `Vec` (`nodes`); children are referenced by
/// index. `root` is the index of the tree root (always `0` when non-empty).
pub struct KdTree {
    nodes: Vec<KdNode>,
    root: Option<u32>,
    points: Vec<Point3f>, // Keep original points for reference
}

impl KdTree {
    /// Child value in [`FlatKdNode`] meaning "no child".
    pub const NO_CHILD: u32 = NIL;

    /// The tree's nodes in their stored order. The root is the first node (when
    /// the tree is not empty), and children are referenced by position in this
    /// sequence. A query walks it like any binary kd-tree: go to the child on
    /// the query's side of the split, and visit the other child only if the
    /// split plane is closer than the current best match.
    pub fn flat_nodes(&self) -> impl ExactSizeIterator<Item = FlatKdNode> + '_ {
        self.nodes.iter().map(|node| FlatKdNode {
            point: node.point,
            index: node.original_index,
            left: node.left,
            right: node.right,
            axis: node.axis,
        })
    }

    /// Create a new KD-tree from a slice of points.
    ///
    /// Points with a NaN or infinite coordinate are left out of the tree and are
    /// never returned as neighbors (indices of the remaining points still refer
    /// to `points`). Organized clouds store invalid returns as NaN; a NaN split
    /// value would send every query down one side and make the finite points on
    /// the other side unreachable.
    pub fn new(points: &[Point3f]) -> Result<Self> {
        Self::from_points(points.to_vec())
    }

    /// Like [`KdTree::new`], but takes ownership of the points instead of
    /// copying them (useful when the caller already built a converted copy).
    pub fn from_points(points: Vec<Point3f>) -> Result<Self> {
        let mut points_with_indices: Vec<(Point3f, usize)> = points
            .iter()
            .enumerate()
            .filter(|(_, point)| point.coords.iter().all(|c| c.is_finite()))
            .map(|(i, &point)| (point, i))
            .collect();

        if points_with_indices.is_empty() {
            return Ok(Self {
                nodes: Vec::new(),
                root: None,
                points,
            });
        }

        // Every slot is overwritten by `build_tree`; the placeholder only lets
        // us hand out disjoint `&mut` sub-slices to parallel subtree builds.
        let placeholder = KdNode {
            point: Point3f::origin(),
            original_index: 0,
            left: NIL,
            right: NIL,
            axis: 0,
        };
        let mut nodes: Vec<KdNode> = (0..points_with_indices.len())
            .map(|_| placeholder.clone())
            .collect();
        Self::build_tree(&mut nodes, 0, &mut points_with_indices, 0);

        Ok(Self {
            nodes,
            root: Some(0),
            points,
        })
    }

    /// Recursively build the KD-tree for `points` into `nodes`, whose first slot
    /// sits at absolute array index `base`.
    ///
    /// Slots are filled in pre-order: the subtree root goes in `nodes[0]`, the
    /// left subtree (`median` nodes) in `nodes[1..=median]`, and the right
    /// subtree after it. Because each subtree's slot range is known up front,
    /// the two halves are disjoint slices and large subtrees build in parallel.
    fn build_tree(nodes: &mut [KdNode], base: u32, points: &mut [(Point3f, usize)], depth: usize) {
        /// Below this size, spawning rayon tasks costs more than it saves.
        const PARALLEL_THRESHOLD: usize = 4096;

        let axis = depth % 3;
        let len = points.len();
        let median = (len - 1) / 2;

        // `select_nth_unstable_by` is introselect: O(n) and, unlike a fixed
        // last-element pivot, not quadratic on already-sorted input such as
        // row-ordered depth images. Coordinates are finite here (`new` filters
        // the rest), so `total_cmp` matches numeric order.
        points.select_nth_unstable_by(median, |a, b| a.0.coords[axis].total_cmp(&b.0.coords[axis]));

        let (left_points, rest) = points.split_at_mut(median);
        let ((point, index), right_points) = (rest[0], &mut rest[1..]);

        let (root, children) = nodes.split_first_mut().expect("one slot per point");
        let (left_nodes, right_nodes) = children.split_at_mut(median);

        let left_base = base + 1;
        let right_base = base + 1 + median as u32;
        *root = KdNode {
            point,
            original_index: index,
            left: if left_points.is_empty() {
                NIL
            } else {
                left_base
            },
            right: if right_points.is_empty() {
                NIL
            } else {
                right_base
            },
            axis: axis as u8,
        };

        let build_left = |nodes: &mut [KdNode], points: &mut [(Point3f, usize)]| {
            if !points.is_empty() {
                Self::build_tree(nodes, left_base, points, depth + 1);
            }
        };
        let build_right = |nodes: &mut [KdNode], points: &mut [(Point3f, usize)]| {
            if !points.is_empty() {
                Self::build_tree(nodes, right_base, points, depth + 1);
            }
        };

        if len > PARALLEL_THRESHOLD {
            rayon::join(
                || build_left(left_nodes, left_points),
                || build_right(right_nodes, right_points),
            );
        } else {
            build_left(left_nodes, left_points);
            build_right(right_nodes, right_points);
        }
    }

    /// Find the `k` nearest neighbors of `query`, writing them into `out` as
    /// `(original_index, squared_distance)`, closest first.
    ///
    /// `out` is cleared first and its allocation is reused, so calling this in a
    /// loop (one buffer per thread) does no allocation per query. This is the hot
    /// path of normal estimation. `k` is expected to be small (tens): neighbors
    /// are kept in a sorted array rather than a heap.
    pub fn find_k_nearest_into(&self, query: &Point3f, k: usize, out: &mut Vec<(usize, f32)>) {
        /// See `find_nearest_bounded` for why 64 is enough.
        const MAX_STACK: usize = 64;

        out.clear();
        let Some(root) = self.root else {
            return;
        };
        if k == 0 {
            return;
        }

        // Squared distance a point must beat to get in: the current k-th
        // nearest once we have k, otherwise anything.
        let bound = |out: &Vec<(usize, f32)>| {
            if out.len() < k {
                f32::INFINITY
            } else {
                out[k - 1].1
            }
        };

        // (node, squared distance from the query to the split plane that
        // separated it), re-checked on pop as in `find_nearest_bounded`.
        let mut stack = [(0u32, 0.0f32); MAX_STACK];
        stack[0] = (root, 0.0);
        let mut len = 1usize;

        while len > 0 {
            len -= 1;
            let (idx, plane_sq) = stack[len];
            if plane_sq >= bound(out) {
                continue;
            }
            let node = &self.nodes[idx as usize];

            let dist_sq = Self::distance_squared(&node.point, query);
            if dist_sq < bound(out) {
                if out.len() == k {
                    out.pop();
                }
                let pos = out.partition_point(|&(_, d)| d <= dist_sq);
                out.insert(pos, (node.original_index, dist_sq));
            }

            let axis_dist =
                query.coords[node.axis as usize] - node.point.coords[node.axis as usize];
            let axis_dist_sq = axis_dist * axis_dist;
            let (near, far) = if axis_dist <= 0.0 {
                (node.left, node.right)
            } else {
                (node.right, node.left)
            };

            // Push far before near so near is popped first (LIFO).
            if far != NIL && axis_dist_sq < bound(out) {
                stack[len] = (far, axis_dist_sq);
                len += 1;
            }
            if near != NIL {
                stack[len] = (near, 0.0);
                len += 1;
            }
        }
    }

    /// Find the single nearest neighbor of `query`, allocation-free.
    ///
    /// `bound_sq` is a squared-distance upper bound: only points strictly closer
    /// than it are considered, and the far side of a split is skipped as soon as
    /// it cannot beat the best so far. Pass `f32::INFINITY` for an unbounded
    /// search. When the caller already knows a candidate (e.g. last ICP
    /// iteration's match), pass its index and squared distance as `seed` so the
    /// search starts pruned; `seed` is returned if nothing closer is found.
    ///
    /// Returns `(original_index, squared_distance)`, or `None` if no point lies
    /// within the bound and no seed was given. This is the hot path for ICP
    /// correspondence search, so — unlike `find_k_nearest` — it uses a fixed-size
    /// stack and no heap, result `Vec`, or `sqrt`.
    pub fn find_nearest_bounded(
        &self,
        query: &Point3f,
        bound_sq: f32,
        seed: Option<(usize, f32)>,
    ) -> Option<(usize, f32)> {
        // The tree is median-split, so its depth is at most ceil(log2(n + 1)),
        // which for `u32` node indices is ≤ 33. The stack holds at most one
        // deferred far child per level plus the current near child.
        const MAX_STACK: usize = 64;

        let (mut best, mut best_sq) = match seed {
            Some((_, d)) if d < bound_sq => (seed, d),
            _ => (None, bound_sq),
        };

        // Each entry is (node, squared distance from the query to the split
        // plane that separated it). Far children are pushed before the near
        // child is explored, so `best_sq` may have shrunk by the time one is
        // popped; re-checking the plane distance then prunes the whole subtree.
        let mut stack = [(0u32, 0.0f32); MAX_STACK];
        let mut len = 0usize;
        if let Some(root) = self.root {
            stack[0] = (root, 0.0);
            len = 1;
        }

        while len > 0 {
            len -= 1;
            let (idx, plane_sq) = stack[len];
            if plane_sq >= best_sq {
                continue;
            }
            let node = &self.nodes[idx as usize];

            let dist_sq = Self::distance_squared(&node.point, query);
            if dist_sq < best_sq {
                best_sq = dist_sq;
                best = Some((node.original_index, dist_sq));
            }

            let axis_dist =
                query.coords[node.axis as usize] - node.point.coords[node.axis as usize];
            let axis_dist_sq = axis_dist * axis_dist;
            let (near, far) = if axis_dist <= 0.0 {
                (node.left, node.right)
            } else {
                (node.right, node.left)
            };

            // Push far before near so near is popped first (LIFO).
            if far != NIL && axis_dist_sq < best_sq {
                stack[len] = (far, axis_dist_sq);
                len += 1;
            }
            if near != NIL {
                stack[len] = (near, 0.0);
                len += 1;
            }
        }

        best
    }

    /// Calculate squared distance between two points
    fn distance_squared(a: &Point3f, b: &Point3f) -> f32 {
        let dx = a.x - b.x;
        let dy = a.y - b.y;
        let dz = a.z - b.z;
        dx * dx + dy * dy + dz * dz
    }
}

impl NearestNeighborSearch for KdTree {
    /// Find the `k` nearest neighbors, closest first, as `(index, distance)`.
    ///
    /// For many queries in a loop, [`KdTree::find_k_nearest_into`] avoids the
    /// allocation per call.
    fn find_k_nearest(&self, query: &Point3f, k: usize) -> Vec<(usize, f32)> {
        let mut neighbors = Vec::with_capacity(k);
        self.find_k_nearest_into(query, k, &mut neighbors);
        neighbors
            .into_iter()
            .map(|(index, distance_sq)| (index, distance_sq.sqrt()))
            .collect()
    }

    /// Find all neighbors within `radius` using an iterative stack-based traversal.
    fn find_radius_neighbors(&self, query: &Point3f, radius: f32) -> Vec<(usize, f32)> {
        if radius <= 0.0 || self.points.is_empty() {
            return Vec::new();
        }

        let radius_sq = radius * radius;
        let mut result: Vec<(usize, f32)> = Vec::new();
        let mut stack: Vec<u32> = Vec::new();

        if let Some(root) = self.root {
            stack.push(root);
        }

        while let Some(idx) = stack.pop() {
            let node = &self.nodes[idx as usize];
            let dist_sq = Self::distance_squared(&node.point, query);
            if dist_sq <= radius_sq {
                result.push((node.original_index, dist_sq.sqrt()));
            }

            let query_val = query.coords[node.axis as usize];
            let node_val = node.point.coords[node.axis as usize];
            let axis_dist = query_val - node_val;

            let (near, far) = if query_val <= node_val {
                (node.left, node.right)
            } else {
                (node.right, node.left)
            };

            // The far subtree can only contain in-radius points when the
            // distance to the splitting hyperplane is within the search radius.
            if axis_dist * axis_dist <= radius_sq {
                if far != NIL {
                    stack.push(far);
                }
            }
            if near != NIL {
                stack.push(near);
            }
        }

        result.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        result
    }
}

/// Simple brute force nearest neighbor search for small datasets
pub struct BruteForceSearch {
    points: Vec<Point3f>,
}

impl BruteForceSearch {
    pub fn new(points: &[Point3f]) -> Self {
        Self {
            points: points.to_vec(),
        }
    }
}

impl NearestNeighborSearch for BruteForceSearch {
    fn find_k_nearest(&self, query: &Point3f, k: usize) -> Vec<(usize, f32)> {
        if k == 0 || self.points.is_empty() {
            return Vec::new();
        }

        let mut distances: Vec<(usize, f32)> = self
            .points
            .iter()
            .enumerate()
            .map(|(idx, point)| {
                let dx = point.x - query.x;
                let dy = point.y - query.y;
                let dz = point.z - query.z;
                let distance = (dx * dx + dy * dy + dz * dz).sqrt();
                (idx, distance)
            })
            .collect();

        // Sort by distance and take k nearest
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
        distances.truncate(k);
        distances
    }

    fn find_radius_neighbors(&self, query: &Point3f, radius: f32) -> Vec<(usize, f32)> {
        if radius <= 0.0 || self.points.is_empty() {
            return Vec::new();
        }

        let radius_squared = radius * radius;
        self.points
            .iter()
            .enumerate()
            .filter_map(|(idx, point)| {
                let dx = point.x - query.x;
                let dy = point.y - query.y;
                let dz = point.z - query.z;
                let distance_squared = dx * dx + dy * dy + dz * dz;

                if distance_squared <= radius_squared {
                    Some((idx, distance_squared.sqrt()))
                } else {
                    None
                }
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::Rng;
    use threecrate_core::Point3f;

    fn create_test_points() -> Vec<Point3f> {
        vec![
            Point3f::new(0.0, 0.0, 0.0),
            Point3f::new(1.0, 0.0, 0.0),
            Point3f::new(0.0, 1.0, 0.0),
            Point3f::new(0.0, 0.0, 1.0),
            Point3f::new(1.0, 1.0, 0.0),
            Point3f::new(1.0, 0.0, 1.0),
            Point3f::new(0.0, 1.0, 1.0),
            Point3f::new(1.0, 1.0, 1.0),
        ]
    }

    #[test]
    fn test_kd_tree_construction() {
        let points = create_test_points();
        let kdtree = KdTree::new(&points).unwrap();

        assert_eq!(kdtree.points.len(), points.len());
        assert!(kdtree.root.is_some());
    }

    #[test]
    fn test_empty_kd_tree() {
        let kdtree = KdTree::new(&[]).unwrap();
        assert!(kdtree.root.is_none());
        assert!(kdtree.points.is_empty());

        let query = Point3f::new(0.0, 0.0, 0.0);
        let result = kdtree.find_k_nearest(&query, 5);
        assert!(result.is_empty());
    }

    #[test]
    fn test_k_nearest_neighbors_consistency() {
        let points = create_test_points();
        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        let query = Point3f::new(0.5, 0.5, 0.5);
        let k = 3;

        let mut kdtree_result = kdtree.find_k_nearest(&query, k);
        let mut brute_force_result = brute_force.find_k_nearest(&query, k);

        println!("KD-tree result before sorting: {:?}", kdtree_result);
        println!(
            "Brute force result before sorting: {:?}",
            brute_force_result
        );

        // Sort by distance first, then by index for consistent comparison
        kdtree_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });
        brute_force_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });

        println!("KD-tree result after sorting: {:?}", kdtree_result);
        println!("Brute force result after sorting: {:?}", brute_force_result);

        // Results should have the same length
        assert_eq!(kdtree_result.len(), brute_force_result.len());
        assert_eq!(kdtree_result.len(), k);

        // Results should be sorted by distance
        for i in 1..kdtree_result.len() {
            assert!(kdtree_result[i - 1].1 <= kdtree_result[i].1);
            assert!(brute_force_result[i - 1].1 <= brute_force_result[i].1);
        }

        // Check that the distances match (within tolerance)
        for (kdtree_neighbor, brute_neighbor) in kdtree_result.iter().zip(brute_force_result.iter())
        {
            assert!((kdtree_neighbor.1 - brute_neighbor.1).abs() < 1e-6);
        }

        // For points with the same distance, we don't require the exact same indices
        // as long as the distances are correct, the implementation is working
        println!(
            "Test passed: Both methods found {} neighbors with correct distances",
            k
        );
    }

    #[test]
    fn test_radius_neighbors_consistency() {
        let points = create_test_points();
        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        let query = Point3f::new(0.5, 0.5, 0.5);
        let radius = 1.5;

        let mut kdtree_result = kdtree.find_radius_neighbors(&query, radius);
        let mut brute_force_result = brute_force.find_radius_neighbors(&query, radius);

        // Sort by distance first, then by index for consistent comparison
        kdtree_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });
        brute_force_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });

        // Results should have the same length
        assert_eq!(kdtree_result.len(), brute_force_result.len());

        // Results should be sorted by distance
        for i in 1..kdtree_result.len() {
            assert!(kdtree_result[i - 1].1 <= kdtree_result[i].1);
            assert!(brute_force_result[i - 1].1 <= brute_force_result[i].1);
        }

        // All distances should be within radius
        for (_, distance) in &kdtree_result {
            assert!(*distance <= radius);
        }

        for (_, distance) in &brute_force_result {
            assert!(*distance <= radius);
        }

        // Check that the distances match (within tolerance)
        for (kdtree_neighbor, brute_neighbor) in kdtree_result.iter().zip(brute_force_result.iter())
        {
            assert!((kdtree_neighbor.1 - brute_neighbor.1).abs() < 1e-6);
        }

        println!(
            "Test passed: Both methods found {} neighbors within radius {}",
            kdtree_result.len(),
            radius
        );
    }

    #[test]
    fn test_edge_cases() {
        let points = create_test_points();
        let kdtree = KdTree::new(&points).unwrap();
        let _brute_force = BruteForceSearch::new(&points);

        let query = Point3f::new(0.0, 0.0, 0.0);

        // Test k = 0
        let result = kdtree.find_k_nearest(&query, 0);
        assert!(result.is_empty());

        // Test k larger than number of points
        let result = kdtree.find_k_nearest(&query, 20);
        assert_eq!(result.len(), points.len());

        // Test radius = 0
        let result = kdtree.find_radius_neighbors(&query, 0.0);
        assert!(result.is_empty());

        // Test negative radius
        let result = kdtree.find_radius_neighbors(&query, -1.0);
        assert!(result.is_empty());
    }

    #[test]
    fn test_random_points() {
        let mut rng = rand::rng();
        let mut points = Vec::new();

        // Generate 100 random points
        for _ in 0..100 {
            points.push(Point3f::new(
                rng.random_range(-10.0..10.0),
                rng.random_range(-10.0..10.0),
                rng.random_range(-10.0..10.0),
            ));
        }

        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        // Test multiple random queries
        for _ in 0..10 {
            let query = Point3f::new(
                rng.random_range(-5.0..5.0),
                rng.random_range(-5.0..5.0),
                rng.random_range(-5.0..5.0),
            );

            let k = rng.random_range(1..=10);
            let radius = rng.random_range(1.0..5.0);

            let mut kdtree_knn = kdtree.find_k_nearest(&query, k);
            let mut brute_knn = brute_force.find_k_nearest(&query, k);

            let mut kdtree_radius = kdtree.find_radius_neighbors(&query, radius);
            let mut brute_radius = brute_force.find_radius_neighbors(&query, radius);

            // Sort by distance first, then by index for consistent comparison
            kdtree_knn.sort_by(|a, b| {
                a.1.partial_cmp(&b.1)
                    .unwrap_or(Ordering::Equal)
                    .then(a.0.cmp(&b.0))
            });
            brute_knn.sort_by(|a, b| {
                a.1.partial_cmp(&b.1)
                    .unwrap_or(Ordering::Equal)
                    .then(a.0.cmp(&b.0))
            });

            kdtree_radius.sort_by(|a, b| {
                a.1.partial_cmp(&b.1)
                    .unwrap_or(Ordering::Equal)
                    .then(a.0.cmp(&b.0))
            });
            brute_radius.sort_by(|a, b| {
                a.1.partial_cmp(&b.1)
                    .unwrap_or(Ordering::Equal)
                    .then(a.0.cmp(&b.0))
            });

            // Verify k-nearest neighbors consistency
            assert_eq!(kdtree_knn.len(), brute_knn.len());
            assert_eq!(kdtree_knn.len(), k.min(points.len()));

            // Check that the distances match (within tolerance)
            let min_len = kdtree_knn.len().min(brute_knn.len());
            for i in 0..min_len {
                assert!((kdtree_knn[i].1 - brute_knn[i].1).abs() < 1e-6);
            }

            // Verify radius neighbors consistency
            assert_eq!(kdtree_radius.len(), brute_radius.len());

            // Check that the distances match (within tolerance)
            let min_len = kdtree_radius.len().min(brute_radius.len());
            for i in 0..min_len {
                assert!((kdtree_radius[i].1 - brute_radius[i].1).abs() < 1e-6);
            }
        }
    }

    #[test]
    fn test_find_nearest_bounded_matches_brute_force() {
        let mut rng = rand::rng();
        let points: Vec<Point3f> = (0..5000)
            .map(|_| {
                Point3f::new(
                    rng.random_range(-10.0..10.0),
                    rng.random_range(-10.0..10.0),
                    rng.random_range(-10.0..10.0),
                )
            })
            .collect();
        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        for _ in 0..200 {
            let query = Point3f::new(
                rng.random_range(-12.0..12.0),
                rng.random_range(-12.0..12.0),
                rng.random_range(-12.0..12.0),
            );
            let (bf_idx, bf_dist) = brute_force.find_k_nearest(&query, 1)[0];
            let bf_sq = bf_dist * bf_dist;

            // Unbounded, unseeded
            let (_, d) = kdtree
                .find_nearest_bounded(&query, f32::INFINITY, None)
                .unwrap();
            assert!((d - bf_sq).abs() < 1e-4);

            // Seeded with an arbitrary (usually far) point: still exact
            let seed_idx = rng.random_range(0..points.len());
            let seed_sq = (points[seed_idx] - query).magnitude_squared();
            let (_, d) = kdtree
                .find_nearest_bounded(&query, f32::INFINITY, Some((seed_idx, seed_sq)))
                .unwrap();
            assert!((d - bf_sq).abs() < 1e-4);

            // Seeded with the true answer: returns it
            let (idx, _) = kdtree
                .find_nearest_bounded(&query, f32::INFINITY, Some((bf_idx, bf_sq)))
                .unwrap();
            assert_eq!(idx, bf_idx);

            // A bound tighter than the nearest neighbor finds nothing
            assert!(kdtree
                .find_nearest_bounded(&query, bf_sq * 0.5, None)
                .is_none());
        }
    }

    #[test]
    fn test_find_k_nearest_into_matches_brute_force() {
        let mut rng = rand::rng();
        let points: Vec<Point3f> = (0..5000)
            .map(|_| {
                Point3f::new(
                    rng.random_range(-10.0..10.0),
                    rng.random_range(-10.0..10.0),
                    rng.random_range(-10.0..10.0),
                )
            })
            .collect();
        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);
        let mut out = Vec::new();

        for _ in 0..200 {
            let query = Point3f::new(
                rng.random_range(-12.0..12.0),
                rng.random_range(-12.0..12.0),
                rng.random_range(-12.0..12.0),
            );
            let k = rng.random_range(1..=32);
            kdtree.find_k_nearest_into(&query, k, &mut out);
            let expected = brute_force.find_k_nearest(&query, k);

            assert_eq!(out.len(), k);
            for (&(_, got_sq), &(_, want)) in out.iter().zip(&expected) {
                assert!((got_sq.sqrt() - want).abs() < 1e-4);
            }
            // Sorted, closest first
            assert!(out.windows(2).all(|w| w[0].1 <= w[1].1));
        }
    }

    #[test]
    fn test_kd_tree_with_nan_points() {
        let mut points = create_test_points();
        points.push(Point3f::new(f32::NAN, 0.0, 0.0));
        points.push(Point3f::new(0.5, f32::NAN, f32::NAN));

        // Building must not panic, and finite queries still resolve to the
        // nearest finite point.
        let kdtree = KdTree::new(&points).unwrap();
        let (idx, d) = kdtree
            .find_nearest_bounded(&Point3f::new(0.9, 0.1, 0.05), f32::INFINITY, None)
            .unwrap();
        assert_eq!(idx, 1);
        assert!(d.is_finite());
    }

    #[test]
    fn test_kd_tree_mostly_nan_points() {
        // Mostly-invalid organized cloud: NaN points must not become split
        // nodes that hide the finite points from the search.
        let nan = Point3f::new(f32::NAN, f32::NAN, f32::NAN);
        let mut points = vec![nan; 50];
        points[17] = Point3f::new(0.0, 0.0, 0.0);
        points[33] = Point3f::new(5.0, 5.0, 5.0);
        points.push(Point3f::new(f32::INFINITY, 0.0, 0.0));

        let kdtree = KdTree::new(&points).unwrap();
        let query = Point3f::new(0.1, 0.0, 0.0);

        let (idx, _) = kdtree
            .find_nearest_bounded(&query, f32::INFINITY, None)
            .unwrap();
        assert_eq!(idx, 17);

        let knn = kdtree.find_k_nearest(&query, 5);
        assert_eq!(
            knn.iter().map(|(i, _)| *i).collect::<Vec<_>>(),
            vec![17, 33]
        );

        let radius = kdtree.find_radius_neighbors(&query, 1.0);
        assert_eq!(radius.len(), 1);
        assert_eq!(radius[0].0, 17);

        // An all-invalid cloud yields an empty tree, not a panic
        let empty = KdTree::new(&[nan, nan]).unwrap();
        assert!(empty
            .find_nearest_bounded(&query, f32::INFINITY, None)
            .is_none());
        assert!(empty.find_k_nearest(&query, 3).is_empty());
    }

    #[test]
    fn test_performance_comparison() {
        let mut rng = rand::rng();
        let mut points = Vec::new();

        // Generate 1000 random points for performance test
        for _ in 0..1000 {
            points.push(Point3f::new(
                rng.random_range(-10.0..10.0),
                rng.random_range(-10.0..10.0),
                rng.random_range(-10.0..10.0),
            ));
        }

        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        let query = Point3f::new(0.0, 0.0, 0.0);
        let k = 10;

        // Time KD-tree search
        let start = std::time::Instant::now();
        let _kdtree_result = kdtree.find_k_nearest(&query, k);
        let kdtree_time = start.elapsed();

        // Time brute force search
        let start = std::time::Instant::now();
        let _brute_result = brute_force.find_k_nearest(&query, k);
        let brute_time = start.elapsed();

        // KD-tree should be faster for larger datasets
        println!("KD-tree time: {:?}", kdtree_time);
        println!("Brute force time: {:?}", brute_time);

        // For 1000 points, KD-tree should be significantly faster
        // Note: For small k values, brute force might actually be faster due to overhead
        // So we'll just verify both methods work correctly
        assert!(kdtree_time.as_nanos() > 0);
        assert!(brute_time.as_nanos() > 0);
    }

    #[test]
    fn test_debug_k_nearest() {
        let points = vec![
            Point3f::new(0.0, 0.0, 0.0),
            Point3f::new(1.0, 0.0, 0.0),
            Point3f::new(0.0, 1.0, 0.0),
            Point3f::new(0.0, 0.0, 1.0),
            Point3f::new(1.0, 1.0, 0.0),
            Point3f::new(1.0, 0.0, 1.0),
            Point3f::new(0.0, 1.0, 1.0),
            Point3f::new(1.0, 1.0, 1.0),
        ];

        let kdtree = KdTree::new(&points).unwrap();
        let brute_force = BruteForceSearch::new(&points);

        let query = Point3f::new(0.5, 0.5, 0.5);
        let k = 3;

        let mut kdtree_result = kdtree.find_k_nearest(&query, k);
        let mut brute_force_result = brute_force.find_k_nearest(&query, k);

        kdtree_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });
        brute_force_result.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });

        assert_eq!(kdtree_result.len(), brute_force_result.len());
        assert_eq!(kdtree_result.len(), k);
        for (kd, bf) in kdtree_result.iter().zip(brute_force_result.iter()) {
            assert!(
                (kd.1 - bf.1).abs() < 1e-6,
                "distance mismatch: kd={}, bf={}",
                kd.1,
                bf.1
            );
        }
    }
}