ringgrid 0.10.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
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
//! `TargetLayout`: the compositional target model.

use std::collections::HashMap;

use projective_grid::{Coord, LatticeKind};

use super::error::TargetValidationError;
use super::fiducials::OriginFiducials;
use super::lattice::{HexGeometry, LatticeGeometry, RectGeometry};
use super::ring::{CodedRingSpec, MarkerCoding, RingGeometry};

const DEFAULT_HEX_NAME: &str = "ringgrid_200mm_hex";
const DEFAULT_HEX_PITCH_MM: f32 = 8.0;
const DEFAULT_HEX_ROWS: usize = 15;
const DEFAULT_HEX_LONG_ROW_COLS: usize = 14;
const DEFAULT_OUTER_RADIUS_MM: f32 = 4.8;
const DEFAULT_INNER_RADIUS_MM: f32 = 3.2;
const DEFAULT_RING_WIDTH_MM: f32 = 1.152;

/// One marker cell of a target: lattice coordinate, board position, and
/// (for coded targets) the assigned codebook ID.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TargetCell {
    /// Lattice coordinate (axial `(q, r)` for hex, `(col, row)` for rect).
    pub coord: Coord,
    /// Cell center in board-frame millimeters.
    pub xy_mm: [f32; 2],
    /// Codebook ID for coded targets; `None` for plain targets.
    pub id: Option<usize>,
}

/// Compositional target layout: lattice × ring geometry × coding × fiducials.
///
/// This is the runtime description of a printed calibration target. It
/// composes four orthogonal aspects:
///
/// - [`LatticeGeometry`] — how marker cells are arranged (hex or rect grid),
/// - [`RingGeometry`] — the ring radii shared by every marker,
/// - [`MarkerCoding`] — whether markers carry a 16-sector identity code or
///   are plain annuli,
/// - [`OriginFiducials`] — optional filled dots that anchor origin and
///   orientation for targets whose markers don't encode identity.
///
/// Construct via [`TargetLayout::new`], a preset
/// ([`TargetLayout::default_hex`], [`TargetLayout::rect_24x24`]), or the
/// JSON loaders. All construction paths validate; geometry cannot be mutated
/// in place because the derived cell cache would silently desync.
#[derive(Debug, Clone)]
pub struct TargetLayout {
    name: String,
    lattice: LatticeGeometry,
    marker: RingGeometry,
    coding: MarkerCoding,
    fiducials: Option<OriginFiducials>,

    /// Derived cell cache in generation order.
    cells: Vec<TargetCell>,
    /// Fast lookup: codebook ID -> index into `cells` (empty for plain).
    id_to_idx: HashMap<usize, usize>,
    /// Fast lookup: lattice coordinate -> index into `cells`.
    coord_to_idx: HashMap<Coord, usize>,
}

impl TargetLayout {
    /// Construct and validate a target layout.
    pub fn new(
        name: impl Into<String>,
        lattice: LatticeGeometry,
        marker: RingGeometry,
        coding: MarkerCoding,
        fiducials: Option<OriginFiducials>,
    ) -> Result<Self, TargetValidationError> {
        let name = name.into();
        if name.trim().is_empty() {
            return Err(TargetValidationError::EmptyName);
        }
        lattice.validate()?;
        marker.validate()?;

        let raw_cells = lattice.generate_cells()?;
        coding.validate(&marker, raw_cells.len())?;

        let min_center_spacing = lattice.min_center_spacing_mm();
        if marker.outer_radius_mm * 2.0 >= min_center_spacing {
            return Err(
                TargetValidationError::OuterDiameterExceedsMinCenterSpacing {
                    marker_outer_diameter_mm: marker.outer_radius_mm * 2.0,
                    min_center_spacing_mm: min_center_spacing,
                },
            );
        }
        let draw_diameter_mm = 2.0 * coding.outer_draw_radius_mm(&marker);
        if draw_diameter_mm >= min_center_spacing {
            return Err(
                TargetValidationError::MarkerDrawDiameterExceedsMinCenterSpacing {
                    marker_draw_diameter_mm: draw_diameter_mm,
                    min_center_spacing_mm: min_center_spacing,
                },
            );
        }

        if let Some(fiducials) = &fiducials {
            let positions: Vec<[f32; 2]> = raw_cells.iter().map(|(_, xy)| *xy).collect();
            fiducials.validate(
                lattice.kind(),
                &positions,
                coding.outer_draw_radius_mm(&marker),
            )?;
        }

        let assignment = match &coding {
            MarkerCoding::Coded16(spec) => spec.id_assignment.as_deref(),
            MarkerCoding::Plain => None,
        };
        let cells: Vec<TargetCell> = raw_cells
            .into_iter()
            .enumerate()
            .map(|(idx, (coord, xy_mm))| TargetCell {
                coord,
                xy_mm,
                id: coding.is_coded().then(|| match assignment {
                    Some(ids) => ids[idx],
                    None => idx,
                }),
            })
            .collect();

        let id_to_idx = cells
            .iter()
            .enumerate()
            .filter_map(|(idx, cell)| cell.id.map(|id| (id, idx)))
            .collect();
        let coord_to_idx = cells
            .iter()
            .enumerate()
            .map(|(idx, cell)| (cell.coord, idx))
            .collect();

        Ok(Self {
            name,
            lattice,
            marker,
            coding,
            fiducials,
            cells,
            id_to_idx,
            coord_to_idx,
        })
    }

    /// Construct and validate a target layout with automatically-placed origin
    /// fiducials.
    ///
    /// Convenience over [`new`](Self::new): computes an asymmetric dot triad in
    /// the lattice gaps near the origin corner via [`OriginFiducials::auto`],
    /// then constructs. Works for any lattice/coding; the `dots: auto` config
    /// option routes here. (Origin dots are only meaningful for plain targets —
    /// coded markers already encode identity — but this method does not forbid
    /// coded targets: the dots are simply inert during coded detection.)
    pub fn with_auto_fiducials(
        name: impl Into<String>,
        lattice: LatticeGeometry,
        marker: RingGeometry,
        coding: MarkerCoding,
    ) -> Result<Self, TargetValidationError> {
        let fiducials = OriginFiducials::auto(&lattice, marker, &coding)?;
        Self::new(name, lattice, marker, coding, Some(fiducials))
    }

    /// Construct a 16-sector coded hex target from direct geometry arguments.
    ///
    /// Uses a deterministic geometry-derived name (the same scheme legacy v4
    /// boards used) so the layout round-trips through JSON without the caller
    /// supplying a name up front.
    pub fn coded_hex(
        pitch_mm: f32,
        rows: usize,
        long_row_cols: usize,
        outer_radius_mm: f32,
        inner_radius_mm: f32,
        ring_width_mm: f32,
    ) -> Result<Self, TargetValidationError> {
        Self::new(
            hex_generated_name(
                pitch_mm,
                rows,
                long_row_cols,
                outer_radius_mm,
                inner_radius_mm,
                ring_width_mm,
            ),
            LatticeGeometry::Hex(HexGeometry {
                rows,
                long_row_cols,
                pitch_mm,
            }),
            RingGeometry {
                outer_radius_mm,
                inner_radius_mm,
            },
            MarkerCoding::Coded16(CodedRingSpec {
                ring_width_mm,
                id_assignment: None,
            }),
            None,
        )
    }

    /// The classic ringgrid target: 15-row hex lattice of 16-sector coded
    /// rings at 8 mm pitch (203 markers on a 200 mm board).
    pub fn default_hex() -> Self {
        Self::new(
            DEFAULT_HEX_NAME,
            LatticeGeometry::Hex(HexGeometry {
                rows: DEFAULT_HEX_ROWS,
                long_row_cols: DEFAULT_HEX_LONG_ROW_COLS,
                pitch_mm: DEFAULT_HEX_PITCH_MM,
            }),
            RingGeometry {
                outer_radius_mm: DEFAULT_OUTER_RADIUS_MM,
                inner_radius_mm: DEFAULT_INNER_RADIUS_MM,
            },
            MarkerCoding::Coded16(CodedRingSpec {
                ring_width_mm: DEFAULT_RING_WIDTH_MM,
                id_assignment: None,
            }),
            None,
        )
        .expect("default hex target spec must be valid")
    }

    /// A 24×24 plain rect target: a square lattice of plain rings at 14 mm
    /// pitch (outer Ø11.2 / inner Ø5.6 mm) with an origin-dot triad in the cell
    /// gaps near the board center.
    ///
    /// The dot geometry is **frozen**: physical boards printed from this named
    /// preset in earlier releases depend on these exact positions to anchor.
    /// For a fresh target with automatically placed dots, use
    /// [`with_auto_fiducials`](Self::with_auto_fiducials) or the recipe
    /// `dots: auto` option instead — the auto placement is not required to match
    /// this preset's historical triad.
    pub fn rect_24x24() -> Self {
        Self::new(
            "rect_24x24",
            LatticeGeometry::Rect(RectGeometry {
                rows: 24,
                cols: 24,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            MarkerCoding::Plain,
            Some(OriginFiducials {
                dot_radius_mm: 1.4,
                // Cell-gap centers near the board center: middle, one pitch
                // left, one pitch down (board frame: first ring at [0, 0],
                // +y downward).
                dots_mm: vec![[161.0, 161.0], [147.0, 161.0], [161.0, 175.0]],
            }),
        )
        .expect("rect_24x24 preset must be valid")
    }

    /// Human-readable name of the target layout.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Lattice arrangement of marker cells.
    pub fn lattice(&self) -> &LatticeGeometry {
        &self.lattice
    }

    /// The projective-grid lattice kind matching this target.
    pub fn lattice_kind(&self) -> LatticeKind {
        self.lattice.kind()
    }

    /// Ring radii shared by every marker.
    pub fn ring(&self) -> RingGeometry {
        self.marker
    }

    /// Marker coding style.
    pub fn coding(&self) -> &MarkerCoding {
        &self.coding
    }

    /// Origin fiducials, when the target defines them.
    pub fn fiducials(&self) -> Option<&OriginFiducials> {
        self.fiducials.as_ref()
    }

    /// Whether markers encode a decodable identity.
    pub fn is_coded(&self) -> bool {
        self.coding.is_coded()
    }

    /// Lattice pitch in millimeters.
    pub fn pitch_mm(&self) -> f32 {
        self.lattice.pitch_mm()
    }

    /// Minimum center-to-center spacing between adjacent markers (mm).
    pub fn min_center_spacing_mm(&self) -> f32 {
        self.lattice.min_center_spacing_mm()
    }

    /// Outermost drawn radius of a marker (mm), including ring stroke.
    pub fn outer_draw_radius_mm(&self) -> f32 {
        self.coding.outer_draw_radius_mm(&self.marker)
    }

    /// Code band bounds `(inner_mm, outer_mm)` for coded targets.
    pub fn code_band_bounds_mm(&self) -> Option<(f32, f32)> {
        self.coding.code_band_bounds_mm(&self.marker)
    }

    /// All marker cells in generation order.
    pub fn cells(&self) -> &[TargetCell] {
        &self.cells
    }

    /// Total number of marker cells.
    pub fn n_cells(&self) -> usize {
        self.cells.len()
    }

    /// Cell center in board millimeters for a lattice coordinate.
    pub fn cell_xy_mm(&self, coord: Coord) -> Option<[f32; 2]> {
        self.coord_to_idx.get(&coord).map(|&i| self.cells[i].xy_mm)
    }

    /// Codebook ID assigned to a lattice coordinate (coded targets only).
    pub fn id_of(&self, coord: Coord) -> Option<usize> {
        self.coord_to_idx
            .get(&coord)
            .and_then(|&i| self.cells[i].id)
    }

    /// Lattice coordinate of a codebook ID (coded targets only).
    pub fn coord_of_id(&self, id: usize) -> Option<Coord> {
        self.id_to_idx.get(&id).map(|&i| self.cells[i].coord)
    }

    /// Cell center in board millimeters for a codebook ID (coded targets only).
    pub fn xy_mm_of_id(&self, id: usize) -> Option<[f32; 2]> {
        self.id_to_idx.get(&id).map(|&i| self.cells[i].xy_mm)
    }

    /// Cell carrying a codebook ID (coded targets only).
    pub fn cell_of_id(&self, id: usize) -> Option<&TargetCell> {
        self.id_to_idx.get(&id).map(|&i| &self.cells[i])
    }

    /// Iterator over all codebook IDs present on the target.
    pub fn marker_ids(&self) -> impl Iterator<Item = usize> + '_ {
        self.cells.iter().filter_map(|cell| cell.id)
    }

    /// Maximum codebook ID present on the target (0 for plain targets).
    pub fn max_marker_id(&self) -> usize {
        self.marker_ids().max().unwrap_or(0)
    }

    /// Axis-aligned cell-center bounds in board mm: `(min_xy, max_xy)`.
    pub fn marker_bounds_mm(&self) -> Option<([f32; 2], [f32; 2])> {
        let first = self.cells.first()?;
        let mut min_xy = first.xy_mm;
        let mut max_xy = first.xy_mm;
        for cell in &self.cells[1..] {
            min_xy[0] = min_xy[0].min(cell.xy_mm[0]);
            min_xy[1] = min_xy[1].min(cell.xy_mm[1]);
            max_xy[0] = max_xy[0].max(cell.xy_mm[0]);
            max_xy[1] = max_xy[1].max(cell.xy_mm[1]);
        }
        Some((min_xy, max_xy))
    }

    /// Axis-aligned cell-center span in board mm (`[width, height]`).
    pub fn marker_span_mm(&self) -> Option<[f32; 2]> {
        self.marker_bounds_mm()
            .map(|(min_xy, max_xy)| [max_xy[0] - min_xy[0], max_xy[1] - min_xy[1]])
    }
}

impl Default for TargetLayout {
    fn default() -> Self {
        Self::default_hex()
    }
}

/// Deterministic geometry-derived name for coded hex targets, matching the
/// scheme legacy v4 boards used so v4 and v5 flows produce identical names for
/// identical geometry.
pub(crate) fn hex_generated_name(
    pitch_mm: f32,
    rows: usize,
    long_row_cols: usize,
    outer_radius_mm: f32,
    inner_radius_mm: f32,
    ring_width_mm: f32,
) -> String {
    format!(
        "ringgrid_hex_r{rows}_c{long_row_cols}_p{pitch_mm:.3}_o{outer_radius_mm:.3}_i{inner_radius_mm:.3}_w{ring_width_mm:.3}"
    )
}

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

    fn hex_lattice(rows: usize, long_row_cols: usize, pitch_mm: f32) -> LatticeGeometry {
        LatticeGeometry::Hex(HexGeometry {
            rows,
            long_row_cols,
            pitch_mm,
        })
    }

    fn coded(ring_width_mm: f32) -> MarkerCoding {
        MarkerCoding::Coded16(CodedRingSpec {
            ring_width_mm,
            id_assignment: None,
        })
    }

    #[test]
    fn rect_preset_has_expected_shape() {
        let target = TargetLayout::rect_24x24();
        assert_eq!(target.n_cells(), 576);
        assert!(!target.is_coded());
        assert_eq!(target.pitch_mm(), 14.0);
        assert_eq!(target.min_center_spacing_mm(), 14.0);
        assert_eq!(target.outer_draw_radius_mm(), 5.6);
        assert_eq!(target.code_band_bounds_mm(), None);

        // 23 pitches of 14 mm per side.
        let span = target.marker_span_mm().expect("span");
        assert_eq!(span, [322.0, 322.0]);

        // Corner cells at lattice coordinates.
        assert_eq!(
            target.cell_xy_mm(projective_grid::Coord::new(0, 0)),
            Some([0.0, 0.0])
        );
        assert_eq!(
            target.cell_xy_mm(projective_grid::Coord::new(23, 23)),
            Some([322.0, 322.0])
        );

        let fiducials = target.fiducials().expect("preset has dots");
        assert_eq!(fiducials.dots_mm.len(), 3);
        assert_eq!(fiducials.dot_radius_mm, 1.4);

        // Plain targets expose no IDs.
        assert_eq!(target.marker_ids().count(), 0);
        assert_eq!(target.id_of(projective_grid::Coord::new(0, 0)), None);
    }

    #[test]
    fn coded_ids_are_sequential_without_assignment() {
        let target = TargetLayout::new(
            "t",
            hex_lattice(3, 4, 8.0),
            RingGeometry {
                outer_radius_mm: 4.8,
                inner_radius_mm: 3.2,
            },
            coded(1.152),
            None,
        )
        .expect("valid");
        for (idx, cell) in target.cells().iter().enumerate() {
            assert_eq!(cell.id, Some(idx));
        }
        assert_eq!(target.coord_of_id(0), Some(target.cells()[0].coord));
        assert_eq!(target.xy_mm_of_id(0), Some([0.0, 0.0]));
    }

    #[test]
    fn rejects_codebook_capacity_overflow() {
        // 40×40 rect = 1600 cells > 893 codewords.
        let err = TargetLayout::new(
            "t",
            LatticeGeometry::Rect(RectGeometry {
                rows: 40,
                cols: 40,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            coded(0.8),
            None,
        )
        .expect_err("capacity exceeded");
        assert!(matches!(
            err,
            TargetValidationError::CodebookCapacityExceeded { n_cells: 1600, .. }
        ));

        // The same board as plain is fine: no identity coding needed.
        TargetLayout::new(
            "t",
            LatticeGeometry::Rect(RectGeometry {
                rows: 40,
                cols: 40,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            MarkerCoding::Plain,
            None,
        )
        .expect("plain has no capacity limit");
    }

    #[test]
    fn rejects_rotationally_symmetric_fiducials() {
        // A single dot at the exact lattice center is invariant under every
        // rotation.
        let err = TargetLayout::new(
            "t",
            LatticeGeometry::Rect(RectGeometry {
                rows: 4,
                cols: 4,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            MarkerCoding::Plain,
            Some(OriginFiducials {
                dot_radius_mm: 1.4,
                dots_mm: vec![[21.0, 21.0]],
            }),
        )
        .expect_err("symmetric dots");
        assert!(matches!(
            err,
            TargetValidationError::FiducialsRotationallySymmetric { .. }
        ));

        // Adding one off-center dot breaks all rotations.
        TargetLayout::new(
            "t",
            LatticeGeometry::Rect(RectGeometry {
                rows: 4,
                cols: 4,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            MarkerCoding::Plain,
            Some(OriginFiducials {
                dot_radius_mm: 1.4,
                dots_mm: vec![[21.0, 21.0], [7.0, 21.0]],
            }),
        )
        .expect("asymmetric dots are valid");
    }

    #[test]
    fn rejects_dot_overlapping_marker() {
        let err = TargetLayout::new(
            "t",
            LatticeGeometry::Rect(RectGeometry {
                rows: 4,
                cols: 4,
                pitch_mm: 14.0,
            }),
            RingGeometry {
                outer_radius_mm: 5.6,
                inner_radius_mm: 2.8,
            },
            MarkerCoding::Plain,
            Some(OriginFiducials {
                dot_radius_mm: 1.4,
                // Directly on a ring center.
                dots_mm: vec![[14.0, 14.0]],
            }),
        )
        .expect_err("dot on marker");
        assert!(matches!(
            err,
            TargetValidationError::DotOverlapsMarker { index: 0 }
        ));
    }

    #[test]
    fn rejects_empty_fiducial_dots_and_bad_radius() {
        let make = |dot_radius_mm: f32, dots_mm: Vec<[f32; 2]>| {
            TargetLayout::new(
                "t",
                LatticeGeometry::Rect(RectGeometry {
                    rows: 4,
                    cols: 4,
                    pitch_mm: 14.0,
                }),
                RingGeometry {
                    outer_radius_mm: 5.6,
                    inner_radius_mm: 2.8,
                },
                MarkerCoding::Plain,
                Some(OriginFiducials {
                    dot_radius_mm,
                    dots_mm,
                }),
            )
        };
        assert!(matches!(
            make(1.4, vec![]),
            Err(TargetValidationError::EmptyFiducialDots)
        ));
        assert!(matches!(
            make(0.0, vec![[21.0, 21.0]]),
            Err(TargetValidationError::InvalidDotRadius { .. })
        ));
        assert!(matches!(
            make(1.4, vec![[f32::NAN, 21.0]]),
            Err(TargetValidationError::NonFiniteDot { index: 0 })
        ));
    }

    #[test]
    fn validation_rejects_degenerate_geometry() {
        let ring = |outer, inner| RingGeometry {
            outer_radius_mm: outer,
            inner_radius_mm: inner,
        };
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(0, 4, 8.0),
                ring(4.8, 3.2),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::InvalidRows { rows: 0 })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 1, 8.0),
                ring(4.8, 3.2),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::InvalidLongRowColsForRows {
                rows: 3,
                long_row_cols: 1,
            })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 4, 8.0),
                ring(4.8, 4.8),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::InnerRadiusNotSmallerThanOuter { .. })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 4, 8.0),
                ring(4.8, 4.1),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::NonPositiveCodeBandGap { .. })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 4, f32::NAN),
                ring(4.8, 3.2),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::InvalidPitch { .. })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 4, 5.0),
                ring(4.0, 2.0),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::MarkerDrawDiameterExceedsMinCenterSpacing { .. })
        ));
        assert!(matches!(
            TargetLayout::new(
                "t",
                hex_lattice(3, 4, 8.0),
                ring(4.8, 3.2),
                coded(0.0),
                None
            ),
            Err(TargetValidationError::InvalidRingWidth { .. })
        ));
        assert!(matches!(
            TargetLayout::new(
                "",
                hex_lattice(3, 4, 8.0),
                ring(4.8, 3.2),
                coded(1.152),
                None
            ),
            Err(TargetValidationError::EmptyName)
        ));
    }
}