Frequency hints
The the wave function collapose algorithm, after reading up on it a few times, it seems rather straightforward, and a little magical. The algorithm works generally by splitting a sample image into a lot of overlapping tiles from a sample image, and the putting them together with certain rules that makes the final image looks a bit like the original sample image. Usually the output image, or matrix is bigger. In order to keep resemblance, the algorithm also needs relative frequencies of these tiles, equal to the number of times they appear in the sample matrix.
So today I wrote a bit of code that also calculates the relative frequences of each tile and returns the associated frequences as a sequence of the original unique tiles, to be mapped later.
While writing this I realised I have made a slight mistake, so I am going back to solve this. The following test is with interface that was there before I went to correct it!
fn get_freq_hint_single_entity() {
let str_rep = "\
BBBB\
BBBB\
BBBB\
BBBB\
";
let sample = WorldSample::from_str(str_rep, 4);
let frequencies = sample.create_frequency_hints(3);
assert_eq!(frequencies.len(), 1);
assert_eq!(frequencies, vec![16]);
}
After fix
I fixed it. Now the sequence of the frequences are guranteed because that's paired with the tile itself as a tuple. And now the test & interfaces look like this,
fn get_tiles_from_sample_single_entity() {
let str_rep = "\
BBBB\
BBBB\
BBBB\
BBBB\
";
let sample = WorldSample::from_str(str_rep, 4);
let tiles = sample.create_tiles(3);
assert_eq!(tiles.len(), 1);
let freqs = tiles.into_iter().map(|(_, count)| { count }).collect_vec();
assert_eq!(freqs, vec![16]);
}
fn get_tiles_from_sample_16() {
let str_rep = "\
B~BB\
T~B!\
~~^!\
TT~~\
";
let sample = WorldSample::from_str(str_rep, 4);
let tiles = sample.create_tiles(3);
assert_eq!(tiles.len(), 16);
// Since all tiles are unique
// they will have a relative frequency of 1, all of them.
assert_eq!(tiles.iter()
.map(|(_, count)| { count })
.filter(|i| {**i == 1})
.count(),
16);
}