Files
mass-driver/crates/mass-driver-wasm/src/api.rs
scott a2daa2d617 Add mass driver station system with Lagrange point placement
- Lagrange point computation (L1-L5) for any Sun-planet pair in Rust
- Station generation: auto-place at Lagrange points by priority (inner → outer planets)
- Station panel UI: count slider (5-50), launch velocity slider (5-100 km/s) with info tooltip
- Blue diamond markers on 2D canvas with labels when zoomed in
- Active station list in sidebar (Earth L1, Mars L2, Jupiter L4, etc.)
- WASM API: generate_stations(), get_station_positions(), get_station_names()
- Station positions update every frame (co-rotating with planets at Lagrange points)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 12:00:36 -07:00

101 lines
3.3 KiB
Rust

use mass_driver_core::station;
use orbital_mechanics::bodies;
use orbital_mechanics::orbits;
use wasm_bindgen::prelude::*;
/// Initialize the WASM module. Called once on page load.
#[wasm_bindgen]
pub fn init() -> Result<(), JsValue> {
web_sys::console::log_1(&"mass-driver WASM initialized".into());
Ok(())
}
/// Get the number of celestial bodies in the simulation.
#[wasm_bindgen]
pub fn get_body_count() -> usize {
bodies::all_bodies().len()
}
/// Get positions of all celestial bodies at a given Julian Date.
///
/// Returns a Float64Array of [x0, y0, z0, x1, y1, z1, ...] in AU.
#[wasm_bindgen]
pub fn get_body_positions_at_epoch(jd: f64) -> Vec<f64> {
let all = bodies::all_bodies();
orbits::all_positions_at_epoch(&all, jd)
}
/// Get body names as a JSON array of strings.
#[wasm_bindgen]
pub fn get_body_names() -> String {
let all = bodies::all_bodies();
let names: Vec<&str> = all.iter().map(|b| b.name).collect();
serde_json::to_string(&names).unwrap()
}
/// Get body colors as a flat array [r0, g0, b0, r1, g1, b1, ...].
#[wasm_bindgen]
pub fn get_body_colors() -> Vec<u8> {
let all = bodies::all_bodies();
all.iter().flat_map(|b| b.color.iter().copied()).collect()
}
/// Get body radii in km as a Float64Array.
#[wasm_bindgen]
pub fn get_body_radii() -> Vec<f64> {
let all = bodies::all_bodies();
all.iter().map(|b| b.radius_km).collect()
}
/// Get velocities of all celestial bodies at a given Julian Date.
///
/// Returns a Float64Array of [vx0, vy0, vz0, vx1, vy1, vz1, ...] in AU/day.
#[wasm_bindgen]
pub fn get_body_velocities_at_epoch(jd: f64) -> Vec<f64> {
let all = bodies::all_bodies();
orbits::all_velocities_at_epoch(&all, jd)
}
/// Get orbit points for a given body, sampled around its full orbit.
/// Returns a Float64Array of [x0, y0, z0, x1, y1, z1, ...] in AU.
/// `samples` is the number of points around the orbit.
#[wasm_bindgen]
pub fn get_orbit_points(body_id: usize, jd: f64, samples: usize) -> Vec<f64> {
let all = bodies::all_bodies();
if body_id >= all.len() || body_id == bodies::id::SUN {
return vec![];
}
orbits::orbit_points(&all, body_id, jd, samples)
}
/// Generate default stations and return their configuration as JSON.
#[wasm_bindgen]
pub fn generate_stations(count: usize) -> String {
let stations = station::generate_default_stations(count);
serde_json::to_string(&stations).unwrap()
}
/// Get station positions at a given Julian Date.
/// Takes a JSON config string (from generate_stations) and returns
/// Float64Array of [x0, y0, z0, x1, y1, z1, ...] in AU.
#[wasm_bindgen]
pub fn get_station_positions(stations_json: &str, jd: f64) -> Vec<f64> {
let stations: Vec<station::Station> = match serde_json::from_str(stations_json) {
Ok(s) => s,
Err(_) => return vec![],
};
let all_bodies = bodies::all_bodies();
station::all_station_positions(&stations, &all_bodies, jd)
}
/// Get station names from a config JSON string.
#[wasm_bindgen]
pub fn get_station_names(stations_json: &str) -> String {
let stations: Vec<station::Station> = match serde_json::from_str(stations_json) {
Ok(s) => s,
Err(_) => return "[]".to_string(),
};
let names: Vec<&str> = stations.iter().map(|s| s.name.as_str()).collect();
serde_json::to_string(&names).unwrap()
}