Skip to content

Commit af861d7

Browse files
committed
get cell noise ready for tilable implentation, still held up by compiler bug
1 parent 299bd8b commit af861d7

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

sources/noise/cell.swift

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
fileprivate
2+
protocol _CellNoise2D
3+
{
4+
var frequency:Double { get }
5+
var amplitude:Double { get }
6+
func distance2(from sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
7+
}
8+
9+
extension _CellNoise2D
10+
{
11+
@inline(__always)
12+
func _distance2(hash:Int, sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
13+
{
14+
// hash is within 0 ... 255, take it to 0 ... 0.5
15+
16+
// Notice that we have 256 possible hashes, and therefore 8 bits of entropy,
17+
// to be divided up between 2 axes. We can assign 4 bits to the x and y
18+
// axes each (16 levels each)
19+
20+
// 0b XXXX YYYY
21+
22+
let dp:Math.DoubleV2 = ((Double(hash >> 4 ) - 15/2) * 1/16,
23+
(Double(hash & 0b1111) - 15/2) * 1/16)
24+
25+
let dv:Math.DoubleV2 = Math.sub(Math.add(Math.cast_double(generating_point), dp), sample_point)
26+
return Math.dot(dv, dv)
27+
}
28+
}
29+
130
public
2-
struct CellNoise2D:HashedNoise
31+
struct CellNoise2D:_CellNoise2D, HashedNoise
332
{
433
let permutation_table:PermutationTable,
534
amplitude:Double,
@@ -20,25 +49,13 @@ struct CellNoise2D:HashedNoise
2049
self.permutation_table = PermutationTable(seed: seed)
2150
}
2251

23-
private
52+
fileprivate
2453
func distance2(from sample_point:Math.DoubleV2, generating_point:Math.IntV2) -> Double
2554
{
26-
let hash:Int = self.permutation_table.hash(generating_point)
27-
// hash is within 0 ... 255, take it to 0 ... 0.5
28-
29-
// Notice that we have 256 possible hashes, and therefore 8 bits of entropy,
30-
// to be divided up between 2 axes. We can assign 4 bits to the x and y
31-
// axes each (16 levels each)
32-
33-
// 0b XXXX YYYY
34-
35-
let dp:Math.DoubleV2 = ((Double(hash >> 4 ) - 15/2) * 1/16,
36-
(Double(hash & 0b1111) - 15/2) * 1/16)
37-
38-
let dv:Math.DoubleV2 = Math.sub(Math.add(Math.cast_double(generating_point), dp), sample_point)
39-
return Math.dot(dv, dv)
55+
return self._distance2(hash: self.permutation_table.hash(generating_point), sample_point: sample_point, generating_point: generating_point)
4056
}
4157

58+
// still here to make tests run, because of a linker bug in the Swift compiler
4259
public
4360
func closest_point(_ x:Double, _ y:Double) -> (point:(Int, Int), r2:Double)
4461
{

sources/noise/gradient.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ protocol _ClassicNoise3D
1717
extension _ClassicNoise3D
1818
{
1919
@inline(__always)
20-
fileprivate
2120
func _gradient(hash:Int, offset:Math.DoubleV3) -> Double
2221
{
2322
// use vectors to the edge of a cube
@@ -73,7 +72,7 @@ extension _ClassicNoise3D
7372

7473
// UNDOCUMENTED
7574
public
76-
struct ClassicNoise3D:HashedNoise, _ClassicNoise3D
75+
struct ClassicNoise3D:_ClassicNoise3D, HashedNoise
7776
{
7877
let permutation_table:PermutationTable,
7978
amplitude:Double,
@@ -103,7 +102,7 @@ struct ClassicNoise3D:HashedNoise, _ClassicNoise3D
103102

104103
// UNDOCUMENTED
105104
public
106-
struct ClassicTilingNoise3D:HashedTilingNoise, _ClassicNoise3D
105+
struct ClassicTilingNoise3D:_ClassicNoise3D, HashedTilingNoise
107106
{
108107
let permutation_table:PermutationTable,
109108
amplitude:Double,

tests/LinuxMain.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ for (i, (x, y)) in Domain2D(samples_x: viewer_size, samples_y: viewer_size).enum
5353
print(clock() - t0)
5454
try png_encode(path: "tests/classic3d.png", raw_data: pixbuf, properties: png_properties)
5555

56+
let PT:Noise = ClassicTilingNoise3D(amplitude: 255, frequency: 16 / Double(viewer_size), wavelengths: 16)
57+
t0 = clock()
58+
for (i, (x, y)) in Domain2D(samples_x: viewer_size, samples_y: viewer_size).enumerated()
59+
{
60+
pixbuf[i] = UInt8(max(0, min(255, PT.evaluate(x, y) + 127.5)))
61+
}
62+
print(clock() - t0)
63+
try png_encode(path: "tests/classic_tiling3d.png", raw_data: pixbuf, properties: png_properties)
5664

5765
let SS:FBM<GradientNoise2D> = FBM<GradientNoise2D>(GradientNoise2D(amplitude: 180, frequency: 0.001), octaves: 10, persistence: 0.62)
5866
t0 = clock()

tests/classic_tiling3d.png

192 KB
Loading

tests/noise/tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func banner_disk2d(width:Int, height:Int, seed:Int)
205205

206206
func banner_voronoi2d(width:Int, height:Int, seed:Int)
207207
{
208-
let voronoi = CellNoise2D(amplitude: 255, frequency: 0.025, seed: seed)
208+
let voronoi:CellNoise2D = CellNoise2D(amplitude: 255, frequency: 0.025, seed: seed)
209209
var pixbytes:[UInt8] = [UInt8](repeating: 0, count: 3 * width * height)
210210

211211
let r:PermutationTable = PermutationTable(seed: seed),

0 commit comments

Comments
 (0)