@@ -31,7 +31,6 @@ extension _CellNoise2D
31
31
32
32
// ugly hack to get around compiler linker bug
33
33
@inline ( __always)
34
- fileprivate
35
34
func _closest_point( _ x: Double , _ y: Double ) -> ( point: ( Int , Int ) , r2: Double )
36
35
{
37
36
let sample : Math . DoubleV2 = ( x * self . frequency, y * self . frequency)
@@ -72,7 +71,7 @@ extension _CellNoise2D
72
71
// found.
73
72
74
73
let quadrant : Math . IntV2 = ( sample_rel. x > 0.5 ? 1 : - 1 , sample_rel. y > 0.5 ? 1 : - 1 ) ,
75
- near : Math . IntV2 = ( bin. a + ( quadrant. a + 1 ) >> 1 , bin . b + ( quadrant. b + 1 ) >> 1 ) ,
74
+ near : Math . IntV2 = Math . add ( bin, ( ( quadrant. a + 1 ) >> 1 , ( quadrant. b + 1 ) >> 1 ) ) ,
76
75
far : Math . IntV2 = ( near. a - quadrant. a , near. b - quadrant. b)
77
76
78
77
let nearpoint_disp : Math . DoubleV2 = ( abs ( sample_rel. x - Double( ( quadrant. a + 1 ) >> 1 ) ) ,
@@ -189,12 +188,6 @@ struct CellNoise2D:_CellNoise2D, HashedNoise
189
188
self . permutation_table = PermutationTable ( seed: seed)
190
189
}
191
190
192
- fileprivate
193
- func hash( point: Math . IntV2 ) -> Int
194
- {
195
- return self . permutation_table. hash ( point)
196
- }
197
-
198
191
public
199
192
func closest_point( _ x: Double , _ y: Double ) -> ( point: ( Int , Int ) , r2: Double )
200
193
{
@@ -221,7 +214,6 @@ struct CellNoise2D:_CellNoise2D, HashedNoise
221
214
}
222
215
}
223
216
224
- /*
225
217
public
226
218
struct TilingCellNoise2D : _CellNoise2D , HashedTilingNoise
227
219
{
@@ -256,12 +248,6 @@ struct TilingCellNoise2D:_CellNoise2D, HashedTilingNoise
256
248
self . wavelengths = ( wavelengths_x, wavelengths_y)
257
249
}
258
250
259
- fileprivate
260
- func hash(point:Math.IntV2) -> Int
261
- {
262
- return self.permutation_table.hash(Math.mod(point, self.wavelengths))
263
- }
264
-
265
251
public
266
252
func closest_point( _ x: Double , _ y: Double ) -> ( point: ( Int , Int ) , r2: Double )
267
253
{
@@ -287,34 +273,24 @@ struct TilingCellNoise2D:_CellNoise2D, HashedTilingNoise
287
273
return self . evaluate ( x, y)
288
274
}
289
275
}
290
- */
291
276
292
- public
293
- struct CellNoise3D : HashedNoise
294
- {
295
- let permutation_table : PermutationTable ,
296
- amplitude : Double ,
297
- frequency : Double
298
277
299
- init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable )
300
- {
301
- self . amplitude = amplitude
302
- self . frequency = frequency
303
- self . permutation_table = permutation_table
304
- }
278
+ fileprivate
279
+ protocol _CellNoise3D
280
+ {
281
+ var frequency : Double { get }
282
+ var amplitude : Double { get }
305
283
306
- public
307
- init ( amplitude: Double , frequency: Double , seed: Int = 0 )
308
- {
309
- self . amplitude = amplitude * 1 / 3
310
- self . frequency = frequency
311
- self . permutation_table = PermutationTable ( seed: seed)
312
- }
284
+ func hash( point: Math . IntV3 ) -> Int
285
+ }
313
286
287
+ extension _CellNoise3D
288
+ {
289
+ @inline ( __always)
314
290
private
315
291
func distance2( from sample_point: Math . DoubleV3 , generating_point: Math . IntV3 ) -> Double
316
292
{
317
- let hash : Int = self . permutation_table . hash ( generating_point)
293
+ let hash : Int = self . hash ( point : generating_point)
318
294
// hash is within 0 ... 255, take it to 0 ... 0.5
319
295
320
296
// Notice that we have 256 possible hashes, and therefore 8 bits of entropy,
@@ -333,8 +309,8 @@ struct CellNoise3D:HashedNoise
333
309
return Math . dot ( dv, dv)
334
310
}
335
311
336
- public
337
- func closest_point ( _ x: Double , _ y: Double , _ z: Double ) -> ( point: ( Int , Int , Int ) , r2: Double )
312
+ @ inline ( __always )
313
+ func _closest_point ( _ x: Double , _ y: Double , _ z: Double ) -> ( point: ( Int , Int , Int ) , r2: Double )
338
314
{
339
315
let sample : Math . DoubleV3 = ( x * self . frequency, y * self . frequency, z * self . frequency)
340
316
@@ -354,9 +330,9 @@ struct CellNoise3D:HashedNoise
354
330
let quadrant : Math . IntV3 = ( sample_rel. x > 0.5 ? 1 : - 1 ,
355
331
sample_rel. y > 0.5 ? 1 : - 1 ,
356
332
sample_rel. z > 0.5 ? 1 : - 1 )
357
- let near : Math . IntV3 = ( bin. a + ( quadrant. a + 1 ) >> 1 ,
358
- bin . b + ( quadrant. b + 1 ) >> 1 ,
359
- bin . c + ( quadrant. c + 1 ) >> 1 )
333
+ let near : Math . IntV3 = Math . add ( bin, ( ( quadrant. a + 1 ) >> 1 ,
334
+ ( quadrant. b + 1 ) >> 1 ,
335
+ ( quadrant. c + 1 ) >> 1 ) )
360
336
361
337
let nearpoint_disp : Math . DoubleV3 = ( abs ( sample_rel. x - Double( ( quadrant. a + 1 ) >> 1 ) ) ,
362
338
abs ( sample_rel. y - Double( ( quadrant. b + 1 ) >> 1 ) ) ,
@@ -539,6 +515,96 @@ struct CellNoise3D:HashedNoise
539
515
// stop outside r^2 = 3.0
540
516
return ( closest_point, r2)
541
517
}
518
+ }
519
+
520
+ public
521
+ struct CellNoise3D : _CellNoise3D , HashedNoise
522
+ {
523
+ let permutation_table : PermutationTable ,
524
+ amplitude : Double ,
525
+ frequency : Double
526
+
527
+ init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable )
528
+ {
529
+ self . amplitude = amplitude
530
+ self . frequency = frequency
531
+ self . permutation_table = permutation_table
532
+ }
533
+
534
+ public
535
+ init ( amplitude: Double , frequency: Double , seed: Int = 0 )
536
+ {
537
+ self . amplitude = amplitude * 1 / 3
538
+ self . frequency = frequency
539
+ self . permutation_table = PermutationTable ( seed: seed)
540
+ }
541
+
542
+ public
543
+ func closest_point( _ x: Double , _ y: Double , _ z: Double ) -> ( point: ( Int , Int , Int ) , r2: Double )
544
+ {
545
+ return self . _closest_point ( x, y, z)
546
+ }
547
+
548
+ public
549
+ func evaluate( _ x: Double , _ y: Double ) -> Double
550
+ {
551
+ return self . evaluate ( x, y, 0 )
552
+ }
553
+
554
+ public
555
+ func evaluate( _ x: Double , _ y: Double , _ z: Double ) -> Double
556
+ {
557
+ let ( _, r2) : ( ( Int , Int , Int ) , Double ) = self . closest_point ( x, y, z)
558
+ return self . amplitude * r2
559
+ }
560
+
561
+ public
562
+ func evaluate( _ x: Double , _ y: Double , _ z: Double , _: Double ) -> Double
563
+ {
564
+ return self . evaluate ( x, y, z)
565
+ }
566
+ }
567
+
568
+ public
569
+ struct TilingCellNoise3D : _CellNoise3D , HashedTilingNoise
570
+ {
571
+ let permutation_table : PermutationTable ,
572
+ amplitude : Double ,
573
+ frequency : Double ,
574
+ wavelengths : Math . IntV3
575
+
576
+ init ( amplitude: Double , frequency: Double , permutation_table: PermutationTable , wavelengths: Math . IntV3 )
577
+ {
578
+ self . amplitude = amplitude
579
+ self . frequency = frequency
580
+ self . permutation_table = permutation_table
581
+ self . wavelengths = wavelengths
582
+ }
583
+
584
+ public
585
+ init ( amplitude: Double , frequency: Double , wavelengths: Int , seed: Int = 0 )
586
+ {
587
+ self . init ( amplitude: amplitude, frequency: frequency,
588
+ wavelengths_x: wavelengths,
589
+ wavelengths_y: wavelengths,
590
+ wavelengths_z: wavelengths,
591
+ seed: seed)
592
+ }
593
+
594
+ public
595
+ init ( amplitude: Double , frequency: Double , wavelengths_x: Int , wavelengths_y: Int , wavelengths_z: Int , seed: Int = 0 )
596
+ {
597
+ self . amplitude = 1 / 3 * amplitude
598
+ self . frequency = frequency
599
+ self . permutation_table = PermutationTable ( seed: seed)
600
+ self . wavelengths = ( wavelengths_x, wavelengths_y, wavelengths_z)
601
+ }
602
+
603
+ public
604
+ func closest_point( _ x: Double , _ y: Double , _ z: Double ) -> ( point: ( Int , Int , Int ) , r2: Double )
605
+ {
606
+ return self . _closest_point ( x, y, z)
607
+ }
542
608
543
609
public
544
610
func evaluate( _ x: Double , _ y: Double ) -> Double
0 commit comments