cadmpeg-ir 0.6.5

CAD data model, validation, diffing, and codec API for cadmpeg.
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// SPDX-License-Identifier: Apache-2.0
//! Pattern operands, admitted transforms, and stage composition.

use super::{
    BodySelection, FaceSelection, FeatureDirection3, FeatureId, FinitePoint3, PathRef,
    SelectionMembers,
};
use crate::ids::OccurrenceId;
use crate::scalar::{Angle, Length, PositiveAngle, PositiveLength, PositiveReal};
use crate::units::UnitVector3;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// One geometric selection repeated or reflected by a pattern operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(
    tag = "kind",
    content = "value",
    rename_all = "snake_case",
    deny_unknown_fields
)]
pub enum PatternSeed {
    /// Complete result of a preceding construction-history feature.
    Feature(FeatureId),
    /// Selected faces, including faces in an intermediate regenerated result.
    Faces(FaceSelection),
    /// Selected bodies, including bodies in an intermediate regenerated result.
    Bodies(BodySelection),
    /// Selected placed component occurrences.
    Occurrences(
        #[serde(deserialize_with = "deserialize_local_occurrences")] SelectionMembers<OccurrenceId>,
    ),
}

/// The stages of a composite pattern nested inside a composite stage.
///
/// A composite stage applies one transform, never another sequence of stages,
/// so this type has no value and [`PatternTransform::Composite`] is
/// unreachable for a stage transform.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub enum NoNestedComposite {}

/// A pattern transform a composite stage may apply.
pub type StagePatternKind = PatternKind<NoNestedComposite>;

/// What a composite pattern arm applies.
///
/// A top-level pattern applies a [`CompositePattern`]; a composite stage
/// applies nothing, so its implementation is over an uninhabited type and its
/// methods are unreachable.
pub trait CompositeStages: Sized {
    /// The stages, in application order.
    fn stages(&self) -> &[PatternStage];

    /// Build the same kind of arm from stages.
    ///
    /// # Errors
    ///
    /// Returns the admission message when the stages do not compose.
    fn rebuild(stages: Vec<PatternStage>) -> Result<Self, &'static str>;

    /// Scale the length-bearing fields of every stage while carrying the
    /// admitted stage count, order, and operand relationships.
    fn try_map_stage_lengths<E>(
        &self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>>;

    /// Edit owned stage lengths without copying stage operands or boxes.
    fn try_map_stage_lengths_owned<E>(
        self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>>;
}

impl CompositeStages for CompositePattern {
    fn stages(&self) -> &[PatternStage] {
        &self.0
    }

    fn rebuild(stages: Vec<PatternStage>) -> Result<Self, &'static str> {
        Self::new(stages)
    }

    fn try_map_stage_lengths<E>(
        &self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        self.clone().try_map_stage_lengths_owned(edit)
    }

    fn try_map_stage_lengths_owned<E>(
        mut self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        for stage in &mut self.0 {
            let pattern = std::mem::replace(stage.pattern.as_mut(), StagePatternKind::UNRESOLVED);
            *stage.pattern = pattern.try_map_lengths_owned(edit)?;
        }
        Ok(self)
    }
}

impl CompositeStages for NoNestedComposite {
    fn stages(&self) -> &[PatternStage] {
        match *self {}
    }

    fn rebuild(_stages: Vec<PatternStage>) -> Result<Self, &'static str> {
        Err("a composite stage applies no nested sequence of stages")
    }

    fn try_map_stage_lengths<E>(
        &self,
        _edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        match *self {}
    }
    fn try_map_stage_lengths_owned<E>(
        self,
        _edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        match self {}
    }
}

/// An admitted pattern with valid geometry, repetition counts, and stage composition.
///
/// `C` names the stages a composite arm applies.
#[derive(Debug, Clone, PartialEq)]
pub struct PatternKind<C = CompositePattern>(PatternTransform<C>);

/// A pattern field whose value changes under a length-unit conversion.
pub enum PatternLengthField<'a> {
    /// A finite distance, including a cumulative linear offset.
    Length(&'a mut Length),
    /// A positive distance between instances.
    PositiveLength(&'a mut PositiveLength),
    /// A point in model coordinates.
    Point(&'a mut FinitePoint3),
}

/// A failed length edit or a collapsed ordered offset sequence.
#[derive(Debug, Clone, PartialEq)]
pub enum PatternLengthEditError<E> {
    /// The field conversion failed.
    Field(E),
    /// The converted offsets are no longer strictly increasing.
    Offsets(&'static str),
}

impl<C: CompositeStages + Clone> PatternKind<C> {
    /// Edit length-bearing geometry while retaining admitted unscaled operands.
    ///
    /// # Errors
    ///
    /// Returns a field error or the offset-order refusal when rounding merges offsets.
    pub fn try_map_lengths<E>(
        &self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        self.clone().try_map_lengths_owned(edit)
    }
}

impl<C: CompositeStages> PatternKind<C> {
    /// Edit an owned pattern without copying retained operands or offset rows.
    pub fn try_map_lengths_owned<E>(
        self,
        edit: &mut impl FnMut(PatternLengthField<'_>) -> Result<(), E>,
    ) -> Result<Self, PatternLengthEditError<E>> {
        let mut transform = self.0;
        match &mut transform {
            PatternTransform::Linear {
                spacing, second, ..
            } => {
                edit(PatternLengthField::PositiveLength(spacing))
                    .map_err(PatternLengthEditError::Field)?;
                if let Some(second) = second {
                    edit(PatternLengthField::PositiveLength(&mut second.spacing))
                        .map_err(PatternLengthEditError::Field)?;
                }
            }
            PatternTransform::LinearOffsets { offsets, .. } => {
                for offset in offsets.iter_mut() {
                    edit(PatternLengthField::Length(offset))
                        .map_err(PatternLengthEditError::Field)?;
                }
                if !valid_increasing_locations(offsets.iter().map(|offset| offset.get())) {
                    return Err(PatternLengthEditError::Offsets(
                        "pattern offsets must start at zero and strictly increase",
                    ));
                }
            }
            PatternTransform::CurveDriven { spacing, .. } => {
                edit(PatternLengthField::PositiveLength(spacing))
                    .map_err(PatternLengthEditError::Field)?;
            }
            PatternTransform::Circular { axis_origin, .. }
            | PatternTransform::CircularAngles { axis_origin, .. } => {
                edit(PatternLengthField::Point(axis_origin))
                    .map_err(PatternLengthEditError::Field)?;
            }
            PatternTransform::Mirror { plane_origin, .. } => {
                edit(PatternLengthField::Point(plane_origin))
                    .map_err(PatternLengthEditError::Field)?;
            }
            PatternTransform::Composite { .. } => {
                return match transform {
                    PatternTransform::Composite { stages } => {
                        Ok(Self(PatternTransform::Composite {
                            stages: stages.try_map_stage_lengths_owned(edit)?,
                        }))
                    }
                    other => Ok(Self(other)),
                };
            }
            PatternTransform::Scale { center, .. } => {
                if let PatternScaleCenter::Point(point) = center {
                    edit(PatternLengthField::Point(point))
                        .map_err(PatternLengthEditError::Field)?;
                }
            }
            PatternTransform::Unresolved { .. } | PatternTransform::MirrorReference { .. } => {}
        }
        Ok(Self(transform))
    }
}

impl<C> PatternKind<C> {
    /// An unresolved pattern with no identified form.
    pub const UNRESOLVED: Self = Self(PatternTransform::Unresolved { form: None });

    /// An unresolved linear pattern.
    pub const UNRESOLVED_LINEAR: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::Linear),
    });

    /// An unresolved circular pattern.
    pub const UNRESOLVED_CIRCULAR: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::Circular),
    });

    /// An unresolved curve-driven pattern.
    pub const UNRESOLVED_CURVE_DRIVEN: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::CurveDriven),
    });

    /// An unresolved mirror pattern.
    pub const UNRESOLVED_MIRROR: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::Mirror),
    });

    /// An unresolved scale pattern.
    pub const UNRESOLVED_SCALE: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::Scale),
    });

    /// An unresolved composite pattern.
    pub const UNRESOLVED_COMPOSITE: Self = Self(PatternTransform::Unresolved {
        form: Some(PatternForm::Composite),
    });

    /// Admits pattern geometry, repetition counts, and ordered stage composition.
    pub fn new(transform: PatternTransform<C>) -> Result<Self, &'static str> {
        let require =
            |condition: bool, message: &'static str| condition.then_some(()).ok_or(message);
        match &transform {
            PatternTransform::Unresolved { .. } => {}
            PatternTransform::Linear { count, second, .. } => {
                require(*count > 0, "pattern count must be positive")?;
                if let Some(second) = second {
                    require(second.count > 0, "pattern second.count must be positive")?;
                }
            }
            PatternTransform::LinearOffsets { offsets, .. } => {
                require(
                    valid_increasing_locations(offsets.iter().map(|offset| offset.get())),
                    "pattern offsets must start at zero and strictly increase",
                )?;
            }
            PatternTransform::Circular { count, .. } => {
                require(*count > 0, "pattern count must be positive")?;
            }
            PatternTransform::CircularAngles { angles, .. } => {
                require(
                    valid_increasing_locations(angles.iter().map(|angle| angle.get())),
                    "pattern angles must start at zero and strictly increase",
                )?;
            }
            PatternTransform::CurveDriven { count, .. } => {
                require(*count > 0, "pattern count must be positive")?;
            }
            PatternTransform::Mirror { .. } => {}
            PatternTransform::MirrorReference {
                plane: FaceSelection::Native(reference),
            } => {
                require(
                    !reference.is_empty(),
                    "pattern plane native reference must be nonempty",
                )?;
            }
            PatternTransform::MirrorReference { .. } => {}
            PatternTransform::Scale { count, .. } => {
                require(*count >= 2, "scale pattern count must be at least two")?;
            }
            PatternTransform::Composite { .. } => {}
        }
        Ok(Self(transform))
    }

    /// Returns the admitted transform definition.
    pub fn definition(&self) -> &PatternTransform<C> {
        &self.0
    }

    /// Returns the curve path slot without exposing repetition geometry.
    pub fn curve_path_mut(&mut self) -> Option<&mut Option<PathRef>> {
        match &mut self.0 {
            PatternTransform::CurveDriven { path, .. } => Some(path),
            _ => None,
        }
    }

    /// Returns whether the pattern form lacks its required operands.
    pub const fn is_unresolved(&self) -> bool {
        matches!(self.0, PatternTransform::Unresolved { .. })
    }
}

impl PatternTransform<NoNestedComposite> {
    /// The same transform under an arm that may apply a sequence of stages.
    #[must_use]
    pub fn widen<C>(self) -> PatternTransform<C> {
        match self {
            Self::Unresolved { form } => PatternTransform::Unresolved { form },
            Self::Linear {
                direction,
                spacing,
                count,
                second,
            } => PatternTransform::Linear {
                direction,
                spacing,
                count,
                second,
            },
            Self::LinearOffsets { direction, offsets } => {
                PatternTransform::LinearOffsets { direction, offsets }
            }
            Self::Circular {
                axis_origin,
                axis_dir,
                angle,
                count,
            } => PatternTransform::Circular {
                axis_origin,
                axis_dir,
                angle,
                count,
            },
            Self::CircularAngles {
                axis_origin,
                axis_dir,
                angles,
            } => PatternTransform::CircularAngles {
                axis_origin,
                axis_dir,
                angles,
            },
            Self::CurveDriven {
                path,
                spacing,
                count,
            } => PatternTransform::CurveDriven {
                path,
                spacing,
                count,
            },
            Self::Mirror {
                plane_origin,
                plane_normal,
            } => PatternTransform::Mirror {
                plane_origin,
                plane_normal,
            },
            Self::MirrorReference { plane } => PatternTransform::MirrorReference { plane },
            Self::Scale {
                center,
                final_factor,
                count,
            } => PatternTransform::Scale {
                center,
                final_factor,
                count,
            },
            Self::Composite { stages } => match stages {},
        }
    }
}

impl StagePatternKind {
    /// The same admitted pattern under an arm that may apply a sequence.
    #[must_use]
    pub fn widen<C>(self) -> PatternKind<C> {
        PatternKind(self.0.widen())
    }
}

/// Identified pattern form for a pattern that is not yet resolved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum PatternForm {
    /// Repeats seeds along a straight direction.
    Linear,
    /// Repeats seeds around an axis.
    Circular,
    /// Repeats seeds along a curve.
    CurveDriven,
    /// Reflects seeds across a plane.
    Mirror,
    /// Repeats seeds using progressive uniform scales.
    Scale,
    /// Applies an ordered sequence of pattern stages.
    Composite,
}

/// Spatial transform used to repeat or reflect seed features.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum PatternTransform<C = CompositePattern> {
    /// Pattern construction that is not resolved; `form` names it when the
    /// source identified one.
    Unresolved {
        /// Identified pattern form, when the source established one.
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_form"
        )]
        form: Option<PatternForm>,
    },
    /// Repeats seeds evenly along a straight direction.
    Linear {
        /// Repetition direction, when resolved.
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_direction"
        )]
        direction: Option<FeatureDirection3>,
        /// Distance between consecutive instances.
        spacing: PositiveLength,
        /// Total number of instances, including the original.
        count: u32,
        /// Optional complete second translation direction.
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_second"
        )]
        second: Option<LinearPatternDirection>,
    },
    /// Repeats seeds at explicitly located distances along a straight direction.
    LinearOffsets {
        /// Repetition direction, when resolved.
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_direction"
        )]
        direction: Option<FeatureDirection3>,
        /// Cumulative distances from the original instance, beginning with zero.
        offsets: Vec<Length>,
    },
    /// Repeats seeds evenly around an axis.
    Circular {
        /// A point on the pattern axis.
        axis_origin: FinitePoint3,
        /// Direction of the pattern axis with a finite nonzero norm.
        axis_dir: FeatureDirection3,
        /// Angular span covered by the pattern.
        angle: PositiveAngle,
        /// Total number of instances, including the original.
        count: u32,
    },
    /// Repeats seeds at explicitly located angles around an axis.
    CircularAngles {
        /// A point on the pattern axis.
        axis_origin: FinitePoint3,
        /// Unit direction of the pattern axis.
        axis_dir: UnitVector3,
        /// Cumulative angles from the original instance, beginning with zero.
        angles: Vec<Angle>,
    },
    /// Repeats seeds at fixed arc-length spacing along a curve.
    CurveDriven {
        /// Pattern path, when its native reference is available.
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_path"
        )]
        path: Option<PathRef>,
        /// Arc-length spacing between consecutive instances.
        spacing: PositiveLength,
        /// Total number of instances, including the original.
        count: u32,
    },
    /// Reflects seeds across a plane.
    Mirror {
        /// A point on the mirror plane.
        plane_origin: FinitePoint3,
        /// Normal of the mirror plane with a finite nonzero norm.
        plane_normal: FeatureDirection3,
    },
    /// Reflects seeds across a source-native plane selection whose frame is not resolved.
    MirrorReference {
        /// Plane or planar face that defines the reflection.
        plane: FaceSelection,
    },
    /// Repeats seeds using progressive uniform scales.
    Scale {
        /// Fixed locus used by every scale transform.
        center: PatternScaleCenter,
        /// Scale factor of the final instance relative to the original.
        final_factor: PositiveReal,
        /// Total number of instances, including the original.
        count: u32,
    },
    /// Applies an ordered sequence of pattern stages.
    Composite {
        /// Stages in application order.
        stages: C,
    },
}

/// Ordered composite-pattern stages whose combination rules and occurrence
/// counts compose.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(try_from = "Vec<PatternStage>", into = "Vec<PatternStage>")]
pub struct CompositePattern(Vec<PatternStage>);

impl CompositePattern {
    /// Admits ordered stages whose occurrence counts compose.
    pub fn new(stages: Vec<PatternStage>) -> Result<Self, &'static str> {
        if stages.is_empty() {
            return Err("pattern stages must be nonempty");
        }
        if !composite_composition_is_valid(&stages) {
            return Err("pattern stage counts must not overflow and must divide aligned slices");
        }
        Ok(Self(stages))
    }
}

/// The rule that combines the stage at `index` with the stages before it.
fn stage_combination(index: usize, stage: &PatternStage) -> PatternStageCombination {
    if index == 0 {
        PatternStageCombination::Initialize
    } else if matches!(stage.pattern.definition(), PatternTransform::Scale { .. }) {
        PatternStageCombination::AlignedSlices
    } else {
        PatternStageCombination::CartesianProduct
    }
}

impl std::ops::Deref for CompositePattern {
    type Target = [PatternStage];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TryFrom<Vec<PatternStage>> for CompositePattern {
    type Error = &'static str;

    fn try_from(stages: Vec<PatternStage>) -> Result<Self, Self::Error> {
        Self::new(stages)
    }
}

impl From<CompositePattern> for Vec<PatternStage> {
    fn from(pattern: CompositePattern) -> Self {
        pattern.0
    }
}

impl<'a> IntoIterator for &'a CompositePattern {
    type Item = &'a PatternStage;
    type IntoIter = std::slice::Iter<'a, PatternStage>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<C: Serialize> Serialize for PatternKind<C> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de, C: Deserialize<'de>> Deserialize<'de> for PatternKind<C> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Self::new(PatternTransform::<C>::deserialize(deserializer)?)
            .map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "schema")]
impl<C: JsonSchema> JsonSchema for PatternKind<C> {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "PatternKind".into()
    }

    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        PatternTransform::<C>::json_schema(generator)
    }
}

fn composite_composition_is_valid(stages: &[crate::features::patterns::PatternStage]) -> bool {
    let mut occurrences = None;
    stages.iter().enumerate().all(|(index, stage)| {
        let Some(stage_count) = pattern_occurrence_count(stage.pattern.definition()) else {
            return true;
        };
        if index == 0 {
            occurrences = Some(stage_count);
            return true;
        }
        match stage_combination(index, stage) {
            PatternStageCombination::CartesianProduct => {
                if let Some(count) = occurrences {
                    occurrences = count.checked_mul(stage_count);
                    occurrences.is_some()
                } else {
                    true
                }
            }
            PatternStageCombination::AlignedSlices => {
                occurrences.is_none_or(|count| count % stage_count == 0)
            }
            PatternStageCombination::Initialize => false,
        }
    })
}

fn pattern_occurrence_count<C>(pattern: &PatternTransform<C>) -> Option<usize> {
    match pattern {
        PatternTransform::Linear { count, .. }
        | PatternTransform::Circular { count, .. }
        | PatternTransform::CurveDriven { count, .. }
        | PatternTransform::Scale { count, .. } => usize::try_from(*count).ok(),
        PatternTransform::LinearOffsets { offsets, .. } => Some(offsets.len()),
        PatternTransform::CircularAngles { angles, .. } => Some(angles.len()),
        PatternTransform::Mirror { .. } | PatternTransform::MirrorReference { .. } => Some(2),
        PatternTransform::Unresolved { .. } | PatternTransform::Composite { .. } => None,
    }
}

fn valid_increasing_locations(locations: impl Iterator<Item = f64>) -> bool {
    let mut locations = locations;
    let Some(first) = locations.next() else {
        return false;
    };
    first == 0.0
        && locations
            .try_fold(first, |previous, location| {
                (location.is_finite() && location > previous).then_some(location)
            })
            .is_some()
}

/// Fixed locus for a progressive pattern scale.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum PatternScaleCenter {
    /// Volume centroid of the first seed feature.
    FirstSeedCentroid,
    /// Explicit model-space point.
    Point(FinitePoint3),
    /// Format-native center reference.
    Native(String),
}

/// One stage of an ordered composite pattern.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PatternStage {
    /// Pattern transform contributed by this stage; never a nested sequence.
    pub pattern: Box<StagePatternKind>,
}

/// Combination rule for a composite-pattern stage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum PatternStageCombination {
    /// Establishes the initial transform sequence.
    Initialize,
    /// Applies each new transform to every preceding transform.
    CartesianProduct,
    /// Aligns transforms with equally sized slices of preceding occurrences.
    AlignedSlices,
}

/// Complete secondary direction of a two-direction linear pattern.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct LinearPatternDirection {
    /// Translation direction with a finite nonzero norm.
    pub direction: FeatureDirection3,
    /// Distance between consecutive instances.
    pub spacing: PositiveLength,
    /// Total number of instances, including the original.
    pub count: u32,
}

// Each optional key below names itself in whatever it refuses.
cadmpeg_core::named_optional_field!(deserialize_form, PatternForm, "form");
cadmpeg_core::named_optional_field!(deserialize_direction, FeatureDirection3, "direction");
cadmpeg_core::named_optional_field!(deserialize_second, LinearPatternDirection, "second");
cadmpeg_core::named_optional_field!(deserialize_path, PathRef, "path");

selection_field_deserializer!(deserialize_local_occurrences, "occurrences");

mod decode_clone;

#[cfg(test)]
mod tests;

mod identity_rewrite;

mod serialization;