/
automapper.cpp
399 lines (329 loc) · 10.4 KB
/
automapper.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
// implementations of -mapat and -mapall
namespace nconf {
ipoint get_last_point(pointmap& ptmap, ipoint start) {
vector<ipoint> q;
ptmap[start].type = ptype::marked; q.push_back(start);
for(int i=0; i<isize(q); i++) {
auto xy = q[i];
for(auto k: dv) if(ptmap[k+xy].type == ptype::inside)
ptmap[k+xy].type = ptype::marked, q.push_back(k + xy);
}
for(auto pt: q) ptmap[pt].type = ptype::inside;
return q.back();
}
ld frac(ld x) { return x - floor(x); }
ld join_epsilon = 1e-5;
int join_distance = 5;
ld join_y = .1;
void auto_joins() {
measure(cside());
construct_btd_for(cside());
vector<ipoint> last_points;
again:
vector<pair<ipoint, int>> q;
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto xy = ipoint(x, y);
auto& p = pts[xy];
if(p.type != ptype::inside || p.side != current_side) continue;
auto pxy = p.x;
auto csi = &cside();
auto sid = current_side;
parent_changed:
for(int subid: csi->childsides) {
auto& nsi = sides[subid];
auto& epts = *nsi.submap;
if(epts[xy].type != ptype::inside) continue;
if(epts[xy].x[0] / nsi.cscale[0] * M_PI > nsi.zero_shift) {
pxy = epts[xy].x;
csi = &nsi;
sid = subid;
goto parent_changed;
}
}
if(pxy[1] > join_epsilon && pxy[1] < 1-join_epsilon) p.type = ptype::marked, q.emplace_back(xy, sid);
}
int nextd = isize(q);
int d = 0;
for(int i=0; i<isize(q); i++) {
if(i == nextd) d++, nextd = isize(q);
auto xy = q[i].first;
for(auto k: dv) if(pts[k+xy].type == ptype::inside)
pts[k+xy].type = ptype::marked, q.emplace_back(k + xy, q[i].second);
}
printf("d=%d until %d\n", d, nextd);
ipoint ending = q.back().first;
for(ipoint p: last_points) if(p == ending) {
printf("error: ending repeats\n");
for(auto pt: q) pts[pt.first].type = ptype::inside;
return;
}
last_points.push_back(ending);
int parent_side = q.back().second;
for(auto pt: q) pts[pt.first].type = ptype::inside;
if(d >= join_distance) {
auto& side = new_side(stype::standard);
auto& parside = sides[parent_side];
side.parentid = parent_side;
side.rootid = parside.rootid;
parside.childsides.push_back(side.id);
side.submap = new pointmap;
// side.join = ?
auto &ppts = *parside.submap;
auto &epts = *side.submap;
epts.resize2(SX, SY);
bool over = ppts[ending].x[1] > .5;
ld error = 1e9;
ld low = 1e9;
ipoint lowpoint;
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto xy = ipoint(x, y);
auto& p = epts[xy];
auto& pold = ppts[xy];
p.side = side.id;
p.type = (inner(pold.type) && intdif(pold.x[0] - ppts[ending].x[0]) < 3 * cside().cscale[0]) ? ptype::inside : ptype::outside;
if(p.type == ptype::inside) {
ld err = std::hypot(pold.x[0] - ppts[ending].x[0], pold.x[1] - (over ? 1-join_y : join_y));
if(err < error) error = err, side.join = xy;
ld nlow = frac(pold.x[0] - ppts[ending].x[0] + 2.5);
if(nlow < low) low = nlow, lowpoint = xy;
}
}
printf("side = %d->%d->%d join = %d,%d ending = %d,%d\n", parside.rootid, parent_side, side.id, side.join.x, side.join.y, ending.x, ending.y);
printf("low = %lf (ending = %lf) at %d,%d\n", double(low), double(ppts[ending].x[0]), lowpoint.x, lowpoint.y);
auto [axy2, ad2] = boundary_point_near(epts, lowpoint);
auto [bxy2, bd2] = boundary_point_near(epts, ending);
printf("axy = %d,%d bxy = %d,%d\n", axy2.x, axy2.y, bxy2.x, bxy2.y);
split_boundary(epts, axy2, bxy2, bd2^2);
computemap(epts);
measure(side);
construct_btd_for(side);
goto again;
}
}
#if CAP_BMP
void auto_map_at(ipoint at) {
current_side = new_side(stype::standard).id;
cside().inner_point = at;
auto atpixel = get_heart(at);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto xy = ipoint(x, y);
auto& p = pts[xy];
p.baktype = p.type;
p.type = ptype::outside;
}
vector<ipoint> inpoints;
pts[at].type = ptype::inside;
inpoints.push_back(at);
auto isin = [atpixel] (ipoint xy) {
if(xy.x == 0 || xy.y == 0 || xy.x == SX-1 || xy.y == SY-1)
return false;
return get_heart(xy) == atpixel;
};
int chi1 = 0, chi2 = 0;
for(int i=0; i<isize(inpoints); i++) {
auto xy = inpoints[i];
for(auto k: dv) {
auto xy2 = xy + k;
if(isin(xy2)) {
chi1++;
if(pts[xy2].type == ptype::outside)
pts[xy2].type = ptype::inside, inpoints.push_back(xy2);
}
}
if(isin(xy + ipoint(1,0)) && isin(xy + ipoint(0,1)) && isin(xy + ipoint(1,1)))
chi2++;
}
for(auto xy: inpoints) pts[xy].side = current_side;
int chi0 = isize(inpoints);
chi1 /= 2;
int euler_characteristics = chi0 - chi1 + chi2;
int holes = 1 - euler_characteristics;
printf("chi (%d,%d,%d), number of holes = %d\n", chi0, chi1, chi2, holes);
if(holes >= 2) {
cside().type = stype::fake;
merge_sides();
return;
}
if(holes == 0) {
ipoint axy = get_last_point(pts, at);
ipoint bxy = get_last_point(pts, axy);
printf("axy = %d,%d bxy = %d,%d\n", axy.x, axy.y, bxy.x, bxy.y);
auto [axy1, ad] = boundary_point_near(pts, axy);
auto [bxy1, bd] = boundary_point_near(pts, bxy);
printf("axy1 = %d,%d bxy1 = %d,%d\n", axy1.x, axy1.y, bxy1.x, bxy1.y);
split_boundary(pts, axy, bxy, bd^2);
}
if(holes == 1) {
cside().type = stype::ring;
inpoints.clear();
inpoints.emplace_back(0,0);
pts[0][0].type = ptype::top;
for(int i=0; i<isize(inpoints); i++) {
auto xy = inpoints[i];
for(auto k: dv) {
auto xy2 = xy + k;
if(xy2.x < 0 || xy2.y < 0 || xy2.x >= SX || xy2.y >= SY) continue;
if(isin(xy2)) continue;
if(pts[xy2].type == ptype::outside)
pts[xy2].type = ptype::top, inpoints.push_back(xy2);
}
}
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++)
if(pts[y][x].type == ptype::outside)
pts[y][x].type = ptype::bottom;
ipoint splitat(0, 0);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++)
if(pts[y][x].type == ptype::bottom && pts[y][x-1].type == ptype::inside) {
splitat = ipoint(x, y);
break;
}
for(int y=0; y<SY; y++)
for(int x=0; x<=splitat.x; x++)
if(pts[y][x].type == ptype::inside) {
if(y < splitat.y)
pts[y][x].type = ptype::inside_left_up;
else
pts[y][x].type = ptype::inside_left_down;
}
}
computemap(pts);
auto_joins();
merge_sides();
}
#endif
#if CAP_DRAW
void drawsides() {
paused = true;
do {
initGraph(SX, SY, "conformist", false);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
screen[y][x] = // ((x+y)&1) ? pts[y][x].side * 0x147931 :
(pts[y][x].type != ptype::outside) ? 0xFFFFFF : 0;
}
screen.draw();
SDL_Event event;
SDL_Delay(1);
int ev;
while((ev = SDL_PollEvent(&event))) switch (event.type) {
case SDL_QUIT:
exit(1);
return;
case SDL_MOUSEMOTION: {
mousex = event.motion.x;
mousey = event.motion.y;
break;
}
case SDL_KEYDOWN: {
int key = event.key.keysym.sym;
// int uni = event.key.keysym.unicode;
if(key == 'p') paused = !paused;
break;
}
}
}
while(paused);
}
#endif
#if CAP_BMP
void auto_map_all() {
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++)
pts[y][x].type = ptype::outside;
for(int y=2; y<SY-2; y++)
for(int x=2; x<SX-2; x++)
if(pts[y][x].type == ptype::outside && get_heart({x,y}) == get_heart({x+1, y}) && pts[y][x+1].type == ptype::outside) {
printf("Mapping (%d,%d)\n", x, y);
auto_map_at(ipoint(x, y));
}
}
#endif
void save_all_maps(const string fname) {
FILE *f = fopen(fname.c_str(), "wb");
if(!f) pdie("save_all_maps");
save(f, SX);
save(f, SY);
int nsides = isize(sides);
save(f, nsides);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
save(f, p.type);
save(f, p.side);
if(inner(p.type))
save(f, p.x);
}
for(auto& p: sides) {
save(f, p.inner_point);
save(f, p.type);
save(f, p.join);
save(f, p.parentid);
if(p.parentid != p.id) {
auto& epts = *p.submap;
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = epts[y][x];
save(f, p.type);
save(f, p.side);
if(inner(p.type))
save(f, p.x);
}
}
}
fclose(f);
}
void load_all_maps(const string fname) {
FILE *f = fopen(fname.c_str(), "rb");
if(!f) pdie("save_all_maps");
load(f, SX);
load(f, SY);
resize_pt();
int nsides;
load(f, nsides);
sides.clear();
for(int i=0; i<nsides; i++) new_side(stype::standard);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = pts[y][x];
load(f, p.type);
load(f, p.side);
if(inner(p.type))
load(f, p.x);
}
for(auto& p: sides) {
load(f, p.inner_point);
load(f, p.type);
load(f, p.join);
load(f, p.parentid);
string sidetypenames[4] = { "standard", "ring", "fixed ring", "illegal" };
if(p.parentid == p.id) {
p.rootid = p.id;
printf("Side %d: %s at (%d,%d)\n", p.id, sidetypenames[int(p.type)].c_str(), p.inner_point.x, p.inner_point.y);
}
else {
p.rootid = sides[p.parentid].rootid;
sides[p.parentid].childsides.push_back(p.id);
p.submap = new pointmap;
auto &epts = *p.submap;
epts.resize2(SX, SY);
for(int y=0; y<SY; y++)
for(int x=0; x<SX; x++) {
auto& p = epts[y][x];
load(f, p.type);
load(f, p.side);
if(inner(p.type))
load(f, p.x);
}
int par = p.id;
while(sides[par].parentid != par) par = sides[par].parentid, printf(" ");
printf("%d joins at %d,%d\n", p.id, p.join.x, p.join.y);
}
}
fclose(f);
}
}