Skip to content

An Efficient queue implimintation and backtracking to avoid paradoxes #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Wave Function Collapse is a constraint-satisfaction algorithm inspired by quantu
- [Highly optimized version supporting both single-image and pre-tiled source images](https://github.com/pierrebai/Wave-Function-Collapse)
A detailed write-up of how the changes were done and why can be found in its own
[readme here](https://github.com/pierrebai/Wave-Function-Collapse/tree/main/p5js/hybrid-model/README.md)
- An optimised overlaping model using queues (instead of recurcion) and backtracking to avoid paradoxes. [Version @alin256](https://github.com/alin256/Wave-Function-Collapse/tree/efficientQueueBranch). The version also adds some GUI improvements.


## Key Resources
Expand Down
15 changes: 12 additions & 3 deletions p5js/overlapping-model/cell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Saving the log of 2 for shannon entropy calculation
const log2 = Math.log(2);
const SHOW_OPTION_COUNT_IN_CELL = false;

// A Cell is a single element of the grid
class Cell {
Expand All @@ -16,16 +17,15 @@ class Cell {

// Has it been collapsed to a single tile?
this.collapsed = false;
// Has it already been checked during recursion?
this.checked = false;

// Initialize the options with all possible tile indices
for (let i = 0; i < tiles.length; i++) {
this.options.push(i);
}

// This keeps track of what the previous options were
// Saves recalculating entropy if nothing has changed
// Saves time recalculating entropy if nothing has changed
// TODO (Sergey): I think this should not be needed, but let's keep until someone varifies that
this.previousTotalOptions = -1;

// Variable to track if cell needs to be redrawn
Expand Down Expand Up @@ -87,6 +87,15 @@ class Cell {
fill(sumR, sumG, sumB);
noStroke();
square(this.x, this.y, this.w);

if (SHOW_OPTION_COUNT_IN_CELL) {
fill(0);
noStroke();
textSize(this.w / 2);
textAlign(CENTER, CENTER);
text(this.options.length, this.x + this.w / 2, this.y + this.w / 2);
}

}
// No need to redraw until something has changed
this.needsRedraw = false;
Expand Down
39 changes: 39 additions & 0 deletions p5js/overlapping-model/images/list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
3Bricks.png
Angular.png
BrownFox.png
Cat.png
city.png
ColoredCity.png
ct_logo.png
Disk.png
Dungeon.png
example.png
Flowers.png
Font.png
Hogs.png
Knot.png
Lake.png
Link2.png
MagicOffice.png
Mazelike.png
Mountains.png
Office.png
Office2.png
Paths.png
Platformer.png
RedDot.png
RedMaze.png
Rooms.png
Rule126.png
Sand.png
Sewers.png
SimpleKnot.png
SimpleWall.png
Skew2.png
Skyline.png
SmileCity.png
Spirals.png
Town.png
Village.png
WalledDot.png
water.png
Loading