-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog4.cxx
1069 lines (937 loc) · 23.6 KB
/
prog4.cxx
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
#include <cassert>
#include <cstdio>
#include <memory>
#include <unordered_map>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include "averager.hxx"
#include "math.hxx"
#include "desc/riemann.hxx"
#include "desc/ray.hxx"
#include "desc/euclidean.hxx"
#include "desc/flat.hxx"
#include "desc/fast.hxx"
#include "desc/box.hxx"
struct Hit {
vec2 point;
};
class Shape {
public:
virtual bool hit(Hit *hit, Ray ray) const {
return false;
}
virtual void preview() const {}
};
class TransformedShape final: public Shape {
public:
bool hit(Hit *hit, Ray ray) const noexcept final override {
ray.base = inverse(tfm) * (ray.base - pos);
ray.dir = normalize(inverse(tfm) * ray.dir);
if (!base->hit(hit, ray))
return false;
hit->point = tfm * hit->point + pos;
return true;
}
void preview() const noexcept final override {
glPushMatrix();
mat4 m = tfm;
glTranslatef(pos.x, pos.y, 0.0);
glMultMatrixf(value_ptr(m));
base->preview();
glPopMatrix();
}
Shape const *base;
mat2 tfm;
vec2 pos;
};
class UnitCircle final: public Shape {
public:
bool hit(Hit *hit, Ray ray) const noexcept override {
static constexpr float eps = 1e-3f;
vec2 L = -ray.base;
float tca = dot(L, ray.dir);
if (tca < 0) return false;
float d2 = dot(L, L) - tca*tca;
if (d2 > 1.0f) return false;
float thc = std::sqrt(1.0f - d2);
float t0 = tca-thc, t1 = tca+thc;
if (t0 > eps) return make(hit, ray, t0);
if (t1 > eps) return make(hit, ray, t1);
return false;
}
bool make(Hit *hit, Ray ray, float t) const noexcept {
hit->point = ray.base + t * ray.dir;
return true;
}
void preview() const noexcept override {
glBegin(GL_LINE_LOOP);
for (int k = 0; k < 120; k++) {
float phi = M_PI * k / 60.0;
glVertex2f(cos(phi), sin(phi));
}
glEnd();
}
static const UnitCircle instance;
};
const UnitCircle UnitCircle::instance;
class UnitLine final: public Shape {
public:
bool hit(Hit *hit, Ray ray) const noexcept override {
if (ray.dir.x > 0.0f && ray.base.x < 0.0f) {
float dx = -ray.base.x;
float dy = dx * ray.dir.y / ray.dir.x;
float y = ray.base.y + dy;
if (std::abs(y) <= 1.0f) {
hit->point = {0.0f, y};
return true;
}
}
return false;
}
void preview() const noexcept override {
glBegin(GL_LINES);
glVertex2f(0.0, -1.0);
glVertex2f(0.0, 1.0);
glEnd();
}
static const UnitLine instance;
};
const UnitLine UnitLine::instance;
class UnitSquare final: public Shape {
public:
bool hit(Hit *hit, Ray ray) const noexcept override {
if (ray.dir.x > 0.0f && ray.base.x < -1.0f) {
float dx = -1.0f - ray.base.x;
float dy = dx * ray.dir.y / ray.dir.x;
float y = ray.base.y + dy;
if (std::abs(y) <= 1.0f) {
hit->point = {-1.0f, y};
return true;
}
}
if (ray.dir.x < 0.0f && ray.base.x > 1.0f) {
float dx = ray.base.x - 1.0f;
float dy = -dx * ray.dir.y / ray.dir.x;
float y = ray.base.y + dy;
if (std::abs(y) <= 1.0f) {
hit->point = {1.0f, y};
return true;
}
}
if (ray.dir.y > 0.0f && ray.base.y < -1.0f) {
float dy = -1.0f - ray.base.y;
float dx = dy * ray.dir.x / ray.dir.y;
float x = ray.base.x + dx;
if (std::abs(x) <= 1.0f) {
hit->point = {x, -1.0f};
return true;
}
}
if (ray.dir.y < 0.0f && ray.base.y > 1.0f) {
float dy = ray.base.y - 1.0f;
float dx = -dy * ray.dir.x / ray.dir.y;
float x = ray.base.x + dx;
if (std::abs(x) <= 1.0f) {
hit->point = {x, 1.0f};
return true;
}
}
return false;
}
void preview() const noexcept override {
glBegin(GL_LINE_LOOP);
glVertex2f(-1.0, -1.0);
glVertex2f(1.0, -1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
}
static const UnitSquare instance;
};
const UnitSquare UnitSquare::instance;
class UnitSquareInv final: public Shape {
public:
bool hit(Hit *hit, Ray ray) const noexcept override {
// assert(abs(ray.base.x) < 1.0f + 1e-2);
// assert(abs(ray.base.y) < 1.0f + 1e-2);
if (ray.dir.x < 0.0f) {
float dx = -1.0f - ray.base.x;
float dy = dx * ray.dir.y / ray.dir.x;
float y = ray.base.y + dy;
if (std::abs(y) <= 1.0f) {
hit->point = {-1.0f, y};
return true;
}
}
if (ray.dir.x > 0.0f) {
float dx = ray.base.x - 1.0f;
float dy = -dx * ray.dir.y / ray.dir.x;
float y = ray.base.y + dy;
if (std::abs(y) <= 1.0f) {
hit->point = {1.0f, y};
return true;
}
}
if (ray.dir.y < 0.0f) {
float dy = -1.0f - ray.base.y;
float dx = dy * ray.dir.x / ray.dir.y;
float x = ray.base.x + dx;
if (std::abs(x) <= 1.0f) {
hit->point = {x, -1.0f};
return true;
}
}
if (ray.dir.y > 0.0f) {
float dy = ray.base.y - 1.0f;
float dx = -dy * ray.dir.x / ray.dir.y;
float x = ray.base.x + dx;
if (std::abs(x) <= 1.0f) {
hit->point = {x, 1.0f};
return true;
}
}
abort();
}
void preview() const noexcept override {
glBegin(GL_LINE_LOOP);
glVertex2f(-1.0, -1.0);
glVertex2f(1.0, -1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
}
static const UnitSquareInv instance;
};
const UnitSquareInv UnitSquareInv::instance;
/*
class CoordinateSystem {
public:
virtual decomp2 halfmetric(vec2 pos) = 0;
mat2 metric(vec2 pos) {
decomp2 h = halfmetric(pos);
return transpose(h.ortho) * diagonal(h.diag * h.diag) * h.ortho;
}
};
static constexpr float eps = 1e-3f;
class Universe;
class Region {
public:
vec3 color;
struct Next {
Region *region;
vec2 pos;
float dist;
};
virtual Next next(Ray ray) = 0;
};
class Object: public Region {
public:
};
class Space: public Region {
public:
Shape *shape;
};
class Flat: public Space {
public:
struct Location {
Object *object;
vec2 pos;
float radius;
};
std::vector<Location> objects;
Next next(Ray ray) override {
Next n;
n.dist = MAXFLOAT;
for (auto loc: objects) {
vec2 rel = loc.pos - ray.base;
float a = -dot(rel, ray.dir);
if (a < 0) continue;
float d2 = dot(rel, rel) - a*a;
float b = loc.radius * loc.radius - d2;
if (b < 0) continue;
float t = a-std::sqrt(b);
if (t > n.dist)
continue;
if (t > eps) {
n.region = loc.object;
n.dist = t;
n.pos = ray.base + t * ray.dir;
}
}
return n;
}
};
class Universe: public Flat {
public:
std::vector<Space *> subspaces;
std::vector<Object *> objects;
Next next(Ray ray) override {
Next n = Flat::next(ray);
for (auto *space: subspaces) {
Hit h;
if (space->shape->hit(&h, ray)) {
float t = distance(ray.base, h.point);
if (t > n.dist)
continue;
n.region = space;
n.pos = h.point;
n.dist = t;
}
}
return n;
}
};
*/
/*
class Gluing {
public:
virtual void convertForward(Ray *ray) = 0;
virtual void convertBackward(Ray *ray) = 0;
};
*/
class Universe;
class Space;
/*
class Object {
};
*/
struct Track {
std::vector<vec2> points;
vec2 dir;
Space *next = nullptr;
float length;
};
class Region {
public:
virtual bool contains(vec2 p) const noexcept = 0;
};
class EmptyRegion final: public Region {
public:
bool contains([[maybe_unused]] vec2 p) const noexcept override {
return false;
}
};
class Space {
public:
virtual Track trace(Ray ray, float maxd) const = 0;
};
class Subspace: public Space {
public:
Region const *region;
};
struct Transition {
Space *into;
Shape *shape;
};
/// Описывает выпуклый многоугольник.
class ConvexPoly final: public Region {
private:
struct Segment {
vec2 normal;
float threshold;
};
std::vector<Segment> segments;
public:
/// Создаёт многоугольник с указанными вершинами.
/// @note Выпуклость не проверяется.
ConvexPoly(std::initializer_list<vec2> points) {
int n = points.size();
if (n < 3)
throw std::invalid_argument("There must be at least three points");
segments.reserve(n);
vec2 a = *std::prev(points.end());
for (vec2 b: points) {
vec2 u = b - a;
vec2 v = {u.y, -u.x};
segments.push_back({
.normal = v,
.threshold = dot(a, v),
});
a = b;
}
}
bool contains(vec2 p) const noexcept override {
for (auto &&seg: segments) {
if (dot(p, seg.normal) > seg.threshold)
return false;
}
return true;
}
};
class ConvexBoundary {
private:
struct Segment {
Space *target;
vec2 normal;
float threshold;
};
std::vector<vec2> points;
std::vector<Segment> segments;
public:
ConvexBoundary(std::vector<vec2> &&_points, std::vector<Space *> const &_targets)
: points(std::move(_points))
{
if (points.size() != _targets.size())
throw std::invalid_argument("There must be exactly one target space per boundary segment");
int n = points.size();
if (n < 3)
throw std::invalid_argument("There must be at least three segments");
segments.resize(n);
for (int k = 0; k < n; k++) {
vec2 a = points[k];
vec2 b = points[(k + 1) % n];
vec2 u = b - a;
vec2 v = {-u.y, u.x};
segments[k].target = _targets[k];
segments[k].normal = v;
segments[k].threshold = dot(a, v);
}
}
bool inside(vec2 p) const noexcept {
for (auto &&seg: segments) {
if (dot(p, seg.normal) > seg.threshold)
return false;
}
return true;
}
Space *next(Ray from) const noexcept {
int n = segments.size();
for (int k = 0; k < n; k++) {
vec2 a = points[k] - from.base;
vec2 b = points[(k + 1) % n] - from.base;
// if (dot(a, from.dir) <= 0.0f)
// continue;
// if (dot(b, from.dir) <= 0.0f)
// continue;
float da = dot(vec2(-a.y, a.x), from.dir);
float db = dot(vec2(-b.y, b.x), from.dir);
if (da <= 0.0f && db >= 0.0f)
return segments[k].target;
}
return nullptr;
}
};
class FlatSubspace: public Subspace {
public:
SpaceDesc const *desc = &EuclideanSpace::instance;
Track trace(Ray ray, float maxd) const override {
static constexpr float dt = 1e-2;
Track track = {};
ray = desc->fromGlobal(ray);
int steps = max(1, int(maxd / dt));
track.points.reserve(steps + 1);
for (int k = 0; k <= steps; k++) {
float t = k * maxd / steps;
vec2 p = ray.base + t * ray.dir;
Ray r = desc->toGlobal({p, ray.dir});
track.points.push_back(r.base);
track.dir = r.dir;
track.length = t;
if (!region->contains(r.base))
break;
}
return track;
}
};
class RiemannSubspace: public Subspace {
public:
RiemannMetric<2> *metric;
Track trace(Ray ray, float maxd) const final override {
static constexpr float dt = 1e-2;
static constexpr float eta = 1e-2;
Track t;
int steps = maxd / dt;
t.points.reserve(steps + 1);
auto p = ray.base;
auto v = ray.dir;
v /= length(p, v);
t.points.push_back(p);
for (int k = 0; k < steps; k++) {
t.length = k * dt;
if (!region->contains(p))
break;
auto a = covar(metric->krist(p), v);
if (dt * ::length(a) > eta) {
int substeps = ceil(dt * ::length(a) / eta);
substeps |= substeps >> 16;
substeps |= substeps >> 8;
substeps |= substeps >> 4;
substeps |= substeps >> 2;
substeps |= substeps >> 1;
substeps++;
float subdt = dt / substeps;
for (int l = 0; l < substeps; l++) {
auto a = covar(metric->krist(p), v);
v += subdt * a;
p += subdt * v;
}
} else {
v += dt * a;
p += dt * v;
}
t.points.push_back(p);
}
t.dir = normalize(v);
return t;
}
float length(vec2 pos, vec2 vec) const {
mat2 g = metric->metric(pos);
return sqrt(dot(vec, g * vec));
}
};
/*
struct ObjectLocation {
Object *object;
Space *space;
vec2 pos;
float radius;
};
struct Boundary {
int space1;
int space2;
Shape *shape1;
Shape *shape2;
Gluing *gluing;
};
*/
struct GlobalTrack {
struct Point {
Space const *space;
vec2 pos;
// vec2 dir;
};
std::vector<Point> points;
vec2 dir;
float length;
};
class BooleanRegion final: public Region {
public:
std::vector<Region const *> positive;
std::vector<Region const *> negative;
bool contains(vec2 p) const noexcept override {
for (auto r: positive)
if (!r->contains(p))
return false;
for (auto r: negative)
if (r->contains(p))
return false;
return true;
}
};
class Universe {
private:
FlatSubspace root;
BooleanRegion root_region;
std::vector<Subspace *> subspaces;
public:
Universe() {
root.desc = &EuclideanSpace::instance;
root.region = &root_region;
}
void add(Subspace *space) {
subspaces.push_back(space);
root_region.negative.push_back(space->region);
}
GlobalTrack trace(Ray ray, float max_distance) const {
static constexpr float eps = 1.0e-1f;
GlobalTrack track;
while (max_distance > eps) {
Subspace const *space = &root;
for (auto *s: subspaces) {
if (s->region->contains(ray.base)) {
space = s;
break;
}
}
Track t = space->trace(ray, max_distance);
assert(t.points.size() > 0);
assert(t.length > 0);
ray.base = t.points.back();
ray.dir = t.dir;
max_distance -= t.length;
track.points.reserve(track.points.size() + t.points.size());
if (space == &root)
space = nullptr;
for (auto pt: t.points)
track.points.push_back({space, pt});
track.length += t.length;
}
track.dir = ray.dir;
return track;
}
};
class SegmentedTransitions {
private:
std::vector<TransformedShape> tfms;
std::vector<Transition> trans;
public:
SegmentedTransitions(std::vector<vec2> const &points, std::vector<Space *> const &targets) {
if (points.size() != targets.size())
throw std::invalid_argument("There must be exactly one target space per boundary segment");
int n = points.size();
if (n < 3)
throw std::invalid_argument("There must be at least three segments");
tfms.resize(n);
for (int k = 0; k < n; k++) {
vec2 a = points[k];
vec2 b = points[(k + 1) % n];
vec2 p = 0.5f * (a + b);
vec2 v = b - p;
vec2 u = {-v.y, v.x};
tfms[k].base = &UnitLine::instance;
tfms[k].pos = p;
tfms[k].tfm = mat2(u, v);
}
trans.resize(n);
for (int k = 0; k < n; k++) {
trans[k].into = targets[k];
trans[k].shape = &tfms[k];
}
}
std::vector<Transition> const &transitions() const noexcept {
return trans;
}
auto begin() const noexcept {
return trans.begin();
}
auto end() const noexcept {
return trans.end();
}
};
class MovedSpace: public Space {
public:
Space *base = nullptr;
mat2 transform = mat2(1.0f);
vec2 origin = vec2(0.0f);
Track trace(Ray ray, float maxd) const override {
ray.base -= origin;
ray.base = inverse(transform) * ray.base;
ray.dir = inverse(transform) * ray.dir;
Track t = base->trace(ray, maxd);
t.dir = transform * t.dir;
for (auto &pt: t.points)
pt = transform * pt + origin;
return t;
}
};
void test() {
// FlatSpace s[5];
// ConvexBoundary cb({
// {-2.0f, 0.5f},
// {-2.0f, 1.0f},
// {0.0f, 1.7f},
// {3.0f, 1.0f},
// {3.0f, 0.5f},
// }, {
// &s[0], &s[1], &s[2], &s[3], &s[4],
// });
// for (vec2 pos: {vec2{0.0f, 0.7f}}) {
// for (vec2 dir: {vec2{1.0f, 0.0f}, vec2{0.707f, 0.707f}, vec2{-1.0f, 0.0f}, vec2{0.0f, 1.0f}}) {
// printf("%6.3f,%6.3f:%6.3f,%6.3f -> %ld\n", pos.x, pos.y, dir.x, dir.y, (FlatSpace *)cb.next({pos, dir}) - s);
// }
// }
/*
printf(" ");
for (int j = 0; j <= 15; j++) {
float y = 0.1f * j;
printf(" %6.3f", y);
}
puts("");
for (int i = -30; i <= 40; i++) {
float x = 0.1f * i;
printf("%6.3f ", x);
for (int j = 0; j <= 15; j++) {
float y = 0.1f * j;
printf(" %6.0f", cb.f({x, y}));
}
puts("");
}
*/
// exit(0);
}
#define USE_TRANSFORM 0
#define RENDER_FULL_RIEMANN 0
void render() {
double t0 = glfwGetTime();
// CoilMetric cm;
// cm.coil_scale = 2.0 + sin(.2 * t0);
FastSpace sd;
// sd.inner_hl = clamp(1.125f - .875f * sinf(.2 * t0), sd.inner_pad, sd.outer_hl);
FlatSubspace fs;
fs.desc = &sd;
BoxSmoother bs;
bs.base = &sd;
bs.halfwidth = 0.5f;
bs.pad = 0.125f;
RiemannSubspace rsn;
rsn.metric = &bs;
RiemannSubspace rsp;
rsp.metric = &bs;
float outer_pad = 0.25f;
ConvexPoly fs_bnd{
{-(sd.outer_hl + outer_pad), -(bs.halfwidth - bs.pad)},
{(sd.outer_hl + outer_pad), -(bs.halfwidth - bs.pad)},
{(sd.outer_hl + outer_pad), (bs.halfwidth - bs.pad)},
{-(sd.outer_hl + outer_pad), (bs.halfwidth - bs.pad)},
};
fs.region = &fs_bnd;
ConvexPoly rsn_bnd1{
{-sd.outer_hl, -bs.halfwidth},
{sd.outer_hl, -bs.halfwidth},
{sd.outer_hl, 0.0f},
{-sd.outer_hl, 0.0f},
};
BooleanRegion rsn_bnd;
rsn_bnd.positive.push_back(&rsn_bnd1);
rsn_bnd.negative.push_back(&fs_bnd);
rsn.region = &rsn_bnd;
ConvexPoly rsp_bnd1{
{-sd.outer_hl, 0.0f},
{sd.outer_hl, 0.0f},
{sd.outer_hl, bs.halfwidth},
{-sd.outer_hl, bs.halfwidth},
};
BooleanRegion rsp_bnd;
rsp_bnd.positive.push_back(&rsp_bnd1);
rsp_bnd.negative.push_back(&fs_bnd);
rsp.region = &rsp_bnd;
Universe u;
u.add(&fs);
u.add(&rsn);
u.add(&rsp);
#if RENDER_FULL_RIEMANN
RiemannSpace rs;
rs.metric = &bs;
#endif
#if USE_TRANSFORM
float phi = .23 * t0;
vec2 u{cos(phi), sin(phi)};
mat2 tf = {u.x, u.y, -u.y, u.x};
vec2 pos = {0.0f, 0.0f};
#if RENDER_FULL_RIEMANN
MovedRiemannMetric<2> rm;
rm.base = rs.metric;
rm.origin = pos;
rm.inv_transform = inverse(tf);
rs.metric = &rm;
#endif
MovedSpace fs1;
fs1.base = &fs;
MovedSpace rsn1;
rsn1.base = &rsn;
MovedSpace rsp1;
rsp1.base = &rsp;
fs1.transform = rsn1.transform = rsp1.transform = tf;
fs1.origin = rsn1.origin = rsp1.origin = pos;
SegmentedTransitions eucl_to_pipe({
tf * vec2{-sd.outer_hl, -bs.halfwidth} + pos,
tf * vec2{sd.outer_hl, -bs.halfwidth} + pos,
tf * vec2{sd.outer_hl, -(bs.halfwidth - bs.pad)} + pos,
tf * vec2{sd.outer_hl, (bs.halfwidth - bs.pad)} + pos,
tf * vec2{sd.outer_hl, bs.halfwidth} + pos,
tf * vec2{-sd.outer_hl, bs.halfwidth} + pos,
tf * vec2{-sd.outer_hl, (bs.halfwidth - bs.pad)} + pos,
tf * vec2{-sd.outer_hl, -(bs.halfwidth - bs.pad)} + pos,
}, {
&rsn1, &rsn1, &fs1, &rsp1, &rsp1, &rsp1, &fs1, &rsn1,
});
eucl.transitions.insert(eucl.transitions.end(), eucl_to_pipe.begin(), eucl_to_pipe.end());
SegmentedTransitions pipe_out({
{-sd.inner_hl, -(bs.halfwidth - bs.pad)},
{-sd.inner_hl, (bs.halfwidth - bs.pad)},
{sd.inner_hl, (bs.halfwidth - bs.pad)},
{sd.inner_hl, -(bs.halfwidth - bs.pad)},
}, {
&eucl, &rsp1, &eucl, &rsn1,
});
fs.transitions.insert(fs.transitions.end(), pipe_out.begin(), pipe_out.end());
ConvexBoundary pipe_top({
{-sd.outer_hl, bs.halfwidth - bs.pad},
{-sd.outer_hl, bs.halfwidth},
{sd.outer_hl, bs.halfwidth},
{sd.outer_hl, bs.halfwidth - bs.pad},
}, {
&eucl, &eucl, &eucl, &fs1,
});
rsp.boundary = &pipe_top;
ConvexBoundary pipe_bottom({
{sd.outer_hl, -(bs.halfwidth - bs.pad)},
{sd.outer_hl, -bs.halfwidth},
{-sd.outer_hl, -bs.halfwidth},
{-sd.outer_hl, -(bs.halfwidth - bs.pad)},
}, {
&eucl, &eucl, &eucl, &fs1,
});
rsn.boundary = &pipe_bottom;
std::unordered_map<Space *, vec4> colors = {
{&eucl, {0.1f, 0.4f, 1.0f, 0.75f}},
{&fs1, {0.4f, 1.0f, 0.1f, 0.75f}},
{&rsp1, {1.0f, 0.4f, 0.1f, 0.75f}},
{&rsn1, {1.0f, 0.1f, 0.4f, 0.75f}},
};
#else
std::unordered_map<Space const *, vec4> colors = {
{nullptr, {0.1f, 0.4f, 1.0f, 0.75f}},
{&fs, {0.4f, 1.0f, 0.1f, 0.75f}},
{&rsp, {1.0f, 0.4f, 0.1f, 0.75f}},
{&rsn, {1.0f, 0.1f, 0.4f, 0.75f}},
};
#endif
#if RENDER_GRID
glBegin(GL_LINES);
for (int i = -50; i <= 50; i++)
for (int j = -50; j <= 50; j++) {
float len = 0.1f;
vec2 dir = vec2(0.707f, 0.707f);
vec2 pt = 0.1f * vec2{i, j};
auto r = fs.desc->toGlobal(fs.desc->fromGlobal(Ray{pt, dir}));
// auto r = fs.desc->fromGlobal(Ray{pt, dir});
glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
glVertex2fv(value_ptr(pt));
glColor4f(0.0, 1.0f, 0.0f, 1.0f);
glVertex2fv(value_ptr(pt + len * dir));
// pt += 0.05f * fs.desc->fromGlobal(Ray{pt, vec2(0.707f, 0.707f)}).dir;
pt = r.base;
glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
glVertex2fv(value_ptr(pt));
glColor4f(1.0, 0.0f, 0.0f, 1.0f);
glVertex2fv(value_ptr(pt + len * r.dir));
}
glEnd();
#endif
for (int k = -120; k <= 120; k++) {
Ray r;
r.base = {-5.2f, 0.0f};
r.dir = normalize(vec2(1.0, k / 120.0));
auto track = u.trace(r, 20.0f);
glBegin(GL_LINE_STRIP);
for (auto pt: track.points) {
vec4 color{0.0f, 1.0f, 1.0f, 1.0f};
auto icolor = colors.find(pt.space);
if (icolor != colors.end())
color = icolor->second;
glColor4fv(value_ptr(color));
glVertex2f(pt.pos.x, pt.pos.y);
}
glEnd();
}
// printf("%.3f: |a| ≈ %.3f(1 ± %.3f) ∈ [%.3f, %.3f]\n",
// sd.inner_hl, rsp.a_len.mean(), rsp.a_len.reldev(), rsp.a_len.min(), rsp.a_len.max());
#if RENDER_FULL_RIEMANN
glColor4f(0.4, 0.4, 0.4, 0.75);
for (int k = -120; k <= 120; k++) {
Ray r;
r.base = {-5.2f, 0.0f};
r.dir = normalize(vec2(1.0, k / 120.0));
Track track = rs.trace(r, 20.0f);
glBegin(GL_LINE_STRIP);
for (auto pt: track.points)
glVertex2f(pt.x, pt.y);
glEnd();
}
#endif
}
float background_lightness = 0.1;
int width, height;
float winsize;
GLuint fbs[2] = {0, 0};
GLuint bb = 0;
GLuint mb = 0;
void paint(GLFWwindow* window) {
glBindFramebuffer(GL_FRAMEBUFFER, fbs[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2.3f);
glLineWidth(winsize * 0.0085);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_MULTISAMPLE);
render();
glPushMatrix();
glLoadIdentity();
glDisable(GL_MULTISAMPLE);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbs[1]);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClearColor(background_lightness, background_lightness, background_lightness, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mb);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);