Skip to content

Commit 2f9f694

Browse files
authored
Merge pull request #75 from tpoisot/tpoisot/issue74
Perlin noise bug fix
2 parents db77779 + 500ccec commit 2f9f694

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NeutralLandscapes"
22
uuid = "71847384-8354-4223-ac08-659a5128069f"
33
authors = ["Timothée Poisot <timothee.poisot@umontreal.ca>", "Michael Krabbe Borregaard <mkborregaard@sund.ku.dk>", "Michael David Catchen <michael.catchen@mail.mcgill.ca>", "Rafael Schouten <rafaelschouten@gmail.com>", "Virgile Baudrot <virgile.baudrot@posteo.net>"]
4-
version = "0.1.5"
4+
version = "0.1.6"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/makers/perlinnoise.jl

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Note: This is a memory-intensive algorithm with some settings. Be careful using
2323
prime numbers for `period` when also using a large array size, high lacuarity and/or many
2424
octaves. 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
7171
end
7272

7373
function 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
115115
end
116116

117117
function _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)
@@ -127,7 +127,7 @@ end
127127
function _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]
133133
end

0 commit comments

Comments
 (0)