"""Generate the per-theta viewpoint robustness chart (Fig 4b).

PARA holds steady through ~18 degrees, ACT collapses after ~14 degrees.
This is the single most memorable result chart in the paper.
"""

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

theta = [0, 3.6, 7.1, 10.7, 14.3, 17.9, 21.4, 25]
para  = [88, 79, 62, 63, 62, 62, 33, 38]
act   = [67, 54, 42, 17, 12,  0,  0,  0]

PARA_COLOR = "#16653a"
ACT_COLOR  = "#a12029"

fig, ax = plt.subplots(figsize=(7.0, 4.5), dpi=130)

ax.plot(theta, para, color=PARA_COLOR, linewidth=2.6, marker="o", markersize=8,
        markerfacecolor=PARA_COLOR, markeredgecolor="white", markeredgewidth=1.5,
        label="PARA", zorder=5)
ax.plot(theta, act, color=ACT_COLOR, linewidth=2.6, marker="s", markersize=8,
        markerfacecolor=ACT_COLOR, markeredgecolor="white", markeredgewidth=1.5,
        linestyle="--", label="ACT", zorder=4)

# Annotate the train angle
ax.axvline(x=0, color="#71717a", linestyle=":", linewidth=1.2, alpha=0.7, zorder=1)
ax.annotate("train", xy=(0, 8), xytext=(0.6, 8), fontsize=10, color="#71717a",
            ha="left", va="center")

# Highlight the cliff for ACT
ax.annotate("ACT collapses", xy=(17.9, 0), xytext=(19, 18),
            fontsize=10.5, color=ACT_COLOR, ha="center",
            arrowprops=dict(arrowstyle="->", color=ACT_COLOR, lw=1.2))

# Highlight PARA plateau
ax.annotate("PARA plateau\n~62%", xy=(14.3, 62), xytext=(7.5, 32),
            fontsize=10.5, color=PARA_COLOR, ha="center",
            arrowprops=dict(arrowstyle="->", color=PARA_COLOR, lw=1.2))

ax.set_xlabel("Camera Elevation Angle θ (degrees)", fontsize=12)
ax.set_ylabel("Success Rate (%)", fontsize=12)
ax.set_xlim(-1, 26)
ax.set_ylim(-3, 100)
ax.set_yticks([0, 25, 50, 75, 100])
ax.set_xticks([0, 5, 10, 15, 20, 25])
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_color("#888888")
ax.spines["bottom"].set_color("#888888")
ax.tick_params(colors="#444444", labelsize=11)
ax.legend(fontsize=12, frameon=False, loc="upper right")
ax.set_title("Viewpoint Robustness: Per-Angle Success Rate",
             fontsize=13.5, fontweight="700", pad=12, color="#1f2d3d")

plt.tight_layout()
plt.savefig("/data/cameron/penpot/figures/per_theta.svg", format="svg", bbox_inches="tight")
plt.savefig("/data/cameron/penpot/figures/per_theta.png", format="png",
            bbox_inches="tight", dpi=180)
print("Wrote per_theta.svg and per_theta.png")
