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
extern crate rand;
extern crate rayon;
extern crate concurrent_hashmap;
extern crate fnv;
use std::iter::{
self,
Extend
};
use std::f64;
use rand::{
Rng,
XorShiftRng,
SeedableRng,
distributions::Standard
};
use rayon::prelude::*;
use concurrent_hashmap::{
ConcHashMap,
Options as HashMapOptions
};
use fnv::FnvBuildHasher;
pub struct WorleyNoise {
permutation_table: Vec<u128>,
permutation_mask: usize,
density: f64,
point_count_table: Vec<u32>,
cache: ConcHashMap<(i32, i32), Vec<(f64, f64)>, FnvBuildHasher>,
distance_function: Box<Fn(f64, f64) -> f64 + Sync>,
value_function: Box<Fn(Vec<f64>) -> f64 + Sync>,
radius: u16
}
impl WorleyNoise {
const MIN_POINTS: u32 = 1;
const MAX_POINTS: u32 = 9;
const POINT_COUNT_TABLE_LEN: usize = 150;
pub const DEFAULT_RADIUS: u16 = 1;
pub const DEFAULT_PERMUTATION_BITS: usize = 10;
pub const DEFAULT_DENSITY: f64 = 3.0;
pub const DEFAULT_CACHE_CAPACITY: usize = 2500;
pub fn new() -> Self {
Self::with_cache_capacity(Self::DEFAULT_CACHE_CAPACITY)
}
pub fn with_cache_capacity(capacity: usize) -> Self {
let default_distance_function = |x, y| x * x + y * y;
let default_value_function = |distances: Vec<f64>| distances.iter()
.cloned()
.fold(f64::MAX, f64::min);
let cache_options = HashMapOptions {
capacity: capacity,
hasher_factory: FnvBuildHasher::default(),
.. Default::default()
};
let mut noise = WorleyNoise {
permutation_table: Vec::new(),
permutation_mask: 0,
density: 0.0,
point_count_table: Vec::new(),
cache: ConcHashMap::with_options(cache_options),
distance_function: Box::new(default_distance_function),
value_function: Box::new(default_value_function),
radius: Self::DEFAULT_RADIUS
};
noise.set_density(Self::DEFAULT_DENSITY);
noise.permutate(Self::DEFAULT_PERMUTATION_BITS);
noise
}
fn feature_point_count(&self, probability: f64) -> u32 {
let index = ((self.point_count_table.len() - 1) as f64 * probability).floor() as usize;
self.point_count_table[index]
}
fn hash(&self, x: i32, y: i32) -> [u8; 16] {
let hash = self.permutation_table[x as usize & self.permutation_mask] ^ self.permutation_table[y as usize & self.permutation_mask];
as_bytes(hash)
}
fn feature_points(&self, quad_x: i32, quad_y: i32, collector: &mut Vec<(f64, f64)>) {
let already_calculated = if let Some(cached) = self.cache.find(&(quad_x, quad_y)) {
collector.extend_from_slice(cached.get());
true
} else {
false
};
if !already_calculated {
let mut points = Vec::new();
let seed = self.hash(quad_x, quad_y);
let mut rng = XorShiftRng::from_seed(seed);
let count = self.feature_point_count(rng.gen());
for _ in 0 .. count {
let x = rng.gen::<f64>() + quad_x as f64;
let y = rng.gen::<f64>() + quad_y as f64;
points.push((x, y));
}
collector.extend_from_slice(&points);
self.cache.insert((quad_x, quad_y), points);
}
}
fn adjacent_feature_points(&self, quad_x: i32, quad_y: i32) -> Vec<(f64, f64)> {
let mut squares_around = 0;
for i in 1 ..= self.radius {
squares_around += i;
}
squares_around *= 8;
let expected_point_count = 1 + (self.density * squares_around as f64) as usize;
let mut points = Vec::with_capacity(expected_point_count);
let radius = self.radius as i32;
let start_x = quad_x - radius;
let start_y = quad_y - radius;
let end_x = quad_x + radius;
let end_y = quad_y + radius;
for x in start_x ..= end_x {
for y in start_y ..= end_y {
self.feature_points(x, y, &mut points);
}
}
points
}
pub fn permutate(&mut self, permutation_table_bit_length: usize) {
self.permutate_seeded(permutation_table_bit_length, rand::random());
}
pub fn permutate_seeded(&mut self, permutation_table_bit_length: usize, seed: u128) {
let seed = as_bytes(seed);
let mut rng = XorShiftRng::from_seed(seed);
let length = 1 << permutation_table_bit_length;
self.permutation_table.clear();
self.permutation_table.reserve(length);
self.permutation_table.extend(rng.sample_iter::<u128, Standard>(&Standard).take(length));
self.permutation_mask = length - 1;
}
pub fn set_density(&mut self, density: f64) {
self.point_count_table.clear();
self.point_count_table.reserve(Self::POINT_COUNT_TABLE_LEN);
for i in Self::MIN_POINTS ..= Self::MAX_POINTS {
let poisson = density.powi(i as i32) * f64::consts::E.powf(-density) / factorial(i as u16) as f64;
let count = (poisson * Self::POINT_COUNT_TABLE_LEN as f64).round() as usize;
self.point_count_table.extend(iter::repeat(i).take(count));
}
}
pub fn set_distance_function<F>(&mut self, function: F) where F: Fn(f64, f64) -> f64 + Sync + 'static {
self.distance_function = Box::new(function);
}
pub fn set_value_function<F>(&mut self, function: F) where F: Fn(Vec<f64>) -> f64 + Sync + 'static {
self.value_function = Box::new(function);
}
pub fn set_radius(&mut self, radius: u16) {
self.radius = radius;
}
pub fn value(&self, x: f64, y: f64) -> f64 {
let points = self.adjacent_feature_points(x.floor() as i32, y.floor() as i32);
let distances = points.iter()
.map(|&(p_x, p_y)| (p_x - x, p_y - y))
.map(|(x, y)| (self.distance_function)(x, y))
.collect();
(self.value_function)(distances)
}
pub fn values(&self, points: &Vec<(f64, f64)>) -> Vec<f64> {
let mut values = Vec::with_capacity(points.len());
points.par_iter()
.map(|&(x, y)| self.value(x, y))
.collect_into_vec(&mut values);
values
}
}
fn factorial(x: u16) -> u32 {
let mut val = 1;
for i in 2 .. x as u32 + 1 {
val *= i;
}
val
}
fn as_bytes(val: u128) -> [u8; 16] {
use std::mem;
unsafe {
mem::transmute(val)
}
}