"""Reproducible coordinate sample and exact comparison diagram. CC0-1.0."""
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
W=Path(__file__).resolve().parents[1]/'assets'
plt.rcParams.update({'font.family':'DejaVu Sans','font.size':12,'svg.hashsalt':'CG-S6-sphere-twists','mathtext.fontset':'dejavusans'})
navy='#15354a';teal='#087e82';orange='#a75a22'
fig=plt.figure(figsize=(13,8),facecolor='#faf9f5')
fig.text(.04,.94,'The full tube twist and its signed fibre degree',fontsize=20,color=navy,weight='bold')
fig.text(.04,.89,r'Numerical sample of (1.5), with retained auxiliary values $a=1/4$, $b=3/4$, $R=1$.',fontsize=12,color=navy)
r=np.linspace(0,1,1001)
def eta(t):
    out=np.zeros_like(t);mask=t>0;out[mask]=np.exp(-1/t[mask]);return out
theta=np.pi*eta(.75-r)/(eta(.75-r)+eta(r-.25))
ax=fig.add_axes([.08,.42,.37,.36])
ax.plot(r,theta/np.pi,color=teal,lw=2.5)
ax.axvline(.25,color=orange,ls='--',lw=1);ax.axvline(.75,color=orange,ls='--',lw=1)
ax.set_xticks([0,.25,.75,1],['0','a = 1/4','b = 3/4','R = 1'])
ax.set_xlabel('Original fibre radius r');ax.set_ylabel(r'$\theta(r)/\pi$');ax.set_ylim(-.06,1.06)
ax.grid(alpha=.15);ax.set_title('Smooth cutoff; strict decrease on (a,b)',fontsize=12,color=navy)
ax2=fig.add_axes([.58,.40,.32,.39]);ax2.set_aspect('equal')
th=np.linspace(0,np.pi,501);ax2.plot(np.cos(th),np.sin(th),color=teal,lw=2.5)
for v in [2.4,1.4,.5]:
    ax2.annotate('',xy=(np.cos(v-.15),np.sin(v-.15)),xytext=(np.cos(v+.15),np.sin(v+.15)),arrowprops={'arrowstyle':'->','color':teal,'lw':2})
ax2.scatter([-1,1],[0,0],c=[orange,orange],s=35,zorder=3)
ax2.text(-1,.12,r'$r\leq a$',ha='center',color=orange)
ax2.text(1,.12,r'$r\geq b$',ha='center',color=orange)
ax2.set_xlabel(r'Real coordinate $\cos\theta(r)$');ax2.set_ylabel(r'Coefficient of fixed $u$: $\sin\theta(r)$')
ax2.set_xlim(-1.25,1.25);ax2.set_ylim(-.12,1.2)
ax2.set_title(r'One meridian: $q_0=1$, fixed $u\in S^2$',fontsize=12,color=navy)
fig.text(.06,.29,r'$z=ru\ \mapsto\ \cos\theta(r)\,1+\sin\theta(r)\,u$',fontsize=17,color=navy)
fig.text(.06,.22,r'Full source orientation: $r^2\,dr\wedge\omega_{S^2}$; target: $\sin^2\theta\,d\theta\wedge\omega_{S^2}$.',fontsize=14,color=navy)
fig.text(.06,.15,r'$\theta^\prime(r)<0$ gives degree $-1$:  $\mathcal{F}_*x=x-\lambda(v,x)v$; inverse twist adds the term.',fontsize=14,color=navy)
fig.text(.06,.065,'The right plot is a fixed-direction meridian, not the whole three-sphere or the ambient six-manifold.\nExact proof: Sections 1–2. Human model: Paul Seidel, math/0105186v2, original TeX 663–730.',fontsize=11,color=navy)
fig.savefig(W/'sphere-twist-fibre.svg',metadata={'Date':None})
plt.close(fig)
fig=plt.figure(figsize=(13,10),facecolor='#faf9f5');ax=fig.add_axes([0,0,1,1]);ax.set_xlim(0,13);ax.set_ylim(0,10);ax.axis('off')
def text(x,y,t,size=13,**kw):ax.text(x,y,t,fontsize=size,color=navy,va='top',**kw)
def box(x,y,w,h):ax.add_patch(FancyBboxPatch((x,y),w,h,boxstyle='round,pad=.12',ec='#b8c9cf',fc='white'))
def arrow(x,y,X,Y):ax.annotate('',xy=(X,Y),xytext=(x,y),arrowprops={'arrowstyle':'->','color':teal,'lw':2})
text(.4,9.7,'The original outer boundaries, a changed interior gluing',20,weight='bold')
text(.4,9.16,'Schematic of exact maps (4.3)–(7.5); boxes stand for seven-dimensional pieces.',12)
box(.5,6.75,5.25,1.75);box(7.25,6.75,5.25,1.75)
text(.8,8.26,r'$C_-:\ S^6\longrightarrow P$',18);text(.8,7.7,r'$r$ lower 3-handles; belts $b_i$',14)
text(.8,7.23,r'$B=\mathrm{span}(b_i)$, primitive Lagrangian',13)
text(7.55,8.26,r'$C_+:\ P\longrightarrow\Sigma$',18);text(7.55,7.7,r'$r$ upper 4-handles; classes $a_j$',14)
text(7.55,7.23,r'$L=\mathrm{span}(a_j)$, primitive Lagrangian',13)
arrow(7.1,7.55,5.9,7.55);text(6.5,8.15,r'$\phi$',19,ha='center')
text(.65,6.3,r'Glue $i_+(p)$ to $i_-(\phi(p))$:   $\phi_*L=\mathrm{span}(f_i)$,  $\lambda(b_i,f_j)=\delta_{ij}$.',16)
box(.5,3.85,12,1.85)
text(.8,5.48,'Keep both original comparisons',16,weight='bold')
text(.8,4.94,r'$D^{\rm old}_{ij}=\sigma_i\lambda(b_i,a_j)$;   $D^\phi_{ij}=\sigma_i\lambda(b_i,\phi_*a_j)=U_{ij}$.',16)
text(.8,4.40,r'$A_\phi(p)=\tau_{-,\phi(p)}(D\phi_p\oplus1\oplus I_h)\tau_{+,p}^{-1}$.',17)
arrow(6.5,3.63,6.5,3.14)
text(.7,2.93,r'Framed handle slides: $UU^{-1}=I$  →  supported Whitney moves  →  actual Morse cancellation',13)
box(.5,.83,12,1.53)
text(.8,2.13,r'$\Psi:S^6\times[0,T]\longrightarrow C_\phi$',18)
text(.8,1.60,r'$d(q)=\iota_\Sigma^{-1}\Psi(q,T)$ is a diffeomorphism;  $d\simeq g$ and $d^{-1}\simeq h$.',16)
text(.5,.43,'Old torsion remains recorded in D old. The new filling need not be diffeomorphic to the old one.\nThe frame defect is retained; cancellation uses the full oriented attachments. Proof: Sections 4–7.',11)
fig.savefig(W/'sphere-twist-gluing.svg',metadata={'Date':None})
plt.close(fig)
print('sphere-twist-fibre.svg and sphere-twist-gluing.svg')
