Skip to content

Commit 2d9f746

Browse files
committed
change poisson to use a 1D array instead of a 2D array
1 parent 56c2b9d commit 2d9f746

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

sources/noise/poisson.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ struct PoissonSampler
6666
let normalized_width:Double = Double(width ) / radius,
6767
normalized_height:Double = Double(height) / radius,
6868
grid_width:Int = Int((2.squareRoot() * normalized_width ).rounded(.up)),
69-
grid_height:Int = Int((2.squareRoot() * normalized_height).rounded(.up))
70-
var grid = [[Math.DoubleV2?]](repeating: [Math.DoubleV2?](repeating: nil, count: grid_width + 4), count: grid_height + 4)
69+
grid_height:Int = Int((2.squareRoot() * normalized_height).rounded(.up)),
70+
grid_stride:Int = grid_width + 4
71+
var grid = [Math.DoubleV2?](repeating: nil, count: grid_stride * (grid_height + 4))
7172

7273
var queue:[Math.DoubleV2]
7374
if let seed:Math.DoubleV2 = seed
@@ -93,7 +94,7 @@ struct PoissonSampler
9394
continue
9495
}
9596

96-
if PoissonSampler.attempt_insert(candidate: candidate, into_grid: &grid)
97+
if PoissonSampler.attempt_insert(candidate: candidate, into_grid: &grid, grid_stride: grid_stride)
9798
{
9899
points.append((candidate.x * radius, candidate.y * radius))
99100
queue.append(candidate)
@@ -108,22 +109,28 @@ struct PoissonSampler
108109
}
109110

110111
private static
111-
func attempt_insert(candidate:Math.DoubleV2, into_grid grid:inout [[Math.DoubleV2?]]) -> Bool
112+
func attempt_insert(candidate:Math.DoubleV2, into_grid grid:inout [Math.DoubleV2?], grid_stride:Int) -> Bool
112113
{
113-
let i:Int = Int(candidate.y * 2.squareRoot()) + 2,
114-
j:Int = Int(candidate.x * 2.squareRoot()) + 2
114+
let i:Int = Int(candidate.y * 2.squareRoot()) + 2,
115+
j:Int = Int(candidate.x * 2.squareRoot()) + 2,
116+
center:Int = i * grid_stride + j
115117

116-
guard grid[i][j] == nil
118+
guard grid[center] == nil
117119
else
118120
{
119121
return false
120122
}
121123

122-
let ring:[Math.DoubleV2?] = [ grid[i - 2][j - 1], grid[i - 2][j], grid[i - 2][j + 1],
123-
grid[i - 1][j - 2], grid[i - 1][j - 1], grid[i - 1][j], grid[i - 1][j + 1], grid[i - 1][j + 2],
124-
grid[i ][j - 2], grid[i ][j - 1], grid[i ][j + 1], grid[i ][j + 2],
125-
grid[i + 1][j - 2], grid[i + 1][j - 1], grid[i + 1][j], grid[i + 1][j + 1], grid[i + 1][j + 2],
126-
grid[i + 2][j - 1], grid[i + 2][j], grid[i + 2][j + 1]]
124+
let base:(Int, Int, Int, Int) = (center - 2*grid_stride,
125+
center - grid_stride,
126+
center + grid_stride,
127+
center + 2*grid_stride)
128+
129+
let ring:[Math.DoubleV2?] = [ grid[base.0 - 1], grid[base.0], grid[base.0 + 1],
130+
grid[base.1 - 2], grid[base.1 - 1], grid[base.1], grid[base.1 + 1], grid[base.1 + 2],
131+
grid[center - 2], grid[center - 1], grid[center + 1], grid[center + 2],
132+
grid[base.2 - 2], grid[base.2 - 1], grid[base.2], grid[base.2 + 1], grid[base.2 + 2],
133+
grid[base.3 - 1], grid[base.3], grid[base.3 + 1]]
127134
for cell:Math.DoubleV2? in ring
128135
{
129136
guard let occupant:Math.DoubleV2 = cell
@@ -141,7 +148,7 @@ struct PoissonSampler
141148
}
142149
}
143150

144-
grid[i][j] = candidate
151+
grid[center] = candidate
145152
return true
146153
}
147154
}

0 commit comments

Comments
 (0)