-
Notifications
You must be signed in to change notification settings - Fork 8
/
wfc.py
63 lines (46 loc) · 1.52 KB
/
wfc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
from grid import Grid
from pattern import Pattern
from propagator import Propagator
"""
Implementation of WaveFunctionCollapse
Following the "WaveFunctionCollapse is Constraint Solving in the Wild" terminology
"""
class WaveFunctionCollapse:
"""
WaveFunctionCollapse encapsulates the wfc algorithm
"""
def __init__(self, grid_size, sample, pattern_size):
self.patterns = Pattern.from_sample(sample, pattern_size)
self.grid = self._create_grid(grid_size)
self.propagator = Propagator(self.patterns)
def run(self):
start_time = time.time()
done = False
while not done:
done = self.step()
print("WFC run took %s seconds" % (time.time() - start_time))
def step(self):
self.grid.print_allowed_pattern_count()
cell = self.observe()
if cell is None:
return True
self.propagate(cell)
return False
def get_image(self):
return self.grid.get_image()
def get_patterns(self):
return [pattern.to_image() for pattern in self.patterns]
def observe(self):
if self.grid.check_contradiction():
return None
cell = self.grid.find_lowest_entropy()
if cell is None:
return None
cell.choose_rnd_pattern()
return cell
def propagate(self, cell):
self.propagator.propagate(cell)
def _create_grid(self, grid_size):
num_pattern = len(self.patterns)
return Grid(grid_size, num_pattern)