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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use bevy::math::Vec3;
use bevy::render::mesh::{Indices, Mesh, PrimitiveTopology};
use bevy::render::render_asset::RenderAssetUsages;

/// A 2D or 3D mesh (`bevy::render::mesh::Mesh`) generated from a single-variable function
/// `f(f32) -> f32`.
#[derive(Debug, Clone, Copy)]
pub struct SingleVariableFunctionMesh {
    /// The function to be used as the upper half of the generated polygon.
    /// The function will be mirrored to the x-axis to generate the lower half of the polygon.
    /// If the mesh is 3D (`relative_height` > 0.0), the function will also be applied to the
    /// side vertices. Default = squircle.
    pub f: fn(f32) -> f32,
    /// `f` starts here. Together with `x_end`, this determines the size of the mesh.
    /// Must be lower than `x_end`. Default = -1.0.
    pub x_start: f32,
    /// `f` ends here. Together with `x_start`, this determines the size of the mesh.
    /// Must be bigger than `x_start`. Default = 1.0.
    pub x_end: f32,
    /// The amount of vertices that are used for each upper half of the polygon.
    /// Should be at least 3. There will be (n - 2) * (2 * n - 2) + 2 vertices in total
    /// if `relative_height` > 0.0.  Default = 30.0.
    pub vertices: usize,
    /// If `relative_height` is 0.0, then the mesh is a 2D poglygon without height. If 1.0,
    /// the mesh is fully 3D without any bigger flat surface. Default = 0.1.
    pub relative_height: f32,
}

impl Default for SingleVariableFunctionMesh {
    fn default() -> Self {
        SingleVariableFunctionMesh {
            f: squircle,
            x_start: -1.0,
            x_end: 1.0,
            vertices: 30,
            relative_height: 0.1,
        }
    }
}

impl From<SingleVariableFunctionMesh> for Mesh {
    fn from(mathfunction: SingleVariableFunctionMesh) -> Self {
        debug_assert!(0.0 <= mathfunction.relative_height && mathfunction.relative_height <= 1.0);
        debug_assert!(mathfunction.x_start < mathfunction.x_end);
        let (ring, maximum) = calculate_ring_of_vertices(
            mathfunction.f,
            mathfunction.x_start,
            mathfunction.x_end,
            mathfunction.vertices,
        );
        let width_maximum = mathfunction.x_end - mathfunction.x_start;
        let amount = ring.len();
        let mut amount_layers = (ring.len() - 2) / 2;
        if mathfunction.relative_height == 0.0 {
            amount_layers = 1;
        }

        // Create vertices.
        let mut vertices: Vec<([f32; 3], [f32; 3], [f32; 2])> =
            Vec::with_capacity(amount * amount_layers + 2);
        vertices.push((
            [
                0.0,
                mathfunction.x_start * mathfunction.relative_height,
                0.0,
            ],
            [0.0, -1.0, 0.0],
            [0.5, 0.5],
        ));
        if mathfunction.relative_height >= 0.0 {
            for i in 0..amount_layers {
                for j in 0..amount {
                    let (mut x, mut z) = (ring[j].x, ring[j].y);
                    (x, z) = (
                        x.signum()
                            * (x.abs()
                                * (ring[i].y * mathfunction.relative_height
                                    + maximum * (1.0 - mathfunction.relative_height))),
                        z.signum()
                            * (z.abs()
                                * (ring[i].y * mathfunction.relative_height
                                    + maximum * (1.0 - mathfunction.relative_height))),
                    );
                    let y = ring[i].x * mathfunction.relative_height;

                    let mut normal_horizontally =
                        Vec3::new(-ring[j].slope_in_percentage.tan(), 0.0, 1.0).normalize();
                    if j >= amount / 2 {
                        normal_horizontally[2] = -normal_horizontally[2];
                    }
                    let normal_vertical =
                        Vec3::new(1.0, -ring[i].slope_in_percentage.tan(), 1.0).normalize();
                    let mut normals = [
                        normal_horizontally[0] / 3.0 * 2.0,
                        normal_vertical[1],
                        normal_horizontally[2] / 3.0 * 2.0,
                    ];
                    if amount_layers == 1 {
                        normals = [0.0, 1.0, 0.0];
                    }

                    let realtive_texture_size = 1.0 - (y / maximum.abs());
                    let uv_x =
                        (x * realtive_texture_size + mathfunction.x_start.abs()) / width_maximum;
                    let uv_y = (z * realtive_texture_size + maximum) / (maximum * 2.0);
                    vertices.push(([x, y, z], normals, [uv_x, uv_y]));
                }
            }
        }
        vertices.push((
            [0.0, mathfunction.x_end * mathfunction.relative_height, 0.0],
            [0.0, 1.0, 0.0],
            [0.5, 0.5],
        ));

        // Create faces (indeces).
        let mut indeces: Vec<u32> = Vec::with_capacity((amount * amount_layers + 2) * 3);
        for i in 0..amount {
            if amount_layers > 1 {
                indeces.append(&mut vec![
                    ((i + 1) % amount + 1).try_into().unwrap(),
                    (i + 1).try_into().unwrap(),
                    0,
                ]);
            }
            indeces.append(&mut vec![
                ((amount_layers - 1) * amount + i + 1).try_into().unwrap(),
                ((amount_layers - 1) * amount + (i + 1) % amount + 1)
                    .try_into()
                    .unwrap(),
                (amount_layers * amount + 1).try_into().unwrap(),
            ]);
        }
        for segment in 1..amount_layers {
            for i in 0..amount {
                let tl = (segment * amount + i + 1) as u32;
                let mut tr = (segment * amount + i + 2) as u32;
                let bl = ((segment - 1) * amount + i + 1) as u32;
                let mut br = ((segment - 1) * amount + i + 2) as u32;
                if i == amount - 1 {
                    tr = (segment * amount + 1) as u32;
                    br = ((segment - 1) * amount + 1) as u32;
                }
                indeces.append(&mut vec![br, tr, tl]);
                indeces.append(&mut vec![bl, br, tl]);
            }
        }

        let positions: Vec<_> = vertices.iter().map(|(p, _, _)| *p).collect();
        let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();
        let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();
        let mut mesh = Mesh::new(
            PrimitiveTopology::TriangleList,
            RenderAssetUsages::default(),
        );
        mesh.insert_indices(Indices::U32(indeces));
        mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
        mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
        mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
        mesh
    }
}

/// An example for a single-variable function.
#[allow(dead_code)]
pub fn squircle(x: f32) -> f32 {
    (1.0 - (x).abs().powf(4.0)).powf(0.25)
}

/// An example for a single-variable function.
#[allow(dead_code)]
pub fn circle(x: f32) -> f32 {
    (1.0 - x.powf(2.0)).powf(0.5)
}

#[derive(Copy, Clone, Debug)]
struct Position {
    x: f32,
    y: f32,
    slope_in_percentage: f32,
}

fn calculate_ring_of_vertices(
    f: fn(f32) -> f32,
    x_start: f32,
    x_end: f32,
    vertices: usize,
) -> (Vec<Position>, f32) {
    let delta = 0.000001;
    let start = Position {
        x: x_start,
        y: f(x_start),
        slope_in_percentage: ((f(x_start + delta) - f(x_start)) / (delta)).atan(),
    };
    let end = Position {
        x: x_end,
        y: f(x_end),
        slope_in_percentage: ((f(x_end) - f(x_end - delta)) / (delta)).atan(),
    };
    let mut vec: Vec<Position> = Vec::with_capacity(vertices);
    let mut maximum = 0.0;
    vec.push(start);
    vec.push(end);
    for _ in 2..vertices {
        let (mut index, mut max_slope_difference, mut max_x_difference) = (1, 0.0, 0.0);
        for j in 1..vec.len() {
            let new_x = vec[j - 1].x + (vec[j].x - vec[j - 1].x) / 2.0;
            let new_m = ((f(new_x + delta) - f(new_x)) / (delta)).atan();
            let x_difference = vec[j].x - vec[j - 1].x;
            let slope_difference = (new_m - vec[j].slope_in_percentage).abs()
                + (new_m - vec[j - 1].slope_in_percentage).abs();
            if slope_difference > max_slope_difference
                || (slope_difference == max_slope_difference && x_difference > max_x_difference)
            {
                (index, max_slope_difference, max_x_difference) =
                    (j, slope_difference, x_difference);
            }
        }
        let new_x = vec[index - 1].x + (vec[index].x - vec[index - 1].x) / 2.0;
        vec.insert(
            index,
            Position {
                x: new_x,
                y: f(new_x),
                slope_in_percentage: ((f(new_x + delta) - f(new_x)) / (delta)).atan(),
            },
        );
        if f(new_x) > maximum {
            maximum = f(new_x);
        }
    }
    let mut lower_half = vec.clone();
    if f(lower_half[0].x) != 0.0 {
        lower_half.remove(0);
    }
    lower_half.reverse();
    if f(lower_half[0].x) != 0.0 {
        lower_half.remove(0);
    }
    for vertex in &lower_half {
        vec.push(Position {
            x: vertex.x,
            y: -vertex.y,
            slope_in_percentage: vertex.slope_in_percentage,
        });
    }
    (vec, maximum)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn square(_x: f32) -> f32 {
        1.0
    }

    #[test]
    fn test_amount_of_vertices() {
        let square_2d_mesh: Mesh = SingleVariableFunctionMesh {
            f: square,
            relative_height: 0.0,
            x_start: -1.0,
            x_end: 1.0,
            vertices: 20,
        }
        .into();
        let circle_2d_mesh: Mesh = SingleVariableFunctionMesh {
            f: circle,
            relative_height: 0.0,
            x_start: -1.0,
            x_end: 1.0,
            vertices: 20,
        }
        .into();
        assert_eq!(
            square_2d_mesh.count_vertices(),
            circle_2d_mesh.count_vertices() - 2
        );
    }
}