polydat 0.1.0

Polydat — generation kernel for deterministic variate generation in nb-rs (formerly nbrs-variates)
Documentation
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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Coherent noise functions: Perlin, simplex, fractal Brownian motion.
//!
//! Unlike hash functions (which produce uncorrelated "white noise"),
//! coherent noise produces values that vary smoothly — nearby inputs
//! yield similar outputs. This is essential for generating realistic
//! time-series data, spatial fields, and any workload where adjacent
//! coordinates should have correlated values.
//!
//! The permutation table is built at init time from a seed. The noise
//! evaluation runs at cycle time.
//!
//! Inputs are u64 coordinates mapped to a float domain via scaling.
//! Outputs are f64 in [-1, 1] (raw noise) or [0, 1] (normalized).

use crate::node::{GkNode, NodeMeta, Port, Slot, Value};

// =================================================================
// Permutation table (init-time artifact)
// =================================================================

/// A permutation table for noise functions. Built from a seed at init
/// time, immutable thereafter. The table is doubled (512 entries) to
/// avoid modular indexing.
struct PermTable {
    perm: [u8; 512],
}

impl PermTable {
    fn new(seed: u64) -> Self {
        use xxhash_rust::xxh3::xxh3_64;
        let mut p: Vec<u8> = (0..=255).collect();
        // Fisher-Yates shuffle seeded by hash chain
        let mut s = seed;
        for i in (1..256).rev() {
            s = xxh3_64(&s.to_le_bytes());
            let j = (s as usize) % (i + 1);
            p.swap(i, j);
        }
        let mut perm = [0u8; 512];
        for i in 0..512 {
            perm[i] = p[i & 255];
        }
        Self { perm }
    }

    #[inline]
    fn hash(&self, i: i32) -> u8 {
        self.perm[(i & 255) as usize]
    }
}

// =================================================================
// Perlin noise primitives
// =================================================================

#[inline]
fn fade(t: f64) -> f64 {
    // 6t^5 - 15t^4 + 10t^3 (improved Perlin smoothstep)
    t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}

#[inline]
fn lerp(t: f64, a: f64, b: f64) -> f64 {
    a + t * (b - a)
}

#[inline]
fn grad1d(hash: u8, x: f64) -> f64 {
    if hash & 1 == 0 { x } else { -x }
}

#[inline]
fn grad2d(hash: u8, x: f64, y: f64) -> f64 {
    match hash & 3 {
        0 => x + y,
        1 => -x + y,
        2 => x - y,
        _ => -x - y,
    }
}

/// Evaluate 1D Perlin noise at a given point.
fn perlin_1d(perm: &PermTable, x: f64) -> f64 {
    let xi = x.floor() as i32;
    let xf = x - x.floor();
    let u = fade(xf);

    let a = perm.hash(xi);
    let b = perm.hash(xi + 1);

    lerp(u, grad1d(a, xf), grad1d(b, xf - 1.0))
}

/// Evaluate 2D Perlin noise at a given point.
fn perlin_2d(perm: &PermTable, x: f64, y: f64) -> f64 {
    let xi = x.floor() as i32;
    let yi = y.floor() as i32;
    let xf = x - x.floor();
    let yf = y - y.floor();

    let u = fade(xf);
    let v = fade(yf);

    let aa = perm.hash(perm.hash(xi) as i32 + yi);
    let ab = perm.hash(perm.hash(xi) as i32 + yi + 1);
    let ba = perm.hash(perm.hash(xi + 1) as i32 + yi);
    let bb = perm.hash(perm.hash(xi + 1) as i32 + yi + 1);

    lerp(v,
        lerp(u, grad2d(aa, xf, yf), grad2d(ba, xf - 1.0, yf)),
        lerp(u, grad2d(ab, xf, yf - 1.0), grad2d(bb, xf - 1.0, yf - 1.0)),
    )
}

// =================================================================
// Simplex noise 2D
// =================================================================

const F2: f64 = 0.3660254037844386; // (sqrt(3) - 1) / 2
const G2: f64 = 0.21132486540518713; // (3 - sqrt(3)) / 6

fn simplex_2d(perm: &PermTable, x: f64, y: f64) -> f64 {
    let s = (x + y) * F2;
    let i = (x + s).floor() as i32;
    let j = (y + s).floor() as i32;

    let t = (i + j) as f64 * G2;
    let x0 = x - (i as f64 - t);
    let y0 = y - (j as f64 - t);

    let (i1, j1) = if x0 > y0 { (1, 0) } else { (0, 1) };

    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;

    let gi0 = perm.hash(i + perm.hash(j) as i32);
    let gi1 = perm.hash(i + i1 + perm.hash(j + j1) as i32);
    let gi2 = perm.hash(i + 1 + perm.hash(j + 1) as i32);

    let mut n0 = 0.0;
    let t0 = 0.5 - x0 * x0 - y0 * y0;
    if t0 > 0.0 {
        let t0 = t0 * t0;
        n0 = t0 * t0 * grad2d(gi0, x0, y0);
    }

    let mut n1 = 0.0;
    let t1 = 0.5 - x1 * x1 - y1 * y1;
    if t1 > 0.0 {
        let t1 = t1 * t1;
        n1 = t1 * t1 * grad2d(gi1, x1, y1);
    }

    let mut n2 = 0.0;
    let t2 = 0.5 - x2 * x2 - y2 * y2;
    if t2 > 0.0 {
        let t2 = t2 * t2;
        n2 = t2 * t2 * grad2d(gi2, x2, y2);
    }

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

// =================================================================
// GK Nodes
// =================================================================

/// 1D Perlin noise.
///
/// Signature: `(input: u64) -> (f64)`
///
/// The u64 input is scaled to the float domain by `frequency`.
/// Output is in [-1, 1]. For [0, 1], compose with a remap node.
pub struct Perlin1D {
    meta: NodeMeta,
    perm: PermTable,
    frequency: f64,
}

impl Perlin1D {
    pub fn new(seed: u64, frequency: f64) -> Self {
        Self {
            meta: NodeMeta {
                name: "perlin_1d".into(),
                outs: vec![Port::f64("output")],
                ins: vec![Slot::Wire(Port::u64("input"))],
            },
            perm: PermTable::new(seed),
            frequency,
        }
    }
}

impl GkNode for Perlin1D {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let x = inputs[0].as_u64() as f64 * self.frequency;
        outputs[0] = Value::F64(perlin_1d(&self.perm, x));
    }
}

/// 2D Perlin noise.
///
/// Signature: `(x: u64, y: u64) -> (f64)`
pub struct Perlin2D {
    meta: NodeMeta,
    perm: PermTable,
    frequency: f64,
}

impl Perlin2D {
    pub fn new(seed: u64, frequency: f64) -> Self {
        Self {
            meta: NodeMeta {
                name: "perlin_2d".into(),
                outs: vec![Port::f64("output")],
                ins: vec![Slot::Wire(Port::u64("x")), Slot::Wire(Port::u64("y"))],
            },
            perm: PermTable::new(seed),
            frequency,
        }
    }
}

impl GkNode for Perlin2D {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let x = inputs[0].as_u64() as f64 * self.frequency;
        let y = inputs[1].as_u64() as f64 * self.frequency;
        outputs[0] = Value::F64(perlin_2d(&self.perm, x, y));
    }
}

/// 2D Simplex noise.
///
/// Signature: `(x: u64, y: u64) -> (f64)`
///
/// Faster than Perlin for 2D+ with fewer directional artifacts.
pub struct SimplexNoise2D {
    meta: NodeMeta,
    perm: PermTable,
    frequency: f64,
}

impl SimplexNoise2D {
    pub fn new(seed: u64, frequency: f64) -> Self {
        Self {
            meta: NodeMeta {
                name: "simplex_2d".into(),
                outs: vec![Port::f64("output")],
                ins: vec![Slot::Wire(Port::u64("x")), Slot::Wire(Port::u64("y"))],
            },
            perm: PermTable::new(seed),
            frequency,
        }
    }
}

impl GkNode for SimplexNoise2D {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let x = inputs[0].as_u64() as f64 * self.frequency;
        let y = inputs[1].as_u64() as f64 * self.frequency;
        outputs[0] = Value::F64(simplex_2d(&self.perm, x, y));
    }
}

/// Fractal Brownian motion (FBM): layered octaves of noise.
///
/// Signature: `(input: u64) -> (f64)`
///
/// Each octave doubles the frequency and halves the amplitude,
/// producing multi-scale detail. Output range grows with octaves
/// but stays bounded.
pub struct FractalNoise1D {
    meta: NodeMeta,
    perm: PermTable,
    frequency: f64,
    octaves: u32,
    lacunarity: f64,
    persistence: f64,
}

impl FractalNoise1D {
    /// Create with standard parameters.
    ///
    /// - `octaves`: number of noise layers (1-8 typical)
    /// - `lacunarity`: frequency multiplier per octave (default 2.0)
    /// - `persistence`: amplitude multiplier per octave (default 0.5)
    pub fn new(seed: u64, frequency: f64, octaves: u32) -> Self {
        Self::with_params(seed, frequency, octaves, 2.0, 0.5)
    }

    pub fn with_params(
        seed: u64, frequency: f64, octaves: u32,
        lacunarity: f64, persistence: f64,
    ) -> Self {
        Self {
            meta: NodeMeta {
                name: "fractal_noise_1d".into(),
                outs: vec![Port::f64("output")],
                ins: vec![Slot::Wire(Port::u64("input"))],
            },
            perm: PermTable::new(seed),
            frequency,
            octaves,
            lacunarity,
            persistence,
        }
    }
}

impl GkNode for FractalNoise1D {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let base_x = inputs[0].as_u64() as f64;
        let mut total = 0.0;
        let mut freq = self.frequency;
        let mut amp = 1.0;
        let mut max_amp = 0.0;

        for _ in 0..self.octaves {
            total += perlin_1d(&self.perm, base_x * freq) * amp;
            max_amp += amp;
            freq *= self.lacunarity;
            amp *= self.persistence;
        }

        // Normalize to [-1, 1]
        outputs[0] = Value::F64(total / max_amp);
    }
}

/// 2D Fractal Brownian motion.
///
/// Signature: `(x: u64, y: u64) -> (f64)`
pub struct FractalNoise2D {
    meta: NodeMeta,
    perm: PermTable,
    frequency: f64,
    octaves: u32,
    lacunarity: f64,
    persistence: f64,
}

impl FractalNoise2D {
    pub fn new(seed: u64, frequency: f64, octaves: u32) -> Self {
        Self::with_params(seed, frequency, octaves, 2.0, 0.5)
    }

    pub fn with_params(
        seed: u64, frequency: f64, octaves: u32,
        lacunarity: f64, persistence: f64,
    ) -> Self {
        Self {
            meta: NodeMeta {
                name: "fractal_noise_2d".into(),
                outs: vec![Port::f64("output")],
                ins: vec![Slot::Wire(Port::u64("x")), Slot::Wire(Port::u64("y"))],
            },
            perm: PermTable::new(seed),
            frequency,
            octaves,
            lacunarity,
            persistence,
        }
    }
}

impl GkNode for FractalNoise2D {
    fn meta(&self) -> &NodeMeta { &self.meta }
    fn eval(&self, inputs: &[Value], outputs: &mut [Value]) {
        let base_x = inputs[0].as_u64() as f64;
        let base_y = inputs[1].as_u64() as f64;
        let mut total = 0.0;
        let mut freq = self.frequency;
        let mut amp = 1.0;
        let mut max_amp = 0.0;

        for _ in 0..self.octaves {
            total += perlin_2d(&self.perm, base_x * freq, base_y * freq) * amp;
            max_amp += amp;
            freq *= self.lacunarity;
            amp *= self.persistence;
        }

        outputs[0] = Value::F64(total / max_amp);
    }
}

// ---------------------------------------------------------------------------
// Signature declarations for the DSL registry
// ---------------------------------------------------------------------------

use crate::dsl::registry::{Arity, FuncCategory, FuncSig, ParamSpec};
use crate::node::SlotType;

/// Signatures for coherent noise nodes.
pub fn signatures() -> &'static [FuncSig] {
    use FuncCategory as C;
    &[
        FuncSig {
            name: "perlin_1d", category: C::Noise,
            outputs: 1, description: "1D Perlin noise",
            identity: None, variadic_ctor: None,
            params: &[
                ParamSpec { name: "input", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "seed", slot_type: SlotType::ConstU64, required: true, example: "42", constraint: None },
                ParamSpec { name: "frequency", slot_type: SlotType::ConstF64, required: true, example: "1.0", constraint: None },
            ],
            arity: Arity::Fixed,
            commutativity: crate::node::Commutativity::Positional,
            help: "1D Perlin noise: coherent pseudo-random f64 in [-1, 1].\nThe u64 input is scaled to the float domain by frequency.\nNearby inputs produce smoothly varying outputs (spatial correlation).\nParameters:\n  input     — u64 wire input\n  seed      — permutation table seed (u64)\n  frequency — spatial frequency (f64; higher = more detail)\nExample: perlin_1d(cycle, 42, 0.01)  // slow-varying noise",
            default_resolver: None,
            output_type: crate::dsl::registry::OutputType::Fixed,
        },
        FuncSig {
            name: "perlin_2d", category: C::Noise,
            outputs: 1, description: "2D Perlin noise",
            identity: None, variadic_ctor: None,
            params: &[
                ParamSpec { name: "x", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "y", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "seed", slot_type: SlotType::ConstU64, required: true, example: "42", constraint: None },
                ParamSpec { name: "frequency", slot_type: SlotType::ConstF64, required: true, example: "1.0", constraint: None },
            ],
            arity: Arity::Fixed,
            commutativity: crate::node::Commutativity::Positional,
            help: "2D Perlin noise: coherent pseudo-random f64 in [-1, 1].\nTwo u64 coordinate inputs are scaled by frequency.\nProduces spatially correlated values for terrain, textures, etc.\nParameters:\n  x         — u64 x-coordinate wire input\n  y         — u64 y-coordinate wire input\n  seed      — permutation table seed (u64)\n  frequency — spatial frequency (f64)\nExample: perlin_2d(row, col, 42, 0.05)",
            default_resolver: None,
            output_type: crate::dsl::registry::OutputType::Fixed,
        },
        FuncSig {
            name: "simplex_2d", category: C::Noise,
            outputs: 1, description: "2D simplex noise",
            identity: None, variadic_ctor: None,
            params: &[
                ParamSpec { name: "x", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "y", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "seed", slot_type: SlotType::ConstU64, required: true, example: "42", constraint: None },
                ParamSpec { name: "frequency", slot_type: SlotType::ConstF64, required: true, example: "1.0", constraint: None },
            ],
            arity: Arity::Fixed,
            commutativity: crate::node::Commutativity::Positional,
            help: "2D simplex noise: faster than Perlin for 2D+ with fewer directional artifacts.\nOutput is f64 in [-1, 1]. Uses a simplex grid instead of a square grid.\nParameters:\n  x         — u64 x-coordinate wire input\n  y         — u64 y-coordinate wire input\n  seed      — permutation table seed (u64)\n  frequency — spatial frequency (f64)\nExample: simplex_2d(row, col, 99, 0.1)",
            default_resolver: None,
            output_type: crate::dsl::registry::OutputType::Fixed,
        },
        FuncSig {
            name: "fractal_noise_1d", category: C::Noise,
            outputs: 1, description: "1D fractal Brownian motion",
            identity: None, variadic_ctor: None,
            params: &[
                ParamSpec { name: "input", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "seed", slot_type: SlotType::ConstU64, required: true, example: "42", constraint: None },
                ParamSpec { name: "frequency", slot_type: SlotType::ConstF64, required: true, example: "1.0", constraint: None },
                ParamSpec { name: "octaves", slot_type: SlotType::ConstU64, required: false, example: "4", constraint: None },
            ],
            arity: Arity::Fixed,
            commutativity: crate::node::Commutativity::Positional,
            help: "1D fractal Brownian motion: layered Perlin noise with decreasing\namplitude at each octave. Produces rich, natural-looking signals.\nOutput is f64, roughly in [-1, 1].\nParameters:\n  input     — u64 wire input\n  seed      — permutation table seed (u64)\n  frequency — base spatial frequency (f64)\n  octaves   — number of noise layers (u64, default 4)\nExample: fractal_noise_1d(cycle, 42, 0.02, 4)",
            default_resolver: None,
            output_type: crate::dsl::registry::OutputType::Fixed,
        },
        FuncSig {
            name: "fractal_noise_2d", category: C::Noise,
            outputs: 1, description: "2D fractal Brownian motion",
            identity: None, variadic_ctor: None,
            params: &[
                ParamSpec { name: "x", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "y", slot_type: SlotType::Wire, required: true, example: "cycle", constraint: None },
                ParamSpec { name: "seed", slot_type: SlotType::ConstU64, required: true, example: "42", constraint: None },
                ParamSpec { name: "frequency", slot_type: SlotType::ConstF64, required: true, example: "1.0", constraint: None },
                ParamSpec { name: "octaves", slot_type: SlotType::ConstU64, required: false, example: "4", constraint: None },
            ],
            arity: Arity::Fixed,
            commutativity: crate::node::Commutativity::Positional,
            help: "2D fractal Brownian motion: layered Perlin noise in 2D.\nProduces terrain-like spatial variation.\nParameters:\n  x, y      — u64 coordinate wire inputs\n  seed      — permutation table seed (u64)\n  frequency — base spatial frequency (f64)\n  octaves   — number of noise layers (u64, default 4)\nExample: fractal_noise_2d(row, col, 42, 0.05, 4)",
            default_resolver: None,
            output_type: crate::dsl::registry::OutputType::Fixed,
        },
    ]
}

/// Try to build a noise node from a function name and const args.
///
/// Returns `None` if the name is not handled by this module.
pub(crate) fn build_node(name: &str, _wires: &[crate::assembly::WireRef], _wire_types: &[crate::node::PortType], consts: &[crate::dsl::factory::ConstArg]) -> Option<Result<Box<dyn crate::node::GkNode>, String>> {
    match name {
        "perlin_1d" => Some(Ok(Box::new(Perlin1D::new(
            consts.first().map(|c| c.as_u64()).unwrap_or(0),
            consts.get(1).map(|c| c.as_f64()).unwrap_or(0.01),
        )))),
        "perlin_2d" => Some(Ok(Box::new(Perlin2D::new(
            consts.first().map(|c| c.as_u64()).unwrap_or(0),
            consts.get(1).map(|c| c.as_f64()).unwrap_or(0.01),
        )))),
        "simplex_2d" => Some(Ok(Box::new(SimplexNoise2D::new(
            consts.first().map(|c| c.as_u64()).unwrap_or(0),
            consts.get(1).map(|c| c.as_f64()).unwrap_or(0.01),
        )))),
        "fractal_noise_1d" => Some(Ok(Box::new(FractalNoise1D::new(
            consts.first().map(|c| c.as_u64()).unwrap_or(0),
            consts.get(1).map(|c| c.as_f64()).unwrap_or(0.02),
            consts.get(2).map(|c| c.as_u64() as u32).unwrap_or(4),
        )))),
        "fractal_noise_2d" => Some(Ok(Box::new(FractalNoise2D::new(
            consts.first().map(|c| c.as_u64()).unwrap_or(0),
            consts.get(1).map(|c| c.as_f64()).unwrap_or(0.02),
            consts.get(2).map(|c| c.as_u64() as u32).unwrap_or(4),
        )))),
        _ => None,
    }
}


crate::register_nodes!(signatures, build_node);
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn perlin_1d_bounded() {
        let node = Perlin1D::new(42, 0.01);
        let mut out = [Value::None];
        for i in 0..1000u64 {
            node.eval(&[Value::U64(i)], &mut out);
            let v = out[0].as_f64();
            assert!(v >= -1.0 && v <= 1.0, "out of range: {v} at i={i}");
        }
    }

    #[test]
    fn perlin_1d_smooth() {
        // Adjacent inputs should produce similar (not identical) values
        let node = Perlin1D::new(42, 0.01);
        let mut prev = [Value::None];
        let mut curr = [Value::None];
        node.eval(&[Value::U64(100)], &mut prev);
        let mut large_jumps = 0;
        for i in 101..200u64 {
            node.eval(&[Value::U64(i)], &mut curr);
            let diff = (curr[0].as_f64() - prev[0].as_f64()).abs();
            if diff > 0.5 { large_jumps += 1; }
            prev[0] = curr[0].clone();
        }
        // With frequency 0.01, adjacent samples should rarely jump more than 0.5
        assert!(large_jumps < 5, "too many large jumps: {large_jumps}");
    }

    #[test]
    fn perlin_1d_deterministic() {
        let node = Perlin1D::new(42, 0.1);
        let mut out1 = [Value::None];
        let mut out2 = [Value::None];
        node.eval(&[Value::U64(123)], &mut out1);
        node.eval(&[Value::U64(123)], &mut out2);
        assert_eq!(out1[0].as_f64(), out2[0].as_f64());
    }

    #[test]
    fn perlin_1d_different_seeds() {
        let a = Perlin1D::new(1, 0.1);
        let b = Perlin1D::new(2, 0.1);
        let mut out_a = [Value::None];
        let mut out_b = [Value::None];
        let mut differ = false;
        for i in 0..100u64 {
            a.eval(&[Value::U64(i)], &mut out_a);
            b.eval(&[Value::U64(i)], &mut out_b);
            if (out_a[0].as_f64() - out_b[0].as_f64()).abs() > 0.01 {
                differ = true;
                break;
            }
        }
        assert!(differ, "different seeds should produce different noise");
    }

    #[test]
    fn perlin_2d_bounded() {
        let node = Perlin2D::new(42, 0.01);
        let mut out = [Value::None];
        for x in 0..50u64 {
            for y in 0..50u64 {
                node.eval(&[Value::U64(x), Value::U64(y)], &mut out);
                let v = out[0].as_f64();
                assert!(v >= -1.5 && v <= 1.5, "out of range: {v} at ({x},{y})");
            }
        }
    }

    #[test]
    fn perlin_2d_smooth() {
        let node = Perlin2D::new(42, 0.01);
        let mut prev = [Value::None];
        let mut curr = [Value::None];
        node.eval(&[Value::U64(100), Value::U64(100)], &mut prev);
        let mut large_jumps = 0;
        for i in 101..150u64 {
            node.eval(&[Value::U64(i), Value::U64(100)], &mut curr);
            let diff = (curr[0].as_f64() - prev[0].as_f64()).abs();
            if diff > 0.5 { large_jumps += 1; }
            prev[0] = curr[0].clone();
        }
        assert!(large_jumps < 5, "too many large jumps: {large_jumps}");
    }

    #[test]
    fn simplex_2d_bounded() {
        let node = SimplexNoise2D::new(42, 0.01);
        let mut out = [Value::None];
        for x in 0..50u64 {
            for y in 0..50u64 {
                node.eval(&[Value::U64(x), Value::U64(y)], &mut out);
                let v = out[0].as_f64();
                assert!(v >= -1.5 && v <= 1.5, "out of range: {v}");
            }
        }
    }

    #[test]
    fn fractal_1d_bounded() {
        let node = FractalNoise1D::new(42, 0.01, 4);
        let mut out = [Value::None];
        for i in 0..500u64 {
            node.eval(&[Value::U64(i)], &mut out);
            let v = out[0].as_f64();
            assert!(v >= -1.5 && v <= 1.5, "out of range: {v}");
        }
    }

    #[test]
    fn fractal_1d_more_detail_than_single_octave() {
        // FBM with 4 octaves should have more high-frequency variation
        // than a single octave
        let single = Perlin1D::new(42, 0.01);
        let fbm = FractalNoise1D::new(42, 0.01, 4);
        let mut s_out = [Value::None];
        let mut f_out = [Value::None];
        let mut s_changes = 0.0;
        let mut f_changes = 0.0;
        let mut s_prev = 0.0;
        let mut f_prev = 0.0;
        for i in 0..500u64 {
            single.eval(&[Value::U64(i)], &mut s_out);
            fbm.eval(&[Value::U64(i)], &mut f_out);
            if i > 0 {
                s_changes += (s_out[0].as_f64() - s_prev).abs();
                f_changes += (f_out[0].as_f64() - f_prev).abs();
            }
            s_prev = s_out[0].as_f64();
            f_prev = f_out[0].as_f64();
        }
        // FBM should have more total variation (higher frequency detail)
        assert!(f_changes > s_changes * 0.8,
            "FBM should have comparable or more detail: single={s_changes}, fbm={f_changes}");
    }

    #[test]
    fn fractal_2d_bounded() {
        let node = FractalNoise2D::new(42, 0.01, 3);
        let mut out = [Value::None];
        for x in 0..30u64 {
            for y in 0..30u64 {
                node.eval(&[Value::U64(x), Value::U64(y)], &mut out);
                let v = out[0].as_f64();
                assert!(v >= -1.5 && v <= 1.5, "out of range: {v}");
            }
        }
    }
}