"""Single source of truth for the Green Man mask: every part, where it goes, what it is glued to, and in what order. The cut list, print list, step order and all drawings are derived from PLACEMENTS. validate() fails loudly if anything is made but not placed, placed but not made, glued onto an occupied spot, or sequenced after something that covers it.""" from collections import Counter, OrderedDict from shapely.geometry import Point, LineString, box from shapely.ops import unary_union import geom as G from geom import TOP SLIT_HALF = G.SLIT_W/2 PARTS = OrderedDict([ # id name made from / file kind ('BASE', ('Mask base', '6 mm EVA foam', 'foam')), ('PATCH', ('Strap reinforcement patch', '2 mm EVA foam, 30 x 36 mm', 'foam')), ('L', ('Large oak leaf, 95 mm', '2 mm EVA foam', 'leaf')), ('M', ('Medium oak leaf, 70 mm', '2 mm EVA foam', 'leaf')), ('S', ('Small oak leaf, 48 mm', '2 mm EVA foam', 'leaf')), ('T', ('Tip leaf, 30 mm', '2 mm EVA foam', 'leaf')), ('STRIP', ('Brow strip', 'brow_strip.stl', 'print')), ('KNOT', ('Bark knot', 'bark_knot.stl', 'print')), ('TWIG', ('Forked twig', 'twig_forked.stl', 'print')), ('AL', ('Acorn, large half', 'acorn_large_half.stl', 'print')), ('AS', ('Acorn, small half', 'acorn_small_half.stl', 'print')), ('AF', ('Acorn, full (hangs on a line)', 'acorn_full.stl', 'print')), ('SPIRAL',('Tendril spiral', 'tendril_spiral.stl', 'print')), ('STRAP', ('Elastic strap, 19-25 mm wide', 'flat elastic, about 45 cm', 'other')), ('WIRE', ('Hanging line, 8 cm', 'fishing line or thin wire', 'other')), ]) TOOLS = OrderedDict([('STAMP', ('Leaf vein stamp', 'leaf_vein_stamp.stl'))]) SPARES = {'M': 1, 'S': 2} # cut and keep UNSEALED as repair stock; the only parts not placed # Assembly steps in order. 'after' lists (step, reason): the reason is printed in the guide. STEPS = OrderedDict([ ('strap', dict(title='Strap', view='both', prep=True, after=[])), ('strip', dict(title='Brow strip', view='front', after=[('strap', 'the strip is pressed on hard; do it while the band is bare and can be backed with your hand')])), ('clusters',dict(title='Acorn clusters', view='front', after=[('strap', 'the clusters cover the strap where it crosses the front of the band'), ('strip', 'the clusters butt against the ends of the strip')])), ('centre', dict(title='Crown, centre large leaves', view='front', after=[('strip', 'their stems sit on the front of the band above the strip'), ('clusters', 'the clusters are pressed on hard, which is easier before any leaves stand on the band')])), ('back', dict(title='Crown, outer large leaves', view='back', after=[('centre', 'the centre leaves set the height and rake that the outer ones are matched to')])), ('knot', dict(title='Bark knot', view='front', after=[('centre', 'the knot sits on top of the three centre stems and hides them'), ('strip', 'its lower edge touches the top edge of the strip')])), ('twigs', dict(title='Twigs', view='front', after=[('back', 'each twig lies up the front of the large leaves, so all seven need to be in place')])), ('front', dict(title='Crown, front row', view='front', after=[('knot', 'the two middle leaves fan around the knot and overlap its sides'), ('clusters', 'the outer leaves lean over the inner edge of each cluster'), ('twigs', 'the front-row leaves cover the thick ends of the twigs'), ('centre', 'the front row leans back onto the large leaves')])), ('hang', dict(title='Hanging acorns', view='front', after=[('clusters', 'so the hanging acorn can be judged against the cluster above it')])), ('tendrils',dict(title='Tendrils', view='front', after=[('hang', 'the cheek tab is still bare except for the line, so both tendril stems can be pressed flat')])), ('cheeks', dict(title='Cheek leaves', view='front', after=[('hang', 'two of the leaf stems go over the line and hide it'), ('tendrils', 'the leaf blades hang in front of the tendril stems; glued first, the leaves would be in the way')])), ('nose', dict(title='Nose leaf', view='front', after=[('strip', 'its stem sits just under the brow strip')])), ]) def _pair(fn): out = [] for s in (-1, 1): out += fn(s) return out def build_placements(): P = [] def add(pid, part, step, face, glue, shape, footprint, z, x=None, y=None, note='', onto=(), ang=0.0, flip=False, variant=0, count=1): P.append(dict(id=pid, part=part, step=step, face=face, glue=glue, shape=shape, footprint=footprint, z=z, x=x, y=y, note=note, onto=tuple(onto), ang=ang, flip=flip, variant=variant, count=count)) side = {-1: 'R', 1: 'L'} # wearer's right is at -x in the front view # --- strap: around the outer edge onto the front, through the slit front-to-back, tail glued to the underside of the strap for s in (-1, 1): run = box(min(s*G.SLIT_X, s*(G.FRAME_W/2+23)), G.SLIT_Y0, max(s*G.SLIT_X, s*(G.FRAME_W/2+23)), G.SLIT_Y1) add(f'STRAP-front-{side[s]}', 'STRAP', 'strap', 'front', 'none', run, run, 12, s*G.SLIT_X, (G.SLIT_Y0+G.SLIT_Y1)/2, count=1 if s < 0 else 0) # one strap, two ends add(f'PATCH-{side[s]}', 'PATCH', 'strap', 'back', 'contact', G._common()['patches'][(s+1)//2], G._common()['patches'][(s+1)//2], 1, s*G.SLIT_X, (G.SLIT_Y0+G.SLIT_Y1)/2) # --- brow strip and knot sp = G.place(G.strip_poly(), 0, G.STRIP_Y) add('STRIP', 'STRIP', 'strip', 'front', 'contact', sp, sp, 20, 0, G.STRIP_Y) kn = Point(*G.KNOT_C).buffer(G.KNOT_R, 48) add('KNOT', 'KNOT', 'knot', 'front', 'contact', kn, kn.intersection(G.base_glasses()['outline']), 21, *G.KNOT_C, onto=('L-front-1', 'L-front-2', 'L-front-3')) # --- crown, large leaves. As built: the four outer ones on the BACK of the band, the three centre ones on the FRONT # (their stems end up under the knot). for j, (x, ang, drop) in enumerate([(-36, -22, 2), (36, 22, 2), (-52, -30, 12), (52, 30, 12)]): y = G.band_top(x) - drop; v = (j // 2) % 2 add(f'L-back-{j+1}', 'L', 'back', 'back', 'hot', G.place(G.oak('L', v), x, y, ang, flip=x < 0), G.place(G.stem_tab('L'), x, y, ang), 2 + j // 2, x, y, ang=ang, flip=x < 0, variant=v) for j, (x, ang, drop) in enumerate([(0, 0, 2), (-17, -10, 8), (17, 10, 8)]): y = G.band_top(x) - drop; v = 0 if j == 0 else 1 add(f'L-front-{j+1}', 'L', 'centre', 'front', 'hot', G.place(G.oak('L', v), x, y, ang, flip=x < 0), G.place(G.stem_tab('L'), x, y, ang), 6 + (j == 0), x, y, ang=ang, flip=x < 0, variant=v) # --- twigs: printed flat-backed, so they go on the FRONT. Thick end on the front of the band near its top edge, # the rest lying up the front faces of the back-row leaves. The front row later hides the thick ends. for s in (-1, 1): x, y, ang = s*30, TOP + 29, 2 paths = [G.place(l, x, y, s*ang, flip=s < 0) for l in G.twig_paths()] tw = unary_union(paths).buffer(1.6) foot = LineString([paths[0].interpolate(d) for d in (1, 8, 16, 22)]).buffer(3.2) # the 22 mm that lies on the band add(f'TWIG-{side[s]}', 'TWIG', 'twigs', 'front', 'contact', tw, foot, 10, x, y, ang=s*ang, flip=s < 0) # --- acorn clusters at the strap exits: 2 large + 1 small per side, stacked on each other for s in (-1, 1): zone = box(min(s*70, s*100), TOP+4, max(s*70, s*100), TOP+47) acs = [('AL', 86, TOP+23, 15), ('AL', 80, TOP+41, -35), ('AS', 93, TOP+35, 50)] for k, (kind, ax, ay, rot) in enumerate(acs): shp = G.place(G.acorn_poly(kind), s*ax, ay, s*rot) add(f'{kind}-cluster-{side[s]}{k+1}', kind, 'clusters', 'front', 'contact', shp, shp.intersection(zone) if k == 0 else Point(s*ax, ay).buffer(3), 22+k, s*ax, ay, ang=s*rot, onto=(f'STRAP-front-{side[s]}',) + tuple(f'{a[0]}-cluster-{side[s]}{m+1}' for m, a in enumerate(acs[:k]))) # --- crown front row: 6 medium leaves on the FRONT of the band, stems just above the strip for k, (x0, y0, ang) in enumerate([(24, TOP+41, 12), (42, TOP+39, 27), (58, TOP+37, 42)]): for s in (-1, 1): add(f'M-front-{side[s]}{k+1}', 'M', 'front', 'front', 'hot', G.place(G.oak('M', k % 2), s*x0, y0, s*ang, flip=s < 0), G.place(G.stem_tab('M'), s*x0, y0, s*ang), 30+k, s*x0, y0, note=f'{ang} deg out', ang=s*ang, flip=s < 0, variant=k % 2) # --- hanging acorns: wire glued to the top of the cheek tab, later covered by the medium cheek leaf stem for s in (-1, 1): anchor = Point(s*88, 13).buffer(3) add(f'WIRE-{side[s]}', 'WIRE', 'hang', 'front', 'hot', anchor, anchor, 25, s*88, 13) add(f'AF-hang-{side[s]}', 'AF', 'hang', 'hangs', 'wire', G.place(G.acorn_poly('AF'), s*108, -16), None, 45, s*108, -16, onto=(f'WIRE-{side[s]}',)) # --- tendrils: two small spirals per side on the lower cheek tab, each with a tip leaf glued to its curl for s in (-1, 1): for k, (x0, y0, ang, fl) in enumerate([(73, -6, 20, True), (89, -4, -22, False)]): # side by side, curling away from each other line = G.place(G.spiral_path(), s*x0, y0, 180 + s*ang, flip=(s < 0) != fl) add(f'SPIRAL-{side[s]}{k+1}', 'SPIRAL', 'tendrils', 'front', 'contact', line.buffer(2.2), line.interpolate(6).buffer(4), 26+k, s*x0, y0, ang=180 + s*ang, flip=(s < 0) != fl) ex, ey = line.coords[-1] add(f'T-{side[s]}{k+1}', 'T', 'tendrils', 'onpart', 'hot', G.place(G.oak('T', k), ex, ey, 180 + s*(8 if k else -12)), None, 44, ex, ey, onto=(f'SPIRAL-{side[s]}{k+1}',), ang=180 + s*(8 if k else -12), variant=k) # --- cheek leaves: 1 medium + 2 small per side, tips down, stems just below the line the glasses arms rest on for s in (-1, 1): for k, (size, x0, y0, th) in enumerate([('M', 85, 6, 0), ('S', 76, 7, -3), ('S', 92.5, 7, -8)]): add(f'{size}-cheek-{side[s]}{k+1}', size, 'cheeks', 'front', 'hot', G.place(G.oak(size, k % 2), s*x0, y0, 180 + s*th, flip=s < 0), G.place(G.stem_tab(size), s*x0, y0, 180 + s*th), 40+k, s*x0, y0, onto=(f'WIRE-{side[s]}',) if (size == 'M' or k == 2) else (), ang=180 + s*th, flip=s < 0, variant=k % 2) # --- nose leaf add('S-nose', 'S', 'nose', 'front', 'hot', G.place(G.oak('S', 1), 0, TOP-8, 180), G.place(G.stem_tab('S'), 0, TOP-8, 180), 46, 0, TOP-8, ang=180, variant=1) order = list(STEPS) P.sort(key=lambda p: (order.index(p['step']),)) # stable: keeps authoring order inside a step for i, p in enumerate(P): p['seq'] = i return P PLACEMENTS = build_placements() def counts(): """Number of each part actually placed.""" c = Counter() for p in PLACEMENTS: c[p['part']] += p.get('count', 1) return c def make_list(): """What to cut / print / buy = placed + declared spares. Nothing else.""" c = counts(); out = OrderedDict() for pid, (name, src, kind) in PARTS.items(): n = (1 if pid == 'BASE' else c.get(pid, 0)); sp = SPARES.get(pid, 0) out[pid] = dict(name=name, src=src, kind=kind, placed=n, spare=sp, total=n+sp) return out def step_placements(step): return [p for p in PLACEMENTS if p['step'] == step] def validate(verbose=True): errs, notes = [], [] base = G.base_glasses(); outline = base['outline']; order = list(STEPS) # 1. every part is placed; every placement is a known part for pid, row in make_list().items(): if row['placed'] == 0: errs.append(f'{pid} is in the parts list but never placed') for p in PLACEMENTS: if p['part'] not in PARTS: errs.append(f"{p['id']} uses unknown part {p['part']}") # 2. step dependencies are satisfied by the written order for i, (k, st) in enumerate(STEPS.items()): for dep, why in st['after']: if order.index(dep) >= i: errs.append(f'step {k} is listed before {dep} but must follow it ({why})') # 3. glue footprints on the same face never collide unless one is declared glued onto the other glued = [p for p in PLACEMENTS if p['footprint'] is not None and p['face'] in ('front', 'back')] for i, a in enumerate(glued): for b in glued[i+1:]: if a['face'] != b['face']: continue ov = a['footprint'].intersection(b['footprint']).area if ov > 1.0 and a['id'] not in b['onto'] and b['id'] not in a['onto']: errs.append(f"glue areas collide on the {a['face']}: {a['id']} and {b['id']} ({ov:.0f} mm2)") # 4. nothing is sequenced after a part that already covers its glue area (same face) for a in glued: for b in PLACEMENTS: if b is a or b['face'] != a['face'] or b['shape'] is None: continue if b['seq'] < a['seq'] and b['shape'].intersection(a['footprint']).area > 1.0 and b['id'] not in a['onto']: errs.append(f"{a['id']} is glued after {b['id']}, which already covers its glue area") # 5. glue areas sit on foam: leaf stems fully, rigid printed parts at least 55% for p in glued: if p['part'] in ('STRAP',): continue ground = unary_union([outline] + [q['shape'] for q in PLACEMENTS if q['id'] in p['onto']]) frac = p['footprint'].intersection(ground).area / max(p['footprint'].area, 1e-6) need = 0.97 if p['part'] in ('L', 'M', 'S', 'PATCH', 'WIRE', 'SPIRAL', 'TWIG', 'STRIP') else 0.55 if frac < need: errs.append(f"{p['id']}: only {frac:.0%} of its glue area is on the base (needs {need:.0%})") # 6. glasses keep-outs on the front: nothing in the arm zones; only the nose leaf under the frame for p in glued: if p['face'] != 'front' or p['part'] == 'STRAP': continue for z in G.arm_zones(): if p['footprint'].intersection(z).area > 0.5: errs.append(f"{p['id']} is glued where a glasses arm rests") if p['footprint'].intersection(G.frame_zone()).area > 0.5 and p['id'] != 'S-nose': errs.append(f"{p['id']} is glued under the glasses frame") for p in PLACEMENTS: if p['shape'] is None or p['face'] == 'back' or p['id'] == 'S-nose': continue for sg in (-1, 1): if p['shape'].intersection(G.lens(sg).buffer(-4)).area > 2: errs.append(f"{p['id']} hangs over the glass of a lens") rigid = [p for p in PLACEMENTS if p['part'] in ('STRIP', 'KNOT', 'AL', 'AS', 'SPIRAL') and p['shape'] is not None] for i, a in enumerate(rigid): for b in rigid[i+1:]: ov = a['shape'].intersection(b['shape']).area if ov > 3 and a['id'] not in b['onto'] and b['id'] not in a['onto']: errs.append(f"rigid parts overlap: {a['id']} and {b['id']} ({ov:.0f} mm2); they cannot both sit flat") # 7. on the back: leaf stems and twigs stay clear of the strap patches (the tail of the elastic lives there) for p in glued: if p['face'] == 'back' and p['part'] != 'PATCH': for pt in base['patches']: if p['footprint'].intersection(pt).area > 0.5: errs.append(f"{p['id']} is glued onto a strap patch") # 8. base template is one piece with no thin necks, slits sit well inside foam and inside their patches for b in (G.base_glasses(), G.base_plain()): o = b['outline'] if o.geom_type != 'Polygon' or not o.is_valid: errs.append(f"{b['kind']} base is not one valid piece") if o.buffer(-4).geom_type != 'Polygon': errs.append(f"{b['kind']} base has a neck narrower than 8 mm") for sl, pt in zip(b['slits'], b['patches']): cx, cy = sl.centroid.x, sl.centroid.y; sgn = 1 if cx > 0 else -1 ray = box(min(cx, cx+sgn*60), cy-0.5, max(cx, cx+sgn*60), cy+0.5).intersection(o) outward = ray.bounds[2]-ray.bounds[0] - SLIT_HALF if outward < 15: errs.append(f"{b['kind']} base: only {outward:.0f} mm of foam outboard of the strap slit") if sl.distance(o.exterior) < 8: errs.append(f"{b['kind']} base: strap slit is {sl.distance(o.exterior):.0f} mm from an edge") if not pt.buffer(-4).contains(sl): errs.append(f"{b['kind']} base: patch does not cover its slit with 4 mm to spare") for h in b['holes']: d = h.distance(o.exterior) if not o.interiors else min(h.exterior.distance(o.exterior), 99) if d < 10: errs.append(f"{b['kind']} base: eye hole is {d:.0f} mm from the outer edge") notes.append(f"{b['kind']} base: {o.bounds[2]-o.bounds[0]:.0f} x {o.bounds[3]-o.bounds[1]:.0f} mm, one piece, min neck >= 8 mm") ml = make_list() notes.append('leaves to cut: ' + ', '.join(f"{ml[k]['total']} {k} ({ml[k]['placed']} placed + {ml[k]['spare']} spare)" for k in 'LMST')) notes.append('to print: ' + ', '.join(f"{v['total']} x {k}" for k, v in ml.items() if v['kind'] == 'print') + ', 1 x STAMP (tool)') if verbose: for n in notes: print(' ok ', n) for e in errs: print(' FAIL', e) print(f'{len(PLACEMENTS)} placements, {len(STEPS)} steps, {len(errs)} problems') return errs if __name__ == '__main__': import sys; sys.exit(1 if validate() else 0)