Skip to main content

euv_core/game/engine3d/modeling/foliage/
impl.rs

1use super::*;
2
3/// Species grammar constructors and deterministic seeded string rewriting for `LSystemGrammar`.
4impl LSystemGrammar {
5    /// Returns the canonical 3D botanical L-system grammar for `species`.
6    ///
7    /// # Arguments
8    ///
9    /// - `TreeSpecies` - Target botanical tree species.
10    ///
11    /// # Returns
12    ///
13    /// - `Self` - Configured L-system grammar.
14    #[must_use]
15    pub fn for_species(species: TreeSpecies) -> Self {
16        match species {
17            TreeSpecies::Pine => Self::new(
18                LSYSTEM_PINE_AXIOM.to_string(),
19                vec![
20                    LSystemRule::new('A', LSYSTEM_PINE_RULE_A.to_string(), 1.0),
21                    LSystemRule::new('B', LSYSTEM_PINE_RULE_B.to_string(), 1.0),
22                ],
23            ),
24            TreeSpecies::Oak => Self::new(
25                LSYSTEM_OAK_AXIOM.to_string(),
26                vec![
27                    LSystemRule::new('A', LSYSTEM_OAK_RULE_A1.to_string(), 0.65),
28                    LSystemRule::new('A', LSYSTEM_OAK_RULE_A2.to_string(), 0.35),
29                    LSystemRule::new('F', LSYSTEM_OAK_RULE_F.to_string(), 1.0),
30                ],
31            ),
32            TreeSpecies::Palm => Self::new(
33                LSYSTEM_PALM_AXIOM.to_string(),
34                vec![LSystemRule::new('A', LSYSTEM_PALM_RULE_A.to_string(), 1.0)],
35            ),
36            TreeSpecies::WeepingWillow => Self::new(
37                LSYSTEM_WILLOW_AXIOM.to_string(),
38                vec![
39                    LSystemRule::new('A', LSYSTEM_WILLOW_RULE_A.to_string(), 1.0),
40                    LSystemRule::new('B', LSYSTEM_WILLOW_RULE_B.to_string(), 1.0),
41                ],
42            ),
43        }
44    }
45
46    /// Expands the axiom deterministically for `iterations` steps using seeded stochastic rule selection.
47    ///
48    /// # Arguments
49    ///
50    /// - `u32` - Number of rewriting passes (clamped to `0..=6`).
51    /// - `u64` - Deterministic seed controlling weighted production selection.
52    ///
53    /// # Returns
54    ///
55    /// - `String` - Expanded L-system turtle instruction sequence.
56    #[must_use]
57    pub fn rewrite(&self, iterations: u32, seed: u64) -> String {
58        let passes: u32 = iterations.min(TREE_MAX_ITERATIONS);
59        let mut current: String = self.get_axiom().clone();
60        let rules: &[LSystemRule] = self.get_rules();
61        let mut rng: u64 = seed ^ 0x517C_C1B7_2722_0A95;
62        for _ in 0..passes {
63            let mut next: String = String::with_capacity((current.len() * 2).min(TREE_MAX_SYMBOLS));
64            for ch in current.chars() {
65                let mut total_w: f32 = 0.0;
66                let mut match_count: usize = 0;
67                for rule in rules.iter() {
68                    if rule.get_predecessor() == ch {
69                        total_w += sanitize_scalar(rule.get_weight(), 1.0).max(0.0);
70                        match_count += 1;
71                    }
72                }
73                if match_count == 0 {
74                    if next.len() < TREE_MAX_SYMBOLS {
75                        next.push(ch);
76                    }
77                    continue;
78                }
79                let mut chosen: Option<&str> = None;
80                if match_count == 1 || total_w <= 1e-6 {
81                    for rule in rules.iter() {
82                        if rule.get_predecessor() == ch {
83                            chosen = Some(rule.get_successor().as_str());
84                            break;
85                        }
86                    }
87                } else {
88                    let pick: f32 = terrain_next_f32(&mut rng) * total_w;
89                    let mut accum: f32 = 0.0;
90                    for rule in rules.iter() {
91                        if rule.get_predecessor() == ch {
92                            accum += sanitize_scalar(rule.get_weight(), 1.0).max(0.0);
93                            chosen = Some(rule.get_successor().as_str());
94                            if pick <= accum {
95                                break;
96                            }
97                        }
98                    }
99                }
100                if let Some(replacement) = chosen {
101                    if next.len() + replacement.len() <= TREE_MAX_SYMBOLS {
102                        next.push_str(replacement);
103                    } else {
104                        break;
105                    }
106                }
107            }
108            current = next;
109        }
110        current
111    }
112}
113
114/// Default oak configuration for `ProceduralTreeConfig`.
115impl Default for ProceduralTreeConfig {
116    /// Returns the default oak tree configuration.
117    ///
118    /// # Returns
119    ///
120    /// - `Self` - Default oak `ProceduralTreeConfig`.
121    fn default() -> Self {
122        Self::oak()
123    }
124}
125
126/// Species preset constructors for `ProceduralTreeConfig`.
127impl ProceduralTreeConfig {
128    /// Creates a conifer pine tree preset.
129    ///
130    /// # Returns
131    ///
132    /// - `Self` - Pine configuration.
133    #[must_use]
134    pub fn pine() -> Self {
135        Self {
136            species: TreeSpecies::Pine,
137            iterations: 4,
138            trunk_radius: 0.24,
139            segment_length: 1.05,
140            branch_angle_deg: 56.0,
141            radius_decay: 0.72,
142            length_decay: 0.78,
143            radial_segments: 6,
144            leaf_cluster_size: 0.65,
145            gravity_bias: 0.06,
146        }
147    }
148
149    /// Creates a broad-canopy deciduous oak tree preset.
150    ///
151    /// # Returns
152    ///
153    /// - `Self` - Oak configuration.
154    #[must_use]
155    pub fn oak() -> Self {
156        Self {
157            species: TreeSpecies::Oak,
158            iterations: 3,
159            trunk_radius: 0.34,
160            segment_length: 0.95,
161            branch_angle_deg: 34.0,
162            radius_decay: 0.68,
163            length_decay: 0.76,
164            radial_segments: 7,
165            leaf_cluster_size: 0.85,
166            gravity_bias: 0.12,
167        }
168    }
169
170    /// Creates a tropical palm tree preset with curved trunk and crown fronds.
171    ///
172    /// # Returns
173    ///
174    /// - `Self` - Palm configuration.
175    #[must_use]
176    pub fn palm() -> Self {
177        Self {
178            species: TreeSpecies::Palm,
179            iterations: 1,
180            trunk_radius: 0.22,
181            segment_length: 0.9,
182            branch_angle_deg: 48.0,
183            radius_decay: 0.86,
184            length_decay: 0.92,
185            radial_segments: 7,
186            leaf_cluster_size: 1.15,
187            gravity_bias: -0.14,
188        }
189    }
190
191    /// Creates a weeping willow preset with hanging foliage curtains.
192    ///
193    /// # Returns
194    ///
195    /// - `Self` - Weeping willow configuration.
196    #[must_use]
197    pub fn weeping_willow() -> Self {
198        Self {
199            species: TreeSpecies::WeepingWillow,
200            iterations: 3,
201            trunk_radius: 0.32,
202            segment_length: 0.85,
203            branch_angle_deg: 38.0,
204            radius_decay: 0.7,
205            length_decay: 0.8,
206            radial_segments: 6,
207            leaf_cluster_size: 0.78,
208            gravity_bias: -0.32,
209        }
210    }
211}