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
//! Wallpaper group patterns and frieze patterns.

/// Wallpaper group enumeration (17 plane symmetry groups).
#[derive(Debug, Clone, PartialEq)]
pub enum WallpaperGroup {
    /// Translations only.
    P1,
    /// Translations and 180 degree rotations.
    P2,
    /// Translations and parallel mirror lines.
    PM,
    /// Translations and parallel glide reflections.
    PG,
    /// Mirror lines with glide reflections between them (rhombic lattice).
    CM,
    /// Perpendicular mirror lines (rectangular lattice).
    PMM,
    /// Mirrors in one direction, glides in the other, plus 180 degree rotations.
    PMG,
    /// Perpendicular glide reflections and 180 degree rotations, no mirrors.
    PGG,
    /// Perpendicular mirrors on a rhombic lattice.
    CMM,
    /// 90 degree rotations only.
    P4,
    /// 90 degree rotations with mirrors through the rotation centres (square).
    P4M,
    /// 90 degree rotations with mirrors not through the 4-fold centres.
    P4G,
    /// 120 degree rotations only.
    P3,
    /// 120 degree rotations with all rotation centres on mirror lines.
    P3M1,
    /// 120 degree rotations with some rotation centres off the mirror lines.
    P31M,
    /// 60 degree rotations only.
    P6,
    /// 60 degree rotations with mirrors (hexagonal, full symmetry).
    P6M,
}

/// Frieze group enumeration (7 frieze symmetry groups).
#[derive(Debug, Clone, PartialEq)]
pub enum FriezeGroup {
    /// Translation only (hop).
    F1,
    /// Translation and half-turns (spinning hop).
    F2,
    /// Translation and a horizontal mirror (jump).
    F11,
    /// Translation and vertical mirrors (sidle).
    F1M,
    /// Translation and glide reflection (step).
    FM,
    /// Horizontal and vertical mirrors (spinning jump).
    F2MM,
    /// Vertical mirrors and glide reflection (spinning sidle).
    F2MG,
}

/// A 2D symmetry transform.
#[derive(Debug, Clone)]
pub struct SymmetryTransform {
    /// Translation `(dx, dy)` applied last.
    pub translate: (f64, f64),
    /// Rotation about the origin in degrees, applied after the reflections.
    pub rotate_deg: f64,
    /// Mirror across the x axis (negates y).
    pub reflect_x: bool,
    /// Mirror across the y axis (negates x).
    pub reflect_y: bool,
}

impl SymmetryTransform {
    /// The transform that leaves points unchanged.
    pub fn identity() -> Self {
        Self {
            translate: (0.0, 0.0),
            rotate_deg: 0.0,
            reflect_x: false,
            reflect_y: false,
        }
    }
}

/// Apply a transform to a point.
pub fn apply_transform(x: f64, y: f64, t: &SymmetryTransform) -> (f64, f64) {
    let mut px = x;
    let mut py = y;

    // Reflect
    if t.reflect_x {
        py = -py;
    }
    if t.reflect_y {
        px = -px;
    }

    // Rotate
    if t.rotate_deg.abs() > 1e-9 {
        let angle = t.rotate_deg.to_radians();
        let cos_a = angle.cos();
        let sin_a = angle.sin();
        let rx = cos_a * px - sin_a * py;
        let ry = sin_a * px + cos_a * py;
        px = rx;
        py = ry;
    }

    // Translate
    px += t.translate.0;
    py += t.translate.1;

    (px, py)
}

fn make_rotation(deg: f64) -> SymmetryTransform {
    SymmetryTransform {
        translate: (0.0, 0.0),
        rotate_deg: deg,
        reflect_x: false,
        reflect_y: false,
    }
}

fn make_translation(tx: f64, ty: f64) -> SymmetryTransform {
    SymmetryTransform {
        translate: (tx, ty),
        rotate_deg: 0.0,
        reflect_x: false,
        reflect_y: false,
    }
}

fn make_reflect_x_translate(tx: f64, ty: f64) -> SymmetryTransform {
    SymmetryTransform {
        translate: (tx, ty),
        rotate_deg: 0.0,
        reflect_x: true,
        reflect_y: false,
    }
}

fn make_reflect_y_translate(tx: f64, ty: f64) -> SymmetryTransform {
    SymmetryTransform {
        translate: (tx, ty),
        rotate_deg: 0.0,
        reflect_x: false,
        reflect_y: true,
    }
}

/// Return fundamental domain transforms for a wallpaper group.
pub fn wallpaper_transforms(
    group: &WallpaperGroup,
    lattice_a: f64,
    lattice_b: f64,
) -> Vec<SymmetryTransform> {
    match group {
        WallpaperGroup::P1 => vec![SymmetryTransform::identity()],
        WallpaperGroup::P2 => vec![SymmetryTransform::identity(), make_rotation(180.0)],
        WallpaperGroup::PM => vec![
            SymmetryTransform::identity(),
            make_reflect_y_translate(0.0, 0.0),
            make_translation(lattice_a, 0.0),
            SymmetryTransform {
                translate: (lattice_a, 0.0),
                rotate_deg: 0.0,
                reflect_x: false,
                reflect_y: true,
            },
        ],
        WallpaperGroup::PG => vec![
            SymmetryTransform::identity(),
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
        ],
        WallpaperGroup::CM => vec![
            SymmetryTransform::identity(),
            make_reflect_x_translate(0.0, 0.0),
            make_translation(lattice_a / 2.0, lattice_b / 2.0),
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
        ],
        WallpaperGroup::PMM => vec![
            SymmetryTransform::identity(),
            make_reflect_x_translate(0.0, 0.0),
            make_reflect_y_translate(0.0, 0.0),
            SymmetryTransform {
                translate: (0.0, 0.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: true,
            },
        ],
        WallpaperGroup::PMG => vec![
            SymmetryTransform::identity(),
            make_rotation(180.0),
            make_reflect_x_translate(lattice_a / 2.0, 0.0),
            SymmetryTransform {
                translate: (lattice_a / 2.0, 0.0),
                rotate_deg: 180.0,
                reflect_x: true,
                reflect_y: false,
            },
        ],
        WallpaperGroup::PGG => vec![
            SymmetryTransform::identity(),
            make_rotation(180.0),
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: false,
                reflect_y: true,
            },
        ],
        WallpaperGroup::CMM => vec![
            SymmetryTransform::identity(),
            make_rotation(180.0),
            make_reflect_x_translate(0.0, 0.0),
            make_reflect_y_translate(0.0, 0.0),
            make_translation(lattice_a / 2.0, lattice_b / 2.0),
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 180.0,
                reflect_x: false,
                reflect_y: false,
            },
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
            SymmetryTransform {
                translate: (lattice_a / 2.0, lattice_b / 2.0),
                rotate_deg: 0.0,
                reflect_x: false,
                reflect_y: true,
            },
        ],
        WallpaperGroup::P4 => vec![
            SymmetryTransform::identity(),
            make_rotation(90.0),
            make_rotation(180.0),
            make_rotation(270.0),
        ],
        WallpaperGroup::P4M => {
            let mut t = vec![
                SymmetryTransform::identity(),
                make_rotation(90.0),
                make_rotation(180.0),
                make_rotation(270.0),
            ];
            for deg in [0.0, 90.0, 180.0, 270.0] {
                t.push(SymmetryTransform {
                    translate: (0.0, 0.0),
                    rotate_deg: deg,
                    reflect_x: true,
                    reflect_y: false,
                });
            }
            t
        }
        WallpaperGroup::P4G => {
            let mut t = vec![
                SymmetryTransform::identity(),
                make_rotation(90.0),
                make_rotation(180.0),
                make_rotation(270.0),
            ];
            let off = lattice_a / 2.0;
            for deg in [0.0, 90.0, 180.0, 270.0] {
                t.push(SymmetryTransform {
                    translate: (off, off),
                    rotate_deg: deg,
                    reflect_x: true,
                    reflect_y: false,
                });
            }
            t
        }
        WallpaperGroup::P3 => {
            let deg = 120.0_f64;
            vec![
                SymmetryTransform::identity(),
                make_rotation(deg),
                make_rotation(2.0 * deg),
            ]
        }
        WallpaperGroup::P3M1 => {
            let deg = 120.0_f64;
            let mut t = vec![
                SymmetryTransform::identity(),
                make_rotation(deg),
                make_rotation(2.0 * deg),
            ];
            for d in [0.0, deg, 2.0 * deg] {
                t.push(SymmetryTransform {
                    translate: (0.0, 0.0),
                    rotate_deg: d,
                    reflect_x: true,
                    reflect_y: false,
                });
            }
            t
        }
        WallpaperGroup::P31M => {
            let deg = 120.0_f64;
            let mut t = vec![
                SymmetryTransform::identity(),
                make_rotation(deg),
                make_rotation(2.0 * deg),
            ];
            for d in [0.0, deg, 2.0 * deg] {
                t.push(SymmetryTransform {
                    translate: (0.0, 0.0),
                    rotate_deg: d,
                    reflect_x: false,
                    reflect_y: true,
                });
            }
            t
        }
        WallpaperGroup::P6 => (0..6).map(|i| make_rotation(i as f64 * 60.0)).collect(),
        WallpaperGroup::P6M => {
            let mut t: Vec<SymmetryTransform> =
                (0..6).map(|i| make_rotation(i as f64 * 60.0)).collect();
            for i in 0..6 {
                t.push(SymmetryTransform {
                    translate: (0.0, 0.0),
                    rotate_deg: i as f64 * 60.0,
                    reflect_x: true,
                    reflect_y: false,
                });
            }
            t
        }
    }
}

/// Return transforms for a frieze group along the X axis.
pub fn frieze_transforms(group: &FriezeGroup, repeat: f64) -> Vec<SymmetryTransform> {
    match group {
        FriezeGroup::F1 => vec![SymmetryTransform::identity()],
        FriezeGroup::F2 => vec![SymmetryTransform::identity(), make_rotation(180.0)],
        FriezeGroup::F11 => vec![
            SymmetryTransform::identity(),
            // Glide reflection: reflect y, translate x by half
            SymmetryTransform {
                translate: (repeat / 2.0, 0.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
        ],
        FriezeGroup::F1M => vec![
            SymmetryTransform::identity(),
            // Vertical mirror
            make_reflect_y_translate(0.0, 0.0),
        ],
        FriezeGroup::FM => vec![
            SymmetryTransform::identity(),
            // Horizontal mirror
            make_reflect_x_translate(0.0, 0.0),
        ],
        FriezeGroup::F2MM => vec![
            SymmetryTransform::identity(),
            make_rotation(180.0),
            make_reflect_x_translate(0.0, 0.0),
            make_reflect_y_translate(0.0, 0.0),
        ],
        FriezeGroup::F2MG => vec![
            SymmetryTransform::identity(),
            make_rotation(180.0),
            SymmetryTransform {
                translate: (repeat / 2.0, 0.0),
                rotate_deg: 0.0,
                reflect_x: true,
                reflect_y: false,
            },
            SymmetryTransform {
                translate: (repeat / 2.0, 0.0),
                rotate_deg: 180.0,
                reflect_x: true,
                reflect_y: false,
            },
        ],
    }
}

/// Apply all transforms to each point in the motif.
pub fn tile_motif(
    motif_points: &[(f64, f64)],
    transforms: &[SymmetryTransform],
) -> Vec<(f64, f64)> {
    let mut result = Vec::with_capacity(motif_points.len() * transforms.len());
    for &(x, y) in motif_points {
        for t in transforms {
            result.push(apply_transform(x, y, t));
        }
    }
    result
}

/// Draw a filled circle onto an RGB image buffer.
pub fn draw_circle(image: &mut Vec<u8>, width: u32, cx: f64, cy: f64, r: f64, color: [u8; 3]) {
    let height = image.len() as u32 / (width * 3);
    let x_min = ((cx - r).floor() as i64).max(0) as u32;
    let x_max = ((cx + r).ceil() as i64).min(width as i64 - 1) as u32;
    let y_min = ((cy - r).floor() as i64).max(0) as u32;
    let y_max = ((cy + r).ceil() as i64).min(height as i64 - 1) as u32;

    for py in y_min..=y_max {
        for px in x_min..=x_max {
            let dx = px as f64 - cx;
            let dy = py as f64 - cy;
            if dx * dx + dy * dy <= r * r {
                let idx = ((py * width + px) * 3) as usize;
                if idx + 2 < image.len() {
                    image[idx] = color[0];
                    image[idx + 1] = color[1];
                    image[idx + 2] = color[2];
                }
            }
        }
    }
}

/// LCG random number generator returning f64 in [0, 1).
fn lcg_next(state: &mut u64) -> f64 {
    *state = state
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407);
    (*state >> 33) as f64 / u32::MAX as f64
}

/// Render a wallpaper pattern as an RGB image.
pub fn render_wallpaper(group: &WallpaperGroup, width: u32, height: u32, seed: u64) -> Vec<u8> {
    let mut image = vec![0u8; (width * height * 3) as usize];
    let transforms = wallpaper_transforms(group, 1.0, 1.0);

    // Generate random motif: 5-10 points in [0,1]²
    let mut state = seed.wrapping_add(1);
    let num_pts = 5 + (lcg_next(&mut state) * 6.0) as usize;
    let motif: Vec<(f64, f64)> = (0..num_pts)
        .map(|_| (lcg_next(&mut state), lcg_next(&mut state)))
        .collect();

    let tiled = tile_motif(&motif, &transforms);

    let r_px = (width.min(height) as f64 * 0.02).max(2.0);

    // Tile across the image using a grid of repeating cells
    let tile_w = width as f64;
    let tile_h = height as f64;
    for &(fx, fy) in &tiled {
        // Map normalized coords to pixel coords, tile with modulo
        let px = ((fx % 1.0 + 1.0) % 1.0 * tile_w) as f64;
        let py = ((fy % 1.0 + 1.0) % 1.0 * tile_h) as f64;
        draw_circle(&mut image, width, px, py, r_px, [255, 255, 255]);
    }

    image
}

/// Render a frieze pattern as an RGB image.
pub fn render_frieze(group: &FriezeGroup, width: u32, height: u32) -> Vec<u8> {
    let mut image = vec![0u8; (width * height * 3) as usize];
    let transforms = frieze_transforms(group, 1.0);

    // Simple motif: a triangle of 3 points
    let motif: Vec<(f64, f64)> = vec![(0.1, 0.3), (0.15, 0.7), (0.05, 0.7)];
    let tiled = tile_motif(&motif, &transforms);

    let r_px = (width.min(height) as f64 * 0.04).max(2.0);

    for &(fx, fy) in &tiled {
        let px = ((fx % 1.0 + 1.0) % 1.0 * width as f64) as f64;
        let py = ((fy % 1.0 + 1.0) % 1.0 * height as f64) as f64;
        draw_circle(&mut image, width, px, py, r_px, [255, 255, 255]);
    }

    image
}

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

    #[test]
    fn test_apply_transform_identity() {
        let t = SymmetryTransform::identity();
        let (x, y) = apply_transform(3.0, 4.0, &t);
        assert!((x - 3.0).abs() < 1e-9);
        assert!((y - 4.0).abs() < 1e-9);
    }

    #[test]
    fn test_p4_produces_4_transforms() {
        let transforms = wallpaper_transforms(&WallpaperGroup::P4, 1.0, 1.0);
        assert_eq!(transforms.len(), 4);
    }

    #[test]
    fn test_frieze_f2mm_produces_expected_count() {
        let transforms = frieze_transforms(&FriezeGroup::F2MM, 1.0);
        assert_eq!(transforms.len(), 4);
    }

    #[test]
    fn test_tile_motif_multiplies_points() {
        let motif = vec![(0.5, 0.5), (0.1, 0.2)];
        let transforms = wallpaper_transforms(&WallpaperGroup::P4, 1.0, 1.0);
        let tiled = tile_motif(&motif, &transforms);
        assert_eq!(tiled.len(), motif.len() * transforms.len());
    }

    #[test]
    fn test_render_returns_correct_buffer_size() {
        let img = render_wallpaper(&WallpaperGroup::P4, 64, 64, 42);
        assert_eq!(img.len(), 64 * 64 * 3);
        let frieze_img = render_frieze(&FriezeGroup::F2MM, 128, 32);
        assert_eq!(frieze_img.len(), 128 * 32 * 3);
    }
}