euv-core 0.56.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Species grammar constructors and deterministic seeded string rewriting for `LSystemGrammar`.
impl LSystemGrammar {
    /// Returns the canonical 3D botanical L-system grammar for `species`.
    ///
    /// # Arguments
    ///
    /// - `TreeSpecies` - Target botanical tree species.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured L-system grammar.
    #[must_use]
    pub fn for_species(species: TreeSpecies) -> Self {
        match species {
            TreeSpecies::Pine => Self::new(
                LSYSTEM_PINE_AXIOM.to_string(),
                vec![
                    LSystemRule::new('A', LSYSTEM_PINE_RULE_A.to_string(), 1.0),
                    LSystemRule::new('B', LSYSTEM_PINE_RULE_B.to_string(), 1.0),
                ],
            ),
            TreeSpecies::Oak => Self::new(
                LSYSTEM_OAK_AXIOM.to_string(),
                vec![
                    LSystemRule::new('A', LSYSTEM_OAK_RULE_A1.to_string(), 0.65),
                    LSystemRule::new('A', LSYSTEM_OAK_RULE_A2.to_string(), 0.35),
                    LSystemRule::new('F', LSYSTEM_OAK_RULE_F.to_string(), 1.0),
                ],
            ),
            TreeSpecies::Palm => Self::new(
                LSYSTEM_PALM_AXIOM.to_string(),
                vec![LSystemRule::new('A', LSYSTEM_PALM_RULE_A.to_string(), 1.0)],
            ),
            TreeSpecies::WeepingWillow => Self::new(
                LSYSTEM_WILLOW_AXIOM.to_string(),
                vec![
                    LSystemRule::new('A', LSYSTEM_WILLOW_RULE_A.to_string(), 1.0),
                    LSystemRule::new('B', LSYSTEM_WILLOW_RULE_B.to_string(), 1.0),
                ],
            ),
        }
    }

    /// Expands the axiom deterministically for `iterations` steps using seeded stochastic rule selection.
    ///
    /// # Arguments
    ///
    /// - `u32` - Number of rewriting passes (clamped to `0..=6`).
    /// - `u64` - Deterministic seed controlling weighted production selection.
    ///
    /// # Returns
    ///
    /// - `String` - Expanded L-system turtle instruction sequence.
    #[must_use]
    pub fn rewrite(&self, iterations: u32, seed: u64) -> String {
        let passes: u32 = iterations.min(TREE_MAX_ITERATIONS);
        let mut current: String = self.get_axiom().clone();
        let rules: &[LSystemRule] = self.get_rules();
        let mut rng: u64 = seed ^ 0x517C_C1B7_2722_0A95;
        for _ in 0..passes {
            let mut next: String = String::with_capacity((current.len() * 2).min(TREE_MAX_SYMBOLS));
            for ch in current.chars() {
                let mut total_w: f32 = 0.0;
                let mut match_count: usize = 0;
                for rule in rules.iter() {
                    if rule.get_predecessor() == ch {
                        total_w += sanitize_scalar(rule.get_weight(), 1.0).max(0.0);
                        match_count += 1;
                    }
                }
                if match_count == 0 {
                    if next.len() < TREE_MAX_SYMBOLS {
                        next.push(ch);
                    }
                    continue;
                }
                let mut chosen: Option<&str> = None;
                if match_count == 1 || total_w <= 1e-6 {
                    for rule in rules.iter() {
                        if rule.get_predecessor() == ch {
                            chosen = Some(rule.get_successor().as_str());
                            break;
                        }
                    }
                } else {
                    let pick: f32 = terrain_next_f32(&mut rng) * total_w;
                    let mut accum: f32 = 0.0;
                    for rule in rules.iter() {
                        if rule.get_predecessor() == ch {
                            accum += sanitize_scalar(rule.get_weight(), 1.0).max(0.0);
                            chosen = Some(rule.get_successor().as_str());
                            if pick <= accum {
                                break;
                            }
                        }
                    }
                }
                if let Some(replacement) = chosen {
                    if next.len() + replacement.len() <= TREE_MAX_SYMBOLS {
                        next.push_str(replacement);
                    } else {
                        break;
                    }
                }
            }
            current = next;
        }
        current
    }
}

/// Default oak configuration for `ProceduralTreeConfig`.
impl Default for ProceduralTreeConfig {
    /// Returns the default oak tree configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Default oak `ProceduralTreeConfig`.
    fn default() -> Self {
        Self::oak()
    }
}

/// Species preset constructors for `ProceduralTreeConfig`.
impl ProceduralTreeConfig {
    /// Creates a conifer pine tree preset.
    ///
    /// # Returns
    ///
    /// - `Self` - Pine configuration.
    #[must_use]
    pub fn pine() -> Self {
        Self {
            species: TreeSpecies::Pine,
            iterations: 4,
            trunk_radius: 0.24,
            segment_length: 1.05,
            branch_angle_deg: 56.0,
            radius_decay: 0.72,
            length_decay: 0.78,
            radial_segments: 6,
            leaf_cluster_size: 0.65,
            gravity_bias: 0.06,
        }
    }

    /// Creates a broad-canopy deciduous oak tree preset.
    ///
    /// # Returns
    ///
    /// - `Self` - Oak configuration.
    #[must_use]
    pub fn oak() -> Self {
        Self {
            species: TreeSpecies::Oak,
            iterations: 3,
            trunk_radius: 0.34,
            segment_length: 0.95,
            branch_angle_deg: 34.0,
            radius_decay: 0.68,
            length_decay: 0.76,
            radial_segments: 7,
            leaf_cluster_size: 0.85,
            gravity_bias: 0.12,
        }
    }

    /// Creates a tropical palm tree preset with curved trunk and crown fronds.
    ///
    /// # Returns
    ///
    /// - `Self` - Palm configuration.
    #[must_use]
    pub fn palm() -> Self {
        Self {
            species: TreeSpecies::Palm,
            iterations: 1,
            trunk_radius: 0.22,
            segment_length: 0.9,
            branch_angle_deg: 48.0,
            radius_decay: 0.86,
            length_decay: 0.92,
            radial_segments: 7,
            leaf_cluster_size: 1.15,
            gravity_bias: -0.14,
        }
    }

    /// Creates a weeping willow preset with hanging foliage curtains.
    ///
    /// # Returns
    ///
    /// - `Self` - Weeping willow configuration.
    #[must_use]
    pub fn weeping_willow() -> Self {
        Self {
            species: TreeSpecies::WeepingWillow,
            iterations: 3,
            trunk_radius: 0.32,
            segment_length: 0.85,
            branch_angle_deg: 38.0,
            radius_decay: 0.7,
            length_decay: 0.8,
            radial_segments: 6,
            leaf_cluster_size: 0.78,
            gravity_bias: -0.32,
        }
    }
}