author | unC0Rr |
Sun, 12 Feb 2023 14:19:02 +0100 | |
branch | transitional_engine |
changeset 15918 | 9bd828451d77 |
parent 15917 | 60b5639cc3a5 |
child 15919 | 659c92124c26 |
permissions | -rw-r--r-- |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
1 |
use super::tile_image::{Edge, TileImage}; |
15917 | 2 |
use super::wavefront_collapse::{CollapseRule, Tile, WavefrontCollapse}; |
15913 | 3 |
use crate::{LandGenerationParameters, LandGenerator}; |
15915 | 4 |
use integral_geometry::Size; |
5 |
use png::Decoder; |
|
15917 | 6 |
use std::collections::HashSet; |
15915 | 7 |
use std::fs::File; |
8 |
use std::io::BufReader; |
|
15913 | 9 |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
10 |
pub struct WavefrontCollapseLandGenerator { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
11 |
pub size: Size, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
12 |
} |
15913 | 13 |
|
14 |
impl WavefrontCollapseLandGenerator { |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
15 |
pub fn new(size: &Size) -> Self { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
16 |
Self { size: *size } |
15913 | 17 |
} |
18 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
19 |
pub fn load_template<T: Copy + PartialEq + Default>( |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
20 |
&self, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
21 |
parameters: &LandGenerationParameters<T>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
22 |
) -> Vec<TileImage<T, String>> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
23 |
let mut result = Vec::new(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
24 |
|
15915 | 25 |
let file = File::open("sample.png").expect("file exists"); |
26 |
let decoder = Decoder::new(BufReader::new(file)); |
|
27 |
let mut reader = decoder.read_info().unwrap(); |
|
28 |
||
29 |
let info = reader.info(); |
|
30 |
let mut tiles_image = vec2d::Vec2D::new( |
|
31 |
&Size::new(info.width as usize, info.height as usize), |
|
32 |
parameters.zero, |
|
33 |
); |
|
34 |
||
35 |
let mut buf = vec![0; reader.output_buffer_size()]; |
|
36 |
let info = reader.next_frame(&mut buf).unwrap(); |
|
37 |
let bytes = &buf[..info.buffer_size()]; |
|
15913 | 38 |
|
15915 | 39 |
let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter(); |
40 |
||
41 |
for line in bytes.chunks_exact(info.line_size) { |
|
42 |
for value in line.chunks_exact(info.color_type.samples()) { |
|
43 |
*tiles_image_pixels |
|
44 |
.next() |
|
45 |
.expect("vec2d size matching image dimensions") = |
|
46 |
if value.into_iter().all(|p| *p == 0) { |
|
47 |
parameters.zero |
|
48 |
} else { |
|
49 |
parameters.basic |
|
50 |
}; |
|
51 |
} |
|
52 |
} |
|
53 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
54 |
let top_edge = Edge::new("ef".to_owned(), false); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
55 |
let right_edge = top_edge.reversed(); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
56 |
let bottom_edge = Edge::new("ee".to_owned(), true); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
57 |
let left_edge = bottom_edge.clone(); |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
58 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
59 |
let tile = |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
60 |
TileImage::<T, String>::new(tiles_image, top_edge, right_edge, bottom_edge, left_edge); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
61 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
62 |
result.push(tile.clone()); |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
63 |
result.push(tile.rotated90()); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
64 |
result.push(tile.rotated180()); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
65 |
result.push(tile.rotated270()); |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
66 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
67 |
result |
15913 | 68 |
} |
69 |
} |
|
70 |
||
71 |
impl LandGenerator for WavefrontCollapseLandGenerator { |
|
72 |
fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>( |
|
73 |
&self, |
|
74 |
parameters: &LandGenerationParameters<T>, |
|
75 |
random_numbers: &mut I, |
|
76 |
) -> land2d::Land2D<T> { |
|
15915 | 77 |
let tiles = self.load_template(parameters); |
78 |
||
15917 | 79 |
let mut rules = Vec::<CollapseRule>::new(); |
80 |
||
81 |
let default_connection = HashSet::from_iter(vec![Tile::Outside, Tile::Empty].into_iter()); |
|
82 |
for (i, tile) in tiles.iter().enumerate() { |
|
83 |
let mut right = default_connection.clone(); |
|
84 |
let mut bottom = default_connection.clone(); |
|
85 |
let mut left = default_connection.clone(); |
|
86 |
let mut top = default_connection.clone(); |
|
87 |
||
88 |
for p in 0..i { |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
89 |
if tiles[p].left_edge().is_compatible(tile.right_edge()) { |
15917 | 90 |
rules[p].left.insert(Tile::Numbered(i)); |
91 |
right.insert(Tile::Numbered(p)); |
|
92 |
} |
|
93 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
94 |
if tiles[p].right_edge().is_compatible(tile.left_edge()) { |
15917 | 95 |
rules[p].right.insert(Tile::Numbered(i)); |
96 |
left.insert(Tile::Numbered(p)); |
|
97 |
} |
|
98 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
99 |
if tiles[p].top_edge().is_compatible(tile.bottom_edge()) { |
15917 | 100 |
rules[p].top.insert(Tile::Numbered(i)); |
101 |
bottom.insert(Tile::Numbered(p)); |
|
102 |
} |
|
103 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
104 |
if tiles[p].bottom_edge().is_compatible(tile.top_edge()) { |
15917 | 105 |
rules[p].bottom.insert(Tile::Numbered(i)); |
106 |
top.insert(Tile::Numbered(p)); |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
rules.push(CollapseRule { |
|
111 |
tile: Tile::Numbered(i), |
|
112 |
top, |
|
113 |
right, |
|
114 |
bottom, |
|
115 |
left, |
|
116 |
}); |
|
117 |
} |
|
118 |
||
119 |
let mut wfc = WavefrontCollapse::default(); |
|
120 |
wfc.set_rules(rules); |
|
121 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
122 |
let wfc_size = if let Some(first_tile) = tiles.first() { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
123 |
let tile_size = first_tile.size(); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
124 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
125 |
Size::new( |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
126 |
self.size.width / tile_size.width, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
127 |
self.size.height / tile_size.height, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
128 |
) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
129 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
130 |
Size::new(1, 1) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
131 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
132 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
133 |
wfc.generate_map(&wfc_size, |_| {}, random_numbers); |
15917 | 134 |
|
135 |
let grid = wfc.grid(); |
|
136 |
||
137 |
for r in 0..grid.height() { |
|
138 |
for c in 0..grid.width() { |
|
139 |
print!("{:?}", grid.get(r, c)); |
|
140 |
} |
|
141 |
||
142 |
println!(); |
|
143 |
} |
|
144 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
145 |
let mut result = land2d::Land2D::new(&self.size, parameters.zero); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
146 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
147 |
for row in 0..wfc_size.height { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
148 |
for column in 0..wfc_size.width { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
149 |
if let Some(Tile::Numbered(tile_index)) = wfc.grid().get(row, column) { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
150 |
let tile = &tiles[*tile_index]; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
151 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
152 |
for tile_row in 0..tile.size().height { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
153 |
for tile_column in 0..tile.size().width { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
154 |
result.map( |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
155 |
(row * tile.size().height + tile_row) as i32, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
156 |
(column * tile.size().width + tile_column) as i32, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
157 |
|p| { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
158 |
*p = |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
159 |
*tile.get(tile_row, tile_column).unwrap_or(¶meters.zero) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
160 |
}, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
161 |
); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
162 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
163 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
164 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
165 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
166 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
167 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
168 |
result |
15913 | 169 |
} |
170 |
} |
|
15915 | 171 |
|
172 |
#[cfg(test)] |
|
173 |
mod tests { |
|
174 |
use super::WavefrontCollapseLandGenerator; |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
175 |
use crate::{LandGenerationParameters, LandGenerator}; |
15915 | 176 |
use integral_geometry::Size; |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
177 |
use std::fs::File; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
178 |
use std::io::BufWriter; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
179 |
use std::path::Path; |
15915 | 180 |
use vec2d::Vec2D; |
181 |
||
182 |
#[test] |
|
183 |
fn test_generation() { |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
184 |
let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024)); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
185 |
let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
186 |
let land = wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(0u32)); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
187 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
188 |
let path = Path::new(r"output.png"); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
189 |
let file = File::create(path).unwrap(); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
190 |
let ref mut w = BufWriter::new(file); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
191 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
192 |
let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1. |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
193 |
encoder.set_color(png::ColorType::Rgba); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
194 |
encoder.set_depth(png::BitDepth::Eight); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
195 |
encoder.set_source_gamma(png::ScaledFloat::from_scaled(45455)); // 1.0 / 2.2, scaled by 100000 |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
196 |
encoder.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2)); // 1.0 / 2.2, unscaled, but rounded |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
197 |
let source_chromaticities = png::SourceChromaticities::new( |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
198 |
// Using unscaled instantiation here |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
199 |
(0.31270, 0.32900), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
200 |
(0.64000, 0.33000), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
201 |
(0.30000, 0.60000), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
202 |
(0.15000, 0.06000), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
203 |
); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
204 |
encoder.set_source_chromaticities(source_chromaticities); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
205 |
let mut writer = encoder.write_header().unwrap(); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
206 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
207 |
writer.write_image_data(land.raw_pixel_bytes()).unwrap(); // Save |
15915 | 208 |
} |
209 |
} |