chess-corners 1.2.0

High-level chessboard / ChESS corner detection API
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
use chess_corners_core::{
    CenterOfMassConfig, ForstnerConfig, OrientationMethod, PeakFitMode, RadonDetectorParams,
    RefinerKind, SaddlePointConfig,
};

use crate::upscale::UpscaleConfig;

use super::{
    ChessConfig, ChessRefiner, ChessRing, DetectionStrategy, DetectorConfig, MultiscaleConfig,
    RadonConfig,
};

fn assert_strategy_chess(cfg: &DetectorConfig) -> &ChessConfig {
    match &cfg.strategy {
        DetectionStrategy::Chess(c) => c,
        other => panic!("expected ChESS strategy, got {other:?}"),
    }
}

fn assert_strategy_radon(cfg: &DetectorConfig) -> &RadonConfig {
    match &cfg.strategy {
        DetectionStrategy::Radon(r) => r,
        other => panic!("expected Radon strategy, got {other:?}"),
    }
}

#[test]
fn default_is_single_scale_chess_with_denoise_floor() {
    let cfg = DetectorConfig::default();
    let chess = assert_strategy_chess(&cfg);
    assert_eq!(chess.ring, ChessRing::Canonical);
    assert_eq!(
        chess.refiner,
        ChessRefiner::CenterOfMass(CenterOfMassConfig::default())
    );
    assert_eq!(cfg.detection.nms_radius, 2);
    assert_eq!(cfg.detection.min_cluster_size, 2);
    assert_eq!(cfg.multiscale, MultiscaleConfig::SingleScale);
    assert_eq!(cfg.upscale, UpscaleConfig::Disabled);
    assert_eq!(cfg.threshold, 30.0);
    assert_eq!(cfg.merge_radius, 3.0);
    assert!(cfg.coarse_to_fine_params().is_none());

    let params = cfg.chess_params();
    assert!(!params.use_radius10);
    assert_eq!(params.threshold, 30.0);
    assert_eq!(params.nms_radius, 2);
    assert_eq!(params.min_cluster_size, 2);
    assert_eq!(
        params.refiner,
        RefinerKind::CenterOfMass(CenterOfMassConfig::default())
    );
}

#[test]
fn chess_threshold_lowers_to_absolute_floor() {
    let cfg = DetectorConfig {
        threshold: 7.5,
        ..DetectorConfig::chess()
    };
    let params = cfg.chess_params();
    assert_eq!(params.threshold, 7.5);
}

#[test]
fn radon_threshold_lowers_to_relative_fraction() {
    let cfg = DetectorConfig {
        threshold: 0.15,
        ..DetectorConfig::radon()
    };
    let params = cfg.radon_detector_params();
    assert!((params.threshold_rel - 0.15).abs() < f32::EPSILON);
}

#[test]
fn chess_multiscale_preset_carries_pyramid_params() {
    let cfg = DetectorConfig::chess_multiscale();
    let MultiscaleConfig::Pyramid {
        levels,
        min_size,
        refinement_radius,
    } = cfg.multiscale
    else {
        panic!("chess_multiscale preset must carry Pyramid params");
    };
    assert_eq!(levels, 3);
    assert_eq!(min_size, 128);
    assert_eq!(refinement_radius, 3);

    let cf = cfg
        .coarse_to_fine_params()
        .expect("chess_multiscale config must produce CoarseToFineParams");
    assert_eq!(cf.pyramid.num_levels, 3);
    assert_eq!(cf.pyramid.min_size, 128);
    assert_eq!(cf.refinement_radius, 3);
    assert_eq!(cf.merge_radius, 3.0);
}

#[test]
fn radon_preset_uses_radon_config_and_relative_threshold() {
    let cfg = DetectorConfig::radon();
    let radon = assert_strategy_radon(&cfg);
    assert_eq!(radon.ray_radius, 4);
    assert_eq!(radon.image_upsample, 2);
    assert_eq!(radon.response_blur_radius, 1);
    assert_eq!(radon.peak_fit, PeakFitMode::Gaussian);
    assert_eq!(cfg.detection.nms_radius, 4);
    assert_eq!(cfg.detection.min_cluster_size, 2);
    assert_eq!(cfg.threshold, RadonDetectorParams::DEFAULT_THRESHOLD_REL);
    assert_eq!(cfg.multiscale, MultiscaleConfig::SingleScale);
    assert!(cfg.coarse_to_fine_params().is_none());

    let radon_params = cfg.radon_detector_params();
    assert_eq!(radon_params.ray_radius, 4);
    assert_eq!(radon_params.image_upsample, 2);
    assert_eq!(radon_params.nms_radius, 4);
    assert_eq!(radon_params.min_cluster_size, 2);
    assert!(
        (radon_params.threshold_rel - RadonDetectorParams::DEFAULT_THRESHOLD_REL).abs()
            < f32::EPSILON
    );
}

#[test]
fn radon_multiscale_preset_carries_pyramid_params() {
    let cfg = DetectorConfig::radon_multiscale();
    assert_strategy_radon(&cfg);
    assert_eq!(cfg.threshold, RadonDetectorParams::DEFAULT_THRESHOLD_REL);
    let MultiscaleConfig::Pyramid {
        levels,
        min_size,
        refinement_radius,
    } = cfg.multiscale
    else {
        panic!("radon_multiscale preset must carry Pyramid params");
    };
    assert_eq!(levels, 3);
    assert_eq!(min_size, 128);
    assert_eq!(refinement_radius, 3);

    let cf = cfg
        .coarse_to_fine_params()
        .expect("radon_multiscale config must produce CoarseToFineParams");
    assert_eq!(cf.pyramid.num_levels, 3);
    assert_eq!(cf.pyramid.min_size, 128);
    assert_eq!(cf.refinement_radius, 3);
    assert_eq!(cf.merge_radius, 3.0);
}

#[test]
fn broad_ring_and_forstner_refiner_propagate_to_params() {
    // `ForstnerConfig` is `#[non_exhaustive]`; build via `Default`
    // and set the field under test.
    let mut forstner = ForstnerConfig::default();
    forstner.max_offset = 2.0;
    let cfg = DetectorConfig {
        strategy: DetectionStrategy::Chess(ChessConfig {
            ring: ChessRing::Broad,
            refiner: ChessRefiner::Forstner(forstner),
            ..ChessConfig::default()
        }),
        ..DetectorConfig::chess()
    };

    let params = cfg.chess_params();
    assert!(params.use_radius10);
    assert_eq!(params.refiner, RefinerKind::Forstner(forstner));
}

#[test]
fn chess_preset_round_trips_through_serde() {
    let cfg = DetectorConfig::chess();
    let json = serde_json::to_string(&cfg).expect("serialize chess config");
    let decoded: DetectorConfig = serde_json::from_str(&json).expect("deserialize chess config");
    assert_eq!(decoded, cfg);
}

#[test]
fn chess_multiscale_preset_round_trips_through_serde() {
    let cfg = DetectorConfig::chess_multiscale();
    let json = serde_json::to_string(&cfg).expect("serialize chess_multiscale config");
    let decoded: DetectorConfig =
        serde_json::from_str(&json).expect("deserialize chess_multiscale config");
    assert_eq!(decoded, cfg);
}

#[test]
fn radon_preset_round_trips_through_serde() {
    let cfg = DetectorConfig::radon();
    let json = serde_json::to_string(&cfg).expect("serialize radon config");
    let decoded: DetectorConfig = serde_json::from_str(&json).expect("deserialize radon config");
    assert_eq!(decoded, cfg);
}

#[test]
fn radon_multiscale_preset_round_trips_through_serde() {
    let cfg = DetectorConfig::radon_multiscale();
    let json = serde_json::to_string(&cfg).expect("serialize radon_multiscale config");
    let decoded: DetectorConfig =
        serde_json::from_str(&json).expect("deserialize radon_multiscale config");
    assert_eq!(decoded, cfg);
}

#[test]
fn threshold_serializes_as_a_bare_number() {
    let cfg = DetectorConfig::chess().with_threshold(3.5);
    let json = serde_json::to_string(&cfg).expect("serialize config");
    assert!(json.contains("\"threshold\":3.5"));
    let decoded: DetectorConfig = serde_json::from_str(&json).expect("deserialize config");
    assert_eq!(decoded.threshold, 3.5);
}

#[test]
fn multiscale_config_round_trips_with_externally_tagged_payload() {
    let single = MultiscaleConfig::SingleScale;
    let single_json = serde_json::to_string(&single).expect("serialize single-scale");
    assert!(single_json.contains("single_scale"));
    let decoded: MultiscaleConfig =
        serde_json::from_str(&single_json).expect("deserialize single-scale");
    assert_eq!(decoded, single);

    let pyramid = MultiscaleConfig::Pyramid {
        levels: 3,
        min_size: 128,
        refinement_radius: 3,
    };
    let pyramid_json = serde_json::to_string(&pyramid).expect("serialize pyramid");
    assert!(pyramid_json.contains("pyramid"));
    let decoded: MultiscaleConfig =
        serde_json::from_str(&pyramid_json).expect("deserialize pyramid");
    assert_eq!(decoded, pyramid);
}

#[test]
fn chess_refiner_round_trips_each_variant() {
    let variants = [
        ChessRefiner::CenterOfMass(CenterOfMassConfig::default()),
        ChessRefiner::Forstner(ForstnerConfig::default()),
        ChessRefiner::SaddlePoint(SaddlePointConfig::default()),
    ];
    for v in variants {
        let json = serde_json::to_string(&v).expect("serialize chess refiner");
        let decoded: ChessRefiner = serde_json::from_str(&json).expect("deserialize chess refiner");
        assert_eq!(decoded, v);
    }
}

#[test]
fn unit_enum_variants_serialize_as_bare_strings() {
    // Codifies the externally-tagged serde encoding for unit variants.
    // The Python from_dict paths must accept these bare strings produced
    // by serde so that Rust→JSON→Python round-trips work end-to-end.
    let json = serde_json::to_string(&MultiscaleConfig::SingleScale).unwrap();
    assert_eq!(json, "\"single_scale\"");

    let json = serde_json::to_string(&UpscaleConfig::Disabled).unwrap();
    assert_eq!(json, "\"disabled\"");
}

#[test]
fn with_chess_mutates_in_place_when_strategy_is_chess() {
    let cfg = DetectorConfig::chess().with_chess(|c| c.ring = ChessRing::Broad);
    let chess = assert_strategy_chess(&cfg);
    assert_eq!(chess.ring, ChessRing::Broad);
    // Other chess fields untouched
    assert_eq!(
        chess.refiner,
        ChessRefiner::CenterOfMass(CenterOfMassConfig::default())
    );
}

#[test]
fn with_chess_replaces_radon_preserves_threshold() {
    let cfg = DetectorConfig::radon()
        .with_threshold(5.0)
        .with_chess(|c| c.ring = ChessRing::Broad);
    // Strategy replaced with chess
    let chess = assert_strategy_chess(&cfg);
    assert_eq!(chess.ring, ChessRing::Broad);
    // Top-level threshold preserved
    assert_eq!(cfg.threshold, 5.0);
}

#[test]
fn with_radon_mutates_in_place_when_strategy_is_radon() {
    let cfg = DetectorConfig::radon().with_radon(|r| r.ray_radius = 9);
    let radon = assert_strategy_radon(&cfg);
    assert_eq!(radon.ray_radius, 9);
    // Other radon fields untouched
    assert_eq!(radon.image_upsample, 2);
}

#[test]
fn with_radon_replaces_chess_preserves_threshold() {
    let cfg = DetectorConfig::chess()
        .with_threshold(0.5)
        .with_radon(|r| r.ray_radius = 6);
    let radon = assert_strategy_radon(&cfg);
    assert_eq!(radon.ray_radius, 6);
    // Threshold preserved
    assert_eq!(cfg.threshold, 0.5);
}

#[test]
fn with_detection_sets_shared_params_for_both_strategies() {
    // ChESS strategy: shared detection params flow into ChessParams.
    let cfg = DetectorConfig::chess().with_detection(|d| {
        d.nms_radius = 7;
        d.min_cluster_size = 4;
    });
    assert_eq!(cfg.detection.nms_radius, 7);
    assert_eq!(cfg.detection.min_cluster_size, 4);
    let params = cfg.chess_params();
    assert_eq!(params.nms_radius, 7);
    assert_eq!(params.min_cluster_size, 4);

    // Radon strategy: same shared params flow into RadonDetectorParams.
    let cfg = DetectorConfig::radon().with_detection(|d| d.nms_radius = 9);
    assert_eq!(cfg.detection.nms_radius, 9);
    let params = cfg.radon_detector_params();
    assert_eq!(params.nms_radius, 9);
}

#[test]
fn chained_builder_produces_expected_state() {
    let cfg = DetectorConfig::chess()
        .with_threshold(0.15)
        .with_chess(|c| c.refiner = ChessRefiner::forstner());
    assert_eq!(cfg.threshold, 0.15);
    let chess = assert_strategy_chess(&cfg);
    assert_eq!(
        chess.refiner,
        ChessRefiner::Forstner(ForstnerConfig::default())
    );
}

#[test]
fn with_multiscale_sets_multiscale() {
    let cfg = DetectorConfig::chess().with_multiscale(MultiscaleConfig::pyramid_default());
    assert_eq!(
        cfg.multiscale,
        MultiscaleConfig::Pyramid {
            levels: 3,
            min_size: 128,
            refinement_radius: 3
        }
    );
}

#[test]
fn with_upscale_sets_upscale() {
    let cfg = DetectorConfig::chess().with_upscale(UpscaleConfig::Fixed(2));
    assert_eq!(cfg.upscale, UpscaleConfig::Fixed(2));
}

#[test]
fn with_orientation_method_sets_method() {
    let method = OrientationMethod::DiskFit;
    let cfg = DetectorConfig::chess().with_orientation_method(method);
    assert_eq!(cfg.orientation_method, Some(method));
}

#[test]
fn without_orientation_clears_method() {
    let cfg = DetectorConfig::chess().without_orientation();
    assert_eq!(cfg.orientation_method, None);
    // Lowering carries the `None` through to the low-level params.
    assert_eq!(cfg.chess_params().orientation_method, None);
}

#[test]
fn default_orientation_method_is_some_ring_fit() {
    let cfg = DetectorConfig::chess();
    assert_eq!(cfg.orientation_method, Some(OrientationMethod::RingFit));
}

#[test]
fn with_merge_radius_sets_radius() {
    let cfg = DetectorConfig::chess().with_merge_radius(5.0);
    assert!((cfg.merge_radius - 5.0).abs() < f32::EPSILON);
}

#[test]
fn chess_refiner_shortcuts_equal_full_constructors() {
    assert_eq!(
        ChessRefiner::center_of_mass(),
        ChessRefiner::CenterOfMass(CenterOfMassConfig::default())
    );
    assert_eq!(
        ChessRefiner::forstner(),
        ChessRefiner::Forstner(ForstnerConfig::default())
    );
    assert_eq!(
        ChessRefiner::saddle_point(),
        ChessRefiner::SaddlePoint(SaddlePointConfig::default())
    );
}

#[test]
fn multiscale_config_pyramid_default_equals_literal() {
    assert_eq!(
        MultiscaleConfig::pyramid_default(),
        MultiscaleConfig::Pyramid {
            levels: 3,
            min_size: 128,
            refinement_radius: 3
        }
    );
}

#[cfg(feature = "ml-refiner")]
#[test]
fn chess_refiner_ml_serializes_as_bare_string() {
    let json = serde_json::to_string(&ChessRefiner::Ml).unwrap();
    assert_eq!(json, "\"ml\"");
    let decoded: ChessRefiner = serde_json::from_str(&json).expect("deserialize ml refiner");
    assert_eq!(decoded, ChessRefiner::Ml);
}