Skip to content

Commit 1c496f9

Browse files
committed
rewrite DomainND code with vector math
1 parent 04dc9f6 commit 1c496f9

File tree

2 files changed

+82
-79
lines changed

2 files changed

+82
-79
lines changed

sources/noise/hash.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ extension HashedNoise
3030

3131
protocol HashedTilingNoise:Noise
3232
{
33+
associatedtype IntV
34+
3335
var permutation_table:PermutationTable { get }
3436
var amplitude:Double { get }
3537
var frequency:Double { get }
36-
var wavelengths:Math.IntV3 { get }
38+
var wavelengths:IntV { get }
3739

38-
init(amplitude:Double, frequency:Double, permutation_table:PermutationTable, wavelengths:Math.IntV3)
40+
init(amplitude:Double, frequency:Double, permutation_table:PermutationTable, wavelengths:IntV)
3941
}
4042

4143
extension HashedTilingNoise
@@ -59,7 +61,10 @@ extension HashedTilingNoise
5961
return Self(amplitude: self.amplitude, frequency: self.frequency,
6062
permutation_table: new_table, wavelengths: self.wavelengths)
6163
}
62-
64+
}
65+
/*
66+
extension HashedTilingNoise where IntV == Math.IntV3
67+
{
6368
public
6469
func transposed(octaves:Int) -> Self
6570
{
@@ -69,6 +74,7 @@ extension HashedTilingNoise
6974
wavelengths: (self.wavelengths.a << octaves, self.wavelengths.b << octaves, self.wavelengths.c << octaves))
7075
}
7176
}
77+
*/
7278

7379
public
7480
struct RandomXorshift

sources/noise/noise.swift

Lines changed: 73 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,34 @@ enum Math
170170
return (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
171171
}
172172

173+
@inline(__always)
174+
static
175+
func mult(_ v1:DoubleV2, _ v2:DoubleV2) -> DoubleV2
176+
{
177+
return (v1.x * v2.x, v1.y * v2.y)
178+
}
179+
180+
@inline(__always)
181+
static
182+
func mult(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
183+
{
184+
return (v1.x * v2.x, v1.y * v2.y, v1.z * v2.z)
185+
}
186+
187+
@inline(__always)
188+
static
189+
func div(_ v1:DoubleV2, _ v2:DoubleV2) -> DoubleV2
190+
{
191+
return (v1.x / v2.x, v1.y / v2.y)
192+
}
193+
194+
@inline(__always)
195+
static
196+
func div(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
197+
{
198+
return (v1.x / v2.x, v1.y / v2.y, v1.z / v2.z)
199+
}
200+
173201
@inline(__always)
174202
private static
175203
func mod(_ x:Int, _ n:Int) -> Int
@@ -255,72 +283,62 @@ public
255283
struct Domain2D:Sequence
256284
{
257285
private
258-
let i_max:Double,
259-
j_max:Double,
260-
261-
dx:Double,
262-
dy:Double,
263-
i0:Double,
264-
j0:Double
286+
let sample_lower_bound:Math.DoubleV2,
287+
sample_upper_bound:Math.DoubleV2,
288+
increment:Math.DoubleV2
265289

266290
public
267291
struct Iterator:IteratorProtocol
268292
{
269293
private
270-
var i:Double = -0.5,
271-
j:Double = 0.5
294+
var sample:Math.DoubleV2
272295

273296
private
274297
let domain:Domain2D
275298

276299
init(_ domain:Domain2D)
277300
{
278-
self.i = domain.i0 - 0.5
279-
self.j = domain.j0 + 0.5
280-
301+
self.sample = Math.add(domain.sample_lower_bound, (-0.5, 0.5))
281302
self.domain = domain
282303
}
283304

284305
public mutating
285306
func next() -> (Double, Double)?
286307
{
287-
self.i += 1
288-
if self.i >= self.domain.i_max
308+
self.sample.x += 1
309+
if self.sample.x >= self.domain.sample_upper_bound.x
289310
{
290-
self.i = self.domain.i0 + 0.5
291-
self.j += 1
292-
if self.j >= self.domain.j_max
311+
self.sample.x = self.domain.sample_lower_bound.x + 0.5
312+
self.sample.y += 1
313+
if self.sample.y >= self.domain.sample_upper_bound.y
293314
{
294315
return nil
295316
}
296317
}
297318

298-
return (self.domain.dx * self.i, self.domain.dy * self.j)
319+
return Math.mult(self.domain.increment, self.sample)
299320
}
300321
}
301322

302323
public
303324
init(samples_x:Int, samples_y:Int)
304325
{
305-
self.dx = 1
306-
self.dy = 1
307-
self.i0 = 0
308-
self.j0 = 0
309-
310-
self.i_max = Double(samples_x)
311-
self.j_max = Double(samples_y)
326+
self.increment = (1, 1)
327+
self.sample_lower_bound = (0, 0)
328+
self.sample_upper_bound = Math.cast_double((samples_x, samples_y))
312329
}
313330

314331
public
315332
init(_ x_range:ClosedRange<Double>, _ y_range:ClosedRange<Double>, samples_x:Int, samples_y:Int)
316333
{
317-
self.dx = (x_range.upperBound - x_range.lowerBound) / Double(samples_x)
318-
self.dy = (y_range.upperBound - y_range.lowerBound) / Double(samples_y)
319-
self.i0 = x_range.lowerBound * Double(samples_x) / (x_range.upperBound - x_range.lowerBound)
320-
self.j0 = y_range.lowerBound * Double(samples_y) / (y_range.upperBound - y_range.lowerBound)
334+
let sample_count:Math.DoubleV2 = Math.cast_double((samples_x, samples_y)),
335+
range_lower_bound:Math.DoubleV2 = (x_range.lowerBound, y_range.lowerBound),
336+
range_upper_bound:Math.DoubleV2 = (x_range.upperBound, y_range.upperBound),
337+
range_difference:Math.DoubleV2 = Math.sub(range_upper_bound, range_lower_bound)
321338

322-
self.i_max = self.i0 + Double(samples_x)
323-
self.j_max = self.j0 + Double(samples_y)
339+
self.increment = Math.div(range_difference, sample_count)
340+
self.sample_lower_bound = Math.div(Math.mult(range_lower_bound, sample_count), range_difference)
341+
self.sample_upper_bound = Math.add(self.sample_lower_bound, sample_count)
324342
}
325343

326344
public
@@ -335,89 +353,68 @@ public
335353
struct Domain3D:Sequence
336354
{
337355
private
338-
let i_max:Double,
339-
j_max:Double,
340-
k_max:Double,
341-
342-
dx:Double,
343-
dy:Double,
344-
dz:Double,
345-
i0:Double,
346-
j0:Double,
347-
k0:Double
356+
let sample_lower_bound:Math.DoubleV3,
357+
sample_upper_bound:Math.DoubleV3,
358+
increment:Math.DoubleV3
348359

349360
public
350361
struct Iterator:IteratorProtocol
351362
{
352363
private
353-
var i:Double = -0.5,
354-
j:Double = 0.5,
355-
k:Double = 0.5
364+
var sample:Math.DoubleV3
356365

357366
private
358367
let domain:Domain3D
359368

360369
init(_ domain:Domain3D)
361370
{
362-
self.i = domain.i0 - 0.5
363-
self.j = domain.j0 + 0.5
364-
self.k = domain.k0 + 0.5
365-
371+
self.sample = Math.add(domain.sample_lower_bound, (-0.5, 0.5, 0.5))
366372
self.domain = domain
367373
}
368374

369375
public mutating
370376
func next() -> (Double, Double, Double)?
371377
{
372-
self.i += 1
373-
if self.i >= self.domain.i_max
378+
self.sample.x += 1
379+
if self.sample.x >= self.domain.sample_upper_bound.x
374380
{
375-
self.i = self.domain.i0 + 0.5
376-
self.j += 1
377-
if self.j >= self.domain.j_max
381+
self.sample.x = self.domain.sample_lower_bound.x + 0.5
382+
self.sample.y += 1
383+
if self.sample.y >= self.domain.sample_upper_bound.y
378384
{
379-
self.j = self.domain.j0 + 0.5
380-
self.k += 1
381-
if self.k >= self.domain.k_max
385+
self.sample.y = self.domain.sample_lower_bound.y + 0.5
386+
self.sample.z += 1
387+
if self.sample.z >= self.domain.sample_upper_bound.z
382388
{
383389
return nil
384390
}
385391
}
386392
}
387393

388-
return (self.domain.dx * self.i, self.domain.dy * self.j, self.domain.dz * self.k)
394+
return Math.mult(self.domain.increment, self.sample)
389395
}
390396
}
391397

392398
public
393399
init(samples_x:Int, samples_y:Int, samples_z:Int)
394400
{
395-
self.dx = 1
396-
self.dy = 1
397-
self.dz = 1
398-
self.i0 = 0
399-
self.j0 = 0
400-
self.k0 = 0
401-
402-
self.i_max = Double(samples_x)
403-
self.j_max = Double(samples_y)
404-
self.k_max = Double(samples_z)
401+
self.increment = (1, 1, 1)
402+
self.sample_lower_bound = (0, 0, 0)
403+
self.sample_upper_bound = Math.cast_double((samples_x, samples_y, samples_z))
405404
}
406405

407406
public
408407
init(_ x_range:ClosedRange<Double>, _ y_range:ClosedRange<Double>, _ z_range:ClosedRange<Double>,
409408
samples_x:Int, samples_y:Int, samples_z:Int)
410409
{
411-
self.dx = (x_range.upperBound - x_range.lowerBound) / Double(samples_x)
412-
self.dy = (y_range.upperBound - y_range.lowerBound) / Double(samples_y)
413-
self.dz = (z_range.upperBound - z_range.lowerBound) / Double(samples_z)
414-
self.i0 = x_range.lowerBound * Double(samples_x) / (x_range.upperBound - x_range.lowerBound)
415-
self.j0 = y_range.lowerBound * Double(samples_y) / (y_range.upperBound - y_range.lowerBound)
416-
self.k0 = z_range.lowerBound * Double(samples_z) / (z_range.upperBound - z_range.lowerBound)
410+
let sample_count:Math.DoubleV3 = Math.cast_double((samples_x, samples_y, samples_z)),
411+
range_lower_bound:Math.DoubleV3 = (x_range.lowerBound, y_range.lowerBound, z_range.lowerBound),
412+
range_upper_bound:Math.DoubleV3 = (x_range.upperBound, y_range.upperBound, z_range.upperBound),
413+
range_difference:Math.DoubleV3 = Math.sub(range_upper_bound, range_lower_bound)
417414

418-
self.i_max = self.i0 + Double(samples_x)
419-
self.j_max = self.j0 + Double(samples_y)
420-
self.k_max = self.k0 + Double(samples_z)
415+
self.increment = Math.div(range_difference, sample_count)
416+
self.sample_lower_bound = Math.div(Math.mult(range_lower_bound, sample_count), range_difference)
417+
self.sample_upper_bound = Math.add(self.sample_lower_bound, sample_count)
421418
}
422419

423420
public

0 commit comments

Comments
 (0)