-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_gg_render.c
1734 lines (1267 loc) · 46.9 KB
/
fast_gg_render.c
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE /* gets us M_PIl */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <float.h>
#include <quadmath.h>
#include <complex.h>
#include <assert.h>
#include <setjmp.h>
#include <pthread.h>
#include <getopt.h>
#include <png.h>
#define ORD_LIMIT (500 * 1000 * 1000)
#define NUM_THREADS 24
#define LOG_SCALE 0x100000000
#define GROW_VISITED 1024
#define MAX_GEN_LEN 8
#define FNAME_LEN 1024 /* max output file path length */
/* #define naming conventions:
* _T -> type
* _F -> function
* _L -> literal
*/
#define FPREC 64
#if FPREC == 128
#define COMPLEX_T __complex128
#define FLOAT_T __float128
#define FLOAT_L(N) (N##Q)
#define FABS_F(x) (fabsq(x))
#define CABS_F(x) (cabsq(x))
#define SQRT_F(x) (sqrtq(x))
#define FSIN_F(x) (sinq(x))
#define FCOS_F(x) (cosq(x))
#define STRTO_F(x) (strtoflt128(x, NULL))
#define PI_L M_PIq
#define EPSILON_L FLOAT_L(1e-16)
#define MANTDIG_L FLT128_MANT_DIG
#elif FPREC == 80
#define COMPLEX_T _Complex long double
#define FLOAT_T long double
#define FLOAT_L(N) (N##L)
#define FABS_F(x) (fabsl(x))
#define CABS_F(x) (cabsl(x))
#define SQRT_F(x) (sqrtl(x))
#define FSIN_F(x) (sinl(x))
#define FCOS_F(x) (cosl(x))
#define STRTO_F(x) (strtold(x, NULL))
#define PI_L M_PIl
#define EPSILON_L FLOAT_L(1e-10)
#define MANTDIG_L LDBL_MANT_DIG
#elif FPREC == 64
#define COMPLEX_T complex
#define FLOAT_T double
#define FLOAT_L(N) (N)
#define FABS_F(x) (fabs(x))
#define CABS_F(x) (cabs(x))
#define SQRT_F(x) (sqrt(x))
#define FSIN_F(x) (sin(x))
#define FCOS_F(x) (cos(x))
#define STRTO_F(x) (strtod(x, NULL))
#define PI_L M_PI
#define EPSILON_L FLOAT_L(1e-10)
#define MANTDIG_L DBL_MANT_DIG
# else
#error "FPREC must defined and set to one of {128,80,64}"
#endif
struct thread_ctx {
pthread_t tid;
int tnum;
int num_threads;
struct render_ctx *ctx;
};
struct samples {
uint32_t count;
int64_t scaled_log_order;
};
struct visited_ctx {
uint32_t limit;
uint8_t *visited, *visited_m;
uint32_t *vx, *vy, *vx_m, *vy_m;
int32_t vsize, vused, vsize_m, vused_m;
};
struct render_ctx {
char out[FNAME_LEN];
int verbose;
uint32_t n;
FLOAT_T r;
FLOAT_T r_sq;
int img_w, img_h;
double xmin, xmax, ymin, ymax;
double pwidth, pheight, half_pwidth, half_pheight, pradius;
COMPLEX_T rot[MAX_GEN_LEN]; /* alternates a, b, a, b, ... */
int gen_len;
FLOAT_T epsilon;
struct samples *grid;
pthread_mutex_t *grid_mutex;
int wedge_only;
int box_only;
int sym180;
int order_delta;
uint64_t highest_order;
double stretch_exp; /* order to val stretching */
};
void write_png_file(char *filename, int width, int height, png_bytep image_data) {
FILE *fp = fopen(filename, "wb");
assert(fp != NULL);
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
assert(png != NULL);
png_infop info = png_create_info_struct(png);
assert(info != NULL);
/* PNG "exception handling" */
assert(setjmp(png_jmpbuf(png)) == 0);
png_init_io(png, fp);
png_set_IHDR(
png,
info,
width, height,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png, info);
assert(image_data != NULL);
for (int y = 0; y < height; y++) {
png_write_row(png, &(image_data[y * (width * 3)]));
}
png_write_end(png, NULL);
fclose(fp);
png_destroy_write_struct(&png, &info);
}
COMPLEX_T turn_angle(struct render_ctx *ctx, int amt) {
COMPLEX_T rot_ang;
__real__ rot_ang = FCOS_F(PI_L * FLOAT_L(-2.0) * ((FLOAT_T)amt / (FLOAT_T)ctx->n));
__imag__ rot_ang = FSIN_F(PI_L * FLOAT_L(-2.0) * ((FLOAT_T)amt / (FLOAT_T)ctx->n));
return rot_ang;
}
int xy_to_offset(struct render_ctx *ctx, int x, int y) {
return y * ctx->img_w + x;
}
int point_to_xy(struct render_ctx *ctx, COMPLEX_T p, int *x, int *y) {
double px = (double)(__real__ p);
double py = (double)(__imag__ p);
if ((px < ctx->xmin) || (px > ctx->xmax) ||
(py < ctx->ymin) || (py > ctx->ymax)) {
return -1;
}
*x = (int)floor((px - ctx->xmin) / ctx->pwidth);
*y = (int)floor((ctx->ymax - py) / ctx->pheight);
return 0;
}
COMPLEX_T point_from_xy(struct render_ctx *ctx, int x, int y) {
COMPLEX_T p;
__real__ p = ctx->xmin + (ctx->pwidth * x);
__imag__ p = ctx->ymax - (ctx->pheight * y);
return p;
}
COMPLEX_T point_rand_offset(struct render_ctx *ctx) {
COMPLEX_T p;
__real__ p = (FLOAT_T)(((double)rand() / (double)0x80000000) * ctx->pwidth);
__imag__ p = FLOAT_L(0.0) - ((FLOAT_T)(((double)rand() / (double)0x80000000) * ctx->pheight));
return p;
}
COMPLEX_T point_from_xy_rand(struct render_ctx *ctx, int x, int y) {
return point_from_xy(ctx, x, y) + point_rand_offset(ctx);
}
int point_equal_double(COMPLEX_T p, COMPLEX_T q) {
if (((double)(__real__ p) == (double)(__real__ q)) &&
((double)(__imag__ p) == (double)(__imag__ q))) {
return 1;
} else {
return 0;
}
}
int point_equal_epsilon(struct render_ctx *ctx, COMPLEX_T p, COMPLEX_T q) {
if (FABS_F(__real__ p - __real__ q) + FABS_F(__imag__ p - __imag__ q) < ctx->epsilon) {
return 1;
} else {
return 0;
}
}
int point_in_a(struct render_ctx *ctx, COMPLEX_T p) {
COMPLEX_T np = p;
__real__ np += FLOAT_L(1.0);
/* Check triangle inequality first */
if (FABS_F(__real__ np) + FABS_F(__imag__ np) < ctx->r) {
return 1;
/* Else check squared pythagorean */
} else if (((__real__ np) * (__real__ np)) +
((__imag__ np) * (__imag__ np)) < ctx->r_sq) {
return 1;
} else {
return 0;
}
}
int point_in_n(struct render_ctx *ctx, COMPLEX_T p, int n) {
COMPLEX_T np = p;
if ((n & 1) == 0) {
/* an A check */
__real__ np += FLOAT_L(1.0);
} else {
/* a B check */
__real__ np -= FLOAT_L(1.0);
}
/* Check triangle inequality first */
if (FABS_F(__real__ np) + FABS_F(__imag__ np) < ctx->r) {
return 1;
/* Else check squared pythagorean */
} else if (((__real__ np) * (__real__ np)) +
((__imag__ np) * (__imag__ np)) < ctx->r_sq) {
return 1;
} else {
return 0;
}
}
int point_in_b(struct render_ctx *ctx, COMPLEX_T p) {
COMPLEX_T np = p;
__real__ np -= FLOAT_L(1.0);
/* Check triangle inequality first */
if (FABS_F(__real__ np) + FABS_F(__imag__ np) < ctx->r) {
return 1;
/* Else check squared pythagorean */
} else if (((__real__ np) * (__real__ np)) +
((__imag__ np) * (__imag__ np)) < ctx->r_sq) {
return 1;
} else {
return 0;
}
}
int xy_on_border(struct render_ctx *ctx, int x, int y) {
COMPLEX_T p = point_from_xy(ctx, x, y);
COMPLEX_T np;
double dist;
/* Check distace to A */
np = p;
__real__ np += FLOAT_L(1.0);
dist = (double)CABS_F(np);
if ((dist <= (double)ctx->r + (2.0 * ctx->pradius)) && (dist >= (double)ctx->r - (2.0 * ctx->pradius))) {
return 1;
}
/* Check distace to B */
np = p;
__real__ np -= FLOAT_L(1.0);
dist = (double)CABS_F(np);
if ((dist <= (double)ctx->r + (2.0 * ctx->pradius)) && (dist >= (double)ctx->r - (2.0 * ctx->pradius))) {
return 1;
}
return 0;
}
int point_in_puzzle(struct render_ctx *ctx, COMPLEX_T p) {
int in_a = point_in_a(ctx, p);
int in_b = point_in_b(ctx, p);
if (ctx->wedge_only == 1) {
if ((in_a == 1) && (in_b == 1)) {
return 1;
}
} else {
if ((in_a == 1) || (in_b == 1)) {
return 1;
}
}
return 0;
}
int point_in_wedge(struct render_ctx *ctx, COMPLEX_T p) {
if ((point_in_a(ctx, p) == 1) && (point_in_b(ctx, p) == 1)) {
return 1;
} else {
return 0;
}
}
int point_in_box(struct render_ctx *ctx, COMPLEX_T p) {
double px = (double)(__real__ p);
double py = (double)(__imag__ p);
if ((px > ctx->xmin) && (px < ctx->xmax) &&
(py > ctx->ymin) && (py < ctx->ymax)) {
return 1;
} else {
return 0;
}
}
COMPLEX_T turn_a(struct render_ctx *ctx, COMPLEX_T p) {
COMPLEX_T np = p;
__real__ np += FLOAT_L(1.0);
np *= ctx->rot[0];
__real__ np -= FLOAT_L(1.0);
return np;
}
COMPLEX_T turn_b(struct render_ctx *ctx, COMPLEX_T p) {
COMPLEX_T np = p;
__real__ np -= FLOAT_L(1.0);
np *= ctx->rot[1];
__real__ np += FLOAT_L(1.0);
return np;
}
COMPLEX_T turn_n(struct render_ctx *ctx, COMPLEX_T p, int n) {
COMPLEX_T np = p;
if ((n & 1) == 0) {
/* an A turn */
__real__ np += FLOAT_L(1.0);
np *= ctx->rot[n];
__real__ np -= FLOAT_L(1.0);
} else {
/* a B turn */
__real__ np -= FLOAT_L(1.0);
np *= ctx->rot[n];
__real__ np += FLOAT_L(1.0);
}
return np;
}
double convolve_get_xy_val(double *source, int w, int h, double edge_val, int x, int y) {
if ((x < 0) || (x >= w) ||
(y < 0) || (y >= h)) {
return edge_val;
}
return source[y * w + x];
}
void convolve_sobel(double *source, double *mag, double *angle, int w, int h, double edge_val) {
double mh[3][3] = {{1.0, 0.0, -1.0}, {2.0, 0.0, -2.0}, {1.0, 0.0, -1.0}};
double mv[3][3] = {{1.0, 2.0, 1.0}, {0.0, 0.0, 0.0}, {-1.0, -2.0, -1.0}};
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
double sob_h = 0.0;
double sob_v = 0.0;
for (int yo = -1; yo <= 1; yo++) {
for (int xo = -1; xo <= 1; xo++) {
sob_h += mh[yo + 1][xo + 1] * convolve_get_xy_val(source, w, h, edge_val, x + xo, y + yo);
sob_v += mv[yo + 1][xo + 1] * convolve_get_xy_val(source, w, h, edge_val, x + xo, y + yo);
}
}
/* magnitude uses pythagorean theorem */
mag[y * w + x] = sqrt((sob_h * sob_h) + (sob_v * sob_v));
/* angle is atan of rise / run */
angle[y * w + x] = atan2(sob_v, sob_h);
}
}
}
double delta_log_order_to_val(double log_order, double min_order, double max_order) {
/* Only let exp() operate on positive logs */
int neg = 0;
if (log_order < 0) {
neg = 1;
log_order = fabs(log_order);
}
/* Undo log */
double order = exp(log_order);
/* Saturate order in case of minor roundoff issues */
if (order > max_order) {
order = max_order;
}
if (order < min_order) {
order = min_order;
}
/* atan -> val mapping */
double v = atan2(order - min_order, 1.0) / atan2(max_order - min_order, 1.0);
/* Saturate (shouldn't happen if atan2 behaves itself) */
if (v > 1.0) {
v = 1.0;
}
/* Flip val to [0, -1] if the log was negative */
if (neg == 1) {
v = 0.0 - v;
}
return v;
}
double log_order_to_val(double log_order, double min_order, double max_order, double stretch_exp) {
double log_min_order = log(min_order);
double log_max_order = log(max_order);
double offset = 1.0; /* required so log(x < 1) doesn't turn negative */
double v = ((log_order - log_min_order) + offset) / ((log_max_order - log_min_order) + offset);
/* This stretches out the color space where more colors are allocated
* to higher orders. An exponent of 1 is no stretch. 2.0 is a minor stretch.
*/
return 1.0 - pow((1.0 - v), stretch_exp);
}
double log_order_atan_to_val(double log_order, double log_min_order, double log_max_order, double stretch_exp) {
/* Saturate order in case of minor roundoff issues */
if (log_order > log_max_order) {
log_order = log_max_order;
}
if (log_order < log_min_order) {
log_order = log_min_order;
}
/* atan -> val mapping */
double v = atan2(log_order - log_min_order, 1.0) / atan2(log_max_order - log_min_order, 1.0);
/* Saturate (shouldn't happen if atan2 behaves itself) */
if (v > 1.0) {
v = 1.0;
}
return 1.0 - pow((1.0 - v), stretch_exp);
}
double log_order_scale_cycle_to_val(double log_order) {
double neg = 1;
if (log_order < 0) {
neg = -1;
log_order = fabs(log_order);
}
double v = fmod(log_order, log(10.0)); /* fractional part of log base 10 */
return v * neg;
}
void val_to_rgb(double val, uint8_t *R, uint8_t *G, uint8_t *B, double brightness) {
assert((brightness >= 0.0) && (brightness <= 1.0));
/* delta colors */
double vp2 = val * M_PI_2;
if (vp2 >= 0) {
*R = (uint8_t)round(sin(vp2) * 255.0 * brightness);
*G = (uint8_t)round((1.0 - sin(vp2 * 2.0)) * 255.0 * brightness);
*B = (uint8_t)round(cos(vp2) * 255.0 * brightness);
} else {
vp2 = 0.0 - vp2; /* flip to positive */
*R = (int)round((1.0 - sin(vp2)) * 255.0 * brightness);
*G = (int)round(sin(vp2 * 2.0) * 255.0 * brightness);
*B = (int)round((1.0 - cos(vp2)) * 255.0 * brightness);
}
}
double hsv_to_rgb_f(int n, double h, double s, double v) {
/* https://en.wikipedia.org/wiki/HSL_and_HSV */
double k = fmod((double)n + h * 6.0, 6.0);
double k_adj = fmax(0.0, fmin(fmin(k, 4 - k), 1.0));
return v - (v * s * k_adj);
}
void hsv_to_rgb(double h, double s, double v, uint8_t *R, uint8_t *G, uint8_t *B) {
*R = (uint8_t)round(hsv_to_rgb_f(5.0, h, s, v) * 255.0);
*G = (uint8_t)round(hsv_to_rgb_f(3.0, h, s, v) * 255.0);
*B = (uint8_t)round(hsv_to_rgb_f(1.0, h, s, v) * 255.0);
}
void val_to_rgb2(double val, uint8_t *R, uint8_t *G, uint8_t *B, double brightness) {
assert((brightness >= 0.0) && (brightness <= 1.0));
/* delta colors */
double vp2 = val * M_PI_2;
double h, s, v;
if (vp2 >= 0) {
h = 0;
} else {
vp2 = 0.0 - vp2; /* flip to positive */
h = 0.5;
}
s = 0.25 + 0.5 * sin(2.0 * vp2);
v = 0.75 + 0.25 * sin(vp2);
v *= brightness;
hsv_to_rgb(h, s, v, R, G, B);
}
void add_visited_xy(struct render_ctx *ctx, struct visited_ctx *vctx, int x, int y, int x_m, int y_m) {
if ((x >= 0) && (y >= 0)) {
uint32_t o = xy_to_offset(ctx, x, y);
if (vctx->visited[o] == 0) {
vctx->visited[o] = 1;
/* grow x/y array */
if (vctx->vused >= (vctx->vsize - 1)) {
vctx->vx = realloc(vctx->vx, (vctx->vsize + GROW_VISITED) * sizeof(uint32_t));
assert(vctx->vx != NULL);
vctx->vy = realloc(vctx->vy, (vctx->vsize + GROW_VISITED) * sizeof(uint32_t));
assert(vctx->vy != NULL);
vctx->vsize += GROW_VISITED;
}
vctx->vx[vctx->vused] = x;
vctx->vy[vctx->vused] = y;
vctx->vused += 1;
} else if (vctx->visited[o] < 32) {
/* Let visited counter grow as high as 32 per pixel per point sampled */
vctx->visited[o] =+ 1;
}
}
if ((x_m >= 0) && (y_m >= 0)) {
uint32_t o_m = xy_to_offset(ctx, x_m, y_m);
if (vctx->visited_m[o_m] == 0) {
vctx->visited_m[o_m] = 1;
/* grow x/y array */
if (vctx->vused_m >= (vctx->vsize_m - 1)) {
vctx->vx_m = realloc(vctx->vx_m, (vctx->vsize_m + GROW_VISITED) * sizeof(uint32_t));
assert(vctx->vx_m != NULL);
vctx->vy_m = realloc(vctx->vy_m, (vctx->vsize_m + GROW_VISITED) * sizeof(uint32_t));
assert(vctx->vy_m != NULL);
vctx->vsize_m += GROW_VISITED;
}
vctx->vx_m[vctx->vused_m] = x_m;
vctx->vy_m[vctx->vused_m] = y_m;
vctx->vused_m += 1;
} else if (vctx->visited_m[o_m] < 32) {
/* Let visited counter grow as high as 32 per pixel per point sampled */
vctx->visited_m[o_m] =+ 1;
}
}
}
double point_order(struct render_ctx *ctx, COMPLEX_T p, struct visited_ctx *vctx) {
if (point_in_puzzle(ctx, p) != 1) {
return 0;
}
int x, y, x_m, y_m;
COMPLEX_T op = p; /* Original p */
uint8_t step = 0;
uint32_t count = 0;
int32_t count_a = 0;
int32_t count_b = 0;
int stuck = 0;
do {
if (step == 0) {
/* Do stuck checking */
if (stuck == 1) {
fprintf(stderr, "point (%.15f, %.15f) stuck at count %d\n", (double)__real__ op, (double)__imag__ op, count);
return NAN;
}
stuck = 1;
/* Only track before a is done */
x = -1;
y = -1;
x_m = -1;
y_m = -1;
if (((ctx->wedge_only == 0) && (ctx->box_only == 0)) ||
((ctx->wedge_only == 1) && (point_in_wedge(ctx, p) == 1)) ||
((ctx->box_only == 1) && (point_in_box(ctx, p) == 1))) {
assert(point_to_xy(ctx, p, &x, &y) == 0);
}
if (ctx->sym180 == 1) {
COMPLEX_T p_m;
__real__ p_m = FLOAT_L(0.0) - __real__ p;
__imag__ p_m = FLOAT_L(0.0) - __imag__ p;
if (((ctx->wedge_only == 0) && (ctx->box_only == 0)) ||
((ctx->wedge_only == 1) && (point_in_wedge(ctx, p_m) == 1)) ||
((ctx->box_only == 1) && (point_in_box(ctx, p_m) == 1))) {
assert(point_to_xy(ctx, p_m, &x_m, &y_m) == 0);
}
}
/* Now add */
add_visited_xy(ctx, vctx, x, y, x_m, y_m);
}
/* Try to do turn */
if (point_in_n(ctx, p, step) == 1) {
count++;
stuck = 0;
p = turn_n(ctx, p, step);
if ((step & 1) == 0) {
count_a++;
} else {
count_b++;
}
}
/*step ^= 1;*/ /* toggle between a and b */
step = (step + 1) % ctx->gen_len;
if ((count > 0) && (count % 50000000 == 0)) {
fprintf(stderr, "Done %d turns\n", count);
}
/*
* Note the condition (step != 0) here is critical for
* correctness. There are sometimes points that cycle around
* back to themselves, but they return to their original
* position partway through the application of the generator.
* For A' B that means they end with a final A' turn (so the
* next turn would be B). This is different than how they
* started which was to start with the next turn (first turn)
* being A'.
*
* This has the effect of causing some points to be measured
* with two different orders depending on where they happen to
* be sampled in their orbit.
*
* For example with N = 12; R = sqrt(2): The point
* -0.199445936742669, 0.736391888282075 returns back to
* itself with the opposite turn parity from how it started.
* If the orbit is terminated at this step the order is 62
* turns. However if another point in the orbit is sampled:
* -0.210794173119580, 0.770061921590612 this point will cycle
* through the point -0.199445936742669, 0.736391888282075 but
* has an order of 216 turns.
*
* So if all the points in the orbit of either of these get
* labeled with their order, then the pixel containing
* -0.199445936742669, 0.736391888282075 can receive two
* different order values.
*
* By enforcing step != 0 to continue, the loop points must
* return back to their original spot with the same turn
* parity, and this causes the order to be measured the same
* no matter what point in the orbit is sampled.
*
* It took me nearly 20 hours over three days to find this
* bug.
*
*/
} while ((count < vctx->limit) && ((step != 0) || (count < ctx->n) || (point_equal_epsilon(ctx, op, p) != 1)));
uint64_t order_sum = count_a + count_b;
if (count < vctx->limit) {
uint64_t max_order = __atomic_load_n(&(ctx->highest_order), __ATOMIC_RELAXED);
if (order_sum > max_order) {
fprintf(stderr, "New max order of %lu found for point (%.15f, %.15f)\n", order_sum, (double)__real__ op, (double)__imag__ op);
}
/* Now write new order back into highest_seen atomically */
while ((order_sum > max_order) && (__atomic_compare_exchange_n(&(ctx->highest_order), &max_order, order_sum, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED) == true));
}
/* Figure out how to calculate order based on absolute or delta measure */
if (ctx->order_delta == 0) {
/* absolute order a + b stuff */
if (count >= vctx->limit) {
fprintf(stderr, "point (%.15f, %.15f) hit limit with order %lu\n", (double)__real__ op, (double)__imag__ op, order_sum);
/*return NAN;*/
}
if (order_sum == 0) {
return NAN;
}
return (log((double)(order_sum))); /* log(a + b) */
} else {
/* delta order a/b stuff */
if (count >= vctx->limit) {
/* Try to salvage this point if it's extremely close to 0 */
/* Within one loop around of each other */
if (abs(count_a - count_b) <= ctx->n) {
/*fprintf(stderr, "Salvaged point at limit\n");*/
return 0.0;
} else if (count > ORD_LIMIT) {
/*fprintf(stderr, "count over limit assuming 0\n");*/
return 0.0; /* Just assume they were equal as a speedup hack */
} else {
/*fprintf(stderr, "count over limit but not at max, returning NAN\n");*/
return NAN; /* We didn't try long enough to be sure */
}
}
if ((count_a > 0) && (count_b > 0)) {
/* int excess = (((count_a % ctx->n) - (count_b % ctx->n)) + ctx->n) % ctx->n; */
/* if (excess != 0) { */
/* fprintf(stderr, "point (%.15f, %.15f) with order %d with %d, %d\n", (double)__real__ op, (double)__imag__ op, count, count_a, count_b); */
/* } */
return (log((double)count_a) - log((double)count_b)); /* log(a/b) */
} else {
/* Check that if this is one of the the points in the
* center of a disk that can only be turned by that disk
* and aren't reached by the other disk.
*/
if (count_a == (int32_t)ctx->n) {
return log((double)count_a);
} else if (count_b == (int32_t)ctx->n) {
return 0.0 - log((double)count_b);
} else {
/*fprintf(stderr, "a or b was zero, returning NAN\n");*/
return NAN;
}
}
}
}
void point_sample(struct render_ctx *ctx, COMPLEX_T p, struct visited_ctx *vctx) {
if (point_in_puzzle(ctx, p) != 1) {
return;
}
if ((ctx->wedge_only == 1) && (point_in_wedge(ctx, p) != 1)) {
return;
}
if ((ctx->box_only == 1) && (point_in_box(ctx, p) != 1)) {
return;
}
/* Don't sample from bottom, mirror and sample from top instead */
/* if (__imag__ p < 0.0Q) { */
/* __real__ p = 0.0Q - __real__ p; */
/* __imag__ p = 0.0Q - __imag__ p; */
/* } */
/* Don't sample from wedge, rotate A by half and start there */
/* if (point_in_wedge(ctx, p) == 1) { */
/* __real__ p = 0.0Q - (2.0Q + __real__ p); */
/* __imag__ p = 0.0Q - __imag__ p; */
/* } */
vctx->vused = 0;
vctx->vused_m = 0;
double ord = point_order(ctx, p, vctx);
if (isnan(ord) == 0) {
int64_t scaled_ord = (int64_t)round(ord * (double)LOG_SCALE);
for (int i = 0; i < vctx->vused; i++) {
int o = xy_to_offset(ctx, vctx->vx[i], vctx->vy[i]);
__sync_add_and_fetch(&(ctx->grid[o].count), vctx->visited[o]);
__sync_add_and_fetch(&(ctx->grid[o].scaled_log_order), scaled_ord * vctx->visited[o]);
vctx->visited[o] = 0; /* clear this visit */
}
if (ctx->sym180 == 1) {
for (int i = 0; i < vctx->vused_m; i++) {
int o_m = xy_to_offset(ctx, vctx->vx_m[i], vctx->vy_m[i]);
__sync_add_and_fetch(&(ctx->grid[o_m].count), vctx->visited_m[o_m]);
if (ctx->order_delta == 0) {
__sync_add_and_fetch(&(ctx->grid[o_m].scaled_log_order), scaled_ord * vctx->visited_m[o_m]);
} else {
__sync_sub_and_fetch(&(ctx->grid[o_m].scaled_log_order), scaled_ord * vctx->visited_m[o_m]);
}
vctx->visited_m[o_m] = 0; /* clear this visit */
}
}
} else {
/* Gotta clear visited since we didn't loop */
memset(vctx->visited, 0, ctx->img_w * ctx->img_h * sizeof(uint8_t));
if (ctx->sym180 == 1) {
memset(vctx->visited_m, 0, ctx->img_w * ctx->img_h * sizeof(uint8_t));
}
}
}
void xy_sample(struct render_ctx *ctx, int x, int y, uint32_t n, uint32_t m, struct visited_ctx *vctx) {
int o = xy_to_offset(ctx, x, y);
for (uint32_t i = 0; i < n; i++) {
uint32_t gcount = __sync_add_and_fetch(&(ctx->grid[o].count), 0);
if (gcount < m) {
COMPLEX_T p = point_from_xy_rand(ctx, x, y);
point_sample(ctx, p, vctx);
} else {
break;
}
}
}
void image_aa_sobel(struct render_ctx *ctx) {
struct visited_ctx svctx;
struct visited_ctx *vctx = &svctx;
vctx->limit = ORD_LIMIT;
vctx->visited = calloc(ctx->img_w * ctx->img_h, sizeof(uint8_t));
assert(vctx->visited != NULL);
vctx->visited_m = calloc(ctx->img_w * ctx->img_h, sizeof(uint8_t));
assert(vctx->visited_m != NULL);
vctx->vx = malloc(GROW_VISITED * sizeof(uint32_t));
assert(vctx->vx != NULL);
vctx->vy = malloc(GROW_VISITED * sizeof(uint32_t));
assert(vctx->vy != NULL);
vctx->vx_m = malloc(GROW_VISITED * sizeof(uint32_t));
assert(vctx->vx_m != NULL);
vctx->vy_m = malloc(GROW_VISITED * sizeof(uint32_t));
assert(vctx->vy_m != NULL);
vctx->vused = 0;
vctx->vsize = GROW_VISITED;
vctx->vused_m = 0;
vctx->vsize_m = GROW_VISITED;