misc/vim: inform the user when no results are found
[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 # 76666555444322234567789abbcbbaabbaa98777766665665566667888987655444444
118 # 776665554443322234567789abbbbbbbbba98777766666665556666788998654444444
119 # 777766544443322234566789abbbbbbbbaa99877777776665556666788888655444444
120 # 777776444443322244556679abbbccbbbaa99877777776655556666688888655444444
121 # 777766444444332244555678abbbccbbbaa99887787877655556666678888654444444
122 # 8887654344443333444456789abcccbbaa999877888886555555666688777654444455
123 # 8887654344443333444456789abbcdcbaa999887889887655555566677777654444456
124 # 7876654434444444444456778abbcccaaa999888899888655555566677777654444556
125 # 78765544344445544444567789bbccca99999888899988765555566666667654445566
126 # 77765444344455554445567889bbccba99999998999988765555566555666654445667
127 # 7765444334555665445556788abbbba988998999999988765555566545556554456677
128 # 87654444334556655455567899bbbba998888899999887766555566544556555456777
129 # 87655444334566665555567899bbbbba98888899988888776555566544556555556777
130 # 97655544334566665555567899abbbba98888899988888776555655544456555667777
131 # 97655544444566665556667899aaaaba98888999877777776555555444456666667777
132 # 866555444456666666566789999aaaaa98889998877777766556544443456667777777
133 # 976555445556776666666789aa99aaaa98889998876777666555544444456677887777
134 # 9765554556667777776667899999aaaa98889988876676666555443444446678888888
135 # 87655555666777788766678999899aaa99889988776666666554433344446789998888
136 # 876555566777788888766889998899a999889987776666666543333334456899a99899
137 # 766556677877889998877888888889a99998888777666666653222233345799aaa999a
138 # 6665556777777899998878988888899999999887777656666543222233446899aa999a
139 # 6655456777777899999888988888889999a988887776566666532222233457899a999a
140 # 665555677777789999998998888878899aa9888887765666655322222234578899aa9a
141 # 665555677777789999a98888888877899aa9888887766666655322222234467899aa9a
142 # 65666677667778999aaa988878877789aaa9888887776676654322222344467889aa9a
143 # 55566677767788899aaa987777777789aaa9888887776666654322222344567889aaa9
144 # 5566767777788889aaaa987777777789aaaa988887777666555432122344556899aaa9
145 # 5567777777788889aaaa977777777789aaaa99888777766555543212234555689aaaaa
146 # 5667877777889989aaa9876677777889aaaa99888777765554443212334555689aaaaa
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 # 89abcddeeeeeeeddcba9877666555555555666778766555544444555566789abcddeee
245 # 789abcddeeeeeeddccba887766655555555566677766555544444555566779abcddeee
246 # 689abcddeeeeeeeddcba988776655555555555667666555554455555566778abccdeee
247 # 678abccdeeeeeeeedccba988766655555555555666655555555555556666789abcddee
248 # 5789abcddeeeeeeeddcba998776655544444555666655555555555556666789abcddee
249 # 5689abcddeeeeeeeedccba98776655544444455566555555555555566666789abccdde
250 # 4679abccdeeeffeeeddcba98776655444444445565555555555555666666789abbcddd
251 # 4678abccdeeeffeeeedcba98876555444444444555555555566666666666689aabccdd
252 # 46789abcdeeeeffeeedccb988765544443344445555566666666666666666789abccdd
253 # 45789abcddeeeffeeeddcb987765544433334445555666666666666666666789abbccd
254 # 45789abcddeeeeeeeeddcb987665444333333445556666666777777777766789aabccc
255 # 45789abcddeeeeeeeeddca987655443333333445566666777777777777776789aabbcc
256 # 45789abcddeeeeeeeedcca9876544333333333455666777777788877777767899aabbc
257 # 46789abcddeeeeeeeddcba9876544333222333455667777888888888877767899aabbb
258 # 46789abcdddeeeeedddcba87655433222223334566777888889998888877778899aabb
259 # 5678aabcdddeeeedddccb987654332222222334566778889999999998887778899aaab
260 # 5689abbcddddeedddccba9865443222222223345677889999aaaa99998877788999aaa
261 # 6789abbcddddddddccbba8765432221111223345678899aaaaaaaaaa9988778889999a
262 # 6789abccdddddddccbba9865433221111122344577899aabbbbbbbaaa9987788889999
263 # 789abbccddddddccbba9876543211111111234567899aabbbccccbbbaa987788888899
264 # 889abbccdddddccbba9886543211000001123456889abbcccccccccbba988888888888
265 # 899abbcccddddcccbaa9875432211000011223457899abbcccccccccbba98888888888
266 # 899abbccccddccccbba9876533211000001123456789aabccccddcccbbaa9998888888
267 # 899abbccccccccccbbaa9765432111000011223456899abbcccdddcccbba9999988888
268 # 899abbbcccccccccbbaa9865432211000011123456789abbccdddddcccbba999988888
269 # 899aabbcccccccccbbaa9875433211100001122346789abbccddddddcccbaa99988888
270 # 899aabbbcccccccbbbbaa876543211100001122345689aabccdddddddccbaaa9988887
271 # 899aabbbbbbccbbbbbbaa876543221110001112335679aabccddddddddcbbaa9988877
272 # 899aaabbbbbbbbbbbbbaa9765433211111111123356789abccddddddddccbaa9988777
273 # 8999aaaabbbbbbbbbbaaa9765433221111111122356789abccdddeedddccbaa9988777
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 component `w` of the gradient unit vector at `x`, `y`
308 #
309 # `w` at 0 targets the X axis, at 1 the Y axis.
310 #
311 # Returns a value between -1.0 and 1.0.
312 #
313 # Require: `w == 0 or w == 1`
314 protected fun gradient_vector(x, y, w: Int): Float
315 do
316 assert w == 0 or w == 1
317
318 # Use our own deterministic pseudo random number generator
319 #
320 # These magic prime numbers were determined good enough by
321 # non-emperical experimentation. They may need to be changed/improved.
322 var i = 17957*seed + 45127*x + 22613*y
323 var mod = 19031
324
325 var angle = (i%mod).to_f*2.0*pi/mod.to_f
326 if w == 0 then return angle.cos
327 return angle.sin
328 end
329
330 private fun gradient_dot_product(ix, iy: Int, x, y: Float): Float
331 do
332 var dx = x - ix.to_f
333 var dy = y - iy.to_f
334
335 return dx*gradient_vector(ix, iy, 0) + dy*gradient_vector(ix, iy, 1)
336 end
337 end