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
use crate::prelude::Vector;
use num_traits::{
Float as FloatT, Num as NumT, NumAssignOps, NumAssignRef, NumCast, NumOps, NumRef,
};
use rand::{self, distributions::uniform::SampleUniform, Rng};
use std::ops::{AddAssign, Range};
use once_cell::sync::Lazy;
pub use std::f64::consts::*;
pub trait Num:
NumT + NumOps + NumAssignOps + NumAssignRef + Copy + Default + PartialOrd + PartialEq
{
}
pub trait Float: Num + FloatT {}
impl<T> Num for T where
T: NumT
+ NumOps
+ NumRef
+ NumAssignOps
+ NumAssignRef
+ Copy
+ Default
+ PartialOrd
+ PartialEq
{
}
impl<T> Float for T where T: Num + FloatT {}
const PERLIN_YWRAPB: usize = 4;
const PERLIN_YWRAP: usize = 1 << PERLIN_YWRAPB;
const PERLIN_ZWRAPB: usize = 8;
const PERLIN_ZWRAP: usize = 1 << PERLIN_ZWRAPB;
const PERLIN_SIZE: usize = 4095;
static PERLIN: Lazy<Vec<f64>> = Lazy::new(|| {
let mut perlin = Vec::with_capacity(PERLIN_SIZE + 1);
for _ in 0..=PERLIN_SIZE {
perlin.push(random(1.0));
}
perlin
});
pub fn random_rng<T, R>(val: R) -> T
where
T: SampleUniform + PartialOrd,
R: Into<Range<T>>,
{
let val = val.into();
rand::thread_rng().gen_range(val)
}
pub fn random<T>(val: T) -> T
where
T: Num + SampleUniform + PartialOrd,
{
if val > T::zero() {
random_rng(T::zero()..val)
} else {
random_rng(val..T::zero())
}
}
pub fn noise<V, const N: usize>(vector: V) -> f64
where
V: Into<Vector<f64, N>>,
{
let v = vector.into();
let values = v.coords();
let x = values.first().unwrap_or(&0.0).abs();
let y = values.get(1).unwrap_or(&0.0).abs();
let z = values.get(2).unwrap_or(&0.0).abs();
let mut xi: usize = x.trunc() as usize;
let mut yi: usize = y.trunc() as usize;
let mut zi: usize = z.trunc() as usize;
let mut xf = x.fract();
let mut yf = y.fract();
let mut zf = z.fract();
let (mut rxf, mut ryf);
let mut noise_result = 0.0;
let mut ampl = 0.5;
let (mut n1, mut n2, mut n3);
let scaled_cosine = |i: f64| 0.5 * (1.0 - (i - PI).cos());
let perlin_octaves = 4; let perlin_amp_falloff = 0.5; for _ in 0..perlin_octaves {
let mut of = xi + (yi << PERLIN_YWRAPB) + (zi << PERLIN_ZWRAPB);
rxf = scaled_cosine(xf);
ryf = scaled_cosine(yf);
n1 = PERLIN[of & PERLIN_SIZE];
n1 += rxf * (PERLIN[(of + 1) & PERLIN_SIZE] - n1);
n2 = PERLIN[(of + PERLIN_YWRAP) & PERLIN_SIZE];
n2 += rxf * (PERLIN[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n2);
n1 += ryf * (n2 - n1);
of += PERLIN_ZWRAP;
n2 = PERLIN[of & PERLIN_SIZE];
n2 += rxf * (PERLIN[(of + 1) & PERLIN_SIZE] - n2);
n3 = PERLIN[(of + PERLIN_YWRAP) & PERLIN_SIZE];
n3 += rxf * (PERLIN[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n3);
n2 += ryf * (n3 - n2);
n1 += scaled_cosine(zf) * (n2 - n1);
noise_result += n1 * ampl;
ampl *= perlin_amp_falloff;
xi <<= 1;
xf *= 2.0;
yi <<= 1;
yf *= 2.0;
zi <<= 1;
zf *= 2.0;
if xf >= 1.0 {
xi += 1;
xf -= 1.0;
}
if yf >= 1.0 {
yi += 1;
yf -= 1.0;
}
if zf >= 1.0 {
zi += 1;
zf -= 1.0;
}
}
noise_result
}
#[macro_export]
macro_rules! random {
() => {
$crate::math::random(1.0)
};
($v:expr) => {
$crate::math::random($v)
};
($s:expr, $e:expr$(,)?) => {
$crate::math::random_rng($s..$e)
};
}
#[macro_export]
macro_rules! noise {
($x:expr$(,)?) => {
$crate::math::noise([$x])
};
($x:expr, $y:expr$(,)?) => {
$crate::math::noise([$x, $y])
};
($x:expr, $y:expr, $z:expr$(,)?) => {
$crate::math::noise([$x, $y, $z])
};
}
pub fn map<T>(value: T, start1: T, end1: T, start2: T, end2: T) -> T
where
T: NumCast + Into<f64> + PartialOrd + Copy,
{
let default = end2;
let start1 = start1.into();
let end1 = end1.into();
let start2 = start2.into();
let end2 = end2.into();
let value = value.into();
let new_val = ((value - start1) / (end1 - start1)).mul_add(end2 - start2, start2);
NumCast::from(new_val.clamp(start2, end2)).unwrap_or(default)
}
pub fn lerp<T>(start: T, end: T, amount: T) -> T
where
T: Num + Copy + PartialOrd,
{
(T::one() - amount) * start + amount * end
}
pub fn lerp_map<T>(start1: T, end1: T, start2: T, end2: T) -> Vec<T>
where
T: Num + NumCast + Copy + PartialOrd + AddAssign,
{
if start1 == end1 {
vec![start2]
} else {
let size: usize = NumCast::from(end1 - start1).unwrap_or(4);
let mut values = Vec::with_capacity(size);
let a = (end2 - start2) / (end1 - start1);
let mut d = start2;
let mut i = start1;
while i <= end1 {
values.push(d);
d += a;
i += T::one();
}
values
}
}