havok_types/math/
quaternion.rs
1use parse_display::Display;
2
3#[repr(C, align(16))]
14#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Display)]
17#[display("({x:.06} {y:.06} {z:.06} {scaler:.06})")]
18pub struct Quaternion {
19 pub x: f32,
24 pub y: f32,
29 pub z: f32,
34 pub scaler: f32,
39}
40
41static_assertions::assert_eq_size!(Quaternion, [u8; 16]);
42
43impl Quaternion {
44 pub const fn new(x: f32, y: f32, z: f32, scaler: f32) -> Self {
46 Self { x, y, z, scaler }
47 }
48
49 #[inline]
50 pub fn to_le_bytes(&self) -> [u8; 16] {
51 let mut bytes = [0; 16];
52 bytes[0..4].copy_from_slice(&self.x.to_le_bytes());
53 bytes[4..8].copy_from_slice(&self.y.to_le_bytes());
54 bytes[8..12].copy_from_slice(&self.z.to_le_bytes());
55 bytes[12..16].copy_from_slice(&self.scaler.to_le_bytes());
56 bytes
57 }
58
59 #[inline]
60 pub fn to_be_bytes(&self) -> [u8; 16] {
61 let mut bytes = [0; 16];
62 bytes[0..4].copy_from_slice(&self.x.to_be_bytes());
63 bytes[4..8].copy_from_slice(&self.y.to_be_bytes());
64 bytes[8..12].copy_from_slice(&self.z.to_be_bytes());
65 bytes[12..16].copy_from_slice(&self.scaler.to_be_bytes());
66 bytes
67 }
68
69 #[inline]
71 pub const fn from_le_bytes(bytes: &[u8; 16]) -> Self {
72 Self {
73 x: f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
74 y: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
75 z: f32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
76 scaler: f32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
77 }
78 }
79
80 #[inline]
82 pub const fn from_be_bytes(bytes: &[u8; 16]) -> Self {
83 Self {
84 x: f32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
85 y: f32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
86 z: f32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
87 scaler: f32::from_be_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
88 }
89 }
90}