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
68
69
70
71
72
73
//! Rotate a distance field.
use rust_gpu_bridge::prelude::{Quat, Vec2, Vec3};
use type_fields::Field;

use crate::prelude::{Distance, Operator, SignedDistanceField, SignedDistanceOperator};

/// Rotate a distance field.
#[derive(Debug, Default, Copy, Clone, PartialEq, Field)]
pub struct Rotate2dOp {
    pub angle: f32,
}

impl SignedDistanceOperator<Vec2, Distance> for Rotate2dOp {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec2) -> Distance
    where
        Sdf: SignedDistanceField<Vec2, Distance>,
    {
        sdf.evaluate(Vec2::from_angle(-self.angle).rotate(p))
    }
}

/// Rotate a 3D distance field.
pub type Rotate2d<Sdf> = Operator<Rotate2dOp, Sdf>;

impl<Sdf> Rotate2d<Sdf> {
    pub fn angle(&mut self) -> &mut f32 {
        &mut self.op.angle
    }
}

/// Rotate a distance field.
#[derive(Debug, Default, Copy, Clone, PartialEq, Field)]
pub struct Rotate3dOp {
    pub rotation: Quat,
}

impl SignedDistanceOperator<Vec3, Distance> for Rotate3dOp {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec3) -> Distance
    where
        Sdf: SignedDistanceField<Vec3, Distance>,
    {
        sdf.evaluate(self.rotation.inverse() * p)
    }
}

/// Rotate a distance field.
pub type Rotate3d<Sdf> = Operator<Rotate3dOp, Sdf>;

impl<Sdf> Rotate3d<Sdf> {
    pub fn rotation(&mut self) -> &mut Quat {
        &mut self.op.rotation
    }
}

#[cfg(test)]
pub mod test {
    use rust_gpu_bridge::prelude::Quat;
    use type_fields::field::Field;

    use crate::signed_distance_field::shapes::composite::{Cube, Square};

    use super::{Rotate2d, Rotate3d};

    #[test]
    fn test_rotate_2d() {
        Rotate2d::<Square>::default().with(Rotate2d::angle, f32::default());
    }

    #[test]
    fn test_rotate_3d() {
        Rotate3d::<Cube>::default().with(Rotate3d::rotation, Quat::default());
    }
}