Repository navigation
Expand file tree
/
Copy pathbtd.cpp
More file actions
289 lines (226 loc) · 7.02 KB
/
Copy pathbtd.cpp
File metadata and controls
289 lines (226 loc) · 7.02 KB
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
// translate computed coordinates to Poincare disk coordinates
// includes the algorithm for syncing the branched shapes (construct_btd)
namespace nconf {
ld cspin;
pair<ld, ld> unband(cpoint& c, sideinfo& si, ld shift) {
ld y = c[1];
ld x = c[0] + shift;
y *= 2; y -= 1; // -1 .. 1
x *= 2; x /= si.cscale[0];
y *= M_PI / 2;
x *= M_PI / 2;
y = -2 * atanh(tan(y/2));
return make_pair(x, y);
}
hyperpoint equirectangular(ld x, ld y) {
return { sinh(x) * cosh(y), sinh(y), cosh(y) * cosh(x)};
}
void set_column(transmatrix& M, int id, const hyperpoint& h) {
M[id] = h[0];
M[id+3] = h[1];
M[id+6] = h[2];
}
ld det(const transmatrix& T) {
ld det = 0;
for(int i=0; i<3; i++)
det += T[i] * T[3+(i+1)%3] * T[6+(i+2)%3];
for(int i=0; i<3; i++)
det -= T[i] * T[3+(i+2)%3] * T[6+(i+1)%3];
return det;
}
transmatrix inverse(const transmatrix& T) {
ld d = det(T);
transmatrix T2;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
T2[j*3+i] = (T[(i+1)%3*3+(j+1)%3] * T[(i+2)%3*3+(j+2)%3] - T[(i+1)%3*3+(j+2)%3] * T[(i+2)%3*3+(j+1)%3]) / d;
return T2;
}
int debugsi;
transmatrix get_matrix_at(sideinfo& si, ld x) {
x -= si.zero_shift;
if(si.type == stype::fixed_ring) {
ld d = si.period;
x -= si.xcenter / si.cscale[0];
while(x > d/2) x -= d;
while(x < -d/2) x += d;
return mul(si.matrixlist[0], xpush(x));
}
int x0 = int(x);
if(x0 >= 0) {
while(isize(si.matrixlist) <= x0) {
transmatrix M = mul(si.matrixlist.back(), xpush(1));
fixmatrix(M);
M = reperiod(M, rootof(si).period_matrices);
si.matrixlist.push_back(M);
}
return mul(si.matrixlist[x0], xpush(x - x0));
}
else {
if(si.rmatrixlist.empty()) si.rmatrixlist.push_back(si.matrixlist[0]);
x0 = -x0;
while(isize(si.rmatrixlist) <= x0) {
transmatrix M = mul(si.rmatrixlist.back(), xpush(-1));
fixmatrix(M);
M = reperiod(M, rootof(si).period_matrices);
si.rmatrixlist.push_back(M);
}
return mul(si.rmatrixlist[x0], xpush(x + x0));
}
}
void construct_btd_for(sideinfo& si) {
si.matrixlist.clear();
si.rmatrixlist.clear();
if(si.parentid == si.id) {
si.need_btd = cspin > 0;
si.matrixlist.push_back(spin(cspin));
si.zero_shift = -si.animshift;
}
else {
// auto& root = rootof(si);
auto& par = sides[si.parentid];
par.need_btd = true;
ipoint ex0 = si.join;
ipoint ex1 = ex0 + ipoint(1, 0);
ipoint ex2 = ex0 + ipoint(0, 1);
auto& ppts = *par.submap;
auto [old_x0, old_y0] = unband(ppts[ex0].x, par, 0);
auto [old_x1, old_y1] = unband(ppts[ex1].x, par, 0);
auto [old_x2, old_y2] = unband(ppts[ex2].x, par, 0);
auto& epts = *si.submap;
auto [new_x0, new_y0] = unband(epts[ex0].x, si, 0);
auto [new_x1, new_y1] = unband(epts[ex1].x, si, 0);
auto [new_x2, new_y2] = unband(epts[ex2].x, si, 0);
transmatrix T = get_matrix_at(par, old_x0);
transmatrix mold, mnew;
set_column(mold, 0, equirectangular(0, old_y0));
set_column(mold, 1, equirectangular(old_x1 - old_x0, old_y1));
set_column(mold, 2, equirectangular(old_x2 - old_x0, old_y2));
set_column(mnew, 0, equirectangular(0, new_y0));
set_column(mnew, 1, equirectangular(new_x1 - new_x0, new_y1));
set_column(mnew, 2, equirectangular(new_x2 - new_x0, new_y2));
T = mul(T, mul(mold, inverse(mnew)));
fixmatrix(T);
si.matrixlist.push_back(T);
si.zero_shift = new_x0;
ld dx0 = old_x0 - par.zero_shift;
si.parentrel_x = int(dx0);
si.parentrel_matrix = mul(xpush(dx0-si.parentrel_x), mul(mold, inverse(mnew)));
}
}
void construct_btd() {
static int p;
p++;
for(auto& si: sides)
construct_btd_for(si);
}
ld btd_at = 2;
struct shift_data {
ld px, py, ps;
};
bool use_shift;
shift_data cshift;
cpoint band_to_disk(int px, int py, sideinfo& si, int& tsiid, ld& xval, ld& yval, bool to_img = true) {
cpoint c = pts[py][px].x;
hyperpoint p;
if(si.need_btd) {
auto csi = &si;
auto [x,y] = unband(c, *csi, 0);
parent_changed:
if(use_childsides) for(int subid: csi->childsides) {
auto& nsi = sides[subid];
auto& epts = *nsi.submap;
if(epts[py][px].type != ptype::inside) continue;
auto [nx, ny] = unband(epts[py][px].x, nsi, 0);
if(nx > nsi.zero_shift) {
x = nx; y = ny;
csi = &nsi;
goto parent_changed;
}
}
p = {0, sinh(y), cosh(y)};
p = mul(get_matrix_at(*csi, x), p);
p = reperiod(p, si.period_matrices);
tsiid = csi->id;
yval = y;
xval = x;
}
else {
auto [x,y] = unband(c, si, -si.xcenter);
if(std::isinf(x) || std::isnan(x)) {
printf("c = %Lf,%Lf\n", c[0], c[1]);
printf("xcenter = %Lf\n", si.xcenter);
printf("scale = %Lf\n", si.cscale[0]);
exit(1);
}
if(si.period > 0) {
ld d = si.period;
while(x > d/2) x -= d;
while(x < -d/2) x += d;
}
p = equirectangular(x, y);
if(use_shift) {
p = mul(xpush(cshift.px), p);
p = mul(ypush(cshift.py), p);
p = mul(spin(cshift.ps), p);
}
p = mul(spin(cspin), p);
p = reperiod(p, si.period_matrices);
yval = y;
}
cpoint pt = hyper_to_disk(p);
#if CAP_BMP
if(!si.img.s || !to_img) return pt;
return (cpoint{1, 1} + pt) * (si.img.s->h / 2);
#else
return pt;
#endif
}
void measure_if_needed();
void prepare_all_matrices() {
measure_if_needed();
construct_btd();
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
if(inner(p.type)) {
ld xval = 0, yval = 0;
int tsiid = p.side;
/*auto dc =*/ band_to_disk(x, y, sides[tsiid], tsiid, xval, yval);
}
}
}
void create_viewlist(int current_side, string fname) {
prepare_all_matrices();
FILE *f = fopen(fname.c_str(), "wt");
for(auto& si: sides) {
if(si.rootid == current_side) {
if(si.parentid != si.id) {
fprintf(f, "%d %d %d ", si.id, si.parentid, si.parentrel_x);
for(int i=0; i<9; i++) fprintf(f, "%lf ", double(si.parentrel_matrix[i]));
}
else fprintf(f, "%d ", si.id);
fprintf(f, "%d %d\n", isize(si.matrixlist), isize(si.rmatrixlist));
}
}
fprintf(f, "%d\n", -1);
fclose(f);
}
#if CAP_GD
void read_viewlist(int /* ignored current_side */, string format) {
prepare_all_matrices();
for(auto& si: sides) {
for(int i=0; i<isize(si.matrixlist); i++) {
char buf[1000];
sprintf(buf, format.c_str(), si.id, i);
FILE *f = fopen(buf, "rb");
if(f) {
printf("found %s\n", buf);
fclose(f);
si.img_line.push_back(readPng(buf));
}
}
}
}
#endif
}