ringgrid 0.9.0

Pure-Rust detector for coded ring calibration targets
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
use crate::detector::marker_build::MarkerRecord;

use super::index::dist2;
use super::types::{ConsistencyEvidence, ScrubStage, Trust};
use super::vote::{VoteOutcome, gather_trusted_neighbors_local_scale, vote_for_candidate};
use super::workspace::{
    IdCorrectionWorkspace, clear_marker_id, is_soft_locked_assignment, marker_center_is_finite,
};

pub(super) fn local_edge_neighbor_ids(
    marker_index: usize,
    markers: &[MarkerRecord],
    board_index: &super::index::BoardIndex,
    outer_radii_px: &[f64],
    outer_mul: f64,
) -> Vec<usize> {
    let center_i = markers[marker_index].center;
    let radius_i = outer_radii_px[marker_index];
    let mut out = Vec::<usize>::new();
    for (j, m) in markers.iter().enumerate() {
        if j == marker_index {
            continue;
        }
        let Some(id_j) = m.id else {
            continue;
        };
        if !board_index.id_to_xy.contains_key(&id_j) || !marker_center_is_finite(m) {
            continue;
        }
        let radius_j = outer_radii_px[j];
        let gate = outer_mul * 0.5 * (radius_i + radius_j);
        if gate <= 0.0 || !gate.is_finite() {
            continue;
        }
        if dist2(center_i, m.center) <= gate * gate {
            out.push(id_j);
        }
    }
    out
}

/// Count `(support, contradiction)` board-adjacency edges to decoded neighbors
/// whose trust state satisfies `keep`. Support = the neighbor is one board hop
/// from `assumed_id`; contradiction = the neighbor is decoded but not adjacent.
///
/// Filtering by trust is what keeps confirmation/scrub decisions anchored to
/// ground truth: counting *any* decoded neighbor would let a chain of mutually
/// board-adjacent untrusted decodes vouch for each other.
fn edge_support_counts(
    ws: &IdCorrectionWorkspace<'_>,
    marker_index: usize,
    assumed_id: usize,
    keep: impl Fn(Trust) -> bool,
) -> (usize, usize) {
    let neighbors = local_edge_neighbor_ids(
        marker_index,
        ws.markers,
        &ws.board_index,
        &ws.outer_radii_px,
        ws.config.consistency_outer_mul,
    );
    let mut support = 0usize;
    let mut contradiction = 0usize;
    for id_j in neighbors {
        let kept = ws
            .markers
            .iter()
            .enumerate()
            .find_map(|(j, m)| (m.id == Some(id_j)).then_some(ws.trust[j]))
            .is_some_and(&keep);
        if !kept {
            continue;
        }
        if ws.board_index.are_neighbors(assumed_id, id_j) {
            support += 1;
        } else {
            contradiction += 1;
        }
    }
    (support, contradiction)
}

pub(super) fn consistency_evidence_for_id(
    ws: &IdCorrectionWorkspace<'_>,
    marker_index: usize,
    assumed_id: usize,
) -> ConsistencyEvidence {
    let neighbor_ids = local_edge_neighbor_ids(
        marker_index,
        ws.markers,
        &ws.board_index,
        &ws.outer_radii_px,
        ws.config.consistency_outer_mul,
    );

    let mut support_edges = 0usize;
    let mut contradiction_edges = 0usize;
    for &neighbor_id in &neighbor_ids {
        if ws.board_index.are_neighbors(assumed_id, neighbor_id) {
            support_edges += 1;
        } else {
            contradiction_edges += 1;
        }
    }

    let n_neighbors = support_edges + contradiction_edges;
    let contradiction_frac = if n_neighbors == 0 {
        0.0
    } else {
        contradiction_edges as f64 / n_neighbors as f64
    };

    let vote_neighbors = gather_trusted_neighbors_local_scale(
        marker_index,
        ws.markers,
        &ws.trust,
        &ws.board_index,
        &ws.outer_radii_px,
        ws.config.consistency_outer_mul,
    );
    let vote = vote_for_candidate(
        ws.markers[marker_index].center,
        ws.outer_radii_px[marker_index],
        &vote_neighbors,
        &ws.board_index,
        ws.board_index.pitch_mm * 0.6,
        ws.config.min_votes,
        ws.config.min_vote_weight_frac,
    );

    let (vote_mismatch, vote_winner_frac) = match vote {
        VoteOutcome::Candidate {
            id,
            winner_weight_frac,
            ..
        } if id != assumed_id => (true, winner_weight_frac),
        _ => (false, 0.0),
    };

    ConsistencyEvidence {
        n_neighbors,
        support_edges,
        contradiction_edges,
        contradiction_frac,
        vote_mismatch,
        vote_winner_frac,
    }
}

pub(super) fn should_clear_by_consistency(
    evidence: ConsistencyEvidence,
    soft_locked: bool,
    config: &crate::detector::config::IdCorrectionConfig,
) -> bool {
    if evidence.n_neighbors < config.consistency_min_neighbors {
        return false;
    }
    if soft_locked {
        evidence.support_edges == 0 && evidence.contradiction_edges >= 2
    } else {
        let strong_vote_mismatch = evidence.vote_mismatch && evidence.vote_winner_frac >= 0.60;
        evidence.support_edges < config.consistency_min_support_edges
            || evidence.contradiction_frac > f64::from(config.consistency_max_contradiction_frac)
            || strong_vote_mismatch
    }
}

pub(super) fn scrub_inconsistent_ids(
    ws: &mut IdCorrectionWorkspace<'_>,
    stage: ScrubStage,
) -> usize {
    let mut to_clear = Vec::<usize>::new();
    for i in 0..ws.markers.len() {
        let Some(id) = ws.markers[i].id else {
            continue;
        };
        if !ws.board_index.id_to_xy.contains_key(&id) {
            to_clear.push(i);
            continue;
        }
        let evidence = consistency_evidence_for_id(ws, i, id);
        let (support_anchor, contradiction_anchor) =
            edge_support_counts(ws, i, id, Trust::is_anchor);
        let recovered_two_neighbor_contradiction = matches!(stage, ScrubStage::Post)
            && matches!(
                ws.trust[i],
                Trust::RecoveredLocal | Trust::RecoveredHomography
            )
            && ((evidence.support_edges == 0
                && evidence.contradiction_edges >= 2
                && evidence.vote_mismatch
                && evidence.vote_winner_frac >= 0.60)
                || (contradiction_anchor >= 1 && support_anchor == 0));
        if recovered_two_neighbor_contradiction {
            to_clear.push(i);
            continue;
        }
        let is_soft_locked = is_soft_locked_assignment(
            &ws.markers[i],
            ws.config.soft_lock_exact_decode,
            ws.codebook_min_cyclic_dist,
        );
        let soft_locked_anchor_contradiction = matches!(stage, ScrubStage::Post)
            && is_soft_locked
            && support_anchor == 0
            && contradiction_anchor >= 2;
        let soft_locked_contradiction_dominated = matches!(stage, ScrubStage::Post)
            && is_soft_locked
            && evidence.contradiction_edges >= 2
            && evidence.contradiction_frac
                > f64::from(ws.config.consistency_max_contradiction_frac);
        if soft_locked_anchor_contradiction || soft_locked_contradiction_dominated {
            to_clear.push(i);
            continue;
        }
        if should_clear_by_consistency(evidence, is_soft_locked, ws.config) {
            to_clear.push(i);
        }
    }

    let mut cleared = 0usize;
    for i in to_clear {
        if clear_marker_id(
            i,
            ws.markers,
            &mut ws.trust,
            &mut ws.stats,
            ws.config.soft_lock_exact_decode,
            ws.codebook_min_cyclic_dist,
            stage,
        ) {
            cleared += 1;
        }
    }
    cleared
}

pub(super) fn candidate_passes_local_consistency_gate(
    ws: &IdCorrectionWorkspace<'_>,
    marker_index: usize,
    candidate_id: usize,
) -> bool {
    let neighbor_ids = local_edge_neighbor_ids(
        marker_index,
        ws.markers,
        &ws.board_index,
        &ws.outer_radii_px,
        ws.config.consistency_outer_mul,
    );
    let mut support_edges = 0usize;
    let mut contradiction_edges = 0usize;
    for id in neighbor_ids {
        if ws.board_index.are_neighbors(candidate_id, id) {
            support_edges += 1;
        } else {
            contradiction_edges += 1;
        }
    }
    let total = support_edges + contradiction_edges;
    if support_edges < 1 || total == 0 {
        return false;
    }
    let contradiction_frac = contradiction_edges as f64 / total as f64;
    contradiction_frac <= f64::from(ws.config.consistency_max_contradiction_frac)
}

/// Promote decoded IDs the voting stages could not reach but whose local
/// neighborhood structurally confirms them.
///
/// In sparse/partial/blurry views a correctly-decoded but non-exact marker may
/// have no affine and no adjacent voting pair (⇒ zero votes), so it is never
/// promoted to trusted and would be cleared by `cleanup_unverified_markers`
/// despite being correct. This pass trusts such a marker when its decoded ID is
/// structurally clean: at least one board-adjacent neighbor supports it and no
/// neighbor contradicts it. It is precision-first — it never confirms an ID that
/// a confident local vote actively disputes (`strong_vote_mismatch`).
pub(super) fn confirm_ids_by_consistency(ws: &mut IdCorrectionWorkspace<'_>) -> usize {
    if !ws.config.confirm_by_consistency {
        return 0;
    }
    let mut confirmed = 0usize;
    for i in 0..ws.markers.len() {
        if ws.trust[i].is_trusted() {
            continue;
        }
        let Some(id) = ws.markers[i].id else {
            continue;
        };
        // Only confirm genuine decodes whose ID exists on the board.
        if ws.markers[i].decode.is_none() || !ws.board_index.id_to_xy.contains_key(&id) {
            continue;
        }
        let evidence = consistency_evidence_for_id(ws, i, id);
        // Precision: the support must come from *trusted* neighbors. Counting any
        // decoded neighbor (as `evidence.support_edges` does) would let a chain of
        // mutually board-adjacent untrusted decodes self-confirm — each seeing the
        // others as support — keeping false IDs that cleanup would otherwise clear.
        let (trusted_support, _) = edge_support_counts(ws, i, id, Trust::is_trusted);
        let strong_vote_mismatch = evidence.vote_mismatch && evidence.vote_winner_frac >= 0.60;
        if evidence.n_neighbors >= ws.config.consistency_min_neighbors
            && trusted_support >= ws.config.consistency_min_support_edges
            && evidence.contradiction_edges == 0
            && !strong_vote_mismatch
        {
            ws.trust[i] = Trust::ConfirmedConsistent;
            ws.stats.n_confirmed_by_consistency += 1;
            confirmed += 1;
        }
    }
    confirmed
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detector::config::IdCorrectionConfig;

    fn evidence(
        n_neighbors: usize,
        support_edges: usize,
        contradiction_edges: usize,
        vote_mismatch: bool,
        vote_winner_frac: f64,
    ) -> ConsistencyEvidence {
        let contradiction_frac = if n_neighbors == 0 {
            0.0
        } else {
            contradiction_edges as f64 / n_neighbors as f64
        };
        ConsistencyEvidence {
            n_neighbors,
            support_edges,
            contradiction_edges,
            contradiction_frac,
            vote_mismatch,
            vote_winner_frac,
        }
    }

    // --- not soft-locked: a marker is precision-first cleared on weak structure ---

    #[test]
    fn keeps_marker_below_min_neighbors() {
        // Default `consistency_min_neighbors == 1`: zero neighbors ⇒ never clear,
        // even with no support (avoids clearing isolated-but-correct markers).
        let cfg = IdCorrectionConfig::default();
        assert!(!should_clear_by_consistency(
            evidence(0, 0, 0, false, 0.0),
            false,
            &cfg
        ));
    }

    #[test]
    fn keeps_structurally_clean_marker() {
        let cfg = IdCorrectionConfig::default();
        // 3 supporting neighbors, no contradictions, no vote dispute ⇒ keep.
        assert!(!should_clear_by_consistency(
            evidence(3, 3, 0, false, 0.0),
            false,
            &cfg
        ));
    }

    #[test]
    fn clears_marker_with_no_support() {
        let cfg = IdCorrectionConfig::default();
        // support_edges (0) < consistency_min_support_edges (1) ⇒ clear.
        assert!(should_clear_by_consistency(
            evidence(2, 0, 2, false, 0.0),
            false,
            &cfg
        ));
    }

    #[test]
    fn clears_marker_with_contradiction_majority() {
        let cfg = IdCorrectionConfig::default();
        // contradiction_frac 3/4 = 0.75 > 0.5 even though one neighbor supports.
        assert!(should_clear_by_consistency(
            evidence(4, 1, 3, false, 0.0),
            false,
            &cfg
        ));
    }

    #[test]
    fn clears_on_strong_vote_mismatch_only() {
        let cfg = IdCorrectionConfig::default();
        // Clean edges, but a confident local vote (>= 0.60) disputes the id.
        assert!(should_clear_by_consistency(
            evidence(2, 2, 0, true, 0.70),
            false,
            &cfg
        ));
        // A weak vote mismatch (< 0.60) is not enough to clear a clean marker.
        assert!(!should_clear_by_consistency(
            evidence(2, 2, 0, true, 0.55),
            false,
            &cfg
        ));
    }

    // --- soft-locked exact decodes: only strict structural contradiction clears ---

    #[test]
    fn soft_locked_marker_survives_partial_contradiction() {
        let cfg = IdCorrectionConfig::default();
        // One support edge present ⇒ a soft-locked exact decode is protected even
        // with two contradictions (requires support == 0 to clear).
        assert!(!should_clear_by_consistency(
            evidence(3, 1, 2, false, 0.0),
            true,
            &cfg
        ));
    }

    #[test]
    fn soft_locked_marker_cleared_on_strict_contradiction() {
        let cfg = IdCorrectionConfig::default();
        // Zero support and >= 2 contradictions ⇒ even a soft-locked decode clears.
        assert!(should_clear_by_consistency(
            evidence(2, 0, 2, false, 0.0),
            true,
            &cfg
        ));
    }
}