Skip to main content

symbios_shape/
ops.rs

1use serde::{Deserialize, Serialize};
2
3use crate::expr::Expr;
4use crate::model::Material;
5use crate::scope::Vec3;
6
7/// A reference to a production rule, optionally carrying call arguments.
8///
9/// Every successor position in the grammar — bare rule ops, split slots,
10/// `Comp` / `Offset` / `Roof` / `Attach` cases, occlusion conditionals — is a
11/// `RuleCall`. Arguments are expressions evaluated in the *calling* shape's
12/// context at push time; the callee binds the resulting values to its
13/// declared parameter names (see `Interpreter::add_rule_def`).
14///
15/// ```text
16/// Spire(4)                       // bare call with one argument
17/// Split(Y) { 3: Base | ~1: Tier(depth + 1) }
18/// ```
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub struct RuleCall {
21    pub name: String,
22    /// Call arguments; empty for plain references. Skipped in serde when
23    /// empty so pre-0.3 serialized ops round-trip unchanged.
24    #[serde(default, skip_serializing_if = "Vec::is_empty")]
25    pub args: Vec<Expr>,
26}
27
28impl RuleCall {
29    /// Plain, argument-less reference.
30    pub fn new(name: impl Into<String>) -> Self {
31        Self {
32            name: name.into(),
33            args: Vec::new(),
34        }
35    }
36
37    /// Reference with call arguments.
38    pub fn with_args(name: impl Into<String>, args: Vec<Expr>) -> Self {
39        Self {
40            name: name.into(),
41            args,
42        }
43    }
44}
45
46impl From<&str> for RuleCall {
47    fn from(name: &str) -> Self {
48        Self::new(name)
49    }
50}
51
52/// Argument-less calls compare equal to their bare name — keeps assertions
53/// and look-ups terse (`assert_eq!(slot.rule, "Floor")`). A call *with*
54/// arguments never equals a bare name.
55impl PartialEq<&str> for RuleCall {
56    fn eq(&self, other: &&str) -> bool {
57        self.args.is_empty() && self.name == *other
58    }
59}
60
61impl PartialEq<str> for RuleCall {
62    fn eq(&self, other: &str) -> bool {
63        self.args.is_empty() && self.name == other
64    }
65}
66
67impl From<String> for RuleCall {
68    fn from(name: String) -> Self {
69        Self::new(name)
70    }
71}
72
73/// Optional snap-binding attached to a `Split` op.
74///
75/// When set, after the slot sizes are resolved the interior boundaries are
76/// snapped to the nearest registered snap-plane along the split axis carrying
77/// the matching `label`, provided the snap-plane lies within `tolerance`
78/// world-space units of the resolved boundary. Slots on either side of a
79/// snapped boundary stretch / shrink to absorb the offset; total scope
80/// length is preserved.
81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
82pub struct SnapBinding {
83    /// Group label of snap-planes to align to (matches `RegSnap("label")`).
84    pub label: String,
85    /// Maximum world-space distance between a slot boundary and a snap-plane
86    /// for the snap to apply. When `None`, defaults to `5%` of the split-axis
87    /// scope length at interpret time.
88    pub tolerance: Option<f64>,
89}
90
91/// The axis along which a `Split` or `Repeat` operation acts.
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
93pub enum Axis {
94    X,
95    Y,
96    Z,
97}
98
99/// Sizing mode for a single slot within a `Split` operation.
100///
101/// Mirrors CityEngine CGA syntax:
102/// - `Absolute(e)`: a fixed world-unit size.
103/// - `Relative(e)`: a fraction of the scope's total dimension (prefix `'` in CGA text).
104/// - `Floating(e)`: a weight that shares the remaining space after absolutes are consumed
105///   (prefix `~` in CGA text). Multiple floating slots divide the remainder proportionally.
106///
107/// Sizes are [`Expr`]s evaluated per shape at derivation time; validation
108/// (finite, positive) happens on the evaluated value in the interpreter.
109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
110pub enum SplitSize {
111    Absolute(Expr),
112    Relative(Expr),
113    Floating(Expr),
114}
115
116impl SplitSize {
117    /// Convenience constructors for literal sizes (tests, programmatic use).
118    pub fn abs(v: f64) -> Self {
119        SplitSize::Absolute(Expr::lit(v))
120    }
121    pub fn rel(v: f64) -> Self {
122        SplitSize::Relative(Expr::lit(v))
123    }
124    pub fn float(v: f64) -> Self {
125        SplitSize::Floating(Expr::lit(v))
126    }
127
128    /// The size expression, whatever the mode.
129    pub fn expr(&self) -> &Expr {
130        match self {
131            SplitSize::Absolute(e) | SplitSize::Relative(e) | SplitSize::Floating(e) => e,
132        }
133    }
134
135    /// Mutable access to the size expression (genetics mutation hook).
136    pub fn expr_mut(&mut self) -> &mut Expr {
137        match self {
138            SplitSize::Absolute(e) | SplitSize::Relative(e) | SplitSize::Floating(e) => e,
139        }
140    }
141}
142
143/// A single slot in a `Split` operation: a size mode paired with a successor rule call.
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub struct SplitSlot {
146    pub size: SplitSize,
147    /// The shape rule invoked on the resulting child scope.
148    pub rule: RuleCall,
149}
150
151/// One entry in a `Split` body: a single slot, or a rhythm group
152/// `{ a | b }*` whose slot pattern repeats to fill the space left by the
153/// entries outside it.
154///
155/// ```text
156/// Split(X) { 1.2: Corner | { 0.5: Pier | ~1: Win }* | 1.2: Corner }
157/// ```
158///
159/// Constraints (enforced at parse / derivation): at most **one** group per
160/// split, no nested groups. Allocation: fixed entries outside the group are
161/// placed first; the group tiles `k` whole copies of its nominal width into
162/// the remainder; leftover space goes to floating slots outside the group,
163/// or — when there are none — the copies stretch uniformly to close the gap
164/// exactly. Sizes inside a copy resolve like a mini-split of the copy width.
165#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub enum SplitEntry {
167    Slot(SplitSlot),
168    Group(Vec<SplitSlot>),
169}
170
171impl SplitEntry {
172    /// The single slot, when this entry is not a group.
173    pub fn as_slot(&self) -> Option<&SplitSlot> {
174        match self {
175            SplitEntry::Slot(s) => Some(s),
176            SplitEntry::Group(_) => None,
177        }
178    }
179}
180
181impl From<SplitSlot> for SplitEntry {
182    fn from(s: SplitSlot) -> Self {
183        SplitEntry::Slot(s)
184    }
185}
186
187/// Face selectors for the `Comp(Faces)` decomposition.
188#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
189pub enum FaceSelector {
190    Top,
191    Bottom,
192    Front,
193    Back,
194    Left,
195    Right,
196    /// Matches all non-top, non-bottom faces (shorthand for the four sides).
197    Side,
198    /// Matches all faces not otherwise mapped.
199    All,
200}
201
202impl FaceSelector {
203    pub fn parse(s: &str) -> Option<Self> {
204        match s {
205            "top" | "Top" => Some(Self::Top),
206            "bottom" | "Bottom" => Some(Self::Bottom),
207            "front" | "Front" => Some(Self::Front),
208            "back" | "Back" => Some(Self::Back),
209            "left" | "Left" => Some(Self::Left),
210            "right" | "Right" => Some(Self::Right),
211            "side" | "Side" => Some(Self::Side),
212            "all" | "All" | "_" => Some(Self::All),
213            _ => None,
214        }
215    }
216}
217
218/// A single mapping in a `Comp(Faces)` block: a face selector → rule name.
219#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
220pub struct CompFaceCase {
221    pub selector: FaceSelector,
222    pub rule: RuleCall,
223}
224
225/// Edge-class selectors for the `Comp(Edges)` decomposition.
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
227pub enum EdgeSelector {
228    /// Vertical edges: a volume's four corner posts; a face's left/right rim.
229    Vertical,
230    /// All horizontal edges (both rings on a volume; top+bottom on a face).
231    Horizontal,
232    /// The top horizontal ring / edge only.
233    Top,
234    /// The bottom horizontal ring / edge only.
235    Bottom,
236    /// Matches all edges not otherwise mapped.
237    All,
238}
239
240impl EdgeSelector {
241    pub fn parse(s: &str) -> Option<Self> {
242        match s {
243            "vertical" | "Vertical" => Some(Self::Vertical),
244            "horizontal" | "Horizontal" => Some(Self::Horizontal),
245            "top" | "Top" => Some(Self::Top),
246            "bottom" | "Bottom" => Some(Self::Bottom),
247            "all" | "All" | "_" => Some(Self::All),
248            _ => None,
249        }
250    }
251}
252
253/// A single mapping in a `Comp(Edges)` block: an edge selector → rule call.
254#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
255pub struct CompEdgeCase {
256    pub selector: EdgeSelector,
257    pub rule: RuleCall,
258}
259
260/// The decomposition target for a `Comp` operation.
261#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
262pub enum CompTarget {
263    /// Decomposes the volume into its six axis-aligned face scopes.
264    Faces(Vec<CompFaceCase>),
265    /// Decomposes into zero-cross-section edge scopes: 12 for a volume,
266    /// 4 for a face. Local X runs along the edge; give the scope thickness
267    /// with `Size(scope.x, t, t)` and centre it with `Translate`.
268    Edges(Vec<CompEdgeCase>),
269}
270
271/// Face selectors for the `Offset` operation.
272#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
273pub enum OffsetSelector {
274    /// The inset/outset region (the area inside the border).
275    Inside,
276    /// The surrounding border strips.
277    Border,
278    /// Matches any selector not otherwise mapped.
279    All,
280}
281
282impl OffsetSelector {
283    pub fn parse(s: &str) -> Option<Self> {
284        match s {
285            "inside" | "Inside" => Some(Self::Inside),
286            "border" | "Border" => Some(Self::Border),
287            "all" | "All" | "_" => Some(Self::All),
288            _ => None,
289        }
290    }
291}
292
293/// A single mapping in an `Offset` block: a selector → rule name.
294#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
295pub struct OffsetCase {
296    pub selector: OffsetSelector,
297    pub rule: RuleCall,
298}
299
300/// Roof shape types for the `Roof` operation.
301#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
302pub enum RoofType {
303    // ── Original types ────────────────────────────────────────────────────────
304    /// Four triangular slope panels meeting at a single apex.
305    Pyramid,
306    /// Single slope panel from front eave to back eave.
307    Shed,
308    /// Two slope panels meeting at a horizontal ridge; two triangular gable ends.
309    Gable,
310    /// Four trapezoidal slope panels meeting at a horizontal ridge.
311    Hip,
312
313    // ── New types ─────────────────────────────────────────────────────────────
314    /// Flat horizontal roof — a single horizontal panel covering the scope top.
315    Flat,
316    /// Two rectangular slope panels only (Gable without the triangular end panels).
317    OpenGable,
318    /// Two slope panels + two rectangular (non-tapered) gable-end wall panels.
319    BoxGable,
320    /// Four panels from a rectangular base meeting at a single apex point (no ridge).
321    /// Equivalent to `Pyramid` for square footprints; left/right end panels are triangular.
322    PyramidHip,
323    /// Two inward-tilting slopes forming a central valley (inverted Gable).
324    Butterfly,
325    /// Four panels forming two parallel ridges with a central valley between them (M profile).
326    MShaped,
327    /// Two pitches per slope: steeper lower zone + shallower upper zone (barn roof).
328    /// Requires `secondary_pitch` in `RoofConfig`.
329    Gambrel,
330    /// Gambrel applied to all four sides: 4 steep lower panels + 4 shallow upper panels.
331    /// Requires `secondary_pitch` in `RoofConfig`.
332    Mansard,
333    /// Asymmetric Gable: the ridge is offset toward one end (`ridge_offset` in `RoofConfig`).
334    /// Front slope is steeper; back slope is shallower. Gable ends are asymmetric triangles.
335    Saltbox,
336    /// Gable with clipped hip ends: the upper corners of each gable end are replaced by
337    /// small triangular hip panels. Controlled by `tier_height` in `RoofConfig`.
338    Jerkinhead,
339    /// Hip roof with a small gable rising from the ridge centre.
340    /// Controlled by `tier_height` (fraction of slope from base where the gable starts).
341    DutchGable,
342}
343
344impl RoofType {
345    pub fn parse(s: &str) -> Option<Self> {
346        match s {
347            "pyramid" | "Pyramid" => Some(Self::Pyramid),
348            "shed" | "Shed" => Some(Self::Shed),
349            "gable" | "Gable" => Some(Self::Gable),
350            "hip" | "Hip" => Some(Self::Hip),
351            "flat" | "Flat" => Some(Self::Flat),
352            "openGable" | "OpenGable" => Some(Self::OpenGable),
353            "boxGable" | "BoxGable" => Some(Self::BoxGable),
354            "pyramidHip" | "PyramidHip" => Some(Self::PyramidHip),
355            "butterfly" | "Butterfly" => Some(Self::Butterfly),
356            "mShaped" | "MShaped" => Some(Self::MShaped),
357            "gambrel" | "Gambrel" => Some(Self::Gambrel),
358            "mansard" | "Mansard" => Some(Self::Mansard),
359            "saltbox" | "Saltbox" => Some(Self::Saltbox),
360            "jerkinhead" | "Jerkinhead" => Some(Self::Jerkinhead),
361            "dutchGable" | "DutchGable" => Some(Self::DutchGable),
362            _ => None,
363        }
364    }
365}
366
367/// Face selectors for the `Roof` operation.
368#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
369pub enum RoofFaceSelector {
370    /// The main sloped panel(s) — front/back in most roof types.
371    Slope,
372    /// The triangular vertical end panels of a Gable or Saltbox roof.
373    GableEnd,
374    /// The steeper, lower zone of a Gambrel or Mansard roof.
375    LowerSlope,
376    /// The shallower, upper zone of a Gambrel or Mansard roof.
377    UpperSlope,
378    /// The small triangular hip panels at the clipped ends of a Jerkinhead roof.
379    HipEnd,
380    /// The inward-facing slopes of a Butterfly or MShaped valley.
381    ValleySlope,
382    /// The outer slopes of an MShaped roof (facing away from the valley).
383    OuterSlope,
384    /// The inner slopes of an MShaped roof (facing toward the valley).
385    InnerSlope,
386    /// The vertical back wall of a `Shed` roof — the raised face under the
387    /// high eave (the glazed "northlight" of a sawtooth factory profile).
388    Back,
389    /// Vertical fascia bands hanging below the eaves.
390    /// Generated when [`RoofConfig::fascia_depth`] is `> 0`. One panel per perimeter
391    /// slope eave; supported for all roof types whose slope panels share a horizontal
392    /// eave at the perimeter (Gable, Hip, Pyramid, Shed, Saltbox, Jerkinhead, DutchGable,
393    /// Gambrel, Mansard, MShaped, BoxGable, OpenGable, PyramidHip). `Flat` and `Butterfly`
394    /// have no perimeter eave at the slope-panel level and produce no fascia panels.
395    Fascia,
396    /// Matches any selector not otherwise mapped.
397    All,
398}
399
400impl RoofFaceSelector {
401    pub fn parse(s: &str) -> Option<Self> {
402        match s {
403            "slope" | "Slope" => Some(Self::Slope),
404            "gable" | "GableEnd" | "gableEnd" => Some(Self::GableEnd),
405            "lowerSlope" | "LowerSlope" => Some(Self::LowerSlope),
406            "upperSlope" | "UpperSlope" => Some(Self::UpperSlope),
407            "hipEnd" | "HipEnd" => Some(Self::HipEnd),
408            "valleySlope" | "ValleySlope" => Some(Self::ValleySlope),
409            "outerSlope" | "OuterSlope" => Some(Self::OuterSlope),
410            "innerSlope" | "InnerSlope" => Some(Self::InnerSlope),
411            "back" | "Back" => Some(Self::Back),
412            "fascia" | "Fascia" => Some(Self::Fascia),
413            "all" | "All" | "_" => Some(Self::All),
414            _ => None,
415        }
416    }
417}
418
419/// A single mapping in a `Roof` block: a face selector → rule name.
420#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
421pub struct RoofCase {
422    pub selector: RoofFaceSelector,
423    pub rule: RuleCall,
424}
425
426/// Rich parametric configuration for the `Roof` operation.
427///
428/// All angular values are in degrees. Lengths are in world units.
429/// Optional fields default as described; see each field doc.
430#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
431pub struct RoofConfig {
432    pub roof_type: RoofType,
433    /// Primary pitch angle in degrees from horizontal. Must be in (0°, 90°).
434    pub pitch: f64,
435    /// Secondary pitch angle in degrees. Used by `Gambrel` (upper zone) and `Mansard`.
436    /// If `None` when required, defaults to `pitch / 2`.
437    pub secondary_pitch: Option<f64>,
438    /// Extra overhang beyond the scope footprint on each side. Default `0.0`.
439    pub overhang: f64,
440    /// Ridge offset for `Saltbox`: fraction [0, 1] of the scope depth (Z) where the
441    /// ridge is positioned from the front. Default `0.5` (symmetric / centred ridge).
442    pub ridge_offset: f64,
443    /// Thickness of the roof fascia edge in world units. Default `0.0` (flat panels).
444    pub fascia_depth: f64,
445    /// Normalised height at which the pitch break occurs for `Gambrel`, `Mansard`,
446    /// `Jerkinhead`, and `DutchGable`. `0.5` means the break is at half the eave-to-ridge
447    /// distance. `None` uses a type-specific default.
448    pub tier_height: Option<f64>,
449}
450
451impl RoofConfig {
452    /// Creates a minimal config for deterministic types (Pyramid, Shed, Gable, Hip, Flat, …).
453    pub fn new(roof_type: RoofType, pitch: f64) -> Self {
454        Self {
455            roof_type,
456            pitch,
457            secondary_pitch: None,
458            overhang: 0.0,
459            ridge_offset: 0.5,
460            fascia_depth: 0.0,
461            tier_height: None,
462        }
463    }
464
465    /// Returns the secondary pitch, defaulting to `pitch / 2` if unset.
466    pub fn secondary_pitch_or_default(&self) -> f64 {
467        self.secondary_pitch.unwrap_or(self.pitch / 2.0)
468    }
469
470    /// Returns the tier height, defaulting to `default` if unset.
471    pub fn tier_height_or(&self, default: f64) -> f64 {
472        self.tier_height.unwrap_or(default)
473    }
474}
475
476/// Expression-valued roof parameters as they appear in the grammar.
477///
478/// The interpreter evaluates every field against the current shape's context
479/// and produces a resolved [`RoofConfig`] for the geometry builder. `pitch`
480/// and `height` are mutually exclusive ways to set the roof's steepness:
481/// when `height` is `Some`, the pitch is derived from it and the scope's
482/// half-span at derivation time (CGA `byHeight` parity), letting mixed-width
483/// wings share one ridge line.
484#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
485pub struct RoofSpec {
486    pub roof_type: RoofType,
487    /// Primary pitch angle in degrees, exclusive range (0°, 90°). Ignored
488    /// when `height` is set.
489    pub pitch: Expr,
490    /// Absolute roof rise in world units (`height=` named arg). Overrides
491    /// `pitch` when present.
492    #[serde(default, skip_serializing_if = "Option::is_none")]
493    pub height: Option<Expr>,
494    /// Secondary pitch in degrees for `Gambrel` / `Mansard`.
495    #[serde(default, skip_serializing_if = "Option::is_none")]
496    pub secondary_pitch: Option<Expr>,
497    /// Eave overhang beyond the footprint, world units. Default `0`.
498    pub overhang: Expr,
499    /// Ridge offset fraction for `Saltbox`. Default `0.5`.
500    pub ridge_offset: Expr,
501    /// Fascia band depth below each perimeter eave. Default `0`.
502    pub fascia_depth: Expr,
503    /// Pitch-break height fraction for tiered types.
504    #[serde(default, skip_serializing_if = "Option::is_none")]
505    pub tier_height: Option<Expr>,
506    /// Forces the ridge to run along the given scope axis (`ridge=X` /
507    /// `ridge=Z`), overriding the default longest-axis heuristic.
508    #[serde(default, skip_serializing_if = "Option::is_none")]
509    pub ridge_axis: Option<Axis>,
510}
511
512/// Wraps every numeric field of a resolved config back into literal
513/// expressions — the programmatic bridge for builders that think in numbers.
514impl From<RoofConfig> for RoofSpec {
515    fn from(c: RoofConfig) -> Self {
516        Self {
517            roof_type: c.roof_type,
518            pitch: Expr::lit(c.pitch),
519            height: None,
520            secondary_pitch: c.secondary_pitch.map(Expr::lit),
521            overhang: Expr::lit(c.overhang),
522            ridge_offset: Expr::lit(c.ridge_offset),
523            fascia_depth: Expr::lit(c.fascia_depth),
524            tier_height: c.tier_height.map(Expr::lit),
525            ridge_axis: None,
526        }
527    }
528}
529
530impl RoofSpec {
531    /// Literal spec with defaults matching `RoofConfig::new` — the
532    /// programmatic construction path for tests and builders.
533    pub fn new(roof_type: RoofType, pitch: f64) -> Self {
534        Self {
535            roof_type,
536            pitch: Expr::lit(pitch),
537            height: None,
538            secondary_pitch: None,
539            overhang: Expr::lit(0.0),
540            ridge_offset: Expr::lit(0.5),
541            fascia_depth: Expr::lit(0.0),
542            tier_height: None,
543            ridge_axis: None,
544        }
545    }
546}
547
548/// Selector for the `Attach` operation.
549#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
550pub enum AttachSelector {
551    /// The projected scope that sits on (or comes out of) the surface.
552    Surface,
553    /// Matches any selector not otherwise mapped.
554    All,
555}
556
557impl AttachSelector {
558    pub fn parse(s: &str) -> Option<Self> {
559        match s {
560            "surface" | "Surface" => Some(Self::Surface),
561            "all" | "All" | "_" => Some(Self::All),
562            _ => None,
563        }
564    }
565}
566
567/// A single mapping in an `Attach` block: a selector → rule name.
568#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
569pub struct AttachCase {
570    pub selector: AttachSelector,
571    pub rule: RuleCall,
572}
573
574/// How one variant of a rule is selected during derivation.
575#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
576pub enum VariantSelector {
577    /// Stochastic: relative weight among the rule's weighted variants
578    /// (`70% ops | 30% ops`). Weights need not sum to 1.
579    Weight(f64),
580    /// Guarded: taken when the expression evaluates non-zero, top-down
581    /// (`when(scope.x < 4): ops`). A guarded rule's variants are evaluated
582    /// in order; the first true guard wins.
583    When(Expr),
584    /// Fallback for a guarded rule (`else: ops`); must be last. In weighted
585    /// rules `else:` is parse-time sugar resolved into a `Weight` of the
586    /// remaining probability mass, so it never reaches the interpreter.
587    Else,
588}
589
590/// One alternative in a rule: how it is selected, and what it does.
591#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
592pub struct RuleVariant {
593    pub selector: VariantSelector,
594    pub ops: Vec<ShapeOp>,
595}
596
597impl RuleVariant {
598    /// Weighted variant — the pre-0.3 shape.
599    pub fn weighted(weight: f64, ops: Vec<ShapeOp>) -> Self {
600        Self {
601            selector: VariantSelector::Weight(weight),
602            ops,
603        }
604    }
605
606    /// The stochastic weight, when this variant is weighted.
607    pub fn weight(&self) -> Option<f64> {
608        match self.selector {
609            VariantSelector::Weight(w) => Some(w),
610            _ => None,
611        }
612    }
613}
614
615/// Region selectors for the `ShapeL` / `ShapeU` footprint-carving ops.
616#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
617pub enum CarveSelector {
618    /// The carved letter shape. `ShapeL` delivers it as TWO rectangular
619    /// scopes (front bar + side leg), `ShapeU` as three — OBB purity means a
620    /// letter footprint is a set of boxes, never one polygon.
621    Shape,
622    /// The rectangular remainder cut away from the letter.
623    Remainder,
624    /// Matches any selector not otherwise mapped.
625    All,
626}
627
628impl CarveSelector {
629    pub fn parse(s: &str) -> Option<Self> {
630        match s {
631            "shape" | "Shape" => Some(Self::Shape),
632            "remainder" | "Remainder" | "rest" | "Rest" => Some(Self::Remainder),
633            "all" | "All" | "_" => Some(Self::All),
634            _ => None,
635        }
636    }
637}
638
639/// A single mapping in a `ShapeL` / `ShapeU` block: selector → rule call.
640#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
641pub struct CarveCase {
642    pub selector: CarveSelector,
643    pub rule: RuleCall,
644}
645
646/// One candidate in a `Fit` op: the minimum extent it needs, and the rule
647/// invoked on the whole scope when it is the first that fits.
648#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
649pub struct FitCandidate {
650    pub min_size: Expr,
651    pub rule: RuleCall,
652}
653
654/// The atomic CGA operations that the interpreter executes.
655///
656/// Every operation transforms the current `Scope` into zero or more child scopes,
657/// each tagged with a rule name that will be recursively evaluated.
658#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
659pub enum ShapeOp {
660    /// Lifts a 2-D footprint (XZ plane) into a 3-D volume by setting the Y size.
661    Extrude(Expr),
662
663    /// Pyramidal taper: scales the top face toward the centroid.
664    /// `amount` ∈ `[0, 1]`: 0 = no taper, 1 = full pyramid (top collapses to a point).
665    Taper(Expr),
666
667    /// Applies an additional rotation to the scope (cumulative with existing rotation).
668    /// Components are `(w, x, y, z)` in grammar order; the evaluated quaternion
669    /// is normalized at derivation time.
670    Rotate([Expr; 4]),
671
672    /// Translates the scope origin in local space.
673    Translate([Expr; 3]),
674
675    /// Scales the scope size along each axis (multiplicative).
676    Scale([Expr; 3]),
677
678    /// Divides the scope along `axis` into ordered slots.
679    ///
680    /// When `snap` is `Some`, interior slot boundaries are snapped to the
681    /// nearest registered snap-plane along `axis` (see [`SnapBinding`]).
682    Split {
683        axis: Axis,
684        entries: Vec<SplitEntry>,
685        snap: Option<SnapBinding>,
686    },
687
688    /// Divides the scope along `axis` by *target areas* instead of lengths.
689    ///
690    /// Slot sizes are read as areas: absolute = square units, relative (`'`)
691    /// = fraction of the face area, floating (`~`) = share of the remaining
692    /// area. Lengths are recovered by dividing through the cross-axis extent,
693    /// so this is only meaningful on the horizontal axes of a footprint-like
694    /// scope; `SplitArea(Y)` is rejected.
695    ///
696    /// Syntax: `SplitArea(X) { 30: Lot | ~1: Rest }`
697    SplitArea { axis: Axis, slots: Vec<SplitSlot> },
698
699    /// Size-fallback choice: invokes the first candidate whose minimum
700    /// extent fits the scope along `axis`; candidates are tried in order and
701    /// the scope vanishes when none fits (use a `0:` catch-all to avoid
702    /// that).
703    ///
704    /// Syntax: `Fit(X) { 2.2: DoorBay | 1.2: WinBay | 0: Wall }`
705    Fit {
706        axis: Axis,
707        candidates: Vec<FitCandidate>,
708    },
709
710    /// Tiles the scope along `axis` with child scopes drawn from `tile_sizes`.
711    ///
712    /// `tile_sizes` is a per-slot pattern that is cycled to fill the axis range.
713    /// Tiles are appended greedily (next tile from the cycle is added while it
714    /// still fits inside the remaining range), then **all** placed tiles are
715    /// scaled by the same factor `total / Σ(placed)` so they fill the scope
716    /// exactly with no gap and no overshoot.
717    ///
718    /// A single-element list `[t]` is the legacy uniform `Repeat(axis, t)`.
719    /// A multi-element list `[a, b, c]` produces an `…, a, b, c, a, b, c, …`
720    /// cadence where each tile's relative width is preserved.
721    Repeat {
722        axis: Axis,
723        tile_sizes: Vec<Expr>,
724        rule: RuleCall,
725    },
726
727    /// Decomposes the scope into its geometric components (faces, edges, vertices).
728    Comp(CompTarget),
729
730    /// Terminal: replace the scope with the named mesh asset.
731    /// This is the "terminal symbol" — produces a `Terminal` node in the output model.
732    I(String),
733
734    /// Sets the material on the current work item.
735    /// The material is propagated to the final `Terminal`, allowing downstream
736    /// renderers to apply textures / shaders and physics consumers to derive
737    /// volumetric mass properties without changing the scope.
738    ///
739    /// Syntax:
740    /// - `Mat("Brick")` / `Mat(Brick)` — id-only material; no density.
741    /// - `Mat("Brick", 1800)` — id + density in kg/m³; the interpreter computes
742    ///   [`crate::model::MassProperties`] for terminals stamped with this material.
743    Mat(Material),
744
745    /// Calls a named sub-rule on the current scope unchanged, optionally
746    /// passing call arguments (`Tier(depth + 1)`).
747    /// Used for grammar rule references that don't transform the scope themselves.
748    Rule(RuleCall),
749
750    /// Sets the scope size to absolute world-unit values (CGA `s()` parity).
751    /// Components must be finite and non-negative; `0` flattens the axis
752    /// (face-scope semantics). Essential for sizing the zero-extent scopes
753    /// produced by `Scatter` and `Comp(Edges)`.
754    ///
755    /// Syntax: `Size(2.1, 0.9, 0.12)` — expressions welcome:
756    /// `Size(scope.x, 0.3, 0.3)`.
757    Size([Expr; 3]),
758
759    /// Re-centres the scope inside the axis-aligned bounds it occupied when
760    /// the current rule was entered, along the masked axes. The scope must
761    /// have been shrunk (e.g. by `Size`) for this to move anything.
762    ///
763    /// Syntax: `Center(X)`, `Center(XY)`, `Center(XYZ)` …
764    Center { x: bool, y: bool, z: bool },
765
766    /// Mirrors the *pending face profile* horizontally (Triangle peak,
767    /// Trapezoid offset, Polygon points). Scope geometry is untouched —
768    /// terminals carry rotations, not reflections, so a scope-level mirror
769    /// cannot exist in this engine. Apply after the profile is set.
770    ///
771    /// Syntax: `Mirror(X)` (only X — profiles are 2-D, mirrored across
772    /// their vertical centre line).
773    Mirror,
774
775    /// Carves an L footprint: a front bar of depth `front` (along local Z
776    /// from the scope origin) plus a side leg of width `side` (along local X)
777    /// over the remaining depth. The `Shape` selector receives both boxes;
778    /// `Remainder` receives the cut-away rectangle.
779    ///
780    /// Syntax: `ShapeL(4, 3) { Shape: Wing | Remainder: Court }`
781    ShapeL {
782        front: Expr,
783        side: Expr,
784        cases: Vec<CarveCase>,
785    },
786
787    /// Carves a U footprint: a front bar plus left and right legs; the
788    /// remainder is the inner court between the legs behind the bar.
789    ///
790    /// Syntax: `ShapeU(4, 3, 3) { Shape: Range | Remainder: Court }`
791    ShapeU {
792        front: Expr,
793        left: Expr,
794        right: Expr,
795        cases: Vec<CarveCase>,
796    },
797
798    /// Rotates the scope so that the specified local axis points in the given world direction.
799    ///
800    /// Applies the shortest-arc rotation from the current world direction of `local_axis`
801    /// to `target`. Useful for recovering from accumulated rotations.
802    /// Syntax: `Align(Y, Up)`, `Align(Z, Forward)`, etc.
803    /// Named targets: `Up`=(0,1,0), `Down`=(0,-1,0), `Right`=(1,0,0), `Left`=(-1,0,0),
804    /// `Forward`=(0,0,-1), `Back`=(0,0,1).
805    Align { local_axis: Axis, target: Vec3 },
806
807    /// Creates an inset (`distance < 0`) frame on a 2D face scope.
808    ///
809    /// Produces up to two kinds of child scopes:
810    /// - `Inside`: the inset rectangle.
811    /// - `Border`: four surrounding strips (bottom, top, left, right), each invoking the same rule.
812    ///
813    /// Syntax: `Offset(-0.2) { Inside: Glass | Border: Frame }`
814    Offset {
815        distance: Expr,
816        cases: Vec<OffsetCase>,
817    },
818
819    /// Generates a roof structure above the current scope using rich parametric configuration.
820    ///
821    /// Operates on a volume scope. The `config` contains the roof type, primary pitch angle,
822    /// optional secondary pitch, overhang, ridge offset, fascia depth, and tier height.
823    ///
824    /// Syntax examples:
825    /// - `Roof(Gable, 30) { Slope: Tiles | GableEnd: Bricks }` — basic Gable
826    /// - `Roof(Hip, 30, 0.5) { Slope: Tiles }` — Hip with overhang
827    /// - `Roof(Gambrel, 45, 20) { LowerSlope: Shingles | UpperSlope: Tiles }` — Gambrel
828    /// - `Roof(Saltbox, 45, offset=0.3) { Slope: Tiles | GableEnd: Bricks }` — Saltbox
829    /// - `Roof(DutchGable, 45, tier=0.7) { Slope: Tiles | GableEnd: Bricks }` — Dutch Gable
830    Roof {
831        spec: RoofSpec,
832        cases: Vec<RoofCase>,
833    },
834
835    /// Registers all six face planes of the current scope as snap-planes
836    /// under the given label. Subsequent `Split(snap="label")` ops can align
837    /// their interior boundaries to these planes. Read-only with respect to
838    /// the scope (the scope itself passes through unchanged).
839    ///
840    /// Syntax: `RegSnap("bays")`
841    RegSnap(String),
842
843    /// Conditionally invokes `rule` on the current scope only when no
844    /// already-emitted terminal occludes the scope (true OBB overlap test).
845    /// Useful for placing decorative elements that should only appear where
846    /// no structural element has already been placed.
847    ///
848    /// The grammar author is responsible for ordering — terminals derived
849    /// before this op participate in the test; later terminals do not.
850    ///
851    /// Syntax: `IfClear { Window }` / `IfClear("chimneys") { Window }` —
852    /// the optional label restricts the test to terminals stamped with that
853    /// `Label`.
854    IfClear {
855        rule: RuleCall,
856        #[serde(default, skip_serializing_if = "Option::is_none")]
857        label: Option<String>,
858    },
859
860    /// Inverse of [`ShapeOp::IfClear`]: invokes `rule` only when the current
861    /// scope **is** occluded by an already-emitted terminal.
862    ///
863    /// Syntax: `IfOccluded { Patch }` / `IfOccluded("roof") { Patch }`.
864    IfOccluded {
865        rule: RuleCall,
866        #[serde(default, skip_serializing_if = "Option::is_none")]
867        label: Option<String>,
868    },
869
870    /// Graded occlusion: invokes `rule` only when the scope is FULLY inside
871    /// a single already-emitted terminal (optionally of one label class).
872    ///
873    /// Syntax: `IfInside { Core }` / `IfInside("mass") { Core }`
874    IfInside {
875        rule: RuleCall,
876        #[serde(default, skip_serializing_if = "Option::is_none")]
877        label: Option<String>,
878    },
879
880    /// Graded occlusion: invokes `rule` only when the scope is in surface
881    /// contact with a terminal — overlapping at a hair's growth but not at a
882    /// hair's shrinkage (optionally restricted to one label class).
883    ///
884    /// Syntax: `IfTouches { Trim }` / `IfTouches("walls") { Trim }`
885    IfTouches {
886        rule: RuleCall,
887        #[serde(default, skip_serializing_if = "Option::is_none")]
888        label: Option<String>,
889    },
890
891    /// Coordination key: a weighted choice resolved once per derivation —
892    /// every `Pick` with the same key picks the SAME index, wherever it
893    /// appears in the tree. The poor man's CGA++ event: all floors agree on
894    /// one window variant, front and back facades match.
895    ///
896    /// The choice is a pure function of `(interpreter seed, key)` — no
897    /// derivation-order dependence.
898    ///
899    /// Syntax: `Pick("winStyle") { 60% WinA | 40% WinB }`
900    Pick {
901        key: String,
902        /// `(weight, successor)` pairs; weights need not sum to 1.
903        choices: Vec<(f64, RuleCall)>,
904    },
905
906    /// Stamps an occlusion label on subsequent terminals of this branch
907    /// (propagates like `Mat`). Labelled terminals form a named class the
908    /// occlusion conditionals can filter on.
909    ///
910    /// Syntax: `Label("chimneys")`
911    Label(String),
912
913    /// Scatters `count` zero-size point scopes uniformly over the scope's
914    /// top face (`Top`) or through its volume (`Volume`), invoking `rule` on
915    /// each. Points are drawn from the shape's RNG stream (seed-stable);
916    /// give them extent with `Size(..)`. `count` is capped at 1024.
917    ///
918    /// Syntax: `Scatter(Top, 12) { Bush }`
919    Scatter {
920        volume: bool,
921        count: Expr,
922        rule: RuleCall,
923    },
924
925    /// Stamps an explicit polygonal `FaceProfile` on the next terminal in this rule.
926    ///
927    /// Mirrors how [`ShapeOp::Taper`] sets a profile override: the next `I(...)`
928    /// (or implicit terminal) emits a `Terminal` whose `face_profile` is
929    /// [`crate::model::FaceProfile::Polygon`] with the provided vertex list.
930    /// Vertices are 2-D points in **normalized `[0, 1]²` scope coordinates**
931    /// (X: 0 = left edge → 1 = right edge; Y: 0 = bottom → 1 = top of the
932    /// face); the renderer triangulates and stretches them across the
933    /// scope's extent. They are NOT world units.
934    ///
935    /// Syntax: `Polygon((0,0), (4,0), (4,2), (2,2), (2,4), (0,4))`
936    /// (variadic `(x,y)` list, capped at 256 vertices for parser DoS hardening).
937    Polygon(Vec<glam::DVec2>),
938
939    /// Projects a new horizontal scope out of a sloped face for attaching dormers or details.
940    ///
941    /// `world_axis` defines the "up" direction for the attached scope (usually world Y).
942    /// The resulting scope sits on the face's surface with its Y axis aligned to `world_axis`,
943    /// inheriting the face's width and height but with depth = 0.
944    ///
945    /// Syntax: `Attach(Up) { Surface: DormerMass }`
946    Attach {
947        world_axis: Vec3,
948        cases: Vec<AttachCase>,
949    },
950}