-
Notifications
You must be signed in to change notification settings - Fork 2
/
weighted_distort.cpp
348 lines (310 loc) · 10.2 KB
/
weighted_distort.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
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const double distortion_threshold = 0.01, outer_threshold = 2,
accuracy_threshold = 1.01;
const double pi = acos(-1);
// A pair of (x,y) coordinates is a point
typedef std::pair<double, double> point;
// A simply connected non-intersecting polygon is a piece
typedef std::vector<point> piece;
// A group of non-intersecting pieces is a region (state/district...)
typedef std::vector<piece> region;
// And a group of regions forms a map
typedef std::vector<region> map;
// Find area and centroid of piece from line integral using Green's theorem
std::pair<double, point> area_centroid(const piece& p) {
int N = p.size();
double intdA = 0, intxdA = 0, intydA = 0;
for (int i = 1; i < N; ++i) {
auto [xp, yp] = p[i - 1];
auto [x, y] = p[i];
intdA += y * xp - x * yp;
intxdA += (y - yp) * (x * x + x * xp + xp * xp) / 3;
intydA += (xp - x) * (y * y + y * yp + yp * yp) / 3;
}
return {std::abs(intdA) / 2, {intxdA / intdA, intydA / intdA}};
}
// Now propagate it to regions and maps
template <class T>
std::pair<double, point> area_centroid(const T& r) {
double intdA = 0, intxdA = 0, intydA = 0;
for (auto& p : r) {
if(p.empty()) continue;
auto [dA, dxy] = area_centroid(p);
intdA += dA;
intxdA += dA * dxy.first;
intydA += dA * dxy.second;
}
return {intdA, {intxdA / intdA, intydA / intdA}};
}
// Find the convex hull of a region using Graham's scan
piece convexhull(const region& r) {
const double eps = 1e-9;
point P0 = r[0][0];
auto& [x0, y0] = P0;
for (const piece& p : r)
for (auto [x, y] : p) {
if (y < y0 or (y == y0 and x < x0)) x0 = x, y0 = y;
}
piece pvec, hull;
for (const piece& p : r)
for (auto [x, y] : p) {
if (std::abs(x - x0) > eps or std::abs(y - y0) > eps)
pvec.push_back({x, y});
}
auto comp = [eps](const point& p0, const point& p1, const point& p2) {
return (p1.first - p0.first) * (p2.second - p0.second) -
(p2.first - p0.first) * (p1.second - p0.second) <
-eps;
};
sort(pvec.begin(), pvec.end(),
[&P0, &comp](const point& p1, const point& p2) {
return comp(P0, p1, p2);
});
hull.push_back(P0);
pvec.push_back(P0);
int H = 1;
for (auto& p : pvec) {
while (H > 1 and !comp(hull[H - 2], hull[H - 1], p)) hull.pop_back(), --H;
hull.push_back(p);
++H;
}
return hull;
}
// Poor implementation of a subset of svg path reading
// Only considers piecewise linear paths
map readsvgpaths(const std::string& filename) {
std::ifstream in(filename);
map ret;
for (std::string s; std::getline(in, s);) {
ret.push_back({});
auto& subreg = ret.back();
std::istringstream iss(s);
double x = 0, y = 0, x0, y0;
for (char motion; iss;) {
if (!(iss >> std::ws)) break;
int next = iss.peek();
if (next == EOF)
break;
else if (isalpha(next))
iss >> motion;
std::cerr << motion;
if (motion == 'm' or motion == 'l') {
double dx, dy;
iss >> dx >> dy;
x += dx;
y += dy;
if (motion == 'm') {
x0 = x;
y0 = y;
motion = 'l';
subreg.push_back({});
}
} else if (motion == 'M' or motion == 'L') {
iss >> x >> y;
if (motion == 'M') {
x0 = x;
y0 = y;
motion = 'L';
subreg.push_back({});
}
} else if (motion == 'h') {
double dx;
iss >> dx;
x += dx;
} else if (motion == 'H') {
iss >> x;
} else if (motion == 'v') {
double dy;
iss >> dy;
y += dy;
} else if (motion == 'V') {
iss >> y;
} else if (motion == 'z') {
x = x0;
y = y0;
} else {
std::cerr << "Error " << motion << "\n";
exit(1);
}
subreg.back().push_back({x, y});
}
std::cerr << "\n";
}
return ret;
}
// Read and normalise the weights for each part of the map
std::vector<double> readweights(const std::string& filename) {
std::ifstream in(filename);
std::vector<double> ret;
double p, tot = 0;
while (in >> p) {
ret.push_back(p);
tot += p;
}
for (double& p : ret) p /= tot;
return ret;
}
// Aspect ratio of the map
double aspect(const map& M) {
double xmin = std::numeric_limits<double>::max(),
xmax = std::numeric_limits<double>::min(), ymin = xmin, ymax = xmax;
for (auto& reg : M)
for (auto& p : reg)
for (auto& [x, y] : p) {
xmax = std::max(x, xmax);
xmin = std::min(x, xmin);
ymax = std::max(y, ymax);
ymin = std::min(y, ymin);
}
return (ymax - ymin) / (xmax - xmin);
}
// Move the origin to the centroid of the map
void recentre(map& M) {
auto [A, centre] = area_centroid(M);
auto [cx, cy] = centre;
for (auto& reg : M)
for (auto& p : reg)
for (auto& [x, y] : p) x -= cx, y -= cy;
}
// Major work: Stretch or compress a region by a factor
// without messing up rest of the map
void distort(map& M, int id, double distortion_factor, double aspect_target) {
// Start with the centroid of the convex hull
auto H = convexhull(M[id]);
auto [A, centre] = area_centroid(H);
auto [cx, cy] = centre;
// Sort the points on the convex hull using angle around centroid
// Unnecessary log N factor here due to map, TODO: Optimise if too slow
std::map<double, point> anglehull;
for (auto [x, y] : H) {
double theta = atan2(y - cy, x - cx);
anglehull[theta] = {x - cx, y - cy};
}
// Close the "gap" in the hull near -pi/pi
{
auto [theta1, c1] = *anglehull.begin();
auto [theta2, c2] = *anglehull.rbegin();
anglehull[theta1 + 2 * pi] = c1;
anglehull[theta2 - 2 * pi] = c2;
}
// Express every line as a*x+b=c, such that c=1 for the convex hull
std::vector<std::tuple<double, double, double>> theta_ab;
for (auto mit = anglehull.begin();;) {
auto [x1, y1] = mit->second;
double theta1 = mit->first;
if(++mit == anglehull.end()) break;
auto [x2, y2] = mit->second;
double theta2 = mit->first;
if(theta2 - theta1 < 1e-6) continue;
double det = x1 * y2 - x2 * y1, a = (y2-y1) / det, b = (x1-x2) / det;
theta_ab.push_back({theta2, a, b});
//std::cerr << theta2 << " " << a << " " << b << "\n";
}
// Stretch each point in the map depending on the angular region
// Mapping c in [0,1] to [0, distortion_factor]
// and [1, outer_threshold] to [distortion_factor, outer_threshold]
for (auto& reg : M)
for (auto& p : reg)
for (auto& [x, y] : p) {
double dx = x - cx, dy = y - cy;
double theta = atan2(dy, dx);
// Find angular sector the point falls in
auto [thetahull, a, b] = *upper_bound(theta_ab.begin(), theta_ab.end(),
std::make_tuple(theta, 0., 0.));
// c value range tells us how to distort
double c = a * dx + b * dy;
if(c > outer_threshold) continue;
double cnew = 1;
if(c < 1) {
cnew = c * distortion_factor;
} else {
double m1 = (outer_threshold - distortion_factor) /
(outer_threshold - 1),
m2 = outer_threshold * (distortion_factor - 1) /
(outer_threshold - 1);
cnew = m1 * c + m2;
}
x = cx + dx * cnew / c;
y = cy + dy * cnew / c;
//std::cerr << dx << " "<< dy << " " << c << " " << cnew << "\n";
}
// Points have been stretched, now recentre the map
recentre(M);
// And fix its aspect ratio without affecting the area
double aspect_modifier = sqrt(aspect(M) / aspect_target);
for (auto& reg : M)
for (auto& p : reg)
for (auto& [x, y] : p) x *= aspect_modifier, y /= aspect_modifier;
}
int main() {
std::string mapfilename, popfilename;
int rounds;
std::cin >> mapfilename >> popfilename >> rounds;
map M = readsvgpaths(mapfilename);
auto weights = readweights(popfilename);
int N = M.size();
for (int i = 0; i < N; ++i)
if (weights[i] == 0) M[i].clear();
recentre(M);
double aspect_target = aspect(M);
std::vector<int> cnt(N);
for (int i = 0; ; ++i) {
// Normalise total area of the map to 1
std::vector<double> areas;
std::transform(M.begin(), M.end(), std::back_inserter(areas),
[](const region& d) { return area_centroid(d).first; });
double totarea = std::accumulate(areas.begin(), areas.end(), 0.);
double rescale_factor = sqrt(totarea);
for (auto& reg : M)
for (auto& subreg : reg)
for (auto& xy : subreg) xy = {xy.first / rescale_factor, xy.second / rescale_factor};
for (double& a : areas) a /= totarea;
// Find the most deviating region, but reduce its weight
// if we have processed it too many times before
double worst = 0, absworst = 0;
int worst_id = -1;
for (int i = 0; i < N; ++i) {
if(weights[i] == 0) continue;
double bad = areas[i] / weights[i];
bad = std::max(bad, 1 / bad);
absworst = std::max(absworst, bad);
bad = (bad - 1) / (cnt[i] + 1);
if (bad > worst) {
worst = bad;
worst_id = i;
}
}
// Find the amount of distortion needed to "correct" the area
double distortion_factor = sqrt(weights[worst_id] / areas[worst_id]);
// But pin it to threshold level
if (distortion_factor > 1 + distortion_threshold)
distortion_factor = 1 + distortion_threshold;
else if (distortion_factor < 1 - distortion_threshold)
distortion_factor = 1 - distortion_threshold;
std::cerr << worst_id << " " << cnt[worst_id] << " " << areas[worst_id]
<< " " << weights[worst_id] << " " << absworst << " "
<< distortion_factor << "\n";
// Stop if all regions are fixed to desired accuracy
if (absworst < accuracy_threshold) break;
if (i == rounds) break;
// Distort the map
distort(M, worst_id, distortion_factor, aspect_target);
++cnt[worst_id];
}
// Output the map with region ID in gnuplot-readable format
for (int i = 0; i < N; ++i) {
for (auto& r : M[i]) {
for (auto [x, y] : r) std::cout << x << " " << y << " " << i + 1 << "\n";
std::cout << "\n";
}
}
return 0;
}