Skip to main content

symbios_shape/
lib.rs

1//! # Symbios Shape
2//!
3//! **A Sovereign Derivation Engine for CGA Shape Grammars.**
4//!
5//! Symbios Shape is a pure-Rust engine for generating procedural geometry using
6//! Computer-Generated Architecture (CGA) Shape Grammars, as popularised by
7//! Esri CityEngine. It is designed for embedding in game engines (Bevy, Godot)
8//! and offline procedural pipelines where reliability and determinism are paramount.
9//!
10//! ## Key Features
11//!
12//! - **Lightweight**: Depends on `glam` (math), `nom` (parsing), `rand` (stochastic rules), `thiserror`, `serde`, and `symbios-genetics` — no engine or runtime required.
13//! - **CGA-Compatible Operations**: `Extrude`, `Split` (with optional `snap=` binding), `Comp`, `Repeat`, `Taper`, `Scale`, `Translate`, `Rotate`, `Align`, `Offset`, `Roof`, `Attach`, `Polygon`, `RegSnap`, `IfClear`, `IfOccluded`, `I`, `Mat`.
14//! - **15 Roof Types**: Pyramid, Shed, Gable, Hip, Flat, OpenGable, BoxGable, PyramidHip, Butterfly, MShaped, Gambrel, Mansard, Saltbox, Jerkinhead, DutchGable.
15//! - **Flexible Split Sizing**: Absolute, relative (`'`), and floating (`~`) modes.
16//! - **Rich Face Profiles**: [`FaceProfile`] describes each terminal's cross-section (Rectangle, Taper, Triangle, Trapezoid, Polygon).
17//! - **Mass Model**: [`Material`] `{ id, density }` + [`MassProperties`] `{ mass, centroid, inertia }` computed on [`Terminal`] for physics / LOD / IK consumers.
18//! - **Snap-Lines + Occlusion Queries**: `RegSnap` records face planes; `Split(snap=...)` aligns to them. `IfClear` / `IfOccluded` gate sub-rules on OBB overlap with already-emitted terminals; the same overlap test is exposed at runtime via [`ShapeModel::query`] → [`TerminalQuery`] and the free function [`obb_overlap`].
19//! - **Genetic Evolution**: [`genetics::ShapeGenotype`] wraps the rule table for `symbios-genetics` algorithms (Gaussian-jitter mutation, BLX-α crossover).
20//! - **Bevy-Ready Output**: [`ShapeModel`] containing [`Terminal`] nodes with scope, mesh_id, face_profile, material, and mass_properties.
21//!
22//! ## Example
23//!
24//! ```rust
25//! use symbios_shape::{Interpreter, Scope, Vec3, Quat};
26//! use symbios_shape::grammar::parse_ops;
27//!
28//! let mut interp = Interpreter::new();
29//!
30//! // A simple 3-storey building
31//! interp.add_rule("Lot", parse_ops("Extrude(12) Split(Y) { 3: Ground | ~1: Upper | 2: Roof }").unwrap());
32//! interp.add_rule("Ground", parse_ops(r#"I("GroundFloor")"#).unwrap());
33//! interp.add_rule("Upper",  parse_ops(r#"I("Floor")"#).unwrap());
34//! interp.add_rule("Roof",   parse_ops(r#"Taper(0.8) I("Roof")"#).unwrap());
35//!
36//! let footprint = Scope::new(Vec3::ZERO, Quat::IDENTITY, Vec3::new(10.0, 0.0, 10.0));
37//! let model = interp.derive(footprint, "Lot").unwrap();
38//!
39//! assert_eq!(model.len(), 3);
40//! assert_eq!(model.terminals[0].mesh_id, "GroundFloor");
41//! assert_eq!(model.terminals[2].mesh_id, "Roof");
42//! assert!(matches!(model.terminals[2].face_profile, symbios_shape::FaceProfile::Taper(t) if (t - 0.8).abs() < 1e-9));
43//! ```
44
45pub mod error;
46pub mod genetics;
47pub mod grammar;
48pub mod interpreter;
49pub mod model;
50pub mod ops;
51pub mod query;
52pub mod scope;
53
54pub use error::ShapeError;
55pub use interpreter::Interpreter;
56pub use model::{FaceProfile, MassProperties, Material, ShapeModel, SnapPlane, Terminal};
57pub use ops::{
58    AttachCase, AttachSelector, Axis, CompTarget, FaceSelector, OffsetCase, OffsetSelector,
59    RoofCase, RoofConfig, RoofFaceSelector, RoofType, ShapeOp, SnapBinding, SplitSize, SplitSlot,
60};
61pub use query::{TerminalQuery, obb_overlap};
62pub use scope::{Quat, Scope, Vec3};