dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
// Generative Art Spirit - Noise Module
// Procedural noise functions for natural textures and terrain

module generative.noise @ 0.1.0

use @univrs/visual.geometry.{ Point2D, Point3D }

// ============================================================================
// CONSTANTS
// ============================================================================

pub const PI: f64 = 3.14159265358979323846
pub const TAU: f64 = 6.28318530717958647692
pub const SQRT_3: f64 = 1.7320508075688772935

// Permutation table size (must be power of 2)
pub const PERM_SIZE: u32 = 256
pub const PERM_MASK: u32 = 255

// Simplex skew factors
pub const F2: f64 = 0.3660254037844386   // (sqrt(3) - 1) / 2
pub const G2: f64 = 0.21132486540518713  // (3 - sqrt(3)) / 6
pub const F3: f64 = 0.3333333333333333   // 1/3
pub const G3: f64 = 0.1666666666666667   // 1/6

// Default noise parameters
pub const DEFAULT_OCTAVES: u32 = 4
pub const DEFAULT_LACUNARITY: f64 = 2.0
pub const DEFAULT_PERSISTENCE: f64 = 0.5
pub const DEFAULT_FREQUENCY: f64 = 1.0

// ============================================================================
// NOISE CONFIGURATION
// ============================================================================

pub gen NoiseConfig {
    has seed: u64                   // Random seed
    has octaves: u32                // Number of noise layers
    has lacunarity: f64             // Frequency multiplier per octave
    has persistence: f64            // Amplitude multiplier per octave
    has frequency: f64              // Base frequency

    rule valid_octaves {
        this.octaves >= 1 && this.octaves <= 16
    }

    rule valid_lacunarity {
        this.lacunarity > 0.0
    }

    rule valid_persistence {
        this.persistence > 0.0 && this.persistence <= 1.0
    }

    fun with_octaves(octaves: u32) -> NoiseConfig {
        return NoiseConfig {
            seed: this.seed,
            octaves: octaves,
            lacunarity: this.lacunarity,
            persistence: this.persistence,
            frequency: this.frequency
        }
    }

    fun with_seed(seed: u64) -> NoiseConfig {
        return NoiseConfig {
            seed: seed,
            octaves: this.octaves,
            lacunarity: this.lacunarity,
            persistence: this.persistence,
            frequency: this.frequency
        }
    }

    docs {
        Configuration for fractal noise generation.

        Parameters:
        - seed: Random seed for reproducibility
        - octaves: Number of noise layers (detail levels)
        - lacunarity: Frequency multiplier between octaves (typically 2.0)
        - persistence: Amplitude multiplier between octaves (typically 0.5)
        - frequency: Base sampling frequency
    }
}

pub fun default_noise_config() -> NoiseConfig {
    return NoiseConfig {
        seed: 0,
        octaves: DEFAULT_OCTAVES,
        lacunarity: DEFAULT_LACUNARITY,
        persistence: DEFAULT_PERSISTENCE,
        frequency: DEFAULT_FREQUENCY
    }
}

// ============================================================================
// NOISE FIELDS
// ============================================================================

pub gen NoiseField2D {
    has width: u32                  // Field width in samples
    has height: u32                 // Field height in samples
    has values: Vec<f64>            // Noise values (row-major)

    rule valid_dimensions {
        this.width > 0 && this.height > 0
    }

    rule valid_values_length {
        this.values.length == (this.width * this.height) as u64
    }

    fun get(x: u32, y: u32) -> f64 {
        if x >= this.width || y >= this.height {
            return 0.0
        }
        let index = y * this.width + x
        return this.values[index as u64]
    }

    fun set(x: u32, y: u32, value: f64) -> NoiseField2D {
        if x >= this.width || y >= this.height {
            return this.clone()
        }
        let index = (y * this.width + x) as u64
        let mut values = this.values.clone()
        values[index] = value
        return NoiseField2D {
            width: this.width,
            height: this.height,
            values: values
        }
    }

    fun sample_bilinear(x: f64, y: f64) -> f64 {
        let x0 = floor(x) as u32
        let y0 = floor(y) as u32
        let x1 = x0 + 1
        let y1 = y0 + 1
        let fx = x - x0 as f64
        let fy = y - y0 as f64

        let v00 = this.get(x0, y0)
        let v10 = this.get(x1, y0)
        let v01 = this.get(x0, y1)
        let v11 = this.get(x1, y1)

        let v0 = lerp(v00, v10, fx)
        let v1 = lerp(v01, v11, fx)
        return lerp(v0, v1, fy)
    }

    fun normalize() -> NoiseField2D {
        let min_val = this.values.min()
        let max_val = this.values.max()
        let range = max_val - min_val
        if range == 0.0 {
            return this.clone()
        }
        let normalized = this.values.map(|v| (v - min_val) / range).collect()
        return NoiseField2D {
            width: this.width,
            height: this.height,
            values: normalized
        }
    }

    docs {
        A 2D field of noise values.
        Values are stored in row-major order.
    }
}

pub gen NoiseField3D {
    has width: u32                  // Field width (X)
    has height: u32                 // Field height (Y)
    has depth: u32                  // Field depth (Z)
    has values: Vec<f64>            // Noise values

    rule valid_dimensions {
        this.width > 0 && this.height > 0 && this.depth > 0
    }

    fun get(x: u32, y: u32, z: u32) -> f64 {
        if x >= this.width || y >= this.height || z >= this.depth {
            return 0.0
        }
        let index = (z * this.width * this.height + y * this.width + x) as u64
        return this.values[index]
    }

    fun sample_trilinear(x: f64, y: f64, z: f64) -> f64 {
        let x0 = floor(x) as u32
        let y0 = floor(y) as u32
        let z0 = floor(z) as u32
        let fx = x - x0 as f64
        let fy = y - y0 as f64
        let fz = z - z0 as f64

        let v000 = this.get(x0, y0, z0)
        let v100 = this.get(x0 + 1, y0, z0)
        let v010 = this.get(x0, y0 + 1, z0)
        let v110 = this.get(x0 + 1, y0 + 1, z0)
        let v001 = this.get(x0, y0, z0 + 1)
        let v101 = this.get(x0 + 1, y0, z0 + 1)
        let v011 = this.get(x0, y0 + 1, z0 + 1)
        let v111 = this.get(x0 + 1, y0 + 1, z0 + 1)

        let v00 = lerp(v000, v100, fx)
        let v10 = lerp(v010, v110, fx)
        let v01 = lerp(v001, v101, fx)
        let v11 = lerp(v011, v111, fx)

        let v0 = lerp(v00, v10, fy)
        let v1 = lerp(v01, v11, fy)

        return lerp(v0, v1, fz)
    }

    docs {
        A 3D field of noise values.
        Useful for volumetric effects and animation.
    }
}

// ============================================================================
// PERMUTATION AND GRADIENT TABLES
// ============================================================================

pub gen PermutationTable {
    has perm: Vec<u8>               // Permutation array [0..255]

    fun get(index: u32) -> u8 {
        return this.perm[(index & PERM_MASK) as u64]
    }

    fun hash2(x: u32, y: u32) -> u8 {
        return this.get(x + this.get(y) as u32)
    }

    fun hash3(x: u32, y: u32, z: u32) -> u8 {
        return this.get(x + this.get(y + this.get(z) as u32) as u32)
    }

    docs {
        Permutation table for noise functions.
        Provides pseudo-random but repeatable hash values.
    }
}

pub gen GradientTable {
    has gradients_2d: Vec<(f64, f64)>     // 2D unit gradient vectors
    has gradients_3d: Vec<(f64, f64, f64)> // 3D unit gradient vectors

    fun gradient_2d(hash: u8) -> (f64, f64) {
        let index = (hash & 7) as u64  // 8 gradients
        return this.gradients_2d[index]
    }

    fun gradient_3d(hash: u8) -> (f64, f64, f64) {
        let index = (hash & 15) as u64  // 16 gradients
        return this.gradients_3d[index]
    }

    docs {
        Gradient vectors for Perlin noise.
    }
}

pub fun create_permutation_table(seed: u64) -> PermutationTable {
    // Initialize with 0..255
    let mut perm: Vec<u8> = (0..256).map(|i| i as u8).collect()

    // Fisher-Yates shuffle with seed
    let mut rng_state = seed
    for i in (1..256).rev() {
        // Simple LCG for shuffling
        rng_state = (rng_state * 6364136223846793005 + 1442695040888963407) % (1 << 63)
        let j = (rng_state % (i as u64 + 1)) as u64
        let temp = perm[i as u64]
        perm[i as u64] = perm[j]
        perm[j] = temp
    }

    return PermutationTable { perm: perm }
}

pub fun create_gradient_table() -> GradientTable {
    // Standard 2D gradients (8 directions)
    let gradients_2d = vec![
        (1.0, 0.0), (-1.0, 0.0), (0.0, 1.0), (0.0, -1.0),
        (0.7071067811865476, 0.7071067811865476),
        (-0.7071067811865476, 0.7071067811865476),
        (0.7071067811865476, -0.7071067811865476),
        (-0.7071067811865476, -0.7071067811865476)
    ]

    // Standard 3D gradients (12 edges of cube + 4 diagonals)
    let gradients_3d = vec![
        (1.0, 1.0, 0.0), (-1.0, 1.0, 0.0), (1.0, -1.0, 0.0), (-1.0, -1.0, 0.0),
        (1.0, 0.0, 1.0), (-1.0, 0.0, 1.0), (1.0, 0.0, -1.0), (-1.0, 0.0, -1.0),
        (0.0, 1.0, 1.0), (0.0, -1.0, 1.0), (0.0, 1.0, -1.0), (0.0, -1.0, -1.0),
        (1.0, 1.0, 0.0), (-1.0, 1.0, 0.0), (0.0, -1.0, 1.0), (0.0, -1.0, -1.0)
    ]

    return GradientTable {
        gradients_2d: gradients_2d,
        gradients_3d: gradients_3d
    }
}

// ============================================================================
// TRAITS
// ============================================================================

pub trait NoiseSampler {
    fun sample(x: f64, y: f64) -> f64

    docs {
        Types that can sample noise at 2D coordinates.
    }
}

pub trait Tileable {
    fun make_tileable(width: f64, height: f64) -> Self

    docs {
        Types that can be made seamlessly tileable.
    }
}

pub trait Seedable {
    fun with_seed(seed: u64) -> Self

    docs {
        Types that can be reseeded.
    }
}

// ============================================================================
// TRAIT IMPLEMENTATIONS
// ============================================================================

impl NoiseSampler for NoiseConfig {
    fun sample(x: f64, y: f64) -> f64 {
        return perlin_2d(x, y, this)
    }
}

impl Seedable for NoiseConfig {
    fun with_seed(seed: u64) -> NoiseConfig {
        return NoiseConfig {
            seed: seed,
            octaves: this.octaves,
            lacunarity: this.lacunarity,
            persistence: this.persistence,
            frequency: this.frequency
        }
    }
}

// ============================================================================
// 2D NOISE FUNCTIONS
// ============================================================================

pub fun perlin_2d(x: f64, y: f64, config: NoiseConfig) -> f64 {
    let perm = create_permutation_table(config.seed)
    let grad = create_gradient_table()

    let xs = x * config.frequency
    let ys = y * config.frequency

    // Grid cell coordinates
    let x0 = floor(xs) as i32
    let y0 = floor(ys) as i32

    // Fractional position within cell
    let xf = xs - x0 as f64
    let yf = ys - y0 as f64

    // Fade curves
    let u = fade(xf)
    let v = fade(yf)

    // Hash coordinates
    let aa = perm.hash2(x0 as u32, y0 as u32)
    let ab = perm.hash2(x0 as u32, (y0 + 1) as u32)
    let ba = perm.hash2((x0 + 1) as u32, y0 as u32)
    let bb = perm.hash2((x0 + 1) as u32, (y0 + 1) as u32)

    // Get gradients and compute dot products
    let g00 = grad.gradient_2d(aa)
    let g10 = grad.gradient_2d(ba)
    let g01 = grad.gradient_2d(ab)
    let g11 = grad.gradient_2d(bb)

    let d00 = dot2(g00, (xf, yf))
    let d10 = dot2(g10, (xf - 1.0, yf))
    let d01 = dot2(g01, (xf, yf - 1.0))
    let d11 = dot2(g11, (xf - 1.0, yf - 1.0))

    // Bilinear interpolation
    let nx0 = lerp(d00, d10, u)
    let nx1 = lerp(d01, d11, u)
    return lerp(nx0, nx1, v)

    docs {
        Classic Perlin noise in 2D.
        Returns values in range [-1, 1].

        Perlin noise is gradient noise that:
        - Is continuous and smooth
        - Has zero values at integer coordinates
        - Is band-limited (no high frequencies)
    }
}

pub fun simplex_2d(x: f64, y: f64, seed: u64) -> f64 {
    let perm = create_permutation_table(seed)

    // Skew input space to simplex space
    let s = (x + y) * F2
    let i = floor(x + s) as i32
    let j = floor(y + s) as i32

    // Unskew back to input space
    let t = (i + j) as f64 * G2
    let x0 = x - (i as f64 - t)
    let y0 = y - (j as f64 - t)

    // Determine which simplex we're in
    let (i1, j1) = if x0 > y0 { (1, 0) } else { (0, 1) }

    // Offsets for corners
    let x1 = x0 - i1 as f64 + G2
    let y1 = y0 - j1 as f64 + G2
    let x2 = x0 - 1.0 + 2.0 * G2
    let y2 = y0 - 1.0 + 2.0 * G2

    // Hash coordinates
    let h0 = perm.hash2(i as u32, j as u32)
    let h1 = perm.hash2((i + i1) as u32, (j + j1) as u32)
    let h2 = perm.hash2((i + 1) as u32, (j + 1) as u32)

    // Contribution from each corner
    let n0 = simplex_corner_2d(x0, y0, h0)
    let n1 = simplex_corner_2d(x1, y1, h1)
    let n2 = simplex_corner_2d(x2, y2, h2)

    // Scale to [-1, 1]
    return 70.0 * (n0 + n1 + n2)

    docs {
        Simplex noise in 2D.
        Returns values in range [-1, 1].

        Simplex noise advantages over Perlin:
        - Lower computational complexity O(n) vs O(2^n)
        - No directional artifacts
        - Better scaling to higher dimensions
    }
}

pub fun worley_2d(x: f64, y: f64, seed: u64) -> f64 {
    let perm = create_permutation_table(seed)

    let xi = floor(x) as i32
    let yi = floor(y) as i32

    let mut min_dist = 999999.0

    // Check 3x3 neighborhood of cells
    for di in -1..2 {
        for dj in -1..2 {
            let cx = xi + di
            let cy = yi + dj

            // Hash to get feature point position within cell
            let h = perm.hash2(cx as u32, cy as u32)
            let fx = cx as f64 + (h as f64 / 255.0)
            let fy = cy as f64 + (perm.get(h as u32) as f64 / 255.0)

            // Distance to feature point
            let dx = x - fx
            let dy = y - fy
            let dist = sqrt(dx * dx + dy * dy)

            if dist < min_dist {
                min_dist = dist
            }
        }
    }

    return min_dist

    docs {
        Worley (cellular/Voronoi) noise in 2D.
        Returns distance to nearest feature point.

        Creates cell-like patterns useful for:
        - Stone/scale textures
        - Cracked surfaces
        - Organic patterns
    }
}

pub fun value_noise_2d(x: f64, y: f64, seed: u64) -> f64 {
    let perm = create_permutation_table(seed)

    let x0 = floor(x) as i32
    let y0 = floor(y) as i32
    let x1 = x0 + 1
    let y1 = y0 + 1

    let xf = x - x0 as f64
    let yf = y - y0 as f64

    // Smooth interpolation
    let u = fade(xf)
    let v = fade(yf)

    // Random values at corners
    let v00 = perm.hash2(x0 as u32, y0 as u32) as f64 / 255.0
    let v10 = perm.hash2(x1 as u32, y0 as u32) as f64 / 255.0
    let v01 = perm.hash2(x0 as u32, y1 as u32) as f64 / 255.0
    let v11 = perm.hash2(x1 as u32, y1 as u32) as f64 / 255.0

    // Bilinear interpolation
    let n0 = lerp(v00, v10, u)
    let n1 = lerp(v01, v11, u)
    return lerp(n0, n1, v) * 2.0 - 1.0  // Scale to [-1, 1]

    docs {
        Value noise in 2D.
        Interpolates random values at integer coordinates.
        Simpler but blockier than gradient noise.
    }
}

// ============================================================================
// 3D NOISE FUNCTIONS
// ============================================================================

pub fun perlin_3d(x: f64, y: f64, z: f64, config: NoiseConfig) -> f64 {
    let perm = create_permutation_table(config.seed)
    let grad = create_gradient_table()

    let xs = x * config.frequency
    let ys = y * config.frequency
    let zs = z * config.frequency

    let x0 = floor(xs) as i32
    let y0 = floor(ys) as i32
    let z0 = floor(zs) as i32

    let xf = xs - x0 as f64
    let yf = ys - y0 as f64
    let zf = zs - z0 as f64

    let u = fade(xf)
    let v = fade(yf)
    let w = fade(zf)

    // Hash all 8 corners
    let aaa = perm.hash3(x0 as u32, y0 as u32, z0 as u32)
    let aba = perm.hash3(x0 as u32, (y0 + 1) as u32, z0 as u32)
    let aab = perm.hash3(x0 as u32, y0 as u32, (z0 + 1) as u32)
    let abb = perm.hash3(x0 as u32, (y0 + 1) as u32, (z0 + 1) as u32)
    let baa = perm.hash3((x0 + 1) as u32, y0 as u32, z0 as u32)
    let bba = perm.hash3((x0 + 1) as u32, (y0 + 1) as u32, z0 as u32)
    let bab = perm.hash3((x0 + 1) as u32, y0 as u32, (z0 + 1) as u32)
    let bbb = perm.hash3((x0 + 1) as u32, (y0 + 1) as u32, (z0 + 1) as u32)

    // Gradient dot products
    let d000 = dot3(grad.gradient_3d(aaa), (xf, yf, zf))
    let d100 = dot3(grad.gradient_3d(baa), (xf - 1.0, yf, zf))
    let d010 = dot3(grad.gradient_3d(aba), (xf, yf - 1.0, zf))
    let d110 = dot3(grad.gradient_3d(bba), (xf - 1.0, yf - 1.0, zf))
    let d001 = dot3(grad.gradient_3d(aab), (xf, yf, zf - 1.0))
    let d101 = dot3(grad.gradient_3d(bab), (xf - 1.0, yf, zf - 1.0))
    let d011 = dot3(grad.gradient_3d(abb), (xf, yf - 1.0, zf - 1.0))
    let d111 = dot3(grad.gradient_3d(bbb), (xf - 1.0, yf - 1.0, zf - 1.0))

    // Trilinear interpolation
    let nx00 = lerp(d000, d100, u)
    let nx01 = lerp(d001, d101, u)
    let nx10 = lerp(d010, d110, u)
    let nx11 = lerp(d011, d111, u)
    let nxy0 = lerp(nx00, nx10, v)
    let nxy1 = lerp(nx01, nx11, v)
    return lerp(nxy0, nxy1, w)

    docs {
        Classic Perlin noise in 3D.
        Useful for volumetric effects and animated noise.
    }
}

pub fun simplex_3d(x: f64, y: f64, z: f64, seed: u64) -> f64 {
    let perm = create_permutation_table(seed)

    // Skew to simplex space
    let s = (x + y + z) * F3
    let i = floor(x + s) as i32
    let j = floor(y + s) as i32
    let k = floor(z + s) as i32

    // Unskew
    let t = (i + j + k) as f64 * G3
    let x0 = x - (i as f64 - t)
    let y0 = y - (j as f64 - t)
    let z0 = z - (k as f64 - t)

    // Determine simplex
    let (i1, j1, k1, i2, j2, k2) = if x0 >= y0 {
        if y0 >= z0 { (1, 0, 0, 1, 1, 0) }
        else if x0 >= z0 { (1, 0, 0, 1, 0, 1) }
        else { (0, 0, 1, 1, 0, 1) }
    } else {
        if y0 < z0 { (0, 0, 1, 0, 1, 1) }
        else if x0 < z0 { (0, 1, 0, 0, 1, 1) }
        else { (0, 1, 0, 1, 1, 0) }
    }

    let x1 = x0 - i1 as f64 + G3
    let y1 = y0 - j1 as f64 + G3
    let z1 = z0 - k1 as f64 + G3
    let x2 = x0 - i2 as f64 + 2.0 * G3
    let y2 = y0 - j2 as f64 + 2.0 * G3
    let z2 = z0 - k2 as f64 + 2.0 * G3
    let x3 = x0 - 1.0 + 3.0 * G3
    let y3 = y0 - 1.0 + 3.0 * G3
    let z3 = z0 - 1.0 + 3.0 * G3

    let h0 = perm.hash3(i as u32, j as u32, k as u32)
    let h1 = perm.hash3((i + i1) as u32, (j + j1) as u32, (k + k1) as u32)
    let h2 = perm.hash3((i + i2) as u32, (j + j2) as u32, (k + k2) as u32)
    let h3 = perm.hash3((i + 1) as u32, (j + 1) as u32, (k + 1) as u32)

    let n0 = simplex_corner_3d(x0, y0, z0, h0)
    let n1 = simplex_corner_3d(x1, y1, z1, h1)
    let n2 = simplex_corner_3d(x2, y2, z2, h2)
    let n3 = simplex_corner_3d(x3, y3, z3, h3)

    return 32.0 * (n0 + n1 + n2 + n3)

    docs {
        Simplex noise in 3D.
        More efficient than 3D Perlin noise.
    }
}

pub fun worley_3d(x: f64, y: f64, z: f64, seed: u64) -> f64 {
    let perm = create_permutation_table(seed)

    let xi = floor(x) as i32
    let yi = floor(y) as i32
    let zi = floor(z) as i32

    let mut min_dist = 999999.0

    for di in -1..2 {
        for dj in -1..2 {
            for dk in -1..2 {
                let cx = xi + di
                let cy = yi + dj
                let cz = zi + dk

                let h = perm.hash3(cx as u32, cy as u32, cz as u32)
                let fx = cx as f64 + (h as f64 / 255.0)
                let fy = cy as f64 + (perm.get(h as u32) as f64 / 255.0)
                let fz = cz as f64 + (perm.get((h + 1) as u32) as f64 / 255.0)

                let dx = x - fx
                let dy = y - fy
                let dz = z - fz
                let dist = sqrt(dx * dx + dy * dy + dz * dz)

                if dist < min_dist {
                    min_dist = dist
                }
            }
        }
    }

    return min_dist

    docs {
        Worley (cellular) noise in 3D.
    }
}

// ============================================================================
// FRACTAL NOISE FUNCTIONS
// ============================================================================

pub fun fbm(x: f64, y: f64, config: NoiseConfig) -> f64 {
    let mut total = 0.0
    let mut amplitude = 1.0
    let mut frequency = config.frequency
    let mut max_value = 0.0

    for _ in 0..config.octaves {
        total = total + perlin_2d(x * frequency, y * frequency, config.with_seed(config.seed)) * amplitude
        max_value = max_value + amplitude
        amplitude = amplitude * config.persistence
        frequency = frequency * config.lacunarity
    }

    return total / max_value

    docs {
        Fractal Brownian Motion (fBm).
        Layers multiple octaves of noise with decreasing amplitude.

        Creates natural-looking noise useful for:
        - Terrain heightmaps
        - Cloud textures
        - Natural surfaces
    }
}

pub fun turbulence(x: f64, y: f64, config: NoiseConfig) -> f64 {
    let mut total = 0.0
    let mut amplitude = 1.0
    let mut frequency = config.frequency
    let mut max_value = 0.0

    for _ in 0..config.octaves {
        // Use absolute value for turbulent effect
        total = total + abs(perlin_2d(x * frequency, y * frequency, config.with_seed(config.seed))) * amplitude
        max_value = max_value + amplitude
        amplitude = amplitude * config.persistence
        frequency = frequency * config.lacunarity
    }

    return total / max_value

    docs {
        Turbulent noise using absolute values.
        Creates sharp ridge-like features useful for:
        - Fire and smoke effects
        - Marble textures
        - Water caustics
    }
}

pub fun ridged_multifractal(x: f64, y: f64, config: NoiseConfig) -> f64 {
    let mut total = 0.0
    let mut amplitude = 1.0
    let mut frequency = config.frequency
    let mut weight = 1.0

    for i in 0..config.octaves {
        let signal = perlin_2d(x * frequency, y * frequency, config.with_seed(config.seed + i as u64))
        let ridge = 1.0 - abs(signal)
        let ridge_sq = ridge * ridge

        total = total + ridge_sq * amplitude * weight
        weight = clamp(ridge_sq * 2.0, 0.0, 1.0)
        amplitude = amplitude * config.persistence
        frequency = frequency * config.lacunarity
    }

    return total

    docs {
        Ridged multifractal noise.
        Creates sharp ridges useful for mountain terrain.
    }
}

pub fun billow(x: f64, y: f64, config: NoiseConfig) -> f64 {
    let mut total = 0.0
    let mut amplitude = 1.0
    let mut frequency = config.frequency
    let mut max_value = 0.0

    for _ in 0..config.octaves {
        let noise = perlin_2d(x * frequency, y * frequency, config.with_seed(config.seed))
        // Map [-1,1] to [0,1] then back to [-1,1] with bias toward extremes
        total = total + (2.0 * abs(noise) - 1.0) * amplitude
        max_value = max_value + amplitude
        amplitude = amplitude * config.persistence
        frequency = frequency * config.lacunarity
    }

    return total / max_value

    docs {
        Billowy noise with puffy, cloud-like appearance.
    }
}

// ============================================================================
// DOMAIN OPERATIONS
// ============================================================================

pub fun domain_warp(x: f64, y: f64, config: NoiseConfig) -> (f64, f64) {
    let warp_strength = 4.0
    let dx = perlin_2d(x, y, config)
    let dy = perlin_2d(x + 5.2, y + 1.3, config)

    return (x + dx * warp_strength, y + dy * warp_strength)

    docs {
        Warp coordinates using noise.
        Creates swirling, organic distortions.
    }
}

pub fun domain_warp_fbm(x: f64, y: f64, config: NoiseConfig, iterations: u32) -> (f64, f64) {
    let mut wx = x
    let mut wy = y
    let warp_strength = 4.0

    for i in 0..iterations {
        let offset = i as f64 * 1.7
        let dx = fbm(wx + offset, wy + offset, config)
        let dy = fbm(wx + 5.2 + offset, wy + 1.3 + offset, config)
        wx = x + dx * warp_strength
        wy = y + dy * warp_strength
    }

    return (wx, wy)

    docs {
        Multi-iteration domain warping with fBm.
        Creates complex, fluid-like patterns.
    }
}

// ============================================================================
// FIELD GENERATION
// ============================================================================

pub fun generate_noise_field_2d(
    width: u32,
    height: u32,
    config: NoiseConfig,
    scale: f64
) -> NoiseField2D {
    let mut values = vec![]

    for y in 0..height {
        for x in 0..width {
            let nx = x as f64 / scale
            let ny = y as f64 / scale
            let value = fbm(nx, ny, config)
            values.push(value)
        }
    }

    return NoiseField2D {
        width: width,
        height: height,
        values: values
    }

    docs {
        Generate a 2D noise field.
        Scale controls the zoom level (larger = more zoomed in).
    }
}

pub fun generate_noise_field_3d(
    width: u32,
    height: u32,
    depth: u32,
    config: NoiseConfig,
    scale: f64
) -> NoiseField3D {
    let mut values = vec![]

    for z in 0..depth {
        for y in 0..height {
            for x in 0..width {
                let nx = x as f64 / scale
                let ny = y as f64 / scale
                let nz = z as f64 / scale
                let value = perlin_3d(nx, ny, nz, config)
                values.push(value)
            }
        }
    }

    return NoiseField3D {
        width: width,
        height: height,
        depth: depth,
        values: values
    }

    docs {
        Generate a 3D noise field.
    }
}

pub fun make_tileable(field: NoiseField2D, period_x: f64, period_y: f64) -> NoiseField2D {
    let mut values = vec![]

    for y in 0..field.height {
        for x in 0..field.width {
            let u = x as f64 / field.width as f64
            let v = y as f64 / field.height as f64

            // Map to torus coordinates
            let nx = cos(u * TAU) * period_x / TAU
            let ny = sin(u * TAU) * period_x / TAU
            let nz = cos(v * TAU) * period_y / TAU
            let nw = sin(v * TAU) * period_y / TAU

            // Sample 4D noise projected to 2D
            // Approximation using 2D noise at multiple offsets
            let v1 = field.sample_bilinear(nx * field.width as f64, nz * field.height as f64)
            let v2 = field.sample_bilinear(ny * field.width as f64, nw * field.height as f64)
            values.push((v1 + v2) / 2.0)
        }
    }

    return NoiseField2D {
        width: field.width,
        height: field.height,
        values: values
    }

    docs {
        Make a noise field seamlessly tileable.
        Uses torus mapping for perfect tiling.
    }
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fun fade(t: f64) -> f64 {
    // Improved smoothstep: 6t^5 - 15t^4 + 10t^3
    return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}

fun lerp(a: f64, b: f64, t: f64) -> f64 {
    return a + t * (b - a)
}

fun dot2(a: (f64, f64), b: (f64, f64)) -> f64 {
    return a.0 * b.0 + a.1 * b.1
}

fun dot3(a: (f64, f64, f64), b: (f64, f64, f64)) -> f64 {
    return a.0 * b.0 + a.1 * b.1 + a.2 * b.2
}

fun simplex_corner_2d(x: f64, y: f64, hash: u8) -> f64 {
    let t = 0.5 - x * x - y * y
    if t < 0.0 {
        return 0.0
    }
    let t2 = t * t
    let grad_idx = (hash & 7) as f64
    let gx = if (hash & 1) == 0 { 1.0 } else { -1.0 }
    let gy = if (hash & 2) == 0 { 1.0 } else { -1.0 }
    return t2 * t2 * (gx * x + gy * y)
}

fun simplex_corner_3d(x: f64, y: f64, z: f64, hash: u8) -> f64 {
    let t = 0.6 - x * x - y * y - z * z
    if t < 0.0 {
        return 0.0
    }
    let t2 = t * t
    let h = hash & 15
    let u = if h < 8 { x } else { y }
    let v = if h < 4 { y } else if h == 12 || h == 14 { x } else { z }
    return t2 * t2 * ((if (h & 1) == 0 { u } else { -u }) + (if (h & 2) == 0 { v } else { -v }))
}

fun floor(x: f64) -> f64 {
    __builtin_floor(x)
}

fun sqrt(x: f64) -> f64 {
    __builtin_sqrt(x)
}

fun cos(x: f64) -> f64 {
    __builtin_cos(x)
}

fun sin(x: f64) -> f64 {
    __builtin_sin(x)
}

fun abs(x: f64) -> f64 {
    if x < 0.0 { -x } else { x }
}

fun clamp(x: f64, min_val: f64, max_val: f64) -> f64 {
    if x < min_val { min_val }
    else if x > max_val { max_val }
    else { x }
}

docs {
    Generative Art Spirit - Noise Module

    Procedural noise functions for generating natural-looking textures
    and terrain. Based on Ken Perlin's and Stefan Gustavson's work.

    Noise Types:
    - **Perlin**: Classic gradient noise, smooth and natural
    - **Simplex**: Improved noise with fewer artifacts
    - **Worley**: Cellular noise for organic cell patterns
    - **Value**: Simple interpolated random values

    Fractal Techniques:
    - **fBm**: Fractal Brownian Motion - layered octaves
    - **Turbulence**: Absolute value fBm for sharp features
    - **Ridged**: Sharp ridges, good for mountains
    - **Billow**: Puffy, cloud-like appearance

    Domain Operations:
    - **Domain Warp**: Distort coordinates with noise
    - **Tileable**: Make seamless repeating textures

    Configuration:
    - seed: Random seed for reproducibility
    - octaves: Detail levels (more = more detail)
    - lacunarity: Frequency multiplier (2.0 typical)
    - persistence: Amplitude multiplier (0.5 typical)

    Usage:
        // Simple Perlin noise
        let config = default_noise_config()
        let value = perlin_2d(x, y, config)

        // Fractal terrain
        let terrain_config = NoiseConfig {
            seed: 42,
            octaves: 6,
            lacunarity: 2.0,
            persistence: 0.5,
            frequency: 0.01
        }
        let height = fbm(x, y, terrain_config)

        // Generate a noise field
        let field = generate_noise_field_2d(256, 256, config, 32.0)
        let tileable = make_tileable(field, 1.0, 1.0)

    References:
    - Perlin, K. "Improving Noise" (2002)
    - Gustavson, S. "Simplex noise demystified" (2005)
    - Worley, S. "A Cellular Texture Basis Function" (1996)
}