-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecompose.go
519 lines (512 loc) · 11.5 KB
/
decompose.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Code generated by cxgo. DO NOT EDIT.
package gotrace
import (
"math"
"github.com/gotranspile/cxgo/runtime/libc"
)
func detrand(x int, y int) int {
var (
z uint
t [256]uint8 = [256]uint8{0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}
)
z = uint(((x * 0x4B3E375) ^ y) * 0x5A8EF93)
z = uint(int(t[z&math.MaxUint8]) ^ int(t[(z>>8)&math.MaxUint8]) ^ int(t[(z>>16)&math.MaxUint8]) ^ int(t[(z>>24)&math.MaxUint8]))
return int(z)
}
func bm_clearexcess(bm *Bitmap) {
var (
mask Word
y int
)
if bm.W%(8*(int(sizeofWord))) != 0 {
mask = (^Word(0)) << Word((8*(int(sizeofWord)))-bm.W%(8*(int(sizeofWord))))
for y = 0; y < bm.H; y++ {
bm.Map[int64(y)*int64(bm.Dy)+int64(bm.W/(8*(int(sizeofWord))))] &= mask
}
}
}
type bbox_s struct {
X0 int
X1 int
Y0 int
Y1 int
}
type bbox_t bbox_s
func clear_bm_with_bbox(bm *Bitmap, bbox *bbox_t) {
var (
imin int = (bbox.X0 / (8 * (int(sizeofWord))))
imax int = ((bbox.X1 + 8*(int(sizeofWord)) - 1) / (8 * (int(sizeofWord))))
i int
y int
)
for y = bbox.Y0; y < bbox.Y1; y++ {
for i = imin; i < imax; i++ {
bm.Map[int64(y)*int64(bm.Dy)+int64(i)] = 0
}
}
}
func majority(bm *Bitmap, x int, y int) int {
var (
i int
a int
ct int
)
for i = 2; i < 5; i++ {
ct = 0
for a = -i + 1; a <= i-1; a++ {
if func() bool {
if (x+a) >= 0 && (x+a) < bm.W && ((y+i-1) >= 0 && (y+i-1) < bm.H) {
return (bm.Map[int64(y+i-1)*int64(bm.Dy)+int64((x+a)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x+a)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
ct += 1
} else {
ct += -1
}
if func() bool {
if (x+i-1) >= 0 && (x+i-1) < bm.W && ((y+a-1) >= 0 && (y+a-1) < bm.H) {
return (bm.Map[int64(y+a-1)*int64(bm.Dy)+int64((x+i-1)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x+i-1)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
ct += 1
} else {
ct += -1
}
if func() bool {
if (x+a-1) >= 0 && (x+a-1) < bm.W && ((y-i) >= 0 && (y-i) < bm.H) {
return (bm.Map[int64(y-i)*int64(bm.Dy)+int64((x+a-1)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x+a-1)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
ct += 1
} else {
ct += -1
}
if func() bool {
if (x-i) >= 0 && (x-i) < bm.W && ((y+a) >= 0 && (y+a) < bm.H) {
return (bm.Map[int64(y+a)*int64(bm.Dy)+int64((x-i)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x-i)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
ct += 1
} else {
ct += -1
}
}
if ct > 0 {
return 1
} else if ct < 0 {
return 0
}
}
return 0
}
func xor_to_ref(bm *Bitmap, x int, y int, xa int) {
var (
xhi int = x & (-(8 * (int(sizeofWord))))
xlo int = x & ((8 * (int(sizeofWord))) - 1)
i int
)
if xhi < xa {
for i = xhi; i < xa; i += 8 * (int(sizeofWord)) {
bm.Map[int64(y)*int64(bm.Dy)+int64(i/(8*(int(sizeofWord))))] ^= ^Word(0)
}
} else {
for i = xa; i < xhi; i += 8 * (int(sizeofWord)) {
bm.Map[int64(y)*int64(bm.Dy)+int64(i/(8*(int(sizeofWord))))] ^= ^Word(0)
}
}
if xlo != 0 {
bm.Map[int64(y)*int64(bm.Dy)+int64(xhi/(8*(int(sizeofWord))))] ^= (^Word(0)) << Word((8*(int(sizeofWord)))-xlo)
}
}
func xor_path(bm *Bitmap, p *Path) {
var (
xa int
x int
y int
k int
y1 int
)
if p.Priv.Len <= 0 {
return
}
y1 = p.Priv.Pt[p.Priv.Len-1].Y
xa = p.Priv.Pt[0].X & (-(8 * (int(sizeofWord))))
for k = 0; k < p.Priv.Len; k++ {
x = p.Priv.Pt[k].X
y = p.Priv.Pt[k].Y
if y != y1 {
xor_to_ref(bm, x, func() int {
if y < y1 {
return y
}
return y1
}(), xa)
y1 = y
}
}
}
func setbbox_path(bbox *bbox_t, p *Path) {
var (
x int
y int
k int
)
bbox.Y0 = math.MaxInt64
bbox.Y1 = 0
bbox.X0 = math.MaxInt64
bbox.X1 = 0
for k = 0; k < p.Priv.Len; k++ {
x = p.Priv.Pt[k].X
y = p.Priv.Pt[k].Y
if x < bbox.X0 {
bbox.X0 = x
}
if x > bbox.X1 {
bbox.X1 = x
}
if y < bbox.Y0 {
bbox.Y0 = y
}
if y > bbox.Y1 {
bbox.Y1 = y
}
}
}
func findpath(bm *Bitmap, x0 int, y0 int, sign int, turnpolicy int) *Path {
var (
x int
y int
dirx int
diry int
len_ int
size int
area uint64
c int
d int
tmp int
pt, pt1 []Point
p *Path = nil
)
x = x0
y = y0
dirx = 0
diry = -1
len_ = func() int {
size = 0
return size
}()
pt = nil
area = 0
for {
if len_ >= size {
size += 100
size = int(float64(size) * 1.3)
pt1 = make([]Point, size)
copy(pt1, pt)
if pt1 == nil {
goto error
}
pt = pt1
}
pt[len_].X = x
pt[len_].Y = y
len_++
x += dirx
y += diry
area += uint64(x * diry)
if x == x0 && y == y0 {
break
}
c = int(libc.BoolToInt(func() bool {
if (x+(dirx+diry-1)/2) >= 0 && (x+(dirx+diry-1)/2) < bm.W && ((y+(diry-dirx-1)/2) >= 0 && (y+(diry-dirx-1)/2) < bm.H) {
return (bm.Map[int64(y+(diry-dirx-1)/2)*int64(bm.Dy)+int64((x+(dirx+diry-1)/2)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x+(dirx+diry-1)/2)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}()))
d = int(libc.BoolToInt(func() bool {
if (x+(dirx-diry-1)/2) >= 0 && (x+(dirx-diry-1)/2) < bm.W && ((y+(diry+dirx-1)/2) >= 0 && (y+(diry+dirx-1)/2) < bm.H) {
return (bm.Map[int64(y+(diry+dirx-1)/2)*int64(bm.Dy)+int64((x+(dirx-diry-1)/2)/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>((x+(dirx-diry-1)/2)&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}()))
if c != 0 && d == 0 {
if turnpolicy == TurnRight || turnpolicy == TurnBlack && sign == '+' || turnpolicy == TurnWhite && sign == '-' || turnpolicy == TurnRandom && detrand(x, y) != 0 || turnpolicy == TurnMajority && majority(bm, x, y) != 0 || turnpolicy == TurnMinority && majority(bm, x, y) == 0 {
tmp = dirx
dirx = diry
diry = -tmp
} else {
tmp = dirx
dirx = -diry
diry = tmp
}
} else if c != 0 {
tmp = dirx
dirx = diry
diry = -tmp
} else if d == 0 {
tmp = dirx
dirx = -diry
diry = tmp
}
}
p = path_new()
if p == nil {
goto error
}
p.Priv.Pt = []Point(pt)
p.Priv.Len = len_
if area <= math.MaxInt64 {
p.Area = int(area)
} else {
p.Area = math.MaxInt64
}
p.Sign = sign
return p
error:
return nil
}
func pathlist_to_tree(plist *Path, bm *Bitmap) {
var (
p *Path
p1 *Path
heap *Path
heap1 *Path
cur *Path
head *Path
plist_hook **Path
hook_in **Path
hook_out **Path
bbox bbox_t
)
bm_clear(bm, 0)
for p = plist; p != nil; p = p.Next {
p.Sibling = p.Next
p.Childlist = nil
}
heap = plist
for heap != nil {
cur = heap
heap = heap.Childlist
cur.Childlist = nil
head = cur
cur = cur.Next
head.Next = nil
xor_path(bm, head)
setbbox_path(&bbox, head)
hook_in = &head.Childlist
hook_out = &head.Next
for p = cur; func() int {
if p != nil {
return func() int {
cur = p.Next
p.Next = nil
return 1
}()
}
return 0
}() != 0; p = cur {
if p.Priv.Pt[0].Y <= bbox.Y0 {
for {
p.Next = *hook_out
*hook_out = p
hook_out = &p.Next
if true {
break
}
}
*hook_out = cur
break
}
if func() bool {
if p.Priv.Pt[0].X >= 0 && p.Priv.Pt[0].X < bm.W && ((p.Priv.Pt[0].Y-1) >= 0 && (p.Priv.Pt[0].Y-1) < bm.H) {
return (bm.Map[int64(p.Priv.Pt[0].Y-1)*int64(bm.Dy)+int64(p.Priv.Pt[0].X/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>(p.Priv.Pt[0].X&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
for {
p.Next = *hook_in
*hook_in = p
hook_in = &p.Next
if true {
break
}
}
} else {
for {
p.Next = *hook_out
*hook_out = p
hook_out = &p.Next
if true {
break
}
}
}
}
clear_bm_with_bbox(bm, &bbox)
if head.Next != nil {
head.Next.Childlist = heap
heap = head.Next
}
if head.Childlist != nil {
head.Childlist.Childlist = heap
heap = head.Childlist
}
}
p = plist
for p != nil {
p1 = p.Sibling
p.Sibling = p.Next
p = p1
}
heap = plist
if heap != nil {
heap.Next = nil
}
plist = nil
plist_hook = &plist
for heap != nil {
heap1 = heap.Next
for p = heap; p != nil; p = p.Sibling {
for {
p.Next = *plist_hook
*plist_hook = p
plist_hook = &p.Next
if true {
break
}
}
for p1 = p.Childlist; p1 != nil; p1 = p1.Sibling {
for {
p1.Next = *plist_hook
*plist_hook = p1
plist_hook = &p1.Next
if true {
break
}
}
if p1.Childlist != nil {
for {
{
var _hook **Path
for _hook = &heap1; *_hook != nil; _hook = &(*_hook).Next {
}
for {
p1.Childlist.Next = *_hook
*_hook = p1.Childlist
if true {
break
}
}
}
if true {
break
}
}
}
}
}
heap = heap1
}
return
}
func findnext(bm *Bitmap, xp *int, yp *int) int {
var (
x int
y int
x0 int
)
x0 = (*xp) & ^((8 * (int(sizeofWord))) - 1)
for y = *yp; y >= 0; y-- {
for x = x0; x < bm.W && x >= 0; x += int(uint(8 * (int(sizeofWord)))) {
if bm.Map[int64(y)*int64(bm.Dy)+int64(x/(8*(int(sizeofWord))))] != 0 {
for !(func() bool {
if x >= 0 && x < bm.W && (y >= 0 && y < bm.H) {
return (bm.Map[int64(y)*int64(bm.Dy)+int64(x/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>(x&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}()) {
x++
}
*xp = x
*yp = y
return 0
}
}
x0 = 0
}
return 1
}
func bm_to_pathlist(bm *Bitmap, plistp **Path, param *Config, progress *progress) int {
var (
x int
y int
p *Path
plist *Path = nil
plist_hook **Path = &plist
bm1 *Bitmap = nil
sign int
)
bm1 = bm_dup(bm)
if bm1 == nil {
goto error
}
bm_clearexcess(bm1)
x = 0
y = bm1.H - 1
for findnext(bm1, &x, &y) == 0 {
if func() bool {
if x >= 0 && x < bm.W && (y >= 0 && y < bm.H) {
return (bm.Map[int64(y)*int64(bm.Dy)+int64(x/(8*(int(sizeofWord))))] & Word((1<<((8*(int(sizeofWord)))-1))>>(x&((8*(int(sizeofWord)))-1)))) != 0
}
return false
}() {
sign = '+'
} else {
sign = '-'
}
p = findpath(bm1, x, y+1, sign, param.TurnPolicy)
if p == nil {
goto error
}
xor_path(bm1, p)
if p.Area <= param.TurdSize {
path_free(p)
} else {
for {
p.Next = *plist_hook
*plist_hook = p
plist_hook = &p.Next
if true {
break
}
}
}
if bm1.H > 0 {
progress_update(1-float64(y)/float64(bm1.H), progress)
}
}
pathlist_to_tree(plist, bm1)
bm_free(bm1)
*plistp = plist
progress_update(1.0, progress)
return 0
error:
bm_free(bm1)
for p = plist; func() int {
if p != nil {
return func() int {
plist = p.Next
p.Next = nil
return 1
}()
}
return 0
}() != 0; p = plist {
path_free(p)
}
return -1
}