use crate::madd::Madd;
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use crate::MathError;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vector<const N: usize>(pub [f64; N]);
pub type Vec2 = Vector<2>;
pub type Vec3 = Vector<3>;
const SQUARES_LOW: f64 = f64::from_bits(23 << 52);
const SCALE_UP: f64 = f64::from_bits(1623 << 52);
const SCALE_DOWN: f64 = f64::from_bits(423 << 52);
#[must_use]
pub fn unit_scale(size: f64) -> f64 {
if !(size.is_finite() && size > 0.0) {
return 1.0;
}
#[allow(clippy::cast_possible_truncation)]
let exponent = size.log2().floor().clamp(-1000.0, 1000.0) as i32;
2.0_f64.powi(-exponent)
}
impl<const N: usize> Vector<N> {
#[must_use]
pub fn length_squared(self) -> f64 {
let mut sum = 0.0;
let mut i = 0;
while i < N {
sum = self.0[i].madd(self.0[i], sum);
i += 1;
}
sum
}
#[must_use]
pub fn length(self) -> f64 {
let sum = self.length_squared();
if sum.is_finite() && sum >= SQUARES_LOW {
return sum.sqrt();
}
self.rescaled()
.map_or_else(|| sum.sqrt(), |(v, back)| v.length_squared().sqrt() * back)
}
fn rescaled(self) -> Option<(Self, f64)> {
if !self.0.iter().all(|c| c.is_finite()) {
return None;
}
let most = self.0.iter().fold(0.0_f64, |m, c| m.max(c.abs()));
if most <= 0.0 {
return None;
}
let (by, back) = if most > 1.0 {
(SCALE_DOWN, SCALE_UP)
} else {
(SCALE_UP, SCALE_DOWN)
};
Some((Self(std::array::from_fn(|i| self.0[i] * by)), back))
}
#[must_use]
pub fn largest_component(self) -> f64 {
self.0.iter().fold(0.0_f64, |m, c| {
if m.is_nan() || c.is_nan() {
f64::NAN
} else {
m.max(c.abs())
}
})
}
#[must_use]
pub fn at_unit_size(self) -> Self {
let by = unit_scale(self.largest_component());
Self(std::array::from_fn(|i| self.0[i] * by))
}
pub fn normalize(self) -> Result<Self, MathError> {
let sum = self.length_squared();
let v = if sum.is_finite() && sum >= SQUARES_LOW {
self
} else {
self.rescaled().ok_or(MathError::ZeroVector)?.0
};
let len = v.length_squared().sqrt();
if !len.is_finite() || len < f64::MIN_POSITIVE {
return Err(MathError::ZeroVector);
}
let inv = 1.0 / len;
Ok(Self(std::array::from_fn(|i| v.0[i] * inv)))
}
}
impl Vector<2> {
#[must_use]
pub const fn new(x: f64, y: f64) -> Self {
Self([x, y])
}
#[must_use]
pub const fn x(self) -> f64 {
self.0[0]
}
#[must_use]
pub const fn y(self) -> f64 {
self.0[1]
}
#[must_use]
pub fn dot(self, rhs: Self) -> f64 {
self.0[0].madd(rhs.0[0], self.0[1] * rhs.0[1])
}
}
impl Vector<3> {
#[must_use]
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Self([x, y, z])
}
#[must_use]
pub const fn x(self) -> f64 {
self.0[0]
}
#[must_use]
pub const fn y(self) -> f64 {
self.0[1]
}
#[must_use]
pub const fn z(self) -> f64 {
self.0[2]
}
#[must_use]
pub fn dot(self, rhs: Self) -> f64 {
self.0[0].madd(rhs.0[0], self.0[1].madd(rhs.0[1], self.0[2] * rhs.0[2]))
}
#[must_use]
pub fn cross(self, rhs: Self) -> Self {
Self([
self.0[1].madd(rhs.0[2], -(self.0[2] * rhs.0[1])),
self.0[2].madd(rhs.0[0], -(self.0[0] * rhs.0[2])),
self.0[0].madd(rhs.0[1], -(self.0[1] * rhs.0[0])),
])
}
pub fn square_part(self, axis: Self) -> Result<Self, MathError> {
let v = self.at_unit_size();
let along = axis * v.dot(axis);
let part = v - along;
if part.length() >= 0.5 * v.length() {
return part.normalize();
}
let tilt = part.dot(axis);
let again = part - axis * tilt;
let bound = (0..3)
.map(|i| {
let off_axis = (1.0 - axis.0[i] * axis.0[i]).max(0.0).sqrt();
let sizes = along.0[i].abs()
+ part.0[i].abs()
+ again.0[i].abs()
+ (axis.0[i] * tilt).abs();
sizes * off_axis
})
.sum::<f64>()
* f64::EPSILON;
let length = again.length();
let taken = length > bound && length.is_finite();
if !taken {
return Err(MathError::ZeroVector);
}
let unit = again.normalize()?;
(unit - axis * unit.dot(axis)).normalize()
}
}
impl<const N: usize> Add for Vector<N> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(std::array::from_fn(|i| self.0[i] + rhs.0[i]))
}
}
impl<const N: usize> AddAssign for Vector<N> {
fn add_assign(&mut self, rhs: Self) {
for i in 0..N {
self.0[i] += rhs.0[i];
}
}
}
impl<const N: usize> Sub for Vector<N> {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(std::array::from_fn(|i| self.0[i] - rhs.0[i]))
}
}
impl<const N: usize> SubAssign for Vector<N> {
fn sub_assign(&mut self, rhs: Self) {
for i in 0..N {
self.0[i] -= rhs.0[i];
}
}
}
impl<const N: usize> Mul<f64> for Vector<N> {
type Output = Self;
fn mul(self, s: f64) -> Self {
Self(std::array::from_fn(|i| self.0[i] * s))
}
}
impl<const N: usize> Mul<Vector<N>> for f64 {
type Output = Vector<N>;
fn mul(self, rhs: Vector<N>) -> Vector<N> {
Vector(std::array::from_fn(|i| self * rhs.0[i]))
}
}
impl<const N: usize> Neg for Vector<N> {
type Output = Self;
fn neg(self) -> Self {
Self(std::array::from_fn(|i| -self.0[i]))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Position<const N: usize>(pub [f64; N]);
pub type Point2 = Position<2>;
pub type Point3 = Position<3>;
impl Position<2> {
#[must_use]
pub const fn new(x: f64, y: f64) -> Self {
Self([x, y])
}
#[must_use]
pub const fn x(self) -> f64 {
self.0[0]
}
#[must_use]
pub const fn y(self) -> f64 {
self.0[1]
}
}
impl Position<3> {
#[must_use]
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Self([x, y, z])
}
#[must_use]
pub const fn x(self) -> f64 {
self.0[0]
}
#[must_use]
pub const fn y(self) -> f64 {
self.0[1]
}
#[must_use]
pub const fn z(self) -> f64 {
self.0[2]
}
}
impl<const N: usize> Add<Vector<N>> for Position<N> {
type Output = Self;
fn add(self, rhs: Vector<N>) -> Self {
Self(std::array::from_fn(|i| self.0[i] + rhs.0[i]))
}
}
impl<const N: usize> Sub<Vector<N>> for Position<N> {
type Output = Self;
fn sub(self, rhs: Vector<N>) -> Self {
Self(std::array::from_fn(|i| self.0[i] - rhs.0[i]))
}
}
impl<const N: usize> Sub for Position<N> {
type Output = Vector<N>;
fn sub(self, rhs: Self) -> Vector<N> {
Vector(std::array::from_fn(|i| self.0[i] - rhs.0[i]))
}
}
macro_rules! impl_serde_for_array_newtype {
($ty:ident, $name:expr) => {
#[cfg(feature = "serde")]
impl<const N: usize> serde::Serialize for $ty<N> {
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeTuple;
let mut tup = ser.serialize_tuple(N)?;
for &val in &self.0 {
tup.serialize_element(&val)?;
}
tup.end()
}
}
#[cfg(feature = "serde")]
impl<'de, const N: usize> serde::Deserialize<'de> for $ty<N> {
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
struct ArrayVisitor<const M: usize>;
impl<'de, const M: usize> serde::de::Visitor<'de> for ArrayVisitor<M> {
type Value = [f64; M];
fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "an array of {} floats", M)
}
fn visit_seq<A: serde::de::SeqAccess<'de>>(
self,
mut seq: A,
) -> Result<Self::Value, A::Error> {
let mut arr = [0.0; M];
for (i, slot) in arr.iter_mut().enumerate() {
*slot = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(i, &self))?;
}
Ok(arr)
}
}
de.deserialize_tuple(N, ArrayVisitor::<N>).map($ty)
}
}
};
}
impl_serde_for_array_newtype!(Vector, "Vector");
impl_serde_for_array_newtype!(Position, "Position");
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn a_square_part_near_the_axis_is_square_to_it_or_refused() {
let ulps = 8.0 * f64::EPSILON;
let z = Vec3::new(0.0, 0.0, 1.0);
let x = Vec3::new(1.0, 0.0, 0.0);
let diagonal = Vec3::new(1.0, 1.0, 1.0).normalize().unwrap();
let across = Vec3::new(1.0, -1.0, 0.0).normalize().unwrap();
for s in [1e-200, 1.0, 1e200] {
for off in [1e-15, 1e-12, 1e-9] {
let part = Vec3::new(off * s, 0.0, s).square_part(z).unwrap();
assert!(
(part - x).length() <= ulps,
"size {s}, {off} off z: {part:?}"
);
}
for off in [1e-14, 1e-12, 1e-10, 1e-9, 1e-7, 1e-4, 0.3] {
let v = (diagonal + across * off) * s;
let part = v.square_part(diagonal).unwrap();
assert!(
part.dot(diagonal).abs() <= ulps,
"size {s}, {off} off: {part:?}"
);
assert!(
(part.length() - 1.0).abs() <= ulps,
"size {s}, {off} off: {part:?}"
);
assert!(
(part - across).length() <= 64.0 * f64::EPSILON / off,
"size {s}, {off} off: {part:?}"
);
}
assert!(Vec3::new(0.0, 0.0, s).square_part(z).is_err(), "size {s}");
assert!(
Vec3::new(s, s, s).square_part(diagonal).is_err(),
"size {s}"
);
let tilted = Vec3::new(s, s, s * (1.0 + 1e-10)).square_part(diagonal);
if let Ok(part) = tilted {
assert!(part.dot(diagonal).abs() <= ulps, "size {s}: {part:?}");
assert!((part.length() - 1.0).abs() <= ulps, "size {s}: {part:?}");
}
}
}
#[test]
fn a_square_part_holds_at_the_ends_of_the_float_range() {
let ulps = 8.0 * f64::EPSILON;
let z = Vec3::new(0.0, 0.0, 1.0);
let diagonal = Vec3::new(1.0, 1.0, 1.0).normalize().unwrap();
let least = f64::from_bits(1);
for (v, axis, want) in [
(
Vec3::new(1.7e308, 1.7e308, 0.0),
z,
Vec3::new(1.0, 1.0, 0.0).normalize().unwrap(),
),
(
Vec3::new(least, 0.0, 0.0),
diagonal,
Vec3::new(2.0, -1.0, -1.0).normalize().unwrap(),
),
(
Vec3::new(f64::MAX, -f64::MAX, f64::MAX),
diagonal,
Vec3::new(1.0, -2.0, 1.0).normalize().unwrap(),
),
] {
let part = v.square_part(axis).unwrap();
assert!(part.dot(axis).abs() <= ulps, "{v:?}: {part:?}");
assert!((part.length() - 1.0).abs() <= ulps, "{v:?}: {part:?}");
assert!((part - want).length() <= ulps, "{v:?}: {part:?}");
}
for s in [1.0, 1e-300, 1e300, least, f64::MAX] {
assert!(
Vec3::new(s, s, s).square_part(diagonal).is_err(),
"size {s}"
);
}
for bad in [
Vec3::new(f64::NAN, 1.0, 0.0),
Vec3::new(f64::INFINITY, 1.0, 0.0),
Vec3::new(0.0, 0.0, 0.0),
] {
assert!(bad.square_part(z).is_err(), "{bad:?}");
}
}
#[test]
fn the_unit_size_holds_at_the_ends_of_the_float_range() {
let least = f64::from_bits(1);
for v in [
Vec3::new(f64::MAX, f64::MAX, -f64::MAX),
Vec3::new(least, 0.0, -least),
Vec3::new(f64::MAX, least, 0.0),
Vec3::new(-least, f64::MAX, 1.0),
] {
let u = v.at_unit_size();
let most = u.largest_component();
assert!(u.0.iter().all(|c| c.is_finite()), "{v:?}: {u:?}");
assert!(
(2.0_f64.powi(-74)..=2.0_f64.powi(24)).contains(&most),
"{v:?}: {u:?}"
);
for (a, b) in v.0.iter().zip(u.0) {
assert!(
b.to_bits() == 0 || a.signum().to_bits() == b.signum().to_bits(),
"{v:?}: {u:?}"
);
}
}
let zero = Vec3::new(0.0, 0.0, 0.0);
assert_eq!(zero.at_unit_size(), zero);
for size in [0.0, f64::NAN, f64::INFINITY, -1.0] {
assert_eq!(unit_scale(size).to_bits(), 1.0_f64.to_bits(), "{size}");
}
assert_eq!(unit_scale(least).to_bits(), 2.0_f64.powi(1000).to_bits());
assert_eq!(
unit_scale(f64::MAX).to_bits(),
2.0_f64.powi(-1000).to_bits()
);
assert!(Vec3::new(f64::NAN, 1.0, 0.0).largest_component().is_nan());
assert!(Vec3::new(1.0, f64::NAN, 0.0).at_unit_size().y().is_nan());
}
#[test]
fn a_vector_of_any_finite_size_has_a_length_and_a_direction() {
let sizes = [
1e200,
1e-200,
1e-310,
f64::MIN_POSITIVE,
1.7e308,
1e155,
1e-160,
];
for s in sizes {
for (v, length) in [
(Vec3::new(s, 0.0, 0.0), s),
(Vec3::new(0.0, -s, 0.0), s),
(Vec3::new(0.0, 0.0, s), s),
] {
assert_eq!(v.length().to_bits(), length.to_bits(), "{v:?}");
let unit = v.normalize().unwrap();
assert!((unit.length() - 1.0).abs() < 1e-15, "{v:?} gives {unit:?}");
assert!(
(unit.dot(v) - v.length()).abs() <= 1e-15 * v.length(),
"{v:?}"
);
}
let tilt = Vec3::new(3.0 * s / 4.0, 0.0, s);
if (3.0 * s / 4.0).is_normal() {
let unit = tilt.normalize().unwrap();
assert!(
(unit.x() - 0.6).abs() < 1e-15 && (unit.z() - 0.8).abs() < 1e-15,
"{s}: {unit:?}"
);
assert!(
(tilt.length() / (1.25 * s) - 1.0).abs() < 1e-15,
"{s}: {}",
tilt.length()
);
}
let flat = Vec2::new(0.0, s);
assert_eq!(flat.length().to_bits(), s.to_bits());
assert!((flat.normalize().unwrap().y() - 1.0).abs() < 1e-15);
}
let past = Vec3::new(1.7e308, 1.7e308, 0.0);
assert!(past.length().is_infinite());
let unit = past.normalize().unwrap();
assert!(
(unit.x() - std::f64::consts::FRAC_1_SQRT_2).abs() < 1e-15,
"{unit:?}"
);
for bad in [
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(f64::NAN, 1.0, 0.0),
Vec3::new(f64::INFINITY, 1.0, 0.0),
] {
assert!(bad.normalize().is_err(), "{bad:?}");
}
}
#[test]
fn vec3_dot() {
let a = Vec3::new(1.0, 2.0, 3.0);
let b = Vec3::new(4.0, 5.0, 6.0);
assert!((a.dot(b) - 32.0).abs() < 1e-14);
}
#[test]
fn vec3_cross() {
let x = Vec3::new(1.0, 0.0, 0.0);
let y = Vec3::new(0.0, 1.0, 0.0);
let z = x.cross(y);
assert!((z.x()).abs() < 1e-14);
assert!((z.y()).abs() < 1e-14);
assert!((z.z() - 1.0).abs() < 1e-14);
}
#[test]
fn vec3_normalize() {
let v = Vec3::new(3.0, 4.0, 0.0);
let n = v.normalize().expect("non-zero");
assert!((n.length() - 1.0).abs() < 1e-14);
}
#[test]
fn vec3_zero_normalize_fails() {
let v = Vec3::new(0.0, 0.0, 0.0);
assert!(v.normalize().is_err());
}
#[test]
fn vec3_denormal_normalize_gives_a_unit_vector() {
let v = Vec3::new(1e-320, 0.0, 0.0);
let unit = v.normalize().unwrap();
assert!(
(unit.x() - 1.0).abs() < 1e-15 && unit.y().abs() < 1e-15,
"{unit:?}"
);
}
#[test]
fn vec2_dot() {
let a = Vec2::new(3.0, 4.0);
let b = Vec2::new(1.0, 2.0);
assert!((a.dot(b) - 11.0).abs() < 1e-14);
}
#[test]
fn point3_sub_gives_vec3() {
let a = Point3::new(3.0, 4.0, 5.0);
let b = Point3::new(1.0, 1.0, 1.0);
let v = a - b;
assert!((v.x() - 2.0).abs() < 1e-14);
assert!((v.y() - 3.0).abs() < 1e-14);
assert!((v.z() - 4.0).abs() < 1e-14);
}
#[test]
fn point3_add_vec3() {
let p = Point3::new(1.0, 2.0, 3.0);
let v = Vec3::new(1.0, 1.0, 1.0);
let q = p + v;
assert!((q.x() - 2.0).abs() < 1e-14);
}
#[test]
fn point3_sub_vec3() {
let p = Point3::new(3.0, 4.0, 5.0);
let v = Vec3::new(1.0, 1.0, 1.0);
let q = p - v;
assert!((q.x() - 2.0).abs() < 1e-14);
assert!((q.y() - 3.0).abs() < 1e-14);
assert!((q.z() - 4.0).abs() < 1e-14);
}
#[test]
fn scalar_times_vec3() {
let v = Vec3::new(1.0, 2.0, 3.0);
let scaled = 2.0 * v;
assert!((scaled.x() - 2.0).abs() < 1e-14);
assert!((scaled.y() - 4.0).abs() < 1e-14);
assert!((scaled.z() - 6.0).abs() < 1e-14);
}
#[test]
fn vec3_add_assign() {
let mut a = Vec3::new(1.0, 2.0, 3.0);
a += Vec3::new(4.0, 5.0, 6.0);
assert!((a.x() - 5.0).abs() < 1e-14);
assert!((a.y() - 7.0).abs() < 1e-14);
assert!((a.z() - 9.0).abs() < 1e-14);
}
#[test]
fn vec3_sub_assign() {
let mut a = Vec3::new(5.0, 7.0, 9.0);
a -= Vec3::new(1.0, 2.0, 3.0);
assert!((a.x() - 4.0).abs() < 1e-14);
assert!((a.y() - 5.0).abs() < 1e-14);
assert!((a.z() - 6.0).abs() < 1e-14);
}
use proptest::prelude::*;
proptest! {
#[test]
fn prop_normalize_unit_length(x in -10.0f64..10.0, y in -10.0f64..10.0, z in -10.0f64..10.0) {
let v = Vec3::new(x, y, z);
if let Ok(n) = v.normalize() {
prop_assert!((n.length() - 1.0).abs() < 1e-12, "length = {}", n.length());
}
}
#[test]
fn prop_cross_anticommutative(
ax in -10.0f64..10.0, ay in -10.0f64..10.0, az in -10.0f64..10.0,
bx in -10.0f64..10.0, by in -10.0f64..10.0, bz in -10.0f64..10.0,
) {
let a = Vec3::new(ax, ay, az);
let b = Vec3::new(bx, by, bz);
let ab = a.cross(b);
let ba = b.cross(a);
prop_assert!((ab.x() + ba.x()).abs() < 1e-10);
prop_assert!((ab.y() + ba.y()).abs() < 1e-10);
prop_assert!((ab.z() + ba.z()).abs() < 1e-10);
}
#[test]
fn prop_point_sub_vec_inverse_of_add(
px in -100.0f64..100.0, py in -100.0f64..100.0, pz in -100.0f64..100.0,
vx in -100.0f64..100.0, vy in -100.0f64..100.0, vz in -100.0f64..100.0,
) {
let p = Point3::new(px, py, pz);
let v = Vec3::new(vx, vy, vz);
let roundtrip = (p + v) - v;
prop_assert!((roundtrip.x() - p.x()).abs() < 1e-10);
prop_assert!((roundtrip.y() - p.y()).abs() < 1e-10);
prop_assert!((roundtrip.z() - p.z()).abs() < 1e-10);
}
}
}