Wave

Struct Wave 

Source
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).

  1. Collapse that tile by selecting a single allowed tile, removing other possibility’s.
  2. 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: usize

The 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: usize

X and Y dimentions, this needs to match .wave

§y: usize

X and Y dimentions, this needs to match .wave

§rng: StdRng

Implementations§

Source§

impl<T: Clone, const N: usize> Wave<T, N>

Source

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

Source

pub fn get_lowest_entropy(&self) -> (usize, usize)

Get the lowest entropy tile, excluding fully colapsed tiles and contradictions

Source

pub fn step(&mut self) -> (usize, usize, usize)

Single step the wave-function-collapse algoritim Returns x, y, and collapsed idx of the tile

Source

pub fn is_done(&self) -> bool

Checks if the wave function is fully collapsed, returns true on contradiction.

Source

pub fn is_contradiction(&self) -> bool

Checks if the function contains a contradiction.

Source

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.

Source

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.

Source

pub fn get_collapsed_vec(&self) -> Option<Vec<Vec<usize>>>

Returns a 2dim vector containing tileids for all collapsed tiles, none if the wave is not colapsed.

Source

pub fn get_collapsed_data(&self) -> Option<Vec<Vec<&T>>>

Returns the assocated data for every tile in the wave, None if it is not fully collapsed.

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Wave<T, N>

§

impl<T, const N: usize> !RefUnwindSafe for Wave<T, N>

§

impl<T, const N: usize> !Send for Wave<T, N>

§

impl<T, const N: usize> !Sync for Wave<T, N>

§

impl<T, const N: usize> Unpin for Wave<T, N>
where T: Unpin,

§

impl<T, const N: usize> !UnwindSafe for Wave<T, N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V