havok_types/math/
matrix4.rs

1use crate::Vector4;
2use parse_display::Display;
3
4/// # Matrix4x4
5///
6/// # C++ Info
7/// - name: `hkMatrix4`
8/// - type_size: ` 64`(x86)/` 64`(x86_64)
9/// - align: ` 16`(x86)/` 16`(x86_64)
10///
11/// # XML representation
12/// ```xml
13/// <hkparam>(0.000000 0.000000 0.000000 0.000000)(-0.000000 0.000000 -0.000000 1.000000)(1.000000 1.000000 1.000000 0.000000)(1.000000 1.000000 1.000000 0.000000)</hkparam>
14/// ```
15#[repr(C, align(16))]
16#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Display)]
19#[display("{x}{y}{z}{w}")]
20pub struct Matrix4 {
21    /// # C++ Info
22    /// - name: `x`(ctype: `hkVector4`)
23    /// - offset: `  0`(x86)/`  0`(x86_64)
24    /// - type_size: ` 16`(x86)/` 16`(x86_64)
25    pub x: Vector4,
26    /// # C++ Info
27    /// - name: `y`(ctype: `hkVector4`)
28    /// - offset: ` 16`(x86)/` 16`(x86_64)
29    /// - type_size: ` 16`(x86)/` 16`(x86_64)
30    pub y: Vector4,
31    /// # C++ Info
32    /// - name: `z`(ctype: `hkVector4`)
33    /// - offset: ` 32`(x86)/` 32`(x86_64)
34    /// - type_size: ` 16`(x86)/` 16`(x86_64)
35    pub z: Vector4,
36    /// # C++ Info
37    /// - name: `w`(ctype: `hkVector4`)
38    /// - offset: ` 48`(x86)/` 48`(x86_64)
39    /// - type_size: ` 16`(x86)/` 16`(x86_64)
40    pub w: Vector4,
41}
42
43static_assertions::assert_eq_size!(Matrix4, [u8; 64]);
44
45impl Matrix4 {
46    /// Creates a new `Matrix4`
47    #[inline]
48    pub const fn new(x: Vector4, y: Vector4, z: Vector4, w: Vector4) -> Self {
49        Self { x, y, z, w }
50    }
51
52    pub fn to_le_bytes(&self) -> [u8; 64] {
53        let mut bytes = [0; 64];
54        bytes[0..16].copy_from_slice(&self.x.to_le_bytes());
55        bytes[16..32].copy_from_slice(&self.y.to_le_bytes());
56        bytes[32..48].copy_from_slice(&self.z.to_le_bytes());
57        bytes[48..64].copy_from_slice(&self.w.to_le_bytes());
58        bytes
59    }
60
61    pub fn to_be_bytes(&self) -> [u8; 64] {
62        let mut bytes = [0_u8; 64];
63        bytes[0..16].copy_from_slice(&self.x.to_be_bytes());
64        bytes[16..32].copy_from_slice(&self.y.to_be_bytes());
65        bytes[32..48].copy_from_slice(&self.z.to_be_bytes());
66        bytes[48..64].copy_from_slice(&self.w.to_be_bytes());
67        bytes
68    }
69}