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
//! User-defined mathematical surfaces via config.toml expressions.
//!
//! Allows users to specify a surface by providing a mathematical expression
//! for `z` as a function of `(x, y)` in their `config.toml`:
//!
//! ```toml
//! [surface.custom]
//! z_expr = "sin(x) * cos(y) * exp(-0.1 * (x*x + y*y))"
//! x_range = [-5.0, 5.0]
//! y_range = [-5.0, 5.0]
//! ```
//!
//! The expression is parsed at startup into an AST and evaluated at runtime
//! without any external crate dependency — the parser is a hand-written
//! recursive descent parser supporting:
//!
//! - Arithmetic: `+`, `-`, `*`, `/`, unary `-`
//! - Exponentiation: `^` or `**`
//! - Parentheses
//! - Variables: `x`, `y`
//! - Constants: `pi`, `e`
//! - Functions: `sin`, `cos`, `tan`, `exp`, `ln`, `log`, `sqrt`, `abs`,
//!   `sinh`, `cosh`, `tanh`, `asin`, `acos`, `atan`, `atan2(y,x)`,
//!   `sign`, `floor`, `ceil`
//!
//! ## Surface protocol
//!
//! [`UserDefinedSurface`] implements the same interface as the built-in
//! surfaces: it provides a `sample(u, v)` method returning `[f32; 3]` in
//! world space, and `normal(u, v)` returning the surface normal via central
//! finite differences.
//!
//! ## Usage
//!
//! ```rust
//! use geodesic_wallpaper::surface::user_defined::{UserDefinedSurface, UserSurfaceConfig};
//!
//! let cfg = UserSurfaceConfig {
//!     z_expr: "sin(x) * cos(y)".to_string(),
//!     x_range: [-3.14, 3.14],
//!     y_range: [-3.14, 3.14],
//!     z_scale: 1.0,
//! };
//! let surface = UserDefinedSurface::new(cfg).unwrap();
//! let pos = surface.sample(0.5, 0.5);
//! assert!(pos[2].is_finite());
//! ```

use std::fmt;

// ── public types ──────────────────────────────────────────────────────────────

/// Configuration for a user-defined surface loaded from `config.toml`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct UserSurfaceConfig {
    /// Mathematical expression for `z(x, y)`.
    pub z_expr: String,
    /// Range `[x_min, x_max]` mapped from parametric `u ∈ [0, 1]`.
    pub x_range: [f64; 2],
    /// Range `[y_min, y_max]` mapped from parametric `v ∈ [0, 1]`.
    pub y_range: [f64; 2],
    /// Uniform scale applied to the `z` output.
    pub z_scale: f64,
}

impl Default for UserSurfaceConfig {
    fn default() -> Self {
        Self {
            z_expr: "sin(x) * cos(y) * exp(-0.1 * (x*x + y*y))".to_string(),
            x_range: [-5.0, 5.0],
            y_range: [-5.0, 5.0],
            z_scale: 1.0,
        }
    }
}

/// Error type for expression parsing failures.
#[derive(Debug)]
pub struct ParseError(pub String);

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "parse error: {}", self.0)
    }
}

/// A surface defined by a user-provided mathematical expression `z(x, y)`.
pub struct UserDefinedSurface {
    cfg: UserSurfaceConfig,
    ast: Expr,
}

impl UserDefinedSurface {
    /// Parse the expression in `cfg.z_expr` and construct the surface.
    pub fn new(cfg: UserSurfaceConfig) -> Result<Self, ParseError> {
        let ast = parse_expr(&cfg.z_expr)?;
        Ok(Self { cfg, ast })
    }

    /// Sample the surface at parametric coordinates `(u, v) ∈ [0, 1]²`.
    ///
    /// Maps `u → x` over `x_range` and `v → y` over `y_range`, then
    /// evaluates `z(x, y)`.  Returns `[x, y, z]` in world space.
    pub fn sample(&self, u: f32, v: f32) -> [f32; 3] {
        let x = self.cfg.x_range[0] + u as f64 * (self.cfg.x_range[1] - self.cfg.x_range[0]);
        let y = self.cfg.y_range[0] + v as f64 * (self.cfg.y_range[1] - self.cfg.y_range[0]);
        let z = eval(&self.ast, x, y) * self.cfg.z_scale;
        [x as f32, y as f32, z as f32]
    }

    /// Evaluate the expression directly at world coordinates `(x, y)`.
    pub fn eval_at(&self, x: f64, y: f64) -> f64 {
        eval(&self.ast, x, y) * self.cfg.z_scale
    }

    /// Compute the surface normal at `(u, v)` via central finite differences.
    pub fn normal(&self, u: f32, v: f32) -> [f32; 3] {
        let eps = 1e-4_f32;
        let p = self.sample(u, v);
        let px = self.sample(u + eps, v);
        let py = self.sample(u, v + eps);
        let tx = [px[0] - p[0], px[1] - p[1], px[2] - p[2]];
        let ty = [py[0] - p[0], py[1] - p[1], py[2] - p[2]];
        // Cross product tx × ty.
        let nx = tx[1] * ty[2] - tx[2] * ty[1];
        let ny = tx[2] * ty[0] - tx[0] * ty[2];
        let nz = tx[0] * ty[1] - tx[1] * ty[0];
        let len = (nx * nx + ny * ny + nz * nz).sqrt().max(1e-8);
        [nx / len, ny / len, nz / len]
    }

    /// The parsed expression as a debug string (for display in the TUI).
    pub fn expression(&self) -> &str {
        &self.cfg.z_expr
    }
}

// ── AST ───────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
enum Expr {
    Num(f64),
    X,
    Y,
    Pi,
    E,
    Neg(Box<Expr>),
    Add(Box<Expr>, Box<Expr>),
    Sub(Box<Expr>, Box<Expr>),
    Mul(Box<Expr>, Box<Expr>),
    Div(Box<Expr>, Box<Expr>),
    Pow(Box<Expr>, Box<Expr>),
    Func1(String, Box<Expr>),
    Func2(String, Box<Expr>, Box<Expr>),
}

fn eval(e: &Expr, x: f64, y: f64) -> f64 {
    match e {
        Expr::Num(n) => *n,
        Expr::X => x,
        Expr::Y => y,
        Expr::Pi => std::f64::consts::PI,
        Expr::E => std::f64::consts::E,
        Expr::Neg(a) => -eval(a, x, y),
        Expr::Add(a, b) => eval(a, x, y) + eval(b, x, y),
        Expr::Sub(a, b) => eval(a, x, y) - eval(b, x, y),
        Expr::Mul(a, b) => eval(a, x, y) * eval(b, x, y),
        Expr::Div(a, b) => {
            let d = eval(b, x, y);
            if d.abs() < 1e-300 {
                f64::NAN
            } else {
                eval(a, x, y) / d
            }
        }
        Expr::Pow(a, b) => eval(a, x, y).powf(eval(b, x, y)),
        Expr::Func1(name, a) => {
            let v = eval(a, x, y);
            match name.as_str() {
                "sin" => v.sin(),
                "cos" => v.cos(),
                "tan" => v.tan(),
                "exp" => v.exp(),
                "ln" | "log" => v.ln(),
                "sqrt" => v.sqrt(),
                "abs" => v.abs(),
                "sinh" => v.sinh(),
                "cosh" => v.cosh(),
                "tanh" => v.tanh(),
                "asin" => v.asin(),
                "acos" => v.acos(),
                "atan" => v.atan(),
                "sign" => v.signum(),
                "floor" => v.floor(),
                "ceil" => v.ceil(),
                _ => f64::NAN,
            }
        }
        Expr::Func2(name, a, b) => {
            let va = eval(a, x, y);
            let vb = eval(b, x, y);
            match name.as_str() {
                "atan2" => va.atan2(vb),
                "pow" => va.powf(vb),
                _ => f64::NAN,
            }
        }
    }
}

// ── Parser ────────────────────────────────────────────────────────────────────

/// Deepest nesting of parentheses, function calls and powers accepted.
/// Deeper input is rejected so a hostile config cannot overflow the stack.
const MAX_DEPTH: usize = 64;

/// One-argument functions understood by the parser.
const FUNCS1: &[&str] = &[
    "sin", "cos", "tan", "exp", "ln", "log", "sqrt", "abs", "sinh", "cosh", "tanh", "asin", "acos",
    "atan", "sign", "floor", "ceil",
];
/// Two-argument functions understood by the parser.
const FUNCS2: &[&str] = &["atan2", "pow"];

struct Parser<'a> {
    input: &'a [u8],
    pos: usize,
    depth: usize,
}

impl<'a> Parser<'a> {
    fn new(s: &'a str) -> Self {
        Self {
            input: s.as_bytes(),
            pos: 0,
            depth: 0,
        }
    }

    fn peek(&self) -> Option<u8> {
        self.input.get(self.pos).copied()
    }

    fn skip_ws(&mut self) {
        while matches!(self.peek(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
            self.pos += 1;
        }
    }

    fn consume(&mut self) -> Option<u8> {
        let c = self.peek()?;
        self.pos += 1;
        Some(c)
    }

    fn expect(&mut self, ch: u8) -> Result<(), ParseError> {
        self.skip_ws();
        match self.peek() {
            Some(c) if c == ch => {
                self.pos += 1;
                Ok(())
            }
            other => Err(ParseError(format!(
                "expected '{}', got {:?}",
                ch as char,
                other.map(|c| c as char)
            ))),
        }
    }

    fn parse_expr(&mut self) -> Result<Expr, ParseError> {
        self.parse_add()
    }

    /// Run `f` one nesting level deeper, failing past [`MAX_DEPTH`].
    fn nested<T>(
        &mut self,
        f: impl FnOnce(&mut Self) -> Result<T, ParseError>,
    ) -> Result<T, ParseError> {
        if self.depth >= MAX_DEPTH {
            return Err(ParseError(format!(
                "expression nested deeper than {MAX_DEPTH} levels"
            )));
        }
        self.depth += 1;
        let r = f(self);
        self.depth -= 1;
        r
    }

    fn parse_add(&mut self) -> Result<Expr, ParseError> {
        let mut left = self.parse_mul()?;
        loop {
            self.skip_ws();
            match self.peek() {
                Some(b'+') => {
                    self.pos += 1;
                    left = Expr::Add(Box::new(left), Box::new(self.parse_mul()?));
                }
                Some(b'-') => {
                    self.pos += 1;
                    left = Expr::Sub(Box::new(left), Box::new(self.parse_mul()?));
                }
                _ => break,
            }
        }
        Ok(left)
    }

    fn parse_mul(&mut self) -> Result<Expr, ParseError> {
        let mut left = self.parse_unary()?;
        loop {
            self.skip_ws();
            match self.peek() {
                Some(b'*') => {
                    // `**` (power) is consumed by `parse_pow`, so this is `*`.
                    self.pos += 1;
                    left = Expr::Mul(Box::new(left), Box::new(self.parse_unary()?));
                }
                Some(b'/') => {
                    self.pos += 1;
                    left = Expr::Div(Box::new(left), Box::new(self.parse_unary()?));
                }
                _ => break,
            }
        }
        Ok(left)
    }

    fn parse_unary(&mut self) -> Result<Expr, ParseError> {
        self.skip_ws();
        if self.peek() == Some(b'-') {
            self.pos += 1;
            return Ok(Expr::Neg(Box::new(self.parse_pow()?)));
        }
        if self.peek() == Some(b'+') {
            self.pos += 1;
        }
        self.parse_pow()
    }

    /// `primary (('^' | '**') unary)?`, right-associative, so `2^3^2` and
    /// `2**3**2` are both 512 and `-x**2` is `-(x**2)`.
    fn parse_pow(&mut self) -> Result<Expr, ParseError> {
        let base = self.parse_primary()?;
        self.skip_ws();
        let op_len = match (self.peek(), self.input.get(self.pos + 1)) {
            (Some(b'^'), _) => 1,
            (Some(b'*'), Some(b'*')) => 2,
            _ => 0,
        };
        if op_len > 0 {
            self.pos += op_len;
            let exp = self.nested(|p| p.parse_unary())?;
            return Ok(Expr::Pow(Box::new(base), Box::new(exp)));
        }
        Ok(base)
    }

    fn parse_primary(&mut self) -> Result<Expr, ParseError> {
        self.skip_ws();
        match self.peek() {
            Some(b'(') => {
                self.pos += 1;
                let e = self.nested(|p| p.parse_expr())?;
                self.expect(b')')?;
                Ok(e)
            }
            Some(c) if c.is_ascii_digit() || c == b'.' => self.parse_number(),
            Some(c) if c.is_ascii_alphabetic() || c == b'_' => self.parse_ident_or_func(),
            other => Err(ParseError(format!(
                "unexpected token: {:?}",
                other.map(|c| c as char)
            ))),
        }
    }

    fn parse_number(&mut self) -> Result<Expr, ParseError> {
        let start = self.pos;
        while matches!(
            self.peek(),
            Some(b'0'..=b'9' | b'.' | b'e' | b'E' | b'+' | b'-')
        ) {
            // Only allow + / - after e/E.
            let c = self.peek().unwrap();
            if (c == b'+' || c == b'-') && self.pos == start {
                break;
            }
            if (c == b'+' || c == b'-') {
                // Allowed only immediately after e/E.
                let prev = if self.pos > start {
                    self.input[self.pos - 1]
                } else {
                    0
                };
                if prev != b'e' && prev != b'E' {
                    break;
                }
            }
            self.pos += 1;
        }
        let s = std::str::from_utf8(&self.input[start..self.pos])
            .map_err(|e| ParseError(e.to_string()))?;
        s.parse::<f64>()
            .map(Expr::Num)
            .map_err(|e| ParseError(e.to_string()))
    }

    fn parse_ident_or_func(&mut self) -> Result<Expr, ParseError> {
        let start = self.pos;
        while matches!(self.peek(), Some(c) if c.is_ascii_alphanumeric() || c == b'_') {
            self.pos += 1;
        }
        let name = std::str::from_utf8(&self.input[start..self.pos])
            .map_err(|e| ParseError(e.to_string()))?;
        // Keywords.
        match name {
            "x" => return Ok(Expr::X),
            "y" => return Ok(Expr::Y),
            "pi" | "PI" => return Ok(Expr::Pi),
            "e" | "E" => return Ok(Expr::E),
            _ => {}
        }
        // Function call.
        self.skip_ws();
        if self.peek() == Some(b'(') {
            self.pos += 1;
            let arg1 = self.nested(|p| p.parse_expr())?;
            self.skip_ws();
            if self.peek() == Some(b',') {
                self.pos += 1;
                if !FUNCS2.contains(&name) {
                    return Err(ParseError(format!(
                        "unknown two-argument function: {name} (known: {})",
                        FUNCS2.join(", ")
                    )));
                }
                let arg2 = self.nested(|p| p.parse_expr())?;
                self.expect(b')')?;
                return Ok(Expr::Func2(
                    name.to_string(),
                    Box::new(arg1),
                    Box::new(arg2),
                ));
            }
            self.expect(b')')?;
            if !FUNCS1.contains(&name) {
                return Err(ParseError(format!(
                    "unknown function: {name} (known: {})",
                    FUNCS1.join(", ")
                )));
            }
            return Ok(Expr::Func1(name.to_string(), Box::new(arg1)));
        }
        Err(ParseError(format!("unknown identifier: {name}")))
    }
}

fn parse_expr(s: &str) -> Result<Expr, ParseError> {
    let mut p = Parser::new(s.trim());
    let expr = p.parse_expr()?;
    p.skip_ws();
    if p.pos != p.input.len() {
        return Err(ParseError(format!(
            "trailing input at position {}: '{}'",
            p.pos,
            std::str::from_utf8(&p.input[p.pos..]).unwrap_or("?")
        )));
    }
    Ok(expr)
}

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

    fn z(expr: &str, x: f64, y: f64) -> f64 {
        let s = UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: expr.to_string(),
            z_scale: 1.0,
            ..UserSurfaceConfig::default()
        })
        .expect("parse should succeed");
        s.eval_at(x, y)
    }

    // `**` is a power operator and must bind tighter than `*` (it used to be
    // parsed at the `*` level, so 2*x**2 meant (2*x)**2).
    #[test]
    fn bug_double_star_precedence() {
        assert_eq!(z("2*x**2", 3.0, 0.0), 18.0);
        assert_eq!(z("2**3**2", 0.0, 0.0), 512.0);
        assert_eq!(z("-x**2", 3.0, 0.0), -9.0);
        assert_eq!(z("x^2*2", 3.0, 0.0), 18.0);
    }

    // Unknown function names are a parse error, not a silent NaN surface.
    #[test]
    fn bug_unknown_function_is_error() {
        let r = UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: "sinn(x)".to_string(),
            ..UserSurfaceConfig::default()
        });
        assert!(r.is_err());
        let r = UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: "atan3(x, y)".to_string(),
            ..UserSurfaceConfig::default()
        });
        assert!(r.is_err());
    }

    // Deeply nested input is rejected instead of overflowing the stack.
    #[test]
    fn bug_deep_nesting_is_error() {
        let deep = format!("{}x{}", "(".repeat(100_000), ")".repeat(100_000));
        let r = UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: deep,
            ..UserSurfaceConfig::default()
        });
        assert!(r.is_err());
        let towers = vec!["x"; 50_000].join("^");
        assert!(UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: towers,
            ..UserSurfaceConfig::default()
        })
        .is_err());
        // Ordinary nesting still works.
        assert_eq!(z("((((x))))", 2.0, 0.0), 2.0);
    }

    fn surface(expr: &str) -> UserDefinedSurface {
        UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: expr.to_string(),
            ..UserSurfaceConfig::default()
        })
        .expect("parse should succeed")
    }

    #[test]
    fn constant_expression() {
        let s = surface("3.14");
        let p = s.sample(0.5, 0.5);
        assert!((p[2] - 3.14_f32).abs() < 1e-4);
    }

    #[test]
    fn x_variable() {
        let s = surface("x");
        // At u=0, x = x_min = -5.0.
        let p = s.sample(0.0, 0.0);
        assert!((p[0] - (-5.0_f32)).abs() < 1e-4);
        assert!((p[2] - (-5.0_f32)).abs() < 1e-4);
    }

    #[test]
    fn sin_cos_expression() {
        let s = surface("sin(x) * cos(y)");
        let p = s.sample(0.5, 0.5);
        assert!(p[2].is_finite());
    }

    #[test]
    fn default_expression_evaluates() {
        let cfg = UserSurfaceConfig::default();
        let s = UserDefinedSurface::new(cfg).unwrap();
        let p = s.sample(0.5, 0.5);
        assert!(p[2].is_finite());
    }

    #[test]
    fn negative_unary() {
        let s = surface("-x");
        let p_center = s.sample(0.5, 0.5);
        // At u=0.5, x=0, so z=0.
        assert!(p_center[2].abs() < 1e-3);
    }

    #[test]
    fn power_expression() {
        let s = surface("x^2 + y^2");
        let p = s.sample(0.5, 0.5);
        // At centre x=y=0, z=0.
        assert!(p[2].abs() < 1e-3);
    }

    #[test]
    fn exp_expression() {
        let s = surface("exp(-x*x)");
        let p = s.sample(0.5, 0.5); // x=0 → exp(0)=1
        assert!((p[2] - 1.0_f32).abs() < 1e-3);
    }

    #[test]
    fn normal_is_normalised() {
        let s = surface("sin(x) * cos(y)");
        let n = s.normal(0.5, 0.5);
        let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
        assert!((len - 1.0).abs() < 1e-3);
    }

    #[test]
    fn invalid_expression_returns_error() {
        let result = UserDefinedSurface::new(UserSurfaceConfig {
            z_expr: "sin(x".to_string(),
            ..UserSurfaceConfig::default()
        });
        assert!(result.is_err());
    }

    #[test]
    fn pi_constant() {
        let s = surface("pi");
        let p = s.sample(0.0, 0.0);
        assert!((p[2] as f64 - std::f64::consts::PI).abs() < 1e-4);
    }

    #[test]
    fn two_arg_atan2() {
        let s = surface("atan2(y, x)");
        let p = s.sample(0.5, 0.5); // x=0, y=0 → atan2(0,0)=0
        assert!(p[2].is_finite());
    }
}