pub struct Wave<T: Clone, const N: usize> {
pub callback: Option<Box<dyn Fn(&Wave<T, N>, usize)>>,
pub pallet: Vec<Tile<T, N>>,
pub pallet_size: usize,
pub wave: Vec<Vec<Vec<bool>>>,
pub x: usize,
pub y: usize,
pub rng: StdRng,
}Expand description
A Wave function collapse solver. Generic over Pattern size and associated data type.
T: Data type for tiles. N: Size of rules. (MUST BE ODD)
You should use the Wave::new() function to construct this to ensure you get a sane state.
The algorithm starts by assuming a state where every location is a super position of all tiles. (.wave is all trues.)
Then until the wave is fully collapsed (one possibility per location): 0. Find lowest entropy tile, the one with the most information that has not been collapsed. (Least possibility’s).
- Collapse that tile by selecting a single allowed tile, removing other possibility’s.
- Use the rules to narrow down the possibility’s for nearby tiles.
It is theoretic possible for a tile to have not possibility’s, but this is very rare and not handled here.
Fields§
§callback: Option<Box<dyn Fn(&Wave<T, N>, usize)>>A callback called on each step of the .collapse() method, I used this to make an animation of the algoritim.
pallet: Vec<Tile<T, N>>The pallet of tiles avalable, should not be modifyed ater creation.
pallet_size: usizeThe pallet size, if this is not pallet.len(), weirdness will occur.
wave: Vec<Vec<Vec<bool>>>The actual wave function, 2D array of vec![bool; pallet_size] If true, the tile is possible at the location.
x: usizeX and Y dimentions, this needs to match .wave
y: usizeX and Y dimentions, this needs to match .wave
rng: StdRngImplementations§
Source§impl<T: Clone, const N: usize> Wave<T, N>
impl<T: Clone, const N: usize> Wave<T, N>
Sourcepub fn new(pallet: Vec<Tile<T, N>>, x: usize, y: usize, seed: u64) -> Wave<T, N>
pub fn new(pallet: Vec<Tile<T, N>>, x: usize, y: usize, seed: u64) -> Wave<T, N>
Create a solver, taking a tile pallet, size of image to generate and rng seed. Panics if x or y is zeor or the pallet is empty
Sourcepub fn get_lowest_entropy(&self) -> (usize, usize)
pub fn get_lowest_entropy(&self) -> (usize, usize)
Get the lowest entropy tile, excluding fully colapsed tiles and contradictions
Sourcepub fn step(&mut self) -> (usize, usize, usize)
pub fn step(&mut self) -> (usize, usize, usize)
Single step the wave-function-collapse algoritim Returns x, y, and collapsed idx of the tile
Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Checks if the wave function is fully collapsed, returns true on contradiction.
Sourcepub fn is_contradiction(&self) -> bool
pub fn is_contradiction(&self) -> bool
Checks if the function contains a contradiction.
Sourcepub fn collapse(&mut self) -> usize
pub fn collapse(&mut self) -> usize
Fully collapse a wavefunction, may produce a function with contradictions. Returns the count of steps it took to collapse.
Sourcepub fn get_collapsed_tile(&self, x: usize, y: usize) -> Option<usize>
pub fn get_collapsed_tile(&self, x: usize, y: usize) -> Option<usize>
Gets the tileid for a collapsed location in the wavefunction. None if it is not col;apsed.