forked from saxifrage/caac-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenmap.py
executable file
·544 lines (436 loc) · 17.6 KB
/
genmap.py
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
#!/usr/bin/env python
import io
import random
import sys
import traceback
import itertools as it
import uuid
from math import ceil, sqrt, factorial
import pickle
import pathways_solver
class NoPossibleShapes(Exception): pass
class TargetAreaTooSmall(Exception): pass
class UnevenAlleys(Exception): pass
class DoneAssigningIds(Exception): pass
class TilePlacementError(Exception):
def __init__(self, *a):
self.base_message = "Can't place '{}' at ({},{}).".format(*a[:3])
Exception.__init__(self, *a)
def __str__(self):
return self.base_message + " " + self.message.format(*self.args)
def __bytes__(self):
return str(self).encode('utf8')
class BadTile(TilePlacementError):
message = "Bad tile."
class OutOfBounds(TilePlacementError):
message = "Out of bounds. Canvas size is ({3},{4})."
class AlreadyPlaced(TilePlacementError):
message = "Already placed: '{3}'."
class MagnitudeMap(list):
A = '-' # Alley
B = '#' # Building
C = ' ' # Canvas
def __init__(self, canvas_size, sum_of_magnitudes=0, charset='-# ', alley_width=2,
building_min=4, aspect_min=0.2):
self.W, self.H = canvas_size
if alley_width % 2 == 1: raise UnevenAlleys()
self.alley_width = alley_width
self.area = self.W * self.H
self.remaining_area = (self.W - alley_width) * (self.H - alley_width)
self.sum_of_magnitudes = sum_of_magnitudes
self.remaining_magnitudes = sum_of_magnitudes
self.charset = charset
self.A, self.B, self.C = charset
self.half_alley = alley_width // 2
self.shape_min = building_min + alley_width
self.aspect_min = aspect_min
self.area_threshold = 1 # lowered automatically as space shrinks
self.shapes = {}
self.assignments = {}
# Build the base map. It's surrounded by alleys.
innerW = self.W - (self.alley_width * 2)
innerH = self.H - (self.alley_width * 2)
half_alley = lambda P=self.A: [P] * self.half_alley
padded = lambda col, P: half_alley() + half_alley(P) + col + half_alley(P) + half_alley()
col = lambda char, P: self.append(padded([char for y in range(innerH)], P))
for x in range(self.half_alley): col(self.A, self.A)
for x in range(self.half_alley): col(self.C, self.C)
for x in range(innerW): col(self.C, self.C)
for x in range(self.half_alley): col(self.C, self.C)
for x in range(self.half_alley): col(self.A, self.A)
def __str__(self):
out = []
for y in range(self.H):
for x in range(self.W):
out.append(self[x][y])
out.append('\n')
return ''.join(out[:-1])
def __bytes__(self):
return str(self).encode('UTF-8')
def to_svg(self, id='', offset_x=0, offset_y=0):
fp = io.StringIO()
print(' <svg id="{}" x="{}" y="{}" '
'xmlns="http://www.w3.org/2000/svg">'.format(id, offset_x, offset_y), file=fp)
for uid, (x, y, (w, h)) in self.shapes.items():
uid = self.assignments.get(uid)
if uid is None:
continue
print( ' <rect id="{}" x="{}px" y="{}px" width="{}px" height="{}px" />'
.format( uid
, x+self.half_alley
, y+self.half_alley
, w-self.alley_width
, h-self.alley_width
)
, file=fp
)
print(' </svg>', file=fp)
return fp.getvalue()
def load(self, u):
self.remaining_area = self.W * self.H
for y, row in enumerate(u.splitlines()):
for x, char in enumerate(row):
assert char in self.charset
self[x][y] = char
if char != self.C:
self.remaining_area -= 1
def find_starting_corner(self):
x = y = 0
while 1:
if self[x][y] == self.C:
break
x += 1
if x == self.W:
x = 0
y += 1
return x, y
def determine_target_area(self, magnitude):
target_area = int(self.remaining_area * (magnitude / self.remaining_magnitudes))
min_area = self.shape_min ** 2
if target_area < min_area:
raise TargetAreaTooSmall()
return target_area
def add(self, magnitude, uid=None, shape_choice=None):
# Find first empty cell.
x, y = self.find_starting_corner()
# Determine target area.
target_area = self.determine_target_area(magnitude)
# Try to find a nice fit.
shapes = self.get_snapped_shapes(x, y, target_area)
if not shapes:
shapes = self.get_unsnapped_shapes(x, y, target_area)
if not shapes:
raise NoPossibleShapes()
# Pick a shape and draw it!
if shape_choice is not None:
shape = shapes[shape_choice]
else:
shape = random.choice(shapes)
self.draw_shape_at(shape, x, y)
# Also save it for the SVG renderer to use.
uid = uid if uid else uuid.uuid4().hex
self.shapes[uid] = (x, y, shape)
# Decrement remaining_magnitudes.
self.remaining_magnitudes -= magnitude
# Recalculate area_threshold.
self.area_threshold = self.remaining_area / self.area
def place_tile(self, tile, x, y):
if tile not in (self.A, self.B):
raise BadTile(tile, x, y)
try:
if x < 0 or y < 0: # Beware of negative indexing! We don't want it.
raise IndexError
if self[x][y] != self.C:
raise AlreadyPlaced(tile, x, y, self[x][y])
except IndexError:
raise OutOfBounds(tile, x, y, self.W, self.H)
self[x][y] = tile
self.remaining_area -= 1
def draw_shape_at(self, shape, x, y):
w, h = [dimension - self.alley_width for dimension in shape]
x = x + self.half_alley
y = y + self.half_alley
for x_ in range(x, x+w):
for y_ in range(y, y+h):
self.place_tile(self.B, x_, y_)
self.draw_half_alleys_around_shape((w,h), x, y)
def draw_half_alleys_around_shape(self, shape, x, y):
w, h = shape
left, right = x, x+w
top, bottom = y, y+h
def place_alley_tile(x, y):
if self[x][y] != self.A:
self.place_tile(self.A, x, y)
for x in range(right, right + self.half_alley):
for y in range(top - self.half_alley, bottom + self.half_alley):
place_alley_tile(x, y)
for x in range(left - self.half_alley, left):
for y in range(top - self.half_alley, bottom + self.half_alley):
place_alley_tile(x, y)
for x in range(left, right):
for y in range(top - self.half_alley, top):
place_alley_tile(x, y)
for x in range(left, right):
for y in range(bottom, bottom + self.half_alley):
place_alley_tile(x, y)
def too_small(self, w, h):
return w < self.shape_min or h < self.shape_min
def too_skinny(self, w, h):
return min(w, h) / max(w, h) < self.aspect_min
def enough_room(self, w, h, x, y):
for x_ in range(x, x+w): # check first row
if self[x_][y] != self.C:
return False
for y_ in range(y, y+h): # check final col
if self[x+w-1][y_] != self.C:
return False
return True
def bad_shape_for(self, shape, x, y):
w, h = shape
return self.too_small(w, h) or self.too_skinny(w, h) or not self.enough_room(w, h, x, y)
def get_snapped_shapes(self, x, y, target_area):
"""Return a list of shapes we can reasonably snap to.
We can snap in one or two directions. If we can snap in two directions,
then the return list will only have one item: the two-snapped shape. If
we can't snap in two directions but we can snap in one or the other
direction, then we return a list of all the possible shapes snapped in
either direction.
"""
right_bounds = self.get_right_bounds(x, y)
bottom_bounds = self.get_bottom_bounds(x, y)
# Two-Snappers
# ============
two_snappers = []
for right_bound in right_bounds:
for bottom_bound in bottom_bounds:
w = right_bound - x
h = bottom_bound - y
if self.bad_shape_for((w, h), x, y):
continue
candidate_area = w * h
delta = abs(target_area - candidate_area)
threshold = target_area - (target_area * self.area_threshold)
if delta <= threshold:
shape = (w, h)
two_snappers.append(shape)
if two_snappers:
return two_snappers
# One-Snappers
# ============
one_snappers = []
def enough_remaining(x, y):
try:
return self[x][y] == self.C
except IndexError:
return False
for right_bound in right_bounds:
w = right_bound - x
h = target_area // w
while h and not enough_remaining(x, y + h-1 + self.shape_min):
h -= 1
if not self.bad_shape_for((w, h), x, y):
one_snappers.append((w, h))
for bottom_bound in bottom_bounds:
h = bottom_bound - y
w = target_area // h
while w and not enough_remaining(x + w-1 + self.shape_min, y):
w -= 1
if not self.bad_shape_for((w, h), x, y):
one_snappers.append((w, h))
return one_snappers
def get_right_bounds(self, x, y):
return self._get_bounds(self.W, x, y, lambda a,b: self[a][b])
def get_bottom_bounds(self, x, y):
return self._get_bounds(self.H, y, x, lambda a,b: self[b][a])
def _get_bounds(self, D, a, b, tile):
bounds = set()
into_alley = 0
b_ = b - self.half_alley - 1
while a < D - self.half_alley:
a += 1
# hard bound
if tile(a, b) == self.A:
bounds.add(a)
break
# soft bounds
if b_ < self.half_alley:
continue
c = tile(a, b_)
if c == self.A:
into_alley += 1
if into_alley == self.half_alley:
bounds.add(a+1)
elif c == self.B:
into_alley = 0
return sorted(list(bounds))
def get_unsnapped_shapes(self, x, y, target_area):
lo = self.shape_min
hi = None
while 1:
hi = target_area // lo
if lo / hi >= 0.2:
break # maintain a certain minimum aspect ratio
else:
lo += 1
unsnapped = []
def enough_room(w, h):
try:
return self[x+w][y+h] in (self.C, self.A)
except IndexError:
return False
for w in range(lo, hi+1):
h = target_area // w
if enough_room(w, h):
unsnapped.append((w, h))
return unsnapped
def assign_ids(self, pathways, take_first=True):
"""Given a pathways data structure, assign resources to shapes.
"""
solutions = pathways_solver.solve( self.shapes
, pathways
, take_first
, relax_crossings_until=1e8
)
self.assignments = dict(pathways_solver.flatten(random.choice(solutions)))
assert len(set(self.assignments.values())) == len(self.assignments)
return solutions
def fake_data(N):
return [random.randint(1, 20) for i in range(N)]
charsets = { 'ascii': '-# '
, 'utf8': '\u2591\u2593 '
, 'svg': 'SVG' # hack
}
def err(*a, **kw):
print(file=sys.stderr, *a, **kw)
def generate_map(topics, charset='utf8', width=1024, height=1024, alley_width=6, building_min=10):
charset = charsets[charset]
canvas_size = (width, height)
street_width = alley_width * 10
offset = street_width - alley_width
big = [(topic_id, len(topic['subtopics'])) for topic_id, topic in topics.items()]
big = fill_one( charset
, 'the whole thing'
, canvas_size
, big
, street_width
, building_min
, monkeys=False
, aspect_min=0.5
)
print(big.to_svg(), file=open('output/big.svg', 'w+')) # for debugging
blocks = []
for topic_id, topic in topics.items():
small = []
for subtopic in topic['subtopics'].values():
for resource in subtopic['resources'].values():
small.append((resource['id'], None))
err()
err(topic_id, '-' * (80 - len(topic_id) - 1))
err()
x, y, (w, h) = big.shapes[topic_id]
canvas_size = (w - offset, h - offset)
blocks.append((topic_id, fill_one( charset
, topic_id
, canvas_size
, small
, alley_width
, building_min
, aspect_min=0.2
, monkeys=True
)))
return big, blocks
def output_svg(topics, fp, big, blocks):
half_W = big.W / 2
half_H = big.H / 2
rotated_side = lambda x: int(ceil(sqrt((x ** 2) / 2)))
w = h = rotated_side(big.W) + rotated_side(big.H)
half_w = w / 2
half_h = h / 2
print('<svg width="{}px" height="{}px" '
'xmlns="http://www.w3.org/2000/svg">'.format(w, h), file=fp)
print(' <g transform="translate({} {}) rotate(45 {} {})">'
.format(half_w - half_W, half_h - half_H, half_W, half_H), file=fp)
offset = big.alley_width // 2
for uid, block in blocks:
x, y, shape = big.shapes[uid]
subtopics = topics[uid]['subtopics'].values()
pathways = {s['id']: s['dag']['names'] for s in subtopics}
block.assign_ids(pathways)
print(block.to_svg(uid, x + offset, y + offset), file=fp)
print(' </g>', file=fp)
print('</svg>', file=fp)
def fill_one(charset, name, canvas_size, magnitudes, alley_width, building_min, monkeys, **kw):
i = 0
mfunc = (lambda m: random.randint(3, 10)) if monkeys else (lambda m: m)
while 1:
i += 1
err('Iteration:', i)
magnitudes = [(uid, mfunc(m)) for uid, m in magnitudes]
nmagnitudes = len(magnitudes)
smagnitudes = sum([m[1] for m in magnitudes])
nplaced = 0
nremaining = nmagnitudes
m = MagnitudeMap(canvas_size=canvas_size, sum_of_magnitudes=smagnitudes, charset=charset,
alley_width=alley_width, building_min=building_min, **kw)
try:
for uid, magnitude in magnitudes:
m.add(magnitude, uid=uid)
nplaced += 1
nremaining -= 1
except:
tb = traceback.format_exc()
else:
tb = ''
err()
err("Placed {} out of {} magnitudes for {}.".format(nplaced, len(magnitudes), name))
err( "Sum of remaining magnitudes: {} / {} ({:.1f}%)".format(
m.remaining_magnitudes
, m.sum_of_magnitudes
, (m.remaining_magnitudes / m.sum_of_magnitudes) * 100
))
err( "Remaining area: {} / {} ({:.1f}%)".format(
m.remaining_area
, m.area
, (m.remaining_area / m.area) * 100
))
if tb:
err()
err(tb)
if nremaining == 0 and m.remaining_area == 0:
break
return m
def dump(big, blocks):
pickle.dump([big] + blocks, open('output/map.cache', 'bw+'))
def load():
big, blocks = None, None
try:
inp = pickle.load(open('output/map.cache', 'br'))
big = inp[0]
blocks = inp[1:]
except FileNotFoundError:
pass
return big, blocks
def main(topics, fp, *a, **kw):
big, blocks = load()
if blocks is None:
big, blocks = generate_map(topics, *a, **kw)
dump(big, blocks)
output_svg(topics, fp, big, blocks)
if __name__ == '__main__':
import argparse, json
parser = argparse.ArgumentParser(description='Generate a CaaC map.')
parser.add_argument('input', help='the name of an input file in json format, or - for stdin')
parser.add_argument('output', help='the name of an output file, or - for stdout')
parser.add_argument('--charset', '-c', default='utf8', help='the character set to use',
choices=sorted(charsets.keys()))
parser.add_argument('--width', '-W', default=128, type=int, help='the width of the canvas')
parser.add_argument('--height', '-H', default=128, type=int, help='the height of the canvas')
parser.add_argument('--alley_width', '-a', default=6, type=int, help='the width of the alleys')
parser.add_argument('--building_min', '-b', default=10, type=int,
help='the minimum width of the blocks')
args = parser.parse_args()
topics = json.load(sys.stdin if args.input == '-' else open(args.input, 'r'))
fp = sys.stdout if args.output == '-' else open(args.output, 'w+')
args.__dict__.pop('input')
args.__dict__.pop('output')
main(topics, fp, **args.__dict__)