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
use num_traits::Float;
use math;
use math::{Point2, Point3, Point4};
use {NoiseModule, PermutationTable, gradient};
#[derive(Clone, Copy, Debug)]
pub struct Perlin {
perm_table: PermutationTable,
}
impl Perlin {
pub fn new(seed: usize) -> Perlin {
Perlin { perm_table: PermutationTable::new(seed as u32) }
}
}
impl<T: Float> NoiseModule<Point2<T>> for Perlin {
type Output = T;
fn get(&self, point: Point2<T>) -> T {
#[inline(always)]
fn surflet<T: Float>(perm_table: &PermutationTable,
corner: math::Point2<isize>,
distance: math::Vector2<T>)
-> T {
let attn = T::one() - math::dot2(distance, distance);
if attn > T::zero() {
math::pow4(attn) * math::dot2(distance, gradient::get2(perm_table.get2(corner)))
} else {
T::zero()
}
}
let floored = math::map2(point, T::floor);
let near_corner = math::map2(floored, math::cast);
let far_corner = math::add2(near_corner, math::one2());
let near_distance = math::sub2(point, floored);
let far_distance = math::sub2(near_distance, math::one2());
let f00 = surflet(&self.perm_table,
[near_corner[0], near_corner[1]],
[near_distance[0], near_distance[1]]);
let f10 = surflet(&self.perm_table,
[far_corner[0], near_corner[1]],
[far_distance[0], near_distance[1]]);
let f01 = surflet(&self.perm_table,
[near_corner[0], far_corner[1]],
[near_distance[0], far_distance[1]]);
let f11 = surflet(&self.perm_table,
[far_corner[0], far_corner[1]],
[far_distance[0], far_distance[1]]);
(f00 + f10 + f01 + f11) * math::cast(3.1604938271604937)
}
}
impl<T: Float> NoiseModule<Point3<T>> for Perlin {
type Output = T;
fn get(&self, point: Point3<T>) -> T {
#[inline(always)]
fn surflet<T: Float>(perm_table: &PermutationTable,
corner: math::Point3<isize>,
distance: math::Vector3<T>)
-> T {
let attn = T::one() - math::dot3(distance, distance);
if attn > T::zero() {
math::pow4(attn) * math::dot3(distance, gradient::get3(perm_table.get3(corner)))
} else {
T::zero()
}
}
let floored = math::map3(point, T::floor);
let near_corner = math::map3(floored, math::cast);
let far_corner = math::add3(near_corner, math::one3());
let near_distance = math::sub3(point, floored);
let far_distance = math::sub3(near_distance, math::one3());
let f000 = surflet(&self.perm_table,
[near_corner[0], near_corner[1], near_corner[2]],
[near_distance[0], near_distance[1], near_distance[2]]);
let f100 = surflet(&self.perm_table,
[far_corner[0], near_corner[1], near_corner[2]],
[far_distance[0], near_distance[1], near_distance[2]]);
let f010 = surflet(&self.perm_table,
[near_corner[0], far_corner[1], near_corner[2]],
[near_distance[0], far_distance[1], near_distance[2]]);
let f110 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], near_corner[2]],
[far_distance[0], far_distance[1], near_distance[2]]);
let f001 = surflet(&self.perm_table,
[near_corner[0], near_corner[1], far_corner[2]],
[near_distance[0], near_distance[1], far_distance[2]]);
let f101 = surflet(&self.perm_table,
[far_corner[0], near_corner[1], far_corner[2]],
[far_distance[0], near_distance[1], far_distance[2]]);
let f011 = surflet(&self.perm_table,
[near_corner[0], far_corner[1], far_corner[2]],
[near_distance[0], far_distance[1], far_distance[2]]);
let f111 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], far_corner[2]],
[far_distance[0], far_distance[1], far_distance[2]]);
(f000 + f100 + f010 + f110 + f001 + f101 + f011 + f111) * math::cast(3.8898553255531074)
}
}
impl<T: Float> NoiseModule<Point4<T>> for Perlin {
type Output = T;
fn get(&self, point: Point4<T>) -> T {
#[inline(always)]
fn surflet<T: Float>(perm_table: &PermutationTable,
corner: math::Point4<isize>,
distance: math::Vector4<T>)
-> T {
let attn = T::one() - math::dot4(distance, distance);
if attn > T::zero() {
math::pow4(attn) * math::dot4(distance, gradient::get4(perm_table.get4(corner)))
} else {
T::zero()
}
}
let floored = math::map4(point, T::floor);
let near_corner = math::map4(floored, math::cast);
let far_corner = math::add4(near_corner, math::one4());
let near_distance = math::sub4(point, floored);
let far_distance = math::sub4(near_distance, math::one4());
let f0000 =
surflet(&self.perm_table,
[near_corner[0], near_corner[1], near_corner[2], near_corner[3]],
[near_distance[0], near_distance[1], near_distance[2], near_distance[3]]);
let f1000 =
surflet(&self.perm_table,
[far_corner[0], near_corner[1], near_corner[2], near_corner[3]],
[far_distance[0], near_distance[1], near_distance[2], near_distance[3]]);
let f0100 =
surflet(&self.perm_table,
[near_corner[0], far_corner[1], near_corner[2], near_corner[3]],
[near_distance[0], far_distance[1], near_distance[2], near_distance[3]]);
let f1100 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], near_corner[2], near_corner[3]],
[far_distance[0], far_distance[1], near_distance[2], near_distance[3]]);
let f0010 =
surflet(&self.perm_table,
[near_corner[0], near_corner[1], far_corner[2], near_corner[3]],
[near_distance[0], near_distance[1], far_distance[2], near_distance[3]]);
let f1010 = surflet(&self.perm_table,
[far_corner[0], near_corner[1], far_corner[2], near_corner[3]],
[far_distance[0], near_distance[1], far_distance[2], near_distance[3]]);
let f0110 = surflet(&self.perm_table,
[near_corner[0], far_corner[1], far_corner[2], near_corner[3]],
[near_distance[0], far_distance[1], far_distance[2], near_distance[3]]);
let f1110 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], far_corner[2], near_corner[3]],
[far_distance[0], far_distance[1], far_distance[2], near_distance[3]]);
let f0001 =
surflet(&self.perm_table,
[near_corner[0], near_corner[1], near_corner[2], far_corner[3]],
[near_distance[0], near_distance[1], near_distance[2], far_distance[3]]);
let f1001 = surflet(&self.perm_table,
[far_corner[0], near_corner[1], near_corner[2], far_corner[3]],
[far_distance[0], near_distance[1], near_distance[2], far_distance[3]]);
let f0101 = surflet(&self.perm_table,
[near_corner[0], far_corner[1], near_corner[2], far_corner[3]],
[near_distance[0], far_distance[1], near_distance[2], far_distance[3]]);
let f1101 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], near_corner[2], far_corner[3]],
[far_distance[0], far_distance[1], near_distance[2], far_distance[3]]);
let f0011 = surflet(&self.perm_table,
[near_corner[0], near_corner[1], far_corner[2], far_corner[3]],
[near_distance[0], near_distance[1], far_distance[2], far_distance[3]]);
let f1011 = surflet(&self.perm_table,
[far_corner[0], near_corner[1], far_corner[2], far_corner[3]],
[far_distance[0], near_distance[1], far_distance[2], far_distance[3]]);
let f0111 = surflet(&self.perm_table,
[near_corner[0], far_corner[1], far_corner[2], far_corner[3]],
[near_distance[0], far_distance[1], far_distance[2], far_distance[3]]);
let f1111 = surflet(&self.perm_table,
[far_corner[0], far_corner[1], far_corner[2], far_corner[3]],
[far_distance[0], far_distance[1], far_distance[2], far_distance[3]]);
(f0000 + f1000 + f0100 + f1100 + f0010 + f1010 + f0110 + f1110 + f0001 +
f1001 + f0101 + f1101 + f0011 + f1011 + f0111 + f1111) *
math::cast(4.424369240215691)
}
}