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 { 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 { 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 { 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 { 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 { 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 { let stations: Vec = 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 = 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() }