sylva-rs 2.0.0

Terrestrial and airborne laser scanning processing for forest ecology and remote sensing research.
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
// Sylva: LiDAR processing for forest ecology and remote sensing research.
// Copyright (C) 2026 Tim Devereux, The University of Queensland.
// Free software under the GNU General Public License v3.0 or later;
// see the LICENSE file. There is no warranty, to the extent permitted by law.
//! Multi-slice stem detection.
//!
//! Bottom-up: the stem band (1–5 m above ground)
//! is cut into horizontal layers; each layer is clustered in 2-D and circles
//! are fitted by RANSAC (Fischler & Bolles 1981) with an angular-coverage check; circles are linked
//! across layers into stem chains, which must span at least `min_slices`
//! layers and lean less than `max_lean_deg`. A branch, a shrub or a foliage
//! clump rarely produces consistent circles in three or more layers, which
//! is what makes this robust where a single thick slice is not.

use std::collections::HashMap;

use rayon::prelude::*;

use crate::filters::Rng;
use crate::trees::Tree;
use crate::Point;

/// Settings of [`detect_stems`]: the stem band and its layers, the 2-D clustering, the RANSAC circle fits
/// and how circles are linked into stems. The coregistration-mode flags reproduce the stem maps of the coregistration pipeline.
#[derive(Debug, Clone)]
pub struct StemParams {
    /// Bottom of the stem search band (m above ground).
    pub slice_min: f64,
    /// Top of the band.
    pub slice_max: f64,
    /// Height of each layer.
    pub slice_thickness: f64,
    /// Vertical spacing between layer centres.
    pub slice_step: f64,
    /// Height at which positions and DBH are reported.
    pub reference_height: f64,
    /// Smallest circle radius accepted (m), for a layer's circle and for half the DBH.
    pub min_radius: f64,
    /// Largest circle radius accepted (m), for a layer's circle and for half the DBH.
    pub max_radius: f64,
    /// 2-D grid cell for connected-component clustering (about half the smallest stem radius).
    pub cluster_cell: f64,
    /// Clusters with fewer points than this are dropped (count).
    pub min_cluster_points: usize,
    /// Clusters wider than this cannot be a single stem.
    pub max_cluster_extent: f64,
    /// Three-point circle hypotheses drawn per RANSAC fit (count).
    pub ransac_iterations: usize,
    /// Inlier band around a hypothesised circle (m).
    pub ransac_tolerance: f64,
    /// Split merged clusters (stem + neighbour) into up to this many circles.
    pub max_circles_per_cluster: usize,
    /// Fewest inliers a circle may have (count); a cluster with fewer points left is not fitted.
    pub min_circle_inliers: usize,
    /// Fraction of the circumference that must be observed (a single scan sees ~0.5).
    pub min_coverage: f64,
    /// Longest contiguous arc (degrees) the inliers must cover; 0 disables.
    /// The arc runs from point to point while neighbours are within 15
    /// degrees of each other, or within the wider gap a sparsely sampled
    /// circle's point count explains (at most 35 degrees). A high value
    /// rejects short arcs on merged multi-scan plots, and with them the
    /// stems seen from one side only.
    pub min_arc_deg: f64,
    /// Largest RMS radial residual of its inliers a circle may have (m).
    pub max_circle_rmse: f64,
    /// Max lateral offset between consecutive layers.
    pub link_radius: f64,
    /// Max relative radius change between layers.
    pub link_radius_ratio: f64,
    /// Fewest layers a chain of linked circles must span to count as a stem (count).
    pub min_slices: usize,
    /// Largest lean of the fitted stem axis from vertical (degrees).
    pub max_lean_deg: f64,
    /// Absolute radius tolerance for linking (so 5 cm saplings can still link).
    pub link_radius_abs: f64,
    /// Keep only stem-like band points before slicing: locally planar surface
    /// patches with a near-horizontal normal (bark), which removes foliage and
    /// horizontal branches that bridge neighbouring stems into one cluster.
    pub prefilter: bool,
    /// Neighbours per point for the prefilter's local PCA (count).
    pub prefilter_k: usize,
    /// Max |normal_z| for a point to count as stem surface.
    pub prefilter_max_nz: f64,
    /// Max surface variation `l1 / (l1 + l2 + l3)` (0 = perfect plane).
    pub prefilter_max_variation: f64,
    /// Seed of the RANSAC random stream: one stream from it with `shared_rng`, otherwise `seed + layer` per layer.
    pub seed: u64,
    /// RANSAC adaptive stop granularity: the stop is checked before every
    /// block of this many hypotheses (coregistration mode scores 32 at a time); 0 checks
    /// before every hypothesis.
    pub ransac_block: usize,
    /// Draw all `ransac_iterations` triples up front, keep only the distinct,
    /// non-collinear ones whose circle radius is in range, and count only
    /// those as tried (coregistration mode). Otherwise triples are drawn one at a time
    /// and every distinct triple counts as tried.
    pub ransac_presample: bool,
    /// Re-cluster clusters wider than `max_cluster_extent` with half the cell
    /// and keep the stem-sized pieces; false skips them (coregistration mode).
    pub recluster_wide: bool,
    /// Grid each layer from its own xy minimum and order clusters (and the
    /// points in them) as `scipy.ndimage.label` does: by first cell in
    /// row-major (y, x) order, points by index (coregistration mode). Otherwise an
    /// absolute `floor(p / cell)` grid in (x, y) order.
    pub cluster_grid_at_slice_min: bool,
    /// Include points at exactly `slice_max + thickness / 2` in the band (coregistration mode).
    pub band_top_inclusive: bool,
    /// One random stream shared by all layers in order (coregistration mode, sequential);
    /// otherwise each layer gets `seed + layer` and layers run in parallel.
    pub shared_rng: bool,
    /// The DBH taper is a least-squares line whose squared residuals are
    /// weighted by `n_inliers^power`. 1.0 (the default) weights each slice by
    /// its inlier count, as `np.polyfit(w=sqrt(n))` does (polyfit weights the
    /// unsquared residuals).
    pub taper_weight_power: f64,
    /// Clouds with fewer points find no stems (coregistration mode: 100).
    pub min_total_points: usize,
    /// Give every cluster its own random stream, seeded from `seed`, the
    /// layer height and the cluster's first grid cell, instead of one stream
    /// running through all the clusters of a layer. A stem's circles then
    /// depend only on the points around it, so that a cloud processed in
    /// tiles finds the same stems as the whole cloud.
    pub cluster_seeds: bool,
}

impl Default for StemParams {
    fn default() -> Self {
        StemParams {
            slice_min: 1.0,
            slice_max: 5.0,
            slice_thickness: 0.3,
            slice_step: 0.25,
            reference_height: 1.3,
            min_radius: 0.015,
            max_radius: 0.75,
            cluster_cell: 0.06,
            min_cluster_points: 12,
            max_cluster_extent: 2.0,
            ransac_iterations: 120,
            ransac_tolerance: 0.02,
            max_circles_per_cluster: 3,
            min_circle_inliers: 10,
            min_coverage: 0.12,
            min_arc_deg: 0.0,
            max_circle_rmse: 0.02,
            link_radius: 0.2,
            link_radius_ratio: 0.45,
            min_slices: 3,
            max_lean_deg: 25.0,
            link_radius_abs: 0.02,
            prefilter: true,
            prefilter_k: 16,
            prefilter_max_nz: 0.6,
            prefilter_max_variation: 0.15,
            seed: 0,
            ransac_block: 0,
            ransac_presample: false,
            recluster_wide: true,
            cluster_grid_at_slice_min: false,
            band_top_inclusive: false,
            shared_rng: false,
            taper_weight_power: 1.0,
            min_total_points: 0,
            cluster_seeds: false,
        }
    }
}

impl StemParams {
    /// The detector settings used for coregistration stem maps (the
    /// coregistration mode): 0.4 m slice spacing, radii from 2.5 to 60 cm, no
    /// prefilter, over-wide clusters skipped, RANSAC triples presampled and
    /// scored in blocks of 32, each layer gridded from its own minimum, and
    /// one random stream shared by all layers in order.
    pub fn coreg() -> Self {
        StemParams {
            slice_min: 1.0,
            slice_max: 5.0,
            slice_thickness: 0.3,
            slice_step: 0.4,
            reference_height: 1.3,
            min_radius: 0.025,
            max_radius: 0.60,
            cluster_cell: 0.06,
            min_cluster_points: 12,
            max_cluster_extent: 2.0,
            ransac_iterations: 120,
            ransac_tolerance: 0.02,
            max_circles_per_cluster: 3,
            min_circle_inliers: 10,
            min_coverage: 0.12,
            min_arc_deg: 0.0,
            max_circle_rmse: 0.02,
            link_radius: 0.20,
            link_radius_ratio: 0.45,
            min_slices: 3,
            max_lean_deg: 25.0,
            link_radius_abs: 0.0,
            prefilter: false,
            prefilter_k: 16,
            prefilter_max_nz: 0.6,
            prefilter_max_variation: 0.15,
            seed: 0,
            ransac_block: 32,
            ransac_presample: true,
            recluster_wide: false,
            cluster_grid_at_slice_min: true,
            band_top_inclusive: true,
            shared_rng: true,
            taper_weight_power: 1.0,
            min_total_points: 100,
            cluster_seeds: false,
        }
    }
}

/// A circle fitted to one layer.
#[derive(Debug, Clone, Copy)]
pub struct CircleFit {
    /// Circle centre x (m, cloud frame).
    pub x: f64,
    /// Circle centre y (m, cloud frame).
    pub y: f64,
    /// Fitted radius (m).
    pub radius: f64,
    /// Height above ground of the layer's centre (m).
    pub height: f64,
    /// Points within `ransac_tolerance` of the circle (count).
    pub n_inliers: usize,
    /// RMS radial residual of the inliers (m).
    pub rmse: f64,
    /// Fraction of the 36 angular bins around the centre that the inliers occupy (0-1).
    pub coverage: f64,
}

fn circumcircle(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> Option<(f64, f64, f64)> {
    let d = 2.0 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]));
    if d.abs() < 1e-12 {
        return None;
    }
    let (s1, s2, s3) = (a[0] * a[0] + a[1] * a[1], b[0] * b[0] + b[1] * b[1], c[0] * c[0] + c[1] * c[1]);
    let cx = (s1 * (b[1] - c[1]) + s2 * (c[1] - a[1]) + s3 * (a[1] - b[1])) / d;
    let cy = (s1 * (c[0] - b[0]) + s2 * (a[0] - c[0]) + s3 * (b[0] - a[0])) / d;
    Some((cx, cy, (a[0] - cx).hypot(a[1] - cy)))
}

/// Kåsa (1976) fit about the centroid followed by Gauss–Newton geometric
/// refinement.
pub(crate) fn fit_circle_refined(xy: &[[f64; 2]]) -> Option<(f64, f64, f64)> {
    let n = xy.len() as f64;
    if xy.len() < 3 {
        return None;
    }
    let mx = xy.iter().map(|p| p[0]).sum::<f64>() / n;
    let my = xy.iter().map(|p| p[1]).sum::<f64>() / n;
    let mut ata = nalgebra::Matrix3::<f64>::zeros();
    let mut atb = nalgebra::Vector3::<f64>::zeros();
    for p in xy {
        let (px, py) = (p[0] - mx, p[1] - my);
        let row = nalgebra::Vector3::new(2.0 * px, 2.0 * py, 1.0);
        ata += row * row.transpose();
        atb += row * (px * px + py * py);
    }
    let s = ata.lu().solve(&atb)?;
    let r2 = s[2] + s[0] * s[0] + s[1] * s[1];
    if !r2.is_finite() || r2 <= 0.0 {
        return None;
    }
    let (mut cx, mut cy, mut r) = (s[0], s[1], r2.sqrt());
    for _ in 0..8 {
        let mut jtj = nalgebra::Matrix3::<f64>::zeros();
        let mut jtr = nalgebra::Vector3::<f64>::zeros();
        for p in xy {
            let dx = p[0] - mx - cx;
            let dy = p[1] - my - cy;
            let d = dx.hypot(dy).max(1e-9);
            let res = d - r;
            let j = nalgebra::Vector3::new(-dx / d, -dy / d, -1.0);
            jtj += j * j.transpose();
            jtr += j * res;
        }
        let Some(step) = jtj.lu().solve(&(-jtr)) else { break };
        cx += step[0];
        cy += step[1];
        r += step[2];
        if r <= 0.0 {
            return None;
        }
        if step.norm() < 1e-9 {
            break;
        }
    }
    Some((cx + mx, cy + my, r))
}

/// What a point at either end of an arc adds to it (degrees): a point stands
/// for a sector of the circle, half of it on either side.
const SECTOR: f64 = 10.0;
/// Widest gap between neighbouring points that an arc runs across (degrees).
const ARC_GAP: f64 = 15.0;
/// Widest gap [`sampled_arc`] lets a sparse arc run across (degrees).
const ARC_GAP_SPARSE: f64 = 35.0;

/// Share of the 36 ten-degree bins about `(cx, cy)` that hold a point: the
/// coverage of the stem detector, as its Python predecessor measured it.
fn bin_coverage(xy: &[[f64; 2]], cx: f64, cy: f64) -> f64 {
    const BINS: usize = 36;
    let mut seen = [false; BINS];
    for p in xy {
        let a = (p[1] - cy).atan2(p[0] - cx);
        seen[(((a + std::f64::consts::PI) / std::f64::consts::TAU) * BINS as f64) as usize % BINS] = true;
    }
    seen.iter().filter(|&&s| s).count() as f64 / BINS as f64
}

/// The gaps between points that are neighbours in angle about `(cx, cy)`,
/// in degrees, in order round the circle (the last one closes it).
fn angular_gaps(xy: &[[f64; 2]], cx: f64, cy: f64) -> Vec<f64> {
    let mut a: Vec<f64> = xy.iter().map(|p| (p[1] - cy).atan2(p[0] - cx).to_degrees()).collect();
    a.sort_by(|x, y| x.partial_cmp(y).unwrap());
    (0..a.len()).map(|i| if i + 1 < a.len() { a[i + 1] - a[i] } else { a[0] + 360.0 - a[i] }).collect()
}

/// The longest arc, in degrees, for the gaps of [`angular_gaps`]: it runs
/// from point to point while neighbours are no more than `max_gap` apart,
/// and counts the half sector beyond the point at either end. Unlike a run
/// of occupied bins it does not depend on where a grid of bins would fall.
fn longest_arc(gaps: &[f64], max_gap: f64) -> f64 {
    let n = gaps.len();
    if n == 0 {
        return 0.0;
    }
    let Some(first_break) = gaps.iter().position(|&g| g > max_gap) else {
        return 360.0;
    };
    let (mut best, mut run) = (0.0f64, 0.0f64);
    for k in 1..=n {
        let g = gaps[(first_break + k) % n];
        if g > max_gap {
            best = best.max(run);
            run = 0.0;
        } else {
            run += g;
        }
    }
    (best + SECTOR).min(360.0)
}

/// `(share of ten-degree bins occupied, longest arc in degrees)` about
/// `(cx, cy)`: the arc runs from point to point while neighbours are no more
/// than 15 degrees apart (see [`longest_arc`]).
pub(crate) fn angular_coverage(xy: &[[f64; 2]], cx: f64, cy: f64) -> (f64, f64) {
    (bin_coverage(xy, cx, cy), longest_arc(&angular_gaps(xy, cx, cy), ARC_GAP))
}

/// The longest arc about `(cx, cy)`, in degrees, measured from its first
/// point to its last (neighbours no more than 15 degrees apart) without the
/// half sectors beyond them: the arc of a densely sampled section, as the
/// cylinder models test it.
pub(crate) fn angular_extent(xy: &[[f64; 2]], cx: f64, cy: f64) -> f64 {
    let (_, arc) = angular_coverage(xy, cx, cy);
    if arc >= 360.0 { 360.0 } else { (arc - SECTOR).max(0.0) }
}

/// [`angular_coverage`] for a circle whose points may be sparse: the arc
/// runs across the gaps that the sampling itself accounts for.
///
/// Points scattered along an arc at a mean spacing `d` leave gaps that are
/// exponentially distributed, so the median gap is `d ln 2` and one of `n`
/// gaps is wider than `G` with probability `exp(-G / d)`. The arc is carried
/// over gaps up to the `G` for which a wider chance gap is expected fewer
/// than once in twenty circles (`n exp(-G / d) <= 0.05`), no less than the 15
/// degrees of the plain test and no more than 35. A densely sampled circle
/// gets the plain test.
fn sampled_arc(xy: &[[f64; 2]], cx: f64, cy: f64) -> (f64, f64) {
    let gaps = angular_gaps(xy, cx, cy);
    if gaps.is_empty() {
        return (0.0, 0.0);
    }
    let mut sorted = gaps.clone();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
    let spacing = sorted[sorted.len() / 2] / std::f64::consts::LN_2;
    let reach = (spacing * (20.0 * gaps.len() as f64).ln()).clamp(ARC_GAP, ARC_GAP_SPARSE);
    (bin_coverage(xy, cx, cy), longest_arc(&gaps, reach))
}

/// RANSAC circle scored by (`inliers * (1 - mean_residual / tol)`)
/// and adaptive stopping. Returns `(cx, cy, r, inlier mask)`.
pub(crate) fn ransac_circle(xy: &[[f64; 2]], p: &StemParams, rng: &mut Rng) -> Option<(f64, f64, f64, Vec<bool>)> {
    let n = xy.len();
    if n < 3 {
        return None;
    }
    let mut best_score = 0.0;
    let mut best_count = 0usize;
    let mut best = (0.0, 0.0, 0.0);
    let mut tried = 0usize;
    // Adaptive stop: once a hypothesis explains a large share of the
    // cluster, more sampling is very unlikely to find a better one.
    let stop = |best_count: usize, tried: usize| {
        if best_count < 3 {
            return false;
        }
        let ratio = best_count as f64 / n as f64;
        ratio > 0.99 || tried as f64 >= (1e-3f64).ln() / (1.0 - ratio.powi(3)).max(1e-12).ln()
    };
    let consider = |cx: f64, cy: f64, r: f64, best_score: &mut f64, best_count: &mut usize, best: &mut (f64, f64, f64)| {
        let mut count = 0usize;
        let mut total = 0.0;
        for q in xy {
            let res = ((q[0] - cx).hypot(q[1] - cy) - r).abs();
            if res < p.ransac_tolerance {
                count += 1;
                total += res;
            }
        }
        if count < 3 {
            return;
        }
        let score = count as f64 * (1.0 - total / count as f64 / p.ransac_tolerance);
        if score > *best_score {
            *best_score = score;
            *best_count = count;
            *best = (cx, cy, r);
        }
    };
    let block = p.ransac_block.max(1);
    if p.ransac_presample {
        // Every triple is drawn first; only valid circles are candidates.
        let triples: Vec<(usize, usize, usize)> = (0..p.ransac_iterations).map(|_| (rng.below(n), rng.below(n), rng.below(n))).collect();
        let candidates: Vec<(f64, f64, f64)> = triples
            .into_iter()
            .filter(|&(i, j, k)| i != j && j != k && i != k)
            .filter_map(|(i, j, k)| circumcircle(xy[i], xy[j], xy[k]))
            .filter(|&(_, _, r)| r >= p.min_radius && r <= p.max_radius)
            .collect();
        for chunk in candidates.chunks(block) {
            if stop(best_count, tried) {
                break;
            }
            tried += chunk.len();
            for &(cx, cy, r) in chunk {
                consider(cx, cy, r, &mut best_score, &mut best_count, &mut best);
            }
        }
    } else {
        for _ in 0..p.ransac_iterations {
            if tried.is_multiple_of(block) && stop(best_count, tried) {
                break;
            }
            let (i, j, k) = (rng.below(n), rng.below(n), rng.below(n));
            if i == j || j == k || i == k {
                continue;
            }
            tried += 1;
            let Some((cx, cy, r)) = circumcircle(xy[i], xy[j], xy[k]) else { continue };
            if r < p.min_radius || r > p.max_radius {
                continue;
            }
            consider(cx, cy, r, &mut best_score, &mut best_count, &mut best);
        }
    }
    if best_score <= 0.0 {
        return None;
    }
    let (mut cx, mut cy, mut r) = best;
    let mut mask: Vec<bool> = xy.iter().map(|q| ((q[0] - cx).hypot(q[1] - cy) - r).abs() < p.ransac_tolerance).collect();
    for _ in 0..3 {
        let pts: Vec<[f64; 2]> = xy.iter().zip(&mask).filter(|(_, &m)| m).map(|(q, _)| *q).collect();
        if pts.len() < 3 {
            break;
        }
        let (ncx, ncy, nr) = fit_circle_refined(&pts)?;
        if nr < p.min_radius || nr > p.max_radius {
            return None;
        }
        cx = ncx;
        cy = ncy;
        r = nr;
        let new_mask: Vec<bool> = xy.iter().map(|q| ((q[0] - cx).hypot(q[1] - cy) - r).abs() < p.ransac_tolerance).collect();
        let cnt = new_mask.iter().filter(|&&m| m).count();
        if cnt < 3 || new_mask == mask {
            if cnt >= 3 {
                mask = new_mask;
            }
            break;
        }
        mask = new_mask;
    }
    Some((cx, cy, r, mask))
}

/// Connected components on a 2-D occupancy grid (8-connectivity).
fn cluster_2d(xy: &[[f64; 2]], cell: f64, min_points: usize) -> Vec<Vec<usize>> {
    if xy.is_empty() {
        return Vec::new();
    }
    let mut cells: HashMap<(i64, i64), Vec<usize>> = HashMap::new();
    for (i, p) in xy.iter().enumerate() {
        cells.entry(((p[0] / cell).floor() as i64, (p[1] / cell).floor() as i64)).or_default().push(i);
    }
    let mut label: HashMap<(i64, i64), usize> = HashMap::new();
    let mut clusters: Vec<Vec<usize>> = Vec::new();
    let mut stack = Vec::new();
    // Deterministic traversal order (HashMap iteration is randomised), so the
    // RANSAC sampling sequence -- and therefore the result -- is reproducible.
    let mut keys: Vec<(i64, i64)> = cells.keys().copied().collect();
    keys.sort_unstable();
    for start in keys {
        if label.contains_key(&start) {
            continue;
        }
        let id = clusters.len();
        clusters.push(Vec::new());
        label.insert(start, id);
        stack.push(start);
        while let Some(c) = stack.pop() {
            clusters[id].extend_from_slice(&cells[&c]);
            for di in -1..=1 {
                for dj in -1..=1 {
                    let nb = (c.0 + di, c.1 + dj);
                    if (di != 0 || dj != 0) && cells.contains_key(&nb) && !label.contains_key(&nb) {
                        label.insert(nb, id);
                        stack.push(nb);
                    }
                }
            }
        }
    }
    clusters.retain(|c| c.len() >= min_points);
    clusters
}

/// Connected components (8-connectivity) in a fixed order: the grid starts at the points' xy minimum, components come in the
/// order `scipy.ndimage.label` numbers them (first cell in row-major (y, x)
/// order) and the points of each component in index order.
fn cluster_2d_raster(xy: &[[f64; 2]], cell: f64, min_points: usize) -> Vec<Vec<usize>> {
    if xy.is_empty() {
        return Vec::new();
    }
    let lo = [xy.iter().map(|q| q[0]).fold(f64::INFINITY, f64::min), xy.iter().map(|q| q[1]).fold(f64::INFINITY, f64::min)];
    let mut cells: HashMap<(i64, i64), Vec<usize>> = HashMap::new();
    for (i, q) in xy.iter().enumerate() {
        // (row, column) = (y, x), truncated as numpy's astype(int64) does.
        cells.entry((((q[1] - lo[1]) / cell) as i64, ((q[0] - lo[0]) / cell) as i64)).or_default().push(i);
    }
    let mut keys: Vec<(i64, i64)> = cells.keys().copied().collect();
    keys.sort_unstable();
    let mut seen: std::collections::HashSet<(i64, i64)> = std::collections::HashSet::new();
    let mut clusters: Vec<Vec<usize>> = Vec::new();
    let mut stack = Vec::new();
    for start in keys {
        if !seen.insert(start) {
            continue;
        }
        let mut members = Vec::new();
        stack.push(start);
        while let Some(c) = stack.pop() {
            members.extend_from_slice(&cells[&c]);
            for di in -1..=1 {
                for dj in -1..=1 {
                    let nb = (c.0 + di, c.1 + dj);
                    if (di != 0 || dj != 0) && cells.contains_key(&nb) && seen.insert(nb) {
                        stack.push(nb);
                    }
                }
            }
        }
        if members.len() >= min_points {
            members.sort_unstable();
            clusters.push(members);
        }
    }
    clusters
}

/// Fit every plausible stem circle in one layer.
fn fit_layer(xy: &[[f64; 2]], height: f64, p: &StemParams, rng: &mut Rng) -> Vec<CircleFit> {
    let cluster = |xy: &[[f64; 2]], cell: f64| if p.cluster_grid_at_slice_min { cluster_2d_raster(xy, cell, p.min_cluster_points) } else { cluster_2d(xy, cell, p.min_cluster_points) };
    let mut circles = Vec::new();
    let extent = |c: &[[f64; 2]]| {
        let (mut lo, mut hi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
        for q in c {
            for k in 0..2 {
                lo[k] = lo[k].min(q[k]);
                hi[k] = hi[k].max(q[k]);
            }
        }
        (hi[0] - lo[0]).max(hi[1] - lo[1])
    };
    // With `cluster_seeds`, each cluster's stream is seeded from the layer
    // and the cluster's first cell at the cell size it was found with.
    let cluster_seed = |c: &[[f64; 2]], cell: f64| -> u64 {
        let first = c.iter().map(|q| ((q[0] / cell).floor() as i64, (q[1] / cell).floor() as i64)).min().unwrap_or((0, 0));
        let mut h = splitmix(p.seed ^ splitmix(height.to_bits()));
        for v in [cell.to_bits(), first.0 as u64, first.1 as u64] {
            h = splitmix(h ^ v);
        }
        h
    };
    // With `cluster_seeds`, each cluster also keeps its points in input
    // order: the order the traversal gathers them in starts from the first
    // cell of the whole connected component, which for a component wider
    // than `max_cluster_extent` (re-clustered below) depends on how much of
    // it a tile holds, and RANSAC draws its samples by position.
    let canonical = |mut idx: Vec<usize>| {
        if p.cluster_seeds {
            idx.sort_unstable();
        }
        idx
    };
    let mut clusters: Vec<(Vec<[f64; 2]>, u64)> = Vec::new();
    for idx in cluster(xy, p.cluster_cell) {
        let idx = canonical(idx);
        let cluster_xy: Vec<[f64; 2]> = idx.iter().map(|&i| xy[i]).collect();
        if extent(&cluster_xy) <= p.max_cluster_extent {
            let seed = if p.cluster_seeds { cluster_seed(&cluster_xy, p.cluster_cell) } else { 0 };
            clusters.push((cluster_xy, seed));
            continue;
        }
        if !p.recluster_wide {
            continue;
        }
        // Too wide to be one stem: re-cluster with a finer cell to break
        // bridges, keeping only the pieces that are stem-sized.
        for sub in cluster(&cluster_xy, p.cluster_cell * 0.5) {
            let sub = canonical(sub);
            let c: Vec<[f64; 2]> = sub.iter().map(|&i| cluster_xy[i]).collect();
            if extent(&c) <= p.max_cluster_extent {
                let seed = if p.cluster_seeds { cluster_seed(&c, p.cluster_cell * 0.5) } else { 0 };
                clusters.push((c, seed));
            }
        }
    }
    for (cluster, seed) in clusters {
        let mut own = Rng::new(seed);
        let rng: &mut Rng = if p.cluster_seeds { &mut own } else { &mut *rng };
        let mut remaining = cluster;
        for _ in 0..p.max_circles_per_cluster {
            if remaining.len() < p.min_circle_inliers {
                break;
            }
            let Some((cx, cy, r, mask)) = ransac_circle(&remaining, p, rng) else { break };
            let inliers: Vec<[f64; 2]> = remaining.iter().zip(&mask).filter(|(_, &m)| m).map(|(q, _)| *q).collect();
            if inliers.len() < p.min_circle_inliers {
                break;
            }
            let rmse = (inliers.iter().map(|q| ((q[0] - cx).hypot(q[1] - cy) - r).powi(2)).sum::<f64>() / inliers.len() as f64).sqrt();
            let (coverage, arc) = sampled_arc(&inliers, cx, cy);
            if rmse <= p.max_circle_rmse && coverage >= p.min_coverage && arc >= p.min_arc_deg {
                circles.push(CircleFit { x: cx, y: cy, radius: r, height, n_inliers: inliers.len(), rmse, coverage });
            }
            remaining = remaining.iter().zip(&mask).filter(|(_, &m)| !m).map(|(q, _)| *q).collect();
        }
    }
    circles
}

/// SplitMix64 finaliser (Steele et al. 2014), to mix seeds.
fn splitmix(x: u64) -> u64 {
    let mut z = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

fn predict_next(chain: &[CircleFit], next_height: f64) -> (f64, f64) {
    let last = chain[chain.len() - 1];
    if chain.len() < 2 {
        return (last.x, last.y);
    }
    let prev = chain[chain.len() - 2];
    let dh = last.height - prev.height;
    if dh.abs() < 1e-9 {
        return (last.x, last.y);
    }
    let step = (next_height - last.height) / dh;
    (last.x + (last.x - prev.x) * step, last.y + (last.y - prev.y) * step)
}

/// Greedily link circles of successive layers into chains, extrapolating each
/// chain's lean so leaning stems stay linked.
fn link_layers(layers: &[Vec<CircleFit>], p: &StemParams) -> Vec<Vec<CircleFit>> {
    let mut chains: Vec<Vec<CircleFit>> = Vec::new();
    let mut open: Vec<Vec<CircleFit>> = Vec::new();
    for layer in layers {
        let mut used = vec![false; layer.len()];
        let mut still_open = Vec::new();
        let layer_height = layer.first().map(|c| c.height);
        for mut chain in open {
            let mut best: Option<(usize, f64)> = None;
            if let Some(h) = layer_height {
                let (px, py) = predict_next(&chain, h);
                let r_ref = chain[chain.len() - 1].radius;
                for (i, c) in layer.iter().enumerate() {
                    if used[i] || (c.radius - r_ref).abs() > (p.link_radius_ratio * r_ref).max(p.link_radius_abs) {
                        continue;
                    }
                    let d = (c.x - px).hypot(c.y - py);
                    if d <= p.link_radius && best.map(|b| d < b.1).unwrap_or(true) {
                        best = Some((i, d));
                    }
                }
            }
            match best {
                Some((i, _)) => {
                    used[i] = true;
                    chain.push(layer[i]);
                    still_open.push(chain);
                }
                None if chain.len() >= p.min_slices => chains.push(chain),
                None => {}
            }
        }
        for (i, c) in layer.iter().enumerate() {
            if !used[i] {
                still_open.push(vec![*c]);
            }
        }
        open = still_open;
    }
    chains.extend(open.into_iter().filter(|c| c.len() >= p.min_slices));
    chains
}

fn diameter_at(chain: &[CircleFit], height: f64, weight_power: f64) -> f64 {
    let hs: Vec<f64> = chain.iter().map(|c| c.height).collect();
    let rs: Vec<f64> = chain.iter().map(|c| c.radius).collect();
    let nearest = rs[hs.iter().enumerate().min_by(|a, b| (a.1 - height).abs().partial_cmp(&(b.1 - height).abs()).unwrap()).unwrap().0];
    let span = hs.iter().cloned().fold(f64::NEG_INFINITY, f64::max) - hs.iter().cloned().fold(f64::INFINITY, f64::min);
    if chain.len() >= 3 && span > 0.5 {
        // Weighted linear taper r(h).
        let w: Vec<f64> = chain.iter().map(|c| {
            let n = c.n_inliers as f64;
            if weight_power == 0.5 { n.sqrt() } else if weight_power == 1.0 { n } else { n.powf(weight_power) }
        }).collect();
        let sw: f64 = w.iter().sum();
        let mh = hs.iter().zip(&w).map(|(h, w)| h * w).sum::<f64>() / sw;
        let mr = rs.iter().zip(&w).map(|(r, w)| r * w).sum::<f64>() / sw;
        let sxx: f64 = hs.iter().zip(&w).map(|(h, w)| w * (h - mh).powi(2)).sum();
        let sxy: f64 = hs.iter().zip(&rs).zip(&w).map(|((h, r), w)| w * (h - mh) * (r - mr)).sum();
        if sxx > 0.0 {
            let radius = mr + sxy / sxx * (height - mh);
            let (rmin, rmax) = (rs.iter().cloned().fold(f64::INFINITY, f64::min), rs.iter().cloned().fold(f64::NEG_INFINITY, f64::max));
            if radius >= 0.5 * rmin && radius <= 1.5 * rmax {
                return 2.0 * radius;
            }
        }
    }
    2.0 * nearest
}

/// A detected stem with everything the detector reports for it.
#[derive(Debug, Clone)]
pub struct StemFit {
    /// Position at `reference_height`, DBH, quality etc.; `inlier_fraction` is the coverage.
    pub tree: Tree,
    /// Height above ground of the reported position (the reference height, up to rounding).
    pub z: f64,
    /// Unit stem direction, pointing up, fitted in height-above-ground space.
    pub axis: [f64; 3],
    /// Inlier-weighted mean angular coverage of the circles.
    pub coverage: f64,
    /// The linked circles, bottom up.
    pub circles: Vec<CircleFit>,
}

fn chain_to_stem(chain: &[CircleFit], p: &StemParams) -> Option<StemFit> {
    if chain.len() < p.min_slices {
        return None;
    }
    let wsum: f64 = chain.iter().map(|c| c.n_inliers as f64).sum();
    let w: Vec<f64> = chain.iter().map(|c| c.n_inliers as f64 / wsum).collect();
    let mut mean = [0.0; 3];
    for (c, w) in chain.iter().zip(&w) {
        mean[0] += w * c.x;
        mean[1] += w * c.y;
        mean[2] += w * c.height;
    }
    let mut cov = nalgebra::Matrix3::<f64>::zeros();
    for (c, w) in chain.iter().zip(&w) {
        let d = nalgebra::Vector3::new(c.x - mean[0], c.y - mean[1], c.height - mean[2]);
        cov += d * d.transpose() * *w;
    }
    let eig = cov.symmetric_eigen();
    let imax = (0..3).max_by(|&a, &b| eig.eigenvalues[a].partial_cmp(&eig.eigenvalues[b]).unwrap()).unwrap();
    let mut axis = [eig.eigenvectors[(0, imax)], eig.eigenvectors[(1, imax)], eig.eigenvectors[(2, imax)]];
    if axis[2] < 0.0 {
        axis = [-axis[0], -axis[1], -axis[2]];
    }
    let lean = axis[2].abs().clamp(0.0, 1.0).acos().to_degrees();
    if lean > p.max_lean_deg || axis[2].abs() < 1e-6 {
        return None;
    }
    let t = (p.reference_height - mean[2]) / axis[2];
    let (x, y, z) = (mean[0] + t * axis[0], mean[1] + t * axis[1], mean[2] + t * axis[2]);
    let dbh = diameter_at(chain, p.reference_height, p.taper_weight_power);
    if dbh < 2.0 * p.min_radius || dbh > 2.0 * p.max_radius {
        return None;
    }
    // np.average: divide by the (normalised, so ~1) weight sum.
    let sw: f64 = w.iter().sum();
    let rmse: f64 = chain.iter().zip(&w).map(|(c, w)| c.rmse * w).sum::<f64>() / sw;
    let coverage: f64 = chain.iter().zip(&w).map(|(c, w)| c.coverage * w).sum::<f64>() / sw;
    let quality = (1.0 / (1.0 + rmse / 0.01)) * coverage * (chain.len() as f64 / 6.0).min(1.0);
    let tree = Tree {
        tree_id: 0,
        x,
        y,
        dbh,
        height: f64::NAN,
        n_points: chain.iter().map(|c| c.n_inliers).sum(),
        inlier_fraction: coverage,
        n_slices: chain.len(),
        rmse,
        lean_deg: lean,
        quality: quality.clamp(0.0, 1.0),
    };
    Some(StemFit { tree, z, axis, coverage, circles: chain.to_vec() })
}

/// Detect stems in a height-normalised cloud. Returns trees sorted by
/// quality descending with `tree_id` 1..n.
pub fn detect_stems(points: &[Point], heights: &[f64], p: &StemParams) -> Vec<Tree> {
    detect_stems_full(points, heights, p).into_iter().map(|s| s.tree).collect()
}

/// [`detect_stems`] with each stem's axis, coverage and circles.
pub fn detect_stems_full(points: &[Point], heights: &[f64], p: &StemParams) -> Vec<StemFit> {
    if points.len() < p.min_total_points {
        return Vec::new();
    }
    let half = 0.5 * p.slice_thickness;
    let (lo, hi) = (p.slice_min - half, p.slice_max + half);
    // Restrict to the band once; the prefilter runs on the band only.
    let band_idx: Vec<usize> = (0..points.len())
        .filter(|&i| heights[i] >= lo && (heights[i] < hi || (p.band_top_inclusive && heights[i] == hi)))
        .collect();
    let mut band: Vec<Point> = band_idx.iter().map(|&i| points[i]).collect();
    let mut band_h: Vec<f64> = band_idx.iter().map(|&i| heights[i]).collect();
    if p.prefilter && band.len() > p.prefilter_k {
        // With `cluster_seeds` (tiled detection), neighbours at equal
        // distances are taken by index, so that a point is kept or dropped
        // as in the whole cloud; quantised coordinates make such ties common.
        let (normals, vals) = if p.cluster_seeds { crate::filters::local_pca_by_index(&band, p.prefilter_k) } else { crate::filters::local_pca(&band, p.prefilter_k) };
        let keep: Vec<bool> = normals
            .iter()
            .zip(&vals)
            .map(|(n, [l1, l2, l3])| {
                let sum = l1 + l2 + l3;
                n[2].abs() <= p.prefilter_max_nz && sum > 0.0 && l1 / sum <= p.prefilter_max_variation
            })
            .collect();
        band = band.iter().zip(&keep).filter(|(_, &k)| k).map(|(q, _)| *q).collect();
        band_h = band_h.iter().zip(&keep).filter(|(_, &k)| k).map(|(&h, _)| h).collect();
    }
    // Centres as np.arange(slice_min, slice_max + 1e-9, slice_step) makes them.
    let delta = (p.slice_min + p.slice_step) - p.slice_min;
    let n_layers = ((p.slice_max + 1e-9 - p.slice_min) / delta).ceil().max(0.0) as usize;
    let centres: Vec<f64> = (0..n_layers).map(|i| if i == 0 { p.slice_min } else { p.slice_min + i as f64 * delta }).collect();
    let layer_xy = |c: f64| -> Vec<[f64; 2]> {
        band.iter()
            .zip(&band_h)
            .filter(|(_, &h)| h >= c - half && h < c + half)
            .map(|(q, _)| [q[0], q[1]])
            .collect()
    };
    let layers: Vec<Vec<CircleFit>> = if p.shared_rng {
        let mut rng = Rng::new(p.seed);
        centres.iter().map(|&c| fit_layer(&layer_xy(c), c, p, &mut rng)).collect()
    } else {
        centres
            .par_iter()
            .enumerate()
            .map(|(li, &c)| fit_layer(&layer_xy(c), c, p, &mut Rng::new(p.seed.wrapping_add(li as u64))))
            .collect()
    };
    let chains = link_layers(&layers, p);
    let mut out: Vec<StemFit> = chains.iter().filter_map(|c| chain_to_stem(c, p)).collect();
    out.sort_by(|a, b| b.tree.quality.partial_cmp(&a.tree.quality).unwrap());
    for (i, s) in out.iter_mut().enumerate() {
        s.tree.tree_id = i as i64 + 1;
    }
    out
}

/// Bark-point support of each candidate's stem below the search band.
///
/// For every tree and every slab centre in `slab_heights` (m above ground),
/// counts the prefiltered (bark-like) points within `slab_half` of that
/// height and within `max(trunk_scale * radius, trunk_min) + tan(lean) *
/// (reference_height - h)` of the stem axis. A real stem is supported all the
/// way down to the ground; a branch or a circle fitted to a clump is not.
pub fn stem_root_support(points: &[Point], heights: &[f64], trees: &[Tree], p: &StemParams, slab_heights: &[f64], slab_half: f64, trunk_scale: f64, trunk_min: f64) -> Vec<Vec<usize>> {
    let lo = slab_heights.iter().cloned().fold(f64::INFINITY, f64::min) - slab_half;
    let hi = slab_heights.iter().cloned().fold(f64::NEG_INFINITY, f64::max) + slab_half;
    let idx: Vec<usize> = (0..points.len()).filter(|&i| heights[i] >= lo && heights[i] <= hi).collect();
    let mut band: Vec<Point> = idx.iter().map(|&i| points[i]).collect();
    let mut band_h: Vec<f64> = idx.iter().map(|&i| heights[i]).collect();
    if p.prefilter && band.len() > p.prefilter_k {
        let (normals, vals) = crate::filters::local_pca(&band, p.prefilter_k);
        let keep: Vec<bool> = normals
            .iter()
            .zip(&vals)
            .map(|(n, [l1, l2, l3])| {
                let sum = l1 + l2 + l3;
                n[2].abs() <= p.prefilter_max_nz && sum > 0.0 && l1 / sum <= p.prefilter_max_variation
            })
            .collect();
        band = band.iter().zip(&keep).filter(|(_, &k)| k).map(|(q, _)| *q).collect();
        band_h = band_h.iter().zip(&keep).filter(|(_, &k)| k).map(|(&h, _)| h).collect();
    }
    let flat: Vec<Point> = band.iter().map(|q| [q[0], q[1], 0.0]).collect();
    let kd = crate::util::spatial::KdTree::new(&flat);
    trees
        .par_iter()
        .map(|t| {
            let tan_lean = t.lean_deg.to_radians().tan().abs();
            slab_heights
                .iter()
                .map(|&h| {
                    let r = (trunk_scale * t.dbh / 2.0).max(trunk_min) + tan_lean * (p.reference_height - h).abs();
                    kd.within(&[t.x, t.y, 0.0], r)
                        .into_iter()
                        .filter(|(i, _)| (band_h[*i] - h).abs() <= slab_half)
                        .count()
                })
                .collect()
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Half-visible vertical cylinders, as one scan sees them, plus scattered clutter.
    fn plot() -> (Vec<Point>, Vec<f64>, Vec<(f64, f64, f64)>) {
        let stems = vec![(0.0, 0.0, 0.15), (3.0, 1.0, 0.08), (-2.5, 4.0, 0.3), (5.0, -3.0, 0.2), (-4.0, -4.0, 0.05)];
        let mut rng = Rng::new(7);
        let mut unit = || (rng.next_u64() >> 11) as f64 / (1u64 << 53) as f64;
        let mut pts = Vec::new();
        for &(x, y, r) in &stems {
            let mut z = 0.0;
            while z < 6.0 {
                for k in 0..40 {
                    let a = std::f64::consts::PI * (k as f64 / 40.0 + 0.1 * unit());
                    let rr = r + 0.003 * (unit() - 0.5);
                    pts.push([x + rr * a.cos(), y - rr * a.sin(), z]);
                }
                z += 0.02;
            }
        }
        for _ in 0..5000 {
            pts.push([12.0 * unit() - 6.0, 12.0 * unit() - 6.0, 6.0 * unit()]);
        }
        let h = pts.iter().map(|q| q[2]).collect();
        (pts, h, stems)
    }

    /// `n` points evenly over `arc_deg` of a unit circle, leaving out those
    /// within `hole` degrees of its middle.
    fn arc_points(arc_deg: f64, n: usize, hole: f64) -> Vec<[f64; 2]> {
        (0..n)
            .map(|i| (i as f64 + 0.5) * arc_deg / n as f64)
            .filter(|a| (a - arc_deg / 2.0).abs() >= hole)
            .map(|a| [a.to_radians().cos(), a.to_radians().sin()])
            .collect()
    }

    #[test]
    fn the_arc_runs_from_point_to_point() {
        // Every degree over 120: 119 degrees from first to last, and the
        // half sector beyond each end; twelve of the 36 bins hold points.
        let (share, arc) = angular_coverage(&arc_points(120.0, 120, 0.0), 0.0, 0.0);
        assert!((arc - 129.0).abs() < 1e-9 && (share - 12.0 / 36.0).abs() < 1e-12);
        // All round, nothing breaks it.
        assert_eq!(angular_coverage(&arc_points(360.0, 90, 0.0), 0.0, 0.0), (1.0, 360.0));
        // One point covers its own sector and one bin.
        assert_eq!(angular_coverage(&[[1.0, 0.0]], 0.0, 0.0), (1.0 / 36.0, 10.0));
        // The arc of the same points turned by any angle is the same.
        let turned: Vec<[f64; 2]> = arc_points(120.0, 120, 0.0).iter().map(|p| [p[0] * 0.8 - p[1] * 0.6, p[0] * 0.6 + p[1] * 0.8]).collect();
        assert!((angular_coverage(&turned, 0.0, 0.0).1 - 129.0).abs() < 1e-9);
    }

    #[test]
    fn the_extent_leaves_out_the_end_sectors() {
        assert!((angular_extent(&arc_points(120.0, 120, 0.0), 0.0, 0.0) - 119.0).abs() < 1e-9);
        assert_eq!(angular_extent(&arc_points(360.0, 90, 0.0), 0.0, 0.0), 360.0);
        assert_eq!(angular_extent(&[[1.0, 0.0]], 0.0, 0.0), 0.0);
        assert_eq!(angular_extent(&[], 0.0, 0.0), 0.0);
    }

    #[test]
    fn a_sparse_arc_runs_across_the_gaps_its_sampling_leaves() {
        // A point every 10 degrees over 200, two missing in the middle (a
        // gap of 30): the plain test sees two short arcs, the sampled one
        // the whole.
        let xy = arc_points(200.0, 20, 10.0);
        assert!((angular_coverage(&xy, 0.0, 0.0).1 - 90.0).abs() < 1e-9);
        assert!((sampled_arc(&xy, 0.0, 0.0).1 - 200.0).abs() < 1e-9);
        // The share of the circle covered is that of the plain test: 18 sectors.
        assert_eq!(sampled_arc(&xy, 0.0, 0.0).0, angular_coverage(&xy, 0.0, 0.0).0);
        assert!((sampled_arc(&xy, 0.0, 0.0).0 - 0.5).abs() < 1e-9);
    }

    #[test]
    fn a_dense_arc_is_broken_by_a_gap() {
        // Two points a degree: 20 degrees without any is a real gap.
        let xy = arc_points(200.0, 400, 10.0);
        assert!((sampled_arc(&xy, 0.0, 0.0).1 - 99.5).abs() < 1e-9);
        assert_eq!(sampled_arc(&xy, 0.0, 0.0), angular_coverage(&xy, 0.0, 0.0));
    }

    #[test]
    fn a_short_arc_stays_short_however_sparse() {
        // Running across gaps joins points; it never extends an arc past the
        // sectors of its end points, and never crosses a gap wider than 35
        // degrees.
        let xy = arc_points(100.0, 20, 0.0);
        assert!((sampled_arc(&xy, 0.0, 0.0).1 - 105.0).abs() < 1e-9);
        // Two arcs of 55 and 35 degrees with 45 degrees between them.
        let mut two = arc_points(60.0, 12, 0.0);
        two.extend(arc_points(140.0, 28, 0.0).into_iter().skip(20));
        assert!((sampled_arc(&two, 0.0, 0.0).1 - 65.0).abs() < 1e-9);
        assert_eq!(sampled_arc(&[], 0.0, 0.0), (0.0, 0.0));
    }

    #[test]
    fn both_modes_find_every_stem() {
        let (pts, h, truth) = plot();
        for p in [StemParams { prefilter: false, ..Default::default() }, StemParams::coreg()] {
            let found = detect_stems_full(&pts, &h, &p);
            for &(x, y, r) in &truth {
                let s = found.iter().find(|s| (s.tree.x - x).hypot(s.tree.y - y) < 0.03).unwrap_or_else(|| panic!("missed ({x}, {y}) with {p:?}"));
                assert!((s.tree.dbh - 2.0 * r).abs() < 0.02, "dbh {} for r {r}", s.tree.dbh);
                assert!(s.axis[2] > 0.99 && (s.z - p.reference_height).abs() < 1e-9);
            }
            assert!(found.windows(2).all(|w| w[0].tree.quality >= w[1].tree.quality));
        }
    }

    #[test]
    fn coreg_mode_is_deterministic() {
        let (pts, h, _) = plot();
        let a = detect_stems(&pts, &h, &StemParams::coreg());
        let b = detect_stems(&pts, &h, &StemParams::coreg());
        assert_eq!(a.len(), b.len());
        assert!(a.iter().zip(&b).all(|(s, t)| s.x == t.x && s.dbh == t.dbh));
    }

    #[test]
    fn raster_clusters_come_in_ndimage_label_order() {
        // Cells (row = y, col = x): a U whose arms start at row 0, cols 0 and 4,
        // one cell at (0, 2) and one at (1, 6): scipy labels them 1 (U), 2, 3.
        let c = |col: f64, row: f64| [col * 0.06 + 0.03, row * 0.06 + 0.03];
        let mut xy = vec![c(6.0, 1.0)];
        for r in 0..4 {
            xy.push(c(0.0, r as f64));
            xy.push(c(4.0, r as f64));
        }
        for col in 1..4 {
            xy.push(c(col as f64, 3.0));
        }
        xy.push(c(2.0, 0.0));
        let cl = cluster_2d_raster(&xy, 0.06, 1);
        assert_eq!(cl.len(), 3);
        assert_eq!(cl[0], (1..12).collect::<Vec<_>>());
        assert_eq!(cl[1], vec![12]);
        assert_eq!(cl[2], vec![0]);
    }
}