delaunay 0.8.1

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
//! Fluent construction of proof-bearing triangulation data structures.
//!
//! [`TdsBuilder`] owns the raw explicit-connectivity boundary. It assembles
//! vertices and maximal simplices, derives adjacency and incidence, normalizes
//! coherent combinatorial orientation, and publishes a [`Tds`] only after
//! cumulative Levels 1–2 validation succeeds.

#![forbid(unsafe_code)]

use super::{
    Tds, TdsConstructionError, TdsDraft, TdsDraftError, TdsError, TdsMutationError, VertexKey,
};
use crate::core::collections::{
    Entry, FastHashMap, SimplexVertexKeyBuffer, fast_hash_map_with_capacity,
};
use crate::core::facet::facet_key_from_vertices;
use crate::core::simplex::{Simplex, SimplexValidationError};
use crate::core::vertex::Vertex;
use std::marker::PhantomData;
use thiserror::Error;

/// Parse failures at the raw explicit-connectivity boundary.
#[derive(Clone, Debug, Error, PartialEq)]
pub(crate) enum ExplicitSimplexParseError {
    /// No maximal simplices were supplied for a nonempty vertex set.
    #[error("no simplices provided for nonempty TDS construction")]
    EmptySimplices,
    /// A simplex specification has the wrong arity for dimension `D`.
    #[error(
        "simplex {simplex_index} has {actual} vertex indices, expected {expected} for a simplex"
    )]
    InvalidSimplexArity {
        simplex_index: usize,
        actual: usize,
        expected: usize,
    },
    /// A simplex specification references a missing input vertex.
    #[error(
        "simplex {simplex_index} references vertex index {vertex_index}, but the vertex count is {bound}"
    )]
    IndexOutOfBounds {
        simplex_index: usize,
        vertex_index: usize,
        bound: usize,
    },
    /// A simplex specification repeats one input vertex.
    #[error("simplex {simplex_index} contains duplicate vertex index {vertex_index}")]
    DuplicateVertexInSimplex {
        simplex_index: usize,
        vertex_index: usize,
    },
}

impl From<ExplicitSimplexParseError> for TdsBuilderError {
    fn from(source: ExplicitSimplexParseError) -> Self {
        match source {
            ExplicitSimplexParseError::EmptySimplices => Self::EmptySimplices,
            ExplicitSimplexParseError::InvalidSimplexArity {
                simplex_index,
                actual,
                expected,
            } => Self::InvalidSimplexArity {
                simplex_index,
                actual,
                expected,
            },
            ExplicitSimplexParseError::IndexOutOfBounds {
                simplex_index,
                vertex_index,
                bound,
            } => Self::IndexOutOfBounds {
                simplex_index,
                vertex_index,
                bound,
            },
            ExplicitSimplexParseError::DuplicateVertexInSimplex {
                simplex_index,
                vertex_index,
            } => Self::DuplicateVertexInSimplex {
                simplex_index,
                vertex_index,
            },
        }
    }
}

/// Parsed explicit TDS input whose simplex indices are locally well formed.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ParsedTdsInput<'a, U, const D: usize> {
    vertices: &'a [Vertex<U, D>],
    simplices: &'a [Vec<usize>],
}

impl<'a, U, const D: usize> ParsedTdsInput<'a, U, D> {
    /// Parses raw vertex-index connectivity exactly once at the input boundary.
    pub(crate) fn try_new(
        vertices: &'a [Vertex<U, D>],
        simplices: &'a [Vec<usize>],
    ) -> Result<Self, ExplicitSimplexParseError> {
        if simplices.is_empty() && !vertices.is_empty() {
            return Err(ExplicitSimplexParseError::EmptySimplices);
        }

        for (simplex_index, simplex) in simplices.iter().enumerate() {
            if simplex.len() != D + 1 {
                return Err(ExplicitSimplexParseError::InvalidSimplexArity {
                    simplex_index,
                    actual: simplex.len(),
                    expected: D + 1,
                });
            }

            for (offset, &vertex_index) in simplex.iter().enumerate() {
                if vertex_index >= vertices.len() {
                    return Err(ExplicitSimplexParseError::IndexOutOfBounds {
                        simplex_index,
                        vertex_index,
                        bound: vertices.len(),
                    });
                }
                if simplex[..offset].contains(&vertex_index) {
                    return Err(ExplicitSimplexParseError::DuplicateVertexInSimplex {
                        simplex_index,
                        vertex_index,
                    });
                }
            }
        }

        Ok(Self {
            vertices,
            simplices,
        })
    }
}

#[derive(Clone, Copy, Debug)]
enum TdsBuilderInput<'a, U, const D: usize> {
    Raw {
        vertices: &'a [Vertex<U, D>],
        simplices: &'a [Vec<usize>],
    },
    Parsed(ParsedTdsInput<'a, U, D>),
}

impl<'a, U, const D: usize> TdsBuilderInput<'a, U, D> {
    const fn vertices(&self) -> &'a [Vertex<U, D>] {
        match self {
            Self::Raw { vertices, .. } | Self::Parsed(ParsedTdsInput { vertices, .. }) => vertices,
        }
    }

    const fn simplices(&self) -> &'a [Vec<usize>] {
        match self {
            Self::Raw { simplices, .. } | Self::Parsed(ParsedTdsInput { simplices, .. }) => {
                simplices
            }
        }
    }

    fn parse(self) -> Result<ParsedTdsInput<'a, U, D>, ExplicitSimplexParseError> {
        match self {
            Self::Raw {
                vertices,
                simplices,
            } => ParsedTdsInput::try_new(vertices, simplices),
            Self::Parsed(parsed) => Ok(parsed),
        }
    }
}

/// Typed failures from explicit Levels 1–2 TDS construction.
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum TdsBuilderError {
    /// No maximal simplices were supplied for a nonempty vertex set.
    #[error("no simplices provided for nonempty TDS construction")]
    EmptySimplices,
    /// A simplex specification has the wrong arity for dimension `D`.
    #[error(
        "simplex {simplex_index} has {actual} vertex indices, expected {expected} for a simplex"
    )]
    InvalidSimplexArity {
        /// Zero-based simplex specification index.
        simplex_index: usize,
        /// Number of supplied vertex indices.
        actual: usize,
        /// Required number of vertex indices (`D + 1`).
        expected: usize,
    },
    /// A simplex specification references a missing input vertex.
    #[error(
        "simplex {simplex_index} references vertex index {vertex_index}, but the vertex count is {bound}"
    )]
    IndexOutOfBounds {
        /// Zero-based simplex specification index.
        simplex_index: usize,
        /// Invalid input vertex index.
        vertex_index: usize,
        /// Number of supplied vertices.
        bound: usize,
    },
    /// A simplex specification repeats one input vertex.
    #[error("simplex {simplex_index} contains duplicate vertex index {vertex_index}")]
    DuplicateVertexInSimplex {
        /// Zero-based simplex specification index.
        simplex_index: usize,
        /// Repeated input vertex index.
        vertex_index: usize,
    },
    /// A vertex could not be inserted into the unpublished TDS workspace.
    #[error("vertex {vertex_index} could not be inserted during TDS construction: {source}")]
    VertexInsertion {
        /// Zero-based input vertex index.
        vertex_index: usize,
        /// Typed TDS insertion failure.
        #[source]
        source: Box<TdsConstructionError>,
    },
    /// Cross-simplex topology failed before simplex insertion began.
    #[error("explicit TDS topology is invalid: {source}")]
    TopologyValidation {
        /// Typed TDS topology failure.
        #[source]
        source: Box<TdsConstructionError>,
    },
    /// A validated simplex specification could not create a simplex value.
    #[error("simplex {simplex_index} could not be created during TDS construction: {source}")]
    SimplexCreation {
        /// Zero-based simplex specification index.
        simplex_index: usize,
        /// Typed simplex construction failure.
        #[source]
        source: SimplexValidationError,
    },
    /// A simplex could not be inserted into the unpublished TDS workspace.
    #[error("simplex {simplex_index} could not be inserted during TDS construction: {source}")]
    SimplexInsertion {
        /// Zero-based simplex specification index.
        simplex_index: usize,
        /// Typed TDS insertion failure.
        #[source]
        source: Box<TdsConstructionError>,
    },
    /// Neighbor derivation failed for the assembled connectivity.
    #[error("neighbor assignment failed during TDS construction: {source}")]
    NeighborAssignment {
        /// Typed TDS structural failure.
        #[source]
        source: Box<TdsError>,
    },
    /// Vertex-incidence derivation failed for the assembled connectivity.
    #[error("incident-simplex assignment failed during TDS construction: {source}")]
    IncidentAssignment {
        /// Typed TDS mutation failure.
        #[source]
        source: Box<TdsMutationError>,
    },
    /// The simplex complex could not be assigned coherent combinatorial orientation.
    #[error("orientation normalization failed during TDS construction: {source}")]
    OrientationNormalization {
        /// Typed TDS orientation failure.
        #[source]
        source: Box<TdsError>,
    },
    /// Final cumulative Levels 1–2 validation failed.
    #[error("Levels 1-2 validation failed during TDS construction: {source}")]
    Validation {
        /// Typed TDS validation failure.
        #[source]
        source: Box<TdsError>,
    },
}

/// Fluent builder for a proof-bearing Levels 1–2 [`Tds`].
///
/// Raw simplex specifications index the supplied vertex slice. [`build`](Self::build)
/// validates the complete request, derives all TDS-owned state, normalizes
/// coherent orientation, and returns only a constructed TDS that passes
/// [`Tds::validate`]. No geometric realization or Delaunay property is inferred
/// at this layer.
///
/// # Examples
///
/// ```rust
/// use delaunay::prelude::geometry::CoordinateConversionError;
/// use delaunay::prelude::tds::{TdsBuilder, TdsBuilderError};
///
/// # #[derive(Debug, thiserror::Error)]
/// # enum ExampleError {
/// #     #[error(transparent)]
/// #     Coordinate(#[from] CoordinateConversionError),
/// #     #[error(transparent)]
/// #     Build(#[from] TdsBuilderError),
/// # }
/// # fn main() -> Result<(), ExampleError> {
/// let vertices = [
///     delaunay::vertex![0.0, 0.0]?,
///     delaunay::vertex![1.0, 0.0]?,
///     delaunay::vertex![0.0, 1.0]?,
/// ];
/// let simplices = [vec![0, 1, 2]];
///
/// let tds = TdsBuilder::new(&vertices, &simplices).build()?;
/// assert_eq!(tds.number_of_simplices(), 1);
/// assert!(tds.validate().is_ok());
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug)]
pub struct TdsBuilder<'a, U, const D: usize, V = ()> {
    input: TdsBuilderInput<'a, U, D>,
    _simplex_data: PhantomData<V>,
}

impl<'a, U, const D: usize> TdsBuilder<'a, U, D> {
    /// Creates an inert explicit-connectivity construction request.
    ///
    /// Validation is deliberately deferred to [`build`](Self::build), so
    /// creating a builder is infallible even when the raw simplex specifications
    /// are invalid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use delaunay::prelude::tds::TdsBuilder;
    ///
    /// # fn main() -> Result<(), delaunay::prelude::geometry::CoordinateConversionError> {
    /// let vertices = [
    ///     delaunay::vertex![0.0, 0.0]?,
    ///     delaunay::vertex![1.0, 0.0]?,
    ///     delaunay::vertex![0.0, 1.0]?,
    /// ];
    /// let simplices = [vec![0, 1, 2]];
    /// let builder = TdsBuilder::new(&vertices, &simplices);
    /// assert_eq!(builder.vertex_count(), 3);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub const fn new(vertices: &'a [Vertex<U, D>], simplices: &'a [Vec<usize>]) -> Self {
        Self {
            input: TdsBuilderInput::Raw {
                vertices,
                simplices,
            },
            _simplex_data: PhantomData,
        }
    }

    /// Creates a builder from input already parsed by a higher boundary.
    pub(crate) const fn from_parsed(input: ParsedTdsInput<'a, U, D>) -> Self {
        Self {
            input: TdsBuilderInput::Parsed(input),
            _simplex_data: PhantomData,
        }
    }
}

impl<'a, U, V, const D: usize> TdsBuilder<'a, U, D, V> {
    /// Returns the number of raw input vertices in this request.
    #[must_use]
    pub const fn vertex_count(&self) -> usize {
        self.input.vertices().len()
    }

    /// Returns the number of raw maximal-simplex specifications in this request.
    #[must_use]
    pub const fn simplex_count(&self) -> usize {
        self.input.simplices().len()
    }

    /// Selects the persisted simplex payload type without changing connectivity.
    ///
    /// The builder initializes simplex payloads as `None`; callers can populate
    /// them through checked owner methods after construction.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use delaunay::prelude::geometry::CoordinateConversionError;
    /// use delaunay::prelude::tds::{Tds, TdsBuilder, TdsBuilderError};
    ///
    /// # #[derive(Debug, thiserror::Error)]
    /// # enum ExampleError {
    /// #     #[error(transparent)]
    /// #     Coordinate(#[from] CoordinateConversionError),
    /// #     #[error(transparent)]
    /// #     Build(#[from] TdsBuilderError),
    /// # }
    /// # fn main() -> Result<(), ExampleError> {
    /// let vertices = [
    ///     delaunay::vertex![0.0, 0.0]?,
    ///     delaunay::vertex![1.0, 0.0]?,
    ///     delaunay::vertex![0.0, 1.0]?,
    /// ];
    /// let simplices = [vec![0, 1, 2]];
    /// let tds: Tds<(), usize, 2> = TdsBuilder::new(&vertices, &simplices)
    ///     .simplex_data_type::<usize>()
    ///     .build()?;
    /// assert_eq!(tds.number_of_simplices(), 1);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub const fn simplex_data_type<W>(self) -> TdsBuilder<'a, U, D, W> {
        TdsBuilder {
            input: self.input,
            _simplex_data: PhantomData,
        }
    }

    /// Validates and assembles a proof-bearing Levels 1–2 TDS.
    ///
    /// The returned value has complete UUID mappings, bounded facet sharing,
    /// reciprocal neighbors, complete incidence, no duplicate simplices, and
    /// coherent combinatorial orientation. The construction state is marked
    /// complete only after [`Tds::validate`] succeeds.
    /// Input vertices and their payloads are cloned into the new TDS, so `U`
    /// need only implement [`Clone`]. The simplex payload type `V` is unbounded
    /// because explicit construction initializes each simplex payload as `None`.
    ///
    /// # Errors
    ///
    /// Returns [`TdsBuilderError`] when raw indices are malformed, element
    /// insertion fails, derived adjacency or incidence cannot be established,
    /// coherent orientation cannot be normalized, or final Levels 1–2
    /// validation fails.
    pub fn build(self) -> Result<Tds<U, V, D>, TdsBuilderError>
    where
        U: Clone,
    {
        let parsed = self.input.parse().map_err(TdsBuilderError::from)?;
        let vertices = parsed.vertices;
        let simplices = parsed.simplices;

        let mut draft = TdsDraft::new();
        let mut index_to_key = Vec::with_capacity(vertices.len());
        for (vertex_index, vertex) in vertices.iter().cloned().enumerate() {
            let vertex_key =
                draft
                    .insert_vertex(vertex)
                    .map_err(|source| TdsBuilderError::VertexInsertion {
                        vertex_index,
                        source: Box::new(source),
                    })?;
            index_to_key.push(vertex_key);
        }

        Self::validate_topology(simplices, &index_to_key).map_err(|source| {
            TdsBuilderError::TopologyValidation {
                source: Box::new(source),
            }
        })?;

        for (simplex_index, simplex_spec) in simplices.iter().enumerate() {
            let vertex_keys = Self::simplex_vertex_keys(simplex_spec, &index_to_key);
            let simplex = Simplex::try_new(vertex_keys).map_err(|source| {
                TdsBuilderError::SimplexCreation {
                    simplex_index,
                    source,
                }
            })?;
            draft
                .insert_simplex_prechecked_topology(simplex)
                .map_err(|source| TdsBuilderError::SimplexInsertion {
                    simplex_index,
                    source: Box::new(source),
                })?;
        }

        draft.finish().map_err(|source| match source {
            TdsDraftError::NeighborAssignment { source } => {
                TdsBuilderError::NeighborAssignment { source }
            }
            TdsDraftError::IncidentAssignment { source } => {
                TdsBuilderError::IncidentAssignment { source }
            }
            TdsDraftError::OrientationNormalization { source } => {
                TdsBuilderError::OrientationNormalization { source }
            }
            TdsDraftError::Validation { source } => TdsBuilderError::Validation { source },
        })
    }

    /// Proves cross-simplex uniqueness and facet multiplicity in one linear pass.
    fn validate_topology(
        simplices: &[Vec<usize>],
        index_to_key: &[VertexKey],
    ) -> Result<(), TdsConstructionError> {
        Self::reject_duplicate_simplices(simplices, index_to_key)?;
        Self::reject_overshared_facets(simplices, index_to_key)?;
        Ok(())
    }

    /// Rejects repeated maximal simplices before the unchecked bulk insertion loop.
    fn reject_duplicate_simplices(
        simplices: &[Vec<usize>],
        index_to_key: &[VertexKey],
    ) -> Result<(), TdsConstructionError> {
        let mut seen: FastHashMap<SimplexVertexKeyBuffer, usize> =
            fast_hash_map_with_capacity(simplices.len());

        for (simplex_index, simplex_spec) in simplices.iter().enumerate() {
            let mut identity = Self::simplex_vertex_keys(simplex_spec, index_to_key);
            identity.as_mut_slice().sort_unstable();
            match seen.entry(identity) {
                Entry::Occupied(entry) => {
                    let mut vertex_indices = simplex_spec.clone();
                    vertex_indices.sort_unstable();
                    return Err(TdsConstructionError::ValidationError {
                        source: TdsError::DuplicateExplicitSimplices {
                            existing_simplex_index: *entry.get(),
                            duplicate_simplex_index: simplex_index,
                            vertex_indices,
                        },
                    });
                }
                Entry::Vacant(entry) => {
                    entry.insert(simplex_index);
                }
            }
        }

        Ok(())
    }

    /// Rejects facet multiplicity above two before neighbor derivation begins.
    fn reject_overshared_facets(
        simplices: &[Vec<usize>],
        index_to_key: &[VertexKey],
    ) -> Result<(), TdsConstructionError> {
        let capacity = simplices.len().saturating_mul(D.saturating_add(1));
        let mut incident_counts: FastHashMap<SimplexVertexKeyBuffer, usize> =
            fast_hash_map_with_capacity(capacity);

        for (simplex_index, simplex_spec) in simplices.iter().enumerate() {
            for facet_index in 0..=D {
                let mut facet_identity: SimplexVertexKeyBuffer = simplex_spec
                    .iter()
                    .enumerate()
                    .filter_map(|(local_index, &input_index)| {
                        (local_index != facet_index).then_some(index_to_key[input_index])
                    })
                    .collect();
                facet_identity.as_mut_slice().sort_unstable();

                match incident_counts.entry(facet_identity) {
                    Entry::Occupied(mut entry) => {
                        let incident_count = *entry.get();
                        if incident_count >= 2 {
                            let mut facet_vertex_indices: Vec<usize> = simplex_spec
                                .iter()
                                .enumerate()
                                .filter_map(|(local_index, &input_index)| {
                                    (local_index != facet_index).then_some(input_index)
                                })
                                .collect();
                            facet_vertex_indices.sort_unstable();
                            return Err(TdsConstructionError::ValidationError {
                                source: TdsError::ExplicitFacetSharingViolation {
                                    facet_key: facet_key_from_vertices(entry.key().as_slice()),
                                    facet_vertex_indices,
                                    existing_incident_count: incident_count,
                                    attempted_incident_count: incident_count + 1,
                                    max_incident_count: 2,
                                    candidate_simplex_index: simplex_index,
                                    candidate_facet_index: facet_index,
                                },
                            });
                        }
                        *entry.get_mut() += 1;
                    }
                    Entry::Vacant(entry) => {
                        entry.insert(1);
                    }
                }
            }
        }

        Ok(())
    }

    /// Maps prevalidated input indices into canonical TDS vertex keys.
    fn simplex_vertex_keys(
        simplex_spec: &[usize],
        index_to_key: &[VertexKey],
    ) -> SimplexVertexKeyBuffer {
        simplex_spec
            .iter()
            .map(|&vertex_index| index_to_key[vertex_index])
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::tds::TriangulationConstructionState;
    use crate::vertex;
    use std::assert_matches;

    /// Supplies a two-simplex disk whose raw orientations require normalization.
    fn two_triangle_fixture() -> ([Vertex<(), 2>; 4], [Vec<usize>; 2]) {
        let vertices = [
            vertex![0.0, 0.0].unwrap(),
            vertex![1.0, 0.0].unwrap(),
            vertex![0.0, 1.0].unwrap(),
            vertex![1.0, 0.2].unwrap(),
        ];
        let simplices = [vec![0, 1, 3], vec![0, 2, 3]];
        (vertices, simplices)
    }

    #[test]
    fn build_publishes_only_a_complete_valid_tds() {
        let (vertices, simplices) = two_triangle_fixture();
        let builder = TdsBuilder::new(&vertices, &simplices).simplex_data_type::<usize>();
        assert_eq!(builder.vertex_count(), 4);
        assert_eq!(builder.simplex_count(), 2);

        let tds = builder.build().unwrap();

        assert_matches!(
            tds.construction_state(),
            TriangulationConstructionState::Constructed
        );
        assert!(tds.validate().is_ok());
        assert_eq!(tds.number_of_vertices(), 4);
        assert_eq!(tds.number_of_simplices(), 2);
        assert!(tds.simplices().all(|(_, simplex)| simplex.data().is_none()));
    }

    #[test]
    fn build_rejects_raw_specs_before_assembly() {
        let vertices = [
            vertex![0.0, 0.0].unwrap(),
            vertex![1.0, 0.0].unwrap(),
            vertex![0.0, 1.0].unwrap(),
        ];

        assert_matches!(
            TdsBuilder::new(&vertices, &[]).build(),
            Err(TdsBuilderError::EmptySimplices)
        );
        assert_matches!(
            TdsBuilder::new(&vertices, &[vec![0, 1]]).build(),
            Err(TdsBuilderError::InvalidSimplexArity {
                simplex_index: 0,
                actual: 2,
                expected: 3,
            })
        );
        assert_matches!(
            TdsBuilder::new(&vertices, &[vec![0, 1, 3]]).build(),
            Err(TdsBuilderError::IndexOutOfBounds {
                simplex_index: 0,
                vertex_index: 3,
                bound: 3,
            })
        );
        assert_matches!(
            TdsBuilder::new(&vertices, &[vec![0, 1, 1]]).build(),
            Err(TdsBuilderError::DuplicateVertexInSimplex {
                simplex_index: 0,
                vertex_index: 1,
            })
        );
    }

    #[test]
    fn build_reports_the_input_index_for_a_duplicate_vertex_uuid() {
        let repeated = vertex![0.0, 0.0].unwrap();
        let repeated_uuid = repeated.uuid();
        let vertices = [repeated, repeated, vertex![0.0, 1.0].unwrap()];
        let simplices = [vec![0, 1, 2]];

        assert_matches!(
            TdsBuilder::new(&vertices, &simplices).build(),
            Err(TdsBuilderError::VertexInsertion {
                vertex_index: 1,
                source,
            }) if matches!(
                source.as_ref(),
                TdsConstructionError::DuplicateUuid {
                    entity: crate::core::tds::EntityKind::Vertex,
                    uuid,
                } if *uuid == repeated_uuid
            )
        );
    }

    #[test]
    fn build_accepts_the_vacuously_valid_empty_complex() {
        let vertices: [Vertex<(), 2>; 0] = [];
        let simplices: [Vec<usize>; 0] = [];

        let tds = TdsBuilder::new(&vertices, &simplices).build().unwrap();
        assert_eq!(tds.number_of_vertices(), 0);
        assert_eq!(tds.number_of_simplices(), 0);
        assert!(tds.validate().is_ok());
    }

    #[test]
    fn parsed_input_flows_into_assembly_without_returning_to_raw_state() {
        let (vertices, simplices) = two_triangle_fixture();
        let parsed = ParsedTdsInput::try_new(&vertices, &simplices).unwrap();

        let tds = TdsBuilder::from_parsed(parsed).build().unwrap();

        assert_eq!(tds.number_of_vertices(), vertices.len());
        assert_eq!(tds.number_of_simplices(), simplices.len());
        assert!(tds.validate().is_ok());
    }
}