/
nconf.cpp
1807 lines (1527 loc) · 48.7 KB
/
nconf.cpp
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
/** New Conformist
Copyright (C) 2018 Zeno Rogue
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CAP_GD
#define CAP_GD 1
#endif
#ifndef CAP_DRAW
#define CAP_DRAW 1
#endif
#ifndef CAP_BMP
#define CAP_BMP (CAP_GD | CAP_DRAW)
#endif
#include "graph2.h"
#include <unistd.h>
#include <unordered_map>
#include <array>
#include <vector>
#include <map>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <complex>
namespace nconf {
using namespace graph2;
bool chessmap = false;
ipoint chesspos;
int zoomout = 1;
int lined_out;
typedef std::complex<ld> cld;
using std::isnan;
using std::vector;
using std::printf;
using std::string;
using std::pair;
using std::array;
using std::sort;
using std::make_pair;
using std::tie;
using std::tuple;
using std::min;
using std::max;
using std::make_tuple;
using std::queue;
int elim_order = 3;
ld spinspeed;
bool use_childsides = true;
bool view_error = false;
}
#include "mat.cpp"
#include "zebra.cpp"
namespace nconf {
int SX, SY;
// Side types.
// stype::standard is homeomorphic to disk
// stype::ring is homeomorphic to ring
// stype::fixed_ring is used for rings if we want to make the period divisible by the tesselation period
// stpe::fake is used for found regions which have more than 1 hole
enum class stype : int { standard, ring, fixed_ring, fake };
// Point types.
// For stype::standard, iside points are of type 'inside', and the boundary is: top, right_inf, bottom, left_inf.
// For stype::ring, the boundary is top (external) and bottom (internal), and the ring is split into three inside_* subtypes.
enum class ptype : char { outside, inside, inside_left_up, inside_left_down, top, bottom, left_inf, right_inf, marked };
bool inner(ptype t) { return int(t) > 0 && int(t) < 4; }
bool infinitary(ptype t) { return int(t) >= 6; }
bool inner_border(ptype t) { return t == ptype::inside || t == ptype::top || t == ptype::bottom; }
// the four directions
ipoint dv[4] = { ipoint(1, 0), ipoint(0, -1), ipoint(-1, 0), ipoint(0, 1) };
void resize_pt();
bool draw_progress = true;
bool text_progress = true;
#if CAP_GD
// the bitmap used for the shape (called 'heart' because heart was the first bitmap shape)
bitmap heart;
#endif
typedef pair<struct datapoint*, ld> equation;
// information about a pixel
struct datapoint {
cpoint x;
ptype type, baktype;
char state;
int side=0;
int pointorder;
ld bonus;
vector<equation> eqs;
};
template<class T> struct vector2 : vector<vector<T>> {
T& operator [] (ipoint i) { return (*this)[i.y][i.x]; }
vector<T>& operator [] (int i) { return (*(vector<vector<T>>*)this) [i]; }
// const T& operator [] const (ipoint i) { return arr[i.y][i.x]; }
void resize2(int X, int Y) { this->resize(Y); for(auto& row: *this) row.resize(X); }
};
typedef vector2<datapoint> pointmap;
pointmap pts;
// information about a side
struct sideinfo {
#if CAP_BMP
bitmap img;
vector<bitmap> img_band;
#endif
cpoint cscale; // cscale[0] says how we should rescale the X coordinates -- we should divide them by cscale[0]
ipoint inner_point;
ld xcenter;
ld period; // period in band units
ld period_unit; // period unit (changed e.g. with -zebra)
ld animshift; // add this to the X coordinates
vector<transmatrix> period_matrices;
stype type;
int id;
vector<int> childsides;
bool need_btd;
vector<transmatrix> matrixlist, rmatrixlist;
ld zero_shift;
pointmap* submap;
ipoint join;
int parentid, rootid;
transmatrix parentrel_matrix;
int parentrel_x;
#if CAP_BMP
vector<bitmap> img_line;
#endif
};
vector<sideinfo> sides;
sideinfo& rootof(const sideinfo& si) { return sides[si.rootid]; }
}
#include "btd.cpp"
namespace nconf {
ld scalex = 1, scaley = 1;
int marginx = 32, marginy = 32;
void resize_pt() {
pts.resize(SY);
for(int y=0; y<SY; y++) pts[y].resize(SX);
}
int current_side;
sideinfo& new_side(stype type) {
int N = size(sides);
sides.emplace_back();
auto& side = sides.back();
side.id = N;
side.type = type;
side.submap = &pts;
side.period_unit = 1;
side.parentid = N;
side.rootid = N;
side.animshift = 0;
return side;
}
sideinfo& single_side(stype type) {
sides.clear();
auto& side = new_side(type);
current_side = side.id;
return side;
}
sideinfo& create_side(stype type) {
auto& side = new_side(type);
current_side = side.id;
return side;
}
sideinfo& cside() { return sides[current_side]; }
sideinfo& csideroot() { return rootof(cside()); }
int sqr(int a) { return a*a; }
// create a rectangular shape
void createb_rectangle() {
single_side(stype::standard);
resize_pt();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
p.type = ptype::inside;
p.side = 0;
if(y == 0 || y == SY-1 || x == 0 || x == SX-1) {
if(y < SY/2) p.type = ptype::top;
else if(y > SY/2) p.type = ptype::bottom;
else if(x == 0) p.type = ptype::left_inf;
else p.type = ptype::right_inf;
}
}
}
// given the points supposed to become left_inf and right_inf, and
// the starting direction, split the rest of the boundary to
// top and bottom.
void split_boundary(pointmap& ptmap, ipoint axy, ipoint bxy, int d) {
ptmap[axy].type = ptype::left_inf;
ptype phase = ptype::bottom;
ptmap[bxy].type = ptype::right_inf;
bxy -= dv[d];
for(int iter=0; iter<100000000; iter++) {
d &= 3;
auto& pt2 = ptmap[bxy + dv[d]];
ptype nphase = ptype(int(phase)+2); // ugly
if(pt2.type == nphase || pt2.type == phase) d++;
else if(pt2.type == ptype::outside) { pt2.type = phase; d++; }
else if(infinitary(pt2.type)) {
if(phase == ptype::bottom) phase = ptype::top;
else break;
}
else if(pt2.type == ptype::inside) { bxy += dv[d]; d--; }
}
}
ld hypot(ipoint a) { return std::hypot(a.x, a.y); }
// find the point on the boundary nearest to cxy
vector<tuple<ipoint, int>> all_boundary(pointmap& ptmap) {
vector<tuple<ipoint, int>> res;
for(int y=1; y<SY-1; y++) for(int x=0; x<SX-1; x++) {
ipoint xy(x, y);
for(int d=0; d<4; d++)
if(ptmap[xy].type == ptype::outside && ptmap[xy + dv[d]].type == ptype::inside)
res.emplace_back(xy, d);
}
return res;
}
tuple<ipoint, int> boundary_point_near(pointmap& ptmap, ipoint cxy) {
ld bestdist = 1e8;
int ad = 0;
ipoint axy (0, 0);
for(int y=1; y<SY-1; y++) for(int x=0; x<SX-1; x++) {
ipoint xy(x, y);
for(int d=0; d<4; d++)
if(ptmap[xy].type == ptype::outside && ptmap[xy + dv[d]].type == ptype::inside) {
ld dist = hypot(xy-cxy);
if(dist < bestdist) bestdist = dist, axy = xy, ad = d;
}
}
printf("bestdist = %lf %d,%d .. %d,%d\n", double(bestdist), cxy.x, cxy.y, axy.x, axy.y);
return make_tuple(axy, ad);
}
#if CAP_BMP
// compute SX and SY based on heart
void set_SXY(bitmap& heart) {
int newSX = heart.s->w / scalex + marginx + marginx;
if(newSX < SX)
marginx = (SX - heart.s->w / scalex) / 2;
else SX = newSX;
int newSY = heart.s->h / scaley + marginy + marginy;
if(newSY < SY)
marginy = (SY - heart.s->h / scaley) / 2;
else SY = newSY;
resize_pt();
}
#endif
#if CAP_GD
void load_image_for_mapping(const string& fname) {
heart = readPng(fname);
errpixel = heart[0][0];
set_SXY(heart);
}
#endif
// create a circular shape
void createb_circle() {
single_side(stype::standard);
resize_pt();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
p.type = ptype::inside;
p.side = 0;
int u = sqr(2 * x - SX + 1) + sqr(2 * y - SY + 1);
if(u >= sqr(min(SY, SX) - 5))
p.type = ptype::outside;
}
auto [axy1, ad] = boundary_point_near(pts, {0, SY/2});
auto [bxy1, bd] = boundary_point_near(pts, {SX-1, SY/2});
printf("%d %d %d %d %d %d\n", axy1.x, axy1.y, ad, bxy1.x, bxy1.y, bd);
split_boundary(pts, axy1, bxy1, bd^2);
}
// trimming (obsolete)
int trim_x1 = 0, trim_y1 = 0, trim_x2 = 99999, trim_y2 = 99999;
void trim(int x1, int y1, int x2, int y2) {
trim_x1 = x1;
trim_y1 = y1;
trim_x2 = x2;
trim_y2 = y2;
}
// translate bitmap coordinates to internal coordinates (taking margin and scale into account)
ipoint unmargin(ipoint xy) {
return ipoint((xy.x-marginx)*scalex, (xy.y-marginy)*scaley);
}
ipoint addmargin(ipoint xy) {
return ipoint(xy.x/scalex+marginx, xy.y/scaley+marginy);
}
#if CAP_BMP
unsigned& get_heart(ipoint xy) {
return heart[unmargin(xy)];
}
#endif
// prepare a ring for mapping, including the given point
#if CAP_BMP
void createb_outer(ipoint cxy) {
create_side(stype::ring);
cside().inner_point = cxy;
auto inpixel = get_heart(cxy);
queue<ipoint> boundary;
while(cxy.x < SX && get_heart(cxy) == inpixel) cxy.x++;
if(cxy.x == SX) die("nothing on the line");
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
ipoint xy(x, y);
auto& p = pts[y][x];
p.baktype = p.type;
if(x == 0) {
p.type = ptype::top;
boundary.emplace(1, y);
}
else if(y == 0) {
p.type = ptype::top;
boundary.emplace(x, 1);
}
else if(x == SX-1) {
p.type = ptype::top;
boundary.emplace(x-1, 1);
}
else if(y == SY-1) {
p.type = ptype::top;
boundary.emplace(1, y-1);
}
else if(get_heart(xy) != inpixel)
p.type = ptype::bottom;
else {
p.side = current_side;
if(x > cxy.x)
p.type = ptype::inside;
else if(y < cxy.y)
p.type = ptype::inside_left_up;
else
p.type = ptype::inside_left_down;
}
}
while(!boundary.empty()) {
auto xy = boundary.front();
boundary.pop();
auto& p = pts[xy];
if(p.type != ptype::bottom) continue;
p.type = ptype::top;
for(int d=0; d<4; d++)
boundary.emplace(xy + dv[d]);
}
}
#endif
// prepare a stype::standard for mapping, including the given point
#if CAP_BMP
void createb_inner(ipoint axy, ipoint bxy) {
create_side(stype::standard);
cside().inner_point = axy;
auto inpixel = heart[axy];
printf("%x %x err %x\n", inpixel, heart[bxy], errpixel);
if(heart[bxy] != inpixel) die("both pixels should be in");
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto xy = ipoint(x, y);
auto& p = pts[xy];
int ax = (x-marginx)/scalex;
int ay = (y-marginy)/scaley;
bool trimmed = ax < trim_x1 || ax >= trim_x2 || ay < trim_y1 || ay >= trim_y2;
p.side = 0;
p.baktype = p.type;
if(get_heart(xy) == inpixel && x && y && x < SX-1 && y < SY-1 && !trimmed)
p.type = ptype::inside;
else
p.type = ptype::outside;
}
auto [axy1, ad] = boundary_point_near(pts, addmargin(axy));
auto [bxy1, bd] = boundary_point_near(pts, addmargin(bxy));
split_boundary(pts, axy1, bxy1, bd^2);
}
#endif
// create the Hilbert curve shape
void create_hilbert(int lev, int pix, int border) {
single_side(stype::standard);
SY = SX = pix << lev;
resize_pt();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++)
pts[y][x].side = 0,
pts[y][x].type = ptype::outside;
ipoint wxy(0, 0);
auto connection = [&] (int dir) {
for(int dy=(dir==1?-border:border); dy<(dir==3?pix+border:pix-border); dy++)
for(int dx=(dir==2?-border:border); dx<(dir==0?pix+border:pix-border); dx++)
pts[dy+wxy.y*pix][dx+wxy.x*pix].type = ptype::inside;
if(dir<4) wxy += dv[dir];
};
std::function<void(int,int,int)> hilbert_recursive = [&] (int maindir, int subdir, int l) {
if(l == 0) return;
hilbert_recursive(subdir, maindir, l-1);
connection(subdir);
hilbert_recursive(maindir, subdir, l-1);
connection(maindir);
hilbert_recursive(maindir, subdir, l-1);
connection(subdir^2);
hilbert_recursive(subdir^2, maindir^2, l-1);
};
pts[border-1][pix/2].type = ptype::left_inf;
hilbert_recursive(0, 3, lev);
connection(4);
split_boundary(pts, {pix/2, border-1}, {SX-1-pix/2, border-1}, 1);
}
void saveb(const string& s) {
FILE *f = fopen(s.c_str(), "wt");
printf("%d %d\n", SX, SY);
for(int y=0; y<SY; y++) {
for(int x=0; x<SX; x++)
fprintf(f, "%c", "X.-+TDLR" [int(pts[y][x].type)]);
fprintf(f, "\n");
}
fclose(f);
}
int mousex, mousey;
}
// 'unofficial' experiments that newconformist has been used for
#include "triangle.cpp"
#include "quincunx.cpp"
#include "spiral.cpp"
namespace nconf {
void klawisze();
bool paused, zoomed;
int zx, zy;
int itc(int a) {
return min(a, 255);
// if(a < 16) return a * 8;
// return min(128 + (a - 16) / 16, 255);
}
ld find_equation(vector<equation>& v, datapoint& p) {
auto seek = std::lower_bound(v.begin(), v.end(), equation{&p, -HUGE_VAL});
if(seek != v.end() && seek->first == &p) return seek->second;
return 0;
}
// draw the state of computation during mapping
#if CAP_DRAW
extern int video_out;
bool states_to_video;
string title = "conformist";
void drawstates(pointmap& ptmap) {
do {
if(!draw_progress) return;
initGraph(SX/zoomout, SY/zoomout, title, false);
SDL_WM_SetCaption(title.c_str(), 0);
int statecolors[4] = {
0x000080, 0x00FF00, 0x000000, 0x00FFFF };
auto& pt = zoomed ? ptmap[(mousey+zy)/4][(mousex+zx)/4] : ptmap[mousey][mousex];
// printf("eqs = %d\n", isize(pt.eqs));
for(int y=0; y<SY/zoomout; y++)
for(int x=0; x<SX/zoomout; x++) {
auto& p = zoomed ? ptmap[(y*zoomout+zy)/4][(x*zoomout+zx)/4] : ptmap[y*zoomout][x*zoomout];
screen[y][x] = statecolors[int(p.state)];
if(p.state == 0) switch(p.type) {
case ptype::top: screen[y][x] = 0xFFFFFF; break;
case ptype::bottom: screen[y][x] = 0xFF00FF; break;
default: ;
}
part(screen[y][x], 2) = itc(isize(p.eqs));
if(find_equation(pt.eqs, p)) part(screen[y][x], 2) = 0x80;
if(p.type == ptype::inside_left_up) part(screen[y][x], 0) |= 0x20;
if(p.type == ptype::inside_left_down) part(screen[y][x], 0) |= 0x40;
}
screen.draw();
if(states_to_video) {
for(int y=0; y<SY; y++)
write(video_out, &screen[y][0], 4 * SX);
}
SDL_Event event;
SDL_Delay(1);
int ev;
while((ev = SDL_PollEvent(&event))) switch (event.type) {
case SDL_QUIT:
exit(1);
return;
case SDL_MOUSEMOTION: {
mousex = event.motion.x;
mousey = event.motion.y;
break;
}
case SDL_KEYDOWN: {
int key = event.key.keysym.sym;
// int uni = event.key.keysym.unicode;
if(key == 'p') paused = !paused;
if(key == 'z') zx = mousex*zoomout*3, zy = mousey*zoomout*3, zoomed = !zoomed;
break;
}
}
} while(paused);
}
#endif
array<ipoint, 4> find_neighbors(pointmap& ptmap, ipoint xy) {
array<ipoint, 4> res;
for(int i=0; i<4; i++) res[i] = xy + dv[i];
int ax = xy.x, ay = xy.y;
if(elim_order == 3) {
if((ax+ay) & 1) { ptmap[xy].pointorder = 1000; return res; }
tie(ax, ay) = make_pair(ax+ay + (1<<16), ax-ay + (1<<16));
}
int axv = 0, ayv = 0;
while(!(ax&1)) ax >>= 1, axv++;
while(!(ay&1)) ay >>= 1, ayv++;
if(elim_order == 0 || elim_order == 3)
ptmap[xy].pointorder = 2000 + max(axv, ayv) * 1000 + (axv>ayv ? 500 : 0) + min(axv, ayv);
if(elim_order == 1)
ptmap[xy].pointorder = xy.x + xy.y;
if(elim_order == 2)
ptmap[xy].pointorder = xy.x + xy.y + ((xy.x ^ xy.y) & 1 ? 2000 : 0);
return res;
}
// compute the mapping, after every pixel/datapoint has been given a type
bool pretty_borders = false;
vector<ipoint> allpoints;
void build_equations(pointmap& ptmap, int i, bool fast) {
printf("Building eqs, i=%d\n", i);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = ptmap[y][x];
p.state = 0;
if(pretty_borders && i == 0 ? !inner_border(p.type) : !inner(p.type)) continue;
p.state = 1;
p.eqs.clear();
p.bonus = 0;
p.x[i] = 0;
p.bonus = 0;
ipoint xy(x, y);
auto nei = find_neighbors(ptmap, xy);
if(i == 0) {
int xp = 0;
for(auto np: nei) {
auto t = ptmap[np].type;
if(inner(t) || infinitary(t) || (pretty_borders && inner_border(t))) xp++;
}
for(auto np: nei) {
auto& p2 = ptmap[np];
if(p2.type == ptype::left_inf) p.bonus += 0;
else if(p2.type == ptype::right_inf) p.bonus += 1./xp;
else if(pretty_borders ? inner_border(p2.type) : inner(p2.type)) {
p.eqs.emplace_back(&p2, 1./xp);
if(p.type == ptype::inside_left_up && p2.type == ptype::inside_left_down) p.bonus += 1./xp;
if(p.type == ptype::inside_left_down && p2.type == ptype::inside_left_up) p.bonus -= 1./xp;
}
}
}
if(i == 1) {
for(auto np: nei) {
auto& p2 = ptmap[np];
if(p2.type == ptype::top) p.bonus += 0;
else if(p2.type == ptype::bottom) p.bonus += 1./4;
else if(infinitary(p2.type)) p.bonus += 1./8;
else {
p.eqs.emplace_back(&p2, 1./4);
}
}
}
sort(p.eqs.begin(), p.eqs.end());
}
allpoints.clear();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) if(ptmap[y][x].state == 1) allpoints.push_back({x, y});
if(fast) sort(allpoints.begin(), allpoints.end(), [&ptmap] (auto p1, auto p2) { return ptmap[p1].pointorder < ptmap[p2].pointorder; });
}
void eliminate(pointmap& ptmap, ipoint co) {
auto &p = ptmap[co];
ld self = find_equation(p.eqs, p);
if(self) {
if(self == 1) {
printf("Variable eliminated at (%d,%d)\n", co.x, co.y);
p.state = 3;
}
else {
ld fac = 1 / (1 - self);
auto b = p.eqs.begin();
for(auto &pa: p.eqs) if(pa.first != &p) *(b++) = {pa.first, pa.second * fac};
p.eqs.resize(b - p.eqs.begin());
p.bonus *= fac;
}
}
for(auto& pa: p.eqs) {
auto& p2 = *pa.first;
ld mirror = find_equation(p2.eqs, p);
if(!mirror) continue;
p2.bonus += p.bonus * mirror;
vector<equation> new_equations;
new_equations.reserve(max(isize(p2.eqs), isize(p.eqs)) + 4);
auto old = p2.eqs.begin();
auto extra = p.eqs.begin();
while(old != p2.eqs.end() && extra != p.eqs.end())
if(old->first < extra->first) {
if(old->first == &p) old++;
else new_equations.push_back(*old), old++;
}
else if(old->first > extra->first)
new_equations.emplace_back(extra->first, extra->second * mirror), extra++;
else
new_equations.emplace_back(old->first, old->second + extra->second * mirror), old++, extra++;
while(old != p2.eqs.end()) {
if(old->first == &p) old++;
else new_equations.push_back(*old), old++;
}
while(extra != p.eqs.end())
new_equations.emplace_back(extra->first, extra->second * mirror), extra++;
p2.eqs = std::move(new_equations);
}
p.state = 2;
}
void retrieve(pointmap& ptmap, int i) {
printf("Solution retrieval\n");
reverse(allpoints.begin(), allpoints.end());
for(auto co: allpoints) {
auto &p = ptmap[co];
if(p.state != 2) continue;
p.x[i] = p.bonus;
for(auto& pa: p.eqs) p.x[i] += pa.second * pa.first->x[i];
p.eqs = vector<equation> ();
}
allpoints.clear();
}
int draw_each = 100;
void computemap(pointmap& ptmap) {
string t = title;
for(int i=0; i<2; i++) {
if(i == 0) title = t + " solve for X";
if(i == 1) title = t + " solve for Y";
build_equations(ptmap, i, true);
int lastt = SDL_GetTicks();
printf("Gaussian elimination\n");
int lastpct = -1, citer = 0;
for(auto co: allpoints) {
auto &p = ptmap[co];
if(p.state != 1) continue;
if(text_progress || draw_progress) {
int cpct = citer * 1000 / size(allpoints);
if(cpct != lastpct) {
lastpct = cpct;
if(text_progress) printf(" %d/1000 [%d]\n", cpct, isize(p.eqs));
#if CAP_DRAW
int nextt = SDL_GetTicks();
if(nextt > lastt + draw_each) {
drawstates(ptmap);
lastt = SDL_GetTicks();
}
#endif
}
}
citer++;
eliminate(ptmap, co);
}
retrieve(ptmap, i);
printf("Done.\n");
}
title = t;
}
template<class T> void save(FILE *f, const T& x) {
fwrite(&x, sizeof(T), 1, f);
}
template<class T> void load(FILE *f, T& x) {
fread(&x, sizeof(T), 1, f);
}
void savemap(const string& fname) {
FILE *f = fopen(fname.c_str(), "wb");
if(!f) pdie("savemap");
fwrite(&SX, sizeof(SX), 1, f);
fwrite(&SY, sizeof(SY), 1, f);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
save(f, p.x);
int t = int(p.type);
save(f, t);
}
fclose(f);
}
void merge_sides() {
if(current_side >= 0) {
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto xy = ipoint(x, y);
auto& p = pts[xy];
if(!inner(p.type))
p.type = p.baktype;
}
}
}
void loadmap(const string& fname) {
auto& side = single_side(stype::standard);
FILE *f = fopen(fname.c_str(), "rb");
if(!f) pdie("loadmap");
fread(&SX, sizeof(SX), 1, f);
fread(&SY, sizeof(SY), 1, f);
resize_pt();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
load(f, p.x);
int t; load(f, t); p.type = ptype(t);
if(p.type == ptype::inside_left_up) side.type = stype::ring;
p.side = 0;
}
fclose(f);
}
void loadmap2(const string& fname) {
auto& side = new_side(stype::standard);
FILE *f = fopen(fname.c_str(), "rb");
if(!f) pdie("loadmap2");
int iSX, iSY;
fread(&iSX, sizeof(iSX), 1, f);
fread(&iSY, sizeof(iSY), 1, f);
if(iSX != SX || iSY != SY) die("map size mismatch\n");
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
datapoint dp;
load(f, dp.x);
int t; load(f, t); dp.type = ptype(t);
if(inner(dp.type)) {
dp.side = side.id;
pts[y][x] = dp;
if(dp.type == ptype::inside_left_up) side.type = stype::ring;
}
}
fclose(f);
current_side = side.id;
}
void loadmap_join(const string& fname, ipoint xy) {
FILE *f = fopen(fname.c_str(), "rb");
if(!f) pdie("loadmap_join");
auto& side = new_side(stype::standard);
side.parentid = current_side;
side.rootid = cside().rootid;
cside().childsides.push_back(side.id);
side.submap = new pointmap;
side.join = addmargin(xy);
auto &epts = *side.submap;
int iSX, iSY;
fread(&iSX, sizeof(iSX), 1, f);
fread(&iSY, sizeof(iSY), 1, f);
if(iSX != SX || iSY != SY) die("map size mismatch\n");
epts.resize2(SX, SY);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = epts[y][x];
load(f, p.x);
int t; load(f, t); p.type = ptype(t);
p.side = side.id;
}
fclose(f);
current_side = side.id;
}
// how should be linearly transform the current harmonic mapping to make it conformal
// ([1] should be 0 and is ignored, we only have to scale the x coordinate by multiplying
// by get_conformity(...)[0]))
cpoint get_conformity(int x, int y, sideinfo& side) {
auto& gpts = *side.submap;
auto vzero = gpts[y][x].x;
array<cpoint, 2> v = {gpts[y][x+1].x - vzero, gpts[y+1][x].x - vzero };
ld det = v[0] ^ v[1];
cpoint ba2 = cpoint{v[1][1], -v[0][1]} / det;
cpoint ca2 = cpoint{-v[1][0], v[0][0]} / det;
ld bad = (ca2|ba2) / (ba2|ba2);
ld good = (ca2^ba2) / (ba2|ba2);
return cpoint{good, bad};
}
bool mark_sides, no_images;
int notypeside = 0xFFD500;
int boundary_color = 0x000000;
int bbnd = 100;
ld intdif(ld z) {
z = z - floor(z);
if(z > .5) z = 1-z;
return z;
}
// the following routines are for -viewerror
ld am[2][2];
cpoint diskpoint(int x, int y) {
cpoint c = pts[y][x].x;
auto [vx,vy] = unband(c, sides[0], -sides[0].xcenter);
hyperpoint p = equirectangular(vx, vy);
cpoint pt = hyper_to_disk(p);
return pt;
}
ld max_error;
void compute_am() {
cpoint sum_x = {0, 0};
cpoint sum_y = {0, 0};
cpoint sum_xx = {0, 0};
cpoint sum_xy = {0, 0};
cpoint sum_yy = {0, 0};
int n = 0;
for(int cy=0; cy<SY; cy++)
for(int cx=0; cx<SX; cx++) {
auto& p = pts[cy][cx];
if(p.type != ptype::inside) continue;
auto y = diskpoint(cx, cy);
if(isnan(y[0]) || isnan(y[1]) || std::hypot(y[0], y[1]) > .9) continue;
cpoint x = { ld(cx), ld(cy) };
for(int i=0; i<2; i++) {
sum_x[i] += x[i];
sum_xx[i] += x[i] * x[i];
sum_y[i] += y[i];
sum_yy[i] += y[i] * y[i];
sum_xy[i] += x[i] * y[i];
}
n++;
}
/*for(int a = 0; a < 5; a ++) {
cpoint x = { a, a };
cpoint y = { 2*a+3, 2*a+3 };
for(int i=0; i<2; i++) {