//a Imports use js_sys::Array; use wasm_bindgen::prelude::*; use geo_nd::{FArray, Vector}; pub type Vec3f32 = FArray; pub type Vec4f32 = FArray; pub type Quatf32 = geo_nd::QArray; pub type Vec3f64 = FArray; pub type Vec4f64 = FArray; pub type Quatf64 = geo_nd::QArray; #[macro_export] macro_rules! wasm_vec { ($t:ident, $v:ident, $f:ty) => { #[wasm_bindgen] pub struct $t($v); #[wasm_bindgen] impl $t { //cp new /// Create a new #[wasm_bindgen(constructor)] pub fn new(x: $f, y: $f, z: $f) -> Result<$t, JsValue> { Ok(Self([x, y, z].into())) } #[wasm_bindgen(getter)] pub fn array(&self) -> Box<[$f]> { let v: [$f; 3] = self.0.into(); v.into() } #[wasm_bindgen(getter)] pub fn is_zero(&self) -> bool { self.0.is_zero() } pub fn reduce_sum(&self) -> $f { self.0.reduce_sum() } #[wasm_bindgen(getter)] pub fn length_sq(&self) -> $f { self.0.length_sq() } #[wasm_bindgen(getter)] pub fn length(&self) -> $f { self.0.length() } pub fn normalize(&self) -> $t { self.0.normalize().into() } pub fn dot(&self, other: &$t) -> $f { self.0.dot(&other.0) } pub fn distance_sq(&self, other: &$t) -> $f { self.0.distance_sq(&other.0) } pub fn distance(&self, other: &$t) -> $f { self.0.distance(&other.0) } pub fn mix(&self, other: &$t, t: $f) -> $t { self.0.mix(&other.0, t).into() } //zz All done } impl From<$v> for $t { fn from(f: $v) -> $t { $t(f) } } impl From<[$f; 3]> for $t { fn from(f: [$f; 3]) -> $t { $t(f.into()) } } }; } wasm_vec!(WasmVec3f32, Vec3f32, f32); wasm_vec!(WasmVec3f64, Vec3f64, f64);