delaunay 0.8.0

D-dimensional Delaunay triangulations and convex hulls in Rust, with exact predicates, deterministic degeneracy handling, explicit topology validation, and bistellar flips for finite point sets.
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
//! Benchmark-only fixture builders for topology states public constructors reject.
//!
//! This module is compiled only with the crate's `bench` feature. Its helpers
//! let Criterion harnesses measure repair paths that require deliberately
//! invalid but structurally coherent TDS states, without exposing those mutation
//! capabilities in the normal library API.

#![forbid(unsafe_code)]

/// Fixtures for PL-manifold repair benchmarks.
pub mod pl_manifold {
    use crate::core::algorithms::pl_manifold_repair::{
        PlManifoldRepairConfig, PlManifoldRepairError, PlManifoldRepairStage,
        PlManifoldRepairStats, manifold_error_matches_repair_stage, repair_facet_oversharing,
        repair_pl_manifold_topology,
    };
    use crate::core::collections::SimplexVertexKeyBuffer;
    use crate::core::simplex::{Simplex, SimplexValidationError};
    use crate::core::tds::{Tds, TdsConstructionError, TdsError, VertexKey};
    use crate::core::vertex::Vertex;
    use crate::geometry::traits::coordinate::CoordinateConversionError;
    use crate::geometry::util::safe_usize_to_scalar;
    use crate::topology::manifold::{
        ManifoldError, ValidatedFacetDegreeMap, validate_closed_boundary_from_validated_facet_map,
        validate_ridge_links, validate_vertex_links_from_validated_facet_map,
    };
    use crate::topology::traits::topological_space::GlobalTopology;
    use thiserror::Error;

    /// Fixture for benchmarking facet over-sharing repair plus orphan cleanup in 3D.
    #[derive(Clone, Debug)]
    #[must_use]
    pub struct OversharedFacetOrphanCleanupFixture3d {
        /// Structurally coherent TDS with one over-shared facet per cluster.
        tds: Tds<(), (), 3>,
        /// Number of independent over-shared clusters in the fixture.
        cluster_count: usize,
    }

    impl OversharedFacetOrphanCleanupFixture3d {
        /// Returns the structurally coherent TDS used as repair benchmark input.
        #[must_use]
        pub const fn repair_input_storage(&self) -> &Tds<(), (), 3> {
            &self.tds
        }

        /// Returns the number of independent over-shared clusters.
        #[must_use]
        pub const fn cluster_count(&self) -> usize {
            self.cluster_count
        }
    }

    /// Fixture for benchmarking targeted PL-manifold repair stages.
    #[derive(Clone, Debug)]
    #[must_use]
    pub struct TargetedTopologyRepairFixture<const D: usize> {
        /// Structurally coherent TDS with targeted PL-manifold violations.
        tds: Tds<(), (), D>,
        /// Number of independent targeted-violation clusters in the fixture.
        cluster_count: usize,
        /// Targeted repair stage exercised by this fixture.
        stage: PlManifoldRepairStage,
    }

    impl<const D: usize> TargetedTopologyRepairFixture<D> {
        /// Returns the structurally coherent TDS used as repair benchmark input.
        #[must_use]
        pub const fn repair_input_storage(&self) -> &Tds<(), (), D> {
            &self.tds
        }

        /// Returns the number of independent targeted-violation clusters.
        #[must_use]
        pub const fn cluster_count(&self) -> usize {
            self.cluster_count
        }

        /// Returns the targeted repair stage validated for this fixture.
        #[must_use]
        pub const fn stage(&self) -> PlManifoldRepairStage {
            self.stage
        }
    }

    /// Errors returned while building PL-manifold repair benchmark fixtures.
    #[derive(Clone, Debug, Error, PartialEq)]
    #[non_exhaustive]
    pub enum PlManifoldRepairFixtureError {
        /// The requested fixture contains no repair work.
        #[error("PL-manifold repair fixture requires at least one cluster")]
        EmptyClusterCount,

        /// The cluster index could not be converted to a finite coordinate offset.
        #[error("failed to convert cluster index {cluster_index} to a coordinate offset: {source}")]
        ClusterIndexConversion {
            /// Cluster index being converted.
            cluster_index: usize,
            /// Underlying coordinate conversion error.
            source: CoordinateConversionError,
        },

        /// A local grid index could not be converted to a finite coordinate offset.
        #[error(
            "failed to convert grid index {grid_index} for cluster {cluster_index} to a coordinate offset: {source}"
        )]
        GridIndexConversion {
            /// Cluster that was being inserted.
            cluster_index: usize,
            /// Local grid index being converted.
            grid_index: usize,
            /// Underlying coordinate conversion error.
            source: CoordinateConversionError,
        },

        /// A fixture vertex could not be constructed.
        #[error("failed to create vertex for cluster {cluster_index}: {source}")]
        Vertex {
            /// Cluster that was being inserted.
            cluster_index: usize,
            /// Underlying coordinate conversion error.
            source: CoordinateConversionError,
        },

        /// A fixture vertex could not be inserted into the TDS.
        #[error("failed to insert vertex for cluster {cluster_index}: {source}")]
        VertexInsert {
            /// Cluster that was being inserted.
            cluster_index: usize,
            /// Underlying typed TDS construction error.
            source: Box<TdsConstructionError>,
        },

        /// A fixture simplex could not be constructed.
        #[error("failed to create simplex for cluster {cluster_index}: {source}")]
        Simplex {
            /// Cluster that was being inserted.
            cluster_index: usize,
            /// Underlying simplex validation error.
            source: SimplexValidationError,
        },

        /// A fixture simplex could not be inserted into the TDS.
        #[error("failed to insert simplex for cluster {cluster_index}: {source}")]
        SimplexInsert {
            /// Cluster that was being inserted.
            cluster_index: usize,
            /// Underlying typed TDS construction error.
            source: Box<TdsConstructionError>,
        },

        /// Structural TDS validation failed for the deliberately invalid fixture.
        #[error("PL-manifold repair fixture has invalid structural TDS state: {source}")]
        StructuralValidation {
            /// Underlying TDS validation error.
            source: TdsError,
        },

        /// The fixture did not produce the intended facet-degree violation.
        #[error(
            "PL-manifold repair fixture with {cluster_count} clusters did not over-share facets"
        )]
        MissingOversharedFacet {
            /// Number of clusters requested.
            cluster_count: usize,
        },

        /// The fixture did not produce the intended targeted topology violation.
        #[error(
            "PL-manifold repair fixture with {cluster_count} clusters did not produce a {stage} violation"
        )]
        MissingTargetedViolation {
            /// Targeted repair stage expected from the fixture.
            stage: PlManifoldRepairStage,
            /// Number of clusters requested.
            cluster_count: usize,
        },

        /// The fixture produced a different topology violation than the one being benchmarked.
        #[error("expected {stage} fixture violation, but validation reported: {source}")]
        UnexpectedTargetedViolation {
            /// Targeted repair stage expected from the fixture.
            stage: PlManifoldRepairStage,
            /// Typed validation error that was observed instead.
            source: ManifoldError,
        },

        /// Repair validation of the fixture failed.
        #[error("PL-manifold repair fixture validation failed: {source}")]
        Repair {
            /// Underlying repair error.
            source: PlManifoldRepairError,
        },

        /// Repair succeeded but did not remove the expected topology.
        #[error(
            "PL-manifold repair fixture removed {actual_simplices} simplices and {actual_vertices} vertices; expected {expected_simplices} simplices and {expected_vertices} vertices"
        )]
        UnexpectedRepairStats {
            /// Expected number of removed simplices.
            expected_simplices: usize,
            /// Actual number of removed simplices.
            actual_simplices: usize,
            /// Expected number of removed orphan vertices.
            expected_vertices: usize,
            /// Actual number of removed orphan vertices.
            actual_vertices: usize,
        },

        /// Targeted repair succeeded but removed less topology than the fixture contains.
        #[error(
            "{stage} fixture removed {actual_simplices} simplices; expected at least {expected_simplices_at_least}"
        )]
        UnexpectedTargetedRepairStats {
            /// Targeted repair stage validated by the fixture.
            stage: PlManifoldRepairStage,
            /// Expected lower bound for removed simplices.
            expected_simplices_at_least: usize,
            /// Actual number of removed simplices.
            actual_simplices: usize,
        },
    }

    /// Builds a 3D fixture with one over-shared facet and one repair-created
    /// orphan vertex per cluster.
    ///
    /// Each cluster contains two valid tetrahedra sharing one triangular facet
    /// plus a deliberately skinny third tetrahedron sharing that same facet. The
    /// skinny simplex owns a unique apex, so removing it creates an isolated
    /// vertex for the repair pass to clean up.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairFixtureError`] if the requested cluster count
    /// is empty, coordinate conversion fails, TDS construction fails, or the
    /// resulting fixture does not contain facet over-sharing.
    fn overshared_facet_orphan_cleanup_3d(
        cluster_count: usize,
    ) -> Result<OversharedFacetOrphanCleanupFixture3d, PlManifoldRepairFixtureError> {
        if cluster_count == 0 {
            return Err(PlManifoldRepairFixtureError::EmptyClusterCount);
        }

        let mut tds = Tds::empty();
        for cluster_index in 0..cluster_count {
            insert_overshared_cluster(&mut tds, cluster_index)?;
        }

        validate_structural_fixture_state(&tds)?;
        let facet_map = tds
            .build_facet_to_simplices_map()
            .map_err(|source| PlManifoldRepairFixtureError::StructuralValidation { source })?;
        if ValidatedFacetDegreeMap::try_from_facet_map(&facet_map).is_ok() {
            return Err(PlManifoldRepairFixtureError::MissingOversharedFacet { cluster_count });
        }

        Ok(OversharedFacetOrphanCleanupFixture3d { tds, cluster_count })
    }

    /// Builds the fixture and verifies the repair pass removes the intended
    /// simplices and orphan vertices.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairFixtureError`] if fixture construction fails,
    /// repair fails, or the repair stats differ from the fixture contract.
    pub fn validated_overshared_facet_orphan_cleanup_3d(
        cluster_count: usize,
    ) -> Result<OversharedFacetOrphanCleanupFixture3d, PlManifoldRepairFixtureError> {
        let fixture = overshared_facet_orphan_cleanup_3d(cluster_count)?;
        let mut repaired = fixture.tds.clone();
        let stats = repair_overshared_facet_orphan_cleanup_3d(&mut repaired)
            .map_err(|source| PlManifoldRepairFixtureError::Repair { source })?;

        if stats.simplices_removed != fixture.cluster_count
            || stats.removed_vertices.len() != fixture.cluster_count
        {
            return Err(PlManifoldRepairFixtureError::UnexpectedRepairStats {
                expected_simplices: fixture.cluster_count,
                actual_simplices: stats.simplices_removed,
                expected_vertices: fixture.cluster_count,
                actual_vertices: stats.removed_vertices.len(),
            });
        }

        Ok(fixture)
    }

    /// Builds and validates a 3D boundary-ridge multiplicity repair benchmark fixture.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairFixtureError`] if construction fails, the
    /// initial topology does not fail with the boundary-ridge stage, or repair
    /// cannot restore targeted PL-manifold invariants.
    pub fn validated_boundary_ridge_multiplicity_repair_3d(
        cluster_count: usize,
    ) -> Result<TargetedTopologyRepairFixture<3>, PlManifoldRepairFixtureError> {
        validate_targeted_fixture(targeted_topology_fixture(
            cluster_count,
            PlManifoldRepairStage::BoundaryRidgeMultiplicity,
            insert_boundary_ridge_multiplicity_cluster,
        )?)
    }

    /// Builds and validates a 2D ridge-link repair benchmark fixture.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairFixtureError`] if construction fails, the
    /// initial topology does not fail with the ridge-link stage, or repair
    /// cannot restore targeted PL-manifold invariants.
    pub fn validated_ridge_link_repair_2d(
        cluster_count: usize,
    ) -> Result<TargetedTopologyRepairFixture<2>, PlManifoldRepairFixtureError> {
        validate_targeted_fixture(targeted_topology_fixture(
            cluster_count,
            PlManifoldRepairStage::RidgeLink,
            insert_ridge_link_cluster,
        )?)
    }

    /// Builds and validates a 3D vertex-link repair benchmark fixture.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairFixtureError`] if construction fails, the
    /// initial topology does not fail with the vertex-link stage, or repair
    /// cannot restore targeted PL-manifold invariants.
    pub fn validated_vertex_link_repair_3d(
        cluster_count: usize,
    ) -> Result<TargetedTopologyRepairFixture<3>, PlManifoldRepairFixtureError> {
        validate_targeted_fixture(targeted_topology_fixture(
            cluster_count,
            PlManifoldRepairStage::VertexLink,
            insert_vertex_link_cluster,
        )?)
    }

    /// Repairs a benchmark fixture using the default PL-manifold repair budget.
    ///
    /// This helper keeps the raw repair primitive crate-internal while giving
    /// benchmark harnesses a stable feature-gated operation to measure.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairError`] if the fixture is structurally invalid
    /// or the default repair budget is exhausted.
    pub fn repair_overshared_facet_orphan_cleanup_3d(
        tds: &mut Tds<(), (), 3>,
    ) -> Result<PlManifoldRepairStats<(), (), 3>, PlManifoldRepairError> {
        repair_facet_oversharing(tds, &PlManifoldRepairConfig::default())
    }

    /// Repairs a targeted topology benchmark fixture using the default PL-manifold repair budget.
    ///
    /// # Errors
    ///
    /// Returns [`PlManifoldRepairError`] if the fixture is structurally invalid,
    /// targeted repair exhausts its default budget, or the postcondition
    /// validation fails.
    pub fn repair_targeted_pl_manifold_topology<const D: usize>(
        tds: &mut Tds<(), (), D>,
    ) -> Result<PlManifoldRepairStats<(), (), D>, PlManifoldRepairError> {
        repair_pl_manifold_topology(
            tds,
            GlobalTopology::Euclidean,
            &PlManifoldRepairConfig::default(),
        )
    }

    /// Inserts one independent over-shared-facet cluster for the benchmark fixture.
    ///
    /// The cluster intentionally creates exactly one third tetrahedron sharing a
    /// triangular facet, so validated public fixture construction can assert one
    /// removed simplex and one orphan cleanup per cluster.
    fn insert_overshared_cluster(
        tds: &mut Tds<(), (), 3>,
        cluster_index: usize,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        let offset = cluster_offset(cluster_index, 4.0)?;

        let vertices = [
            insert_vertex(tds, cluster_index, [offset, 0.0, 0.0])?,
            insert_vertex(tds, cluster_index, [offset + 1.0, 0.0, 0.0])?,
            insert_vertex(tds, cluster_index, [offset, 1.0, 0.0])?,
            insert_vertex(tds, cluster_index, [offset, 0.0, 1.0])?,
            insert_vertex(tds, cluster_index, [offset, 0.0, -1.0])?,
            insert_vertex(tds, cluster_index, [offset + 1.0e-6, 1.0e-6, 1.0e-6])?,
        ];

        insert_checked_simplex(
            tds,
            cluster_index,
            [vertices[0], vertices[1], vertices[2], vertices[3]],
        )?;
        insert_checked_simplex(
            tds,
            cluster_index,
            [vertices[0], vertices[1], vertices[2], vertices[4]],
        )?;
        insert_prechecked_simplex(
            tds,
            cluster_index,
            [vertices[0], vertices[1], vertices[2], vertices[5]],
        )?;
        Ok(())
    }

    /// Builds a targeted topology fixture from independent invalid clusters.
    fn targeted_topology_fixture<const D: usize>(
        cluster_count: usize,
        stage: PlManifoldRepairStage,
        mut insert_cluster: impl FnMut(
            &mut Tds<(), (), D>,
            usize,
        ) -> Result<(), PlManifoldRepairFixtureError>,
    ) -> Result<TargetedTopologyRepairFixture<D>, PlManifoldRepairFixtureError> {
        if cluster_count == 0 {
            return Err(PlManifoldRepairFixtureError::EmptyClusterCount);
        }

        let mut tds = Tds::empty();
        for cluster_index in 0..cluster_count {
            insert_cluster(&mut tds, cluster_index)?;
        }

        validate_structural_fixture_state(&tds)?;
        validate_expected_targeted_violation(&tds, stage, cluster_count)?;

        Ok(TargetedTopologyRepairFixture {
            tds,
            cluster_count,
            stage,
        })
    }

    /// Validates that a targeted fixture repairs successfully before benchmarking.
    fn validate_targeted_fixture<const D: usize>(
        fixture: TargetedTopologyRepairFixture<D>,
    ) -> Result<TargetedTopologyRepairFixture<D>, PlManifoldRepairFixtureError> {
        let mut repaired = fixture.tds.clone();
        let stats = repair_targeted_pl_manifold_topology(&mut repaired)
            .map_err(|source| PlManifoldRepairFixtureError::Repair { source })?;

        if !stats.succeeded || stats.simplices_removed < fixture.cluster_count {
            return Err(
                PlManifoldRepairFixtureError::UnexpectedTargetedRepairStats {
                    stage: fixture.stage,
                    expected_simplices_at_least: fixture.cluster_count,
                    actual_simplices: stats.simplices_removed,
                },
            );
        }

        Ok(fixture)
    }

    /// Confirms the initial fixture fails with the targeted validation stage being measured.
    fn validate_expected_targeted_violation<const D: usize>(
        tds: &Tds<(), (), D>,
        stage: PlManifoldRepairStage,
        cluster_count: usize,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        match validate_targeted_stage(tds, stage) {
            Ok(()) => Err(PlManifoldRepairFixtureError::MissingTargetedViolation {
                stage,
                cluster_count,
            }),
            Err(source) if manifold_error_matches_repair_stage(&source, stage) => Ok(()),
            Err(source) => {
                Err(PlManifoldRepairFixtureError::UnexpectedTargetedViolation { stage, source })
            }
        }
    }

    /// Runs the targeted validator corresponding to a benchmark fixture stage.
    fn validate_targeted_stage<const D: usize>(
        tds: &Tds<(), (), D>,
        stage: PlManifoldRepairStage,
    ) -> Result<(), ManifoldError> {
        match stage {
            PlManifoldRepairStage::BoundaryRidgeMultiplicity => {
                validate_boundary_ridge_multiplicity_stage(tds)
            }
            PlManifoldRepairStage::RidgeLink => validate_ridge_links(tds),
            PlManifoldRepairStage::VertexLink => validate_vertex_link_stage(tds),
        }
    }

    /// Validates the boundary-ridge stage through the same parsed facet-degree proof.
    fn validate_boundary_ridge_multiplicity_stage<const D: usize>(
        tds: &Tds<(), (), D>,
    ) -> Result<(), ManifoldError> {
        let facet_to_simplices = tds.build_facet_to_simplices_map()?;
        let facet_to_simplices = ValidatedFacetDegreeMap::try_from_facet_map(&facet_to_simplices)?;
        validate_closed_boundary_from_validated_facet_map(
            tds,
            facet_to_simplices,
            GlobalTopology::Euclidean,
        )
    }

    /// Validates the vertex-link stage through the same parsed facet-degree proof.
    fn validate_vertex_link_stage<const D: usize>(
        tds: &Tds<(), (), D>,
    ) -> Result<(), ManifoldError> {
        let facet_to_simplices = tds.build_facet_to_simplices_map()?;
        let facet_to_simplices = ValidatedFacetDegreeMap::try_from_facet_map(&facet_to_simplices)?;
        validate_vertex_links_from_validated_facet_map(
            tds,
            facet_to_simplices,
            GlobalTopology::Euclidean,
        )
    }

    /// Inserts one independent cluster with two tetrahedra sharing an edge but no facet.
    fn insert_boundary_ridge_multiplicity_cluster(
        tds: &mut Tds<(), (), 3>,
        cluster_index: usize,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        let offset = cluster_offset(cluster_index, 6.0)?;
        let shared_v0 = insert_vertex(tds, cluster_index, [offset, 0.0, 0.0])?;
        let shared_v1 = insert_vertex(tds, cluster_index, [offset + 2.0, 0.0, 0.0])?;
        let tet1_v2 = insert_vertex(tds, cluster_index, [offset + 0.1, 1.0, 0.2])?;
        let tet1_v3 = insert_vertex(tds, cluster_index, [offset + 0.2, 0.3, 1.3])?;
        let tet2_v2 = insert_vertex(tds, cluster_index, [offset + 0.4, -1.1, 0.7])?;
        let tet2_v3 = insert_vertex(tds, cluster_index, [offset + 0.6, 0.2, -1.4])?;

        for vertices in [
            [shared_v0, shared_v1, tet1_v2, tet1_v3],
            [shared_v0, shared_v1, tet2_v2, tet2_v3],
        ] {
            insert_checked_simplex(tds, cluster_index, vertices)?;
        }

        Ok(())
    }

    /// Inserts one 2D cluster made from two closed `S^2` complexes sharing one vertex.
    fn insert_ridge_link_cluster(
        tds: &mut Tds<(), (), 2>,
        cluster_index: usize,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        let offset = cluster_offset(cluster_index, 16.0)?;
        let v0 = insert_vertex(tds, cluster_index, [offset, 0.0])?;
        let v1 = insert_vertex(tds, cluster_index, [offset + 1.0, 0.0])?;
        let v2 = insert_vertex(tds, cluster_index, [offset, 1.0])?;
        let v3 = insert_vertex(tds, cluster_index, [offset + 1.0, 1.0])?;
        let v4 = insert_vertex(tds, cluster_index, [offset + 8.0, 8.0])?;
        let v5 = insert_vertex(tds, cluster_index, [offset + 9.0, 8.0])?;
        let v6 = insert_vertex(tds, cluster_index, [offset + 8.0, 9.0])?;

        for vertices in [
            [v0, v1, v2],
            [v0, v1, v3],
            [v0, v2, v3],
            [v1, v2, v3],
            [v0, v4, v5],
            [v0, v4, v6],
            [v0, v5, v6],
            [v4, v5, v6],
        ] {
            insert_checked_simplex(tds, cluster_index, vertices)?;
        }

        Ok(())
    }

    /// Inserts one 3D cluster whose apex has a `T^2` link instead of an `S^2` or ball link.
    fn insert_vertex_link_cluster(
        tds: &mut Tds<(), (), 3>,
        cluster_index: usize,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        const N: usize = 3;
        const M: usize = 3;

        let offset = cluster_offset(cluster_index, 8.0)?;
        let mut grid: Vec<Vec<VertexKey>> = Vec::with_capacity(N);
        for i in 0..N {
            let mut row = Vec::with_capacity(M);
            for j in 0..M {
                let x = offset + grid_coordinate(cluster_index, i)?;
                let y = grid_coordinate(cluster_index, j)?;
                row.push(insert_vertex(tds, cluster_index, [x, y, 0.0])?);
            }
            grid.push(row);
        }
        let apex = insert_vertex(tds, cluster_index, [offset + 0.5, 0.5, 1.0])?;

        for i in 0..N {
            for j in 0..M {
                let i1 = (i + 1) % N;
                let j1 = (j + 1) % M;
                let v00 = grid[i][j];
                let v10 = grid[i1][j];
                let v01 = grid[i][j1];
                let v11 = grid[i1][j1];
                for vertices in [[v00, v10, v01, apex], [v10, v11, v01, apex]] {
                    insert_checked_simplex(tds, cluster_index, vertices)?;
                }
            }
        }

        Ok(())
    }

    /// Converts a cluster index to a separated coordinate offset.
    fn cluster_offset(
        cluster_index: usize,
        spacing: f64,
    ) -> Result<f64, PlManifoldRepairFixtureError> {
        Ok(safe_usize_to_scalar(cluster_index).map_err(|source| {
            PlManifoldRepairFixtureError::ClusterIndexConversion {
                cluster_index,
                source,
            }
        })? * spacing)
    }

    /// Converts a local grid index to a finite coordinate.
    fn grid_coordinate(
        cluster_index: usize,
        grid_index: usize,
    ) -> Result<f64, PlManifoldRepairFixtureError> {
        safe_usize_to_scalar(grid_index).map_err(|source| {
            PlManifoldRepairFixtureError::GridIndexConversion {
                cluster_index,
                grid_index,
                source,
            }
        })
    }

    /// Inserts a finite fixture vertex while preserving typed construction errors.
    fn insert_vertex<const D: usize>(
        tds: &mut Tds<(), (), D>,
        cluster_index: usize,
        coords: [f64; D],
    ) -> Result<VertexKey, PlManifoldRepairFixtureError> {
        let vertex =
            Vertex::try_new(coords).map_err(|source| PlManifoldRepairFixtureError::Vertex {
                cluster_index,
                source,
            })?;
        tds.insert_vertex_with_mapping(vertex).map_err(|source| {
            PlManifoldRepairFixtureError::VertexInsert {
                cluster_index,
                source: Box::new(source),
            }
        })
    }

    /// Inserts a manifold-admissible fixture simplex through the normal TDS path.
    fn insert_checked_simplex<const D: usize, const N: usize>(
        tds: &mut Tds<(), (), D>,
        cluster_index: usize,
        vertices: [VertexKey; N],
    ) -> Result<(), PlManifoldRepairFixtureError> {
        let simplex = Simplex::try_new_with_data(
            vertices.into_iter().collect::<SimplexVertexKeyBuffer>(),
            None,
        )
        .map_err(|source| PlManifoldRepairFixtureError::Simplex {
            cluster_index,
            source,
        })?;
        tds.insert_simplex_with_mapping(simplex)
            .map(|_| ())
            .map_err(|source| PlManifoldRepairFixtureError::SimplexInsert {
                cluster_index,
                source: Box::new(source),
            })
    }

    /// Inserts the deliberate over-shared simplex after local fixture assembly
    /// has already proved its vertices exist and its arity is correct.
    fn insert_prechecked_simplex<const D: usize, const N: usize>(
        tds: &mut Tds<(), (), D>,
        cluster_index: usize,
        vertices: [VertexKey; N],
    ) -> Result<(), PlManifoldRepairFixtureError> {
        let simplex = Simplex::try_new_with_data(
            vertices.into_iter().collect::<SimplexVertexKeyBuffer>(),
            None,
        )
        .map_err(|source| PlManifoldRepairFixtureError::Simplex {
            cluster_index,
            source,
        })?;
        tds.insert_simplex_with_mapping_prechecked_topology(simplex)
            .map(|_| ())
            .map_err(|source| PlManifoldRepairFixtureError::SimplexInsert {
                cluster_index,
                source: Box::new(source),
            })
    }

    /// Validates Level 1-2 TDS structure before intentionally violating the
    /// PL-manifold facet-degree invariant measured by the benchmark.
    fn validate_structural_fixture_state<const D: usize>(
        tds: &Tds<(), (), D>,
    ) -> Result<(), PlManifoldRepairFixtureError> {
        tds.validate_vertex_mappings()
            .map_err(|source| PlManifoldRepairFixtureError::StructuralValidation { source })?;
        tds.validate_simplex_mappings()
            .map_err(|source| PlManifoldRepairFixtureError::StructuralValidation { source })?;
        tds.validate_simplex_vertex_keys()
            .map_err(|source| PlManifoldRepairFixtureError::StructuralValidation { source })
    }

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

        #[test]
        fn overshared_fixture_repairs_expected_orphans() {
            let fixture = validated_overshared_facet_orphan_cleanup_3d(3)
                .expect("fixture should repair deterministically");

            assert_eq!(fixture.cluster_count(), 3);
        }

        #[test]
        fn targeted_fixtures_repair_expected_stages() {
            let boundary = validated_boundary_ridge_multiplicity_repair_3d(2)
                .expect("boundary-ridge fixture should repair deterministically");
            let ridge = validated_ridge_link_repair_2d(2)
                .expect("ridge-link fixture should repair deterministically");
            let vertex = validated_vertex_link_repair_3d(2)
                .expect("vertex-link fixture should repair deterministically");

            assert_eq!(
                boundary.stage(),
                PlManifoldRepairStage::BoundaryRidgeMultiplicity
            );
            assert_eq!(ridge.stage(), PlManifoldRepairStage::RidgeLink);
            assert_eq!(vertex.stage(), PlManifoldRepairStage::VertexLink);
        }
    }
}