1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::{Basis, Vector3};
use euclid::{default, Point3D, Transform3D, UnknownUnit};

/// 3D Transformation (3x4 matrix) Using basis + origin representation.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Transform {
    /// The basis is a matrix containing 3 Vector3 as its columns: X axis, Y axis, and Z axis.
    /// These vectors can be interpreted as the basis vectors of local coordinate system
    /// traveling with the object.
    pub basis: Basis,
    /// The translation offset of the transform.
    pub origin: Vector3,
}

impl Transform {
    #[doc(hidden)]
    pub fn sys(&self) -> *const sys::godot_transform {
        unsafe {
            std::mem::transmute::<*const Transform, *const sys::godot_transform>(self as *const _)
        }
    }

    #[doc(hidden)]
    pub fn from_sys(c: sys::godot_transform) -> Self {
        unsafe { std::mem::transmute::<sys::godot_transform, Self>(c) }
    }

    pub fn translate(origin: Vector3) -> Transform {
        Transform {
            basis: Basis::identity(),
            origin,
        }
    }

    /// Creates a `Basis` from the rotation and scaling of the provided transform.
    pub fn from_transform(transform: &default::Transform3D<f32>) -> Transform {
        Self::from_typed_transform::<UnknownUnit, UnknownUnit>(transform)
    }

    /// Creates a `Basis` from the rotation and scaling of the provided transform, in `Dst` space.
    pub fn from_typed_transform<Src, Dst>(transform: &Transform3D<f32, Src, Dst>) -> Transform {
        Transform {
            basis: Basis::from_typed_transform(transform),
            origin: transform
                .transform_point3d(Point3D::origin())
                .unwrap_or(Point3D::origin())
                .to_vector()
                .to_untyped(),
        }
    }
}