geodesic-wallpaper 1.6.1

Animated Windows desktop wallpaper that draws glowing geodesics flowing over curved 3D surfaces (torus, Klein bottle, pseudosphere and more), rendered with wgpu. Also renders PNGs and GIFs headless.
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Procedural surface generation.
//!
//! Generates surfaces of the form `z = f(x, y)` where `f` is defined by either
//! a built-in procedural function (Perlin noise, fractals) or a user-supplied
//! mathematical expression string parsed at runtime.
//!
//! # Expression syntax
//!
//! The expression parser supports:
//! - Variables: `x`, `y`
//! - Constants: numeric literals, `pi`, `e`
//! - Operators: `+`, `-`, `*`, `/`, `^` (power)
//! - Functions: `sin`, `cos`, `tan`, `sqrt`, `abs`, `exp`, `ln`
//!
//! # Examples
//!
//! ```rust
//! use geodesic_wallpaper::surface::procedural::{ProceduralSurface, ProceduralMode};
//!
//! // Built-in Perlin noise surface.
//! let surf = ProceduralSurface::perlin(2.0, 4, 1.0);
//!
//! // User expression: saddle-like ripple.
//! let expr = ProceduralSurface::from_expression("sin(x) * cos(y) * 0.5").unwrap();
//! ```

use glam::Vec3;
use rand::RngCore;

use crate::surface::Surface;

// ── Expression AST ────────────────────────────────────────────────────────────

/// AST node for the expression parser.
#[derive(Debug, Clone)]
pub enum Expr {
    /// A number literal.
    Lit(f32),
    /// The `x` coordinate.
    X,
    /// The `y` coordinate.
    Y,
    /// `a + b`.
    Add(Box<Expr>, Box<Expr>),
    /// `a - b`.
    Sub(Box<Expr>, Box<Expr>),
    /// `a * b`.
    Mul(Box<Expr>, Box<Expr>),
    /// `a / b` (0 when `|b|` is below 1e-10).
    Div(Box<Expr>, Box<Expr>),
    /// `a ^ b`.
    Pow(Box<Expr>, Box<Expr>),
    /// `-a`.
    Neg(Box<Expr>),
    /// `sin(a)`, radians.
    Sin(Box<Expr>),
    /// `cos(a)`, radians.
    Cos(Box<Expr>),
    /// `tan(a)`, radians.
    Tan(Box<Expr>),
    /// `sqrt(|a|)`.
    Sqrt(Box<Expr>),
    /// `|a|`.
    Abs(Box<Expr>),
    /// `e^a`.
    Exp(Box<Expr>),
    /// Natural log of `|a|` (`ln` and `log` both parse to this).
    Ln(Box<Expr>),
}

impl Expr {
    /// Evaluate the expression at `(x, y)`.
    pub fn eval(&self, x: f32, y: f32) -> f32 {
        match self {
            Expr::Lit(v) => *v,
            Expr::X => x,
            Expr::Y => y,
            Expr::Add(a, b) => a.eval(x, y) + b.eval(x, y),
            Expr::Sub(a, b) => a.eval(x, y) - b.eval(x, y),
            Expr::Mul(a, b) => a.eval(x, y) * b.eval(x, y),
            Expr::Div(a, b) => {
                let denom = b.eval(x, y);
                if denom.abs() < 1e-10 {
                    0.0
                } else {
                    a.eval(x, y) / denom
                }
            }
            Expr::Pow(base, exp) => base.eval(x, y).powf(exp.eval(x, y)),
            Expr::Neg(e) => -e.eval(x, y),
            Expr::Sin(e) => e.eval(x, y).sin(),
            Expr::Cos(e) => e.eval(x, y).cos(),
            Expr::Tan(e) => e.eval(x, y).tan(),
            Expr::Sqrt(e) => e.eval(x, y).abs().sqrt(),
            Expr::Abs(e) => e.eval(x, y).abs(),
            Expr::Exp(e) => e.eval(x, y).exp(),
            Expr::Ln(e) => e.eval(x, y).abs().ln(),
        }
    }
}

// ── Expression parser ─────────────────────────────────────────────────────────

/// Parse a mathematical expression string into an [`Expr`] AST.
///
/// Grammar (recursive descent):
/// ```text
/// expr   = term { ('+' | '-') term }
/// term   = unary { ('*' | '/') unary }
/// unary  = '-' unary | factor
/// factor = base ('^' unary)?
/// base   = number | 'x' | 'y' | 'pi' | 'e' | func '(' expr ')' | '(' expr ')'
/// ```
///
/// Numbers may use scientific notation (`1e-3`). Nesting deeper than 64
/// levels is rejected.
pub fn parse_expression(input: &str) -> Result<Expr, String> {
    let tokens = tokenize(input)?;
    let mut p = Parser {
        tokens: &tokens,
        pos: 0,
        depth: 0,
    };
    let expr = p.expr()?;
    if p.pos < tokens.len() {
        return Err(format!(
            "unexpected token at position {}: {:?}",
            p.pos, tokens[p.pos]
        ));
    }
    Ok(expr)
}

#[derive(Debug, Clone, PartialEq)]
enum Token {
    Num(f32),
    Ident(String),
    Plus,
    Minus,
    Star,
    Slash,
    Caret,
    LParen,
    RParen,
}

fn tokenize(input: &str) -> Result<Vec<Token>, String> {
    let mut tokens = Vec::new();
    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        match chars[i] {
            c if c.is_whitespace() => {
                i += 1;
            }
            '+' => {
                tokens.push(Token::Plus);
                i += 1;
            }
            '-' => {
                tokens.push(Token::Minus);
                i += 1;
            }
            '*' => {
                tokens.push(Token::Star);
                i += 1;
            }
            '/' => {
                tokens.push(Token::Slash);
                i += 1;
            }
            '^' => {
                tokens.push(Token::Caret);
                i += 1;
            }
            '(' => {
                tokens.push(Token::LParen);
                i += 1;
            }
            ')' => {
                tokens.push(Token::RParen);
                i += 1;
            }
            c if c.is_ascii_digit() || c == '.' => {
                let start = i;
                while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '.') {
                    i += 1;
                }
                // Optional exponent: e or E, an optional sign, then digits.
                if i < chars.len() && (chars[i] == 'e' || chars[i] == 'E') {
                    let mut j = i + 1;
                    if j < chars.len() && (chars[j] == '+' || chars[j] == '-') {
                        j += 1;
                    }
                    if j < chars.len() && chars[j].is_ascii_digit() {
                        while j < chars.len() && chars[j].is_ascii_digit() {
                            j += 1;
                        }
                        i = j;
                    }
                }
                let s: String = chars[start..i].iter().collect();
                let v: f32 = s.parse().map_err(|_| format!("invalid number: {s}"))?;
                tokens.push(Token::Num(v));
            }
            c if c.is_alphabetic() || c == '_' => {
                let start = i;
                while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                    i += 1;
                }
                let s: String = chars[start..i].iter().collect();
                tokens.push(Token::Ident(s));
            }
            c => return Err(format!("unexpected character: '{c}'")),
        }
    }
    Ok(tokens)
}

/// Deepest nesting of parentheses, function calls, unary minus and powers
/// accepted, so hostile input cannot overflow the stack.
const MAX_DEPTH: usize = 64;

struct Parser<'t> {
    tokens: &'t [Token],
    pos: usize,
    depth: usize,
}

impl Parser<'_> {
    fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.pos)
    }

    fn nested<T>(&mut self, f: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<T, String> {
        if self.depth >= MAX_DEPTH {
            return Err(format!("expression nested deeper than {MAX_DEPTH} levels"));
        }
        self.depth += 1;
        let r = f(self);
        self.depth -= 1;
        r
    }

    /// `expr = term { ('+' | '-') term }`
    fn expr(&mut self) -> Result<Expr, String> {
        let mut left = self.term()?;
        loop {
            match self.peek() {
                Some(Token::Plus) => {
                    self.pos += 1;
                    left = Expr::Add(Box::new(left), Box::new(self.term()?));
                }
                Some(Token::Minus) => {
                    self.pos += 1;
                    left = Expr::Sub(Box::new(left), Box::new(self.term()?));
                }
                _ => return Ok(left),
            }
        }
    }

    /// `term = unary { ('*' | '/') unary }`
    fn term(&mut self) -> Result<Expr, String> {
        let mut left = self.unary()?;
        loop {
            match self.peek() {
                Some(Token::Star) => {
                    self.pos += 1;
                    left = Expr::Mul(Box::new(left), Box::new(self.unary()?));
                }
                Some(Token::Slash) => {
                    self.pos += 1;
                    left = Expr::Div(Box::new(left), Box::new(self.unary()?));
                }
                _ => return Ok(left),
            }
        }
    }

    /// `unary = '-' unary | factor`: minus binds looser than `^`.
    fn unary(&mut self) -> Result<Expr, String> {
        if self.peek() == Some(&Token::Minus) {
            self.pos += 1;
            let inner = self.nested(|p| p.unary())?;
            return Ok(Expr::Neg(Box::new(inner)));
        }
        self.factor()
    }

    /// `factor = base ('^' unary)?`, right-associative.
    fn factor(&mut self) -> Result<Expr, String> {
        let base = self.base()?;
        if self.peek() == Some(&Token::Caret) {
            self.pos += 1;
            let exp = self.nested(|p| p.unary())?;
            return Ok(Expr::Pow(Box::new(base), Box::new(exp)));
        }
        Ok(base)
    }

    /// `base = number | 'x' | 'y' | 'pi' | 'e' | func '(' expr ')' | '(' expr ')'`
    fn base(&mut self) -> Result<Expr, String> {
        let Some(tok) = self.peek().cloned() else {
            return Err("unexpected end of expression".into());
        };
        self.pos += 1;
        match tok {
            Token::Num(v) => Ok(Expr::Lit(v)),
            Token::Ident(name) => match name.as_str() {
                "x" => Ok(Expr::X),
                "y" => Ok(Expr::Y),
                "pi" => Ok(Expr::Lit(std::f32::consts::PI)),
                "e" => Ok(Expr::Lit(std::f32::consts::E)),
                func => {
                    let make: fn(Box<Expr>) -> Expr = match func {
                        "sin" => Expr::Sin,
                        "cos" => Expr::Cos,
                        "tan" => Expr::Tan,
                        "sqrt" => Expr::Sqrt,
                        "abs" => Expr::Abs,
                        "exp" => Expr::Exp,
                        "ln" | "log" => Expr::Ln,
                        other => return Err(format!("unknown function: '{other}'")),
                    };
                    if self.peek() != Some(&Token::LParen) {
                        return Err(format!("expected '(' after function '{func}'"));
                    }
                    self.pos += 1;
                    let arg = self.nested(|p| p.expr())?;
                    if self.peek() != Some(&Token::RParen) {
                        return Err(format!("expected ')' after argument of '{func}'"));
                    }
                    self.pos += 1;
                    Ok(make(Box::new(arg)))
                }
            },
            Token::LParen => {
                let inner = self.nested(|p| p.expr())?;
                if self.peek() != Some(&Token::RParen) {
                    return Err("expected ')'".into());
                }
                self.pos += 1;
                Ok(inner)
            }
            other => Err(format!("unexpected token: {other:?}")),
        }
    }
}

// ── Perlin noise ──────────────────────────────────────────────────────────────

/// Minimal value-noise implementation (no external dep required).
fn value_noise_2d(x: f32, y: f32, seed: u32) -> f32 {
    fn hash(xi: i32, yi: i32, seed: u32) -> f32 {
        let mut h = seed
            .wrapping_add(xi.unsigned_abs())
            .wrapping_mul(2246822519);
        h ^= yi.unsigned_abs().wrapping_mul(3266489917);
        h = h.wrapping_mul(668265263);
        h ^= h >> 15;
        h = h.wrapping_mul(2246822519);
        h ^= h >> 13;
        (h >> 8) as f32 / 16777215.0 * 2.0 - 1.0
    }
    let xi = x.floor() as i32;
    let yi = y.floor() as i32;
    let fx = x - x.floor();
    let fy = y - y.floor();
    // Smooth step.
    let ux = fx * fx * (3.0 - 2.0 * fx);
    let uy = fy * fy * (3.0 - 2.0 * fy);
    let n00 = hash(xi, yi, seed);
    let n10 = hash(xi + 1, yi, seed);
    let n01 = hash(xi, yi + 1, seed);
    let n11 = hash(xi + 1, yi + 1, seed);
    let bottom = n00 * (1.0 - ux) + n10 * ux;
    let top = n01 * (1.0 - ux) + n11 * ux;
    bottom * (1.0 - uy) + top * uy
}

/// Fractal (fBm) noise: sum of `octaves` noise layers at increasing frequency.
pub fn fbm(x: f32, y: f32, octaves: u32, seed: u32) -> f32 {
    let mut value = 0.0_f32;
    let mut amplitude = 1.0_f32;
    let mut frequency = 1.0_f32;
    let mut max_val = 0.0_f32;
    for oct in 0..octaves {
        value += value_noise_2d(x * frequency, y * frequency, seed.wrapping_add(oct)) * amplitude;
        max_val += amplitude;
        amplitude *= 0.5;
        frequency *= 2.0;
    }
    if max_val > 0.0 {
        value / max_val
    } else {
        0.0
    }
}

// ── Procedural surface ────────────────────────────────────────────────────────

/// Method used to compute `z = f(x, y)`.
#[derive(Clone)]
pub enum ProceduralMode {
    /// Fractal Brownian motion noise.
    Perlin {
        /// Spatial frequency: larger values give smaller hills.
        scale: f32,
        /// Number of noise layers summed (each at double frequency).
        octaves: u32,
        /// Height multiplier applied to the noise.
        amplitude: f32,
        /// Seed that selects the noise pattern.
        seed: u32,
    },
    /// Runtime-parsed mathematical expression.
    Expression(Expr),
}

/// A height-field surface `z = f(x, y)` on a flat `[u_min, u_max] × [v_min, v_max]` domain.
///
/// Implements the full [`Surface`] trait so it can be used with the geodesic
/// integrator and the wgpu renderer.
#[derive(Clone)]
pub struct ProceduralSurface {
    /// How the height is computed.
    pub mode: ProceduralMode,
    /// Smallest `x` (the `u` parameter) of the domain.
    pub u_min: f32,
    /// Largest `x` of the domain.
    pub u_max: f32,
    /// Smallest `y` (the `v` parameter) of the domain.
    pub v_min: f32,
    /// Largest `y` of the domain.
    pub v_max: f32,
    /// Finite difference step for metric / Christoffel computation.
    eps: f32,
}

impl ProceduralSurface {
    /// Create a Perlin / fBm noise surface.
    pub fn perlin(scale: f32, octaves: u32, amplitude: f32) -> Self {
        Self {
            mode: ProceduralMode::Perlin {
                scale,
                octaves,
                amplitude,
                seed: 42,
            },
            u_min: -5.0,
            u_max: 5.0,
            v_min: -5.0,
            v_max: 5.0,
            eps: 1e-3,
        }
    }

    /// Create a surface from a user expression string `"z = f(x, y)"`.
    pub fn from_expression(expr_str: &str) -> Result<Self, String> {
        let expr = parse_expression(expr_str)?;
        Ok(Self {
            mode: ProceduralMode::Expression(expr),
            u_min: -5.0,
            u_max: 5.0,
            v_min: -5.0,
            v_max: 5.0,
            eps: 1e-3,
        })
    }

    /// Evaluate the height function at `(u, v)`.
    pub fn height(&self, u: f32, v: f32) -> f32 {
        // Map u/v to x/y world coords.
        let x = u;
        let y = v;
        match &self.mode {
            ProceduralMode::Perlin {
                scale,
                octaves,
                amplitude,
                seed,
            } => fbm(x * scale, y * scale, *octaves, *seed) * amplitude,
            ProceduralMode::Expression(expr) => {
                let z = expr.eval(x, y);
                if z.is_finite() {
                    z
                } else {
                    0.0
                }
            }
        }
    }

    /// Numeric partial derivatives ∂z/∂u and ∂z/∂v.
    fn dz(&self, u: f32, v: f32) -> (f32, f32) {
        let dzu = (self.height(u + self.eps, v) - self.height(u - self.eps, v)) / (2.0 * self.eps);
        let dzv = (self.height(u, v + self.eps) - self.height(u, v - self.eps)) / (2.0 * self.eps);
        (dzu, dzv)
    }
}

impl Surface for ProceduralSurface {
    fn position(&self, u: f32, v: f32) -> Vec3 {
        Vec3::new(u, v, self.height(u, v))
    }

    fn metric(&self, u: f32, v: f32) -> [[f32; 2]; 2] {
        let (dzu, dzv) = self.dz(u, v);
        // g_uu = 1 + (∂z/∂u)², g_vv = 1 + (∂z/∂v)², g_uv = (∂z/∂u)(∂z/∂v)
        [[1.0 + dzu * dzu, dzu * dzv], [dzu * dzv, 1.0 + dzv * dzv]]
    }

    fn christoffel(&self, u: f32, v: f32) -> [[[f32; 2]; 2]; 2] {
        // Numeric Christoffel symbols via finite differences of the metric.
        let eps = self.eps;
        let guu = |u: f32, v: f32| self.metric(u, v)[0][0];
        let guv = |u: f32, v: f32| self.metric(u, v)[0][1];
        let gvv = |u: f32, v: f32| self.metric(u, v)[1][1];

        let dg_uu_u = (guu(u + eps, v) - guu(u - eps, v)) / (2.0 * eps);
        let dg_uu_v = (guu(u, v + eps) - guu(u, v - eps)) / (2.0 * eps);
        let dg_uv_u = (guv(u + eps, v) - guv(u - eps, v)) / (2.0 * eps);
        let dg_uv_v = (guv(u, v + eps) - guv(u, v - eps)) / (2.0 * eps);
        let dg_vv_u = (gvv(u + eps, v) - gvv(u - eps, v)) / (2.0 * eps);
        let dg_vv_v = (gvv(u, v + eps) - gvv(u, v - eps)) / (2.0 * eps);

        // Metric inverse (2×2).
        let g = self.metric(u, v);
        let det = g[0][0] * g[1][1] - g[0][1] * g[1][0];
        let inv_det = if det.abs() < 1e-10 { 0.0 } else { 1.0 / det };
        let g_inv = [
            [g[1][1] * inv_det, -g[0][1] * inv_det],
            [-g[1][0] * inv_det, g[0][0] * inv_det],
        ];

        // Γ^k_ij = ½ g^{kl} (∂_i g_{lj} + ∂_j g_{li} − ∂_l g_{ij})
        let dg = [
            [[dg_uu_u, dg_uv_u], [dg_uv_u, dg_vv_u]], // ∂_u g_{lj}
            [[dg_uu_v, dg_uv_v], [dg_uv_v, dg_vv_v]], // ∂_v g_{lj}
        ];

        let mut gamma = [[[0.0f32; 2]; 2]; 2];
        for k in 0..2 {
            for i in 0..2 {
                for j in 0..2 {
                    let mut sum = 0.0;
                    for l in 0..2 {
                        sum += g_inv[k][l] * (dg[i][l][j] + dg[j][l][i] - dg[l][i][j]);
                    }
                    gamma[k][i][j] = 0.5 * sum;
                }
            }
        }
        gamma
    }

    fn wrap(&self, u: f32, v: f32) -> (f32, f32) {
        (
            u.clamp(self.u_min, self.u_max),
            v.clamp(self.v_min, self.v_max),
        )
    }

    fn normal(&self, u: f32, v: f32) -> Vec3 {
        let (dzu, dzv) = self.dz(u, v);
        Vec3::new(-dzu, -dzv, 1.0).normalize()
    }

    fn random_position(&self, rng: &mut dyn RngCore) -> (f32, f32) {
        use rand::Rng;
        let u = rng.gen::<f32>() * (self.u_max - self.u_min) + self.u_min;
        let v = rng.gen::<f32>() * (self.v_max - self.v_min) + self.v_min;
        (u, v)
    }

    fn random_tangent(&self, u: f32, v: f32, rng: &mut dyn RngCore) -> (f32, f32) {
        use rand::Rng;
        let theta = rng.gen::<f32>() * std::f32::consts::TAU;
        let g = self.metric(u, v);
        // Unit tangent satisfying g_ij t^i t^j = 1.
        let du = theta.cos();
        let dv = theta.sin();
        let spd = (g[0][0] * du * du + 2.0 * g[0][1] * du * dv + g[1][1] * dv * dv)
            .max(1e-10)
            .sqrt();
        (du / spd, dv / spd)
    }

    fn mesh_vertices(&self, u_steps: u32, v_steps: u32) -> (Vec<[f32; 3]>, Vec<u32>) {
        let mut verts = Vec::new();
        let mut indices = Vec::new();
        let u_steps = u_steps.max(2);
        let v_steps = v_steps.max(2);
        for vi in 0..=v_steps {
            for ui in 0..=u_steps {
                let u = self.u_min + (ui as f32 / u_steps as f32) * (self.u_max - self.u_min);
                let v = self.v_min + (vi as f32 / v_steps as f32) * (self.v_max - self.v_min);
                let p = self.position(u, v);
                verts.push([p.x, p.y, p.z]);
            }
        }
        let row = u_steps + 1;
        for vi in 0..v_steps {
            for ui in 0..u_steps {
                let i00 = vi * row + ui;
                let i10 = i00 + 1;
                let i01 = i00 + row;
                let i11 = i01 + 1;
                indices.extend_from_slice(&[i00, i10, i01, i10, i11, i01]);
            }
        }
        (verts, indices)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    fn ev(src: &str, x: f32, y: f32) -> f32 {
        parse_expression(src).expect("parse").eval(x, y)
    }

    // Unary minus binds looser than `^`: -x^2 is -(x^2), as in maths and
    // most languages. It used to parse as (-x)^2.
    #[test]
    fn bug_unary_minus_vs_power() {
        assert_eq!(ev("-x^2", 3.0, 0.0), -9.0);
        assert_eq!(ev("2^-1", 0.0, 0.0), 0.5);
        assert_eq!(ev("-(x)", 3.0, 0.0), -3.0);
    }

    // Scientific notation used to be split into a number and the constant e.
    #[test]
    fn bug_scientific_notation() {
        assert!((ev("1e-3", 0.0, 0.0) - 0.001).abs() < 1e-9);
        assert!((ev("2.5E2 * x", 2.0, 0.0) - 500.0).abs() < 1e-3);
        // The constant e still works on its own.
        assert!((ev("e", 0.0, 0.0) - std::f32::consts::E).abs() < 1e-6);
    }

    #[test]
    fn bug_deep_nesting_is_error() {
        let deep = format!("{}x{}", "(".repeat(100_000), ")".repeat(100_000));
        assert!(parse_expression(&deep).is_err());
        let minus = format!("{}x", "-".repeat(100_000));
        assert!(parse_expression(&minus).is_err());
        assert_eq!(ev("((x))", 4.0, 0.0), 4.0);
    }

    #[test]
    fn test_expression_parse_and_eval() {
        let expr = parse_expression("sin(x) * cos(y)").unwrap();
        let val = expr.eval(0.0, 0.0);
        assert!((val - 0.0).abs() < 1e-5, "sin(0)*cos(0) should be 0: {val}");

        let expr2 = parse_expression("x^2 + y^2").unwrap();
        let val2 = expr2.eval(3.0, 4.0);
        assert!((val2 - 25.0).abs() < 1e-4, "3^2+4^2 should be 25: {val2}");
    }

    #[test]
    fn test_expression_parse_pi() {
        let expr = parse_expression("sin(pi)").unwrap();
        let val = expr.eval(0.0, 0.0);
        assert!(val.abs() < 1e-5, "sin(pi) should be ~0: {val}");
    }

    #[test]
    fn test_expression_parse_error() {
        assert!(parse_expression("sin(x +").is_err());
        assert!(parse_expression("unknown(x)").is_err());
    }

    #[test]
    fn test_perlin_surface_position_finite() {
        let surf = ProceduralSurface::perlin(1.0, 4, 1.0);
        let p = surf.position(1.0, 2.0);
        assert!(p.x.is_finite() && p.y.is_finite() && p.z.is_finite());
    }

    #[test]
    fn test_expression_surface_height() {
        let surf = ProceduralSurface::from_expression("x^2 - y^2").unwrap();
        let h = surf.height(3.0, 2.0);
        assert!((h - 5.0).abs() < 1e-3, "height at (3,2) should be 5: {h}");
    }

    #[test]
    fn test_metric_positive_definite() {
        let surf = ProceduralSurface::perlin(0.5, 2, 0.5);
        let g = surf.metric(1.0, 1.0);
        let det = g[0][0] * g[1][1] - g[0][1] * g[1][0];
        assert!(det > 0.0, "metric should be positive definite");
    }

    #[test]
    fn test_normal_unit_length() {
        let surf = ProceduralSurface::from_expression("0.5 * sin(x) * cos(y)").unwrap();
        let n = surf.normal(1.0, 1.0);
        let len = n.length();
        assert!(
            (len - 1.0).abs() < 1e-4,
            "normal should be unit length: {len}"
        );
    }

    #[test]
    fn test_mesh_vertices_count() {
        let surf = ProceduralSurface::perlin(1.0, 2, 1.0);
        let (verts, indices) = surf.mesh_vertices(4, 4);
        assert_eq!(verts.len(), 5 * 5, "should have (4+1)^2 vertices");
        assert_eq!(indices.len(), 4 * 4 * 6, "should have 4*4*6 indices");
    }
}