-
Notifications
You must be signed in to change notification settings - Fork 15
/
overlap-model.go
461 lines (404 loc) · 11.3 KB
/
overlap-model.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package wfc
import (
// "fmt"
"image"
"image/color"
"math"
)
/**
* OverlappingModel Type
*/
type OverlappingModel struct {
*BaseModel // Underlying model of generic Wave Function Collapse algorithm
N int // Size of patterns (ie pixel distance of influencing pixels)
Colors []color.Color // Array of unique colors in input
Ground int // Id of the specific pattern to use as the bottom of the generation. A value of -1 means that this is unset
Patterns []Pattern // Array of unique patterns in input
Propagator [][][][]int // Table of which patterns (t2) mathch a given pattern (t1) at offset (dx, dy) [t1][dx][dy][t2]
Fmxmn, Fmymn int // Width and height of output, minus n
}
/**
* Pattern Type. Flattened array of color codes.
*/
type Pattern []int
/**
* NewOverlappingModel
* @param {image.Image} img The source image
* @param {int} N Size of the patterns
* @param {int} width The width of the generated image
* @param {int} height The height of the generated image
* @param {bool} periodicInput Whether the source image is to be considered as periodic / as a repeatable texture
* @param {bool} periodicOutput Whether the generation should be periodic / a repeatable texture
* @param {int} symmetry Allowed symmetries from 1 (no symmetry) to 8 (all mirrored / rotated variations)
* @param {int} [ground=0] Id of the specific pattern to use as the bottom of the generation ( see https://github.com/mxgmn/WaveFunctionCollapse/issues/3#issuecomment-250995366 )
* @return *OverlappingModel A pointer to a new copy of the model
*/
func NewOverlappingModel(img image.Image, n, width, height int, periodicInput, periodicOutput bool, symmetry int, ground bool) *OverlappingModel {
// Initialize model
model := &OverlappingModel{BaseModel: &BaseModel{}}
model.N = n
model.Fmx = width
model.Fmy = height
model.Periodic = periodicOutput
model.Ground = -1
bounds := img.Bounds()
dataWidth := bounds.Max.X
dataHeight := bounds.Max.Y
// Build up a palette of colors (by assigning numbers to unique color values)
sample := make([][]int, dataWidth)
for i := range sample {
sample[i] = make([]int, dataHeight)
}
model.Colors = make([]color.Color, 0)
colorMap := make(map[color.Color]int)
for y := 0; y < dataHeight; y++ {
for x := 0; x < dataWidth; x++ {
color := img.At(x, y)
if _, ok := colorMap[color]; !ok {
colorMap[color] = len(model.Colors)
model.Colors = append(model.Colors, color)
}
sample[x][y] = colorMap[color]
}
}
// Extract various patterns from input (patterns are 1D arrays of sample codes)
c := len(model.Colors)
w := int(math.Pow(float64(c), float64(n*n)))
// Given a transforming function, return a flattened array of the N*N pattern
getPattern := func(transformer func(x, y int) int) Pattern {
result := make(Pattern, n*n)
for y := 0; y < n; y++ {
for x := 0; x < n; x++ {
result[x+y*n] = transformer(x, y)
}
}
return result
}
// Return a flattened array of the N*N pattern at (x, y) using sample codes
patternFromSample := func(x, y int) Pattern {
return getPattern(func(dx, dy int) int {
return sample[(x+dx)%dataWidth][(y+dy)%dataHeight]
})
}
rotate := func(p Pattern) Pattern {
return getPattern(func(x, y int) int {
return p[n-1-y+x*n]
})
}
reflect := func(p Pattern) Pattern {
return getPattern(func(x, y int) int {
return p[n-1-x+y*n]
})
}
// Compute a "hash" value for indexing patterns (unique for unique patterns)
indexFromPattern := func(p Pattern) int {
result := 0
power := 1
for i := 0; i < len(p); i++ {
result += p[len(p)-1-i] * power
power *= c
}
return result
}
// Reverse the hash of a pattern's index
patternFromIndex := func(ind int) Pattern {
residue := ind
power := w
result := make(Pattern, n*n)
for i := 0; i < len(result); i++ {
power /= c
count := 0
for residue >= power {
residue -= power
count++
}
result[i] = count
}
return result
}
// Build map of patterns (indexed by computed hash) to weights based on frequency in sample
weights := make(map[int]int)
weightsKeys := make([]int, 0)
var horizontalBound, verticalBound int
if periodicInput {
horizontalBound = dataWidth
verticalBound = dataHeight
} else {
horizontalBound = dataWidth - n + 1
verticalBound = dataHeight - n + 1
}
for y := 0; y < verticalBound; y++ {
for x := 0; x < horizontalBound; x++ {
ps := make([]Pattern, 8, 8)
ps[0] = patternFromSample(x, y)
ps[1] = reflect(ps[0])
ps[2] = rotate(ps[0])
ps[3] = reflect(ps[2])
ps[4] = rotate(ps[2])
ps[5] = reflect(ps[4])
ps[6] = rotate(ps[4])
ps[7] = reflect(ps[6])
for k := 0; k < symmetry; k++ {
ind := indexFromPattern(ps[k])
if _, ok := weights[ind]; ok {
weights[ind]++
} else {
weightsKeys = append(weightsKeys, ind)
weights[ind] = 1
}
if ground && y == verticalBound-1 && x == 0 && k == 0 {
// Set groung pattern
model.Ground = len(weightsKeys) - 1
}
}
}
}
model.T = len(weightsKeys)
// Store the patterns and cooresponding weights (stationary)
model.Patterns = make([]Pattern, model.T)
model.Stationary = make([]float64, model.T)
model.Propagator = make([][][][]int, model.T)
for i, wk := range weightsKeys {
model.Patterns[i] = patternFromIndex(wk)
model.Stationary[i] = float64(weights[wk])
}
// Initialize wave (to all true) and changes (to all false) fields
model.Wave = make([][][]bool, model.Fmx)
model.Changes = make([][]bool, model.Fmx)
for x := 0; x < model.Fmx; x++ {
model.Wave[x] = make([][]bool, model.Fmy)
model.Changes[x] = make([]bool, model.Fmy)
for y := 0; y < model.Fmy; y++ {
model.Wave[x][y] = make([]bool, model.T)
model.Changes[x][y] = false
for t := 0; t < model.T; t++ {
model.Wave[x][y][t] = true
}
}
}
// Check that the spaces n distance away have no conflicts
agrees := func(p1, p2 Pattern, dx, dy int) bool {
var xmin, xmax, ymin, ymax int
if dx < 0 {
xmin = 0
xmax = dx + n
} else {
xmin = dx
xmax = n
}
if dy < 0 {
ymin = 0
ymax = dy + n
} else {
ymin = dy
ymax = n
}
for y := ymin; y < ymax; y++ {
for x := xmin; x < xmax; x++ {
if p1[x+n*y] != p2[x-dx+n*(y-dy)] {
return false
}
}
}
return true
}
// Build table of which patterns can exist next to another
for t := 0; t < model.T; t++ {
model.Propagator[t] = make([][][]int, 2*n-1)
for x := 0; x < 2*n-1; x++ {
model.Propagator[t][x] = make([][]int, 2*n-1)
for y := 0; y < 2*n-1; y++ {
list := make([]int, 0)
for t2 := 0; t2 < model.T; t2++ {
if agrees(model.Patterns[t], model.Patterns[t2], x-n+1, y-n+1) {
list = append(list, t2)
}
}
model.Propagator[t][x][y] = make([]int, len(list))
for k := 0; k < len(list); k++ {
model.Propagator[t][x][y][k] = list[k]
}
}
}
}
model.Fmxmn = model.Fmx - model.N
model.Fmymn = model.Fmy - model.N
return model
}
/**
* OnBoundary
*/
func (model *OverlappingModel) OnBoundary(x, y int) bool {
return !model.Periodic && (x > model.Fmxmn || y > model.Fmymn)
}
/**
* Propagate
* return: bool, change occured in this iteration
*/
func (model *OverlappingModel) Propagate() bool {
change := false
startLoop := -model.N + 1
endLoop := model.N
for x := 0; x < model.Fmx; x++ {
for y := 0; y < model.Fmy; y++ {
if model.Changes[x][y] {
model.Changes[x][y] = false
for dx := startLoop; dx < endLoop; dx++ {
for dy := startLoop; dy < endLoop; dy++ {
sx := x + dx
sy := y + dy
if sx < 0 {
sx += model.Fmx
} else if sx >= model.Fmx {
sx -= model.Fmx
}
if sy < 0 {
sy += model.Fmy
} else if sy >= model.Fmy {
sy -= model.Fmy
}
if !model.Periodic && (sx > model.Fmx || sy > model.Fmy) {
continue
}
allowed := model.Wave[sx][sy]
for t := 0; t < model.T; t++ {
if !allowed[t] {
continue
}
b := false
prop := model.Propagator[t][model.N-1-dx][model.N-1-dy]
for i := 0; i < len(prop) && !b; i++ {
b = model.Wave[x][y][prop[i]]
}
if !b {
model.Changes[sx][sy] = true
change = true
allowed[t] = false
}
}
}
}
}
}
}
return change
}
/**
* Clear the internal state, then set ground pattern
*/
func (model *OverlappingModel) Clear() {
model.ClearBase(model)
if model.Ground != -1 && model.T > 1 {
for x := 0; x < model.Fmx; x++ {
for t := 0; t < model.T; t++ {
if t != model.Ground {
model.Wave[x][model.Fmy-1][t] = false
}
}
model.Changes[x][model.Fmy-1] = true
for y := 0; y < model.Fmy-1; y++ {
model.Wave[x][y][model.Ground] = false
model.Changes[x][y] = true
}
}
for model.Propagate() {
// Empty loop
}
}
}
/**
* Create a image.Image holding the data for a complete image
*/
func (model *OverlappingModel) RenderCompleteImage() image.Image {
output := make([][]color.Color, model.Fmx)
for i := range output {
output[i] = make([]color.Color, model.Fmy)
}
for y := 0; y < model.Fmy; y++ {
for x := 0; x < model.Fmx; x++ {
for t := 0; t < model.T; t++ {
if model.Wave[x][y][t] {
output[x][y] = model.Colors[model.Patterns[t][0]]
}
}
}
}
return GeneratedImage{output}
}
/**
* Create a image.Image holding the data for an incomplete image
*/
func (model *OverlappingModel) RenderIncompleteImage() image.Image {
output := make([][]color.Color, model.Fmx)
for i := range output {
output[i] = make([]color.Color, model.Fmy)
}
var contributorNumber, sR, sG, sB, sA uint32
for y := 0; y < model.Fmy; y++ {
for x := 0; x < model.Fmx; x++ {
contributorNumber, sR, sG, sB, sA = 0, 0, 0, 0, 0
for dy := 0; dy < model.N; dy++ {
for dx := 0; dx < model.N; dx++ {
sx := x - dx
if sx < 0 {
sx += model.Fmx
}
sy := y - dy
if sy < 0 {
sy += model.Fmy
}
if !model.Periodic && (sx > model.Fmxmn || sy > model.Fmymn) {
continue
}
for t := 0; t < model.T; t++ {
if model.Wave[sx][sy][t] {
contributorNumber++
r, g, b, a := model.Colors[model.Patterns[t][dx+dy*model.N]].RGBA()
sR += r
sG += g
sB += b
sA += a
}
}
}
}
if contributorNumber == 0 {
output[x][y] = color.RGBA{127, 127, 127, 255}
} else {
uR := uint8((sR / contributorNumber) >> 8)
uG := uint8((sG / contributorNumber) >> 8)
uB := uint8((sB / contributorNumber) >> 8)
uA := uint8((sA / contributorNumber) >> 8)
output[x][y] = color.RGBA{uR, uG, uB, uA}
}
}
}
return GeneratedImage{output}
}
/**
* Retrieve the RGBA data
* returns: Image
*/
func (model *OverlappingModel) Render() image.Image {
if model.IsGenerationSuccessful() {
return model.RenderCompleteImage()
} else {
return model.RenderIncompleteImage()
}
}
/**
* Retrieve the RGBA data
* returns: Image, finished, successful
*/
func (model *OverlappingModel) Iterate(iterations int) (image.Image, bool, bool) {
finished := model.BaseModel.Iterate(model, iterations)
return model.Render(), finished, model.IsGenerationSuccessful()
}
/**
* Retrieve the RGBA data
* returns: Image, successful
*/
func (model *OverlappingModel) Generate() (image.Image, bool) {
model.BaseModel.Generate(model)
return model.Render(), model.IsGenerationSuccessful()
}