@@ -23,7 +23,7 @@ Note: This is a memory-intensive algorithm with some settings. Be careful using
2323prime numbers for `period` when also using a large array size, high lacuarity and/or many
2424octaves. Memory use scales with the lowest common multiple of `periods`.
2525"""
26- @kwdef struct PerlinNoise <: NeutralLandscapeMaker
26+ @kwdef struct PerlinNoise <: NeutralLandscapeMaker
2727 periods:: Tuple{Int,Int} = (1 , 1 )
2828 octaves:: Int = 1
2929 lacunarity:: Int = 2
@@ -50,35 +50,35 @@ function _landscape!(A, alg::PerlinNoise)
5050 noise = zeros (eltype (A), (dim, dim))
5151 meshbuf1 = Array {eltype(A),2} (undef, dim, dim)
5252 meshbuf2 = Array {eltype(A),2} (undef, dim, dim)
53- nbufs = ntuple (_-> Array {eltype(A),2} (undef, dim, dim), 4 )
53+ nbufs = ntuple (_ -> Array {eltype(A),2} (undef, dim, dim), 4 )
5454 for octave in 0 : alg. octaves- 1
5555 octave_noise! (noise, meshbuf1, meshbuf2, nbufs, alg, octave, dim, dim)
5656 end
5757
5858 # Randomly extract the desired array size
59- noiseview = _view_from_square (noise, size (A)... )
60-
59+ noiseview = _view_from_square (noise, size (A)... )
60+
6161 # Rescale the Perlin noise to mimic different kinds of valley bottoms
6262 return if alg. valley == :u
6363 A .= noiseview
6464 elseif alg. valley == :v
6565 A .= abs .(noiseview)
6666 elseif alg. valley == :-
67- A .= noiseview.^ 2
68- else
67+ A .= noiseview .^ 2
68+ else
6969 error (" $(alg. valley) not recognised for `valley` use `:u`, `:v` or `:-`" )
7070 end
7171end
7272
7373function octave_noise! (
7474 noise, m1, m2, (n11, n21, n12, n22), alg:: PerlinNoise , octave, nrow, ncol
75- )
76- f (t) = @fastmath 6 * t ^ 5 - 15 * t ^ 4 + 10 * t ^ 3 # Wut
75+ )
76+ f (t) = @fastmath 6 * t^ 5 - 15 * t^ 4 + 10 * t^ 3 # Wut
7777
7878 # Mesh
7979 rp, cp = alg. periods .* alg. lacunarity^ (octave)
8080 delta = (rp / nrow, cp / ncol)
81- ranges = range (0 , rp- delta[1 ], length= nrow), range (0 , cp- delta[2 ], length= ncol)
81+ ranges = range (0 , rp - delta[1 ], length= nrow), range (0 , cp - delta[2 ], length= ncol)
8282 _rem_meshes! (m1, m2, ranges... )
8383
8484 # Gradients
@@ -90,32 +90,32 @@ function octave_noise!(
9090 d = (nrow ÷ rp, ncol ÷ cp)
9191 grad = repeat (gradients, inner= [d[1 ], d[2 ], 1 ])
9292
93- g111 = @view grad[1 : ncol, 1 : ncol, 1 ]
94- g211 = @view grad[end - nrow+ 1 : end , 1 : ncol, 1 ]
95- g121 = @view grad[1 : ncol, end - ncol+ 1 : end , 1 ]
93+ g111 = @view grad[1 : ncol, 1 : ncol, 1 ]
94+ g211 = @view grad[end - nrow+ 1 : end , 1 : ncol, 1 ]
95+ g121 = @view grad[1 : ncol, end - ncol+ 1 : end , 1 ]
9696 g221 = @view grad[end - nrow+ 1 : end , end - ncol+ 1 : end , 1 ]
97- g112 = @view grad[1 : ncol, 1 : ncol, 2 ]
98- g212 = @view grad[end - nrow+ 1 : end , 1 : ncol, 2 ]
99- g122 = @view grad[1 : ncol, end - ncol+ 1 : end , 2 ]
97+ g112 = @view grad[1 : ncol, 1 : ncol, 2 ]
98+ g212 = @view grad[end - nrow+ 1 : end , 1 : ncol, 2 ]
99+ g122 = @view grad[1 : ncol, end - ncol+ 1 : end , 2 ]
100100 g222 = @view grad[end - nrow+ 1 : end , end - ncol+ 1 : end , 2 ]
101101
102102 # Ramps
103- n11 .= (( m1 .+ m2 ) . * g111 .+ (m1 .+ m2 ) .* g112)
104- n21 .= ((m1 .- 1 .+ m2 ) .* g211 .+ (m1 .- 1 .+ m2 ) .* g212)
105- n12 .= (( m1 .+ m2 .- 1 ) . * g121 .+ (m1 .+ m2 .- 1 ) .* g122)
106- n22 .= ((m1 .- 1 .+ m2 .- 1 ) .* g221 .+ (m1 .- 1 .+ m2 .- 1 ) .* g222)
103+ n11 .= (m1 . * g111 .+ m2 .* g112) # Bottom-left
104+ n21 .= ((m1 .- 1 ) .* g211 .+ m2 .* g212) # Top-left
105+ n12 .= (m1 . * g121 .+ (m2 .- 1 ) .* g122) # Bottom-right
106+ n22 .= ((m1 .- 1 ) .* g221 .+ (m2 .- 1 ) .* g222) # Top-right
107107
108108 # Interpolation
109109 m1 .= f .(m1)
110110 m2 .= f .(m2)
111- noise .+ = sqrt (2 ) .* (alg. persistance ^ octave) .*
112- ((1 .- m2) .* (n11 .* (1 .- m1) .+ m1 .* n21) .+
113- m2 .* (n12 .* (1 .- m1) .+ m1 .* n22))
111+ noise .+ = sqrt (2 ) .* (alg. persistance^ octave) .*
112+ ((1 .- m2) .* (n11 .* (1 .- m1) .+ m1 .* n21) .+
113+ m2 .* (n12 .* (1 .- m1) .+ m1 .* n22))
114114 return noise
115115end
116116
117117function _rem_meshes! (m1, m2, x, y)
118- for (i, ival) in enumerate (x), j in 1 : length (y)
118+ for (i, ival) in enumerate (x), j in 1 : length (y)
119119 @fastmath m1[i, j] = ival % 1
120120 end
121121 for i in 1 : length (x), (j, jval) in enumerate (y)
127127function _view_from_square (source, nrow, ncol)
128128 # Extract a portion of the array to match the dimensions
129129 dim = size (source, 1 )
130- startrow = rand (1 : (dim - nrow + 1 ))
131- startcol = rand (1 : (dim - ncol + 1 ))
132- return @view source[startrow: startrow + nrow - 1 , startcol: startcol + ncol - 1 ]
130+ startrow = rand (1 : (dim- nrow+ 1 ))
131+ startcol = rand (1 : (dim- ncol+ 1 ))
132+ return @view source[startrow: startrow+ nrow- 1 , startcol: startcol+ ncol- 1 ]
133133end
0 commit comments