proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
Documentation
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
// topology/tiling.rs — Fundamental domains, wallpaper groups, and tilings

use glam::Vec2;
use std::f32::consts::PI;

// ─── Wallpaper Groups ──────────────────────────────────────────────────────

/// All 17 wallpaper groups (2D crystallographic groups).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WallpaperGroup {
    P1,
    P2,
    PM,
    PG,
    CM,
    P2MM,
    P2MG,
    P2GG,
    C2MM,
    P4,
    P4MM,
    P4GM,
    P3,
    P3M1,
    P31M,
    P6,
    P6MM,
}

/// A tile instance placed in the plane.
#[derive(Clone, Debug)]
pub struct TileInstance {
    pub position: Vec2,
    pub rotation: f32,
    pub mirror: bool,
}

/// A fundamental domain defined by its vertices.
#[derive(Clone, Debug)]
pub struct FundamentalDomain {
    pub vertices: Vec<Vec2>,
}

impl FundamentalDomain {
    /// Create a rectangular fundamental domain.
    pub fn rectangle(width: f32, height: f32) -> Self {
        Self {
            vertices: vec![
                Vec2::ZERO,
                Vec2::new(width, 0.0),
                Vec2::new(width, height),
                Vec2::new(0.0, height),
            ],
        }
    }

    /// Create a rhombus fundamental domain.
    pub fn rhombus(a: f32, angle: f32) -> Self {
        let dx = a * angle.cos();
        let dy = a * angle.sin();
        Self {
            vertices: vec![
                Vec2::ZERO,
                Vec2::new(a, 0.0),
                Vec2::new(a + dx, dy),
                Vec2::new(dx, dy),
            ],
        }
    }

    /// Create an equilateral triangle fundamental domain.
    pub fn equilateral_triangle(side: f32) -> Self {
        Self {
            vertices: vec![
                Vec2::ZERO,
                Vec2::new(side, 0.0),
                Vec2::new(side / 2.0, side * (3.0_f32).sqrt() / 2.0),
            ],
        }
    }
}

/// Generate a tiling by applying the symmetries of the given wallpaper group
/// to the fundamental domain, filling the area [-extent, extent] x [-extent, extent].
pub fn generate_tiling(group: WallpaperGroup, domain: &FundamentalDomain, extent: f32) -> Vec<TileInstance> {
    let (t1, t2, symmetries) = group_generators(group, domain);

    let mut tiles = Vec::new();
    let max_n = (extent / t1.length().max(0.01)) as i32 + 2;
    let max_m = (extent / t2.length().max(0.01)) as i32 + 2;

    for n in -max_n..=max_n {
        for m in -max_m..=max_m {
            let base = t1 * n as f32 + t2 * m as f32;

            for sym in &symmetries {
                let pos = base + sym.offset;
                if pos.x.abs() <= extent && pos.y.abs() <= extent {
                    tiles.push(TileInstance {
                        position: pos,
                        rotation: sym.rotation,
                        mirror: sym.mirror,
                    });
                }
            }
        }
    }
    tiles
}

struct SymOp {
    offset: Vec2,
    rotation: f32,
    mirror: bool,
}

/// Return translation vectors and symmetry operations for a wallpaper group.
fn group_generators(group: WallpaperGroup, domain: &FundamentalDomain) -> (Vec2, Vec2, Vec<SymOp>) {
    // Compute a bounding size from the domain
    let mut max_x = 0.0_f32;
    let mut max_y = 0.0_f32;
    for v in &domain.vertices {
        max_x = max_x.max(v.x);
        max_y = max_y.max(v.y);
    }
    let w = max_x.max(1.0);
    let h = max_y.max(1.0);

    match group {
        WallpaperGroup::P1 => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false }],
        ),
        WallpaperGroup::P2 => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, h / 2.0), rotation: PI, mirror: false },
            ],
        ),
        WallpaperGroup::PM => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(0.0, h), rotation: 0.0, mirror: true },
            ],
        ),
        WallpaperGroup::PG => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, 0.0), rotation: 0.0, mirror: true },
            ],
        ),
        WallpaperGroup::CM => (
            Vec2::new(w, 0.0),
            Vec2::new(w / 2.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
            ],
        ),
        WallpaperGroup::P2MM => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
            ],
        ),
        WallpaperGroup::P2MG => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, 0.0), rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::new(w / 2.0, 0.0), rotation: PI, mirror: true },
            ],
        ),
        WallpaperGroup::P2GG => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, h / 2.0), rotation: PI, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, 0.0), rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::new(0.0, h / 2.0), rotation: PI, mirror: true },
            ],
        ),
        WallpaperGroup::C2MM => (
            Vec2::new(w, 0.0),
            Vec2::new(w / 2.0, h),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
            ],
        ),
        WallpaperGroup::P4 => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, w), // square lattice
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI / 2.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 3.0 * PI / 2.0, mirror: false },
            ],
        ),
        WallpaperGroup::P4MM => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, w),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI / 2.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 3.0 * PI / 2.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI / 2.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 3.0 * PI / 2.0, mirror: true },
            ],
        ),
        WallpaperGroup::P4GM => (
            Vec2::new(w, 0.0),
            Vec2::new(0.0, w),
            vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, w / 2.0), rotation: PI / 2.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::new(w / 2.0, w / 2.0), rotation: 3.0 * PI / 2.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::new(w / 2.0, w / 2.0), rotation: PI / 2.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
                SymOp { offset: Vec2::new(w / 2.0, w / 2.0), rotation: 3.0 * PI / 2.0, mirror: true },
            ],
        ),
        WallpaperGroup::P3 => {
            let t1 = Vec2::new(w, 0.0);
            let t2 = Vec2::new(w / 2.0, w * (3.0_f32).sqrt() / 2.0);
            (t1, t2, vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: false },
            ])
        }
        WallpaperGroup::P3M1 => {
            let t1 = Vec2::new(w, 0.0);
            let t2 = Vec2::new(w / 2.0, w * (3.0_f32).sqrt() / 2.0);
            (t1, t2, vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: true },
            ])
        }
        WallpaperGroup::P31M => {
            let t1 = Vec2::new(w, 0.0);
            let t2 = Vec2::new(w / 2.0, w * (3.0_f32).sqrt() / 2.0);
            (t1, t2, vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI / 3.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 5.0 * PI / 3.0, mirror: true },
            ])
        }
        WallpaperGroup::P6 => {
            let t1 = Vec2::new(w, 0.0);
            let t2 = Vec2::new(w / 2.0, w * (3.0_f32).sqrt() / 2.0);
            (t1, t2, vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 5.0 * PI / 3.0, mirror: false },
            ])
        }
        WallpaperGroup::P6MM => {
            let t1 = Vec2::new(w, 0.0);
            let t2 = Vec2::new(w / 2.0, w * (3.0_f32).sqrt() / 2.0);
            (t1, t2, vec![
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 5.0 * PI / 3.0, mirror: false },
                SymOp { offset: Vec2::ZERO, rotation: 0.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI / 3.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 2.0 * PI / 3.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: PI, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 4.0 * PI / 3.0, mirror: true },
                SymOp { offset: Vec2::ZERO, rotation: 5.0 * PI / 3.0, mirror: true },
            ])
        }
    }
}

// ─── Penrose Tiling ────────────────────────────────────────────────────────

/// Penrose tile types: the two rhombus shapes in P3 Penrose tiling.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PenroseType {
    /// Thin rhombus (36/144 degree angles)
    Thin,
    /// Thick rhombus (72/108 degree angles)
    Thick,
}

/// A single Penrose tile.
#[derive(Clone, Debug)]
pub struct PenroseTile {
    pub vertices: Vec<Vec2>,
    pub tile_type: PenroseType,
}

/// Generate a Penrose tiling using the Robinson triangle decomposition.
/// `depth` controls the number of subdivision iterations.
pub fn generate_penrose(depth: usize) -> Vec<PenroseTile> {
    let golden = (1.0 + 5.0_f32.sqrt()) / 2.0;

    // Start with a wheel of 10 "half-kite" triangles
    let mut triangles: Vec<(bool, Vec2, Vec2, Vec2)> = Vec::new(); // (is_type_a, A, B, C)

    for i in 0..10 {
        let angle1 = 2.0 * PI * i as f32 / 10.0;
        let angle2 = 2.0 * PI * (i + 1) as f32 / 10.0;

        let b = Vec2::new(angle1.cos(), angle1.sin());
        let c = Vec2::new(angle2.cos(), angle2.sin());

        if i % 2 == 0 {
            triangles.push((true, Vec2::ZERO, b, c));
        } else {
            triangles.push((true, Vec2::ZERO, c, b));
        }
    }

    // Subdivide
    for _ in 0..depth {
        let mut new_triangles = Vec::new();
        for &(is_a, a, b, c) in &triangles {
            if is_a {
                // Subdivide acute triangle
                let p = a + (b - a) / golden;
                new_triangles.push((true, c, p, b));
                new_triangles.push((false, p, c, a));
            } else {
                // Subdivide obtuse triangle
                let q = b + (a - b) / golden;
                let r = b + (c - b) / golden;
                new_triangles.push((false, r, c, a));
                new_triangles.push((false, q, r, b));
                new_triangles.push((true, r, q, a));
            }
        }
        triangles = new_triangles;
    }

    // Convert pairs of triangles into rhombus tiles
    // For simplicity, output each triangle pair as a tile
    let mut tiles = Vec::new();
    let mut used = vec![false; triangles.len()];

    for i in 0..triangles.len() {
        if used[i] {
            continue;
        }

        let (is_a_i, a_i, b_i, c_i) = triangles[i];

        // Try to find a matching triangle to form a rhombus
        let mut found = false;
        for j in (i + 1)..triangles.len() {
            if used[j] {
                continue;
            }
            let (is_a_j, a_j, b_j, c_j) = triangles[j];
            if is_a_i != is_a_j {
                continue;
            }

            // Check if they share an edge (B-C)
            let share = (b_i - b_j).length() < 0.01 && (c_i - c_j).length() < 0.01
                || (b_i - c_j).length() < 0.01 && (c_i - b_j).length() < 0.01;

            if share {
                let tile_type = if is_a_i { PenroseType::Thin } else { PenroseType::Thick };
                tiles.push(PenroseTile {
                    vertices: vec![a_i, b_i, a_j, c_i],
                    tile_type,
                });
                used[i] = true;
                used[j] = true;
                found = true;
                break;
            }
        }

        if !found {
            // Unpaired triangle — output as a degenerate tile
            let tile_type = if is_a_i { PenroseType::Thin } else { PenroseType::Thick };
            tiles.push(PenroseTile {
                vertices: vec![a_i, b_i, c_i],
                tile_type,
            });
            used[i] = true;
        }
    }

    tiles
}

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

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

    #[test]
    fn test_p1_tiling() {
        let domain = FundamentalDomain::rectangle(1.0, 1.0);
        let tiles = generate_tiling(WallpaperGroup::P1, &domain, 5.0);
        assert!(!tiles.is_empty());
        // P1 has one copy per unit cell
        for tile in &tiles {
            assert!(!tile.mirror);
            assert!((tile.rotation).abs() < 1e-6);
        }
    }

    #[test]
    fn test_p4_tiling() {
        let domain = FundamentalDomain::rectangle(1.0, 1.0);
        let tiles = generate_tiling(WallpaperGroup::P4, &domain, 3.0);
        assert!(!tiles.is_empty());
        // P4 has 4 rotations per unit cell
    }

    #[test]
    fn test_p6mm_tiling() {
        let domain = FundamentalDomain::equilateral_triangle(1.0);
        let tiles = generate_tiling(WallpaperGroup::P6MM, &domain, 3.0);
        assert!(!tiles.is_empty());
        // P6MM has 12 symmetry operations
    }

    #[test]
    fn test_all_17_groups_produce_tiles() {
        let domain = FundamentalDomain::rectangle(1.0, 1.0);
        let groups = [
            WallpaperGroup::P1, WallpaperGroup::P2, WallpaperGroup::PM,
            WallpaperGroup::PG, WallpaperGroup::CM, WallpaperGroup::P2MM,
            WallpaperGroup::P2MG, WallpaperGroup::P2GG, WallpaperGroup::C2MM,
            WallpaperGroup::P4, WallpaperGroup::P4MM, WallpaperGroup::P4GM,
            WallpaperGroup::P3, WallpaperGroup::P3M1, WallpaperGroup::P31M,
            WallpaperGroup::P6, WallpaperGroup::P6MM,
        ];
        for group in groups {
            let tiles = generate_tiling(group, &domain, 2.0);
            assert!(!tiles.is_empty(), "Group {:?} produced no tiles", group);
        }
    }

    #[test]
    fn test_fundamental_domain_rectangle() {
        let d = FundamentalDomain::rectangle(2.0, 3.0);
        assert_eq!(d.vertices.len(), 4);
    }

    #[test]
    fn test_fundamental_domain_rhombus() {
        let d = FundamentalDomain::rhombus(1.0, PI / 3.0);
        assert_eq!(d.vertices.len(), 4);
    }

    #[test]
    fn test_fundamental_domain_triangle() {
        let d = FundamentalDomain::equilateral_triangle(1.0);
        assert_eq!(d.vertices.len(), 3);
    }

    #[test]
    fn test_penrose_tiling_depth_0() {
        let tiles = generate_penrose(0);
        assert!(!tiles.is_empty());
    }

    #[test]
    fn test_penrose_tiling_depth_3() {
        let tiles = generate_penrose(3);
        assert!(tiles.len() > 10, "Depth-3 Penrose should have many tiles, got {}", tiles.len());
    }

    #[test]
    fn test_penrose_has_both_types() {
        let tiles = generate_penrose(3);
        let has_thin = tiles.iter().any(|t| t.tile_type == PenroseType::Thin);
        let has_thick = tiles.iter().any(|t| t.tile_type == PenroseType::Thick);
        assert!(has_thin, "Should have thin tiles");
        assert!(has_thick, "Should have thick tiles");
    }

    #[test]
    fn test_penrose_tile_vertices() {
        let tiles = generate_penrose(2);
        for tile in &tiles {
            assert!(tile.vertices.len() >= 3);
            for v in &tile.vertices {
                // All vertices should be within a reasonable range of the origin
                assert!(v.length() < 5.0, "Vertex too far: {:?}", v);
            }
        }
    }
}