Files
sfhorizont/wasm/src/lib.rs
T
2026-10-24 13:20:55 +02:00

562 lines
21 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use sfzenith_core::{fov, projection};
use std::cell::Cell;
use wasm_bindgen::prelude::*;
use wasm_bindgen::closure::Closure;
use wasm_bindgen_futures::JsFuture;
use web_sys::{
CanvasRenderingContext2d, DeviceOrientationEvent, HtmlAnchorElement, HtmlCanvasElement,
HtmlVideoElement, MediaStreamConstraints,
};
thread_local! {
static CURRENT_HEADING: Cell<f64> = Cell::new(-1.0);
static CURRENT_TILT: Cell<f64> = Cell::new(0.0);
// Raw, unsmoothed absolute (alpha, beta, gamma) for spherical reprojection.
// Component-wise smoothing would corrupt it (0/360 wrap, gimbal coupling).
static CURRENT_ORIENTATION: Cell<Option<(f64, f64, f64)>> = Cell::new(None);
}
/// Install panic hook for readable error messages in the browser console.
#[wasm_bindgen(start)]
pub fn main() {
console_error_panic_hook::set_once();
}
/// Request camera access and attach the stream to the <video> element.
#[wasm_bindgen]
pub async fn init_camera(video_id: &str) -> Result<(), JsValue> {
let window = gloo_utils::window();
let navigator = window.navigator();
let media_devices = navigator.media_devices()?;
let constraints = MediaStreamConstraints::new();
// Request rear camera (environment facing)
let video_constraints = js_sys::Object::new();
js_sys::Reflect::set(
&video_constraints,
&JsValue::from_str("facingMode"),
&JsValue::from_str("environment"),
)?;
constraints.set_video(&video_constraints);
constraints.set_audio(&JsValue::from(false));
let promise = media_devices.get_user_media_with_constraints(&constraints)?;
let stream = JsFuture::from(promise).await?;
let document = gloo_utils::document();
let video: HtmlVideoElement = document
.get_element_by_id(video_id)
.expect("video element not found")
.unchecked_into();
// Use Reflect::set for srcObject — more reliable across web-sys versions
js_sys::Reflect::set(&video, &JsValue::from_str("srcObject"), &stream)?;
Ok(())
}
/// Draw the current video frame onto the canvas, resized to fit within
/// max_w x max_h while maintaining the original aspect ratio.
#[wasm_bindgen]
pub fn capture_frame(video_id: &str, canvas_id: &str, max_w: u32, max_h: u32) -> Result<(), JsValue> {
let document = gloo_utils::document();
let video: HtmlVideoElement = document
.get_element_by_id(video_id)
.expect("video element not found")
.unchecked_into();
let canvas: HtmlCanvasElement = document
.get_element_by_id(canvas_id)
.expect("canvas element not found")
.unchecked_into();
let src_w = video.video_width() as f64;
let src_h = video.video_height() as f64;
// Fit within max_w x max_h keeping aspect ratio
let scale = (max_w as f64 / src_w).min(max_h as f64 / src_h).min(1.0);
let dst_w = (src_w * scale).round() as u32;
let dst_h = (src_h * scale).round() as u32;
canvas.set_width(dst_w);
canvas.set_height(dst_h);
let ctx: CanvasRenderingContext2d = canvas
.get_context("2d")?
.unwrap()
.unchecked_into();
ctx.draw_image_with_html_video_element_and_dw_and_dh(
&video, 0.0, 0.0, dst_w as f64, dst_h as f64,
)?;
Ok(())
}
/// Download the canvas content as a PNG file.
#[wasm_bindgen]
pub fn save_png(canvas_id: &str, filename: &str) -> Result<(), JsValue> {
let document = gloo_utils::document();
let canvas: HtmlCanvasElement = document
.get_element_by_id(canvas_id)
.expect("canvas element not found")
.unchecked_into();
let data_url = canvas.to_data_url_with_type("image/png")?;
let anchor: HtmlAnchorElement = document
.create_element("a")?
.unchecked_into();
anchor.set_href(&data_url);
anchor.set_download(filename);
anchor.click();
Ok(())
}
// Small window: smooths sensor jitter but keeps capture-time lag low.
const ORIENTATION_BUFFER_SIZE: usize = 5;
/// Build a `deviceorientation`/`deviceorientationabsolute` handler sharing the given buffers.
///
/// Heading priority — only north-referenced sources are trusted:
/// 1. `webkitCompassHeading` (iOS Safari), if present.
/// 2. `alpha` **only when** `event.absolute == true` (Android `deviceorientationabsolute`),
/// as `360 − alpha`.
/// A relative `deviceorientation.alpha` (absolute == false) is NOT north-referenced, so it is
/// ignored for heading — otherwise the azimuth would drift from an arbitrary start on Android.
fn make_orientation_closure(
heading_buf: std::rc::Rc<std::cell::RefCell<Vec<f64>>>,
tilt_buf: std::rc::Rc<std::cell::RefCell<Vec<f64>>>,
compass_id: String,
tilt_id: String,
) -> Closure<dyn FnMut(DeviceOrientationEvent)> {
Closure::<dyn FnMut(_)>::new(move |event: DeviceOrientationEvent| {
let document = gloo_utils::document();
let webkit_heading = js_sys::Reflect::get(&event, &JsValue::from_str("webkitCompassHeading"))
.ok()
.and_then(|v| v.as_f64());
// Store the raw north-referenced orientation triple for reprojection:
// Android supplies it via deviceorientationabsolute; on iOS alpha is
// relative, so derive an absolute-equivalent alpha from the compass.
if let (Some(beta), Some(gamma)) = (event.beta(), event.gamma()) {
if event.absolute() {
if let Some(alpha) = event.alpha() {
CURRENT_ORIENTATION.with(|o| o.set(Some((alpha, beta, gamma))));
}
} else if let Some(h) = webkit_heading {
let alpha = projection::derive_ios_alpha(h, beta, gamma);
CURRENT_ORIENTATION.with(|o| o.set(Some((alpha, beta, gamma))));
}
}
// Compass heading (0°=North)
let heading = webkit_heading.or_else(|| {
if event.absolute() {
event.alpha().map(|a| (360.0 - a).rem_euclid(360.0))
} else {
None
}
});
if let Some(h) = heading {
let mut buf = heading_buf.borrow_mut();
if buf.len() >= ORIENTATION_BUFFER_SIZE { buf.remove(0); }
buf.push(h);
let (sin_sum, cos_sum) = buf.iter().fold((0.0, 0.0), |(s, c), &a| {
let rad = a.to_radians();
(s + rad.sin(), c + rad.cos())
});
let avg = sin_sum.atan2(cos_sum).to_degrees().rem_euclid(360.0);
CURRENT_HEADING.with(|h| h.set(avg));
if let Some(el) = document.get_element_by_id(&compass_id) {
el.set_text_content(Some(&format!("{:.0}°", avg)));
}
}
// Tilt: beta is 0° when flat, 90° when vertical.
// We want 0° = camera vertical (phone upright), so tilt = beta - 90.
if let Some(beta) = event.beta() {
let tilt = beta - 90.0; // 0° = vertical, negative = tilted up, positive = tilted down
let mut buf = tilt_buf.borrow_mut();
if buf.len() >= ORIENTATION_BUFFER_SIZE { buf.remove(0); }
buf.push(tilt);
let avg = buf.iter().sum::<f64>() / buf.len() as f64;
CURRENT_TILT.with(|t| t.set(avg));
if let Some(el) = document.get_element_by_id(&tilt_id) {
let sign = if avg > 0.5 { "+" } else { "" };
el.set_text_content(Some(&format!("{}{:.0}°", sign, avg)));
}
}
})
}
/// Start listening to device orientation and update elements with compass heading and tilt.
/// `compass_id`: element for compass heading (0°=N), `tilt_id`: element for vertical tilt (0°=vertical camera).
/// On iOS 13+ requests permission automatically.
///
/// Listens to both `deviceorientation` (iOS `webkitCompassHeading`) and `deviceorientationabsolute`
/// (Android north-referenced `alpha`) so the heading is compass-correct on both platforms.
#[wasm_bindgen]
pub async fn start_orientation(compass_id: &str, tilt_id: &str) -> Result<(), JsValue> {
let window = gloo_utils::window();
// iOS 13+ requires explicit permission request
let doe = js_sys::Reflect::get(&window, &JsValue::from_str("DeviceOrientationEvent"))?;
if !doe.is_undefined() {
let request_perm = js_sys::Reflect::get(&doe, &JsValue::from_str("requestPermission"))?;
if request_perm.is_function() {
let func: js_sys::Function = request_perm.unchecked_into();
let promise: js_sys::Promise = func.call0(&doe)?.unchecked_into();
let result = JsFuture::from(promise).await?;
if result.as_string().map_or(true, |s| s != "granted") {
return Err(JsValue::from_str("Orientation permission denied"));
}
}
}
let compass_id = compass_id.to_string();
let tilt_id = tilt_id.to_string();
let heading_buf = std::rc::Rc::new(std::cell::RefCell::new(
Vec::<f64>::with_capacity(ORIENTATION_BUFFER_SIZE),
));
let tilt_buf = std::rc::Rc::new(std::cell::RefCell::new(
Vec::<f64>::with_capacity(ORIENTATION_BUFFER_SIZE),
));
// Relative event: carries iOS webkitCompassHeading + tilt (beta) on all platforms.
let relative = make_orientation_closure(
heading_buf.clone(), tilt_buf.clone(), compass_id.clone(), tilt_id.clone(),
);
window.add_event_listener_with_callback(
"deviceorientation",
relative.as_ref().unchecked_ref(),
)?;
relative.forget();
// Absolute event: Android's north-referenced heading source.
let absolute = make_orientation_closure(heading_buf, tilt_buf, compass_id, tilt_id);
window.add_event_listener_with_callback(
"deviceorientationabsolute",
absolute.as_ref().unchecked_ref(),
)?;
absolute.forget();
Ok(())
}
/// Return the latest smoothed compass heading (0–360), or -1 if not yet available.
#[wasm_bindgen]
pub fn get_heading() -> f64 {
CURRENT_HEADING.with(|h| h.get())
}
/// Return the latest smoothed tilt in degrees (0 = vertical camera).
#[wasm_bindgen]
pub fn get_tilt() -> f64 {
CURRENT_TILT.with(|t| t.get())
}
/// Latest raw absolute orientation as [alpha, beta, gamma], or [] when no
/// north-referenced orientation has been received yet.
#[wasm_bindgen]
pub fn get_orientation() -> Vec<f64> {
CURRENT_ORIENTATION.with(|o| match o.get() {
Some((a, b, g)) => vec![a, b, g],
None => vec![],
})
}
/// Camera boresight [azimuth, elevation] for an orientation triple — used to
/// cross-check the raw triple against the smoothed compass before projecting.
#[wasm_bindgen]
pub fn orientation_az_el(alpha: f64, beta: f64, gamma: f64, screen_angle: f64) -> Vec<f64> {
let r = projection::rotation_matrix(alpha, beta, gamma, screen_angle);
let (az, el) = projection::camera_az_el(&r);
vec![az, el]
}
/// Result of projecting a camera frame onto the equirectangular strip.
/// `x` is normalized to [0, strip_w); `x + w` may exceed strip_w — composite
/// the overflow again at `x - strip_w`.
#[wasm_bindgen]
pub struct ProjectedPatch {
x: i32,
y: i32,
w: u32,
h: u32,
data: Vec<u8>,
}
#[wasm_bindgen]
impl ProjectedPatch {
#[wasm_bindgen(getter)]
pub fn x(&self) -> i32 { self.x }
#[wasm_bindgen(getter)]
pub fn y(&self) -> i32 { self.y }
#[wasm_bindgen(getter)]
pub fn w(&self) -> u32 { self.w }
#[wasm_bindgen(getter)]
pub fn h(&self) -> u32 { self.h }
/// Move the RGBA bytes out (single wasm→JS copy; the patch is empty after).
pub fn take_data(&mut self) -> Vec<u8> {
std::mem::take(&mut self.data)
}
}
/// Reproject a pinhole-camera RGBA frame into an equirectangular patch using
/// the full device orientation (W3C alpha/beta/gamma + screen angle).
/// Returns undefined/null when the frame falls entirely outside the strip.
#[wasm_bindgen]
#[allow(clippy::too_many_arguments)]
pub fn project_frame(
rgba: &[u8], src_w: u32, src_h: u32,
alpha: f64, beta: f64, gamma: f64, screen_angle: f64,
h_fov_deg: f64, strip_w: u32, strip_h: u32,
elev_min_deg: f64, elev_max_deg: f64,
) -> Option<ProjectedPatch> {
let r = projection::rotation_matrix(alpha, beta, gamma, screen_angle);
projection::project_frame_core(
rgba, src_w, src_h, &r, h_fov_deg, strip_w, strip_h, elev_min_deg, elev_max_deg,
)
.map(|(geom, data)| ProjectedPatch {
x: geom.x,
y: geom.y,
w: geom.w,
h: geom.h,
data,
})
}
/// Calculate camera FOV from two overlapping frames and their heading difference.
/// See `sfzenith_core::fov::calculate_fov`. Returns degrees, or -1.0 on failure.
#[wasm_bindgen]
pub fn calculate_fov(
rgba1: &[u8], w1: u32, h1: u32,
rgba2: &[u8], w2: u32, h2: u32,
heading_diff: f64,
) -> f64 {
fov::calculate_fov(rgba1, w1, h1, rgba2, w2, h2, heading_diff)
}
/// Get raw RGBA pixel data from the canvas.
#[wasm_bindgen]
pub fn get_frame_data(canvas_id: &str) -> Result<Vec<u8>, JsValue> {
let document = gloo_utils::document();
let canvas: HtmlCanvasElement = document
.get_element_by_id(canvas_id)
.expect("canvas element not found")
.unchecked_into();
let w = canvas.width();
let h = canvas.height();
let ctx: CanvasRenderingContext2d = canvas
.get_context("2d")?
.unwrap()
.unchecked_into();
let image_data = ctx.get_image_data(0.0, 0.0, w as f64, h as f64)?;
Ok(image_data.data().to_vec())
}
// ---------------------------------------------------------------------------
// Horizon profile + exporters (thin wrappers over sfzenith-core)
//
// A profile crosses the boundary as a Float64Array of 360 altitudes
// (index = azimuth degree, measured frame). Exporters apply the north offset.
// ---------------------------------------------------------------------------
use sfzenith_core::{hrz, stellarium, zip, HorizonProfile};
fn js_err(e: impl std::fmt::Display) -> JsValue {
JsValue::from_str(&e.to_string())
}
fn profile_arg(alt: &[f64]) -> Result<HorizonProfile, JsValue> {
HorizonProfile::from_altitudes(alt.to_vec()).map_err(js_err)
}
/// Sparse (azimuth, altitude) points → 360-value profile (seam-aware linear).
#[wasm_bindgen]
pub fn profile_from_points(az_deg: &[f64], alt_deg: &[f64]) -> Result<Vec<f64>, JsValue> {
if az_deg.len() != alt_deg.len() {
return Err(js_err("azimuth and altitude arrays differ in length"));
}
let pts: Vec<(f64, f64)> = az_deg.iter().copied().zip(alt_deg.iter().copied()).collect();
HorizonProfile::from_points(&pts).map(|p| p.altitudes().to_vec()).map_err(js_err)
}
/// Median smoothing over ±radius degrees (0 = off, 1 = light, 2 = strong).
#[wasm_bindgen]
pub fn profile_smoothed(alt: &[f64], radius_deg: u32) -> Result<Vec<f64>, JsValue> {
Ok(profile_arg(alt)?.smoothed(radius_deg as usize).altitudes().to_vec())
}
/// Read a NINA `.hrz` (for "Import existing .hrz") into a profile.
#[wasm_bindgen]
pub fn parse_hrz(text: &str) -> Result<Vec<f64>, JsValue> {
hrz::parse_hrz(text).map(|p| p.altitudes().to_vec()).map_err(js_err)
}
/// NINA custom horizon text (edge + margin, 0..=360 in 1° steps).
#[wasm_bindgen]
pub fn export_hrz(alt: &[f64], margin_deg: f64, north_offset_deg: f64) -> Result<String, JsValue> {
let p = profile_arg(alt)?;
Ok(hrz::export_hrz(&p, hrz::HrzOptions { margin_deg, north_offset_deg }))
}
fn meta_arg(meta_json: &str) -> Result<stellarium::LandscapeMeta, JsValue> {
serde_json::from_str(meta_json).map_err(|e| js_err(format!("landscape meta: {e}")))
}
/// `landscape.ini` text for the Export preview. `kind` = "spherical" | "polygonal".
/// meta_json: {"name","author","appName","location":{"latDeg","lonDeg","elevM"}?}
#[wasm_bindgen]
pub fn landscape_ini(meta_json: &str, kind: &str, top_deg: f64, bottom_deg: f64) -> Result<String, JsValue> {
let meta = meta_arg(meta_json)?;
match kind {
"spherical" => Ok(stellarium::spherical_ini(&meta, top_deg, bottom_deg)),
"polygonal" => Ok(stellarium::polygonal_ini(&meta)),
other => Err(js_err(format!("unknown landscape kind \"{other}\""))),
}
}
/// Stellarium outline-only landscape as a .zip.
#[wasm_bindgen]
pub fn export_stellarium_polygonal(alt: &[f64], meta_json: &str, north_offset_deg: f64) -> Result<Vec<u8>, JsValue> {
let p = profile_arg(alt)?;
let meta = meta_arg(meta_json)?;
Ok(zip::zip(&stellarium::export_polygonal(&p, &meta, north_offset_deg)))
}
/// Suggested spherical options for this profile: {widthPx,heightPx,topDeg,bottomDeg,northOffsetDeg}.
#[wasm_bindgen]
pub fn stellarium_fitted_options(alt: &[f64], src_bottom_deg: f64) -> Result<String, JsValue> {
let o = stellarium::SphericalOptions::fitted(&profile_arg(alt)?, src_bottom_deg);
Ok(format!(
"{{\"widthPx\":{},\"heightPx\":{},\"topDeg\":{},\"bottomDeg\":{},\"northOffsetDeg\":{}}}",
o.width_px, o.height_px, o.top_deg, o.bottom_deg, o.north_offset_deg
))
}
/// Stellarium photo landscape as a .zip.
/// `rgba` is the app's equirectangular strip (column 0 = azimuth 0, full 360°,
/// rows from src_top_deg down to src_bottom_deg). opts_json fields are
/// optional: {widthPx, heightPx, topDeg, bottomDeg, northOffsetDeg}.
#[wasm_bindgen]
#[allow(clippy::too_many_arguments)]
pub fn export_stellarium_spherical(
rgba: &[u8], width: u32, height: u32, src_top_deg: f64, src_bottom_deg: f64,
alt: &[f64], meta_json: &str, opts_json: &str,
) -> Result<Vec<u8>, JsValue> {
let p = profile_arg(alt)?;
let meta = meta_arg(meta_json)?;
let opts: stellarium::SphericalOptions = if opts_json.trim().is_empty() {
stellarium::SphericalOptions::fitted(&p, src_bottom_deg)
} else {
serde_json::from_str(opts_json).map_err(|e| js_err(format!("spherical options: {e}")))?
};
let band = stellarium::PanoramaBand { rgba, width, height, top_deg: src_top_deg, bottom_deg: src_bottom_deg, sky_mask: None };
let files = stellarium::export_spherical(&band, &p, &meta, &opts).map_err(js_err)?;
Ok(zip::zip(&files))
}
// ---------------------------------------------------------------------------
// HorizonSurvey: the stitched strip + sky mask + brush edits, kept in wasm
// memory so Review can repaint and re-derive the profile without copying the
// strip across the boundary on every stroke.
// ---------------------------------------------------------------------------
use sfzenith_core::skymask::{self, BandGeom, MaskEdit};
#[wasm_bindgen]
pub struct HorizonSurvey {
rgba: Vec<u8>,
geom: BandGeom,
auto_mask: Vec<u8>,
mask: Vec<u8>,
edits: Vec<MaskEdit>,
}
#[wasm_bindgen]
impl HorizonSurvey {
/// Build from the equirectangular strip (column 0 = azimuth 0, rows from
/// top_deg down to bottom_deg; alpha 0 = not photographed). Runs detection.
#[wasm_bindgen(constructor)]
pub fn new(rgba: Vec<u8>, width: u32, height: u32, top_deg: f64, bottom_deg: f64) -> Result<HorizonSurvey, JsValue> {
let geom = BandGeom { width, height, top_deg, bottom_deg };
if geom.is_empty() || rgba.len() < geom.len() * 4 || top_deg <= bottom_deg {
return Err(js_err("strip size does not match its pixel buffer"));
}
let auto_mask = skymask::detect_sky(&rgba, geom);
let mask = auto_mask.clone();
Ok(HorizonSurvey { rgba, geom, auto_mask, mask, edits: Vec::new() })
}
/// Replace the brush edits (JSON array of {mode:"sky"|"ground", azDeg, altDeg, radiusDeg}).
pub fn set_edits(&mut self, edits_json: &str) -> Result<(), JsValue> {
let edits: Vec<MaskEdit> = if edits_json.trim().is_empty() {
Vec::new()
} else {
serde_json::from_str(edits_json).map_err(|e| js_err(format!("mask edits: {e}")))?
};
self.edits = edits;
self.mask.copy_from_slice(&self.auto_mask);
skymask::apply_edits(&mut self.mask, self.geom, &self.edits);
Ok(())
}
/// Final mask, one byte per strip pixel: 0 = no photo, 1 = sky, 2 = obstruction.
pub fn mask(&self) -> Vec<u8> {
self.mask.clone()
}
/// 360 altitudes (index = azimuth), or undefined when nothing is photographed.
pub fn profile(&self) -> Option<Vec<f64>> {
skymask::edge_profile(&self.mask, self.geom).map(|p| p.altitudes().to_vec())
}
/// 360 flags (1 = this degree has photos).
pub fn coverage(&self) -> Vec<u8> {
skymask::coverage_degrees(&self.mask, self.geom).into_iter().map(u8::from).collect()
}
/// 360 flags (1 = obstruction reaches the top of the photos here).
pub fn top_touch(&self) -> Vec<u8> {
skymask::top_touch_degrees(&self.mask, self.geom).into_iter().map(u8::from).collect()
}
/// Stellarium photo landscape (.zip) with the sky mask applied.
/// opts_json: {widthPx, heightPx, topDeg, bottomDeg, northOffsetDeg}; empty = fitted defaults.
pub fn export_spherical(&self, meta_json: &str, opts_json: &str) -> Result<Vec<u8>, JsValue> {
let profile = skymask::edge_profile(&self.mask, self.geom)
.ok_or_else(|| js_err("nothing photographed yet"))?;
let meta = meta_arg(meta_json)?;
let opts: stellarium::SphericalOptions = if opts_json.trim().is_empty() {
stellarium::SphericalOptions::fitted(&profile, self.geom.bottom_deg)
} else {
serde_json::from_str(opts_json).map_err(|e| js_err(format!("spherical options: {e}")))?
};
let band = stellarium::PanoramaBand {
rgba: &self.rgba,
width: self.geom.width,
height: self.geom.height,
top_deg: self.geom.top_deg,
bottom_deg: self.geom.bottom_deg,
sky_mask: Some(&self.mask),
};
let files = stellarium::export_spherical(&band, &profile, &meta, &opts).map_err(js_err)?;
Ok(zip::zip(&files))
}
}