1#[cfg(any(feature = "rand", feature = "num-std", feature = "num-libm"))]
4use super::Signal;
5
6#[cfg(feature = "rand")]
7use rand::{distributions::Distribution, Rng};
8
9#[cfg(any(feature = "num-std", feature = "num-libm"))]
10use crate::num::Phase;
11
12#[cfg(feature = "rand")]
14#[derive(Clone)]
15pub struct Random<R, D, T> {
16 pub(super) rng: R,
17 pub(super) dist: D,
18 pub(super) _marker: core::marker::PhantomData<T>,
19}
20
21#[cfg(feature = "rand")]
22impl<R, D, T> Signal for Random<R, D, T>
23where
24 R: Rng,
25 D: Distribution<T>,
26{
27 type Type = T;
28
29 #[inline]
30 fn next(&mut self) -> Self::Type {
31 self.dist.sample(&mut self.rng)
32 }
33}
34
35#[cfg(any(feature = "num-std", feature = "num-libm"))]
37#[derive(Clone)]
38pub struct Simplex<S: Signal<Type = f64>> {
39 perm: [u8; 256],
40 phase: Phase<S>,
41}
42
43#[cfg(any(feature = "num-std", feature = "num-libm"))]
44impl<S: Signal<Type = f64>> Simplex<S> {
45 #[inline]
47 pub fn default(steps: S) -> Self {
48 Self {
49 perm: [
50 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36,
51 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0,
52 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87,
53 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146,
54 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40,
55 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18,
56 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
57 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206,
58 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2,
59 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98,
60 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242,
61 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
62 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4,
63 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66,
64 215, 61, 156, 180,
65 ],
66 phase: Phase::new(steps, 256f64),
67 }
68 }
69
70 #[cfg(feature = "rand")]
72 pub fn with_rng<R: rand::Rng + ?Sized>(rng: &mut R, steps: S) -> Self {
73 use core::mem::MaybeUninit;
74 use rand::seq::SliceRandom;
75
76 let mut perm = [MaybeUninit::uninit(); 256];
77
78 for i in 0..=255u8 {
79 perm[i as usize] = MaybeUninit::new(i);
80 }
81
82 perm.shuffle(rng);
83
84 Self {
86 perm: unsafe { core::mem::transmute(perm) },
87 phase: Phase::new(steps, 256f64),
88 }
89 }
90}
91
92#[cfg(any(feature = "num-std", feature = "num-libm"))]
93impl<S> Signal for Simplex<S>
94where
95 S: Signal<Type = f64>,
96{
97 type Type = f64;
98
99 fn next(&mut self) -> Self::Type {
102 fn hash(perm: &[u8; 256], i: i64) -> u8 {
104 unsafe { *perm.get_unchecked(i as u8 as usize) }
107 }
108
109 fn grad(hash: i64, x: f64) -> f64 {
111 let h = hash & 0x0F; let mut grad = (1 + (h & 7)) as f64; if (h & 8) != 0 {
114 grad = -grad;
115 }
116 grad * x
117 }
118
119 let phase = self.phase.next_phase();
120 let phase_floor = num_traits::Float::floor(phase);
121
122 let i0 = phase_floor as i64;
124 let i1 = i0 + 1;
125
126 let x0 = phase - phase_floor;
128 let x1 = x0 - 1.0;
129
130 let mut t0 = 1.0 - x0 * x0;
132 t0 *= t0;
133 let n0 = t0 * t0 * grad(hash(&self.perm, i0) as i64, x0);
134
135 let mut t1 = 1.0 - x1 * x1;
137 t1 *= t1;
138 let n1 = t1 * t1 * grad(hash(&self.perm, i1) as i64, x1);
139
140 0.395 * (n0 + n1)
143 }
144}