|
1
|
1 """
|
|
|
2 mask steps of the build: pixel classes, placement grid and routes.
|
|
|
3
|
|
9
|
4 a mask is a map-sized image with a red path centre line, blue entrances and
|
|
|
5 green exits on a transparent background. edges are hard: every pixel is
|
|
|
6 either fully transparent or opaque pure red, blue or green. pixels are
|
|
|
7 indexed as y * width + x throughout.
|
|
1
|
8 """
|
|
|
9
|
|
|
10 import math
|
|
|
11 from collections import deque
|
|
|
12
|
|
9
|
13 from PIL import Image, ImageChops, ImageDraw
|
|
1
|
14
|
|
|
15 FREE, PATH, ENTRANCE, EXIT = 0, 1, 2, 3
|
|
9
|
16 # rgba bytes of the opaque mask colours
|
|
|
17 COLOURS = {b"\xff\x00\x00\xff": PATH, b"\x00\x00\xff\xff": ENTRANCE,
|
|
|
18 b"\x00\xff\x00\xff": EXIT}
|
|
1
|
19
|
|
|
20 # 8-neighbour offsets in the order n, ne, e, se, s, sw, w, nw
|
|
|
21 NEIGHBOURS = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1))
|
|
|
22
|
|
|
23
|
|
|
24 class Mask:
|
|
9
|
25 """
|
|
|
26 pixel classes of one map mask. raises ValueError when a pixel is
|
|
|
27 neither transparent nor one of the mask colours.
|
|
|
28 """
|
|
1
|
29
|
|
|
30 def __init__(self, path):
|
|
|
31 im = Image.open(path).convert("RGBA")
|
|
|
32 self.w, self.h = im.size
|
|
|
33 data = im.tobytes()
|
|
|
34 cls = bytearray(self.w * self.h)
|
|
9
|
35 bad = []
|
|
1
|
36 for i in range(self.w * self.h):
|
|
|
37 j = i * 4
|
|
9
|
38 if data[j + 3] == 0:
|
|
|
39 continue
|
|
|
40 c = COLOURS.get(data[j:j + 4])
|
|
|
41 if c is None:
|
|
|
42 bad.append(i)
|
|
|
43 else:
|
|
|
44 cls[i] = c
|
|
|
45 if bad:
|
|
|
46 raise ValueError(f"{path}: {len(bad)} pixels are not transparent, red, blue or"
|
|
|
47 f" green, the first at {bad[0] % self.w}, {bad[0] // self.w}")
|
|
1
|
48 self.cls = cls
|
|
|
49
|
|
|
50 def count(self, kind):
|
|
|
51 return self.cls.count(kind)
|
|
|
52
|
|
|
53 def blobs(self, kind):
|
|
|
54 """
|
|
|
55 return the connected areas of one class, largest first, as a list
|
|
|
56 of (centre x, centre y, radius, pixel count). radius is that of a
|
|
|
57 disc with the same area.
|
|
|
58 """
|
|
|
59 w, h, cls = self.w, self.h, self.cls
|
|
|
60 seen = bytearray(w * h)
|
|
|
61 out = []
|
|
|
62 for start in range(w * h):
|
|
|
63 if cls[start] != kind or seen[start]:
|
|
|
64 continue
|
|
|
65 seen[start] = 1
|
|
|
66 queue = deque([start])
|
|
|
67 sx = sy = n = 0
|
|
|
68 while queue:
|
|
|
69 i = queue.popleft()
|
|
|
70 x, y = i % w, i // w
|
|
|
71 sx += x
|
|
|
72 sy += y
|
|
|
73 n += 1
|
|
|
74 for dx, dy in NEIGHBOURS:
|
|
|
75 nx, ny = x + dx, y + dy
|
|
|
76 if 0 <= nx < w and 0 <= ny < h:
|
|
|
77 k = ny * w + nx
|
|
|
78 if cls[k] == kind and not seen[k]:
|
|
|
79 seen[k] = 1
|
|
|
80 queue.append(k)
|
|
|
81 out.append((sx / n, sy / n, math.sqrt(n / math.pi), n))
|
|
|
82 out.sort(key=lambda b: -b[3])
|
|
|
83 return out
|
|
|
84
|
|
|
85 def grown(self, kinds, radius):
|
|
|
86 """
|
|
|
87 return an L image, 255 on pixels of the given classes and on every
|
|
|
88 pixel within radius of one. drawn as discs around the edge pixels
|
|
|
89 of the area, which is the same as a distance test.
|
|
|
90 """
|
|
|
91 w, h, cls = self.w, self.h, self.cls
|
|
|
92 im = Image.new("L", (w, h), 0)
|
|
|
93 draw = ImageDraw.Draw(im)
|
|
|
94 inside = bytes(255 if c in kinds else 0 for c in cls)
|
|
|
95 im.paste(Image.frombytes("L", (w, h), inside))
|
|
|
96 for y in range(h):
|
|
|
97 row = y * w
|
|
|
98 for x in range(w):
|
|
|
99 if not inside[row + x]:
|
|
|
100 continue
|
|
|
101 edge = (x == 0 or x == w - 1 or y == 0 or y == h - 1
|
|
|
102 or not inside[row + x - 1] or not inside[row + x + 1]
|
|
|
103 or not inside[row + x - w] or not inside[row + x + w])
|
|
|
104 if edge:
|
|
|
105 draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=255)
|
|
|
106 return im
|
|
|
107
|
|
|
108
|
|
9
|
109 def discs(w, h, points, radius):
|
|
|
110 """return an L image of size w x h, 255 within radius of any of the points."""
|
|
|
111 im = Image.new("L", (w, h), 0)
|
|
|
112 draw = ImageDraw.Draw(im)
|
|
|
113 for x, y in points:
|
|
|
114 draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=255)
|
|
|
115 return im
|
|
|
116
|
|
|
117
|
|
|
118 def placement_grid(mask, p, centre, half_width, spaces):
|
|
1
|
119 """
|
|
9
|
120 return (cols, rows, grid string) for placement table p. the road is
|
|
|
121 taken as half_width either side of the centre line, given as a list of
|
|
|
122 pixels. spaces is a list of (points, space). a cell is legal when a
|
|
|
123 tower footprint centred on it does not overlap the road, entrances or
|
|
|
124 exits, comes within max_gap of the road, keeps each space from its
|
|
|
125 points and keeps edge_distance from the map edge. the string has one
|
|
|
126 '1' or '0' per cell, row by row.
|
|
1
|
127 """
|
|
|
128 cell = p["grid_cell"]
|
|
|
129 r = p["footprint_radius"]
|
|
9
|
130 w, h = mask.w, mask.h
|
|
|
131 # the centre line inside entrance and exit areas does not count as road
|
|
|
132 road = [(x, y) for x, y in centre if mask.cls[y * w + x] == PATH]
|
|
|
133 blocked = ImageChops.lighter(mask.grown((ENTRANCE, EXIT), r),
|
|
|
134 discs(w, h, road, r + half_width))
|
|
|
135 for points, space in spaces:
|
|
|
136 blocked = ImageChops.lighter(blocked, discs(w, h, points, r + space))
|
|
|
137 blocked = blocked.tobytes()
|
|
|
138 near = discs(w, h, road, r + half_width + p["max_gap"]).tobytes()
|
|
1
|
139 edge = p["edge_distance"]
|
|
|
140 cols, rows = w // cell, h // cell
|
|
|
141 out = []
|
|
|
142 for cy in range(rows):
|
|
|
143 y = cy * cell + cell // 2
|
|
|
144 for cx in range(cols):
|
|
|
145 x = cx * cell + cell // 2
|
|
|
146 i = y * w + x
|
|
|
147 ok = (near[i] and not blocked[i]
|
|
|
148 and edge <= x <= w - edge and edge <= y <= h - edge)
|
|
|
149 out.append("1" if ok else "0")
|
|
|
150 return cols, rows, "".join(out)
|
|
|
151
|
|
|
152
|
|
|
153 def thin(shape, w, h):
|
|
|
154 """
|
|
|
155 reduce a 0/1 bytearray shape to a one pixel wide centre line
|
|
|
156 (zhang-suen thinning). the shape must not touch the image border.
|
|
|
157 only pixels next to removed ones are re-examined, which keeps the
|
|
|
158 pure python loop fast. returns the thinned bytearray.
|
|
|
159 """
|
|
|
160 img = bytearray(shape)
|
|
|
161 offs = [dy * w + dx for dx, dy in NEIGHBOURS]
|
|
|
162
|
|
|
163 def touches_background(i):
|
|
|
164 return any(not img[i + o] for o in offs)
|
|
|
165
|
|
|
166 cand = {i for i in range(w * h) if img[i] and touches_background(i)}
|
|
|
167 while True:
|
|
|
168 removed_any = False
|
|
|
169 for step in (0, 1):
|
|
|
170 remove = []
|
|
|
171 for i in cand:
|
|
|
172 if not img[i]:
|
|
|
173 continue
|
|
|
174 p = [img[i + o] for o in offs]
|
|
|
175 b = sum(p)
|
|
|
176 if b < 2 or b > 6:
|
|
|
177 continue
|
|
|
178 a = sum(1 for k in range(8) if not p[k] and p[(k + 1) % 8])
|
|
|
179 if a != 1:
|
|
|
180 continue
|
|
|
181 n, e, s, wst = p[0], p[2], p[4], p[6]
|
|
|
182 if step == 0:
|
|
|
183 if n and e and s or e and s and wst:
|
|
|
184 continue
|
|
|
185 else:
|
|
|
186 if n and e and wst or n and s and wst:
|
|
|
187 continue
|
|
|
188 remove.append(i)
|
|
|
189 if remove:
|
|
|
190 removed_any = True
|
|
|
191 nxt = set()
|
|
|
192 for i in remove:
|
|
|
193 img[i] = 0
|
|
|
194 for i in remove:
|
|
|
195 for o in offs:
|
|
|
196 if img[i + o]:
|
|
|
197 nxt.add(i + o)
|
|
|
198 cand = (cand | nxt) - set(remove)
|
|
|
199 if not removed_any:
|
|
|
200 return img
|
|
|
201
|
|
|
202
|
|
|
203 class Graph:
|
|
|
204 """
|
|
|
205 the centre line as a graph. nodes are junctions and line ends, edges
|
|
|
206 the pixel chains between them.
|
|
|
207
|
|
|
208 nodes: id -> (x, y)
|
|
|
209 edges: id -> [node a, node b, [(x, y), ...] from a to b]
|
|
|
210 """
|
|
|
211
|
|
|
212 def __init__(self, skel, w):
|
|
|
213 self.w = w
|
|
|
214 pix = {i for i, v in enumerate(skel) if v}
|
|
|
215 offs = [dy * w + dx for dx, dy in NEIGHBOURS]
|
|
|
216
|
|
|
217 def nbrs(i):
|
|
|
218 return [i + o for o in offs if i + o in pix]
|
|
|
219
|
|
|
220 # line pixels have exactly two neighbours, all others belong to nodes
|
|
|
221 nodepix = {i for i in pix if len(nbrs(i)) != 2}
|
|
|
222 node_of = {}
|
|
|
223 self.nodes = {}
|
|
|
224 for i in nodepix:
|
|
|
225 if i in node_of:
|
|
|
226 continue
|
|
|
227 nid = len(self.nodes)
|
|
|
228 members = []
|
|
|
229 queue = deque([i])
|
|
|
230 node_of[i] = nid
|
|
|
231 while queue:
|
|
|
232 j = queue.popleft()
|
|
|
233 members.append(j)
|
|
|
234 for k in nbrs(j):
|
|
|
235 if k in nodepix and k not in node_of:
|
|
|
236 node_of[k] = nid
|
|
|
237 queue.append(k)
|
|
|
238 self.nodes[nid] = (sum(m % w for m in members) / len(members),
|
|
|
239 sum(m // w for m in members) / len(members))
|
|
|
240 self.edges = {}
|
|
|
241 seen = set()
|
|
|
242 for i in nodepix:
|
|
|
243 for j in nbrs(i):
|
|
|
244 if j in nodepix or j in seen:
|
|
|
245 continue
|
|
|
246 chain = [i, j]
|
|
|
247 seen.add(j)
|
|
|
248 prev, cur = i, j
|
|
|
249 while True:
|
|
|
250 nxt = [k for k in nbrs(cur) if k != prev]
|
|
|
251 if not nxt:
|
|
|
252 break
|
|
|
253 k = nxt[0]
|
|
|
254 chain.append(k)
|
|
|
255 if k in nodepix:
|
|
|
256 break
|
|
|
257 seen.add(k)
|
|
|
258 prev, cur = cur, k
|
|
|
259 if chain[-1] not in nodepix:
|
|
|
260 continue
|
|
|
261 a, b = node_of[i], node_of[chain[-1]]
|
|
|
262 if a == b and len(chain) < 8:
|
|
|
263 continue
|
|
|
264 self.add_edge(a, b, [(c % w, c // w) for c in chain])
|
|
|
265
|
|
|
266 def add_edge(self, a, b, pts):
|
|
|
267 eid = max(self.edges, default=-1) + 1
|
|
|
268 self.edges[eid] = [a, b, pts]
|
|
|
269 return eid
|
|
|
270
|
|
|
271 def degree(self, n):
|
|
|
272 return sum((e[0] == n) + (e[1] == n) for e in self.edges.values())
|
|
|
273
|
|
|
274 def incident(self, n):
|
|
|
275 return [eid for eid, e in self.edges.items() if n in (e[0], e[1])]
|
|
|
276
|
|
|
277 def nearest_end(self, x, y):
|
|
|
278 """return the node of degree 1 nearest to a point."""
|
|
|
279 ends = [n for n in self.nodes if self.degree(n) == 1]
|
|
|
280 return min(ends, key=lambda n: math.dist(self.nodes[n], (x, y)))
|
|
|
281
|
|
|
282 def prune(self, keep, min_len):
|
|
|
283 """drop dead ends shorter than min_len, except at nodes in keep."""
|
|
|
284 changed = True
|
|
|
285 while changed:
|
|
|
286 changed = False
|
|
|
287 for n in list(self.nodes):
|
|
|
288 if n in keep or self.degree(n) != 1:
|
|
|
289 continue
|
|
|
290 eid = self.incident(n)[0]
|
|
|
291 if polyline_length(self.edges[eid][2]) < min_len:
|
|
|
292 del self.edges[eid]
|
|
|
293 del self.nodes[n]
|
|
|
294 changed = True
|
|
|
295 changed |= self.merge()
|
|
|
296
|
|
|
297 def contract(self, keep, min_len):
|
|
|
298 """
|
|
|
299 join junctions closer than min_len along an edge into one node,
|
|
|
300 except nodes in keep. thinning splits a wide crossing into several
|
|
|
301 nearby junctions; a loop attached to one of them could otherwise
|
|
|
302 not be walked without visiting a junction twice.
|
|
|
303 """
|
|
|
304 changed = True
|
|
|
305 while changed:
|
|
|
306 changed = False
|
|
|
307 # shortest first, so a chain of nearby junctions joins around its middle
|
|
|
308 for eid, (a, b, pts) in sorted(self.edges.items(),
|
|
|
309 key=lambda e: polyline_length(e[1][2])):
|
|
|
310 if a == b or a in keep or b in keep or polyline_length(pts) >= min_len:
|
|
|
311 continue
|
|
|
312 del self.edges[eid]
|
|
|
313 # reattach the edges of b to a, extended by the joining pixels
|
|
|
314 for f in self.edges.values():
|
|
|
315 if f[0] == b:
|
|
|
316 f[0], f[2] = a, pts[:-1] + f[2]
|
|
|
317 if f[1] == b:
|
|
|
318 f[1], f[2] = a, f[2] + pts[::-1][1:]
|
|
|
319 del self.nodes[b]
|
|
|
320 changed = True
|
|
|
321 break
|
|
|
322
|
|
|
323 def merge(self):
|
|
|
324 """join the two edges at every node of degree 2. returns True on change."""
|
|
|
325 changed = False
|
|
|
326 for n in list(self.nodes):
|
|
|
327 inc = self.incident(n)
|
|
|
328 if len(inc) != 2 or inc[0] == inc[1]:
|
|
|
329 continue
|
|
|
330 e1, e2 = (self.edges[i] for i in inc)
|
|
|
331 p1 = e1[2] if e1[1] == n else e1[2][::-1]
|
|
|
332 a = e1[0] if e1[1] == n else e1[1]
|
|
|
333 p2 = e2[2] if e2[0] == n else e2[2][::-1]
|
|
|
334 b = e2[1] if e2[0] == n else e2[0]
|
|
|
335 if a == n or b == n:
|
|
|
336 continue
|
|
|
337 del self.edges[inc[0]], self.edges[inc[1]]
|
|
|
338 del self.nodes[n]
|
|
|
339 self.add_edge(a, b, p1 + p2[1:])
|
|
|
340 changed = True
|
|
|
341 return changed
|
|
|
342
|
|
|
343 def paths(self, start, goal):
|
|
|
344 """
|
|
|
345 list every route from start to goal that visits no node twice, as
|
|
|
346 a list of (points, key) pairs. a loop that leaves a node and comes
|
|
|
347 back to it (a self-loop edge) may be walked once, in either
|
|
|
348 direction; key names the edges taken without the loop directions,
|
|
|
349 so the two directions of the same route share a key.
|
|
|
350 """
|
|
|
351 out = []
|
|
|
352
|
|
|
353 def walk(n, visited, used, pts, key):
|
|
|
354 if n == goal:
|
|
|
355 out.append((pts, tuple(key)))
|
|
|
356 return
|
|
|
357 for eid in self.incident(n):
|
|
|
358 a, b, epts = self.edges[eid]
|
|
|
359 if a == b:
|
|
|
360 if eid in used:
|
|
|
361 continue
|
|
|
362 for seg in (epts, epts[::-1]):
|
|
|
363 walk(n, visited, used | {eid}, pts + seg[1:], key + [("loop", eid)])
|
|
|
364 continue
|
|
|
365 if a == n:
|
|
|
366 m, seg = b, epts
|
|
|
367 else:
|
|
|
368 m, seg = a, epts[::-1]
|
|
|
369 if m in visited:
|
|
|
370 continue
|
|
|
371 walk(m, visited | {m}, used, pts + seg[1:], key + [eid])
|
|
|
372
|
|
|
373 walk(start, {start}, frozenset(), [self.nodes[start]], [])
|
|
|
374 return out
|
|
|
375
|
|
|
376
|
|
|
377 def polyline_length(pts):
|
|
|
378 return sum(math.dist(pts[i], pts[i + 1]) for i in range(len(pts) - 1))
|
|
|
379
|
|
|
380
|
|
|
381 def turning(pts):
|
|
|
382 """sum of direction changes along a polyline, in degrees."""
|
|
|
383 total = 0.0
|
|
|
384 for i in range(1, len(pts) - 1):
|
|
|
385 a1 = math.atan2(pts[i][1] - pts[i - 1][1], pts[i][0] - pts[i - 1][0])
|
|
|
386 a2 = math.atan2(pts[i + 1][1] - pts[i][1], pts[i + 1][0] - pts[i][0])
|
|
|
387 d = (a2 - a1 + math.pi) % (2 * math.pi) - math.pi
|
|
|
388 total += abs(d)
|
|
|
389 return math.degrees(total)
|
|
|
390
|
|
|
391
|
|
|
392 def resample(pts, step):
|
|
|
393 """return points spaced step apart along the polyline, ends kept."""
|
|
|
394 out = [pts[0]]
|
|
|
395 carry = 0.0
|
|
|
396 for i in range(len(pts) - 1):
|
|
|
397 (x0, y0), (x1, y1) = pts[i], pts[i + 1]
|
|
|
398 seg = math.dist(pts[i], pts[i + 1])
|
|
|
399 d = step - carry
|
|
|
400 while d <= seg:
|
|
|
401 t = d / seg
|
|
|
402 out.append((x0 + (x1 - x0) * t, y0 + (y1 - y0) * t))
|
|
|
403 d += step
|
|
|
404 carry = seg - (d - step)
|
|
|
405 if math.dist(out[-1], pts[-1]) > step * 0.25:
|
|
|
406 out.append(pts[-1])
|
|
|
407 else:
|
|
|
408 out[-1] = pts[-1]
|
|
|
409 return out
|
|
|
410
|
|
|
411
|
|
|
412 def smooth(pts, half):
|
|
|
413 """moving average over 2 * half + 1 points; the window shrinks at the ends."""
|
|
|
414 n = len(pts)
|
|
|
415 out = []
|
|
|
416 for i in range(n):
|
|
|
417 k = min(half, i, n - 1 - i)
|
|
|
418 xs = pts[i - k:i + k + 1]
|
|
|
419 out.append((sum(p[0] for p in xs) / len(xs), sum(p[1] for p in xs) / len(xs)))
|
|
|
420 return out
|
|
|
421
|
|
|
422
|
|
|
423 def simplify(pts, tol):
|
|
|
424 """ramer-douglas-peucker: drop points closer than tol to the line."""
|
|
|
425 if len(pts) < 3:
|
|
|
426 return list(pts)
|
|
|
427 (x0, y0), (x1, y1) = pts[0], pts[-1]
|
|
|
428 dx, dy = x1 - x0, y1 - y0
|
|
|
429 norm = math.hypot(dx, dy) or 1.0
|
|
|
430 best, idx = 0.0, 0
|
|
|
431 for i in range(1, len(pts) - 1):
|
|
|
432 d = abs(dy * (pts[i][0] - x0) - dx * (pts[i][1] - y0)) / norm
|
|
|
433 if d > best:
|
|
|
434 best, idx = d, i
|
|
|
435 if best <= tol:
|
|
|
436 return [pts[0], pts[-1]]
|
|
|
437 return simplify(pts[:idx + 1], tol)[:-1] + simplify(pts[idx:], tol)
|
|
|
438
|
|
|
439
|
|
9
|
440 def line_width(mask, skel_len):
|
|
|
441 """average width of the drawn centre line: its area divided by its length."""
|
|
1
|
442 return mask.count(PATH) / skel_len if skel_len else 0.0
|
|
|
443
|
|
|
444
|
|
9
|
445 def leave_point(graph, mask, n, kind):
|
|
|
446 """
|
|
|
447 return the first centre line pixel outside the area of class kind,
|
|
|
448 walking from the line end n.
|
|
|
449 """
|
|
|
450 a, b, pts = graph.edges[graph.incident(n)[0]]
|
|
|
451 if a != n:
|
|
|
452 pts = pts[::-1]
|
|
|
453 for x, y in pts:
|
|
|
454 if mask.cls[y * mask.w + x] != kind:
|
|
|
455 return (x, y)
|
|
|
456 return pts[-1]
|
|
|
457
|
|
|
458
|
|
1
|
459 def extract_routes(mask, smooth_half, tol):
|
|
|
460 """
|
|
9
|
461 find the routes of a mask. returns a dict with the graph, the entrance
|
|
|
462 and exit blobs, the measured line width, the centre line pixels, the
|
|
|
463 spawn points where the centre line leaves each entrance, the exit
|
|
|
464 points where it enters each exit, and the routes
|
|
|
465 as point lists, entrance to exit, smoothed and simplified.
|
|
1
|
466 """
|
|
|
467 w, h = mask.w, mask.h
|
|
|
468 shape = bytearray(1 if c != FREE else 0 for c in mask.cls)
|
|
|
469 # keep a free border so the thinning never reads past the image
|
|
|
470 for x in range(w):
|
|
|
471 shape[x] = shape[(h - 1) * w + x] = 0
|
|
|
472 for y in range(h):
|
|
|
473 shape[y * w] = shape[y * w + w - 1] = 0
|
|
|
474 skel = thin(shape, w, h)
|
|
|
475 graph = Graph(skel, w)
|
|
|
476
|
|
|
477 entrances = mask.blobs(ENTRANCE)
|
|
|
478 exits = mask.blobs(EXIT)
|
|
9
|
479 width = line_width(mask, sum(polyline_length(e[2]) for e in graph.edges.values()))
|
|
1
|
480 terminals = [graph.nearest_end(b[0], b[1]) for b in entrances + exits]
|
|
|
481 graph.prune(set(terminals), 0.8 * width)
|
|
|
482 graph.contract(set(terminals), 0.75 * width)
|
|
|
483 graph.merge()
|
|
|
484 starts = terminals[:len(entrances)]
|
|
|
485 goals = terminals[len(entrances):]
|
|
|
486
|
|
|
487 routes = []
|
|
|
488 for s in starts:
|
|
|
489 for g in goals:
|
|
|
490 # of the two directions around a loop, keep the one that turns less
|
|
|
491 best = {}
|
|
|
492 for raw, key in graph.paths(s, g):
|
|
|
493 pts = resample(raw, 1.0)
|
|
|
494 pts = simplify(smooth(smooth(pts, smooth_half), smooth_half), tol)
|
|
|
495 if key not in best or turning(pts) < turning(best[key]):
|
|
|
496 best[key] = pts
|
|
|
497 routes.extend(best.values())
|
|
|
498 return {
|
|
|
499 "graph": graph,
|
|
|
500 "entrances": entrances,
|
|
|
501 "exits": exits,
|
|
|
502 "width": width,
|
|
9
|
503 "centre": [p for e in graph.edges.values() for p in e[2]],
|
|
|
504 "spawns": [leave_point(graph, mask, s, ENTRANCE) for s in starts],
|
|
|
505 "exit_points": [leave_point(graph, mask, g, EXIT) for g in goals],
|
|
1
|
506 "routes": routes,
|
|
|
507 }
|