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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use core::ops::{Mul, Neg};

use num_traits::Float;
use vec2;

/// # Example
/// ```
/// let mut m = mat32::new_identity();
/// let position = [0f32, 0f32];
/// let scale = [1f32, 1f32];
/// let rotation = 0f32;
/// mat32::compose(&mut m, &position, &scale, &rotation);
/// assert_eq!(m, mat32::new_identity());
/// ```
#[inline]
pub fn compose<'out, T>(
    out: &'out mut [T; 6],
    position: &[T; 2],
    scale: &[T; 2],
    rotation: &T,
) -> &'out mut [T; 6]
where
    T: Clone + Float,
    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Neg<Output = T>,
{
    let sx = &scale[0];
    let sy = &scale[1];
    let c = rotation.cos();
    let s = rotation.sin();

    out[0] = &c * sx;
    out[1] = &s * sx;
    out[2] = &-&s * sy;
    out[3] = &c * sy;
    out[4] = position[0].clone();
    out[5] = position[1].clone();
    out
}

/// # Example
/// ```
/// let m = mat32::new_identity();
/// let mut position = [0f32, 0f32];
/// let mut scale = [1f32, 1f32];
/// let mut rotation = 0f32;
/// mat32::decompose(&m, &mut position, &mut scale, &mut rotation);
/// assert_eq!(position, [0f32, 0f32]);
/// assert_eq!(scale, [1f32, 1f32]);
/// assert_eq!(rotation, 0f32);
/// ```
#[inline]
pub fn decompose<T>(out: &[T; 6], position: &mut [T; 2], scale: &mut [T; 2], rotation: &mut T)
where
    T: Clone + Float,
    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Neg<Output = T>,
{
    let sx = vec2::len_values(&out[0], &out[1]);
    let sy = vec2::len_values(&out[2], &out[3]);

    position[0] = out[4].clone();
    position[1] = out[5].clone();

    scale[0] = sx;
    scale[1] = sy;

    *rotation = out[1].atan2(out[0].clone());
}