Talk to Claude Code and Codex agents from an Apple Watch and an iPhone. Each agent is a character on the watch.
"""Exports Noodle's pose library to app/ChumsWatch/Rig/NoodleRest.swift: the colour model fitted to the reference, the
tube's length and radius, and every pose as 64 points evenly along the tube (tail to head), each checked to keep its
parts apart. Run: python3 export.py"""
import numpy as np, re, base64, struct, json
import tube3d as T, rod
from zigzag3d import make as make_zigzag
ZIG,_=make_zigzag(kmax=0.6)
T.L=T.length(ZIG) # the tube is as long as the rest pose needs to make its bends round
import tie2
tie2.seg=T.L/(tie2.n-1)
from tie2 import bake
R=T.R; L=T.L; NN=64
def bisect_turns(make, lo=1.0, hi=5.0):
for _ in range(50):
mid=(lo+hi)/2
if T.length(make(mid))<L: lo=mid
else: hi=mid
return make(hi)
def helix(r0,r1,pitch,turns,tilt=0.0,phase=0.0):
t=np.linspace(0,turns*2*np.pi,1600);k=t/t[-1];r=r0+(r1-r0)*k
P=np.stack([r*np.cos(t+phase),-(k*turns*pitch),r*np.sin(t+phase)],1)
if tilt:
c,s=np.cos(tilt),np.sin(tilt);P=P@np.array([[1,0,0],[0,c,-s],[0,s,c]]).T
return P
def at64(P):
Q,_=T.resample(P,NN);return Q
SEG=L/(NN-1)
TMAX=0.78*SEG/R # the most a joint may turn: no bend tighter than about 1.3 tube radii
def turns(P):
d=np.diff(P,axis=0);d/=np.linalg.norm(d,axis=1,keepdims=True)
return np.arccos(np.clip((d[:-1]*d[1:]).sum(1),-1,1))
def roundbends(P):
"""Smooth along the tube just enough that no bend is tighter than TMAX allows, then restore the length."""
from scipy.ndimage import gaussian_filter1d
D,_=T.resample(P,400)
best=None
for sig in np.arange(0.0,14.1,0.5):
if sig>0:
k=int(4*sig)+2
t0=D[1]-D[0];t1=D[-1]-D[-2]
pad=np.vstack([D[0]-t0*np.arange(k,0,-1)[:,None],D,D[-1]+t1*np.arange(1,k+1)[:,None]])
Q=gaussian_filter1d(pad,sig,axis=0)[k:-k]
else: Q=D.copy()
c=Q.mean(0);Q=(Q-c)*(L/T.length(Q))+c
Q64=at64(Q)
m=turns(Q64).max()
if best is None or m<best[0]: best=(m,Q64)
if m<=TMAX: break
return best[1]
G=70.0
def ground(P):
P=P.copy();P[:,1]+=G-P[:,1].max();c=P.mean(0);P[:,0]-=c[0];P[:,2]-=c[2];return P
# zigzag: the traced reference centreline, later folds in front (an 80 pt ramp)
zig=ZIG
poses={
'zigzag':zig,
'tornado':bisect_turns(lambda tr:helix(20,46,38,tr)),
'sleep':bisect_turns(lambda tr:helix(52,20,2*R*1.02,tr,tilt=-0.42,phase=2.2)),
'spring':bisect_turns(lambda tr:helix(26,26,58,tr)),
'messy':np.load('messy.npy'),
}
out={}
radii=np.full(NN,R)
for k,P in poses.items():
Q=ground(roundbends(P*(L/T.length(P))))
out[k]=Q
print('%-8s len %.1f clearance %.2f extent %s max R*k %.2f'%(k,T.length(Q),rod.min_sep(Q,radii,4.5*R),np.ptp(Q,axis=0).round(0),turns(Q).max()/SEG*R))
keys,ms=bake(poses['tornado']*(L/T.length(poses['tornado'])))
print('tie bake: %d keys, sim clearance %.2f'%(len(keys),ms))
# the tie keys start from the tornado as grounded/centred above: same transform
t64=at64(poses['tornado']*(L/T.length(poses['tornado'])))
off=out['tornado']-t64 # a pure translation
tie=np.array([roundbends(at64(k))+off[0] for k in keys])
print('tie keys: min clearance %.2f, max R*k %.2f'%(min(rod.min_sep(k,radii,4.5*R) for k in tie),max(turns(k).max() for k in tie)/SEG*R))
out['knot']=tie[-1]
src=open('CoilRest-fit.swift').read()
def line(name): return [l for l in src.split('\n') if l.strip().startswith('static let %s:'%name)][0]
def pts(P): return '['+', '.join('SIMD3(%.2f, %.2f, %.2f)'%tuple(p) for p in P)+']'
blob=base64.b64encode(np.asarray(tie,np.float32).tobytes()).decode()
lines=['import simd','','// Generated by design/characters/coil3d/export.py; do not edit by hand.','// The colour model fitted to the reference (albedo along the tube × a light of where the surface faces, plus a sheen),',
'// and the tube\'s poses: each 64 points evenly along it, tail to head, in pt (y down, z toward you), checked so no two',
'// parts come closer than the tube\'s width.','','enum NoodleRest {',
' static let length = %.3f, radius = %.3f'%(L,R),
line('albedo'),line('light'),line('sheen')]
for k in ['zigzag','tornado','sleep','spring','messy','knot']:
lines.append(' static let %s: [SIMD3<Double>] = %s'%(k,pts(out[k])))
lines.append(' /// Tying the knot from the tornado: %d keys of 64 points (Float32, little-endian), baked offline with collisions.'%len(tie))
lines.append(' static let tie = "%s"'%blob)
lines.append('}')
open('../../../app/ChumsWatch/Rig/NoodleRest.swift','w').write('\n'.join(lines)+'\n')
np.savez('poses64.npz',**out,tie=tie)
print('written')