Skip to content

Commit 3829b08

Browse files
committed
fix disk sampler bug
1 parent 3f6337c commit 3829b08

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

poisson.png

-30 Bytes
Loading

sources/noise/poisson.swift renamed to sources/noise/disk.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ struct DiskSampler2D
6666
queue = [(0.5 * normalized_width, 0.5 * normalized_height)]
6767
}
6868

69-
var points:[(Double, Double)] = queue
69+
_ = DiskSampler2D.attempt_insert(candidate: queue[0], into_grid: &grid, grid_stride: grid_stride)
70+
var points:[(Double, Double)] = [(queue[0].x * radius, queue[0].y * radius)]
7071
outer: while let front:Math.DoubleV2 = queue.last
7172
{
7273
for _ in 0 ..< k

tests/LinuxMain.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import NoiseTests
1010

1111
banners(width: 700, ratio: 5)
1212

13-
13+
/*
1414
var pixbuf:[UInt8]
1515
let png_properties:PNGProperties = PNGProperties(width: viewer_size, height: viewer_size, bit_depth: 8, color: .grayscale, interlaced: false)!
1616

@@ -112,3 +112,4 @@ t0 = clock()
112112
pixbuf = SS3D.sample_area_saturated_to_u8(width: viewer_size, height: viewer_size, offset: 127.5)
113113
print(clock() - t0)
114114
try png_encode(path: "super_simplex3D.png", raw_data: pixbuf, properties: png_properties)
115+
*/

tests/banner_disk2d.png

18.5 KB
Loading

tests/noise/tests.swift

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import Noise
22

33
import MaxPNG
44

5+
func write_rgb_png(path:String, width:Int, height:Int, pixbuf:[UInt8])
6+
{
7+
guard let properties = PNGProperties(width: width, height: height, bit_depth: 8, color: .rgb, interlaced: false)
8+
else
9+
{
10+
fatalError("failed to set png properties")
11+
}
12+
13+
do
14+
{
15+
try png_encode(path: path, raw_data: pixbuf, properties: properties)
16+
}
17+
catch
18+
{
19+
print(error)
20+
}
21+
}
22+
523
func color_noise_png(r_noise:Noise, g_noise:Noise, b_noise:Noise,
624
width:Int, height:Int, value_offset:Double, invert:Bool = false, path:String)
725
{
@@ -25,20 +43,7 @@ func color_noise_png(r_noise:Noise, g_noise:Noise, b_noise:Noise,
2543
}
2644
}
2745

28-
guard let properties = PNGProperties(width: width, height: height, bit_depth: 8, color: .rgb, interlaced: false)
29-
else
30-
{
31-
fatalError("failed to set png properties")
32-
}
33-
34-
do
35-
{
36-
try png_encode(path: path, raw_data: pixbuf, properties: properties)
37-
}
38-
catch
39-
{
40-
print(error)
41-
}
46+
write_rgb_png(path: path, width: width, height: height, pixbuf: pixbuf)
4247
}
4348

4449
func banner_simplex2d(width:Int, height:Int, seed:Int)
@@ -98,6 +103,47 @@ func banner_cell3d(width:Int, height:Int, seed:Int)
98103
path: "tests/banner_cell3d.png")
99104
}
100105

106+
func circle_at(cx:Double, cy:Double, r:Double, width:Int, height:Int, _ f:(Int, Int, Double) -> ())
107+
{
108+
// get bounding box
109+
let x1:Int = max(0 , Int(cx - r)),
110+
x2:Int = min(width - 1 , Int((cx + r).rounded(.up))),
111+
y1:Int = max(0 , Int(cy - r)),
112+
y2:Int = min(height - 1, Int((cy + r).rounded(.up)))
113+
114+
for y in y1 ... y2
115+
{
116+
let dy:Double = Double(y) - cy
117+
for x in x1 ... x2
118+
{
119+
let dx:Double = Double(x) - cx,
120+
dr:Double = (dx*dx + dy*dy).squareRoot()
121+
122+
f(x, y, min(1, max(0, 1 - dr + r - 0.5)))
123+
}
124+
}
125+
}
126+
127+
func banner_disk2d(width:Int, height:Int, seed:Int)
128+
{
129+
var poisson = DiskSampler2D(seed: seed)
130+
var pixbuf:[UInt8] = [UInt8](repeating: 255, count: 3 * width * height)
131+
132+
let points = poisson.generate(radius: 20, width: width, height: height, k: 80)
133+
for point:(x:Double, y:Double) in points
134+
{
135+
circle_at(cx: point.x, cy: point.y, r: 5, width: width, height: height,
136+
{
137+
(x:Int, y:Int, v:Double) in
138+
139+
let base_addr:Int = 3 * (y * width + x)
140+
pixbuf[base_addr + 1] = pixbuf[base_addr + 1] &- UInt8(255 * v)
141+
})
142+
}
143+
144+
write_rgb_png(path: "tests/banner_disk2d.png", width: width, height: height, pixbuf: pixbuf)
145+
}
146+
101147
public
102148
func banners(width:Int, ratio:Double)
103149
{
@@ -107,4 +153,6 @@ func banners(width:Int, ratio:Double)
107153
banner_supersimplex3d(width: width, height: height, seed: 0)
108154
banner_cell2d(width: width, height: height, seed: 0)
109155
banner_cell3d(width: width, height: height, seed: 0)
156+
157+
banner_disk2d(width: width, height: height, seed: 0)
110158
}

0 commit comments

Comments
 (0)