havok_types/math/
transform.rs

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