Skip to content

Commit

Permalink
Merge pull request #52 from EcoJulia/rs/constructors
Browse files Browse the repository at this point in the history
add default arg constructors, and test all constructors
  • Loading branch information
tpoisot authored Apr 1, 2021
2 parents 8b84f18 + 709e238 commit 49e41d6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/algorithms/nncluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
NearestNeighborCluster <: NeutralLandscapeMaker
NearestNeighborCluster(; p=0.5, n=:rook)
NearestNeighborCluster(p, n)
NearestNeighborCluster(p, [n=:rook])
Create a random cluster nearest-neighbour neutral landscape model with
values ranging 0-1. `p` sets the density of original clusters, and `n`
Expand All @@ -11,7 +11,7 @@ sets the neighborhood for clustering (see `?label` for neighborhood options)
@kwdef struct NearestNeighborCluster <: NeutralLandscapeMaker
p::Float64 = 0.5
n::Symbol = :rook
function NearestNeighborCluster(p::Float64, n::Symbol)
function NearestNeighborCluster(p::Float64, n::Symbol = :rook)
@assert p > 0
@assert n (:rook, :queen, :diagonal)
new(p,n)
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/nnelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
NearestNeighborElement <: NeutralLandscapeMaker
NearestNeighborElement(; n=3, k=1)
NearestNeighborElement(n, k)
NearestNeighborElement(n, [k=1])
Assigns a value to each patch using a k-NN algorithmm with `n` initial clusters
and `k` neighbors. The default is to use three cluster and a single neighbor.
Expand Down
11 changes: 7 additions & 4 deletions src/algorithms/perlinnoise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
PerlinNoise <: NeutralLandscapeMaker
PerlinNoise(; kw...)
PerlinNoise(periods, octaves, lacunarity, persistance, valley)
PerlinNoise(periods, [octaves=1, lacunarity=2, persistance=0.5, valley=:u])
Create a Perlin noise neutral landscape model with values ranging 0-1.
Expand All @@ -13,7 +13,7 @@ Create a Perlin noise neutral landscape model with values ranging 0-1.
- `octaves::Int=1`: the number of octaves that will form the Perlin noise.
- `lacunarity::Int=2` : the rate at which the frequency of periods increases for each
octive.
- `persistance::Real=0.5` : the rate at which the amplitude of periods decreases for each
- `persistance::Float64=0.5` : the rate at which the amplitude of periods decreases for each
octive.
- `valley::Symbol=`:u`: the kind of valley bottom that will be mimicked: `:u` produces
u-shaped valleys, `:v` produces v-shaped valleys, and `:-` produces flat bottomed
Expand All @@ -23,12 +23,15 @@ Note: This is a memory-intensive algorithm with some settings. Be careful using
prime numbers for `period` when also using a large array size, high lacuarity and/or many
octaves. Memory use scales with the lowest common multiple of `periods`.
"""
@kwdef struct PerlinNoise{PT<:Real} <: NeutralLandscapeMaker
@kwdef struct PerlinNoise <: NeutralLandscapeMaker
periods::Tuple{Int,Int} = (1, 1)
octaves::Int = 1
lacunarity::Int = 2
persistance::PT = 0.5
persistance::Float64 = 0.5
valley::Symbol = :u
function PerlinNoise(periods, octaves=1, lacunarity=2, persistance=0.5, valley=:u)
new(periods, octaves, lacunarity, persistance, valley)
end
end

function _landscape!(A, alg::PerlinNoise)
Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/rectangularcluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RectangularCluster <: NeutralLandscapeMaker
RectangularCluster(; minimum=2, maximum=4)
RectangularCluster(minimum, maximum)
RectangularCluster(minimum, [maximum=4])
Fills the landscape with rectangles containing a random value. The size of each
rectangle/patch is between `minimum` and `maximum` (the two can be equal for a
Expand All @@ -11,9 +11,9 @@ fixed size rectangle).
@kwdef struct RectangularCluster <: NeutralLandscapeMaker
minimum::Integer = 2
maximum::Integer = 4
function RectangularCluster(x::T, y::T) where {T <: Integer}
@assert 0 < x <= y
new(x, y)
function RectangularCluster(minimum::T, maximum::T=4) where {T <: Integer}
@assert 0 < minimum <= maximum
new(minimum, maximum)
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/wavesurface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
WaveSurface <: NeutralLandscapeMaker
WaveSurface(; direction=360rand(), periods=1)
WaveSurface(direction, periods)
WaveSurface(direction, [periods=1])
Creates a sinusoidal landscape with a `direction` and a number of `periods`. If
neither are specified, there will be a single period of random direction.
"""
@kwdef struct WaveSurface <: NeutralLandscapeMaker
direction::Float64 = 360rand()
periods::Int64 = 1
function WaveSurface(x::T, y::K) where {T <: Real, K <: Integer}
@assert y >= 1
new(mod(x, 360.0), y)
function WaveSurface(direction::T, periods::K = 1) where {T <: Real, K <: Integer}
@assert periods >= 1
new(mod(direction, 360.0), periods)
end
end

Expand Down
25 changes: 25 additions & 0 deletions test/rand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@ using NeutralLandscapes, Test

algorithms = (
DiamondSquare(),
DiamondSquare(; H=0.2),
DiamondSquare(0.4),
DiscreteVoronoi(),
DiscreteVoronoi(; n=2),
DiscreteVoronoi(3),
DistanceGradient(),
DistanceGradient(; sources=[2]),
DistanceGradient([1, 2]),
EdgeGradient(),
EdgeGradient(; direction=120.0),
EdgeGradient(90.0),
MidpointDisplacement(),
MidpointDisplacement(;),
NearestNeighborElement(),
NearestNeighborElement(; k=1),
NearestNeighborElement(2),
NearestNeighborCluster(),
# NearestNeighborCluster(; n=:queen),
NearestNeighborCluster(; n=:diagonal),
NearestNeighborCluster(0.2, :diagonal),
NoGradient(),
PerlinNoise(),
PerlinNoise(; octaves=3, lacunarity=3, persistance=0.3),
PerlinNoise((1, 2), 3, 2, 0.2),
PlanarGradient(),
PlanarGradient(; direction=120.0),
PlanarGradient(90),
RectangularCluster(),
RectangularCluster(; maximum=5),
RectangularCluster(2),
WaveSurface(),
WaveSurface(90.0),
WaveSurface(; periods=2),
)

sizes = (50, 200), (100, 100), (301, 87)
Expand Down

0 comments on commit 49e41d6

Please sign in to comment.