/
Main.elm
409 lines (368 loc) · 14.8 KB
/
Main.elm
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
module Main exposing (..)
import Adjacency
import Array
import Browser
import Color.Transparent as Color
import Css
import Css.Reset as Reset
import Dict exposing (Dict)
import Grid exposing (Grid)
import Heap
import Html as RootHtml
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attributes exposing (css, style)
import Html.Styled.Events as Events
import Image exposing (Image)
import Murmur3
import Process
import Random
import Set exposing (Set)
import Task
import Wave exposing (Wave)
type alias Model =
{ image : Image
, windowSize : { width : Int, height : Int }
, windows : Grid Image
, wave : Wave Int
, waveSize : { width : Int, height : Int }
, seed : Random.Seed
, running : Bool
, indexes : Dict Int Image
, weights : Dict Int Int
, rules : Adjacency.Rules Int
, showEntropy : Bool
, dimUnknown : Bool
}
type Msg
= Reset { width : Int, height : Int }
| Start
| Stop
| Step
| ToggleEntropy
| ToggleDimUnknown
| Load Image { width : Int, height : Int }
init : () -> ( Model, Cmd Msg )
init _ =
( load Image.waves { width = 3, height = 3 }
, Cmd.none
)
load : Image -> { width : Int, height : Int } -> Model
load image windowSize =
let
windows =
Grid.windows windowSize image
withIndex =
Grid.map
(\window ->
( window
|> Grid.toArrays
|> Array.foldr
(\row rowString ->
Array.foldr (\value string -> Color.toRGBAString value ++ string)
rowString
row
)
""
|> Murmur3.hashString 0
, window
)
)
windows
indexes =
-- wow there is a lot of conversion happening here. Probably should
-- come back and make it more efficient sometime.
withIndex
|> Grid.toArrays
|> Array.foldl Array.append Array.empty
|> Array.toList
|> Dict.fromList
weights =
-- wow there is a lot of conversion happening here. Probably should
-- come back and make it more efficient sometime.
withIndex
|> Grid.toArrays
|> Array.foldl Array.append Array.empty
|> Array.foldl
(\( id, _ ) dict ->
Dict.update id
(\maybeCount ->
case maybeCount of
Just count ->
Just (count + 1)
Nothing ->
Just 1
)
dict
)
Dict.empty
rules =
withIndex
|> Grid.map Tuple.first
|> Adjacency.fromIds
|> Adjacency.finalize
in
{ image = image
, windowSize = windowSize
, windows = windows
, wave = Wave.init rules weights { width = 10, height = 10 }
, waveSize = { width = 10, height = 10 }
, seed = Random.initialSeed 0
, running = False
, indexes = indexes
, weights = weights
, rules = rules
, showEntropy = False
, dimUnknown = False
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Reset dimensions ->
( { model
| wave = Wave.init model.rules model.weights dimensions
, waveSize = dimensions
}
, Cmd.none
)
Step ->
let
( newWave, newSeed ) =
Wave.step model.seed model.wave
in
( { model | wave = newWave, seed = newSeed }
, if model.running then
Task.perform (\_ -> Step) (Process.sleep 0)
else
Cmd.none
)
Start ->
update Step { model | running = True }
Stop ->
( { model | running = False }, Cmd.none )
ToggleEntropy ->
( { model | showEntropy = not model.showEntropy }, Cmd.none )
ToggleDimUnknown ->
( { model | dimUnknown = not model.dimUnknown }, Cmd.none )
Load image dimensions ->
( load image dimensions, Cmd.none )
main : Program () Model Msg
main =
Browser.element
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
view : Model -> RootHtml.Html Msg
view model =
Html.toUnstyled <|
Html.div
[ css
[ Css.fontFamily Css.sansSerif
, Css.margin2 (Css.rem 1) Css.auto
, Css.padding2 (Css.rem 1.5) (Css.rem 2)
, Css.width (Css.pct 80)
, Css.backgroundColor (Css.hex "F0F8FF")
]
]
[ Reset.meyerV2
, Reset.borderBoxV201408
, h1 [ Html.text "Wave Function Collapse" ]
, h2 [ Html.text "Source Image" ]
, [ ( "Waves", Image.waves )
, ( "Bars", Image.bars )
, ( "Recurse", Image.recurse )
, ( "Nyan Cat", Image.nyan )
]
|> List.map
(\( name, image ) ->
[ { width = 1, height = 1 }
, { width = 2, height = 2 }
, { width = 3, height = 3 }
]
|> List.map
(\dimensions ->
Html.button [ Events.onClick (Load image dimensions) ] [ Html.text (name ++ "(" ++ String.fromInt dimensions.height ++ "x" ++ String.fromInt dimensions.width ++ ")") ]
)
|> Html.p []
)
|> Html.div []
, Image.view [] model.image
, Html.details []
[ Html.summary [] [ Html.text "Windows" ]
, model.windows
|> Grid.view
(\window ->
Html.div
[ css
[ Css.border3 (Css.px 1) Css.solid (Css.hex "000")
, Css.display Css.inlineBlock
, Css.margin (Css.px 5)
]
]
[ Image.view [] window ]
)
]
, h2 [ Html.text "Wave" ]
, Html.p []
[ if model.running then
Html.button [ Events.onClick Stop ] [ Html.text "Stop" ]
else
Html.button [ Events.onClick Start ] [ Html.text "Start" ]
, Html.button [ Events.onClick Step ] [ Html.text "Step" ]
]
, Html.p []
[ Html.button [ Events.onClick (Reset { width = 2, height = 2 }) ] [ Html.text "Reset (2x2)" ]
, Html.button [ Events.onClick (Reset { width = 5, height = 5 }) ] [ Html.text "Reset (5x5)" ]
, Html.button [ Events.onClick (Reset { width = 10, height = 10 }) ] [ Html.text "Reset (10x10)" ]
, Html.button [ Events.onClick (Reset { width = 20, height = 20 }) ] [ Html.text "Reset (20x20)" ]
, Html.button [ Events.onClick (Reset { width = 50, height = 50 }) ] [ Html.text "Reset (50x50)" ]
]
, Html.p []
[ Html.button [ Events.onClick ToggleEntropy ] [ Html.text "Toggle Entropy (debug)" ]
, Html.button [ Events.onClick ToggleDimUnknown ] [ Html.text "Toggle Dimming Uknown Cells (debug)" ]
]
, Wave.view
(\cell ->
case cell of
Wave.Collapsed tile ->
case Dict.get tile model.indexes |> Maybe.andThen Grid.topLeft of
Just color ->
Image.viewColor [] color
Nothing ->
Html.text "invalid index"
Wave.Open indexes ->
let
{ reds, blues, greens, opacities } =
indexes
|> Set.toList
|> List.filterMap (\i -> Dict.get i model.indexes)
|> List.filterMap Grid.topLeft
|> List.foldl
(\color soFar ->
let
rgba =
Color.toRGBA color
in
{ reds = rgba.red :: soFar.reds
, blues = rgba.blue :: soFar.blues
, greens = rgba.green :: soFar.greens
, opacities = Color.opacityToFloat rgba.alpha :: soFar.opacities
}
)
{ reds = [], greens = [], blues = [], opacities = [] }
average items =
List.sum items / toFloat (List.length items)
in
if Set.isEmpty indexes then
Html.td
[ css
[ Css.lineHeight (Css.px 10)
, Css.fontSize (Css.px 10)
, Css.textAlign Css.center
]
, Attributes.width 10
, Attributes.height 10
]
[ Html.text "×" ]
else
Image.viewColor
[ Attributes.attribute "data-count" (String.fromInt (Set.size indexes))
, style "opacity"
(if model.dimUnknown then
"0.5"
else
"1"
)
]
(Color.fromRGBA
{ red = average reds
, green = average greens
, blue = average blues
, alpha = Color.customOpacity (average opacities)
}
)
)
model.wave
, if model.showEntropy then
let
entropies =
Wave.getEntropy model.wave
in
Html.div []
[ Html.text "Entropy Info"
, Html.p []
[ Html.text "Next: "
, case Heap.peek entropies of
Nothing ->
Html.text "Nothing"
Just { coords, entropy } ->
Html.span []
[ Html.text (String.fromInt entropy)
, Html.text " @ "
, Html.text (String.fromInt coords.row)
, Html.text ","
, Html.text (String.fromInt coords.column)
]
]
, Html.p []
[ Html.text "Total: "
, Html.text (String.fromInt (Heap.size entropies))
]
, entropies
|> Heap.toList
|> List.foldl
(\new grid ->
Grid.update
(\maybeExisting ->
case maybeExisting of
Nothing ->
Just new
Just existing ->
if existing.entropy < new.entropy then
Just existing
else
Just new
)
{ row = new.coords.row, column = new.coords.column }
grid
)
(Grid.initialize
{ rows = model.waveSize.height
, columns = model.waveSize.width
}
(always Nothing)
)
|> Grid.view
(\value ->
Html.td [ css [ Css.padding (Css.px 2) ] ]
[ value
|> Maybe.map (String.fromInt << .entropy)
|> Maybe.withDefault "-"
|> Html.text
]
)
]
else
Html.text ""
]
h1 : List (Html msg) -> Html msg
h1 contents =
Html.h1
[ css
[ Css.fontSize (Css.rem 2)
, Css.lineHeight (Css.rem 2.5)
, Css.marginBottom (Css.rem 0.5)
, Css.fontWeight Css.bold
]
]
contents
h2 : List (Html msg) -> Html msg
h2 contents =
Html.h2
[ css
[ Css.fontSize (Css.rem 1.25)
, Css.lineHeight (Css.rem 1.5)
]
]
contents