362 lines
11 KiB
TypeScript
362 lines
11 KiB
TypeScript
// Canvas chart renderers for ascent profile, telemetry channels, and orbital geometry
|
||
// I started rendering by making charts first, they're cool
|
||
import { generateTrajectory } from "../../../shared/ephemeris.js";
|
||
import type {
|
||
ContractOffer,
|
||
FlightParams,
|
||
MissionTemplate,
|
||
SimResult,
|
||
TrajectoryResult,
|
||
} from "../../../shared/types.js";
|
||
import { getThemePalette } from "../../theme.js";
|
||
import { clear, fmtDuration, fmtNum, h } from "../../ui.js";
|
||
|
||
export function drawAscentChart(
|
||
ascentCanvas: HTMLCanvasElement,
|
||
sim: SimResult,
|
||
): void {
|
||
const ctx = ascentCanvas.getContext("2d");
|
||
if (!ctx) return;
|
||
const pal = getThemePalette();
|
||
const W = ascentCanvas.width;
|
||
const H = ascentCanvas.height;
|
||
ctx.fillStyle = pal.bgDeep;
|
||
ctx.fillRect(0, 0, W, H);
|
||
ctx.save();
|
||
ctx.strokeStyle = pal.border;
|
||
ctx.globalAlpha = 0.55;
|
||
ctx.lineWidth = 1;
|
||
for (let i = 1; i < 5; i++) {
|
||
const y = (H / 5) * i;
|
||
ctx.beginPath();
|
||
ctx.moveTo(48, y);
|
||
ctx.lineTo(W - 14, y);
|
||
ctx.stroke();
|
||
}
|
||
ctx.restore();
|
||
const pts = sim.telemetry;
|
||
if (!pts.length) return;
|
||
const maxDr = Math.max(250, ...pts.map((p) => p.downrange / 1000));
|
||
const maxAlt = Math.max(220, ...pts.map((p) => p.altitude / 1000));
|
||
const padL = 48;
|
||
const padR = 16;
|
||
const padT = 18;
|
||
const padB = 26;
|
||
const pw = W - padL - padR;
|
||
const ph = H - padT - padB;
|
||
const toX = (drKm: number) => padL + (drKm / maxDr) * pw;
|
||
const toY = (altKm: number) => padT + ph - (altKm / maxAlt) * ph;
|
||
const qPeak = pts.reduce(
|
||
(best, p) => (p.dynamicPressure > best.dynamicPressure ? p : best),
|
||
pts[0],
|
||
);
|
||
const qx = toX(qPeak.downrange / 1000);
|
||
const qy = toY(qPeak.altitude / 1000);
|
||
ctx.strokeStyle = sim.success ? pal.accent : pal.red;
|
||
ctx.lineWidth = 2.4;
|
||
ctx.beginPath();
|
||
pts.forEach((p, idx) => {
|
||
const x = toX(p.downrange / 1000);
|
||
const y = toY(p.altitude / 1000);
|
||
if (idx === 0) ctx.moveTo(x, y);
|
||
else ctx.lineTo(x, y);
|
||
});
|
||
ctx.stroke();
|
||
ctx.fillStyle = pal.amber;
|
||
ctx.beginPath();
|
||
ctx.arc(qx, qy, 4.5, 0, Math.PI * 2);
|
||
ctx.fill();
|
||
ctx.font = "11px monospace";
|
||
ctx.fillText(
|
||
`Max-Q ${(qPeak.dynamicPressure / 1000).toFixed(1)} kPa`,
|
||
Math.min(W - 140, qx + 8),
|
||
Math.max(16, qy - 6),
|
||
);
|
||
ctx.fillStyle = pal.textDim;
|
||
ctx.fillText("0 km", 6, padT + ph);
|
||
ctx.fillText(`${Math.round(maxAlt)} km`, 4, padT + 10);
|
||
ctx.fillText(`Downrange ${Math.round(maxDr)} km`, W - 150, H - 6);
|
||
ctx.fillStyle = sim.success ? pal.green : pal.red;
|
||
ctx.fillText(
|
||
sim.success
|
||
? `ORBIT INSERTION ${Math.round(sim.periapsis / 1000)}×${Math.round(sim.apoapsis / 1000)} km`
|
||
: sim.failureDetail.slice(0, 56),
|
||
padL + 8,
|
||
20,
|
||
);
|
||
}
|
||
|
||
export function drawMultiTelemetryChart(
|
||
telemetryDiagCanvas: HTMLCanvasElement,
|
||
rootCauseBoxEl: HTMLElement,
|
||
sim: SimResult,
|
||
): void {
|
||
const ctx = telemetryDiagCanvas.getContext("2d");
|
||
if (!ctx) return;
|
||
const pal = getThemePalette();
|
||
const W = telemetryDiagCanvas.width;
|
||
const H = telemetryDiagCanvas.height;
|
||
ctx.fillStyle = pal.bgDeep;
|
||
ctx.fillRect(0, 0, W, H);
|
||
ctx.save();
|
||
ctx.strokeStyle = pal.border;
|
||
ctx.globalAlpha = 0.55;
|
||
ctx.lineWidth = 1;
|
||
for (let i = 1; i < 4; i++) {
|
||
const y = (H / 4) * i;
|
||
ctx.beginPath();
|
||
ctx.moveTo(42, y);
|
||
ctx.lineTo(W - 16, y);
|
||
ctx.stroke();
|
||
}
|
||
ctx.restore();
|
||
const pts = sim.telemetry;
|
||
if (!pts.length) return;
|
||
const maxT = Math.max(10, pts[pts.length - 1].t);
|
||
const maxQ = Math.max(10000, ...pts.map((p) => p.dynamicPressure));
|
||
const maxG = Math.max(4, ...pts.map((p) => p.gLoad));
|
||
const maxCpu = Math.max(120, ...pts.map((p) => p.cpuLoadPct ?? 0));
|
||
const maxRad = Math.max(2, ...pts.map((p) => p.radFluxMsvHr ?? 0));
|
||
const padL = 42;
|
||
const padT = 26;
|
||
const padB = 22;
|
||
const pw = W - padL - 16;
|
||
const ph = H - padT - padB;
|
||
const toX = (t: number) => padL + (t / maxT) * pw;
|
||
const plotSeries = (
|
||
color: string,
|
||
getter: (p: (typeof pts)[number]) => number,
|
||
) => {
|
||
ctx.strokeStyle = color;
|
||
ctx.lineWidth = 2;
|
||
ctx.beginPath();
|
||
pts.forEach((p, idx) => {
|
||
const x = toX(p.t);
|
||
const y = padT + ph - Math.min(1, Math.max(0, getter(p))) * ph;
|
||
if (idx === 0) ctx.moveTo(x, y);
|
||
else ctx.lineTo(x, y);
|
||
});
|
||
ctx.stroke();
|
||
};
|
||
const cpu100Y = padT + ph - (100 / maxCpu) * ph;
|
||
ctx.save();
|
||
ctx.setLineDash([4, 4]);
|
||
ctx.strokeStyle = "rgba(245, 158, 11, 0.6)";
|
||
ctx.beginPath();
|
||
ctx.moveTo(padL, cpu100Y);
|
||
ctx.lineTo(W - 16, cpu100Y);
|
||
ctx.stroke();
|
||
ctx.restore();
|
||
plotSeries("#38bdf8", (p) => p.dynamicPressure / maxQ);
|
||
plotSeries("#34d399", (p) => p.gLoad / maxG);
|
||
plotSeries("#f59e0b", (p) => (p.cpuLoadPct ?? 0) / maxCpu);
|
||
plotSeries("#f472b6", (p) => (p.radFluxMsvHr ?? 0) / maxRad);
|
||
ctx.font = "11px monospace";
|
||
ctx.fillStyle = "#38bdf8";
|
||
ctx.fillText(`Q max ${(maxQ / 1000).toFixed(1)}kPa`, 46, 16);
|
||
ctx.fillStyle = "#34d399";
|
||
ctx.fillText(`G max ${maxG.toFixed(1)}G`, 178, 16);
|
||
ctx.fillStyle = "#f59e0b";
|
||
ctx.fillText(`CPU peak ${Math.round(sim.peakCpuLoadPct ?? 0)}%`, 284, 16);
|
||
ctx.fillStyle = "#f472b6";
|
||
ctx.fillText(
|
||
`Rad peak ${maxRad.toFixed(1)} mSv/h (${sim.totalSeuCount ?? 0} SEU)`,
|
||
420,
|
||
16,
|
||
);
|
||
ctx.fillStyle = pal.textDim;
|
||
ctx.fillText(`T+0s`, padL, H - 6);
|
||
ctx.fillText(`T+${Math.round(maxT)}s`, W - 64, H - 6);
|
||
const compFault =
|
||
sim.failureReason === "computer_damaged" ||
|
||
sim.failureReason === "computer_overload" ||
|
||
sim.failureReason === "radiation_seu_lockup" ||
|
||
pts.some((p) => p.computerStatus === "failed");
|
||
clear(rootCauseBoxEl);
|
||
rootCauseBoxEl.append(
|
||
h(
|
||
"div",
|
||
{ class: "spread", style: { marginBottom: "6px" } },
|
||
h("h3", {}, "Tier-2 Avionics & Propulsion Root-Cause Analyzer"),
|
||
h(
|
||
"span",
|
||
{ class: `badge ${sim.success ? "ok" : "err"}` },
|
||
sim.success ? "NOMINAL ASCENT" : (sim.failureReason ?? "FAULT"),
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "Flight Computer Peak Load / SEUs"),
|
||
h(
|
||
"span",
|
||
{
|
||
class: `v ${(sim.peakCpuLoadPct ?? 0) > 125 ? "red" : (sim.peakCpuLoadPct ?? 0) > 95 ? "amber" : "green"}`,
|
||
},
|
||
`${Math.round(sim.peakCpuLoadPct ?? 0)}% • ${sim.totalSeuCount ?? 0} SEUs (${compFault ? "HW/CTRL FAULT" : "NOMINAL"})`,
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "Peak Heat Flux / Dynamic Pressure"),
|
||
h(
|
||
"span",
|
||
{ class: "v" },
|
||
`${sim.peakHeatFluxWcm2.toFixed(1)} W/cm² • ${(sim.maxQ / 1000).toFixed(1)} kPa (Stage ${sim.maxQStage})`,
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "Burnout State (Alt / Vel / Downrange)"),
|
||
h(
|
||
"span",
|
||
{ class: "v" },
|
||
`${(sim.burnoutAltitude / 1000).toFixed(1)} km • ${fmtNum(sim.burnoutVelocity)} m/s • ${(sim.burnoutDownrange / 1000).toFixed(1)} km`,
|
||
),
|
||
),
|
||
...(sim.success
|
||
? []
|
||
: [
|
||
h(
|
||
"p",
|
||
{
|
||
class: "red mono",
|
||
style: { fontSize: "11px", marginTop: "6px" },
|
||
},
|
||
`Diagnostic Verdict: ${sim.failureDetail}`,
|
||
),
|
||
]),
|
||
);
|
||
}
|
||
|
||
export interface OrbitChartOptions {
|
||
orbitCanvas: HTMLCanvasElement;
|
||
gmatStatsBoxEl: HTMLElement;
|
||
contract: ContractOffer | MissionTemplate | null;
|
||
fp: FlightParams;
|
||
gmatLoading: boolean;
|
||
trajInput?: TrajectoryResult | null;
|
||
}
|
||
|
||
export function drawOrbitChart(opts: OrbitChartOptions): void {
|
||
const { orbitCanvas, gmatStatsBoxEl, contract, fp, gmatLoading, trajInput } =
|
||
opts;
|
||
const ctx = orbitCanvas.getContext("2d");
|
||
if (!ctx) return;
|
||
const pal = getThemePalette();
|
||
const W = orbitCanvas.width;
|
||
const H = orbitCanvas.height;
|
||
ctx.fillStyle = pal.bgDeep;
|
||
ctx.fillRect(0, 0, W, H);
|
||
const traj =
|
||
trajInput ??
|
||
generateTrajectory(contract?.target ?? "leo", undefined, 220, {
|
||
altKm: fp.targetApoapsisKm,
|
||
incDeg: fp.targetInclinationDeg,
|
||
});
|
||
const maxR = Math.max(1, ...traj.points.map((p) => Math.hypot(p.x, p.z)));
|
||
const scale = (Math.min(W, H) * 0.4) / maxR;
|
||
const cx = W * 0.5;
|
||
const cy = H * 0.5;
|
||
ctx.fillStyle =
|
||
contract?.target === "mars"
|
||
? "#d96b43"
|
||
: contract?.target === "solar"
|
||
? "#ffd166"
|
||
: "#3498db";
|
||
ctx.beginPath();
|
||
ctx.arc(cx, cy, Math.max(8, Math.min(22, 6371 * scale)), 0, Math.PI * 2);
|
||
ctx.fill();
|
||
ctx.lineWidth = 2.4;
|
||
for (let i = 1; i < traj.points.length; i++) {
|
||
const p0 = traj.points[i - 1];
|
||
const p1 = traj.points[i];
|
||
const x0 = cx + p0.x * scale;
|
||
const y0 = cy + p0.z * scale;
|
||
const x1 = cx + p1.x * scale;
|
||
const y1 = cy + p1.z * scale;
|
||
if (p1.inShadow) {
|
||
ctx.strokeStyle = pal.violet;
|
||
} else if ((p1.radBeltFlux ?? 0) > 1.0) {
|
||
ctx.strokeStyle = pal.amber;
|
||
} else {
|
||
ctx.strokeStyle = pal.accent;
|
||
}
|
||
ctx.beginPath();
|
||
ctx.moveTo(x0, y0);
|
||
ctx.lineTo(x1, y1);
|
||
ctx.stroke();
|
||
}
|
||
const eclipseCount = traj.points.filter((p) => p.inShadow).length;
|
||
const eclipsePct = Math.round(
|
||
(eclipseCount / Math.max(1, traj.points.length)) * 100,
|
||
);
|
||
const peakFlux = Math.max(0, ...traj.points.map((p) => p.radBeltFlux ?? 0));
|
||
const maxDrift = Math.max(
|
||
0,
|
||
...traj.points.map((p) => p.perturbationDriftKm ?? 0),
|
||
);
|
||
const samplePt = traj.points[Math.floor(traj.points.length * 0.25)];
|
||
ctx.fillStyle = pal.textDim;
|
||
ctx.font = "11px monospace";
|
||
ctx.fillText(
|
||
`[${traj.source.toUpperCase()}] Target ${(contract?.target ?? "leo").toUpperCase()} • Inc ${fp.targetInclinationDeg.toFixed(1)}° • Period ${fmtDuration(traj.period)}`,
|
||
14,
|
||
18,
|
||
);
|
||
ctx.fillStyle = pal.violet;
|
||
ctx.fillText(`Eclipse Shadow: ${eclipsePct}%`, 14, H - 10);
|
||
ctx.fillStyle = pal.amber;
|
||
ctx.fillText(`Peak Van Allen Flux: ${peakFlux.toFixed(2)}`, 190, H - 10);
|
||
ctx.fillStyle = pal.green;
|
||
ctx.fillText(`Perturbation Drift: ${maxDrift.toFixed(1)} km`, 420, H - 10);
|
||
clear(gmatStatsBoxEl);
|
||
gmatStatsBoxEl.append(
|
||
h(
|
||
"div",
|
||
{ class: "spread", style: { marginBottom: "4px" } },
|
||
h("h3", {}, "Tier-3 GMAT Orbital Propagation & Environment"),
|
||
h(
|
||
"span",
|
||
{ class: `badge ${traj.source === "gmat" ? "ok" : "info"}` },
|
||
gmatLoading
|
||
? "QUERYING GMAT..."
|
||
: traj.source === "gmat"
|
||
? "GMAT RK89"
|
||
: "ANALYTIC + J2",
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "Osculating SMA / Ecc"),
|
||
h(
|
||
"span",
|
||
{ class: "v" },
|
||
`${fmtNum(samplePt?.smaKm ?? 6371 + fp.targetApoapsisKm)} km • e ${(samplePt?.ecc ?? 0.002).toFixed(4)}`,
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "Eclipse Shadow / Peak Rad Flux"),
|
||
h(
|
||
"span",
|
||
{ class: "v amber" },
|
||
`${eclipsePct}% shadow • ${peakFlux.toFixed(2)} rel flux`,
|
||
),
|
||
),
|
||
h(
|
||
"div",
|
||
{ class: "stat-row" },
|
||
h("span", { class: "k" }, "J2 / Drag Perturbation Drift"),
|
||
h(
|
||
"span",
|
||
{ class: "v" },
|
||
`${maxDrift.toFixed(2)} km per propagation window`,
|
||
),
|
||
),
|
||
);
|
||
}
|