Skip to main content

fromsoftware_shared/dl_math/
vector.rs

1use std::fmt;
2use std::ops::{Add, Sub};
3
4#[repr(C, align(16))]
5#[derive(Clone, Copy, PartialEq)]
6pub struct F32Vector4(pub f32, pub f32, pub f32, pub f32);
7
8#[repr(C)]
9#[derive(Clone, Copy, PartialEq)]
10pub struct F32Vector3(pub f32, pub f32, pub f32);
11
12#[repr(C)]
13#[derive(Clone, Copy, PartialEq)]
14pub struct F32Vector2(pub f32, pub f32);
15
16macro_rules! impl_common_traits {
17    ($t:ident, $($i:tt),+ $(,)?) => {
18        impl Add<$t> for $t {
19            type Output = $t;
20            #[inline]
21            fn add(self, rhs: $t) -> Self::Output {
22                $t($(self.$i + rhs.$i),+)
23            }
24        }
25
26        impl Sub<$t> for $t {
27            type Output = $t;
28            #[inline]
29            fn sub(self, rhs: $t) -> Self::Output {
30                $t($(self.$i - rhs.$i),+)
31            }
32        }
33
34        impl fmt::Debug for $t {
35            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
36                ($(self.$i),+).fmt(f)
37            }
38        }
39    };
40}
41
42impl_common_traits!(F32Vector4, 0, 1, 2, 3);
43impl_common_traits!(F32Vector3, 0, 1, 2);
44impl_common_traits!(F32Vector2, 0, 1);
45
46impl From<F32Vector4> for glam::Vec4 {
47    #[inline]
48    fn from(F32Vector4(x, y, z, w): F32Vector4) -> Self {
49        Self::new(x, y, z, w)
50    }
51}
52
53impl From<F32Vector3> for glam::Vec3 {
54    #[inline]
55    fn from(F32Vector3(x, y, z): F32Vector3) -> Self {
56        Self::new(x, y, z)
57    }
58}
59
60impl From<F32Vector3> for glam::Vec3A {
61    #[inline]
62    fn from(F32Vector3(x, y, z): F32Vector3) -> Self {
63        Self::new(x, y, z)
64    }
65}
66
67impl From<F32Vector2> for glam::Vec2 {
68    #[inline]
69    fn from(F32Vector2(x, y): F32Vector2) -> Self {
70        Self::new(x, y)
71    }
72}
73
74impl From<glam::Vec4> for F32Vector4 {
75    #[inline]
76    fn from(v: glam::Vec4) -> Self {
77        Self(v.x, v.y, v.z, v.w)
78    }
79}
80
81impl From<glam::Vec3> for F32Vector3 {
82    #[inline]
83    fn from(v: glam::Vec3) -> Self {
84        Self(v.x, v.y, v.z)
85    }
86}
87
88impl From<glam::Vec3A> for F32Vector3 {
89    #[inline]
90    fn from(v: glam::Vec3A) -> Self {
91        Self(v.x, v.y, v.z)
92    }
93}
94
95impl From<glam::Vec2> for F32Vector2 {
96    #[inline]
97    fn from(v: glam::Vec2) -> Self {
98        Self(v.x, v.y)
99    }
100}