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
use super::{RandomField, Sampler};
use vek::*;

#[derive(Clone)]
pub struct StructureGen2d {
    freq: u32,
    spread: u32,
    x_field: RandomField,
    y_field: RandomField,
    seed_field: RandomField,
}

pub type StructureField = (Vec2<i32>, u32);

impl StructureGen2d {
    pub fn new(seed: u32, freq: u32, spread: u32) -> Self {
        Self {
            freq,
            spread,
            x_field: RandomField::new(seed + 0),
            y_field: RandomField::new(seed + 1),
            seed_field: RandomField::new(seed + 2),
        }
    }

    #[inline]
    fn sample_to_index_internal(freq: i32, pos: Vec2<i32>) -> Vec2<i32> {
        pos.map(|e| e.div_euclid(freq))
    }

    #[inline]
    pub fn sample_to_index(&self, pos: Vec2<i32>) -> Vec2<i32> {
        Self::sample_to_index_internal(self.freq as i32, pos)
    }

    #[inline]
    fn freq_offset(freq: i32) -> i32 { freq / 2 }

    #[inline]
    fn spread_mul(spread: u32) -> u32 { spread * 2 }

    #[inline]
    fn index_to_sample_internal(
        freq: i32,
        freq_offset: i32,
        spread: i32,
        spread_mul: u32,
        x_field: RandomField,
        y_field: RandomField,
        seed_field: RandomField,
        index: Vec2<i32>,
    ) -> StructureField {
        let center = index * freq + freq_offset;
        let pos = Vec3::from(center);
        (
            center
                + if spread_mul > 0 {
                    Vec2::new(
                        (x_field.get(pos) % spread_mul) as i32 - spread,
                        (y_field.get(pos) % spread_mul) as i32 - spread,
                    )
                } else {
                    Vec2::zero()
                },
            seed_field.get(pos),
        )
    }

    /// Note: Generates all possible closest samples for elements in the range
    /// of min to max, *exclusive.*
    pub fn iter(&self, min: Vec2<i32>, max: Vec2<i32>) -> impl Iterator<Item = StructureField> {
        let freq = self.freq;
        let spread = self.spread;
        let spread_mul = Self::spread_mul(spread);
        assert!(spread * 2 == spread_mul);
        assert!(spread_mul <= freq);
        let spread = spread as i32;
        let freq = freq as i32;
        let freq_offset = Self::freq_offset(freq);
        assert!(freq_offset * 2 == freq);

        let min_index = Self::sample_to_index_internal(freq, min) - 1;
        let max_index = Self::sample_to_index_internal(freq, max) + 1;
        assert!(min_index.x < max_index.x);
        // NOTE: xlen > 0
        let xlen = (max_index.x - min_index.x) as u32;
        assert!(min_index.y < max_index.y);
        // NOTE: ylen > 0
        let ylen = (max_index.y - min_index.y) as u32;
        // NOTE: Cannot fail, since every product of u32s fits in a u64.
        let len = ylen as u64 * xlen as u64;
        // NOTE: since iteration is *exclusive* for the initial range, it's fine that we
        // don't go up to the maximum value.
        // NOTE: we convert to usize first, and then iterate, because we want to make
        // sure we get a properly indexed parallel iterator that can deal with
        // the whole range at once.
        let x_field = self.x_field;
        let y_field = self.y_field;
        let seed_field = self.seed_field;
        (0..len).map(move |xy| {
            let index = min_index + Vec2::new((xy % xlen as u64) as i32, (xy / xlen as u64) as i32);
            Self::index_to_sample_internal(
                freq,
                freq_offset,
                spread,
                spread_mul,
                x_field,
                y_field,
                seed_field,
                index,
            )
        })
    }
}

impl Sampler<'static> for StructureGen2d {
    type Index = Vec2<i32>;
    type Sample = [StructureField; 9];

    fn get(&self, sample_pos: Self::Index) -> Self::Sample {
        let mut samples = [(Vec2::zero(), 0); 9];

        let freq = self.freq;
        let spread = self.spread;
        let spread_mul = Self::spread_mul(spread);
        let spread = spread as i32;
        let freq = freq as i32;
        let freq_offset = Self::freq_offset(freq);

        let sample_closest = Self::sample_to_index_internal(freq, sample_pos);

        for i in 0..3 {
            for j in 0..3 {
                let index = sample_closest + Vec2::new(i as i32, j as i32) - 1;
                let sample = Self::index_to_sample_internal(
                    freq,
                    freq_offset,
                    spread,
                    spread_mul,
                    self.x_field,
                    self.y_field,
                    self.seed_field,
                    index,
                );
                samples[i * 3 + j] = sample;
            }
        }

        samples
    }
}