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
use {gradient, math};
use math::{Point2, Point3};
use noise_fns::{NoiseFn, Seedable};
use permutationtable::PermutationTable;
use std::ops::Add;
const TO_REAL_CONSTANT_2D: f64 = -0.211_324_865_405_187;
const TO_SIMPLEX_CONSTANT_2D: f64 = 0.366_025_403_784_439;
const TO_SIMPLEX_CONSTANT_3D: f64 = -2.0 / 3.0;
const NORM_CONSTANT_2D: f64 = 1.0 / 0.054_282_952_886_616_23;
const NORM_CONSTANT_3D: f64 = 1.0 / 0.086_766_400_165_536_9;
#[cfg_attr(rustfmt, rustfmt_skip)]
const LATTICE_LOOKUP_2D: [([i8; 2], [f64; 2]); 4 * 8] =
[([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([-1, 0], [0.788_675_134_594_813f64, -0.211_324_865_405_187f64]),
([0, -1], [-0.211_324_865_405_187f64, 0.788_675_134_594_813f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([0, 1], [0.211_324_865_405_187f64, -0.788_675_134_594_813f64]),
([1, 0], [-0.788_675_134_594_813f64, 0.211_324_865_405_187f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([1, 0], [-0.788_675_134_594_813f64, 0.211_324_865_405_187f64]),
([0, -1], [-0.211_324_865_405_187f64, 0.788_675_134_594_813f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([2, 1], [-1.366_025_403_784_439f64, -0.366_025_403_784_439_04f64]),
([1, 0], [-0.788_675_134_594_813f64, 0.211_324_865_405_187f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([-1, 0], [0.788_675_134_594_813f64, -0.211_324_865_405_187f64]),
([0, 1], [0.211_324_865_405_187f64, -0.788_675_134_594_813f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([0, 1], [0.211_324_865_405_187f64, -0.788_675_134_594_813f64]),
([1, 2], [-0.366_025_403_784_439_04f64, -1.366_025_403_784_439f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([1, 0], [-0.788_675_134_594_813f64, 0.211_324_865_405_187f64]),
([0, 1], [0.211_324_865_405_187f64, -0.788_675_134_594_813f64]),
([0, 0], [0f64, 0f64]),
([1, 1], [-0.577_350_269_189_626f64, -0.577_350_269_189_626f64]),
([2, 1], [-1.366_025_403_784_439f64, -0.366_025_403_784_439_04f64]),
([1, 2], [-0.366_025_403_784_439_04f64, -1.366_025_403_784_439f64])];
#[cfg_attr(rustfmt, rustfmt_skip)]
const LATTICE_LOOKUP_3D: [[i8; 3]; 4 * 16] =
[[0, 0, 0],[1, 0, 0],[0, 1, 0],[0, 0, 1],
[1, 1, 1],[1, 0, 0],[0, 1, 0],[0, 0, 1],
[0, 0, 0],[0, 1, 1],[0, 1, 0],[0, 0, 1],
[1, 1, 1],[0, 1, 1],[0, 1, 0],[0, 0, 1],
[0, 0, 0],[1, 0, 0],[1, 0, 1],[0, 0, 1],
[1, 1, 1],[1, 0, 0],[1, 0, 1],[0, 0, 1],
[0, 0, 0],[0, 1, 1],[1, 0, 1],[0, 0, 1],
[1, 1, 1],[0, 1, 1],[1, 0, 1],[0, 0, 1],
[0, 0, 0],[1, 0, 0],[0, 1, 0],[1, 1, 0],
[1, 1, 1],[1, 0, 0],[0, 1, 0],[1, 1, 0],
[0, 0, 0],[0, 1, 1],[0, 1, 0],[1, 1, 0],
[1, 1, 1],[0, 1, 1],[0, 1, 0],[1, 1, 0],
[0, 0, 0],[1, 0, 0],[1, 0, 1],[1, 1, 0],
[1, 1, 1],[1, 0, 0],[1, 0, 1],[1, 1, 0],
[0, 0, 0],[0, 1, 1],[1, 0, 1],[1, 1, 0],
[1, 1, 1],[0, 1, 1],[1, 0, 1],[1, 1, 0]];
#[derive(Clone, Copy, Debug)]
pub struct SuperSimplex {
seed: u32,
perm_table: PermutationTable,
}
impl SuperSimplex {
pub const DEFAULT_SEED: u32 = 0;
pub fn new() -> Self {
SuperSimplex {
seed: Self::DEFAULT_SEED,
perm_table: PermutationTable::new(Self::DEFAULT_SEED),
}
}
}
impl Default for SuperSimplex {
fn default() -> Self {
Self::new()
}
}
impl Seedable for SuperSimplex {
fn set_seed(self, seed: u32) -> Self {
if self.seed == seed {
return self;
}
SuperSimplex {
seed,
perm_table: PermutationTable::new(seed),
}
}
fn seed(&self) -> u32 {
self.seed
}
}
impl NoiseFn<Point2<f64>> for SuperSimplex {
fn get(&self, point: Point2<f64>) -> f64 {
let mut value = 0.0;
let to_simplex_offset = math::fold2(point, Add::add) * TO_SIMPLEX_CONSTANT_2D;
let simplex_point = math::map2(point, |v| v + to_simplex_offset);
let simplex_base_point = math::map2(simplex_point, f64::floor);
let simplex_base_point_i = math::to_isize2(simplex_base_point);
let simplex_rel_coords = math::sub2(simplex_point, simplex_base_point);
let region_sum = math::fold2(simplex_rel_coords, Add::add).floor();
let index = ((region_sum >= 1.0) as usize) << 2
| ((simplex_rel_coords[0] - simplex_rel_coords[1] * 0.5 + 1.0 - region_sum * 0.5 >= 1.0)
as usize) << 3
| ((simplex_rel_coords[1] - simplex_rel_coords[0] * 0.5 + 1.0 - region_sum * 0.5 >= 1.0)
as usize) << 4;
let to_real_offset = math::fold2(simplex_rel_coords, Add::add) * TO_REAL_CONSTANT_2D;
let real_rel_coords = math::map2(simplex_rel_coords, |v| v + to_real_offset);
for lattice_lookup in &LATTICE_LOOKUP_2D[index..index + 4] {
let dpos = math::add2(real_rel_coords, math::cast2(lattice_lookup.1));
let attn = (2.0 / 3.0) - math::dot2(dpos, dpos);
if attn > 0.0 {
let lattice_point = math::add2(simplex_base_point_i, math::cast2(lattice_lookup.0));
let gradient = gradient::get2(self.perm_table.get2(lattice_point));
value += attn.powi(4) * math::dot2(gradient, dpos);
}
}
value * NORM_CONSTANT_2D
}
}
impl NoiseFn<Point3<f64>> for SuperSimplex {
fn get(&self, point: Point3<f64>) -> f64 {
let mut value = 0.0;
let to_simplex_offset = math::fold3(point, Add::add) * TO_SIMPLEX_CONSTANT_3D;
let simplex_point = math::map3(point, |v| -(v + to_simplex_offset));
let second_simplex_point = math::map3(simplex_point, |v| v + 512.5);
let simplex_base_point = math::map3(simplex_point, f64::floor);
let simplex_base_point_i = math::to_isize3(simplex_base_point);
let simplex_rel_coords = math::sub3(simplex_point, simplex_base_point);
let second_simplex_base_point = math::map3(second_simplex_point, f64::floor);
let second_simplex_base_point_i = math::to_isize3(second_simplex_base_point);
let second_simplex_rel_coords = math::sub3(second_simplex_point, second_simplex_base_point);
let index = ((simplex_rel_coords[0] + simplex_rel_coords[1] + simplex_rel_coords[2] >= 1.5)
as usize) << 2
| ((-simplex_rel_coords[0] + simplex_rel_coords[1] + simplex_rel_coords[2] >= 0.5)
as usize) << 3
| ((simplex_rel_coords[0] - simplex_rel_coords[1] + simplex_rel_coords[2] >= 0.5)
as usize) << 4
| ((simplex_rel_coords[0] + simplex_rel_coords[1] - simplex_rel_coords[2] >= 0.5)
as usize) << 5;
let second_index = ((second_simplex_rel_coords[0] + second_simplex_rel_coords[1]
+ second_simplex_rel_coords[2] >= 1.5) as usize) << 2
| ((-second_simplex_rel_coords[0] + second_simplex_rel_coords[1]
+ second_simplex_rel_coords[2] >= 0.5) as usize) << 3
| ((second_simplex_rel_coords[0] - second_simplex_rel_coords[1]
+ second_simplex_rel_coords[2] >= 0.5) as usize) << 4
| ((second_simplex_rel_coords[0] + second_simplex_rel_coords[1]
- second_simplex_rel_coords[2] >= 0.5) as usize) << 5;
for &lattice_lookup in &LATTICE_LOOKUP_3D[index..index + 4] {
let dpos = math::sub3(simplex_rel_coords, math::cast3(lattice_lookup));
let attn = 0.75 - math::dot3(dpos, dpos);
if attn > 0.0 {
let lattice_point = math::add3(simplex_base_point_i, math::cast3(lattice_lookup));
let gradient = gradient::get3(self.perm_table.get3(lattice_point));
value += attn.powi(4) * math::dot3(gradient, dpos);
}
}
for &lattice_lookup in &LATTICE_LOOKUP_3D[second_index..second_index + 4] {
let dpos = math::sub3(second_simplex_rel_coords, math::cast3(lattice_lookup));
let attn = 0.75 - math::dot3(dpos, dpos);
if attn > 0.0 {
let lattice_point =
math::add3(second_simplex_base_point_i, math::cast3(lattice_lookup));
let gradient = gradient::get3(self.perm_table.get3(lattice_point));
value += attn.powi(4) * math::dot3(gradient, dpos);
}
}
value * NORM_CONSTANT_3D
}
}