Skip to main content

rust_css_parser/ast/transform/
rotate.rs

1//! Port of `src/ast/transform/rotate.ts`: `from = from × rotation`, angles in radian.
2
3use super::utils::{multiply_in_place, IDENTITY_MATRIX};
4use super::Matrix;
5
6pub fn rotate3d(angle: f64, x: f64, y: f64, z: f64, from: &mut Matrix) {
7    let mut matrix = IDENTITY_MATRIX;
8    let sc = (angle / 2.0).sin() * (angle / 2.0).cos();
9    let sq = (angle / 2.0).sin() * (angle / 2.0).sin();
10    let norm = (x * x + y * y + z * z).sqrt();
11    let unit = if norm == 0.0 { 0.0 } else { 1.0 / norm };
12    let (x, y, z) = (x * unit, y * unit, z * unit);
13    matrix[0] = 1.0 - 2.0 * (y * y + z * z) * sq;
14    matrix[1] = 2.0 * (x * y * sq + z * sc);
15    matrix[2] = 2.0 * (x * z * sq - y * sc);
16    matrix[4] = 2.0 * (x * y * sq - z * sc);
17    matrix[5] = 1.0 - 2.0 * (x * x + z * z) * sq;
18    matrix[6] = 2.0 * (y * z * sq + x * sc);
19    matrix[8] = 2.0 * (x * z * sq + y * sc);
20    matrix[9] = 2.0 * (y * z * sq - x * sc);
21    matrix[10] = 1.0 - 2.0 * (x * x + y * y) * sq;
22    multiply_in_place(from, &matrix);
23}
24
25pub fn rotate(angle: f64, from: &mut Matrix) {
26    let mut matrix = IDENTITY_MATRIX;
27    matrix[0] = angle.cos();
28    matrix[1] = angle.sin();
29    matrix[4] = -angle.sin();
30    matrix[5] = angle.cos();
31    multiply_in_place(from, &matrix);
32}
33
34pub use rotate as rotate_z;