euv_core/game/engine3d/modeling/foliage/
impl.rs1use super::*;
2
3impl LSystemGrammar {
5 #[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 #[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
114impl Default for ProceduralTreeConfig {
116 fn default() -> Self {
122 Self::oak()
123 }
124}
125
126impl ProceduralTreeConfig {
128 #[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 #[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 #[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 #[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}