ranim_core/traits/transform/
rotate.rs

1use glam::DVec3;
2
3use crate::traits::Discard;
4
5/// Rotating operations.
6///
7/// This trait is automatically implemented for [`DVec3`] and `[T]` where `T: RotateTransform`.
8pub trait RotateTransform {
9    /// Rotate the item by a given angle about a given axis.
10    fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self;
11    /// Rotate the item by a given angle about the X axis.
12    fn rotate_on_x(&mut self, angle: f64) -> &mut Self {
13        self.rotate_on_axis(DVec3::X, angle)
14    }
15    /// Rotate the item by a given angle about the Y axis.
16    fn rotate_on_y(&mut self, angle: f64) -> &mut Self {
17        self.rotate_on_axis(DVec3::Y, angle)
18    }
19    /// Rotate the item by a given angle about the Z axis.
20    fn rotate_on_z(&mut self, angle: f64) -> &mut Self {
21        self.rotate_on_axis(DVec3::Z, angle)
22    }
23}
24
25impl RotateTransform for DVec3 {
26    fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
27        *self = DVec3::rotate_axis(*self, axis, angle);
28        self
29    }
30}
31
32impl<T: RotateTransform> RotateTransform for [T] {
33    fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
34        self.iter_mut()
35            .for_each(|x| x.rotate_on_axis(axis, angle).discard());
36        self
37    }
38}
39
40impl<T: RotateTransform> RotateTransform for Vec<T> {
41    fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
42        self.as_mut_slice().rotate_on_axis(axis, angle);
43        self
44    }
45}