-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
embeddings.cpp
1397 lines (1258 loc) · 49.6 KB
/
embeddings.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 -- embeddings
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file embeddings.cpp
* \brief Embedding 2D geometries into 3D
*
* This file handles primarily embedding 2D geometries into 3D.
*
* The following coordinate systems are used for embedding of 2D geometries into 3D:
*
* - *base* coordinates are simply the coordinate in the underlying 2D geometry. They support only two dimensions.
* - *logical* coordinates: X and Y are in the Beltrami-Klein or gnomonic model, or in horocyclic coordinates for binary-like tilings. Z coordinate is the altitude above the plane.
* - *logical_scaled* coordinates: X and Y are scaled (and possibly rotated in the XY plane) in order to match the scale and orientation of the ambient 3D geometry. They are a linear transformation of logical.
* - *intermediate* coordinates: they use the same assignment of coordinates as actual, but they are a linear transformation of logical scaled.
* - *actual* coordinates: final coordinates in the ambient 3D geometry.
*
*/
#include "hyper.h"
namespace hr {
EX namespace geom3 {
#if HDR
enum eSpatialEmbedding {
seNone,
seDefault,
seLowerCurvature,
seMuchLowerCurvature,
seProduct,
seNil,
seSol,
seNIH,
seSolN,
seCliffordTorus,
seProductH,
seProductS,
seSL2,
seCylinderE,
seCylinderH,
seCylinderHE,
seCylinderNil,
seCylinderHoro,
seCylinderSL2
};
#endif
EX vector<pair<string, string>> spatial_embedding_options = {
{"2D engine", "Use HyperRogue's 2D engine to simulate same curvature. Works well in top-down and third-person perspective. The Hypersian Rug mode can be used to project this to a surface."},
{"same curvature", "Embed as an equidistant surface in the 3D version of the same geometry."},
{"lower curvature", "Embed as a surface in a space of lower curvature."},
{"much lower curvature", "Embed sphere as a sphere in hyperbolic space."},
{"product", "Add one extra dimension in the Euclidean way."},
{"Nil", "Embed Euclidean plane into Nil."},
{"Sol", "Embed Euclidean or hyperbolic plane into Sol."},
{"stretched hyperbolic", "Embed Euclidean or hyperbolic plane into stretched hyperbolic geometry."},
{"stretched Sol", "Embed Euclidean or hyperbolic plane into stretched Sol geometry."},
{"Clifford Torus", "Embed Euclidean rectangular torus into S3."},
{"hyperbolic product", "Embed Euclidean or hyperbolic plane in the H2xR product space."},
{"spherical product", "Embed Euclidean cylinder or spherical plane in the H2xR product space."},
{"SL(2,R)", "Embed Euclidean plane in twisted product geometry."},
{"cylinder", "Embed Euclidean cylinder in Euclidean space."},
{"hyperbolic cylinder", "Embed Euclidean cylinder in hyperbolic space."},
{"product cylinder", "Embed Euclidean cylinder in H2xR space."},
{"Nil cylinder", "Embed Euclidean cylinder in Nil."},
{"horocylinder", "Embed Euclidean as a horocylinder in H2xR space."},
{"SL2 cylinder", "Embed Euclidean as a cylinder in twisted product geometry."},
};
EX bool clifford_torus_valid() {
#if CAP_RUG
rug::clifford_torus ct;
ld h = ct.xh | ct.yh;
return !(sqhypot_d(2, ct.xh) < 1e-3 || sqhypot_d(2, ct.yh) < 1e-3 || abs(h) > 1e-3);
#else
return false;
#endif
}
EX string why_wrong(eSpatialEmbedding sp) {
string ans = "";
if(among(sp, seNil, seCliffordTorus, seProductH, seProductS, seSL2) || any_cylinder(sp)) {
if(!PURE)
ans += " pure";
if(!meuclid)
ans += " E";
if((sp == seProductS || any_cylinder(sp)) && !quotient)
ans += " cyl";
if(sp == seCliffordTorus && !clifford_torus_valid())
ans += " torus";
}
if(among(sp, seSol, seNIH, seSolN)) {
if((meuclid && !PURE) || !bt::in()) ans += " pure or bin";
}
return ans;
}
EX eSpatialEmbedding spatial_embedding = seDefault;
EX ld euclid_embed_scale = 1;
EX ld euclid_embed_scale_y = 1;
EX ld euclid_embed_rotate = 0;
EX bool auto_configure = true;
EX bool flat_embedding = false;
EX bool inverted_embedding = false;
EX bool apply_break_cylinder = true;
EX ld euclid_embed_scale_mean() { return euclid_embed_scale * sqrt(euclid_embed_scale_y); }
EX void set_euclid_embed_scale(ld x) { euclid_embed_scale = x; euclid_embed_scale_y = 1; euclid_embed_rotate = 0; }
EX bool supports_flat() { return among(spatial_embedding, seDefault, seProductH, seProductS); }
EX bool supports_invert() { return among(spatial_embedding, seDefault, seLowerCurvature, seMuchLowerCurvature, seNil, seSol, seNIH, seSolN, seProductH, seProductS) || any_cylinder(spatial_embedding); }
EX vector<geometryinfo> ginf_backup;
EX eGeometryClass mgclass() {
return (embedded_plane ? ginf_backup : ginf)[geometry].g.kind;
}
EX eGeometryClass ggclass() {
return (flipped ? ginf_backup : ginf)[geometry].g.kind;
}
EX bool any_cylinder(eSpatialEmbedding e) {
return among(e, seCylinderE, seCylinderH, seCylinderHE, seCylinderHoro, seCylinderNil, seCylinderSL2);
}
EX bool in_product() {
return ggclass() == gcProduct;
}
EX bool flipped;
EX geometry_information* unflipped;
EX void light_flip_geom() {
indenter ind(2);
swap(ginf[geometry].g, geom3::ginf_backup[geometry].g);
swap(ginf[geometry].flags, geom3::ginf_backup[geometry].flags);
if(fake::in()) {
// println(hlog, "warning: flipping while still in fake");
FPIU(light_flip_geom());
}
// hyperbolic arcm needs gNormal for cdata. Also swap gSphere and gEuclid for compute_geometry
else if(arcm::in()) {
dynamicval<eGeometry> g(geometry, gNormal); light_flip_geom();
geometry = gSphere; light_flip_geom();
geometry = gEuclid; light_flip_geom();
}
}
EX void light_flip(bool f) {
if(f != flipped) {
if(!flipped) cgip->use_count++;
if(!flipped) unflipped = cgip;
light_flip_geom();
flipped = f;
if(!flipped) cgip = unflipped;
if(!flipped) cgip->use_count--;
}
}
#if HDR
template<class T> auto in_flipped(const T& f) -> decltype(f()) {
light_flip(true);
finalizer ff([] { light_flip(false); });
return f();
}
template<class T> auto in_not_flipped(const T& f) -> decltype(f()) {
light_flip(false);
finalizer ff([] { light_flip(true); });
return f();
}
#define IPF(x) geom3::in_flipped([&] { return (x); })
#endif
EX void apply_always3() {
if(!vid.always3 && !ginf_backup.empty()) {
ginf = ginf_backup;
ginf_backup.clear();
}
if(vid.always3 && ginf_backup.empty()) {
ginf_backup = ginf;
for(geometryinfo& gi: ginf)
apply_always3_to(gi);
}
}
EX void apply_always3_to(geometryinfo& gi) {
auto &g = gi.g;
if(vid.always3 && g.gameplay_dimension == 2 && g.graphical_dimension == 2) {
/* same-in-same by default */
auto og = g;
g.graphical_dimension++;
g.homogeneous_dimension++;
g.sig[3] = g.sig[2];
g.sig[2] = g.sig[1];
bool ieuclid = g.kind == gcEuclid;
bool isphere = g.kind == gcSphere;
bool ieuc_or_binary = ieuclid || (gi.flags & qBINARY);
if(spatial_embedding == seProduct && !ieuclid) g = giProduct, g.sig[2] = og.sig[2];
if(spatial_embedding == seProductH && ieuclid) g = giProductH;
if(spatial_embedding == seProductS && ieuclid) g = giProductS;
if(spatial_embedding == seLowerCurvature) g = (isphere ? giEuclid3 : giHyperb3);
if(spatial_embedding == seMuchLowerCurvature) g = giHyperb3;
if(spatial_embedding == seNil && ieuclid) g = giNil;
if(spatial_embedding == seCliffordTorus && ieuclid) g = giSphere3;
if(spatial_embedding == seSol && ieuc_or_binary) g = giSol;
if(spatial_embedding == seNIH && ieuc_or_binary) g = giNIH;
if(spatial_embedding == seSolN && ieuc_or_binary) g = giSolN;
if(spatial_embedding == seSL2 && ieuclid) g = giSL2;
if(spatial_embedding == seCylinderH && ieuclid) g = giHyperb3;
if(spatial_embedding == seCylinderHE && ieuclid) g = giProductH;
if(spatial_embedding == seCylinderHoro && ieuclid) g = giProductH;
if(spatial_embedding == seCylinderNil && ieuclid) g = giNil;
if(spatial_embedding == seCylinderSL2 && ieuclid) g = giSL2;
g.gameplay_dimension = 2;
}
}
EX void configure_clifford_torus() {
#if CAP_RUG
dynamicval<ld> dtessf(cgi.tessf, 1);
rug::clifford_torus ct;
if(hypot_d(2, ct.xh) < 1e-6 || hypot_d(2, ct.yh) < 1e-6) {
euclid_embed_scale = TAU / 20.;
euclid_embed_scale_y = 1;
euclid_embed_rotate = 0;
vid.depth = 45._deg - 1;
vid.wall_height = 0.2;
vid.eye = vid.wall_height / 2 - vid.depth;
return;
}
euclid_embed_scale = TAU / hypot_d(2, ct.xh);
euclid_embed_scale_y = TAU / hypot_d(2, ct.yh) / euclid_embed_scale;
euclid_embed_rotate = atan2(ct.xh[1], ct.xh[0]) / degree;
ld alpha = atan2(ct.xfactor, ct.yfactor);
vid.depth = alpha - 1;
vid.wall_height = min(1 / euclid_embed_scale_mean(), (90._deg - alpha) * 0.9);
vid.eye = vid.wall_height / 2 - vid.depth;
#endif
}
EX void configure_cylinder() {
#if CAP_RUG
dynamicval<ld> dtessf(cgi.tessf, 1);
rug::clifford_torus ct;
hyperpoint vec;
if(sqhypot_d(2, ct.yh) > 1e-6) vec = ct.yh;
else if(sqhypot_d(2, ct.xh) > 1e-6) vec = ct.xh;
else vec = hyperpoint(10, 0, 0, 0);
euclid_embed_scale = TAU / hypot_d(2, vec);
euclid_embed_scale_y = 1;
euclid_embed_rotate = atan2(vec[1], vec[0]) / degree;
#endif
}
EX }
#if HDR
struct embedding_method {
virtual ld center_z() { return 0; }
virtual hyperpoint tile_center();
virtual transmatrix intermediate_to_actual_translation(hyperpoint i) = 0;
virtual hyperpoint intermediate_to_actual(hyperpoint i) { return intermediate_to_actual_translation(i) * tile_center(); }
virtual hyperpoint actual_to_intermediate(hyperpoint a) = 0;
virtual hyperpoint orthogonal_move(const hyperpoint& a, ld z);
virtual transmatrix map_relative_push(hyperpoint h);
virtual ld get_logical_z(hyperpoint a) { return (intermediate_to_logical_scaled * actual_to_intermediate(a))[2]; }
virtual hyperpoint logical_to_actual(hyperpoint l) { return intermediate_to_actual(logical_to_intermediate * l); }
virtual hyperpoint actual_to_logical(hyperpoint a) { return intermediate_to_logical * actual_to_intermediate(a); }
virtual hyperpoint base_to_actual(hyperpoint h) = 0;
virtual transmatrix base_to_actual(const transmatrix &T) = 0;
virtual hyperpoint actual_to_base(hyperpoint h) = 0;
virtual transmatrix actual_to_base(const transmatrix &T) = 0;
virtual hyperpoint normalize_flat(hyperpoint a) { return flatten(normalize(a)); }
virtual hyperpoint flatten(hyperpoint a);
virtual void set_radar_transform();
virtual transmatrix get_lsti() { return Id; }
virtual transmatrix get_lti() { return logical_scaled_to_intermediate; }
virtual hyperpoint base_to_logical(hyperpoint h) = 0;
virtual hyperpoint logical_to_base(hyperpoint h) = 0;
virtual ld anim_center_z() { return center_z(); }
virtual hyperpoint anim_tile_center();
virtual void logical_fix(transmatrix&) = 0;
virtual ld height_limit(ld sign);
virtual bool is_euc_in_product() { return false; }
virtual bool is_product_embedding() { return false; }
virtual bool is_euc_in_sl2() { return false; }
virtual bool is_same_in_same() { return false; }
virtual bool is_sph_in_low() { return false; }
virtual bool is_hyp_in_solnih() { return false; }
virtual bool is_euc_scalable() { return false; }
virtual bool is_euc_in_hyp() { return false; }
virtual bool is_euc_in_sph() { return false; }
virtual bool is_euc_in_nil() { return false; }
virtual bool is_euc_in_noniso() { return false; }
virtual bool is_in_noniso() { return false; }
virtual bool is_cylinder() { return false; }
virtual bool no_spin() { return false; }
/* convert the tangent space in logical coordinates to actual coordinates */
transmatrix logical_to_intermediate;
/* convert the tangent space in actual coordinates to logical coordinates */
transmatrix intermediate_to_logical;
/* convert the tangent space in logical coordinates to actual coordinates */
transmatrix logical_scaled_to_intermediate;
/* convert the tangent space in actual coordinates to logical coordinates */
transmatrix intermediate_to_logical_scaled;
void prepare_lta();
void auto_configure();
virtual ~embedding_method() {}
/* should we break cylinder between M1 and M2 */
virtual bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) { return false; }
};
#endif
EX geometry_information *swapper;
ld embedding_method::height_limit(ld sign) {
if(sign > 0) {
if(sol || nih) return 2.5;
if(hyperbolic || sl2 || in_h2xe()) return 5;
if(sphere || nil || in_s2xe()) return M_PI/2;
return 100;
}
if(sign < 0) {
if(center_z()) return -center_z();
if(sol || nih) return -2.5;
if(hyperbolic || sl2 || in_h2xe()) return -5;
if(sphere || nil || in_s2xe()) return -M_PI/2;
return -100;
}
return 0;
}
hyperpoint embedding_method::tile_center() {
ld z = center_z();
if(z == 0) return C0;
return lzpush(z) * C0;
}
hyperpoint embedding_method::anim_tile_center() {
ld z = anim_center_z();
if(z == 0) return C0;
return lzpush(z) * C0;
}
transmatrix embedding_method::map_relative_push(hyperpoint a) {
auto i = actual_to_intermediate(a);
return intermediate_to_actual_translation(i);
}
hyperpoint embedding_method::orthogonal_move(const hyperpoint& a, ld z) {
auto i = actual_to_intermediate(a);
auto l = intermediate_to_logical_scaled * i;
l[2] += z;
i = logical_scaled_to_intermediate * l;
return intermediate_to_actual(i);
}
hyperpoint embedding_method::flatten(hyperpoint a) {
auto i = actual_to_intermediate(a);
auto l = intermediate_to_logical * i;
l[2] = 0; i = logical_to_intermediate * l;
return intermediate_to_actual(i);
}
/** dummy 'embedding method' used when no embedding is used (2D engine or 3D map) */
struct emb_none : embedding_method {
hyperpoint actual_to_intermediate(hyperpoint a) override {
if(mhybrid) return base_to_logical(a);
return a;
}
hyperpoint intermediate_to_actual(hyperpoint i) override {
if(mhybrid) return logical_to_base(i);
return i;
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
if(gproduct) i = intermediate_to_actual(i);
return rgpushxto0(i);
}
hyperpoint flatten(hyperpoint a) override {
if(gproduct || GDIM == 2) return a / exp(zlevel(a));
return embedding_method::flatten(a);
}
hyperpoint normalize_flat(hyperpoint a) override { return gproduct ? flatten(a) : normalize(a); }
transmatrix base_to_actual(const transmatrix& T) override { return T; }
hyperpoint base_to_actual(hyperpoint h) override { return h; }
transmatrix actual_to_base(const transmatrix& T) override { return T; }
hyperpoint actual_to_base(hyperpoint h) override { return h; }
hyperpoint orthogonal_move(const hyperpoint& h, ld z) override {
if(GDIM == 2) return scale_point(h, geom3::scale_at_lev(z));
if(gproduct) return scale_point(h, exp(z));
if(sl2) return slr::translate(h) * cpush0(2, z);
if(nil) return nisot::translate(h) * cpush0(2, z);
if(translatable) return hpxy3(h[0], h[1], h[2] + z);
/* copied from emb_same_in_same */
ld u = 1;
if(h[2]) z += asin_auto(h[2]), u /= cos_auto(asin_auto(h[2]));
u *= cos_auto(z);
return hpxy3(h[0] * u, h[1] * u, sinh(z));
}
hyperpoint base_to_logical(hyperpoint h) override {
if(sn::in() || !bt::in())
return h;
#if CAP_BT
if(bt::in() && !mproduct) return bt::minkowski_to_bt(h);
#endif
if(mproduct) {
ld z = zlevel(h);
h /= h[2];
h[2] = z;
}
if(sl2) {
ld z = atan2(h[2], h[3]);
h = slr::translate(h) * zpush0(-atan2(h[2], h[3]));
h[0] = h[0] / h[3]; h[1] = h[1] / h[3]; h[2] = z;
return h;
}
return h;
}
hyperpoint logical_to_base(hyperpoint h) override {
if(sn::in() || !bt::in())
return ultra_normalize(h);
#if CAP_BT
if(bt::in() && !mproduct)
return bt::bt_to_minkowski(h);
#endif
if(mproduct) {
ld z = h[2];
h[2] = 1;
flatten(h);
h *= exp(z);
}
if(sl2) {
ld z = h[2];
h[2] = 0; h[3] = 1; normalize(h);
h = slr::translate(h) * zpush0(z);
return h;
}
return h;
}
void logical_fix(transmatrix& T) override {
if(nonisotropic) {
hyperpoint h = tC0(T);
transmatrix rot = gpushxto0(h) * T;
fix_rotation(rot);
T = rgpushxto0(h) * rot;
}
else fixmatrix(T);
fixelliptic(T);
}
};
/** embeddings methods that are not emb_none */
struct emb_actual : embedding_method {
hyperpoint base_to_logical(hyperpoint h) override {
#if CAP_BT
if(bt::in()) {
auto h1 = bt::inverse_horopoint(h);
h1[2] = 0; h1[3] = 1;
return h1;
}
#endif
h /= h[2];
h[2] = 0; h[3] = 1;
return h;
}
hyperpoint logical_to_base(hyperpoint h) override {
#if CAP_BT
if(bt::in()) {
auto h1 = bt::get_horopoint(h);
h1[3] = 1;
return h1;
}
#endif
h[2] = 1; h = normalize(h);
h[3] = 1;
return h;
}
void logical_fix(transmatrix& T) override {
hyperpoint a = T * tile_center();
hyperpoint i0 = actual_to_intermediate(a);
auto l0 = intermediate_to_logical * i0;
auto l = l0; l[2] = 0;
auto i = logical_to_intermediate * l;
auto rot0= inverse(intermediate_to_actual_translation(i0)) * T ;
auto rot = intermediate_to_logical_scaled * rot0 * logical_scaled_to_intermediate;
ld alpha = atan2(rot[0][1], rot[0][0]);
T = intermediate_to_actual_translation(i) * spin(alpha);
fixelliptic(T);
}
};
/** embed in the 3D variant of the same geometry */
struct emb_same_in_same : emb_actual {
bool is_same_in_same() override { return true; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override { return rgpushxto0(logical_to_actual(i)); }
hyperpoint actual_to_intermediate(hyperpoint a) override { return actual_to_logical(a); }
hyperpoint orthogonal_move(const hyperpoint& h, ld z) override {
if(euclid) { hyperpoint h1 = h; h1[2] += z; return h1; }
ld u = 1;
if(h[2]) z += asin_auto(h[2]), u /= cos_auto(asin_auto(h[2]));
u *= cos_auto(z);
return hpxy3(h[0] * u, h[1] * u, sinh(z));
}
transmatrix base_to_actual(const transmatrix &T0) override {
auto T = T0;
for(int i=0; i<4; i++) T[i][3] = T[i][2], T[i][2] = 0;
for(int i=0; i<4; i++) T[3][i] = T[2][i], T[i][2] = 0;
for(int i=0; i<4; i++) T[i][2] = T[2][i] = 0;
T[2][2] = 1;
return T;
}
transmatrix actual_to_base(const transmatrix &T0) override {
auto T = T0;
for(int i=0; i<4; i++) T[i][2] = T[i][3], T[i][3] = 0;
for(int i=0; i<4; i++) T[2][i] = T[3][i], T[3][i] = 0;
T[3][3] = 1;
if(MDIM == 3) fixmatrix(T); else IPF(fixmatrix(T));
for(int i=0; i<MDIM; i++) for(int j=0; j<MDIM; j++) if(isnan(T[i][j])) return Id;
return T;
}
hyperpoint base_to_actual(hyperpoint h) override {
h[3] = h[2]; h[2] = 0;
return h;
}
hyperpoint actual_to_base(hyperpoint h) override {
h[2] = h[3]; h[3] = 0;
return h;
}
transmatrix map_relative_push(hyperpoint h) override {
ld z = asin_auto(h[2]);
ld u = 1 / cos_auto(z);
auto h1 = hpxy3(h[0] * u, h[1] * u, 0);
return rgpushxto0(h1) * zpush(z);
}
hyperpoint actual_to_logical(hyperpoint h) override {
if(euclid) { h[3] = 1; return h; }
ld z = asin_auto(h[2]);
ld u = 1 / cos_auto(z);
auto h1 = hpxy3(h[0] * u, h[1] * u, 0);
h1[2] = h1[3];
geom3::light_flip(true);
h1 = base_to_logical(h1);
geom3::light_flip(false);
h1[2] = z;
return h1;
}
hyperpoint logical_to_actual(hyperpoint h) override {
if(euclid) { h[3] = 1; return h; }
geom3::light_flip(true);
auto b = logical_to_base(h);
geom3::light_flip(false);
b[3] = b[2]; b[2] = 0;
return orthogonal_move(b, h[2]);
}
hyperpoint flatten(hyperpoint h) override {
ld z = asin_auto(h[2]);
ld u = 1 / cos_auto(z);
return hpxy3(h[0] * u, h[1] * u, 0);
}
void logical_fix(transmatrix& T) override {
// optimization
for(int i=0; i<4; i++) T[i][2] = T[2][i] = i == 2;
fixmatrix(T);
fixelliptic(T);
}
};
/** embed in the product geometry */
struct emb_product_embedding : emb_actual {
bool is_product_embedding() override { return true; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override { return rgpushxto0(logical_to_actual(i)); }
hyperpoint actual_to_intermediate(hyperpoint a) override { return actual_to_logical(a); }
hyperpoint flatten(hyperpoint h) override { h /= exp(zlevel(h)); return h; }
hyperpoint orthogonal_move(const hyperpoint& h, ld z) override { return h * exp(z); }
transmatrix base_to_actual(const transmatrix &T) override { return T; }
transmatrix actual_to_base(const transmatrix &T0) override {
auto T = T0; fixmatrix(T);
for(int i=0; i<MDIM; i++) for(int j=0; j<MDIM; j++) if(isnan(T[i][j])) return Id;
return T;
}
hyperpoint base_to_actual(hyperpoint h) override { return h; }
hyperpoint actual_to_base(hyperpoint h) override { return flatten(h); }
transmatrix map_relative_push(hyperpoint h) override { return rgpushxto0(h); }
hyperpoint actual_to_logical(hyperpoint h) override {
ld z = zlevel(h);
h /= exp(z);
h = base_to_logical(h);
h[2] = z;
return h;
}
hyperpoint logical_to_actual(hyperpoint h) override {
return logical_to_base(h) * exp(h[2]);
}
};
struct emb_euc_scalable : emb_actual {
bool is_euc_scalable() override { return true; }
transmatrix get_lti() override {
transmatrix lti = Id;
lti[0][0] *= geom3::euclid_embed_scale;
lti[1][1] *= geom3::euclid_embed_scale * geom3::euclid_embed_scale_y;
return logical_scaled_to_intermediate * cspin(0, 1, geom3::euclid_embed_rotate * degree) * lti;
}
};
/** embed Euclidean plane as horosphere */
struct emb_euc_in_hyp : emb_euc_scalable {
bool is_euc_in_hyp() override { return true; }
hyperpoint actual_to_intermediate(hyperpoint a) override { return deparabolic13(a); }
transmatrix intermediate_to_actual_translation(hyperpoint i) override { return parabolic13_at(i); }
transmatrix base_to_actual(const transmatrix &T) override {
geom3::light_flip(true);
hyperpoint mov = T * C02;
transmatrix U = gpushxto0(mov) * T;
mov = logical_to_intermediate * mov;
geom3::light_flip(false);
for(int i=0; i<4; i++) U[i][3] = U[3][i] = i == 3;
return parabolic13(mov[0], mov[1]) * U;
}
hyperpoint base_to_actual(hyperpoint h) override {
h = logical_to_intermediate * h;
h[3] = h[2]; h[2] = 0; return parabolic13(h[0], h[1]) * C0;
}
hyperpoint actual_to_base(hyperpoint h) override {
hyperpoint h1 = deparabolic13(h); h1[2] = 1;
return intermediate_to_logical * h1;
}
transmatrix actual_to_base(const transmatrix& T) override { hyperpoint h = deparabolic13(T * C0); return eupush(h[0], h[1]); }
ld anim_center_z() override { return vid.depth; }
};
/** sphere into a isotropic space of higher curvature */
struct emb_sphere_in_low : emb_actual {
bool is_sph_in_low() override { return true; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return map_relative_push(logical_to_actual(i)) * zpush(-1);
}
hyperpoint actual_to_intermediate(hyperpoint a) override { return actual_to_logical(a); }
ld center_z() override { return 1; }
transmatrix map_relative_push(hyperpoint a) override {
ld z = hdist0(a);
geom3::light_flip(true);
auto h1 = normalize(a);
transmatrix T = rgpushxto0(h1);
geom3::light_flip(false);
return T * zpush(z);
}
transmatrix base_to_actual(const transmatrix &T0) override {
auto T = T0;
for(int i=0; i<4; i++) T[i][3] = T[3][i] = i == 3;
return T;
}
hyperpoint base_to_actual(hyperpoint h) override {
if(euclid) h[3] = 1;
else h *= sinh(1), h[3] = cosh(1);
return h;
}
hyperpoint actual_to_base(hyperpoint h) override { return h; }
transmatrix actual_to_base(const transmatrix& T) override { return T; }
ld get_logical_z(hyperpoint a) override { return hdist0(a) - 1; }
hyperpoint flatten(hyperpoint a) override {
ld d = hdist0(a);
if(d == 0) return a;
a *= sin_auto(1) / sin_auto(d);
a[3] = cos_auto(1);
return a;
}
hyperpoint orthogonal_move(const hyperpoint& h, ld z) override {
ld z0 = hdist0(h);
ld f = sin_auto(z0 + z) / sin_auto(z0);
hyperpoint hf = h * f;
hf[3] = cos_auto(z0 + z);
return hf;
}
hyperpoint logical_to_actual(hyperpoint h) override {
auto z = h[2];
h[2] = 1;
geom3::light_flip(true);
h = normalize(h);
geom3::light_flip(false);
h *= sin_auto(1 + z);
h[3] = cos_auto(1 + z);
return h;
}
hyperpoint actual_to_logical(hyperpoint h) override {
ld z = get_logical_z(h);
geom3::light_flip(true);
h = kleinize(h);
geom3::light_flip(false);
h[2] = z; h[3] = 1;
return h;
}
void logical_fix(transmatrix& T) override {
fix4(T);
fixmatrix(T);
fixelliptic(T);
}
};
/** abstract class for embeddings of Euclidean plane; these embeddings are not isotropic */
struct emb_euclid_noniso : emb_euc_scalable {
bool is_euc_in_noniso() override { return true; }
bool is_in_noniso() override { return true; }
transmatrix base_to_actual(const transmatrix &T) override {
auto T0 = T;
hyperpoint h = get_column(T0, 2);
h[2] = 0; h[3] = 1;
return intermediate_to_actual_translation( logical_to_intermediate * h);
}
hyperpoint base_to_actual(hyperpoint h) override {
h[2] = 0; h[3] = 1;
return intermediate_to_actual_translation( logical_to_intermediate * h ) * tile_center();
}
hyperpoint actual_to_base(hyperpoint h) override {
hyperpoint h1 = intermediate_to_logical * actual_to_intermediate(h);
h1[2] = 1; h1[3] = 0;
return h1;
}
transmatrix actual_to_base(const transmatrix& T) override { hyperpoint h = actual_to_base(T * tile_center()); return eupush(h[0], h[1]); }
};
struct emb_euc_in_product : emb_euclid_noniso {
bool is_euc_in_product() override { return true; }
bool no_spin() override { return true; }
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld bz = zlevel(a);
auto h1 = a / exp(bz);
ld by = asin_auto(h1[1]);
ld bx = atan2_auto(h1[0], h1[2]);
return hyperpoint(bx, by, bz, 1);
}
transmatrix get_lsti() override { return cspin90(2, 1); }
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return zpush(i[2]) * xpush(i[0]) * ypush(i[1]);
}
};
struct emb_euc_in_sl2 : emb_euclid_noniso {
transmatrix esl2_zpush(ld z) { return cspin(2, 3, z) * cspin(0, 1, z); }
hyperpoint intermediate_to_actual(hyperpoint i) override {
return esl2_zpush(i[2]) * xpush(i[0]) * ypush0(i[1]);
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return esl2_zpush(i[2]) * xpush(i[0]) * ypush(i[1]);
}
hyperpoint actual_to_intermediate(hyperpoint h) override {
ld a1 = (h[0] * h[3] - h[1] * h[2]) / (-h[2] * h[2] - h[1] * h[1] -h[0] * h[0] - h[3] * h[3]);
// a1 is S*sqrt(1+S*S) / (1+2*S*S), where S = sinh(-x) and C = cosh(-x); U is S*S
ld a = a1 * a1;
ld b = 4 * a - 1;
ld U = sqrt(.25 - a/b) - .5;
ld S = sqrt(U) * (a1 > 0 ? 1 : -1);
ld x = -asinh(S);
h = lorentz(0, 3, -x) * lorentz(1, 2, x) * h;
ld y = h[3]*h[3] > h[2]*h[2] ? atanh(h[1] / h[3]) : atanh(h[0] / h[2]);
h = lorentz(0, 2, -y) * lorentz(1, 3, -y) * h;
ld z = atan2(h[2], h[3]);
return hyperpoint(x, y, z, 0);
}
bool is_euc_in_sl2() override { return true; }
bool no_spin() override { return true; }
transmatrix get_lsti() override { return cspin90(2, 1); }
};
bool break_dims(const shiftmatrix& M1, const shiftmatrix& M2, int i, int j) {
transmatrix uM1 = current_display->radar_transform * unshift(M1);
transmatrix uM2 = current_display->radar_transform * unshift(M2);
return uM1[j][j] < 0 && uM2[j][j] < 0 && uM1[i][j] * uM2[i][j] < 0;
}
/* for both seCylinderH and seCylinderE. Possibly actually works for CliffordTorus too */
struct emb_euc_cylinder : emb_euclid_noniso {
bool is_cylinder() override { return true; }
ld center_z() override { return 1; }
transmatrix get_lsti() override { return cspin90(0, 1); }
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld z0 = asin_auto(hypot(a[1], a[2]));
ld x0 = a[0];
if(z0 == 0) return hyperpoint(x0, 0, 0, 1);
x0 = asin_auto(x0 / cos_auto(z0));
ld y0 = z0 ? atan2(a[1], a[2]) : 0;
return point31(x0, y0, z0-1);
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return xpush(i[0]) * cspin(1, 2, i[1]) * zpush(i[2]);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return break_dims(M1, M2, 1, 2); }
};
struct emb_euc_cylinder_he : emb_euc_cylinder {
bool no_spin() override { return true; }
transmatrix get_lsti() override { return cspin90(0, 2) * cspin90(0, 1); }
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld z0 = zlevel(a);
a /= exp(z0);
ld y0 = atan2(a[1], a[0]);
ld x0 = asin_auto(hypot(a[0], a[1]));
return hyperpoint(x0-1, y0, z0, 1);
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return zpush(i[2]) * cspin(1, 0, i[1]) * xpush(i[0]);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return break_dims(M1, M2, 1, 0); }
};
struct emb_euc_cylinder_twisted : emb_euc_cylinder {
transmatrix get_lsti() override { return cspin90(0, 2) * cspin90(0, 1); }
transmatrix get_lti() override {
ld depth = 0; // for now?
ld alpha = nil ? (1 + depth) / 2. : sinh(1 + depth) / 2.;
ld c = pow(1 + alpha * alpha, -0.5);
transmatrix U = Id;
U[1][1] = (alpha*alpha+1) * c;
U[0][1] = alpha * c;
return logical_scaled_to_intermediate * U * intermediate_to_logical_scaled * emb_euc_cylinder::get_lti();
}
};
struct emb_euc_cylinder_nil : emb_euc_cylinder_twisted {
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld y0 = atan2(a[1], a[0]);
ld x0 = hypot(a[0], a[1]);
return hyperpoint(x0-1, y0, a[2], 1);
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return zpush(i[2]) * cspin(1, 0, i[1]) * xpush(i[0]);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return break_dims(M1, M2, 1, 0); }
};
struct emb_euc_cylinder_horo : emb_euc_cylinder {
ld center_z() override { return 0; }
bool no_spin() override { return true; }
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld z0 = zlevel(a);
a /= exp(z0);
auto hy = deparabolic13(a);
hy[2] = z0;
return hy;
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return zpush(i[2]) * parabolic1(i[1]) * xpush(i[0]);
}
transmatrix get_lsti() override {
return cspin90(0, 2);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return false; }
};
struct emb_euc_cylinder_sl2 : emb_euc_cylinder_twisted {
bool no_spin() override { return true; }
hyperpoint actual_to_intermediate(hyperpoint a) override {
hyperpoint i = point31(0, 0, 0);
i[2] = atan2(a[2], a[3]);
a = cspin(1, 0, i[2]) * cspin(3, 2, i[2]) * a;
i[1] = (a[0] || a[1]) ? -atan2(a[1], a[0]) : 0;
a = cspin(1, 0, i[1]) * a;
i[0] = asinh(a[0])-1;
return i;
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return cspin(2, 3, i[2]) * cspin(0, 1, i[2] + i[1]) * xpush(i[0]);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return break_dims(M1, M2, 0, 1); }
};
/** Clifford torus */
struct emb_euc_in_sph : emb_euclid_noniso {
bool is_euc_in_sph() override { return true; }
ld center_z() override { return 1; }
// ld height_limit(ld sign) override { return sign < 0 ? 0 : 90._deg; }
hyperpoint actual_to_intermediate(hyperpoint a) override {
ld tx = hypot(a[0], a[2]);
ld ty = hypot(a[1], a[3]);
ld x0 = atan2(a[0], a[2]);
ld y0 = atan2(a[1], a[3]);
ld z0 = atan2(tx, ty);
return hyperpoint(x0, y0, z0-1, 1);
}
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
return cspin(0, 2, i[0]) * cspin(1, 3, i[1]) * cspin(2, 3, i[2]);
}
bool break_cylinder(const shiftmatrix& M1, const shiftmatrix& M2) override { return break_dims(M1, M2, 0, 2) || break_dims(M1, M2, 1, 3); }
};
/* todo model change */
struct emb_euc_in_nil : emb_euclid_noniso {
bool is_euc_in_nil() override { return true; }
hyperpoint actual_to_intermediate(hyperpoint a) override { a[2] -= a[0] * a[1] / 2; return a; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override { i[2] += i[0] * i[1] / 2; return rgpushxto0(i); }
transmatrix get_lsti() override { return cspin90(2, 1); }
};
struct emb_euc_in_solnih : emb_euclid_noniso {
hyperpoint actual_to_intermediate(hyperpoint a) override { return a; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override { return rgpushxto0(i); }
};
struct emb_hyp_in_solnih : emb_actual {
bool is_hyp_in_solnih() override { return true; }
bool is_in_noniso() override { return true; }
transmatrix intermediate_to_actual_translation(hyperpoint i) override {
if(cgclass == gcSol) i[0] *= exp(-i[2]);
if(cgclass == gcSolN) i[0] *= pow(2, -i[2]);
if(cgclass == gcNIH) i[0] *= pow(2, i[2]);
return rgpushxto0(i);
}
hyperpoint actual_to_intermediate(hyperpoint a) override {
if(cgclass == gcSol) a[0] *= exp(a[2]);
if(cgclass == gcSolN) a[0] *= pow(2, a[2]);
if(cgclass == gcNIH) a[0] *= pow(2, -a[2]);
return a;
}
transmatrix base_to_actual(const transmatrix &T) override {
auto T1 = T;
auto h = get_column(T1, 2);
return rgpushxto0(base_to_actual(h));
}
hyperpoint base_to_actual(hyperpoint h) override {
// copied from deparabolic13
h /= (1 + h[2]);
h[0] -= 1;
h /= sqhypot_d(2, h);
h[0] += .5;
ld hx = log(2) + log(-h[0]);
if(cgclass == gcNIH) hx /= log(3);
if(cgclass == gcSolN) hx /= log(3);
ld hy = h[1] * 2;
return point31(0, -hy, hx);
}
transmatrix actual_to_base(const transmatrix& T) override {
hyperpoint h = T * C0;
auto f = geom3::flipped;
geom3::light_flip(true);
transmatrix b = parabolic1(h[1]) * xpush(h[2]);
geom3::light_flip(f);
return b;
}
hyperpoint actual_to_base(hyperpoint h) override {
auto f = geom3::flipped;
geom3::light_flip(true);
hyperpoint b = parabolic1(h[1]) * xpush0(h[2]);
geom3::light_flip(f);
return b;
}
transmatrix get_lsti() override { return cspin90(0, 1) * cspin90(1, 2) * cspin90(0, 1); }
hyperpoint orthogonal_move(const hyperpoint& a, ld z) override { return nisot::translate(a) * cpush0(0, z); }
};
/* the remaining methods */
/*=======================*/
void embedding_method::prepare_lta() {
bool b = geom3::flipped;
if(b) geom3::light_flip(false);