"""SVG drawings generated from the manifest. Units are mm (viewBox in mm); y is flipped for SVG.""" import numpy as np from shapely.geometry import LineString, Point, box from shapely import affinity import geom as G, manifest as M from geom import TOP from paths import WORK VB = (-130.0, -(TOP+156), 260.0, TOP+156+104) # x, y, w, h for the full mask, front or back view BUILD = dict(base='#d9d6cc', base_s='#5b5850', L='#2f6b2f', M='#4c8a3c', S='#77ad4e', T='#93c25c', leaf_s='#1d4a1f', vein='#1d4a1f', bark='#6b4a2b', bark_s='#3e2a16', ridge='#8f6d42', nut='#9a6a34', cap='#5a3d22', tendril='#2e6b2f', strap='#2b2b2b', patch='#bdb7a3', wire='#777', glasses='#8a8a8a') GHOST = dict(base='#eceae4', base_s='#9a978e', L='#e3e2dc', M='#e3e2dc', S='#e3e2dc', T='#e3e2dc', leaf_s='#b3b1a9', vein='#c4c2ba', bark='#dddbd4', bark_s='#b3b1a9', ridge='#c9c7bf', nut='#dddbd4', cap='#d0cec6', tendril='#cfcdc5', strap='#bcbab2', patch='#e6e3d8', wire='#bbb', glasses='#b8b8b8') HL = '#c8321e' def f(v): s = f'{v:.2f}'.rstrip('0').rstrip('.') return '0' if s == '-0' else s # keep output identical across platforms def path(geom, mirror=False): """SVG path data for a shapely polygon / multipolygon (y flipped; optionally x mirrored for the back view).""" sx = -1 if mirror else 1 if geom.geom_type == 'MultiPolygon': return ' '.join(path(g, mirror) for g in geom.geoms) if geom.geom_type != 'Polygon': return '' out = [] for ring in [geom.exterior] + list(geom.interiors): c = list(ring.coords) out.append('M' + ' L'.join(f'{f(sx*x)} {f(-y)}' for x, y in c[:-1]) + ' Z') return ' '.join(out) def line(ls, mirror=False): sx = -1 if mirror else 1 return 'M' + ' L'.join(f'{f(sx*x)} {f(-y)}' for x, y in ls.coords) def _leaf(p, pal, mirror, hl): size = p['part']; tone = p.get('tone') fill = (pal.get(tone) if tone else None) or pal[size] edge = pal.get('edge_' + tone) if tone else None vein = (pal.get('vein_' + tone) if tone else None) or pal['vein'] s = f'' v = ' '.join(line(G.place(l, p['x'], p['y'], p['ang'], p['flip']), mirror) for l in G.vein_lines(size, p.get('variant', 0))) return s + f'' def _strip(p, pal, mirror, hl): s = f'' x = np.linspace(-G.STRIP_L/2+4, G.STRIP_L/2-4, 60); grooves = [] for k in range(5): y = -7 + k*3.5 + 1.6*np.sin(x*0.09+k) + 0.7*np.sin(x*0.31+2*k) keep = np.abs(y) < G.strip_half(x) - 1.5 seg = [(a, b + G.STRIP_Y) for a, b, kk in zip(x, y, keep) if kk] if len(seg) > 2: grooves.append(line(LineString(seg), mirror)) s += f'' for kx in G.STRIP_KNOB_X: s += f'' return s def _knot(p, pal, mirror, hl): cx, cy = G.KNOT_C; s = '' for r, w in ((G.KNOT_R, 0.9 if hl else 0.5), (14, 0.7), (8, 0.7)): s += f'' s += f'' if pal.get('gloss'): s += f'' return s def _acorn(p, pal, mirror, hl): w, l = G.ACORN[p['part']]; e = affinity.scale(Point(0, 0).buffer(1, 32), w/2, l/2) cap = affinity.scale(e, 1.14, 1.0).intersection(box(-w, l*0.08, w, l)) # cap covers the upper ~40% tfm = lambda g: G.place(g, p['x'], p['y'], p['ang']) stroke = HL if hl else pal['bark_s']; sw = 0.9 if hl else 0.45 s = f'' if pal.get('gloss'): s += f'' s += f'' if pal.get('cap_hi'): dots = [Point(dx, l*0.2+dy).buffer(0.9, 6) for dy in (l*0.02, l*0.12, l*0.22) for dx in np.linspace(-w*0.36, w*0.36, 5)] s += ''.join(f'' for d in dots if cap.contains(d)) return s def _poly(p, pal, mirror, hl, fill, stroke, sw=0.45, dash=None): d = f' stroke-dasharray="{dash}"' if dash else '' return f'' def _part(p, pal, mirror, hl): k = p['part'] if k in 'LMST': return _leaf(p, pal, mirror, hl) if k == 'STRIP': return _strip(p, pal, mirror, hl) if k == 'KNOT': return _knot(p, pal, mirror, hl) if k in ('AL', 'AS', 'AF'): return _acorn(p, pal, mirror, hl) if k == 'TWIG': return _poly(p, pal, mirror, hl, pal['bark'], pal['bark_s']) if k == 'SPIRAL': return _poly(p, pal, mirror, hl, pal['tendril'], pal.get('tendril_s') or pal['leaf_s'], 0.4) if k == 'STRAP': return _poly(p, pal, mirror, hl, pal['strap'], pal['strap'], 0.3) if k == 'PATCH': return _poly(p, pal, mirror, hl, pal['patch'], pal['base_s'], 0.4, '1.5 1') if k == 'WIRE': return f'' return '' def _base(base, pal, mirror): return f'' def _glasses(pal): s = ''.join(f'' for sg in (-1, 1)) s += f'' for sg in (-1, 1): s += f'' return s def scene(view='front', done=(), hl=None, palette=None, numbers=False, glasses=True, tones=None): """One picture of the mask. done = steps already on (ghosted unless palette given); hl = step being added now.""" base = G.base_glasses(); mirror = view == 'back' show = set(done) | ({hl} if hl else set()) parts = [p for p in M.PLACEMENTS if p['step'] in show] def pal_for(p): if palette: return palette return BUILD if p['step'] == hl else GHOST backs = sorted([p for p in parts if p['face'] == 'back'], key=lambda p: -p['z']) fronts = sorted([p for p in parts if p['face'] != 'back'], key=lambda p: p['z']) for p in parts: p['tone'] = tones.get(p['id'].rsplit('-', 1)[0]) if tones else None body = [] bpal = palette or (BUILD if (hl in (None, 'strap') or not done) else GHOST) if view == 'front': body += [_part(p, pal_for(p), False, p['step'] == hl and not palette) for p in backs if p['part'] != 'PATCH'] body.append(_base(base, bpal, False)) for p in fronts: if p['part'] == 'AF': w = next(q for q in M.PLACEMENTS if q['id'] == p['onto'][0]) body.append(f'') body.append(_part(p, pal_for(p), False, p['step'] == hl and not palette)) if glasses: body.append(_glasses(bpal)) else: body.append(_base(base, bpal, True)) for p in sorted(backs, key=lambda p: p['z']): body.append(_part(p, pal_for(p), True, p['step'] == hl and not palette)) if 'strap' in show: # the elastic as seen from behind: through the slit, tail lying on the patch, strap leaving sideways sp = BUILD if hl == 'strap' else GHOST for sg in (-1, 1): x0 = -sg*G.SLIT_X; out = -sg*(G.FRAME_W/2+23) body.append(f'') body.append(f'') if numbers and hl: seq = [p for p in M.step_placements(hl) if p['footprint'] is not None and p['part'] != 'STRAP' and (p['face'] == 'back') == mirror] for i, p in enumerate(seq): c = p['footprint'].centroid; x = (-1 if mirror else 1)*c.x body.append(f'{i+1}') return f'{"".join(body)}' if __name__ == '__main__': steps = list(M.STEPS) html = '' html += f'
{scene("front", done=steps, palette=BUILD)}
' html += f'
{scene("back", done=["strap","strip","back","twigs"], palette=BUILD)}
' html += f'
{scene("front", done=steps[:6], hl="front", numbers=True)}
' html += f'
{scene("back", done=["strap","strip"], hl="back", numbers=True)}
' html += f'
{scene("front", done=steps[:8], hl="tendrils", numbers=True)}
' html += f'
{scene("front", done=steps[:9], hl="cheeks", numbers=True)}
' open(WORK / 'preview.html', 'w').write(html + '')