havok_types/math/
rotation.rs

1use crate::Vector4;
2use parse_display::Display;
3
4/// Rotation (same as [`Matrix3`])
5///
6/// # C++ Info
7/// - name: `hkRotation`
8/// - type_size: ` 48`(x86)/` 48`(x86_64)
9/// - align: ` 16`(x86)/` 16`(x86_64)
10///
11/// # Examples
12/// ```
13/// use havok_types::{Rotation, Vector4};
14///
15/// assert_eq!(Rotation::new(
16///  Vector4::new(0.0, 0.0, 0.0, 0.0),
17///  Vector4::new(-0.0, 0.0, 1.0, 0.0),
18///  Vector4::new(1.0, 1.0, 0.0, 0.0),
19/// ).to_string(), "(0.000000 0.000000 0.000000)(-0.000000 0.000000 1.000000)(1.000000 1.000000 0.000000)");
20/// ```
21///
22/// # Note
23/// - [`Vector4::w`] (4th) isn't used.
24#[repr(C, align(16))]
25#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Display)]
28#[display("{x}{y}{z}")]
29pub struct Rotation {
30    /// # C++ Info
31    /// - name: `x`(ctype: `hkVector4`)
32    /// - offset: `  0`(x86)/`  0`(x86_64)
33    /// - type_size: ` 16`(x86)/` 16`(x86_64)
34    ///
35    /// # NOTE
36    /// - `Vector4::w`(4th) isn't used(always 0.0).
37    #[display("({x:.06} {y:.06} {z:.06})")]
38    pub x: Vector4,
39    /// # C++ Info
40    /// - name: `y`(ctype: `hkVector4`)
41    /// - offset: ` 16`(x86)/` 16`(x86_64)
42    /// - type_size: ` 16`(x86)/` 16`(x86_64)
43    ///
44    /// # NOTE
45    /// - `Vector4::w`(4th) isn't used(always 0.0).
46    #[display("({x:.06} {y:.06} {z:.06})")]
47    pub y: Vector4,
48    /// # C++ Info
49    /// - name: `z`(ctype: `hkVector4`)
50    /// - offset: ` 32`(x86)/` 32`(x86_64)
51    /// - type_size: ` 16`(x86)/` 16`(x86_64)
52    ///
53    /// # NOTE
54    /// - `Vector4::w`(4th) isn't used(always 0.0).
55    #[display("({x:.06} {y:.06} {z:.06})")]
56    pub z: Vector4,
57}
58
59static_assertions::assert_eq_size!(Rotation, [u8; 48]);
60
61impl Rotation {
62    /// Creates a new `Rotation`
63    #[inline]
64    pub const fn new(x: Vector4, y: Vector4, z: Vector4) -> Self {
65        Self { x, y, z }
66    }
67
68    pub fn to_le_bytes(&self) -> [u8; 48] {
69        let mut bytes = [0_u8; 48];
70        bytes[0..16].copy_from_slice(&self.x.to_le_bytes());
71        bytes[16..32].copy_from_slice(&self.y.to_le_bytes());
72        bytes[32..48].copy_from_slice(&self.z.to_le_bytes());
73        bytes
74    }
75
76    pub fn to_be_bytes(&self) -> [u8; 48] {
77        let mut bytes = [0_u8; 48];
78        bytes[0..16].copy_from_slice(&self.x.to_be_bytes());
79        bytes[16..32].copy_from_slice(&self.y.to_be_bytes());
80        bytes[32..48].copy_from_slice(&self.z.to_be_bytes());
81        bytes
82    }
83}