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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Stretch a shape along an arbitrary axis, preserving exterior geometry as caps.

use rust_gpu_bridge::prelude::{Vec2, Vec3};
use type_fields::Field;

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

/// Extrude a shape infinitely along an arbitrary axis.
#[derive(Debug, Copy, Clone, PartialEq, Field)]
pub struct StretchInfiniteOp<Dim> {
    pub dir: Dim,
}

impl Default for StretchInfiniteOp<Vec2> {
    fn default() -> Self {
        StretchInfiniteOp { dir: Vec2::X }
    }
}

impl Default for StretchInfiniteOp<Vec3> {
    fn default() -> Self {
        StretchInfiniteOp { dir: Vec3::X }
    }
}

impl SignedDistanceOperator<f32, Distance> for StretchInfiniteOp<f32> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: f32) -> Distance
    where
        Sdf: SignedDistanceField<f32, Distance>,
    {
        assert!(
            self.dir.abs() == 1.0,
            "ExtrudeInfiniteOp dir must be normalized"
        );
        let q = p - p * self.dir * self.dir;
        sdf.evaluate(q)
    }
}

impl SignedDistanceOperator<Vec2, Distance> for StretchInfiniteOp<Vec2> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec2) -> Distance
    where
        Sdf: SignedDistanceField<Vec2, Distance>,
    {
        assert!(
            self.dir.is_normalized(),
            "ExtrudeInfiniteOp dir must be normalized"
        );
        let q = p - p.dot(self.dir) * self.dir;
        sdf.evaluate(q)
    }
}

impl SignedDistanceOperator<Vec3, Distance> for StretchInfiniteOp<Vec3> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec3) -> Distance
    where
        Sdf: SignedDistanceField<Vec3, Distance>,
    {
        assert!(
            self.dir.is_normalized(),
            "ExtrudeInfiniteOp dir must be normalized"
        );
        let q = p - p.dot(self.dir) * self.dir;
        sdf.evaluate(q)
    }
}

/// Extrude a shape infinitely along an arbitrary axis.
pub type StretchInfinite<Dim, Sdf> = Operator<StretchInfiniteOp<Dim>, Sdf>;

impl<Dim, Sdf> StretchInfinite<Dim, Sdf> {
    pub fn dir(&mut self) -> &mut Dim {
        &mut self.op.dir
    }
}

/// Extrude a shape by an arbitrary distance along an arbitrary axis, preserving exterior geometry as caps.
#[derive(Debug, Copy, Clone, PartialEq, Field)]
pub struct StretchDistOp<Dim> {
    pub dir: Dim,
    pub dist: f32,
}

impl Default for StretchDistOp<f32> {
    fn default() -> Self {
        StretchDistOp {
            dir: 1.0,
            dist: 1.0,
        }
    }
}

impl Default for StretchDistOp<Vec2> {
    fn default() -> Self {
        StretchDistOp {
            dir: Vec2::X,
            dist: 1.0,
        }
    }
}

impl Default for StretchDistOp<Vec3> {
    fn default() -> Self {
        StretchDistOp {
            dir: Vec3::X,
            dist: 1.0,
        }
    }
}

impl SignedDistanceOperator<f32, Distance> for StretchDistOp<f32> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: f32) -> Distance
    where
        Sdf: SignedDistanceField<f32, Distance>,
    {
        assert!(
            self.dir.abs() == 1.0,
            "ExtrudeDistOp dir must be normalized"
        );
        let q = p - ((p * self.dir).clamp(-self.dist, self.dist) * self.dir);
        sdf.evaluate(q)
    }
}

impl SignedDistanceOperator<Vec2, Distance> for StretchDistOp<Vec2> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec2) -> Distance
    where
        Sdf: SignedDistanceField<Vec2, Distance>,
    {
        assert!(
            self.dir.is_normalized(),
            "ExtrudeDistOp dir must be normalized"
        );
        let q = p - (p.dot(self.dir).clamp(-self.dist, self.dist) * self.dir);
        sdf.evaluate(q)
    }
}

impl SignedDistanceOperator<Vec3, Distance> for StretchDistOp<Vec3> {
    fn operator<Sdf>(&self, sdf: &Sdf, p: Vec3) -> Distance
    where
        Sdf: SignedDistanceField<Vec3, Distance>,
    {
        assert!(
            self.dir.is_normalized(),
            "ExtrudeDistOp dir must be normalized"
        );
        let q = p - (p.dot(self.dir).clamp(-self.dist, self.dist) * self.dir);
        sdf.evaluate(q)
    }
}

/// Extrude a shape by an arbitrary distance along an arbitrary axis, preserving exterior geometry as caps.
pub type StretchDist<Dim, Sdf> = Operator<StretchDistOp<Dim>, Sdf>;

impl<Dim, Sdf> StretchDist<Dim, Sdf> {
    pub fn dir(&mut self) -> &mut Dim {
        &mut self.op.dir
    }

    pub fn dist(&mut self) -> &mut f32 {
        &mut self.op.dist
    }
}

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

    use crate::signed_distance_field::shapes::composite::Cube;

    use super::{StretchDist, StretchInfinite};

    #[test]
    fn test_stretch_infinite() {
        StretchInfinite::<_, Cube>::default().with(StretchInfinite::dir, Vec3::default());
    }

    #[test]
    fn test_stretch_dist() {
        StretchDist::<_, Cube>::default()
            .with(StretchDist::dir, Vec3::default())
            .with(StretchDist::dist, f32::default());
    }
}