ringgrid 0.5.3

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
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
use image::GrayImage;

use super::config::InnerFitConfig;
use crate::conic::{self, Ellipse};
use crate::marker::MarkerSpec;
use crate::pixelmap::PixelMapper;
use crate::ring::edge_sample::DistortionAwareSampler;
use crate::ring::inner_estimate::{
    InnerEstimate, InnerStatus, Polarity, estimate_inner_scale_from_outer_with_mapper,
};
use crate::ring::radial_profile;

/// Outcome category for robust inner ellipse fitting.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InnerFitStatus {
    /// Inner ellipse fit is valid and accepted by quality gates.
    Ok,
    /// A candidate was fitted but rejected by quality gates.
    Rejected,
    /// Fitting could not be completed (insufficient/invalid data).
    Failed,
}

/// Stable reject/failure code for inner fit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InnerFitReason {
    /// Inner radius estimate returned a non-OK status.
    EstimateNotOk,
    /// No radius hint was available for inner ring estimation.
    EstimateMissingHint,
    /// Too few edge points to attempt a fit.
    InsufficientPoints,
    /// Ellipse fitting algorithm did not converge.
    FitFailed,
    /// RANSAC inlier ratio fell below the acceptance threshold.
    InlierRatioLow,
    /// Fitted center shifted too far from the outer ellipse center.
    CenterShiftTooLarge,
    /// Inner-to-outer axis ratio is outside the expected range.
    RatioMismatch,
    /// RMS residual of the fit exceeded the threshold.
    RmsResidualHigh,
    /// Largest angular gap between inlier edge points is too wide.
    AngularGapTooLarge,
}

impl InnerFitReason {
    pub(crate) const fn code(self) -> &'static str {
        match self {
            Self::EstimateNotOk => "estimate_not_ok",
            Self::EstimateMissingHint => "estimate_missing_hint",
            Self::InsufficientPoints => "insufficient_points",
            Self::FitFailed => "fit_failed",
            Self::InlierRatioLow => "inlier_ratio_low",
            Self::CenterShiftTooLarge => "center_shift_too_large",
            Self::RatioMismatch => "ratio_mismatch",
            Self::RmsResidualHigh => "rms_residual_high",
            Self::AngularGapTooLarge => "angular_gap_too_large",
        }
    }
}

impl std::fmt::Display for InnerFitReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.code())
    }
}

/// Structured diagnostics for inner-fit reject/failure reasons.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub(crate) enum InnerFitReasonContext {
    EstimateStatus {
        status: InnerStatus,
    },
    InsufficientPoints {
        observed_points: usize,
        min_required_points: usize,
    },
    InlierRatioLow {
        observed_inlier_ratio: f32,
        min_required_inlier_ratio: f32,
    },
    CenterShiftTooLarge {
        observed_shift_px: f64,
        max_allowed_shift_px: f64,
    },
    RatioMismatch {
        measured_ratio: f64,
        hint_ratio: f32,
        max_allowed_abs_error: f64,
    },
    RmsResidualHigh {
        observed_rms_residual: f64,
        max_allowed_rms_residual: f64,
    },
    AngularGapTooLarge {
        observed_gap_rad: f64,
        max_allowed_gap_rad: f64,
    },
}

/// Robust inner fit result.
#[derive(Debug, Clone)]
pub(crate) struct InnerFitResult {
    /// Final inner-ellipse fit status.
    pub status: InnerFitStatus,
    /// Optional reject/failure reason from fit stage.
    pub reason: Option<InnerFitReason>,
    /// Optional structured reject/failure context.
    pub reason_context: Option<InnerFitReasonContext>,
    /// Fitted inner ellipse (present only when status is `Ok`).
    pub ellipse_inner: Option<Ellipse>,
    /// Candidate inner-edge points used for fitting.
    pub points_inner: Vec<[f64; 2]>,
    /// RANSAC inlier ratio for the fitted inner ellipse (if RANSAC path was used).
    pub ransac_inlier_ratio_inner: Option<f32>,
    /// RMS Sampson residual of the fitted inner ellipse.
    pub rms_residual_inner: Option<f64>,
    /// Maximum angular gap (radians) between consecutive inner edge points.
    pub max_angular_gap: Option<f64>,
    /// Theta consistency score from the inner estimate stage (fraction of theta
    /// samples that agree on the inner edge location). Present when estimation ran.
    pub theta_consistency: Option<f32>,
}

fn failed_inner_fit(
    reason: InnerFitReason,
    reason_context: Option<InnerFitReasonContext>,
    points_inner: Vec<[f64; 2]>,
    theta_consistency: Option<f32>,
) -> InnerFitResult {
    InnerFitResult {
        status: InnerFitStatus::Failed,
        reason: Some(reason),
        reason_context,
        ellipse_inner: None,
        points_inner,
        ransac_inlier_ratio_inner: None,
        rms_residual_inner: None,
        max_angular_gap: None,
        theta_consistency,
    }
}

fn rejected_inner_fit(
    reason: InnerFitReason,
    reason_context: Option<InnerFitReasonContext>,
    points_inner: Vec<[f64; 2]>,
    inlier_ratio: Option<f32>,
    rms_residual_inner: f64,
    inner_angular_gap: f64,
    theta_consistency: Option<f32>,
) -> InnerFitResult {
    InnerFitResult {
        status: InnerFitStatus::Rejected,
        reason: Some(reason),
        reason_context,
        ellipse_inner: None,
        points_inner,
        ransac_inlier_ratio_inner: inlier_ratio,
        rms_residual_inner: Some(rms_residual_inner),
        max_angular_gap: Some(inner_angular_gap),
        theta_consistency,
    }
}

fn successful_inner_fit(
    ellipse_inner: Ellipse,
    points_inner: Vec<[f64; 2]>,
    inlier_ratio: Option<f32>,
    rms_residual_inner: f64,
    inner_angular_gap: f64,
    theta_consistency: Option<f32>,
) -> InnerFitResult {
    InnerFitResult {
        status: InnerFitStatus::Ok,
        reason: None,
        reason_context: None,
        ellipse_inner: Some(ellipse_inner),
        points_inner,
        ransac_inlier_ratio_inner: inlier_ratio,
        rms_residual_inner: Some(rms_residual_inner),
        max_angular_gap: Some(inner_angular_gap),
        theta_consistency,
    }
}

fn signed_peak_value(v: f32, pol: Polarity) -> f32 {
    match pol {
        Polarity::Pos => v,
        Polarity::Neg => -v,
    }
}

fn refine_peak_idx_subpixel(curve: &[f32], idx: usize, pol: Polarity) -> f32 {
    if idx == 0 || idx + 1 >= curve.len() {
        return idx as f32;
    }
    let y0 = signed_peak_value(curve[idx - 1], pol);
    let y1 = signed_peak_value(curve[idx], pol);
    let y2 = signed_peak_value(curve[idx + 1], pol);
    let denom = y0 - 2.0 * y1 + y2;
    if denom.abs() < 1e-6 {
        return idx as f32;
    }
    let offs = 0.5 * (y0 - y2) / denom;
    (idx as f32 + offs.clamp(-1.0, 1.0)).clamp(0.0, (curve.len() - 1) as f32)
}

fn sample_inner_points_from_hint(
    gray: &GrayImage,
    outer: &Ellipse,
    estimate: &InnerEstimate,
    cfg: &InnerFitConfig,
    mapper: Option<&dyn PixelMapper>,
) -> Vec<[f64; 2]> {
    let (Some(r_hint), Some(pol)) = (estimate.r_inner_found, estimate.polarity) else {
        return Vec::new();
    };

    let window = estimate.search_window;
    let n_r = 64usize.max(((window[1] - window[0]).abs() / 0.0025).round() as usize);
    let n_t = 96usize;
    if n_r < 8 || window[1] <= window[0] {
        return Vec::new();
    }
    let r_step = (window[1] - window[0]) / (n_r as f32 - 1.0);
    let hint_idx = ((r_hint - window[0]) / r_step)
        .round()
        .clamp(0.0, (n_r - 1) as f32) as usize;
    let sampler = DistortionAwareSampler::new(gray, mapper);
    let cx = outer.cx as f32;
    let cy = outer.cy as f32;
    let a = outer.a as f32;
    let b = outer.b as f32;
    let ca = (outer.angle as f32).cos();
    let sa = (outer.angle as f32).sin();

    let mut points = Vec::<[f64; 2]>::with_capacity(n_t);
    let mut profile = vec![0.0f32; n_r];
    let mut d = vec![0.0f32; n_r];
    let d_theta = 2.0 * std::f32::consts::PI / n_t as f32;
    let c_step = d_theta.cos();
    let s_step = d_theta.sin();
    let mut ct = 1.0f32;
    let mut st = 0.0f32;
    for _ in 0..n_t {
        let ray_x = ca * (a * ct) - sa * (b * st);
        let ray_y = sa * (a * ct) + ca * (b * st);

        let mut ok = true;
        for (ri, sample) in profile.iter_mut().enumerate().take(n_r) {
            let r = window[0] + ri as f32 * r_step;
            let x = cx + ray_x * r;
            let y = cy + ray_y * r;
            let Some(v) = sampler.sample_checked(x, y) else {
                ok = false;
                break;
            };
            *sample = v;
        }
        if ok {
            radial_profile::radial_derivative_into(&profile, r_step, &mut d);
            radial_profile::smooth_3point(&mut d);

            let lo = hint_idx.saturating_sub(cfg.local_peak_halfwidth_idx);
            let hi = (hint_idx + cfg.local_peak_halfwidth_idx).min(n_r - 1);
            let local_i = match pol {
                Polarity::Pos => (lo..=hi)
                    .max_by(|&i, &j| d[i].partial_cmp(&d[j]).unwrap())
                    .unwrap_or(hint_idx),
                Polarity::Neg => (lo..=hi)
                    .min_by(|&i, &j| d[i].partial_cmp(&d[j]).unwrap())
                    .unwrap_or(hint_idx),
            };
            let idx_f = refine_peak_idx_subpixel(&d, local_i, pol);
            let r = (window[0] + idx_f * r_step).clamp(window[0], window[1]);
            points.push([(cx + ray_x * r) as f64, (cy + ray_y * r) as f64]);
        }

        let next_ct = ct * c_step - st * s_step;
        let next_st = st * c_step + ct * s_step;
        ct = next_ct;
        st = next_st;
    }

    points
}

/// Check quality gates on a fitted inner ellipse. Returns the first failing
/// gate as `(reason, context)`, or `None` if all gates pass.
fn check_inner_fit_gates(
    inner: &Ellipse,
    outer: &Ellipse,
    inlier_ratio: Option<f32>,
    rms_residual: f64,
    angular_gap: f64,
    r_hint: Option<f32>,
    cfg: &InnerFitConfig,
) -> Option<(InnerFitReason, InnerFitReasonContext)> {
    if let Some(ir) = inlier_ratio
        && ir < cfg.min_inlier_ratio
    {
        return Some((
            InnerFitReason::InlierRatioLow,
            InnerFitReasonContext::InlierRatioLow {
                observed_inlier_ratio: ir,
                min_required_inlier_ratio: cfg.min_inlier_ratio,
            },
        ));
    }

    let dx = inner.cx - outer.cx;
    let dy = inner.cy - outer.cy;
    let center_shift = (dx * dx + dy * dy).sqrt();
    if center_shift > cfg.max_center_shift_px {
        return Some((
            InnerFitReason::CenterShiftTooLarge,
            InnerFitReasonContext::CenterShiftTooLarge {
                observed_shift_px: center_shift,
                max_allowed_shift_px: cfg.max_center_shift_px,
            },
        ));
    }

    let mean_outer = ((outer.a + outer.b) * 0.5).max(1e-9);
    let mean_inner = (inner.a + inner.b) * 0.5;
    let ratio_meas = mean_inner / mean_outer;
    if let Some(r_h) = r_hint
        && (ratio_meas - r_h as f64).abs() > cfg.max_ratio_abs_error
    {
        return Some((
            InnerFitReason::RatioMismatch,
            InnerFitReasonContext::RatioMismatch {
                measured_ratio: ratio_meas,
                hint_ratio: r_h,
                max_allowed_abs_error: cfg.max_ratio_abs_error,
            },
        ));
    }

    if !rms_residual.is_finite() || rms_residual > cfg.max_rms_residual {
        return Some((
            InnerFitReason::RmsResidualHigh,
            InnerFitReasonContext::RmsResidualHigh {
                observed_rms_residual: rms_residual,
                max_allowed_rms_residual: cfg.max_rms_residual,
            },
        ));
    }

    if angular_gap > cfg.max_angular_gap_rad {
        return Some((
            InnerFitReason::AngularGapTooLarge,
            InnerFitReasonContext::AngularGapTooLarge {
                observed_gap_rad: angular_gap,
                max_allowed_gap_rad: cfg.max_angular_gap_rad,
            },
        ));
    }

    None
}

/// Robustly fit inner ellipse points using outer ellipse only as search prior.
///
/// The radial hint/polarity stage is delegated to `estimate_inner_scale_from_outer`
/// so there is a single source of truth for inner-edge signal extraction.
pub(crate) fn fit_inner_ellipse_from_outer_hint(
    gray: &GrayImage,
    outer: &Ellipse,
    spec: &MarkerSpec,
    mapper: Option<&dyn PixelMapper>,
    cfg: &InnerFitConfig,
    store_response: bool,
) -> InnerFitResult {
    let estimate =
        estimate_inner_scale_from_outer_with_mapper(gray, outer, spec, mapper, store_response);

    if estimate.status != InnerStatus::Ok {
        return failed_inner_fit(
            InnerFitReason::EstimateNotOk,
            Some(InnerFitReasonContext::EstimateStatus {
                status: estimate.status,
            }),
            Vec::new(),
            estimate.theta_consistency,
        );
    }
    if estimate.r_inner_found.is_none() || estimate.polarity.is_none() {
        return failed_inner_fit(
            InnerFitReason::EstimateMissingHint,
            None,
            Vec::new(),
            estimate.theta_consistency,
        );
    }

    let points_inner = sample_inner_points_from_hint(gray, outer, &estimate, cfg, mapper);
    if points_inner.len() < cfg.min_points {
        return failed_inner_fit(
            InnerFitReason::InsufficientPoints,
            Some(InnerFitReasonContext::InsufficientPoints {
                observed_points: points_inner.len(),
                min_required_points: cfg.min_points,
            }),
            points_inner,
            estimate.theta_consistency,
        );
    }

    let (ellipse_inner, inlier_ratio): (Ellipse, Option<f32>) = if points_inner.len() >= 8 {
        match conic::try_fit_ellipse_ransac(&points_inner, &cfg.ransac) {
            Ok(r) => (
                r.ellipse,
                Some(r.num_inliers as f32 / points_inner.len().max(1) as f32),
            ),
            Err(_) => match conic::fit_ellipse_direct(&points_inner) {
                Some(e) => (e, None),
                None => {
                    return failed_inner_fit(
                        InnerFitReason::FitFailed,
                        None,
                        points_inner,
                        estimate.theta_consistency,
                    );
                }
            },
        }
    } else {
        match conic::fit_ellipse_direct(&points_inner) {
            Some(e) => (e, None),
            None => {
                return failed_inner_fit(
                    InnerFitReason::FitFailed,
                    None,
                    points_inner,
                    estimate.theta_consistency,
                );
            }
        }
    };

    let rms_residual_inner = conic::rms_sampson_distance(&ellipse_inner, &points_inner);
    let inner_angular_gap = crate::detector::outer_fit::max_angular_gap(
        [ellipse_inner.cx, ellipse_inner.cy],
        &points_inner,
    );

    if let Some((reason, context)) = check_inner_fit_gates(
        &ellipse_inner,
        outer,
        inlier_ratio,
        rms_residual_inner,
        inner_angular_gap,
        estimate.r_inner_found,
        cfg,
    ) {
        return rejected_inner_fit(
            reason,
            Some(context),
            points_inner,
            inlier_ratio,
            rms_residual_inner,
            inner_angular_gap,
            estimate.theta_consistency,
        );
    }

    successful_inner_fit(
        ellipse_inner,
        points_inner,
        inlier_ratio,
        rms_residual_inner,
        inner_angular_gap,
        estimate.theta_consistency,
    )
}

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

    fn draw_ring_image(
        w: u32,
        h: u32,
        center: [f32; 2],
        outer_radius: f32,
        inner_radius: f32,
    ) -> GrayImage {
        crate::test_utils::draw_ring_image(w, h, center, outer_radius, inner_radius, 26, 230)
    }

    #[test]
    fn inner_fit_recovers_circle_inner_from_outer_hint() {
        let center = [64.0f32, 64.0f32];
        let r_out = 26.0f32;
        let r_in = 16.5f32;
        let img = draw_ring_image(128, 128, center, r_out, r_in);

        let outer = Ellipse {
            cx: center[0] as f64,
            cy: center[1] as f64,
            a: r_out as f64,
            b: r_out as f64,
            angle: 0.0,
        };
        let spec = MarkerSpec {
            r_inner_expected: r_in / r_out,
            inner_search_halfwidth: 0.12,
            inner_grad_polarity: GradPolarity::LightToDark,
            ..MarkerSpec::default()
        };

        let res = fit_inner_ellipse_from_outer_hint(
            &img,
            &outer,
            &spec,
            None,
            &InnerFitConfig::default(),
            false,
        );
        assert_eq!(
            res.status,
            InnerFitStatus::Ok,
            "reason={:?} context={:?}",
            res.reason,
            res.reason_context
        );
        let e = res.ellipse_inner.expect("inner ellipse");
        assert!((e.cx - outer.cx).abs() < 1.2);
        assert!((e.cy - outer.cy).abs() < 1.2);
        let mean = (e.a + e.b) * 0.5;
        assert!((mean - r_in as f64).abs() < 1.5, "mean={:.3}", mean);
    }

    #[test]
    fn inner_fit_reason_serialization_is_stable() {
        let reason = InnerFitReason::RmsResidualHigh;
        assert_eq!(reason.to_string(), "rms_residual_high");
        let json = serde_json::to_string(&reason).expect("serialize inner fit reason");
        assert_eq!(json, "\"rms_residual_high\"");
    }
}