-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
arbitrile.cpp
2203 lines (1929 loc) · 65.1 KB
/
arbitrile.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 -- Arbitrary Tilings
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file arbitrile.cpp
* \brief Arbitrary tilings
*
* Arbitrary tilings, defined in .tes files.
*/
#include "hyper.h"
namespace hr {
EX namespace arb {
EX int affine_limit = 200;
#if HDR
/** a type used to specify the connections between shapes */
struct connection_t {
/** the index of the connected shape in the 'shapes' table */
int sid;
/** the index of the edge in the 'shapes' table */
int eid;
/** 1 if this connection mirrored, 0 otherwise. do_unmirror() removes all mirrors by doubling shapes */
int mirror;
bool operator == (const arb::connection_t& b) const { return tie(sid, eid, mirror) == tie(b.sid, b.eid, b.mirror); }
bool operator < (const arb::connection_t& b) const { return tie(sid, eid, mirror) < tie(b.sid, b.eid, b.mirror); }
};
inline void print(hstream& hs, const connection_t& conn) { print(hs, tie(conn.sid, conn.eid, conn.mirror)); }
/** \brief each shape of the arb tessellation
* note: the usual HyperRogue convention is: vertex 0, edge 0, vertex 1, edge 1, ...
* note: the tesfile convention is: edge 0, vertex 0, edge 1, vertex 1, ...
*/
/** edge with infinite end on the left */
constexpr ld INFINITE_LEFT = -1;
/** edge with infinite end on the right */
constexpr ld INFINITE_RIGHT = -2;
/** edge with two infinite ends */
constexpr ld INFINITE_BOTH = -3;
struct shape {
/** index in the arbi_tiling::shapes */
int id;
/** index in the original file */
int orig_id;
/** flags such as sfLINE and sfPH */
int flags;
/** list of vertices in the usual convention */
vector<hyperpoint> vertices;
/** list of angles in the tesfile convention */
vector<ld> angles;
/** list of edge lengths */
vector<ld> edges;
/** list of input edges */
vector<ld> in_edges;
/** list of input angles */
vector<ld> in_angles;
/** (ultra)ideal markers */
vector<bool> ideal_markers;
/** list of edge connections */
vector<connection_t> connections;
int size() const { return isize(vertices); }
void build_from_angles_edges(bool is_comb);
vector<pair<int, int> > sublines;
vector<pair<ld, ld>> stretch_shear;
/** '*inf' was applied to represent an apeirogon/pseudogon */
bool apeirogonal;
/** connections repeat `repeat_value` times */
int repeat_value;
/** 0 if the no mirror symmetries are declared; otherwise, edge i is the mirror of edge gmod(symmetric_value-i, size()). Make sure symmetric_value != 0, e.g., by adding size() */
int symmetric_value;
/** if a tile/edge combination may be connected to edges j1 and j2 of this, j1-j2 must be divisible by cycle_length */
int cycle_length;
/** list of valences of vertices in the tesfile convention */
vector<int> vertex_valence;
/** list of periods of vertices in the tesfile convention */
vector<int> vertex_period;
/** list of angles at vertices in the tesfile convention */
vector<vector<ld>> vertex_angles;
/** football types */
int football_type;
/** is it a mirrored version of an original tile */
bool is_mirrored;
/** auxiliary function for symmetric_value: is the edge index reflectable? */
bool reflectable(int id) {
if(!symmetric_value) return false;
if(apeirogonal && gmod(id, size()) >= size() - 2) return false;
return true;
}
/** reflect a reflectable reflect index */
int reflect(int id) {
return gmod(symmetric_value - id, size() - (apeirogonal ? 2 : 0));
}
};
struct slider {
string name;
ld zero;
ld current;
ld min;
ld max;
};
struct intslider {
string name;
int zero;
int current;
int min;
int max;
};
struct arbi_tiling {
int order;
/* line flags have been marked for tiles */
bool have_line;
/* pseudohept flags have been marked for tiles (1), or the tiling is football-colorable (2), or neither (0) */
int have_ph;
/* is the tree structure given in the tes file */
bool have_tree;
/* is the valence data reliable */
bool have_valence;
/* use "star." if the tessellation includs star polygons */
bool is_star;
/* use "combinatorial." for combinatorial tessellations; vertex valences computed based on their angles. Currently only rulegen works for combinatorial tessellations */
bool is_combinatorial;
/* reserved for future flags */
bool res0, res1, res2, res3;
int yendor_backsteps;
vector<shape> shapes;
string name;
string comment;
vector<slider> sliders;
vector<intslider> intsliders;
ld cscale;
int range;
ld floor_scale;
ld boundary_ratio;
string filename;
int mirror_rules;
vector<string> options;
int min_valence, max_valence;
bool is_football_colorable;
bool was_unmirrored;
bool was_split_for_football;
geometryinfo1& get_geometry();
eGeometryClass get_class() { return get_geometry().kind; }
ld scale();
};
#endif
/** currently loaded tiling */
EX arbi_tiling current;
/** is the currently displayed map current or slided */
EX bool using_slided;
/** for real-valued sliders, current is the tiling used by the map, while slided is the tiling used for the display */
EX arbi_tiling slided;
EX bool in_slided() { return in() && using_slided; }
EX arbi_tiling& current_or_slided() {
return using_slided ? slided : current;
}
/** id of vertex in the arbitrary tiling */
EX short& id_of(heptagon *h) { return h->zebraval; }
#if HDR
struct hr_polygon_error : hr_exception {
vector<transmatrix> v;
eGeometryClass c;
int id;
transmatrix end;
map<string, cld> params;
hr_polygon_error(const vector<transmatrix>& _v, int _id, transmatrix _e) : v(_v), c(cgclass), id(_id), end(_e) {}
~hr_polygon_error() noexcept(true) {}
string generate_error();
};
#endif
string hr_polygon_error::generate_error() {
cld dist = (hdist0(tC0(end)) / params["distunit"]);
bool angle = abs(dist) < 1e-9;
if(angle) dist = (atan2(end * lxpush0(1)) / params["angleunit"]);
return
XLAT("Polygon number %1 did not close correctly (%2 %3). Here is the picture to help you understand the issue.\n\n", its(id),
angle ? "angle" : "distance",
lalign(0, dist)
);
}
struct connection_debug_request : hr_exception {
int id;
eGeometryClass c;
connection_debug_request(int i): id(i), c(cgclass) {}
};
void ensure_geometry(eGeometryClass c) {
stop_game();
if(c != cgclass) {
if(c == gcEuclid) set_geometry(gEuclid);
if(c == gcHyperbolic) set_geometry(gNormal);
if(c == gcSphere) set_geometry(gSphere);
}
if(specialland != laCanvas) {
canvas_default_wall = waInvisibleFloor;
ccolor::set_plain_nowall(0xFFFFFF);
enable_canvas();
}
start_game();
}
void start_poly_debugger(hr_polygon_error& err) {
#if CAP_EDIT
ensure_geometry(err.c);
drawthemap();
mapeditor::drawing_tool = true;
pushScreen(mapeditor::showDrawEditor);
mapeditor::initdraw(cwt.at);
int n = isize(err.v);
mapeditor::dtcolor = 0xFF0000FF;
mapeditor::dtwidth = 0.02;
for(int i=0; i<n-1; i++)
mapeditor::dt_add_line(shiftless(tC0(err.v[i])), shiftless(tC0(err.v[i+1])), 0);
mapeditor::dtcolor = 0xFFFFFFFF;
for(int i=0; i<n; i++)
mapeditor::dt_add_text(shiftless(tC0(err.v[i])), 0.5, its(i));
#endif
}
void shape::build_from_angles_edges(bool is_comb) {
transmatrix at = Id;
int n = isize(in_angles);
hyperpoint ctr = Hypc;
vector<transmatrix> matrices;
for(int i=0; i<n; i++) {
matrices.push_back(at);
if(debugflags & DF_GEOM) println(hlog, "at = ", at);
ctr += tC0(at);
at = at * lxpush(in_edges[i]) * spin(in_angles[i]+M_PI);
}
matrices.push_back(at);
if(is_comb) return;
if(!eqmatrix(at, Id) && !apeirogonal) {
throw hr_polygon_error(matrices, id, at);
}
if(sqhypot_d(3, ctr) < 1e-2) {
// this may happen for some spherical tilings
// try to move towards the center
if(debugflags & DF_GEOM) println(hlog, "special case encountered");
for(int i=0; i<n; i++) {
ctr += at * lxpush(in_edges[i]) * spin((in_angles[i]+M_PI)/2) * lxpush0(.01);
at = at * lxpush(in_edges[i]) * spin(in_angles[i]);
}
if(debugflags & DF_GEOM) println(hlog, "ctr = ", ctr);
}
hyperpoint inf_point;
if(apeirogonal) {
transmatrix U = at;
for(int i=0; i<3; i++) for(int j=0; j<3; j++) U[i][j] -= Id[i][j];
hyperpoint v;
ld det = U[0][1] * U[1][0] - U[1][1] * U[0][0];
v[1] = (U[1][2] * U[0][0] - U[0][2] * U[1][0]) / det;
v[0] = (U[0][2] * U[1][1] - U[1][2] * U[0][1]) / det;
v[2] = 1;
inf_point = v;
ctr = mid(C0, tC0(at));
ctr = towards_inf(ctr, inf_point);
}
ctr = normalize(ctr);
vertices.clear();
angles.clear();
for(int i=0; i<n; i++) {
edges.push_back(in_edges[i]);
if(!ideal_markers[i]) {
vertices.push_back(tC0(gpushxto0(ctr) * matrices[i]));
angles.push_back(in_angles[i]);
}
else {
angles.push_back(0);
hyperpoint a1 = tC0(matrices[i]);
hyperpoint t1 = get_column(matrices[i], 0);
hyperpoint a2 = tC0(matrices[i+2]);
hyperpoint t2 = get_column(matrices[i+2], 0);
a1 /= a1[2];
a2 /= a2[2];
t1 -= a1 * t1[2];
t2 -= a2 * t2[2];
ld c1 = a2[0] - a1[0], c2 = a2[1] - a1[1];
ld v1 = t1[0], v2 = t1[1];
ld u1 = t2[0], u2 = t2[1];
ld r = (u2 * c1 - c2 * u1) / (v1 * u2 - v2 * u1);
// ld s = (v2 * c1 - c2 * v1) / (v1 * u2 - v2 * u1);
hyperpoint v = a1 + r * t1;
// also v == a2 + s * t2;
v[2] = 1;
v = gpushxto0(ctr) * v;
v /= v[2];
vertices.push_back(v);
i++;
}
}
if(apeirogonal) {
vertices.push_back(gpushxto0(ctr) * tC0(at));
hyperpoint v = gpushxto0(ctr) * inf_point;
v /= v[2];
vertices.push_back(v);
auto b = angles.back() / 2;
angles.back() = b;
angles.push_back(0);
angles.push_back(b);
edges.push_back(0);
edges.push_back(0);
}
n = isize(angles);
for(int i=0; i<n; i++) {
bool left = angles[i] == 0;
bool right = angles[gmod(i-1, isize(vertices))] == 0;
if(left && right) edges[i] = INFINITE_BOTH;
else if(left) edges[i] = INFINITE_LEFT;
else if(right) edges[i] = INFINITE_RIGHT;
}
}
EX bool correct_index(int index, int size) { return index >= 0 && index < size; }
template<class T> bool correct_index(int index, const T& v) { return correct_index(index, isize(v)); }
template<class T> void verify_index(int index, const T& v, exp_parser& ep) { if(!correct_index(index, v)) throw hr_parse_exception("bad index: " + its(index) + " at " + ep.where()); }
string unnamed = "unnamed";
EX void load_tile(exp_parser& ep, arbi_tiling& c, bool unit) {
c.shapes.emplace_back();
auto& cc = c.shapes.back();
cc.id = isize(c.shapes) - 1;
cc.orig_id = cc.id;
cc.is_mirrored = false;
cc.symmetric_value = 0;
cc.flags = 0;
cc.repeat_value = 1;
cc.apeirogonal = false;
bool is_symmetric = false;
while(ep.next() != ')') {
cld dist = 1;
ep.skip_white();
if(ep.eat("|")) {
cc.symmetric_value = ep.iparse();
is_symmetric = true;
ep.force_eat(")");
break;
}
if(ep.eat("*")) {
ld frep = ep.rparse(0);
if(isinf(frep)) {
cc.apeirogonal = true;
set_flag(ginf[gArbitrary].flags, qIDEAL, true);
if(ep.eat(",") && ep.eat("|")) {
is_symmetric = true;
if(isize(cc.in_edges) == 1 && ep.eat(")")) break;
cc.symmetric_value = ep.iparse();
}
ep.force_eat(")");
break;
}
int rep = int(frep+.5);
int repeat_from = 0;
int repeat_to = cc.in_edges.size();
if(rep == 0) {
cc.in_edges.resize(repeat_from);
cc.in_angles.resize(repeat_from);
cc.ideal_markers.resize(repeat_from);
}
else if(rep < 0) throw hr_parse_exception("don't know how to use a negative repeat in tile definition");
for(int i=1; i<rep; i++)
for(int j=repeat_from; j<repeat_to; j++) {
cc.in_edges.push_back(cc.in_edges[j]);
cc.in_angles.push_back(cc.in_angles[j]);
cc.ideal_markers.push_back(cc.ideal_markers[j]);
}
ep.skip_white();
if(ep.eat(",")) {
ep.force_eat("|");
is_symmetric = true;
if(repeat_to == 1 && ep.eat(")")) goto skip;
cc.symmetric_value = ep.iparse();
}
if(ep.eat(")")) {
skip:
if(repeat_from == 0) cc.repeat_value = rep;
break;
}
else throw hr_parse_exception("expecting ) after repeat");
}
if(!unit) {
dist = ep.parse(0);
ep.force_eat(",");
}
cld angle;
ep.skip_white();
if(ep.eat("[")) {
cc.in_edges.push_back(ep.validate_real(dist * ep.extra_params["distunit"]));
angle = ep.parse(0); ep.force_eat(",");
cc.in_angles.push_back(ep.validate_real(angle * ep.extra_params["angleunit"]));
cc.ideal_markers.push_back(true);
dist = ep.parse(0); ep.force_eat(",");
angle = ep.parse(0); ep.force_eat("]");
set_flag(ginf[gArbitrary].flags, qIDEAL, true);
}
else
angle = ep.parse(0);
cc.in_edges.push_back(ep.validate_real(dist * ep.extra_params["distunit"]));
cc.in_angles.push_back(ep.validate_real(angle * ep.extra_params["angleunit"]));
cc.ideal_markers.push_back(false);
if(ep.eat(",")) continue;
else if(ep.eat(")")) break;
else throw hr_parse_exception("expecting , or )");
}
try {
cc.build_from_angles_edges(c.is_combinatorial);
}
catch(hr_parse_exception& ex) {
throw hr_parse_exception(ex.s + ep.where());
}
catch(hr_polygon_error& poly) {
poly.params = ep.extra_params;
throw;
}
int n = cc.size();
if(is_symmetric && !cc.symmetric_value) cc.symmetric_value += n - (cc.apeirogonal ? 2 : 0);
cc.connections.resize(n);
for(int i=0; i<isize(cc.connections); i++)
cc.connections[i] = connection_t{cc.id, i, false};
if(cc.apeirogonal) {
cc.connections[n-2].eid = n-1;
cc.connections[n-1].eid = n-2;
}
cc.stretch_shear.resize(n, make_pair(1, 0));
}
EX bool do_unmirror = true;
template<class T> void cycle(vector<T>& t) {
std::rotate(t.begin(), t.begin() + 2, t.end());
}
/** \brief for tessellations which contain mirror rules, remove them by taking the orientable double cover */
EX void unmirror(arbi_tiling& c) {
if(cgflags & qAFFINE) return;
auto& mirror_rules = c.mirror_rules;
mirror_rules = 0;
for(auto& s: c.shapes)
for(auto& t: s.connections)
if(t.mirror)
mirror_rules++;
if(!mirror_rules) return;
auto& sh = c.shapes;
int s = isize(sh);
vector<int> mirrored_id(s, -1);
for(int i=0; i<s; i++)
if(!sh[i].symmetric_value) {
mirrored_id[i] = isize(sh);
sh.push_back(sh[i]);
}
int ss = isize(sh);
for(int i=0; i<ss; i++) {
sh[i].id = i;
if(i >= s) sh[i].is_mirrored = true;
}
for(int i=s; i<ss; i++) {
for(auto& v: sh[i].vertices)
v[1] = -v[1];
reverse(sh[i].edges.begin(), sh[i].edges.end());
for(auto& e: sh[i].edges) {
if(e == INFINITE_LEFT) e = INFINITE_RIGHT;
else if(e == INFINITE_RIGHT) e = INFINITE_LEFT;
}
reverse(sh[i].vertices.begin()+1, sh[i].vertices.end());
reverse(sh[i].angles.begin(), sh[i].angles.end()-1);
reverse(sh[i].connections.begin(), sh[i].connections.end());
if(sh[i].apeirogonal) {
cycle(sh[i].edges);
cycle(sh[i].vertices);
if(debugflags & DF_GEOM) println(hlog, "angles before = ", sh[i].angles);
cycle(sh[i].angles);
if(debugflags & DF_GEOM) println(hlog, "angles now = ", sh[i].angles);
cycle(sh[i].connections);
}
}
if(true) for(int i=0; i<ss; i++) {
for(auto& co: sh[i].connections) {
bool mirr = co.mirror ^ (i >= s);
co.mirror = false;
if(mirr && mirrored_id[co.sid] == -1) {
if(sh[co.sid].reflectable(co.eid)) {
co.eid = sh[co.sid].reflect(co.eid);
}
}
else if(mirr) {
co.sid = mirrored_id[co.sid];
co.eid = isize(sh[co.sid].angles) - 1 - co.eid;
if(sh[co.sid].apeirogonal)
co.eid = gmod(co.eid - 2, isize(sh[co.sid].angles));
}
}
}
c.was_unmirrored = true;
}
static void reduce_gcd(int& a, int b) {
a = abs(gcd(a, b));
}
EX void mirror_connection(arb::arbi_tiling& ac, connection_t& co) {
if(co.mirror && ac.shapes[co.sid].reflectable(co.eid)) {
co.eid = ac.shapes[co.sid].reflect(co.eid);
co.mirror = !co.mirror;
}
}
EX void compute_vertex_valence_prepare(arb::arbi_tiling& ac) {
int tcl = -1;
while(true) {
for(auto& sh: ac.shapes) {
int i = sh.id;
int n = isize(sh.vertices);
for(int k=sh.cycle_length; k<n; k++) {
auto co = sh.connections[k];
auto co1 = sh.connections[k-sh.cycle_length];
if(co.sid != co1.sid) {
println(hlog, "ik = ", tie(i,k), " co=", co, " co1=", co1, " cl=", sh.cycle_length);
throw hr_parse_exception("connection error #2 in compute_vertex_valence");
}
mirror_connection(ac, co);
mirror_connection(ac, co1);
reduce_gcd(ac.shapes[co.sid].cycle_length, co.eid - co1.eid);
}
for(int k=0; k<n; k++) {
auto co = sh.connections[k];
auto co0 = co;
co = ac.shapes[co.sid].connections[co.eid];
if(co.sid != i) throw hr_parse_exception("connection error in compute_vertex_valence");
co.mirror ^= co0.mirror;
mirror_connection(ac, co);
reduce_gcd(sh.cycle_length, k-co.eid);
}
if(debugflags & DF_GEOM)
println(hlog, "tile ", i, " cycle_length = ", sh.cycle_length, " / ", n);
}
int new_tcl = 0;
for(auto& sh: ac.shapes) {
auto& len = sh.cycle_length;
if(len < 0) len = -len;
new_tcl += len;
}
if(new_tcl == tcl) break;
tcl = new_tcl;
}
}
/** returns true if we need to recompute */
EX bool compute_vertex_valence_flat(arb::arbi_tiling& ac) {
for(auto& sh: ac.shapes) {
int n = sh.size();
int i = sh.id;
sh.vertex_valence.resize(n);
sh.vertex_period.resize(n);
sh.vertex_angles.resize(n);
for(int k=0; k<n; k++) {
ld total = 0;
int qty = 0, pqty = 0;
connection_t at = {i, k, false};
connection_t at1 = at;
vector<ld> anglelist;
do {
if(at.sid == at1.sid && (at.eid-at1.eid) % ac.shapes[at.sid].cycle_length == 0) pqty = 0;
if(qty && pqty == 0 && !total) break;
ld a = ac.shapes[at.sid].angles[at.eid];
while(a < 0) a += TAU;
while(a > TAU) a -= TAU;
total += a;
anglelist.push_back(a);
qty++;
pqty++;
at.eid++;
if(at.eid == isize(ac.shapes[at.sid].angles)) at.eid = 0;
at = ac.shapes[at.sid].connections[at.eid];
}
while(total < TAU - 1e-6);
if(total == 0) qty = OINF;
if(total > TAU + 1e-6) throw hr_parse_exception("improper total in compute_stats");
if(at.sid != i) throw hr_parse_exception("ended at wrong type determining vertex_valence");
if((at.eid - k) % ac.shapes[i].cycle_length) {
reduce_gcd(ac.shapes[i].cycle_length, at.eid - k);
return true;
}
sh.vertex_valence[k] = qty;
sh.vertex_period[k] = pqty;
sh.vertex_angles[k] = std::move(anglelist);
}
if(debugflags & DF_GEOM)
println(hlog, "computed vertex_valence of ", i, " as ", ac.shapes[i].vertex_valence);
}
return false;
}
/** returns true if we need to recompute */
EX bool compute_vertex_valence_generic(arb::arbi_tiling& ac) {
for(auto& sh: ac.shapes) {
int n = sh.size();
int i = sh.id;
sh.vertex_valence.resize(n);
for(int k=0; k<n; k++) {
connection_t at = {i, k, false};
transmatrix T = Id;
int qty = 0;
do {
if(qty && at.sid == i) {
auto co1 = at;
bool found = find_connection(T, Id, co1);
if(found) {
mirror_connection(ac, co1);
if((co1.eid - k) % ac.shapes[i].cycle_length) {
reduce_gcd(ac.shapes[i].cycle_length, co1.eid - k);
return true;
}
break;
}
}
if(at.mirror) {
if(at.eid == 0) at.eid = isize(ac.shapes[at.sid].angles);
at.eid--;
}
else {
at.eid++;
if(at.eid == isize(ac.shapes[at.sid].angles)) at.eid = 0;
}
auto at0 = at;
at = ac.shapes[at.sid].connections[at.eid];
T = T * get_adj(ac, at0.sid, at0.eid, at.sid, at.eid, at.mirror);
at.mirror ^= at0.mirror;
qty++;
}
while(qty < OINF);
sh.vertex_valence[k] = qty;
}
if(debugflags & DF_GEOM)
println(hlog, "computed vertex_valence of ", i, " as ", ac.shapes[i].vertex_valence);
}
return false;
}
EX void compute_vertex_valence(arb::arbi_tiling& ac) {
for(auto& sh: ac.shapes)
sh.cycle_length = isize(sh.vertices) / sh.repeat_value;
bool generic = false;
if(!ac.was_unmirrored) for(auto& sh: ac.shapes) if(sh.symmetric_value) generic = true;
for(auto& sh: ac.shapes) for(auto& co: sh.connections) if(co.mirror) generic = true;
if(cgflags & qAFFINE) generic = true;
if(ac.is_star) generic = true;
recompute:
compute_vertex_valence_prepare(ac);
if(generic ? compute_vertex_valence_generic(ac) : compute_vertex_valence_flat(ac)) goto recompute;
ac.have_valence = true;
ac.min_valence = UNKNOWN; ac.max_valence = 0;
for(auto& sh: ac.shapes)
for(auto& val: sh.vertex_valence) {
if(val < ac.min_valence) ac.min_valence = val;
if(val > ac.max_valence) ac.max_valence = val;
}
}
EX bool extended_football = true;
EX void check_football_colorability(arbi_tiling& c) {
if(!c.have_valence) return;
for(auto&sh: c.shapes) for(auto v: sh.vertex_valence)
if(v % 3) return;
for(int i=0; i<3; i++) {
for(auto&sh: c.shapes) sh.football_type = 3;
vector<int> aqueue;
c.shapes[0].football_type = i;
aqueue = {0};
bool bad = false;
for(int qi=0; qi<isize(aqueue); qi++) {
int sid = aqueue[qi];
auto& sh = c.shapes[sid];
for(int j=0; j<sh.size(); j++) {
auto &co = sh.connections[j];
auto t = sh.football_type;
if(c.have_ph && ((sh.flags & arcm::sfPH) != (t==2))) bad = true;
if(sh.apeirogonal && t < 2 && (isize(sh) & 1)) bad = true;
auto assign = [&] (int tt) {
auto& t1 = c.shapes[co.sid].football_type;
if(t1 == 3) {
t1 = tt;
aqueue.push_back(co.sid);
}
else {
if(t1 != tt) bad = true;
}
};
if(t < 2) {
if((j & 1) == t) assign(2); else assign((co.eid & 1) ? 0 : 1);
}
else {
assign((co.eid & 1) ? 1 : 0);
}
}
}
if(!bad) {
c.have_ph = 2;
for(auto& sh: c.shapes) if(sh.football_type == 2) sh.flags |= arcm::sfPH;
return;
}
}
if(extended_football && !c.have_tree) {
for(auto&sh: c.shapes)
sh.football_type = 0;
for(int i=0; i<3*isize(c.shapes); i++) {
for(auto&sh: c.shapes) {
int &res = sh.football_type;
int siz = sh.size();
if(sh.apeirogonal) siz -= 2;
else if(siz & 1) res |= 3;
if((sh.cycle_length & 1) && !sh.apeirogonal) {
if(res & 3) res |= 3;
}
if(sh.apeirogonal && (siz & 1)) {
if(res & 3) res |= 3;
}
if(sh.flags & arcm::sfPH) res |= 3;
for(int i=0; i<sh.size(); i++) {
auto co = sh.connections[i];
co.eid %= c.shapes[co.sid].cycle_length;
if(res & 1) {
if(i&1) {
if(co.eid & 1)
c.shapes[co.sid].football_type |= 1;
else
c.shapes[co.sid].football_type |= 2;
}
else
c.shapes[co.sid].football_type |= 4;
}
if(res & 2) {
if(!(i&1)) {
if(co.eid & 1)
c.shapes[co.sid].football_type |= 1;
else
c.shapes[co.sid].football_type |= 2;
}
else
c.shapes[co.sid].football_type |= 4;
}
if(res & 4) {
if(co.eid & 1)
c.shapes[co.sid].football_type |= 2;
else
c.shapes[co.sid].football_type |= 1;
}
}
}
}
c.is_football_colorable = true;
c.was_split_for_football = true;
for(auto&sh: c.shapes)
if(sh.football_type == 7)
c.is_football_colorable = false;
if(c.is_football_colorable) {
vector<array<int, 3> > new_indices(isize(c.shapes), make_array(-1, -1, -1));
auto oldshapes = c.shapes;
c.shapes.clear();
for(int i=0; i<isize(oldshapes); i++)
for(int t=0; t<3; t++)
if(!(oldshapes[i].football_type & (1<<t))) {
if(t == 1 && (oldshapes[i].cycle_length & 1) && !oldshapes[i].apeirogonal) continue;
new_indices[i][t] = isize(c.shapes);
c.shapes.push_back(oldshapes[i]);
c.shapes.back().football_type = t;
if(t == 2) c.shapes.back().flags |= arcm::sfPH;
}
for(int i=0; i<isize(oldshapes); i++)
for(int t=0; t<3; t++) {
int ni = new_indices[i][t];
if(ni == -1) continue;
auto& sh = c.shapes[ni];
sh.id = ni;
for(int j=0; j<isize(sh); j++) {
auto &co = sh.connections[j];
auto assign = [&] (int tt) {
auto ni1 = new_indices[co.sid][tt];
if(ni1 == -1 && tt == 1) {
ni1 = new_indices[co.sid][0];
co.eid += oldshapes[co.sid].cycle_length;
co.eid %= isize(oldshapes[co.sid]);
}
co.sid = ni1;
};
if(sh.apeirogonal && j >= isize(sh)-2) {
co.sid = ni;
if(t < 2 && (isize(sh) & 1)) co.sid = new_indices[i][t^1];
continue;
}
co.eid %= oldshapes[co.sid].cycle_length;
if(t < 2) {
if((j & 1) == t) assign(2); else assign((co.eid & 1) ? 0 : 1);
}
else {
assign((co.eid & 1) ? 1 : 0);
}
}
if((sh.cycle_length&1) && (t < 2) && !sh.apeirogonal) sh.cycle_length *= 2;
if(debugflags & DF_GEOM)
println(hlog, tie(i,t), " becomes ", ni, " with connections ", sh.connections, " and cycle length = ", sh.cycle_length);
}
c.have_ph = 2;
return;
}
}
for(auto&sh: c.shapes) sh.football_type = 3;
}
EX void add_connection_sub(arbi_tiling& c, int ai, int as, int bi, int bs, int m) {
int as0 = as, bs0 = bs;
auto& ash = c.shapes[ai];
auto& bsh = c.shapes[bi];
do {
ash.connections[as] = connection_t{bi, bs, m};
as = gmod(as + ash.size() / ash.repeat_value, ash.size());
}
while(as != as0);
do {
c.shapes[bi].connections[bs] = connection_t{ai, as, m};
bs = gmod(bs + bsh.size() / bsh.repeat_value, bsh.size());
}
while(bs != bs0);
}
EX void add_connection(arbi_tiling& c, int ai, int as, int bi, int bs, int m) {
auto& ash = c.shapes[ai];
auto& bsh = c.shapes[bi];
add_connection_sub(c, ai, as, bi, bs, m);
int as1, bs1;
if(ash.symmetric_value) {
as1 = ash.reflect(as);
add_connection_sub(c, ai, as1, bi, bs, !m);
}
if(bsh.symmetric_value) {
bs1 = bsh.reflect(bs);
add_connection_sub(c, ai, as, bi, bs1, !m);
}
if(ash.symmetric_value && bsh.symmetric_value)
add_connection_sub(c, ai, as1, bi, bs1, m);
}
EX void set_defaults(arb::arbi_tiling& c, bool keep_sliders, string fname) {
c.order++;
c.name = unnamed;
c.comment = "";
c.filename = fname;
c.cscale = 1;
c.range = 0;
c.boundary_ratio = 1;
c.floor_scale = .5;
c.have_ph = 0;
c.have_line = false;
c.is_football_colorable = false;
c.have_tree = false;
c.have_valence = false;
c.yendor_backsteps = 0;
c.is_star = false;
c.is_combinatorial = false;
c.was_unmirrored = false;
c.was_split_for_football = false;
c.shapes.clear();
if(!keep_sliders) {
c.sliders.clear();
c.intsliders.clear();
}
}
EX void load(const string& fname, bool load_as_slided IS(false), bool keep_sliders IS(false)) {
fhstream f(fname, "rt");
if(!f.f) throw hr_parse_exception("file " + fname + " does not exist");
string s;
while(true) {
int c = fgetc(f.f);
if(c < 0) break;
s += c;
}
auto& c = load_as_slided ? slided : current;
set_defaults(c, keep_sliders, fname);
int qsliders = 0, qintsliders = 0;
exp_parser ep;
ep.s = s;
ld angleunit = 1, distunit = 1;
auto addflag = [&] (int f) {
int ai;
if(ep.next() == ')') ai = isize(c.shapes)-1;
else ai = ep.iparse();
verify_index(ai, c.shapes, ep);
c.shapes[ai].flags |= f;
ep.force_eat(")");
};
while(true) {
ep.extra_params["distunit"] = distunit;
ep.extra_params["angleunit"] = angleunit;
ep.skip_white();
if(ep.next() == 0) break;
if(ep.eat("#")) {
bool doubled = ep.eat("#");
while(ep.eat(" ")) ;
string s = "";
while(ep.next() >= 32) s += ep.next(), ep.at++;
if(doubled) {
if(c.name == unnamed) c.name = s;
else {
c.comment += s;
c.comment += "\n";
}
}
}
else if(ep.eat("c2(")) {
ld curv = ep.rparse(0);
ep.force_eat(")");
ginf[gArbitrary].g = curv > 0 ? giSphere2 : curv < 0 ? giHyperb2 : giEuclid2;
ginf[gArbitrary].sides = 7;
set_flag(ginf[gArbitrary].flags, qCLOSED, curv > 0);
set_flag(ginf[gArbitrary].flags, qAFFINE, false);
geom3::apply_always3();
}
else if(ep.eat("e2.")) {
ginf[gArbitrary].g = giEuclid2;
ginf[gArbitrary].sides = 7;
set_flag(ginf[gArbitrary].flags, qCLOSED, false);