Skip to main content

ginger/
tables.rs

1use std::sync::OnceLock;
2
3/// Number of precomputed Van der Corput sequence values
4pub const VDC_TABLE_SIZE: usize = 1000;
5
6struct LdsTables {
7    vdc2: Vec<f64>,
8    circle_x: Vec<f64>,
9    circle_y: Vec<f64>,
10    cos_pi_vdc2: Vec<f64>,
11}
12
13static TABLES: OnceLock<LdsTables> = OnceLock::new();
14
15fn get_tables() -> &'static LdsTables {
16    TABLES.get_or_init(|| {
17        // Generate VdCorput base-2 sequence
18        let mut vgen = lds_rs::lds::VdCorput::new(2);
19        let vdc2: Vec<f64> = (0..VDC_TABLE_SIZE).map(|_| vgen.pop()).collect();
20
21        let two_pi = std::f64::consts::TAU;
22        let mut circle_x = Vec::with_capacity(VDC_TABLE_SIZE);
23        let mut circle_y = Vec::with_capacity(VDC_TABLE_SIZE);
24        let mut cos_pi_vdc2 = Vec::with_capacity(VDC_TABLE_SIZE);
25        for &v in &vdc2 {
26            let theta = v * two_pi;
27            circle_x.push(theta.cos());
28            circle_y.push(theta.sin());
29            cos_pi_vdc2.push((std::f64::consts::PI * v).cos());
30        }
31
32        LdsTables {
33            vdc2,
34            circle_x,
35            circle_y,
36            cos_pi_vdc2,
37        }
38    })
39}
40
41/// Access the precomputed Van der Corput base-2 value at the given index
42#[inline]
43pub fn vdc2_table(index: usize) -> f64 {
44    get_tables().vdc2[index]
45}
46
47/// Access the precomputed Circle base-2 x-coordinate at the given index
48#[inline]
49pub fn circle2_table_x(index: usize) -> f64 {
50    get_tables().circle_x[index]
51}
52
53/// Access the precomputed Circle base-2 y-coordinate at the given index
54#[inline]
55pub fn circle2_table_y(index: usize) -> f64 {
56    get_tables().circle_y[index]
57}
58
59/// Access the precomputed cos(π * vdc2_table\[i\]) value at the given index
60#[inline]
61pub fn cos_pi_vdc2(index: usize) -> f64 {
62    get_tables().cos_pi_vdc2[index]
63}