"""Figure for patched-conics.qmd, Example 4 (img/fig-pc-departure-tilt.png): why turning the plane in the
departure burn moves the asymptote by much less than the tilt.

The burn can only turn the orbit plane about the radius of the burn point B. The outgoing asymptote is
f_inf = 150.8 deg round from that radius, so as the plane turns by alpha it slides round a cone of half-angle
f_inf about OB, and leaves the old plane by psi with sin(psi) = sin(f_inf) sin(alpha). For psi = 9.72 deg the
tilt must be alpha = 20.3 deg.

Run from the sfm-slides directory:   python3 codes/fig-pc-departure-tilt.py
"""
import numpy as np
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE

finf = np.radians(150.8)
psi = np.radians(9.72)
alpha = np.arcsin(np.sin(psi) / np.sin(finf))
print(f"alpha = {np.degrees(alpha):.2f} deg")


def Rx(a):
    return np.array([[1, 0, 0], [0, np.cos(a), -np.sin(a)], [0, np.sin(a), np.cos(a)]])


t = np.linspace(0, 2 * np.pi, 300)
fig = plt.figure(figsize=(9.5, 7.2))
ax = fig.add_subplot(111, projection="3d")
# the two planes, as discs of radius 1.2
for M, col, lab in ((np.eye(3), BLUE, "parking-orbit plane"), (Rx(alpha), MAROON, "plane after the tilt")):
    ring = M @ np.array([1.2 * np.cos(t), 1.2 * np.sin(t), 0 * t])
    ax.plot(*ring, color=col, lw=1.6, label=lab)
    xs, ys = np.meshgrid(np.linspace(-1.2, 1.2, 2), np.linspace(-1.2, 1.2, 2))
# the burn radius, the axis of the tilt
ax.quiver(0, 0, 0, 1.35, 0, 0, color=INK, lw=2.2, arrow_length_ratio=0.07)
ax.text(0.75, -0.05, -0.25, "B: the burn point;\nthe plane turns about OB", color=INK, fontsize=10)
# the cone traced by the asymptote as the plane turns
cone = np.array([np.cos(finf) * np.ones_like(t), np.sin(finf) * np.cos(t), np.sin(finf) * np.sin(t)])
ax.plot(*cone, color="#9a9388", lw=1.0, ls=":")
A1 = np.array([np.cos(finf), np.sin(finf), 0])
A2 = Rx(alpha) @ A1
for A, col, lab, off in ((A1, BLUE, "asymptote before", np.array([0, 0.05, -0.16])),
                         (A2, MAROON, "asymptote after", np.array([0, 0, 0.12]))):
    ax.quiver(0, 0, 0, *(1.1 * A), color=col, lw=2.4, arrow_length_ratio=0.1)
    ax.text(*(1.12 * A + off), lab, color=col, fontsize=10.5, ha="right")
# angle psi: from A2 down to the parking plane
foot = A2.copy(); foot[2] = 0; foot /= np.linalg.norm(foot)
arc = np.array([np.cos(s) * foot + np.sin(s) * np.array([0, 0, 1]) for s in np.linspace(0, psi, 30)])
ax.plot(*(0.75 * arc).T, color=GREEN, lw=2)
ax.text(*(0.75 * arc[0] + np.array([0.12, -0.05, -0.02])), r"$\psi=9.72^\circ$", color=GREEN, fontsize=11)
# angle alpha between the planes, at the far edge (along +y)
arc2 = np.array([Rx(s) @ np.array([0, 1.2, 0]) for s in np.linspace(0, alpha, 30)])
ax.plot(*arc2.T, color=AMBER, lw=2)
ax.text(*(arc2[-1] + np.array([0, 0.05, 0.08])), r"$\alpha=20.3^\circ$", color=AMBER, fontsize=11)
# f_inf from OB to the asymptote, in the parking plane
arc3 = np.array([0.35 * np.array([np.cos(s), np.sin(s), 0]) for s in np.linspace(0, finf, 40)])
ax.plot(*arc3.T, color=BLUE, lw=1.2)
ax.text(0.05, 0.38, 0, r"$f_\infty=150.8^\circ$", color=BLUE, fontsize=10.5)
ax.scatter([0], [0], [0], color=INK, s=25)
ax.text(0.02, -0.12, -0.08, "O", color=INK, fontsize=11)
ax.set_xlim(-1.2, 1.2); ax.set_ylim(-1.2, 1.2); ax.set_zlim(-0.6, 0.8)
ax.set_box_aspect((1, 1, 0.58), zoom=1.25)
ax.view_init(24, -62)
ax.set_axis_off()
ax.legend(frameon=False, fontsize=10, loc="upper left")
ax.set_title(r"Turning the plane about OB moves the asymptote round a cone: $\sin\psi=\sin f_\infty\,\sin\alpha$", fontsize=12)
fig.savefig("img/fig-pc-departure-tilt.png", dpi=150, bbox_inches="tight")
print("wrote img/fig-pc-departure-tilt.png")
