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
// 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.
//! Trees matched between two epochs, and their increments.
//!
//! Matching is an optimal assignment (Kuhn 1955; Munkres 1957) on stem
//! positions, with DBH and height as tie-breakers, solved separately in
//! each group of trees that could be matched to one another. A pair is
//! allowed only within `max_distance`, and only when the second DBH is
//! at most `dbh_tolerance` larger and `max_shrink` smaller (relative to
//! the larger), so that a felled tree and a young one grown beside it are
//! not taken for one tree. Stems left over are then
//! checked for a merge (two stems of the first epoch found as one in the
//! second) or a split (the reverse) before they are called deaths or
//! recruits: the wide stem must exceed its own partner by the basal area of
//! the stems it would stand for, so a small tree that died beside a large
//! survivor is a death and not a merge.
//!
//! The DBH increment of a survivor is measured on the stem profile: circles
//! are fitted to thin slices between 1 and 3 m in each epoch, and the
//! increment is the weighted mean of the paired differences at the same
//! heights, which cancels the stem's own irregularity. Its standard error
//! combines each slice's circle-fit precision (at least the range noise of
//! the epoch), inflated by the scatter of the differences between slices
//! where that exceeds the expected, and the vertical registration
//! uncertainty times the stem taper. The minimum detectable increment is
//! that standard error times the normal quantile of the confidence level.

use std::collections::HashMap;

use rayon::prelude::*;

use crate::error::{Error, Result};
use crate::trees::{crown_metrics_all, crown_shape, fit_circle, fit_circle_ransac, RansacCircleParams};
use crate::Point;
use super::{positive, non_negative};

/// A tree as matching sees it.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TreeRow {
    /// x of the stem position (m); both epochs in one frame.
    pub x: f64,
    /// y of the stem position (m); both epochs in one frame.
    pub y: f64,
    /// DBH (m); NaN when unknown, which disables the DBH criteria for this tree.
    pub dbh: f64,
    /// Tree height (m); NaN when unknown, which drops the height term of the cost.
    pub height: f64,
}

/// Settings of [`match_trees`].
#[derive(Debug, Clone)]
pub struct MatchParams {
    /// Farthest two stems of one tree can be apart (m).
    pub max_distance: f64,
    /// Largest relative DBH increase `(b - a) / max(a, b)` of one tree.
    pub dbh_tolerance: f64,
    /// Largest relative DBH decrease `(a - b) / max(a, b)` of one tree:
    /// stems do not shrink, so this only absorbs measurement error.
    pub max_shrink: f64,
    /// Weights of the squared relative DBH and height differences in the
    /// cost, next to the squared distance over `max_distance`.
    pub dbh_weight: f64,
    /// Weight of the squared relative height difference in the cost.
    pub height_weight: f64,
    /// A stem explains neighbours that lost their match (a merge or a split)
    /// when the basal area it has beyond its own partner's is at least this
    /// fraction, squared, of theirs: `dbh² - partner² >= merge_factor² *
    /// sum(neighbour dbh²)`, with `partner = 0` for a stem that has no match
    /// (which must then stand for at least two neighbours). A stem no wider
    /// than its partner absorbed nothing, so a small tree that died beside a
    /// large survivor stays a death. Dimensionless; infinity switches merges
    /// and splits off.
    pub merge_factor: f64,
}

impl Default for MatchParams {
    fn default() -> Self {
        MatchParams { max_distance: 1.0, dbh_tolerance: 0.35, max_shrink: 0.15, dbh_weight: 1.0, height_weight: 0.25, merge_factor: 0.8 }
    }
}

/// Status of a tree of the first epoch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusA {
    /// Matched to a tree of the second epoch.
    Survivor,
    /// Not found in the second epoch.
    Dead,
    /// Found in the second epoch only as part of a neighbour's stem.
    Merged,
    /// Found in the second epoch as several stems, none matched to it.
    Split,
}

/// Status of a tree of the second epoch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusB {
    /// Matched to a tree of the first epoch.
    Survivor,
    /// Not present in the first epoch.
    Recruit,
    /// Part of a stem that the first epoch saw as one with a neighbour.
    Split,
    /// A stem, not matched itself, that stands for several stems of the
    /// first epoch.
    Merged,
}

impl StatusA {
    /// The status as a lower-case string: `survivor`, `dead`, `merged` or `split`.
    pub fn name(self) -> &'static str {
        match self {
            StatusA::Survivor => "survivor",
            StatusA::Dead => "dead",
            StatusA::Merged => "merged",
            StatusA::Split => "split",
        }
    }
}

impl StatusB {
    /// The status as a lower-case string: `survivor`, `recruit`, `split` or `merged`.
    pub fn name(self) -> &'static str {
        match self {
            StatusB::Survivor => "survivor",
            StatusB::Recruit => "recruit",
            StatusB::Split => "split",
            StatusB::Merged => "merged",
        }
    }
}

/// Result of [`match_trees`]; indices refer to the input slices.
#[derive(Debug, Clone, PartialEq)]
pub struct TreeMatch {
    /// `(a, b)` of every survivor, sorted by `a`.
    pub pairs: Vec<(usize, usize)>,
    /// Horizontal distance of each pair (m).
    pub distance: Vec<f64>,
    /// Assignment cost of each pair, `(d / max_distance)² + dbh_weight ddbh² + height_weight dh²` with the relative DBH and height differences.
    pub cost: Vec<f64>,
    /// Status of every tree of the first epoch.
    pub status_a: Vec<StatusA>,
    /// Status of every tree of the second epoch.
    pub status_b: Vec<StatusB>,
    /// For a merged tree of the first epoch, the stem of the second it went
    /// into; for a split tree of the second, the stem of the first it came
    /// from; otherwise the matched partner, or `None`.
    pub related_a: Vec<Option<usize>>,
    /// Counterpart of `related_a` for the trees of the second epoch.
    pub related_b: Vec<Option<usize>>,
}

/// Relative difference `(b - a) / max(a, b)`; 0 when either is unknown.
fn relative(a: f64, b: f64) -> f64 {
    if a.is_finite() && b.is_finite() && a.max(b) > 0.0 {
        (b - a) / a.max(b)
    } else {
        0.0
    }
}

/// Minimum-cost assignment of the rows of a `n x m` cost matrix (`n <= m`)
/// to distinct columns: the shortest augmenting path form of the Hungarian
/// method, O(n² m). Returns the column of each row.
///
/// Here the rows are one survey's trees, the columns the other's, and the
/// cost how poorly a pair matches, so the result is the pairing of trees that
/// is best *overall*. That is not the same as giving every tree its own
/// nearest neighbour: two trees cannot claim the same match, and accepting a
/// slightly worse pair here often allows a much better one there.
///
/// The implementation is the textbook one and reads as such: `u` and `v` are
/// the dual potentials, `p[j]` the row currently assigned to column `j`,
/// `way` the augmenting path back, and the 1-based indices with `0` as a
/// sentinel come from the same source. It is a self-contained numerical
/// routine; the names will mean something next to the method's description
/// and little without it.
pub fn assign(cost: &[Vec<f64>]) -> Vec<usize> {
    let n = cost.len();
    if n == 0 {
        return Vec::new();
    }
    let m = cost[0].len();
    assert!(n <= m, "assign needs no more rows than columns");
    let inf = f64::INFINITY;
    let (mut u, mut v) = (vec![0.0; n + 1], vec![0.0; m + 1]);
    let (mut p, mut way) = (vec![0usize; m + 1], vec![0usize; m + 1]);
    for i in 1..=n {
        p[0] = i;
        let mut j0 = 0usize;
        let mut minv = vec![inf; m + 1];
        let mut used = vec![false; m + 1];
        // Grow a shortest augmenting path from row `i` until it reaches a
        // column nothing is assigned to; the row then takes that column and
        // everything along the path shifts over by one.
        loop {
            used[j0] = true;
            let i0 = p[j0];
            let (mut delta, mut j1) = (inf, 0usize);
            for j in 1..=m {
                if !used[j] {
                    let cur = cost[i0 - 1][j - 1] - u[i0] - v[j];
                    if cur < minv[j] {
                        minv[j] = cur;
                        way[j] = j0;
                    }
                    if minv[j] < delta {
                        delta = minv[j];
                        j1 = j;
                    }
                }
            }
            for j in 0..=m {
                if used[j] {
                    u[p[j]] += delta;
                    v[j] -= delta;
                } else {
                    minv[j] -= delta;
                }
            }
            j0 = j1;
            if p[j0] == 0 {
                break;
            }
        }
        // Walk the path back, moving each row on it to the next column.
        loop {
            let j1 = way[j0];
            p[j0] = p[j1];
            j0 = j1;
            if j0 == 0 {
                break;
            }
        }
    }
    let mut out = vec![0usize; n];
    for j in 1..=m {
        if p[j] > 0 {
            out[p[j] - 1] = j - 1;
        }
    }
    out
}

fn find(parent: &mut [usize], i: usize) -> usize {
    let mut r = i;
    while parent[r] != r {
        r = parent[r];
    }
    let mut k = i;
    while parent[k] != r {
        let next = parent[k];
        parent[k] = r;
        k = next;
    }
    r
}

/// Whether a stem of diameter `dbh` (m) stands for the unmatched stems
/// `others` (m) of the other epoch besides its own `partner`: the basal area
/// it has beyond the partner's must be at least `factor²` of theirs. Without
/// a partner it must stand for at least two stems. False when any diameter
/// is unknown.
fn explains(dbh: f64, partner: Option<f64>, others: impl Iterator<Item = f64>, factor: f64) -> bool {
    let (mut sum, mut n) = (0.0, 0usize);
    for d in others {
        sum += d * d;
        n += 1;
    }
    let own = partner.unwrap_or(0.0);
    if !(dbh.is_finite() && own.is_finite() && sum.is_finite()) || n + usize::from(partner.is_some()) < 2 {
        return false;
    }
    dbh * dbh - own * own >= factor * factor * sum
}

/// Match the trees of two epochs given in one frame; see the module
/// documentation.
pub fn match_trees(a: &[TreeRow], b: &[TreeRow], p: &MatchParams) -> Result<TreeMatch> {
    if !positive(p.max_distance) || !positive(p.dbh_tolerance) || !non_negative(p.max_shrink) || !positive(p.merge_factor) || !non_negative(p.dbh_weight) || !non_negative(p.height_weight) {
        return Err(Error::invalid("max_distance, dbh_tolerance and merge_factor must be positive, max_shrink and the weights >= 0"));
    }
    if a.iter().chain(b).any(|t| !t.x.is_finite() || !t.y.is_finite()) {
        return Err(Error::invalid("tree positions must be finite"));
    }
    let (na, nb) = (a.len(), b.len());
    // Allowed pairs, found through a grid of max_distance cells.
    let cell = p.max_distance;
    let key = |t: &TreeRow| ((t.x / cell).floor() as i64, (t.y / cell).floor() as i64);
    let mut grid: HashMap<(i64, i64), Vec<usize>> = HashMap::new();
    for (j, t) in b.iter().enumerate() {
        grid.entry(key(t)).or_default().push(j);
    }
    let mut edges: Vec<(usize, usize, f64, f64)> = Vec::new();
    for (i, t) in a.iter().enumerate() {
        let (kx, ky) = key(t);
        for dx in -1..=1 {
            for dy in -1..=1 {
                for &j in grid.get(&(kx + dx, ky + dy)).map(|v| v.as_slice()).unwrap_or(&[]) {
                    let d = (t.x - b[j].x).hypot(t.y - b[j].y);
                    let rd = relative(t.dbh, b[j].dbh);
                    if d <= p.max_distance && rd <= p.dbh_tolerance && rd >= -p.max_shrink {
                        let rh = relative(t.height, b[j].height);
                        let c = (d / p.max_distance).powi(2) + p.dbh_weight * rd * rd + p.height_weight * rh * rh;
                        edges.push((i, j, d, c));
                    }
                }
            }
        }
    }
    edges.sort_by_key(|e| (e.0, e.1));
    // Groups of trees linked by allowed pairs, each assigned on its own.
    let mut parent: Vec<usize> = (0..na + nb).collect();
    for &(i, j, _, _) in &edges {
        let (ri, rj) = (find(&mut parent, i), find(&mut parent, na + j));
        if ri != rj {
            parent[ri.max(rj)] = ri.min(rj);
        }
    }
    let mut groups: HashMap<usize, (Vec<usize>, Vec<usize>)> = HashMap::new();
    for i in 0..na {
        let r = find(&mut parent, i);
        groups.entry(r).or_default().0.push(i);
    }
    for j in 0..nb {
        let r = find(&mut parent, na + j);
        groups.entry(r).or_default().1.push(j);
    }
    let edge_of: HashMap<(usize, usize), (f64, f64)> = edges.iter().map(|&(i, j, d, c)| ((i, j), (d, c))).collect();
    let mut roots: Vec<usize> = groups.keys().copied().collect();
    roots.sort_unstable();
    // Forbidden pairs cost more than all allowed ones together, so the
    // assignment first matches as many trees as it can.
    const FORBIDDEN: f64 = 1e9;
    let mut pairs: Vec<(usize, usize)> = Vec::new();
    for r in roots {
        let (ga, gb) = &groups[&r];
        if ga.is_empty() || gb.is_empty() {
            continue;
        }
        let transpose = ga.len() > gb.len();
        let (rows, cols) = if transpose { (gb, ga) } else { (ga, gb) };
        let cost: Vec<Vec<f64>> = rows
            .iter()
            .map(|&ri| cols.iter().map(|&ci| {
                let (i, j) = if transpose { (ci, ri) } else { (ri, ci) };
                edge_of.get(&(i, j)).map(|e| e.1).unwrap_or(FORBIDDEN)
            }).collect())
            .collect();
        for (k, c) in assign(&cost).into_iter().enumerate() {
            if cost[k][c] < FORBIDDEN {
                let (i, j) = if transpose { (cols[c], rows[k]) } else { (rows[k], cols[c]) };
                pairs.push((i, j));
            }
        }
    }
    pairs.sort_unstable();
    let mut status_a = vec![StatusA::Dead; na];
    let mut status_b = vec![StatusB::Recruit; nb];
    let mut related_a = vec![None; na];
    let mut related_b = vec![None; nb];
    let (mut distance, mut cost) = (Vec::with_capacity(pairs.len()), Vec::with_capacity(pairs.len()));
    for &(i, j) in &pairs {
        status_a[i] = StatusA::Survivor;
        status_b[j] = StatusB::Survivor;
        related_a[i] = Some(j);
        related_b[j] = Some(i);
        let (d, c) = edge_of[&(i, j)];
        distance.push(d);
        cost.push(c);
    }
    // Merges: a stem of epoch 2 wider than its partner (if any) by enough to
    // stand for every unmatched stem of epoch 1 within reach. Neighbours come
    // from the grids of `max_distance` cells, in index order.
    if p.merge_factor.is_finite() {
        let near = |t: &TreeRow, u: &TreeRow| (t.x - u.x).hypot(t.y - u.y) <= p.max_distance;
        let around = |g: &HashMap<(i64, i64), Vec<usize>>, t: &TreeRow| -> Vec<usize> {
            let (kx, ky) = key(t);
            let mut out: Vec<usize> = Vec::new();
            for dx in -1..=1 {
                for dy in -1..=1 {
                    if let Some(v) = g.get(&(kx + dx, ky + dy)) {
                        out.extend_from_slice(v);
                    }
                }
            }
            out.sort_unstable();
            out
        };
        let mut grid_a: HashMap<(i64, i64), Vec<usize>> = HashMap::new();
        for (i, t) in a.iter().enumerate() {
            grid_a.entry(key(t)).or_default().push(i);
        }
        for j in 0..nb {
            if !b[j].dbh.is_finite() {
                continue;
            }
            let lost: Vec<usize> = around(&grid_a, &b[j]).into_iter().filter(|&i| status_a[i] == StatusA::Dead && near(&a[i], &b[j])).collect();
            if lost.is_empty() {
                continue;
            }
            let partner = related_b[j].map(|i| a[i].dbh);
            if explains(b[j].dbh, partner, lost.iter().map(|&i| a[i].dbh), p.merge_factor) {
                for &i in &lost {
                    status_a[i] = StatusA::Merged;
                    related_a[i] = Some(j);
                }
                if status_b[j] == StatusB::Recruit {
                    status_b[j] = StatusB::Merged;
                    related_b[j] = Some(lost[0]);
                }
            }
        }
        // Splits, the mirror image.
        for i in 0..na {
            if !a[i].dbh.is_finite() {
                continue;
            }
            let new: Vec<usize> = around(&grid, &a[i]).into_iter().filter(|&j| status_b[j] == StatusB::Recruit && near(&a[i], &b[j])).collect();
            if new.is_empty() {
                continue;
            }
            let partner = related_a[i].filter(|_| status_a[i] == StatusA::Survivor).map(|j| b[j].dbh);
            if explains(a[i].dbh, partner, new.iter().map(|&j| b[j].dbh), p.merge_factor) {
                for &j in &new {
                    status_b[j] = StatusB::Split;
                    related_b[j] = Some(i);
                }
                if status_a[i] == StatusA::Dead {
                    status_a[i] = StatusA::Split;
                    related_a[i] = Some(new[0]);
                }
            }
        }
    }
    Ok(TreeMatch { pairs, distance, cost, status_a, status_b, related_a, related_b })
}

// ------------------------------------------------------------ measurement

/// Settings of [`measure_trees`] and [`tree_increments`].
#[derive(Debug, Clone)]
pub struct IncrementParams {
    /// Heights (m above ground) of the stem slices.
    pub slice_heights: Vec<f64>,
    /// Vertical extent (m) of each slice, centred on its height.
    pub slice_thickness: f64,
    /// Only points this close to the stem centre (m) enter a slice. It is a
    /// lower bound: for a wide stem the radius grows to [`slice_radius`]
    /// (1.5 times the median distance of the tree's points at the slice
    /// heights from the stem centre, plus 0.1 m), so that a stem wider than
    /// twice this value is still measured.
    pub search_radius: f64,
    /// Fewest slices measured in both epochs for a DBH increment.
    pub min_slices: usize,
    /// Two-sided confidence of the minimum detectable increment.
    pub confidence: f64,
    /// DBH increments above this (m) are implausible; NaN for no limit.
    pub max_dbh_increment: f64,
    /// Height points at the top of a tree used for its height uncertainty.
    pub top_points: usize,
    /// Assumed one-sigma error of a measured top height, as a fraction of
    /// the height: the top of a tree can be missed (occluded or too thin
    /// to be hit) in a way no single epoch shows.
    pub height_error: f64,
    /// Unassigned points (label < 0) within this horizontal distance (m) of
    /// the stem that continue the tree upwards, with vertical gaps below
    /// `top_gap`, count towards its top: segmentation often leaves a thin
    /// leader unassigned.
    pub top_radius: f64,
    /// Largest vertical gap (m) between successive unassigned points continuing a tree's top.
    pub top_gap: f64,
    /// Distance (m) from the RANSAC circle within which slice points are
    /// inliers; NaN for `max(0.01, 3 sigma)`. Give both epochs the same
    /// value, so that their circles are fitted alike.
    pub inlier_threshold: f64,
    /// Correlation of the diameter errors of the slices of one stem in one
    /// epoch: the same scanners see every slice from the same directions,
    /// so edge effects and occlusion repeat up the stem and averaging over
    /// slices removes only part of the error.
    pub slice_correlation: f64,
    /// Absolute one-sigma accuracy (m) of a single-epoch DBH, beyond its
    /// precision: bark roughness and edge points widen every circle of a
    /// stem alike. It cancels in increments and is added only to `dbh_se`.
    pub dbh_accuracy: f64,
}

impl Default for IncrementParams {
    fn default() -> Self {
        IncrementParams { slice_heights: (0..9).map(|k| 1.0 + 0.25 * k as f64).collect(), slice_thickness: 0.1, search_radius: 0.75, min_slices: 3, confidence: 0.95, max_dbh_increment: f64::NAN, top_points: 5, height_error: 0.02, top_radius: 0.3, top_gap: 0.5, inlier_threshold: f64::NAN, slice_correlation: 0.3, dbh_accuracy: 0.002 }
    }
}

/// One epoch's measurement of one tree.
#[derive(Debug, Clone, PartialEq)]
pub struct TreeMeasure {
    /// Diameter and its standard error at each slice height (NaN if none).
    pub diameter: Vec<f64>,
    /// Standard error of the fitted diameter at each slice (m); NaN where no circle was fitted.
    pub diameter_se: Vec<f64>,
    /// DBH at 1.3 m from a weighted line through the slice diameters.
    pub dbh: f64,
    /// Standard error of `dbh` (m), including `dbh_accuracy`.
    pub dbh_se: f64,
    /// Change of diameter per metre of height (negative for a tapering stem).
    pub taper: f64,
    /// Highest point above ground and its uncertainty.
    pub height: f64,
    /// One-sigma uncertainty of `height` (m).
    pub height_se: f64,
    /// Crown projection area (m²); NaN when the tree has no crown metrics.
    pub crown_area: f64,
    /// Crown volume (m³) of the stacked hulls of the crown; NaN when the tree has no crown metrics.
    pub crown_volume: f64,
    /// Points labelled with this tree (finite height only).
    pub n_points: usize,
}

/// The standard normal quantile (Acklam's rational approximation, relative
/// error below 1.2e-9).
#[allow(clippy::excessive_precision)]
pub fn normal_quantile(p: f64) -> f64 {
    const A: [f64; 6] = [-3.969683028665376e1, 2.209460984245205e2, -2.759285104469687e2, 1.383577518672690e2, -3.066479806614716e1, 2.506628277459239];
    const B: [f64; 5] = [-5.447609879822406e1, 1.615858368580409e2, -1.556989798598866e2, 6.680131188771972e1, -1.328068155288572e1];
    const C: [f64; 6] = [-7.784894002430293e-3, -3.223964580411365e-1, -2.400758277161838, -2.549732539343734, 4.374664141464968, 2.938163982698783];
    const D: [f64; 4] = [7.784695709041462e-3, 3.224671290700398e-1, 2.445134137142996, 3.754408661907416];
    if p.is_nan() || p <= 0.0 || p >= 1.0 {
        return f64::NAN;
    }
    let lo = 0.02425;
    if p < lo {
        let q = (-2.0 * p.ln()).sqrt();
        (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5]) / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
    } else if p <= 1.0 - lo {
        let q = p - 0.5;
        let r = q * q;
        (((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q / (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0)
    } else {
        -normal_quantile(1.0 - p)
    }
}

/// Circle fit of one slice: diameter, its standard error, or NaN.
fn slice_diameter(xy: &[[f64; 2]], sigma: f64, threshold: f64, max_radius: f64) -> (f64, f64) {
    if xy.len() < 10 {
        return (f64::NAN, f64::NAN);
    }
    let ransac = RansacCircleParams { threshold, max_radius, ..Default::default() };
    let Ok((_, _, _, inl)) = fit_circle_ransac(xy, &ransac) else { return (f64::NAN, f64::NAN) };
    let pts: Vec<[f64; 2]> = xy.iter().zip(&inl).filter(|(_, &k)| k).map(|(q, _)| *q).collect();
    if pts.len() < 10 {
        return (f64::NAN, f64::NAN);
    }
    let Ok((cx2, cy2, r, rmse)) = fit_circle(&pts) else { return (f64::NAN, f64::NAN) };
    if !r.is_finite() || r > max_radius {
        return (f64::NAN, f64::NAN);
    }
    // Share of the circumference seen, in 10-degree sectors.
    let mut seen = [false; 36];
    for q in &pts {
        let a = (q[1] - cy2).atan2(q[0] - cx2).to_degrees().rem_euclid(360.0);
        seen[((a / 10.0) as usize).min(35)] = true;
    }
    let coverage = seen.iter().filter(|&&s| s).count() as f64 / 36.0;
    let s = rmse.max(sigma);
    // Radius precision of a circle fitted to n points with radial noise s
    // is s / sqrt(n) on a full circle; a partial arc is less well
    // conditioned.
    (2.0 * r, 2.0 * s / (pts.len() as f64).sqrt() / coverage.max(0.1).sqrt())
}

/// Weighted least-squares line `d = c0 + c1 (h - 1.3)`: value at 1.3, its
/// standard error and the slope. Standard errors are scaled by the Birge
/// ratio where the points scatter more than their errors.
fn line_at_breast_height(h: &[f64], d: &[f64], se: &[f64]) -> (f64, f64, f64) {
    let rows: Vec<(f64, f64, f64)> = (0..h.len()).filter(|&k| d[k].is_finite() && se[k] > 0.0).map(|k| (h[k] - 1.3, d[k], 1.0 / (se[k] * se[k]))).collect();
    if rows.len() < 2 {
        return rows.first().map(|r| (r.1, 1.0 / r.2.sqrt(), f64::NAN)).unwrap_or((f64::NAN, f64::NAN, f64::NAN));
    }
    let (sw, sx, sy, sxx, sxy) = rows.iter().fold((0.0, 0.0, 0.0, 0.0, 0.0), |a, r| (a.0 + r.2, a.1 + r.2 * r.0, a.2 + r.2 * r.1, a.3 + r.2 * r.0 * r.0, a.4 + r.2 * r.0 * r.1));
    let det = sw * sxx - sx * sx;
    if det.abs() < 1e-300 {
        return (sy / sw, (1.0 / sw).sqrt(), f64::NAN);
    }
    let c1 = (sw * sxy - sx * sy) / det;
    let c0 = (sy - c1 * sx) / sw;
    let var0 = sxx / det;
    let chi2: f64 = rows.iter().map(|r| r.2 * (r.1 - c0 - c1 * r.0).powi(2)).sum::<f64>();
    let dof = rows.len() as f64 - 2.0;
    let birge = if dof > 0.0 { (chi2 / dof).max(1.0) } else { 1.0 };
    (c0, (var0 * birge).sqrt(), c1)
}

/// Harmonic mean of the squared standard errors of the measured slices:
/// the error variance of a mean over fully correlated slices.
fn harmonic_variance(d: &[f64], se: &[f64]) -> f64 {
    let w: Vec<f64> = (0..d.len()).filter(|&k| d[k].is_finite() && se[k] > 0.0).map(|k| 1.0 / (se[k] * se[k])).collect();
    if w.is_empty() {
        f64::NAN
    } else {
        w.len() as f64 / w.iter().sum::<f64>()
    }
}

/// Drop slices that do not fit the stem: a slice cut through a branch
/// junction or a neighbour fits a circle far off the taper line of the
/// others. The worst slice is dropped while it lies more than 4 standard
/// errors and 1 cm from the line through the rest, keeping at least three.
fn reject_off_line(h: &[f64], d: &mut [f64], se: &mut [f64]) {
    loop {
        let valid: Vec<usize> = (0..d.len()).filter(|&k| d[k].is_finite()).collect();
        if valid.len() <= 3 {
            return;
        }
        let mut worst = (0.0, usize::MAX);
        for &k in &valid {
            // The line through the other slices, so an outlier cannot pull
            // the line towards itself.
            let (hh, dd, ss): (Vec<f64>, Vec<f64>, Vec<f64>) = valid.iter().filter(|&&q| q != k).map(|&q| (h[q], d[q], se[q])).fold((vec![], vec![], vec![]), |mut a, x| {
                a.0.push(x.0);
                a.1.push(x.1);
                a.2.push(x.2);
                a
            });
            let (c0, c0_se, c1) = line_at_breast_height(&hh, &dd, &ss);
            let fit = c0 + if c1.is_finite() { c1 * (h[k] - 1.3) } else { 0.0 };
            let r = (d[k] - fit).abs();
            let z = r / (se[k] * se[k] + c0_se * c0_se).sqrt();
            if r > 0.01 && z > 4.0 && z > worst.0 {
                worst = (z, k);
            }
        }
        if worst.1 == usize::MAX {
            return;
        }
        d[worst.1] = f64::NAN;
        se[worst.1] = f64::NAN;
    }
}

/// Radius (m) around a stem centre within which points enter its slices:
/// `search_radius`, or `1.5 * median + 0.1` if that is larger, with `median`
/// the median horizontal distance (m) of `dist` (the tree's points between
/// the lowest and highest slice) from the stem centre. Points on a stem lie
/// one stem radius from its centre, so a fixed radius smaller than that
/// would leave a large stem without a single point.
pub fn slice_radius(dist: &mut [f64], search_radius: f64) -> f64 {
    if dist.is_empty() {
        return search_radius;
    }
    let mid = dist.len() / 2;
    let (_, median, _) = dist.select_nth_unstable_by(mid, f64::total_cmp);
    if median.is_finite() { search_radius.max(1.5 * *median + 0.1) } else { search_radius }
}

/// Measure every tree of one epoch: stem profile, DBH, top height and
/// crown. Each tree's slices take the points within [`slice_radius`] of its
/// position. `trees` are `(tree_id, x, y)`; `labels` give each point's tree.
/// `sigma` is the epoch's range noise (m), a floor on the circle-fit
/// residual.
pub fn measure_trees(points: &[Point], heights: &[f64], labels: &[i64], trees: &[(i64, f64, f64)], sigma: f64, p: &IncrementParams) -> Result<Vec<TreeMeasure>> {
    if points.len() != heights.len() || points.len() != labels.len() {
        return Err(Error::invalid("points, heights and labels differ in length"));
    }
    if !positive(p.slice_thickness) || !positive(p.search_radius) || !non_negative(sigma) {
        return Err(Error::invalid("slice_thickness and search_radius must be positive and sigma >= 0"));
    }
    let mut groups: HashMap<i64, Vec<usize>> = HashMap::new();
    for (i, &l) in labels.iter().enumerate() {
        if l >= 0 && heights[i].is_finite() {
            groups.entry(l).or_default().push(i);
        }
    }
    let crowns: HashMap<i64, [f64; 4]> = crown_metrics_all(points, heights, labels, 0.1).into_iter().collect();
    let empty = Vec::new();
    let threshold = if p.inlier_threshold.is_finite() { p.inlier_threshold } else { (3.0 * sigma).max(0.01) };
    // Unassigned points on a grid, for the tops segmentation left out.
    let cell = p.top_radius.max(0.1);
    let mut loose: HashMap<(i64, i64), Vec<usize>> = HashMap::new();
    if p.top_radius > 0.0 {
        for (i, &l) in labels.iter().enumerate() {
            if l < 0 && heights[i].is_finite() {
                loose.entry(((points[i][0] / cell).floor() as i64, (points[i][1] / cell).floor() as i64)).or_default().push(i);
            }
        }
    }
    Ok(trees
        .par_iter()
        .map(|&(id, cx, cy)| {
            let idx = groups.get(&id).unwrap_or(&empty);
            let half = p.slice_thickness / 2.0;
            // The search radius of this tree, widened for a stem that the
            // fixed one would not hold.
            let (h_lo, h_hi) = p.slice_heights.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |a, &h| (a.0.min(h - half), a.1.max(h + half)));
            let mut dist: Vec<f64> = idx.iter().filter(|&&i| heights[i] >= h_lo && heights[i] <= h_hi).map(|&i| (points[i][0] - cx).hypot(points[i][1] - cy)).collect();
            let radius = slice_radius(&mut dist, p.search_radius);
            let (mut diameter, mut diameter_se) = (Vec::with_capacity(p.slice_heights.len()), Vec::with_capacity(p.slice_heights.len()));
            for &hh in &p.slice_heights {
                let xy: Vec<[f64; 2]> = idx.iter().filter(|&&i| (heights[i] - hh).abs() <= half && (points[i][0] - cx).hypot(points[i][1] - cy) <= radius).map(|&i| [points[i][0], points[i][1]]).collect();
                let (d, se) = slice_diameter(&xy, sigma, threshold, radius);
                diameter.push(d);
                diameter_se.push(se);
            }
            reject_off_line(&p.slice_heights, &mut diameter, &mut diameter_se);
            let (dbh, dbh_se, taper) = line_at_breast_height(&p.slice_heights, &diameter, &diameter_se);
            let rho = p.slice_correlation;
            let dbh_se = (dbh_se * dbh_se * (1.0 - rho) + rho * harmonic_variance(&diameter, &diameter_se) + p.dbh_accuracy * p.dbh_accuracy).sqrt();
            // Top height: the highest point, continued upwards through
            // unassigned points above the stem; its uncertainty from how far
            // the next highest points lie below it, and the assumed error of
            // a missed top.
            let mut hs: Vec<f64> = idx.iter().map(|&i| heights[i]).collect();
            hs.sort_by(|x, y| y.total_cmp(x));
            if let Some(&top) = hs.first() {
                let (kx, ky) = ((cx / cell).floor() as i64, (cy / cell).floor() as i64);
                let mut above: Vec<f64> = Vec::new();
                for dx in -1..=1 {
                    for dy in -1..=1 {
                        for &i in loose.get(&(kx + dx, ky + dy)).map(|v| v.as_slice()).unwrap_or(&[]) {
                            if heights[i] > top && (points[i][0] - cx).hypot(points[i][1] - cy) <= p.top_radius {
                                above.push(heights[i]);
                            }
                        }
                    }
                }
                above.sort_by(f64::total_cmp);
                let mut reached = top;
                for h in above {
                    if h - reached > p.top_gap {
                        break;
                    }
                    reached = h;
                    hs.insert(0, h);
                }
            }
            let (height, height_se) = if hs.is_empty() {
                (f64::NAN, f64::NAN)
            } else {
                let k = p.top_points.max(2).min(hs.len());
                let spread = hs[0] - hs[k - 1];
                (hs[0], (spread.max(sigma).max(0.01).powi(2) + (p.height_error * hs[0]).powi(2)).sqrt())
            };
            let (crown_area, crown_volume) = match crowns.get(&id) {
                Some(m) => {
                    let pts: Vec<Point> = idx.iter().map(|&i| [points[i][0], points[i][1], heights[i]]).collect();
                    (m[0], crown_shape(&pts, Some([cx, cy]), m[1], 0.5).volume)
                }
                None => (f64::NAN, f64::NAN),
            };
            TreeMeasure { diameter, diameter_se, dbh, dbh_se, taper, height, height_se, crown_area, crown_volume, n_points: idx.len() }
        })
        .collect())
}

/// Classification of an increment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Change {
    /// At least the minimum detectable increment.
    Growth,
    /// Smaller in magnitude than the minimum detectable increment.
    BelowDetection,
    /// A decrease of at least the minimum detectable increment.
    Decrease,
    /// Not enough data in one of the epochs.
    Unmeasured,
}

impl Change {
    /// The class as a lower-case string: `growth`, `below_detection`, `decrease` or `unmeasured`.
    pub fn name(self) -> &'static str {
        match self {
            Change::Growth => "growth",
            Change::BelowDetection => "below_detection",
            Change::Decrease => "decrease",
            Change::Unmeasured => "unmeasured",
        }
    }
}

/// Increment of one survivor.
#[derive(Debug, Clone, PartialEq)]
pub struct Increment {
    /// DBH increment (m), the weighted mean of the paired slice differences; NaN with fewer than `min_slices` pairs.
    pub d_dbh: f64,
    /// Standard error of `d_dbh` (m), registration uncertainty included.
    pub d_dbh_se: f64,
    /// Minimum detectable DBH increment (m): `d_dbh_se` times the normal quantile of `confidence`.
    pub d_dbh_mdi: f64,
    /// Slices measured in both epochs.
    pub n_slices: usize,
    /// Class of the DBH increment against its minimum detectable increment.
    pub dbh_change: Change,
    /// Height increment (m), second epoch minus first.
    pub d_height: f64,
    /// Standard error of `d_height` (m), from the two height uncertainties.
    pub d_height_se: f64,
    /// Minimum detectable height increment (m).
    pub d_height_mdi: f64,
    /// Class of the height increment against its minimum detectable increment.
    pub height_change: Change,
    /// Crown projection area change (m²), descriptive only (no detection level).
    pub d_crown_area: f64,
    /// Crown volume change (m³), descriptive only (no detection level).
    pub d_crown_volume: f64,
    /// A decrease in DBH beyond detection, or an increase above
    /// `max_dbh_increment`.
    pub implausible: bool,
}

fn classify(delta: f64, mdi: f64) -> Change {
    if !delta.is_finite() || !mdi.is_finite() {
        Change::Unmeasured
    } else if delta >= mdi {
        Change::Growth
    } else if delta <= -mdi {
        Change::Decrease
    } else {
        Change::BelowDetection
    }
}

/// Increment of one tree from its two measurements; `registration_sigma`
/// is the one-sigma vertical uncertainty (m) of the alignment of the epochs.
pub fn increment(a: &TreeMeasure, b: &TreeMeasure, registration_sigma: f64, p: &IncrementParams) -> Increment {
    let z = normal_quantile(0.5 + p.confidence / 2.0);
    let (mut sw, mut swd, mut rows) = (0.0, 0.0, Vec::<(f64, f64)>::new());
    for k in 0..a.diameter.len().min(b.diameter.len()) {
        let (da, db) = (a.diameter[k], b.diameter[k]);
        let var = a.diameter_se[k].powi(2) + b.diameter_se[k].powi(2);
        if da.is_finite() && db.is_finite() && var > 0.0 {
            sw += 1.0 / var;
            swd += (db - da) / var;
            rows.push((db - da, 1.0 / var));
        }
    }
    // Drop the worst paired difference while it is more than 4 of its own
    // standard errors from the mean of the others.
    while rows.len() > p.min_slices.max(3) {
        let mut worst = (0.0, usize::MAX);
        for (k, &(diff, w)) in rows.iter().enumerate() {
            let (sw_o, swd_o) = (sw - w, swd - diff * w);
            let z = (diff - swd_o / sw_o).abs() / (1.0 / w + 1.0 / sw_o).sqrt();
            if z > 4.0 && z > worst.0 {
                worst = (z, k);
            }
        }
        if worst.1 == usize::MAX {
            break;
        }
        let r = rows.remove(worst.1);
        sw -= r.1;
        swd -= r.0 * r.1;
    }
    let n = rows.len();
    let (d_dbh, d_dbh_se) = if n >= p.min_slices.max(1) {
        let mean = swd / sw;
        let chi2: f64 = rows.iter().map(|r| r.1 * (r.0 - mean).powi(2)).sum();
        let birge = if n > 1 { (chi2 / (n as f64 - 1.0)).max(1.0) } else { 1.0 };
        let taper = if a.taper.is_finite() { a.taper } else { 0.0 };
        let rho = p.slice_correlation;
        (mean, (birge * (1.0 - rho) / sw + rho * n as f64 / sw + (taper * registration_sigma).powi(2)).sqrt())
    } else {
        (f64::NAN, f64::NAN)
    };
    let d_dbh_mdi = z * d_dbh_se;
    let dbh_change = classify(d_dbh, d_dbh_mdi);
    let d_height = b.height - a.height;
    let d_height_se = (a.height_se.powi(2) + b.height_se.powi(2)).sqrt();
    let d_height_mdi = z * d_height_se;
    let too_fast = p.max_dbh_increment.is_finite() && d_dbh > p.max_dbh_increment;
    Increment {
        d_dbh,
        d_dbh_se,
        d_dbh_mdi,
        n_slices: n,
        dbh_change,
        d_height,
        d_height_se,
        d_height_mdi,
        height_change: classify(d_height, d_height_mdi),
        d_crown_area: b.crown_area - a.crown_area,
        d_crown_volume: b.crown_volume - a.crown_volume,
        implausible: dbh_change == Change::Decrease || too_fast,
    }
}

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

    fn row(x: f64, y: f64, dbh: f64) -> TreeRow {
        TreeRow { x, y, dbh, height: f64::NAN }
    }

    #[test]
    fn assignment_is_optimal() {
        // Greedy would take (0, 0) at cost 1 and pay 10 for the rest.
        let cost = vec![vec![1.0, 2.0], vec![2.0, 10.0]];
        assert_eq!(assign(&cost), vec![1, 0]);
        let cost = vec![vec![4.0, 1.0, 3.0], vec![2.0, 0.0, 5.0], vec![3.0, 2.0, 2.0]];
        let a = assign(&cost);
        assert_eq!(a.iter().enumerate().map(|(i, &j)| cost[i][j]).sum::<f64>(), 5.0);
        assert_eq!(assign(&[vec![3.0, 1.0, 2.0]]), vec![1]);
    }

    #[test]
    fn survivors_deaths_recruits() {
        let a = [row(0.0, 0.0, 0.3), row(5.0, 0.0, 0.2), row(10.0, 0.0, 0.4)];
        let b = [row(0.1, 0.05, 0.31), row(10.2, 0.0, 0.41), row(20.0, 0.0, 0.1)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert_eq!(m.pairs, vec![(0, 0), (2, 1)]);
        assert_eq!(m.status_a, vec![StatusA::Survivor, StatusA::Dead, StatusA::Survivor]);
        assert_eq!(m.status_b, vec![StatusB::Survivor, StatusB::Survivor, StatusB::Recruit]);
    }

    #[test]
    fn crossing_neighbours_take_the_cheaper_assignment() {
        // Two stems 0.6 m apart; the second epoch saw both slightly moved
        // towards each other. Nearest-first would pair a0 with b1.
        let a = [row(0.0, 0.0, 0.3), row(0.6, 0.0, 0.2)];
        let b = [row(0.35, 0.0, 0.2), row(0.25, 0.0, 0.31)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert_eq!(m.pairs, vec![(0, 1), (1, 0)]);
    }

    #[test]
    fn felled_and_replaced_is_not_a_survivor() {
        let a = [row(0.0, 0.0, 0.4)];
        let b = [row(0.4, 0.0, 0.09)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert!(m.pairs.is_empty());
        assert_eq!((m.status_a[0], m.status_b[0]), (StatusA::Dead, StatusB::Recruit));
    }

    #[test]
    fn merged_and_split_stems() {
        // Two stems seen as one wide stem in the other epoch.
        let a = [row(0.0, 0.0, 0.2), row(0.4, 0.0, 0.18)];
        let b = [row(0.2, 0.0, 0.3)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert!(m.status_b[0] == StatusB::Survivor || m.status_b[0] == StatusB::Merged);
        assert!(m.status_a.contains(&StatusA::Merged) && !m.status_a.contains(&StatusA::Dead));
        let m = match_trees(&b, &a, &MatchParams::default()).unwrap();
        assert!(m.status_b.contains(&StatusB::Split) && !m.status_b.contains(&StatusB::Recruit));
        assert!(!m.status_a.contains(&StatusA::Dead));
    }

    #[test]
    fn a_small_death_beside_a_large_survivor_is_not_a_merge() {
        // A 10 cm tree 0.8 m from a 50 cm one; only the large tree is found
        // again, 5 mm wider: it did not absorb the small one.
        let a = [row(0.0, 0.0, 0.50), row(0.8, 0.0, 0.10)];
        let b = [row(0.01, 0.0, 0.505)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert_eq!(m.status_a, vec![StatusA::Survivor, StatusA::Dead]);
        assert_eq!(m.status_b, vec![StatusB::Survivor]);
        // The mirror image: a small recruit beside a large survivor.
        let m = match_trees(&b, &a, &MatchParams::default()).unwrap();
        assert_eq!(m.status_a, vec![StatusA::Survivor]);
        assert_eq!(m.status_b, vec![StatusB::Survivor, StatusB::Recruit]);
        // A partner wide enough to hold both is still a merge.
        let b = [row(0.01, 0.0, 0.52)];
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert_eq!(m.status_a, vec![StatusA::Survivor, StatusA::Merged]);
    }

    #[test]
    fn merges_and_splits_do_not_scan_every_pair() {
        // 60 000 trees a side: the passes over neighbours must stay near
        // linear (all pairs would be 3.6e9 distance tests per pass).
        let n = 60_000usize;
        let side = 1400.0;
        let mut rng = crate::util::nprandom::Generator::new(5);
        let a: Vec<TreeRow> = (0..n).map(|_| row(rng.uniform(0.0, side), rng.uniform(0.0, side), rng.uniform(0.1, 0.5))).collect();
        let b: Vec<TreeRow> = (0..n).map(|_| row(rng.uniform(0.0, side), rng.uniform(0.0, side), rng.uniform(0.1, 0.5))).collect();
        let t = std::time::Instant::now();
        let m = match_trees(&a, &b, &MatchParams::default()).unwrap();
        assert!(t.elapsed().as_secs_f64() < 20.0, "{:?}", t.elapsed());
        assert!(m.status_a.contains(&StatusA::Dead));
        // Infinity switches merges and splits off.
        let off = match_trees(&a, &b, &MatchParams { merge_factor: f64::INFINITY, ..Default::default() }).unwrap();
        assert!(!off.status_a.contains(&StatusA::Merged) && !off.status_b.contains(&StatusB::Split));
    }

    #[test]
    fn rejects_bad_input() {
        assert!(match_trees(&[row(f64::NAN, 0.0, 0.2)], &[], &MatchParams::default()).is_err());
        assert!(match_trees(&[], &[], &MatchParams { max_distance: 0.0, ..Default::default() }).is_err());
        let m = match_trees(&[], &[], &MatchParams::default()).unwrap();
        assert!(m.pairs.is_empty());
    }

    #[test]
    fn quantile() {
        assert!((normal_quantile(0.975) - 1.959963984540054).abs() < 1e-8);
        assert!((normal_quantile(0.01) + 2.326347874040841).abs() < 1e-8);
        assert!(normal_quantile(0.5).abs() < 1e-12);
    }

    /// A vertical stem of diameter `d` sampled all round at the slice heights.
    fn stem(d: f64, noise: f64, seed: u64) -> (Vec<Point>, Vec<f64>) {
        let mut rng = crate::util::nprandom::Generator::new(seed);
        let mut pts = Vec::new();
        for _ in 0..6000 {
            let h = rng.uniform(0.8, 3.2);
            let a = rng.uniform(0.0, std::f64::consts::TAU);
            let r = d / 2.0 - 0.01 * (h - 1.3) + rng.normal(0.0, noise);
            pts.push([5.0 + r * a.cos(), 5.0 + r * a.sin(), h]);
        }
        let h = pts.iter().map(|p| p[2]).collect();
        (pts, h)
    }

    #[test]
    fn a_stem_wider_than_the_search_radius_is_measured() {
        // DBH 1.8 m: every surface point lies 0.9 m from the centre, beyond
        // the default search radius of 0.75 m.
        let p = IncrementParams::default();
        let (pts, h) = stem(1.8, 0.003, 4);
        let m = measure_trees(&pts, &h, &vec![1; pts.len()], &[(1, 5.0, 5.0)], 0.003, &p).unwrap();
        assert!((m[0].dbh - 1.8).abs() < 0.01, "{}", m[0].dbh);
        assert!(m[0].diameter.iter().all(|d| d.is_finite()));
        // An ordinary stem keeps the radius asked for.
        let mut d = vec![0.15; 100];
        assert_eq!(slice_radius(&mut d, 0.75), 0.75);
        assert_eq!(slice_radius(&mut [], 0.75), 0.75);
        let mut d = vec![0.9; 100];
        assert!((slice_radius(&mut d, 0.75) - 1.45).abs() < 1e-12);
    }

    #[test]
    fn increment_of_a_growing_stem() {
        let p = IncrementParams::default();
        let (pa, ha) = stem(0.3, 0.003, 1);
        let (pb, hb) = stem(0.31, 0.003, 2);
        let ma = measure_trees(&pa, &ha, &vec![1; pa.len()], &[(1, 5.0, 5.0)], 0.003, &p).unwrap();
        let mb = measure_trees(&pb, &hb, &vec![1; pb.len()], &[(1, 5.0, 5.0)], 0.003, &p).unwrap();
        assert!((ma[0].dbh - 0.3).abs() < 0.002, "{}", ma[0].dbh);
        assert!((ma[0].taper + 0.02).abs() < 0.003);
        let inc = increment(&ma[0], &mb[0], 0.01, &p);
        assert_eq!(inc.n_slices, 9);
        assert!((inc.d_dbh - 0.01).abs() < 3.0 * inc.d_dbh_se, "{} {}", inc.d_dbh, inc.d_dbh_se);
        assert_eq!(inc.dbh_change, Change::Growth);
        // A 0.2 mm increment is below detection.
        let (pc, hc) = stem(0.3002, 0.003, 3);
        let mc = measure_trees(&pc, &hc, &vec![1; pc.len()], &[(1, 5.0, 5.0)], 0.003, &p).unwrap();
        assert_eq!(increment(&ma[0], &mc[0], 0.01, &p).dbh_change, Change::BelowDetection);
        // A tree without points is unmeasured.
        let none = measure_trees(&pa, &ha, &vec![-1; pa.len()], &[(1, 5.0, 5.0)], 0.003, &p).unwrap();
        assert_eq!(increment(&ma[0], &none[0], 0.01, &p).dbh_change, Change::Unmeasured);
    }
}