forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goldberg.cpp
1471 lines (1298 loc) · 43.1 KB
/
goldberg.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
// Hyperbolic Rogue -- Goldberg-Coxeter construction
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file goldberg.cpp
* \brief Goldberg-Coxeter construction
*
* This is generally not used for standard pure and bitruncated tilings, even though they are technically Goldberg too.
*/
#include "hyper.h"
namespace hr {
#if HDR
struct hrmap;
extern hrmap *currentmap;
#endif
EX namespace gp {
#if HDR
struct loc : pair<int, int> {
loc() {}
loc(int x, int y) : pair<int,int> (x,y) {}
loc operator+(loc e2) {
return loc(first+e2.first, second+e2.second);
}
loc operator-(loc e2) {
return loc(first-e2.first, second-e2.second);
}
loc operator*(loc e2) {
return loc(first*e2.first-second*e2.second,
first*e2.second + e2.first*second + (S3 == 3 ? second*e2.second : 0));
}
loc operator*(int i) {
return loc(first*i, second*i);
}
int operator %(int i) {
return gmod(first, i) + gmod(second, i);
}
loc operator /(int i) {
return loc(first/i, second/i);
}
loc conj() {
if(S3 == 4) return loc(first, -second);
return loc(first+second, -second);
}
};
struct local_info {
int last_dir;
loc relative;
int first_dir;
int total_dir;
};
#endif
EX local_info current_li;
EX cell *li_for;
EX loc eudir(int d) {
if(S3 == 3) {
d %= 6; if (d < 0) d += 6;
switch(d) {
case 0: return loc(1, 0);
case 1: return loc(0, 1);
case 2: return loc(-1, 1);
case 3: return loc(-1, 0);
case 4: return loc(0, -1);
case 5: return loc(1, -1);
default: return loc(0, 0);
}
}
else switch(d&3) {
case 0: return loc(1, 0);
case 1: return loc(0, 1);
case 2: return loc(-1, 0);
case 3: return loc(0, -1);
default: return loc(0, 0);
}
}
EX int length(loc p) {
return euc::dist(p.first, p.second);
}
#if CAP_GP
EX loc param = loc(1, 0);
EX hyperpoint next;
struct goldberg_mapping_t {
cellwalker cw;
signed char rdir;
signed char mindir;
loc start;
transmatrix adjm;
};
EX int fixg6(int x) { return gmod(x, SG6); }
const int GOLDBERG_LIMIT_HALF = GOLDBERG_LIMIT/2;
const int GOLDBERG_MASK_HALF = GOLDBERG_MASK/2;
EX int get_code(const local_info& li) {
return
((li.relative.first & GOLDBERG_MASK_HALF) << 0) +
((li.relative.second & GOLDBERG_MASK_HALF) << (GOLDBERG_BITS-1)) +
((fixg6(li.total_dir)) << (2*GOLDBERG_BITS-2)) +
((li.last_dir & 15) << (2*GOLDBERG_BITS+2));
}
EX local_info get_local_info(cell *c) {
if(INVERSE) {
c = get_mapped(c);
return UIU(get_local_info(c));
}
local_info li;
if(c == c->master->c7) {
li.relative = loc(0,0);
li.first_dir = -1;
li.last_dir = -1;
li.total_dir = -1;
}
else {
vector<int> dirs;
while(c != c->master->c7) {
dirs.push_back(c->c.spin(0));
c = c->move(0);
}
li.first_dir = dirs[0];
li.last_dir = dirs.back();
loc at(0,0);
int dir = 0;
at = at + eudir(dir);
dirs.pop_back();
while(dirs.size()) {
dir += dirs.back() + SG3;
dirs.pop_back();
at = at + eudir(dir);
}
li.relative = at;
li.total_dir = dir + SG3;
}
return li;
}
EX int last_dir(cell *c) {
return get_local_info(c).last_dir;
}
EX loc get_coord(cell *c) {
return get_local_info(c).relative;
}
EX int pseudohept_val(cell *c) {
loc v = get_coord(c);
return gmod(v.first - v.second, 3);
}
// mapping of the local equilateral triangle
// goldberg_map[y][x].cw is the cellwalker in this triangle at position (x,y)
// facing local direction 0
goldberg_mapping_t goldberg_map[GOLDBERG_LIMIT][GOLDBERG_LIMIT];
void clear_mapping() {
for(int y=0; y<GOLDBERG_LIMIT; y++) for(int x=0; x<GOLDBERG_LIMIT; x++) {
goldberg_map[y][x].cw.at = NULL;
goldberg_map[y][x].rdir = -1;
goldberg_map[y][x].mindir = 0;
}
}
goldberg_mapping_t& get_mapping(loc c) {
return goldberg_map[c.second&GOLDBERG_MASK][c.first&GOLDBERG_MASK];
}
int spawn;
cell*& peek(cellwalker cw) {
return cw.at->move(cw.spin);
}
cellwalker get_localwalk(const goldberg_mapping_t& wc, int dir) {
if(dir < wc.mindir) dir += SG6;
if(dir >= wc.mindir + SG6) dir -= SG6;
return wc.cw + dir;
}
void set_localwalk(goldberg_mapping_t& wc, int dir, const cellwalker& cw) {
if(dir < wc.mindir) dir += SG6;
if(dir >= wc.mindir + SG6) dir -= SG6;
wc.cw = cw - dir;
}
bool pull(loc at, int dir) {
auto& wc = get_mapping(at);
auto at1 = at + eudir(dir);
int dir1 = fixg6(dir+SG3);
cellwalker wcw = get_localwalk(wc, dir);
auto& wc1= get_mapping(at1);
if(wc1.cw.at) {
if(peek(wcw)) {
auto wcw1 = get_localwalk(wc1, dir1);
if(wcw + wstep != wcw1) {
DEBB(DF_GP, (at1, " : ", (wcw+wstep), " / ", wcw1, " (pull error from ", at, " :: ", wcw, ")") );
exit(1);
}
if(do_adjm) wc1.adjm = wc.adjm * get_adj(wcw.at, wcw.spin);
}
return false;
}
if(peek(wcw)) {
set_localwalk(wc1, dir1, wcw + wstep);
DEBB(DF_GP, (at1, " :", wcw+wstep, " (pulled from ", at, " :: ", wcw, ")"));
if(do_adjm) wc1.adjm = wc.adjm * get_adj(wcw.at, wcw.spin);
return true;
}
return false;
}
EX bool do_adjm;
void conn1(loc at, int dir, int dir1) {
auto& wc = get_mapping(at);
auto wcw = get_localwalk(wc, dir);
auto& wc1 = get_mapping(at + eudir(dir));
DEBB0(DF_GP, (hr::format(" md:%02d s:%d", wc.mindir, wc.cw.spin)); )
DEBB0(DF_GP, (" connection ", at, "/", dir, " ", wc.cw+dir, "=", wcw, " ~ ", at+eudir(dir), "/", dir1, " "); )
if(!wc1.cw.at) {
wc1.start = wc.start;
if(peek(wcw)) {
DEBB0(DF_GP, (" (pulled) "); )
set_localwalk(wc1, dir1, wcw + wstep);
if(do_adjm) wc1.adjm = wc.adjm * get_adj(wcw.at, wcw.spin);
}
else {
peek(wcw) = newCell(SG6, wc.cw.at->master);
wcw.at->c.setspin(wcw.spin, 0, false);
set_localwalk(wc1, dir1, wcw + wstep);
if(do_adjm) wc1.adjm = wc.adjm;
spawn++;
DEBB0(DF_GP, (" (created) "); )
}
}
DEBB0(DF_GP, (wc1.cw+dir1, " "));
auto wcw1 = get_localwalk(wc1, dir1);
if(peek(wcw)) {
if(wcw+wstep != wcw1) {
DEBB(DF_GP, ("FAIL: ", wcw, " connected to ", wcw+wstep, " not to ", wcw1); exit(1); )
}
else {
DEBB(DF_GP, ("(was there)"));
}
}
else {
DEBB(DF_GP, ("ok"));
peek(wcw) = wcw1.at;
wcw.at->c.setspin(wcw.spin, wcw1.spin, wcw.mirrored != wcw1.mirrored);
if(wcw+wstep != wcw1) {
DEBB(DF_GP | DF_ERROR, ("assertion failed"));
exit(1);
}
}
if(do_adjm) {
get_adj(wcw.at, wcw.spin) = inverse(wc.adjm) * wc1.adjm;
get_adj(wcw1.at, wcw1.spin) = inverse(wc1.adjm) * wc.adjm;
if(geom3::flipped) gp_swapped.emplace(wcw.at, wcw.spin);
if(geom3::flipped) gp_swapped.emplace(wcw1.at, wcw1.spin);
}
}
void conn(loc at, int dir) {
conn1(at, fixg6(dir), fixg6(dir+SG3));
conn1(at + eudir(dir), fixg6(dir+SG3), fixg6(dir));
}
EX map<pair<cell*, int>, transmatrix> gp_adj;
EX set<pair<cell*, int>> gp_swapped;
EX transmatrix& get_adj(cell *c, int i) { return gp_adj[make_pair(c,i)]; }
goldberg_mapping_t& set_heptspin(loc at, heptspin hs) {
auto& ac0 = get_mapping(at);
ac0.cw = cellwalker(hs.at->c7, hs.spin, hs.mirrored);
ac0.start = at;
DEBB(DF_GP, (at, " : ", ac0.cw));
return ac0;
}
EX void extend_map(cell *c, int d) {
DEBB(DF_GP, ("EXTEND ",c, " ", d));
indenter ind(2);
if(c->master->c7 != c) {
auto c1 = c;
auto d1 = d;
while(c->master->c7 != c) {
DEBB(DF_GP, (c, " direction 0 corresponds to ", c->move(0), " direction ", c->c.spin(0)); )
d = c->c.spin(0);
c = c->move(0);
}
// c move 0 equals c' move spin(0)
extend_map(c, d);
extend_map(c, c->c.fix(d-1));
extend_map(c, c->c.fix(d+1));
if(S3 == 4 && !c1->move(d1)) {
for(int i=0; i<S7; i++)
for(int j=0; j<S7; j++)
extend_map(createStep(c->master, i)->c7, j);
}
if(S3 == 4 && !c1->move(d1)) {
for(int i=0; i<S7; i++)
for(int i1=0; i1<S7; i1++)
for(int j=0; j<S7; j++)
extend_map(createStep(createStep(c->master, i), i1)->c7, j);
}
return;
}
if(S3 == 4 && param.first <= param.second) { d--; if(d<0) d += S7; }
clear_mapping();
// we generate a local map from an Euclidean grid to the
// hyperbolic grid we build.
// we fill the equilateral triangle with the following vertices:
loc vc[4];
vc[0] = loc(0,0);
vc[1] = param;
if(S3 == 3)
vc[2] = param * loc(0,1);
else
vc[2] = param * loc(1,1),
vc[3] = param * loc(0,1);
heptspin hs(c->master, d, false);
auto& ac0 = set_heptspin(vc[0], hs);
ac0.mindir = -1;
auto& ac1 = set_heptspin(vc[1], hs + wstep - SG3);
ac1.mindir = 0;
auto& ac2 = set_heptspin(vc[S3-1], S3 == 3 ? hs + 1 + wstep - 4 : hs + 1 + wstep + 1);
ac2.mindir = S3 == 3 ? 1 : -2;
if(S3 == 4) {
set_heptspin(vc[2], hs + wstep - 1 + wstep + 1).mindir = -3;
}
do_adjm = quotient || sphere;
if(do_adjm) {
auto m = (hrmap_standard*)currentmap;
get_mapping(vc[0]).adjm = Id;
get_mapping(vc[1]).adjm = m->adj(c->master, d);
get_mapping(vc[S3-1]).adjm = m->adj(c->master, (d+1)%c->master->type);
if(S3 == 4) {
heptspin hs1 = hs + wstep - 1;
get_mapping(vc[2]).adjm = m->adj(c->master, d) * m->adj(hs1.at, hs1.spin);
}
}
auto fix_mirrors = [&] {
if(ac1.cw.mirrored != hs.mirrored) ac1.cw--;
if(ac2.cw.mirrored != hs.mirrored) ac2.cw--;
if(S3 == 4) {
auto& ac3 = get_mapping(vc[2]);
if(ac3.cw.mirrored != hs.mirrored) ac3.cw--;
}
};
if(S3 == 4 && param == loc(1,1)) {
fix_mirrors();
conn(loc(0,0), 1);
conn(loc(0,1), 0);
conn(loc(0,1), 1);
conn(loc(0,1), 2);
conn(loc(0,1), 3);
return;
}
if(S3 == 4 && param.first == param.second && nonorientable) {
fix_mirrors();
int size = param.first;
// go along the boundary of the 'diamond'
for(int dir=0; dir<4; dir++) {
int dir_orth = (dir+1) & 3;
loc at = vc[dir];
for(int i=0; i<size; i++) {
if(!pull(at, dir)) break;
at = at + eudir(dir);
if(!pull(at, dir_orth)) break;
at = at + eudir(dir_orth);
}
}
// build the skeleton
for(int dir=0; dir<4; dir++) {
int dir_orth = (dir+1) & 3;
for(int i=0; i<size; i++) {
conn(vc[dir] + eudir(dir_orth) * i, dir_orth);
}
}
// fill everything
for(int y=0; y<2*size; y++) {
int xdist = min(y, 2*size-y);
for(int x=0; x<xdist; x++)
for(int d=0; d<4; d++) {
conn(loc(x, y), d);
conn(loc(-x, y), d);
}
}
return;
}
if(nonorientable && param.first == param.second) {
int x = param.first;
fix_mirrors();
for(int d=0; d<3; d++) for(int k=0; k<3; k++)
for(int i=0; i<x; i++) {
int dd = (2*d+k);
loc cx = vc[d] + eudir(dd) * i;
if(!pull(cx, dd)) break;
}
for(int i=0; i<=2*x; i++)
for(int d=0; d<3; d++) {
loc cx = vc[d] + eudir(1+2*d) * i;
if(i < 2*x) conn(cx, 1+2*d);
int jmax = x-i, drev = 2*d;
if(jmax < 0) drev += 3, jmax = -jmax;
for(int j=0; j<jmax; j++) {
loc cy = cx + eudir(drev) * j;
conn(cy, drev);
conn(cy, drev+1);
conn(cy, drev+2);
}
}
return;
}
// then we set the edges of our big equilateral triangle (in a symmetric way)
for(int i=0; i<S3; i++) {
loc start = vc[i];
loc end = vc[(i+1)%S3];
DEBB(DF_GP, ("from ", start, " to ", end); )
loc rel = param;
auto build = [&] (loc& at, int dx, bool forward) {
int dx1 = dx + SG2*i;
DEBB(DF_GP, (at, " .. ", make_pair(at + eudir(dx1), fixg6(dx1+SG3))));
conn(at, dx1);
if(forward) get_mapping(at).rdir = fixg6(dx1);
else get_mapping(at+eudir(dx1)).rdir = fixg6(dx1+SG3);
at = at + eudir(dx1);
};
while(rel.first >= 2 && (S3 == 3 ? rel.first >= 2 - rel.second : true)) {
build(start, 0, true);
build(end, SG3, false);
rel.first -= 2;
}
while(rel.second >= 2) {
build(start, 1, true);
build(end, 1+SG3, false);
rel.second -= 2;
}
while(rel.second <= -2 && S3 == 3) {
build(start, 5, true);
build(end, 2, false);
rel.second += 2;
rel.first -= 2;
}
if(S3 == 3) while((rel.first>0 && rel.second > 0) | (rel.first > 1 && rel.second < 0)) {
build(start, 0, true);
build(end, 3, false);
rel.first -= 2;
}
if(S3 == 4 && rel == loc(1,1)) {
if(param == loc(3,1) || param == loc(5,1)) {
build(start, 1, true);
build(end, 2, false);
rel.first--;
rel.second--;
}
else {
build(start, 0, true);
build(end, 3, false);
rel.first--;
rel.second--;
}
}
for(int k=0; k<SG6; k++)
if(start + eudir(k+SG2*i) == end)
build(start, k, true);
if(start != end) { DEBB(DF_GP | DF_ERROR, ("assertion failed: start ", start, " == end ", end)); exit(1); }
}
// now we can fill the interior of our big equilateral triangle
loc at = vc[0];
int maxstep = 3000;
while(true) {
maxstep--; if(maxstep < 0) { DEBB(DF_GP | DF_ERROR, ("maxstep exceeded")); exit(1); }
auto& wc = get_mapping(at);
int dx = wc.rdir;
auto at1 = at + eudir(dx);
auto& wc1 = get_mapping(at1);
DEBB(DF_GP, (make_pair(at, dx), " ", make_pair(at1, wc1.rdir)));
int df = wc1.rdir - dx;
if(df < 0) df += SG6;
if(df == SG3) break;
if(S3 == 3) switch(df) {
case 0:
case 4:
case 5:
at = at1;
continue;
case 2: {
conn(at, dx+1);
wc.rdir = (dx+1) % 6;
break;
}
case 1: {
auto at2 = at + eudir(dx+1);
auto& wc2 = get_mapping(at2);
if(wc2.cw.at) { at = at1; continue; }
wc.rdir = (dx+1) % 6;
conn(at, (dx+1) % 6);
conn(at1, (dx+2) % 6);
conn(at2, (dx+0) % 6);
wc1.rdir = -1;
wc2.rdir = dx;
break;
}
default:
println(hlog, "case unhandled ", df);
exit(1);
}
else switch(df) {
case 0:
case 3:
at = at1;
continue;
case 1:
auto at2 = at + eudir(dx+1);
auto& wc2 = get_mapping(at2);
if(wc2.cw.at) {
auto at3 = at1 + eudir(wc1.rdir);
auto& wc3 = get_mapping(at3);
auto at4 = at3 + eudir(wc3.rdir);
if(at4 == at2) {
wc.rdir = (dx+1)%4;
wc1.rdir = -1;
wc3.rdir = -1;
conn(at, (dx+1)%4);
}
else {
at = at1;
}
}
else {
wc.rdir = (dx+1)%4;
wc1.rdir = -1;
wc2.rdir = dx%4;
int bdir = -1;
int bdist = 100;
for(int d=0; d<4; d++) {
auto &wcm = get_mapping(at2 + eudir(d));
if(wcm.cw.at && length(wcm.start - at2) < bdist)
bdist = length(wcm.start - at2), bdir = d;
}
if(bdir != -1) conn(at2 + eudir(bdir), bdir ^ 2);
conn(at, (dx+1)%4);
conn(at2, dx%4);
at = param * loc(1,0) + at * loc(0, 1);
}
break;
}
}
DEBB(DF_GP, ("DONE"))
}
EX hyperpoint loctoh_ort(loc at) {
return point3(at.first, at.second, 1);
}
hyperpoint corner_coords6[7] = {
point3(2, -1, 0),
point3(1, 1, 0),
point3(-1, 2, 0),
point3(-2, 1, 0),
point3(-1, -1, 0),
point3(1, -2, 0),
point3(0, 0, 0) // center, not a corner
};
hyperpoint corner_coords4[7] = {
point3(1.5, -1.5, 0),
// point3(1, 0, 0),
point3(1.5, 1.5, 0),
// point3(0, 1, 0),
point3(-1.5, 1.5, 0),
// point3(-1, 0, 0),
point3(-1.5, -1.5, 0),
// point3(0, -1, 0),
point3(0, 0, 0),
point3(0, 0, 0),
point3(0, 0, 0)
};
#define corner_coords (S3==3 ? corner_coords6 : corner_coords4)
hyperpoint cornmul(const transmatrix& corners, const hyperpoint& c) {
if(sphere && S3 == 3) {
ld cmin = c[0] * c[1] * c[2] * (6 - S7);
return corners * point3(c[0] + cmin, c[1] + cmin, c[2] + cmin);
}
else return corners * c;
}
hyperpoint atz(const transmatrix& T, const transmatrix& corners, loc at, int cornerid = 6, ld cf = 3) {
int sp = 0;
again:
auto corner = corners * (loctoh_ort(at) + (corner_coords[cornerid] / cf));
if(corner[1] < -1e-6 || corner[2] < -1e-6) {
at = at * eudir(1);
if(cornerid < SG6) cornerid = (1 + cornerid) % SG6;
sp++;
goto again;
}
if(sp>SG3) sp -= SG6;
return normalize(spin(TAU*sp/S7) * cornmul(T, corner));
}
transmatrix dir_matrix(int i) {
auto ddspin = [] (int d) -> transmatrix {
return spin(M_PI - d * TAU / S7 - cgi.hexshift);
};
return spin(-cgi.gpdata->alpha) * build_matrix(
geom3::flipped ? C02 : tile_center(),
geom3::flipped ? ddspin(i) * xpush0(cgi.tessf) : ddspin(i) * lxpush0(cgi.tessf),
geom3::flipped ? ddspin(i+1) * xpush0(cgi.tessf) : ddspin(i+1) * lxpush0(cgi.tessf),
C03
);
}
EX void prepare_matrices(bool inv) {
if(!(GOLDBERG_INV || inv)) return;
if(embedded_plane) geom3::light_flip(true);
cgi.gpdata->corners = inverse(build_matrix(
loctoh_ort(loc(0,0)),
loctoh_ort(param),
loctoh_ort(param * loc(0,1)),
C03
));
cgi.gpdata->Tf.resize(S7);
/* should work directly without flipping but it does not... flipping for now */
for(int i=0; i<S7; i++) {
transmatrix T = dir_matrix(i);
for(int x=-GOLDBERG_LIMIT_HALF; x<GOLDBERG_LIMIT_HALF; x++)
for(int y=-GOLDBERG_LIMIT_HALF; y<GOLDBERG_LIMIT_HALF; y++)
for(int d=0; d<(S3==3?6:4); d++) {
loc at = loc(x, y);
hyperpoint h = atz(T, cgi.gpdata->corners, at, 6);
hyperpoint hl = atz(T, cgi.gpdata->corners, at + eudir(d), 6);
auto& res = cgi.gpdata->Tf[i][x&GOLDBERG_MASK][y&GOLDBERG_MASK][d];
res = rgpushxto0(h) * rspintox(gpushxto0(h) * hl) * spin180();
}
}
if(geom3::flipped) {
geom3::light_flip(false);
for(int i=0; i<S7; i++) {
for(int x=-GOLDBERG_LIMIT_HALF; x<GOLDBERG_LIMIT_HALF; x++)
for(int y=-GOLDBERG_LIMIT_HALF; y<GOLDBERG_LIMIT_HALF; y++)
for(int d=0; d<(S3==3?6:4); d++) {
auto& T = cgi.gpdata->Tf[i][x&GOLDBERG_MASK][y&GOLDBERG_MASK][d];
T = cgi.emb->base_to_actual(T);
}
} }
}
EX hyperpoint get_corner_position(const local_info& li, int cid, ld cf IS(3)) {
int i = li.last_dir;
if(i == -1)
return atz(dir_matrix(cid), cgi.gpdata->corners, li.relative, 0, cf);
else {
auto& cellmatrix = cgi.gpdata->Tf[i][li.relative.first&GOLDBERG_MASK][li.relative.second&GOLDBERG_MASK][fixg6(li.total_dir)];
return inverse(cellmatrix) * atz(dir_matrix(i), cgi.gpdata->corners, li.relative, fixg6(cid + li.total_dir), cf);
}
}
EX hyperpoint get_corner_position(cell *c, int cid, ld cf IS(3)) {
return get_corner_position(get_local_info(c), cid, cf);
}
map<pair<int, int>, loc> center_locs;
EX void compute_geometry(bool inv) {
center_locs.clear();
if(GOLDBERG_INV || inv) {
if(!cgi.gpdata) cgi.gpdata = make_shared<geometry_information::gpdata_t>();
gp::clear_plainshapes();
int x = param.first;
int y = param.second;
if(S3 == 3)
cgi.gpdata->area = ((2*x+y) * (2*x+y) + y*y*3) / 4;
else
cgi.gpdata->area = x * x + y * y;
next = point3(x+y/2., -y * sqrt(3) / 2, 0);
ld scale = 1 / hypot_d(2, next);
if(!GOLDBERG) scale = 1;
cgi.crossf *= scale;
cgi.hepvdist *= scale;
cgi.hexhexdist *= scale;
cgi.hexvdist *= scale;
cgi.rhexf *= scale;
// spin = spintox(next);
// ispin = rspintox(next);
cgi.gpdata->alpha = -atan2(next[1], next[0]) * 6 / S7;
if(S3 == 3)
cgi.base_distlimit = (cgi.base_distlimit + log(scale) / log(2.618)) / scale;
else
cgi.base_distlimit = 3 * max(param.first, param.second) + 2 * min(param.first, param.second);
if(S7 == 12)
cgi.base_distlimit = 2 * param.first + 2 * param.second + 1;
if(cgi.base_distlimit > SEE_ALL)
cgi.base_distlimit = SEE_ALL;
DEBB(DF_GEOM | DF_POLY, ("scale = ", scale));
}
}
loc config;
EX bool rotate_and_check_limits(loc& v) {
int& x = v.first, &y = v.second;
while(x < 0 || y < 0 || (x == 0 && y > 0))
v = v * loc(0, 1);
return 2*(x+y) < (1<<GOLDBERG_BITS);
}
EX bool check_limits(loc v) {
return rotate_and_check_limits(v);
}
loc internal_representation(loc v) {
int& x = v.first, &y = v.second;
while(!rotate_and_check_limits(v)) {
if(x > y) x--; else y--;
}
if(S3 == 3 && y > x) v = v * loc(1, -1);
return v;
}
EX loc human_representation(loc v) {
int& x = v.first, &y = v.second;
if(S3 == 3) while(x < 0 || y < 0 || (x == 0 && y > 0))
v = v * loc(0, 1);
return v;
}
EX eVariation variation_for(loc xy) {
if(xy.first == 1 && xy.second == 0)
return eVariation::pure;
if(xy.first == 1 && xy.second == 1 && S3 == 3)
return eVariation::bitruncated;
return eVariation::goldberg;
}
EX void whirl_set(loc xy) {
xy = internal_representation(xy);
if(xy.second && xy.second != xy.first && nonorientable) {
addMessage(XLAT("This does not work in non-orientable geometries"));
xy.second = 0;
}
config = human_representation(xy);
auto g = screens;
if(xy.first == 0 && xy.second == 0) xy.first = 1;
stop_game();
param = xy;
if(xy.first == 1 && xy.second == 0) {
set_variation(eVariation::pure);
}
else if(xy.first == 1 && xy.second == 1 && S3 == 3) {
set_variation(eVariation::bitruncated);
}
else
set_variation(eVariation::goldberg);
start_game();
screens = g;
}
string helptext() {
return XLAT(
"Goldberg polyhedra are obtained by adding extra hexagons to a dodecahedron. "
"GP(x,y) means that, to get to a nearest non-hex from any non-hex, you should move x "
"cells in any direction, turn right 60 degrees, and move y cells. "
"HyperRogue generalizes this to any tesselation with 3 faces per vertex. "
"By default HyperRogue uses bitruncation, which corresponds to GP(1,1)."
);
}
void show() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen();
dialog::init(XLAT("variations"));
int min_quality_chess = 0;
int min_quality = 0;
#if CAP_TEXTURE
if((texture::config.tstate == texture::tsActive) && (S7 % 2 == 1)) {
if(texture::cgroup == cpFootball || texture::cgroup == cpThree) min_quality = 1;
}
if((texture::config.tstate == texture::tsActive) && (S7 % 2 == 1) && (S3 == 4)) {
if(texture::cgroup == cpChess) min_quality = 1;
}
#endif
if(min_quality == 0 && min_quality_chess == 0) {
dialog::addBoolItem(XLAT("pure"), PURE || (GOLDBERG && univ_param() == loc(1,0)), 'a');
dialog::lastItem().value = "GP(1,0)";
dialog::add_action_confirmed([] { whirl_set(loc(1, 0)); });
}
if(min_quality_chess == 0) {
dialog::addBoolItem(XLAT("bitruncated"), BITRUNCATED, 'b');
dialog::add_action_confirmed([] {
if(S3 == 4) {
if(!BITRUNCATED) {
stop_game();
set_variation(eVariation::bitruncated);
start_game();
}
}
else
whirl_set(loc(1, 1));
});
}
dialog::lastItem().value = S3 == 3 ? "GP(1,1)" : ONOFF(BITRUNCATED);
if(min_quality == 0 || min_quality_chess) {
dialog::addBoolItem(S3 == 3 ? XLAT("chamfered") : XLAT("expanded"), univ_param() == loc(2,0) && GOLDBERG, 'c');
dialog::lastItem().value = "GP(2,0)";
dialog::add_action_confirmed([] {
whirl_set(loc(2, 0));
});
}
if(S3 == 3) {
dialog::addBoolItem(XLAT("2x bitruncated"), GOLDBERG && univ_param() == loc(3,0), 'd');
dialog::lastItem().value = "GP(3,0)";
dialog::add_action_confirmed([] {
whirl_set(loc(3, 0));
});
}
else {
dialog::addBoolItem(XLAT("rectified"), param == loc(1,1) && GOLDBERG, 'd');
dialog::lastItem().value = "GP(1,1)";
dialog::add_action_confirmed([] {
whirl_set(loc(1, 1));
});
}
dialog::addBreak(100);
dialog::addSelItem("x", its(config.first), 'x');
dialog::add_action([] { dialog::editNumber(config.first, 0, 8, 1, 1, "x", helptext()); });
dialog::addSelItem("y", its(config.second), 'y');
dialog::add_action([] { dialog::editNumber(config.second, 0, 8, 1, 1, "y", helptext()); });
if(!check_limits(config))
dialog::addInfo(XLAT("Outside of the supported limits"));
if(config.second && config.second != config.first && nonorientable) {
dialog::addInfo(XLAT("This does not work in non-orientable geometries"));
}
else if((config.first-config.second)%3 && min_quality)
dialog::addInfo(XLAT("This pattern needs x-y divisible by 3"));
else if((config.first-config.second)%2 && min_quality_chess)
dialog::addInfo(XLAT("This pattern needs x-y divisible by 2"));
else {
dialog::addBoolItem(XLAT("select"), param == internal_representation(config) && !IRREGULAR && !INVERSE, 'f');
dialog::lastItem().value = "GP(x,y)";
}
dialog::add_action_confirmed([] { whirl_set(config); });
dialog::addBreak(100);
#if CAP_IRR
if(irr::supports(geometry)) {
dialog::addBoolItem(XLAT("irregular"), IRREGULAR, 'i');
dialog::add_action(dialog::add_confirmation([=] () {
if(min_quality && !irr::bitruncations_requested) irr::bitruncations_requested++;
if(euclid && (!closed_manifold || nonorientable)) {
println(hlog, XLAT("To create Euclidean irregular tesselations, first enable a torus"));
return;
}
if(!IRREGULAR) irr::visual_creator();
}));
}
#endif
dialog::addBreak(100);
int style = 0;
auto v0 = variation_for(param);
bool bad_bi = BITRUNCATED && a4;
if(!bad_bi) {
dynamicval<eVariation> v(variation, v0);
if(geosupport_football() == 2) style = 3;
if(geosupport_chessboard()) style = 2;
}
if(style == 2) {
dialog::addBoolItem(XLAT("inverse rectify"), UNRECTIFIED, 'r');
dialog::add_action_confirmed([v0] {
param = univ_param();
if(UNRECTIFIED) set_variation(v0);
else set_variation(eVariation::unrectified);
start_game();
config = human_representation(univ_param());
});
}
else if(style == 3) {
dialog::addBoolItem(XLAT("inverse truncate"), UNTRUNCATED, 't');
dialog::add_action_confirmed([v0] {
param = univ_param();
if(UNTRUNCATED) set_variation(v0);
else set_variation(eVariation::untruncated);
start_game();
});
dialog::addBoolItem(XLAT("warped version"), WARPED, 'w');
dialog::add_action_confirmed([v0] {
param = univ_param();
if(WARPED) set_variation(v0);
else set_variation(eVariation::warped);
start_game();
});
}
dialog::addBreak(100);
dialog::addItem(XLAT("swap x and y"), 'z');
dialog::add_action([] { swap(config.first, config.second); });
bool have_dual = !bad_bi && !IRREGULAR && !WARPED;
if(S3 == 3 && UNTRUNCATED && (univ_param()*loc(1,1)) % 3) have_dual = false;
if(S3 == 4 && UNRECTIFIED && (univ_param()*loc(1,1)) % 2) have_dual = false;
if(have_dual) {
dialog::addItem(XLAT("dual of current"), 'D');
dialog::add_action([] {
auto p = univ_param();
if(S3 == 3 && !UNTRUNCATED) {
println(hlog, "set param to ", p * loc(1,1));
whirl_set(p * loc(1, 1));
set_variation(eVariation::untruncated);
start_game();
config = human_representation(univ_param());
}
else if(S3 == 4 && !UNRECTIFIED) {
whirl_set(p * loc(1, 1));
set_variation(eVariation::unrectified);
start_game();
config = human_representation(univ_param());
}
else if(S3 == 3 && UNTRUNCATED) {
println(hlog, "whirl_set to ", (p * loc(1,1)) / 3);
whirl_set((p * loc(1,1)) / 3);
config = human_representation(univ_param());
}
else if(S3 == 4 && UNRECTIFIED) {
whirl_set((p * loc(1,1)) / 2);
config = human_representation(univ_param());
}
});
}
dialog::addBreak(100);
dialog::addHelp();