"""Lecture 5, the three directions of the general equations at a vehicle: e_V along the velocity, n_gamma
(the direction in which e_V turns when gamma grows: up, in the vertical plane of the velocity) and n_chi
(horizontal, to the right of the path), with the local north, east and up. From the engine
(frames.wind_directions) for gamma = 20 deg, chi = 50 deg. Writes img/fig-05-directions.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, GREY
from flight.frames import wind_directions

GAM, CHI = np.radians(20), np.radians(50)
E_V, N_G, N_C = wind_directions(GAM, CHI)                       # NED components


def enu(v):
    return np.array([v[1], v[0], -v[2]])                         # NED -> east, north, up for drawing


if __name__ == "__main__":
    fig = plt.figure(figsize=(8.5, 7))
    ax = fig.add_subplot(111, projection="3d")
    for v, lab in (((0, 1, 0), "north"), ((1, 0, 0), "east"), ((0, 0, 1), "up")):
        ax.quiver(0, 0, 0, *v, color=GREY, lw=1.4, arrow_length_ratio=0.08)
        ax.text(*(1.08 * np.array(v)), lab, color=GREY, fontsize=12)
    xx, yy = np.meshgrid([-0.3, 1.1], [-0.9, 1.1])
    ax.plot_surface(xx, yy, 0 * xx, color="#e8e2d4", alpha=0.35)
    for v, lab, c in ((E_V, "$\\mathbf{e}_V$", MAROON), (N_G, "$\\mathbf{n}_\\gamma$", BLUE), (N_C, "$\\mathbf{n}_\\chi$", GREEN)):
        p = enu(v)
        ax.quiver(0, 0, 0, *p, color=c, lw=2.6, arrow_length_ratio=0.1)
        ax.text(*(1.12 * p), lab, color=c, fontsize=16)
    h = enu(np.array([np.cos(CHI), np.sin(CHI), 0.0]))
    ax.plot([0, h[0]], [0, h[1]], [0, 0], color=MAROON, ls=":", lw=1.5)
    ax.plot([h[0] * np.cos(GAM), E_V[1]], [h[1] * np.cos(GAM), E_V[0]], [0, -E_V[2]], color=MAROON, ls=":", lw=1)
    ax.text(*(0.55 * h + np.array([0, 0, 0.07])), "$\\gamma$", color=MAROON, fontsize=14)
    ax.text(0.12, 0.45, 0.0, "$\\chi$", color=MAROON, fontsize=14)
    ax.view_init(elev=22, azim=-62)
    ax.set_xlim(-0.3, 1.1)
    ax.set_ylim(-0.9, 1.1)
    ax.set_zlim(-0.2, 1.1)
    ax.set_box_aspect((1.3, 2.0, 1.3))
    ax.axis("off")
    save(fig, "fig-05-directions")
