"""Cirrus SF50 Vision Jet silhouettes, built from smooth Beziers (Hobby splines + filleted polygons).
Units = metres. Plan view: nose at (0,0) pointing up (-y), tail at y=+9.42, span 11.68 (x = +-5.84).
Side view: nose at (0,0) pointing right (+x), z up is -y."""
import math, numpy as np, skia
from hobby import piece, path as hpath
U=skia.PathOp.kUnion_PathOp; D=skia.PathOp.kDifference_PathOp; I=skia.PathOp.kIntersect_PathOp
L_REAL=9.42; SPAN=11.68
def op(a,b,o): return skia.Op(a,b,o)
def segs_path(segs, close=True, p=None):
    p = p or skia.Path(); first=True
    for (a,b,c,e) in segs:
        if first: p.moveTo(*a); first=False
        p.cubicTo(*b,*c,*e)
    if close: p.close()
    return p
def mirror_x(segs):
    return [tuple((-q[0],q[1]) for q in s[::-1]) for s in segs[::-1]]
def rounded_poly(pts, radii):
    """closed polygon with per-corner fillet radii (cubic approx of circular arc)."""
    n=len(pts); P=[np.array(p,float) for p in pts]; p=skia.Path()
    def corner(i):
        a,b,c=P[i-1],P[i],P[(i+1)%n]; r=radii[i]
        u=(a-b)/np.linalg.norm(a-b); v=(c-b)/np.linalg.norm(c-b)
        th=math.acos(max(-1,min(1,float(u@v))))
        if r<=0 or th<1e-3: return b,b,b,b
        t=r/math.tan(th/2); t=min(t,0.45*np.linalg.norm(a-b),0.45*np.linalg.norm(c-b))
        s=b+u*t; e=b+v*t; k=4/3*math.tan((math.pi-th)/4)*(t*math.tan(th/2))
        return s, s+(-u)*k, e+(-v)*k, e
    cs=[corner(i) for i in range(n)]
    p.moveTo(*cs[0][3])
    for i in range(1,n+1):
        s,c1,c2,e=cs[i%n]; p.lineTo(*s); p.cubicTo(*c1,*c2,*e)
    p.close(); return p
def ring(path, w):
    out=skia.Path()
    skia.Paint(Style=skia.Paint.kStroke_Style, StrokeWidth=w, StrokeJoin=skia.Paint.kRound_Join).getFillPath(path,out,None,0.02)
    return out
def ellipse(cx,cy,rx,ry):
    p=skia.Path(); p.addOval(skia.Rect(cx-rx,cy-ry,cx+rx,cy+ry)); return p

# ---------------- PLAN VIEW ----------------
def plan_parts():
    # fuselage right half, nose -> tail (Hobby through measured stations; max ext. width ~1.72 m at cabin)
    half = piece([(0,0),(0.17,0.15),(0.32,0.50),(0.48,1.0),(0.63,1.6),(0.76,2.3),(0.84,3.0),(0.86,3.7),(0.80,4.6),
                  (0.66,5.5),(0.48,6.4),(0.32,7.3),(0.22,8.2),(0.16,9.0),(0.12,9.42)],
                 d0=(1,0), d1=(0,1))
    fus=skia.Path(); first=half[0][0]
    segs = half + [((0.12,9.42),(0.08,9.47),(-0.08,9.47),(-0.12,9.42))] + mirror_x(half)
    fus = segs_path(segs)
    # wing (right): straight tapered, LE sweep ~7 deg, raked rounded tip
    wingR = rounded_poly([(0.2,3.70),(5.70,4.46),(5.86,4.66),(5.82,5.44),(0.2,5.90)],[0,0.24,0.12,0.10,0])
    wingL = rounded_poly([(-0.2,3.70),(-0.2,5.90),(-5.82,5.44),(-5.86,4.66),(-5.70,4.46)],[0,0,0.10,0.12,0.24])
    # V-tail surfaces projected in plan (~42 deg dihedral): LE sweep ~35 deg
    tailR = rounded_poly([(0.05,7.80),(1.92,8.86),(1.98,8.98),(1.96,9.62),(0.05,9.28)],[0,0.10,0.05,0.07,0])
    tailL = rounded_poly([(-0.05,7.80),(-0.05,9.28),(-1.96,9.62),(-1.98,8.98),(-1.92,8.86)],[0,0,0.07,0.05,0.10])
    # dorsal nacelle (FJ33) on the rear fuselage, intake at ~51% length
    nh = piece([(0,4.86),(0.31,4.92),(0.39,5.40),(0.38,6.15),(0.30,6.90),(0.17,7.45),(0.0,7.55)], d0=(1,0), d1=(-1,0))
    nac = segs_path(nh + mirror_x(nh))
    return dict(fus=fus, wings=(wingL,wingR), tails=(tailL,tailR), nac=nac)

def plan(detail='full', cut=0.10):
    """detail: 'full' | 'clean' | 'solid'. cut = negative-space hairline width in metres."""
    P=plan_parts(); fus=P['fus']
    body=skia.Path(fus)
    for w in P['wings']+P['tails']: body=op(body,w,U)
    body=op(body,P['nac'],U)
    if detail=='solid': return body
    if detail=='mini':
        body=op(body, ring(P['nac'],cut*1.3), D); return body
    # hairline wing roots: separate wings from fuselage
    rootcut = op(ring(fus,cut), op(op(P['wings'][0],P['wings'][1],U), op(P['tails'][0],P['tails'][1],U),U), I)
    body=op(body,rootcut,D)
    # nacelle keyline + intake crescent
    body=op(body, ring(P['nac'],cut), D)
    intake = op(ellipse(0,5.00,0.27,0.10), ellipse(0,5.06,0.25,0.07), D)
    body=op(body, intake, D)
    if detail=='full':
        # raked windshield as a fine arc band
        wsh = skia.Path(); wsh.moveTo(-0.70,1.78); wsh.cubicTo(-0.35,1.30,0.35,1.30,0.70,1.78)
        body=op(body, ring(wsh,cut*0.9), D)
    return body

# ---------------- SIDE VIEW ----------------
def side(detail='full', cut=0.085):
    """profile traced from a level side photo of N1808C (110.9 px/m), z measured from the belly.
    nose at x=0; place() mirrors it so it flies to the right."""
    up=lambda pts:[(x,-z) for x,z in pts]
    top = piece(up([(0,0.38),(0.12,0.53),(0.50,0.66),(0.95,0.79),(1.40,0.96),(1.85,1.26),(2.30,1.44),(2.75,1.57),(3.40,1.625),
                    (4.10,1.58),(4.55,1.49),(5.00,1.37),(5.46,1.23),(5.91,1.13),(6.81,0.99),(7.71,0.91),(9.42,0.88)]),
                d0=(0.06,-1), d1=(1,0.0))
    bot = piece(up([(9.42,0.64),(8.61,0.58),(7.71,0.50),(6.81,0.40),(5.91,0.27),(5.00,0.13),(4.10,0.04),(3.20,0.0),
                    (2.30,0.0),(1.40,0.05),(0.50,0.14),(0.14,0.22),(0,0.38)]), d0=(-1,-0.07), d1=(-0.06,-1))
    tailcap=[((9.42,-0.88),(9.47,-0.84),(9.47,-0.68),(9.42,-0.64))]
    fus=segs_path(top+tailcap+bot)
    # FJ33 nacelle riding on the crown: blunt intake, long pointed tail cone, pylon fairing beneath
    nt=piece(up([(4.91,1.49),(5.00,1.70),(5.18,1.76),(5.91,1.74),(6.36,1.70),(6.81,1.62),(7.20,1.46)]), d0=(0,-1), d1=(1,0.55))
    nb=piece(up([(7.20,1.46),(6.81,1.33),(5.91,1.25),(5.10,1.27),(4.91,1.49)]), d0=(-1,0.25), d1=(0,-1))
    nac=segs_path(nt+nb)
    pylon=rounded_poly(up([(5.2,1.30),(6.95,1.34),(7.40,0.86),(5.2,0.9)]),[0,0.12,0,0])
    fin_near=rounded_poly(up([(7.93,0.90),(8.67,2.57),(8.84,2.66),(9.57,2.66),(9.20,0.86)]),[0,0.10,0.04,0.08,0])
    fin_far=rounded_poly(up([(7.70,0.92),(8.44,2.62),(8.61,2.71),(9.34,2.71),(9.00,0.92)]),[0,0.10,0.04,0.08,0])
    body=op(fus,nac,U); body=op(body,pylon,U); body=op(body,fin_near,U)
    if detail=='solid': return op(body,fin_far,U)
    body=op(body, op(fin_far, op(fin_near, ring(fin_near,cut*2),U), D), U)   # far ruddervator peeks ahead: the V
    body=op(body, op(ring(nac,cut), op(fus,pylon,U), I), D)                   # nacelle keyline where the pod sits on the crown
    body=op(body, ellipse(4.955,-1.49,0.035,0.17), D)                        # intake face
    if detail=='full':
        w=skia.Path(); w.moveTo(1.20,-0.72); w.cubicTo(1.60,-0.82,2.05,-1.05,2.50,-1.30)
        body=op(body, ring(w,cut*0.9), D)                                    # windshield sweep
    return body

def place(path, cx, cy, length, heading_deg, pivot=(0,4.71), view='plan'):
    """plan: heading = direction of nose, degrees clockwise from straight up.
       side: heading = climb angle degrees (nose up positive), flying to the right. pivot in metres."""
    q=skia.Path(path); m=skia.Matrix(); sc=length/L_REAL
    m.setTranslate(cx,cy)
    if view=='plan': m.preRotate(heading_deg); m.preScale(sc,sc)
    else: m.preRotate(-heading_deg); m.preScale(-sc,sc)   # side: nose to the right, climb = nose up
    m.preTranslate(-pivot[0],-pivot[1])
    q.transform(m); return q
