9c77d5cc0f804d5ca1c34e7382c288311d236c8a
[nit.git] / lib / noise.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Provides the noise generators `PerlinNoise` and `InterpolatedNoise`
16 module noise
17
18 # 2D noise generator
19 abstract class Noise
20
21 # Get the noise value at `x`, `y`
22 #
23 # The coordinates `x`, `y` can be floats of any size.
24 #
25 # Returns a value between or equal to `min` and `max`.
26 fun [](x, y: Float): Float is abstract
27
28 # Lowest possible value returned by `[]`
29 #
30 # Default at `0.0`.
31 #
32 # Require: `min < max`
33 var min = 0.0 is writable
34
35 # Highest possible value returned by `[]`
36 #
37 # Default at `1.0`.
38 #
39 # Require: `min < max`
40 var max = 1.0 is writable
41
42 # Distance between reference points of the noise
43 #
44 # Higher values will result in smoother noise and
45 # lower values will result in steeper curves.
46 #
47 # Default at `1.0`.
48 var period = 1.0 is writable
49
50 # Amplitude of the values returned by `[]`
51 fun amplitude: Float do return max - min
52
53 # Set the desired amplitude of the values returned by `[]`
54 #
55 # Will only modify `max`, `min` stays the same.
56 fun amplitude=(value: Float) do max = min + value
57
58 # Frequency of this noise
59 fun frequency: Float do return 1.0/period
60
61 # Set the frequency if this noise
62 fun frequency=(value: Float) do period = 1.0/value
63
64 # Seed to the random number generator `gradient_vector`
65 #
66 # By default, `seed` has a random value created with `Int::rand`.
67 var seed: Int = 19559.rand is lazy, writable
68 end
69
70 # 2D Perlin noise generator using layered `InterpolatedNoise`
71 #
72 # Get values at any coordinates with `[]`.
73 # The behavior of this generator can be customized using its attributes `min`,
74 # `max`, `period` and `seed`.
75 #
76 # This noise is more realistic and less smooth than the `InterpolatedNoise`.
77 #
78 # Due to implementation logic, the full amplitude cannot be reached.
79 # In practice, only `amplitude * (1.0 - 1.0 / n_levels)` is covered.
80 #
81 # This implementation uses a custom deterministic pseudo random number
82 # generator to set `InterpolatedNoise::seed` of the `layers`.
83 # It is seeded with the local `seed` and can be further customized by
84 # redefining `pseudo_random`.
85 # This process do not require any state, so this class only holds the
86 # attributes of the generator and does not keep any generated data.
87 #
88 # ## Usage example
89 #
90 # ~~~
91 # var map = new PerlinNoise
92 # map.min = 0.0
93 # map.max = 16.0
94 # map.period = 20.0
95 # map.seed = 0
96 #
97 # var max = 0.0
98 # var min = 100.0
99 # for y in 30.times do
100 # for x in 70.times do
101 # # Get a value at x, y
102 # var val = map[x.to_f, y.to_f]
103 # printn val.to_i.to_hex
104 #
105 # max = max.max(val)
106 # min = min.min(val)
107 # end
108 # print ""
109 # end
110 # assert max <= map.max
111 # assert min >= map.min
112 # ~~~
113 #
114 # ## Result at seed == 0
115 #
116 # ~~~raw
117 # 7778887888899999888777666666666666666667777877776666666677777777788888
118 # 7788877788899999998877776666666666666667777777766666666667777777778888
119 # 77877777888999aa999888777777776666666667777777666666666666667777778888
120 # 7777777888899aaaa99988877777776666666667777777666666666666667777778888
121 # 7777777888899aaaa99998877777776666666667777777666666666666666777778888
122 # 7777778888999aaaa99999887777777666666666777667666666666656667777788888
123 # 7777778888899aaaa99999888877776666666666666666666666666555666777888888
124 # 7777778888999aaaa99999888887777666666666666666666666666555666778888888
125 # 6677778888999aaaa99999888887777666666677766666666666666555667778888888
126 # 6677788889999aaa999998888877777766666677766667666666666656667788888888
127 # 777788889999999a999998888777777766666677777777766666666666667788888888
128 # 6677888899999999999988888777667776677777777777766666666666666788888888
129 # 6677888999999999999988888776667777777777777777766666665566666788888888
130 # 6777788999999999998888887766667777777777777777666666665566666788888889
131 # 6777788999999999998888877766677777777777777777667666655566666788988999
132 # 7777888999988999988888877766677777777777877777777665555666667788999999
133 # 6778888899888889988888877766666777777777777776666665555666667888999999
134 # 7788888899888888888888877666666777777777777766666665556666677888898999
135 # 7788888899888888888888776666666777777777777666666666566666677888899999
136 # 7788889998888888888877766666666777777777777666666666666666777888899999
137 # 7888999988888888888777666666666777777777776666666666666667777788899988
138 # 7888999988888887777776666666666777777777776666766666666677788888899988
139 # 7788999998888877777776666666666777777777776667776666667778888888899988
140 # 7788999988888777777776666666666777776677766777776677777788888889999988
141 # 6788999988888777777766666666666677676677667777766677777888999999999998
142 # 7788888888888777777776666666666666676677777776666777778889999999999998
143 # 6778888888888777777766666566666666667777666776667777888999999999999999
144 # 6778888888888777777766666566666666667776666776667778889999999999999999
145 # 6778888888777777777666666566666666667776666777677788899999999999888999
146 # 6777787777777777776666666666666666677777677777777888999999999998888899
147 # ~~~
148 class PerlinNoise
149 super Noise
150
151 # Desired number of `layers`
152 #
153 # This attribute must be assigned before any call to `layers` or `[]`.
154 #
155 # By default, it is the highest integer under the logarithm base 2
156 # of `amplitude`, or 4, whichever is the highest.
157 var n_layers: Int = 4.max(amplitude.abs.log_base(2.0).to_i) is lazy, writable
158
159 # Layers of `InterpolatedNoise` composing `self`
160 var layers: Array[InterpolatedNoise] is lazy do
161 var layers = new Array[InterpolatedNoise]
162
163 var max = max
164 var min = min
165 var period = period
166 var seed = seed
167 for l in n_layers.times do
168 min = min / 2.0
169 max = max / 2.0
170 seed = pseudo_random(seed)
171
172 var layer = new InterpolatedNoise
173 layer.min = min
174 layer.max = max
175 layer.period = period
176 layer.seed = seed
177 layers.add layer
178
179 period = period / 2.0
180 end
181 return layers
182 end
183
184 redef fun [](x, y)
185 do
186 var val = 0.0
187 for layer in layers do
188 val += layer[x, y]
189 end
190 return val
191 end
192
193 # Deterministic pseudo random number generator
194 #
195 # Used to get seeds for layers from the previous layers or `seed`.
196 protected fun pseudo_random(value: Int): Int
197 do
198 return value + 2935391 % 954847
199 end
200 end
201
202 # Simple interpolated noise
203 #
204 # Generates smoother noise than `PerlinNoise`.
205 #
206 # Each coordinates at a multiple of `period` defines a random vector and
207 # values in between are interpolated from these vectors.
208 #
209 # This implementation uses a custom deterministic pseudo random number
210 # generator seeded with `seed`.
211 # It can be further customized by redefining `gradient_vector`.
212 # This process do not require any state, so this class only holds the
213 # attributes of the generator and does not keep any generated data.
214 #
215 # ## Usage example
216 #
217 # ~~~
218 # var map = new InterpolatedNoise
219 # map.min = 0.0
220 # map.max = 16.0
221 # map.period = 20.0
222 # map.seed = 0
223 #
224 # var max = 0.0
225 # var min = 100.0
226 # for y in 30.times do
227 # for x in 70.times do
228 # # Get a value at x, y
229 # var val = map[x.to_f, y.to_f]
230 # printn val.to_i.to_hex
231 #
232 # max = max.max(val)
233 # min = min.min(val)
234 # end
235 # print ""
236 # end
237 # assert max <= map.max
238 # assert min >= map.min
239 # ~~~
240 #
241 # ## Result at seed == 0
242 #
243 # ~~~raw
244 # 8777766666666666777787777666666666667777888999aaaaaaaaa999888777766666
245 # 88877776666666667777777766666666666667777889999aaaaaaa9999887777666666
246 # 98887777776666667777777666666666666666777889999aaaaaaa9999887776666666
247 # 998888777777766777777776666666666666667778889999aaaaa99998887776666666
248 # a998888777777777777777666666666666666667778899999aaa999998877766666666
249 # aa99888877777777777777666666655566666667778899999999999998877766666665
250 # aa99988877777777777777666666555556666667778889999999999988877766666655
251 # aaa9988887777777777777666665555555666667778889999999999988877766666555
252 # baa9998887777777777776666665555555666666778889999999999988877666666555
253 # baa9998887777777777776666665555555666666778889999999999988877666666555
254 # baa9998887777777777776666655555555566666778889999999999988877666665555
255 # baa9998887777777777776666665555555666666778889999999999988877666666555
256 # baa9998887777777777776666665555555666666778889999999999988877666666555
257 # aaa9988887777777777777666665555555666667778889999999999988877766666555
258 # aa99988877777777777777666666555556666667778889999999999988877766666655
259 # aa99888877777777777777666666655566666667778899999999999998877766666665
260 # a998888777777777777777666666666666666667778899999aaa999998877766666666
261 # 998888777777766777777776666666666666667778889999aaaaa99998887776666666
262 # 98887777776666667777777666666666666666777889999aaaaaaa9999887776666666
263 # 88877776666666667777777766666666666667777889999aaaaaaa9999887777666666
264 # 8777766666666666777787777666666666667777888999aaaaaaaaa999888777766666
265 # 777766666666666667777777777666677777778889999aaaaaaaaaa999887777666666
266 # 7776666666666666667777777777777777778888999aaaaaaaaaaa9999887776666666
267 # 777666666666666666777777777777777888889999aaaaaaaaaaaa9998887776666666
268 # 7766666666666666666777777777778888889999aaaaaaaaaaaaa99998877766666666
269 # 776666666555666666677777777888888899999aaaaaaaaaaaaaa99998877766666665
270 # 7766666655555666666777777788888899999aaaaaaaaaaaaaaa999988877766666655
271 # 776666655555556666677777788888999999aaaaaaaaaaaaaaaa999988877766666555
272 # 7666666555555566666677778888899999aaaaaabbbbaaaaaaa9999888777666666555
273 # 766666655555556666667778888899999aaaaaabbbbaaaaaaa99999888777666666555
274 # ~~~
275 class InterpolatedNoise
276 super Noise
277
278 redef fun [](x, y)
279 do
280 x = x/period
281 y = y/period
282
283 # Get grid coordinates
284 var x0 = if x > 0.0 then x.to_i else x.to_i - 1
285 var x1 = x0 + 1
286 var y0 = if y > 0.0 then y.to_i else y.to_i - 1
287 var y1 = y0 + 1
288
289 # Position in grid
290 var sx = x - x0.to_f
291 var sy = y - y0.to_f
292
293 # Interpolate
294 var n0 = gradient_dot_product(x0, y0, x, y)
295 var n1 = gradient_dot_product(x1, y0, x, y)
296 var ix0 = sx.lerp(n0, n1)
297 n0 = gradient_dot_product(x0, y1, x, y)
298 n1 = gradient_dot_product(x1, y1, x, y)
299 var ix1 = sx.lerp(n0, n1)
300 var val = sy.lerp(ix0, ix1)
301
302 # Return value in [min...max] from val in [-0.5...0.5]
303 val += 0.5
304 return val.lerp(min, max)
305 end
306
307 # Get the gradient vector at `x`, `y` on the axis `w`
308 #
309 # Returns a value between 0.0 and 1.0.
310 #
311 # Require: `w == 0 or w == 1`
312 protected fun gradient_vector(x, y, w: Int): Float
313 do
314 assert w == 0 or w == 1
315
316 # Use our own deterministic pseudo random number generator
317 #
318 # These magic prime numbers were determined good enough by
319 # non-emperical experimentation. They may need to be changed/improved.
320 var i = 17957*seed + 45127*x + 22613*y + 36919*w
321 var mod = 19031
322
323 return (i%mod).to_f/mod.to_f
324 end
325
326 private fun gradient_dot_product(ix, iy: Int, x, y: Float): Float
327 do
328 var dx = x - ix.to_f
329 var dy = y - iy.to_f
330
331 return dx*gradient_vector(ix, iy, 0) + dy*gradient_vector(ix, iy, 1)
332 end
333 end