-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.go
353 lines (292 loc) · 7.34 KB
/
simulator.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
package gobls
import (
"image"
"image/color"
"image/png"
"math/rand"
"os"
"time"
)
const (
BRIGHT_MIN = 57087 // 223(0xDF) for uint8, 57087(0xDEFF) for uint32
TIME_RAISE = 0.5
TIME_FALL = 0.5
TIME_RANDOM = 0.5
)
type Simulator struct {
prevImage image.Image
curImage image.Image
width int
height int
wireMap [][]int
wireRemap []int
states []bool // wire states
gates []*gate // not gates
gatePerm []int // permutation for not gates
}
func NewSimulator() *Simulator {
simulator := new(Simulator)
return simulator
}
func (simulator *Simulator) LoadImage(img image.Image) {
simulator.prevImage = simulator.curImage
simulator.curImage = img
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
// search wires horizontally
wireMap := make([][]int, height)
for i := range wireMap {
wireMap[i] = make([]int, width)
}
wireIdx := -1
for y := 0; y < height; y++ {
pixel := img.At(0, y)
if isConductive(pixel) {
wireIdx++
wireMap[y][0] = wireIdx
} else {
wireMap[y][0] = -1
}
for x := 1; x < width; x++ {
prevPixel := img.At(x-1, y)
curPixel := img.At(x, y)
if isConductive(curPixel) {
if !isConductive(prevPixel) {
wireIdx++
}
wireMap[y][x] = wireIdx
} else {
wireMap[y][x] = -1
}
}
}
// remap wires
wireRemap := make([]int, wireIdx+1)
for i := range wireRemap {
wireRemap[i] = i
}
for y := 1; y < height; y++ {
for x := 0; x < width; x++ {
upperWire := wireMap[y-1][x]
lowerWire := wireMap[y][x]
if upperWire >= 0 && lowerWire >= 0 {
upperIdx := wireRemap[upperWire]
lowerIdx := wireRemap[lowerWire]
if upperIdx != lowerIdx {
// connect two wire
for i, v := range wireRemap {
if v == lowerIdx {
wireRemap[i] = upperIdx
}
}
}
}
}
}
// search crossing wires and not gates
gates := make([]*gate, 0)
for y := 1; y < height-1; y++ {
for x := 1; x < width-1; x++ {
if wireMap[y][x] < 0 && wireMap[y][x-1] >= 0 && wireMap[y][x+1] >= 0 && wireMap[y-1][x] >= 0 && wireMap[y+1][x] >= 0 {
flag := 0
if wireMap[y-1][x-1] >= 0 {
flag |= 1 << 0
}
if wireMap[y-1][x+1] >= 0 {
flag |= 1 << 1
}
if wireMap[y+1][x+1] >= 0 {
flag |= 1 << 2
}
if wireMap[y+1][x-1] >= 0 {
flag |= 1 << 3
}
switch flag {
case 0: // crossing wire
// connect up, down wire and left, right wire
upperIdx := wireRemap[wireMap[y-1][x]]
lowerIdx := wireRemap[wireMap[y+1][x]]
leftIdx := wireRemap[wireMap[y][x-1]]
rightIdx := wireRemap[wireMap[y][x+1]]
for i, v := range wireRemap {
if v == lowerIdx {
wireRemap[i] = upperIdx
}
}
for i, v := range wireRemap {
if v == rightIdx {
wireRemap[i] = leftIdx
}
}
case 1 + 2: // not gate down
gates = append(gates, &gate{in: point{x, y - 1}, out: point{x, y + 1}})
case 2 + 4: // not gate left
gates = append(gates, &gate{in: point{x + 1, y}, out: point{x - 1, y}})
case 4 + 8: // not gate up
gates = append(gates, &gate{in: point{x, y + 1}, out: point{x, y - 1}})
case 8 + 1: // not gate right
gates = append(gates, &gate{in: point{x - 1, y}, out: point{x + 1, y}})
}
}
}
}
// resolve gate in, out idx
for _, gate := range gates {
gate.inIdx = wireRemap[wireMap[gate.in.y][gate.in.x]]
gate.outIdx = wireRemap[wireMap[gate.out.y][gate.out.x]]
}
// find input gates
for _, gate := range gates {
gate.inGates = make([]int, 0)
for gateIdx, inputGate := range gates {
if gate.inIdx == inputGate.outIdx {
gate.inGates = append(gate.inGates, gateIdx)
}
}
}
// gate permutation
gatePerm := rand.Perm(len(gates))
// init wire state
states := make([]bool, len(wireRemap))
for i := range states {
states[i] = false
}
// copy previous states
/*
if simulator.prevImage != nil {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if wireMap[y][x] != 0 && isConductive(simulator.prevImage.At(x, y)) {
states[wireRemap[wireMap[y][x]]] = true
}
}
}
}
*/
simulator.width = width
simulator.height = height
simulator.wireMap = wireMap
simulator.wireRemap = wireRemap
simulator.gates = gates
simulator.states = states
simulator.gatePerm = gatePerm
simulator.Simulate()
simulator.test()
}
func (simulator *Simulator) test() {
// wire remapping test image
wireRemapImgFile, err := os.Create("wireMap.png")
if err != nil {
panic(err)
}
wireRemapImg := image.NewRGBA(simulator.curImage.Bounds())
rand.Seed(time.Now().UTC().UnixNano())
randomRColor := rand.Perm(200)
randomGColor := rand.Perm(200)
randomBColor := rand.Perm(200)
for y := 0; y < simulator.height; y++ {
for x := 0; x < simulator.width; x++ {
wire := simulator.wireMap[y][x]
if wire >= 0 {
idx := simulator.wireRemap[wire]
wireRemapImg.Set(x, y, color.RGBA{
uint8(randomRColor[idx%200] + 55),
uint8(randomGColor[idx%200] + 55),
uint8(randomBColor[idx%200] + 55),
255,
})
} else {
wireRemapImg.Set(x, y, color.RGBA{0, 0, 0, 255})
}
}
}
png.Encode(wireRemapImgFile, wireRemapImg)
// gates test image
gateImgFile, err := os.Create("gate.png")
if err != nil {
panic(err)
}
gateImg := image.NewRGBA(simulator.curImage.Bounds())
for y := 0; y < simulator.height; y++ {
for x := 0; x < simulator.width; x++ {
gateImg.Set(x, y, color.RGBA{0, 0, 0, 0})
}
}
for _, gate := range simulator.gates {
gateImg.Set(gate.in.x, gate.in.y, color.RGBA{255, 0, 0, 255})
gateImg.Set(gate.out.x, gate.out.y, color.RGBA{0, 255, 0, 255})
}
png.Encode(gateImgFile, gateImg)
}
func (simulator *Simulator) Simulate() {
for i := range simulator.gates {
g := simulator.gates[simulator.gatePerm[i]]
newState := !simulator.gateInput(g)
g.updateState(newState)
}
simulator.storeGateStatesToWires()
}
func (simulator *Simulator) Set(x, y int, state bool) bool {
wire := simulator.wireMap[y][x]
if wire >= 0 {
wireIdx := simulator.wireRemap[wire]
simulator.states[wireIdx] = state
return true
}
return false
}
func (simulator *Simulator) Get(x, y int) bool {
wire := simulator.wireMap[y][x]
if wire >= 0 {
wireIdx := simulator.wireRemap[wire]
state := simulator.states[wireIdx]
return state
}
return false
}
func (simulator *Simulator) Size() (int, int) {
return simulator.width, simulator.height
}
func (simulator *Simulator) PerPixel(f func(int, int, bool)) {
for y := 0; y < simulator.height; y++ {
for x := 0; x < simulator.width; x++ {
wire := simulator.wireMap[y][x]
state := false
if wire != -1 {
state = simulator.states[simulator.wireRemap[wire]]
}
f(x, y, state)
}
}
}
func isConductive(pixel color.Color) bool {
r, g, b, _ := pixel.RGBA()
return r > BRIGHT_MIN || g > BRIGHT_MIN || b > BRIGHT_MIN
}
func (simulator *Simulator) gateInput(g *gate) bool {
if g.inGates == nil || len(g.inGates) == 0 {
return simulator.states[g.inIdx]
} else {
for _, inGate := range g.inGates {
if simulator.gates[inGate].state {
return true
}
}
return false
}
}
func (simulator *Simulator) loadGateStatesFromWire() {
for _, g := range simulator.gates {
g.setState(simulator.states[g.outIdx])
}
}
func (simulator *Simulator) storeGateStatesToWires() {
for _, g := range simulator.gates {
simulator.states[g.outIdx] = false
}
for _, g := range simulator.gates {
if g.state {
simulator.states[g.outIdx] = true
}
}
}