scirs2-ndimage 0.4.2

N-dimensional image processing module for SciRS2 (scirs2-ndimage)
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
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
//! 3D Image Filters
//!
//! This module provides a collection of three-dimensional image filters for
//! volumetric data, including linear filters (Gaussian, uniform, Sobel,
//! Laplacian) and non-linear filters (median, bilateral, non-local means,
//! anisotropic diffusion).
//!
//! # Design Notes
//!
//! * All filters operate on `f64` arrays for precision.
//! * Border handling uses "clamp to edge" (nearest neighbor) by default.
//! * The 3D Gaussian filter is implemented as separable 1D convolutions
//!   applied successively along z, y, and x axes.

use crate::error::{NdimageError, NdimageResult};
use scirs2_core::ndarray::{Array1, Array3, ArrayView3};

// ---------------------------------------------------------------------------
// Border handling helpers
// ---------------------------------------------------------------------------

/// Clamp index to `[0, max_idx]`.
#[inline]
fn clamp_idx(i: isize, max_idx: usize) -> usize {
    i.clamp(0, max_idx as isize) as usize
}

/// Read a voxel with clamp-to-edge border handling.
#[inline]
fn get_clamped(img: &Array3<f64>, z: isize, y: isize, x: isize) -> f64 {
    let shape = img.shape();
    let sz = clamp_idx(z, shape[0].saturating_sub(1));
    let sy = clamp_idx(y, shape[1].saturating_sub(1));
    let sx = clamp_idx(x, shape[2].saturating_sub(1));
    img[[sz, sy, sx]]
}

// ---------------------------------------------------------------------------
// 1D Gaussian kernel
// ---------------------------------------------------------------------------

/// Build a 1D Gaussian kernel of half-width `radius = (truncate * sigma).ceil()`.
fn gaussian_kernel1d(sigma: f64, truncate: f64) -> Array1<f64> {
    let radius = (truncate * sigma).ceil() as usize;
    let size = 2 * radius + 1;
    let mut k = Array1::zeros(size);
    let two_sigma_sq = 2.0 * sigma * sigma;
    let mut sum = 0.0;
    for i in 0..size {
        let x = i as f64 - radius as f64;
        let v = (-x * x / two_sigma_sq).exp();
        k[i] = v;
        sum += v;
    }
    k.mapv_inplace(|v| v / sum);
    k
}

// ---------------------------------------------------------------------------
// Separable 1D convolution helpers
// ---------------------------------------------------------------------------

/// Apply a 1D kernel along the Z axis of a 3D array (clamp border).
fn convolve_z(src: &Array3<f64>, kernel: &Array1<f64>) -> Array3<f64> {
    let (sz, sy, sx) = (src.shape()[0], src.shape()[1], src.shape()[2]);
    let half = (kernel.len() / 2) as isize;
    let mut out = Array3::zeros((sz, sy, sx));
    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut acc = 0.0;
                for (ki, &kv) in kernel.iter().enumerate() {
                    let nz = iz as isize + ki as isize - half;
                    let cz = clamp_idx(nz, sz.saturating_sub(1));
                    acc += kv * src[[cz, iy, ix]];
                }
                out[[iz, iy, ix]] = acc;
            }
        }
    }
    out
}

/// Apply a 1D kernel along the Y axis of a 3D array (clamp border).
fn convolve_y(src: &Array3<f64>, kernel: &Array1<f64>) -> Array3<f64> {
    let (sz, sy, sx) = (src.shape()[0], src.shape()[1], src.shape()[2]);
    let half = (kernel.len() / 2) as isize;
    let mut out = Array3::zeros((sz, sy, sx));
    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut acc = 0.0;
                for (ki, &kv) in kernel.iter().enumerate() {
                    let ny = iy as isize + ki as isize - half;
                    let cy = clamp_idx(ny, sy.saturating_sub(1));
                    acc += kv * src[[iz, cy, ix]];
                }
                out[[iz, iy, ix]] = acc;
            }
        }
    }
    out
}

/// Apply a 1D kernel along the X axis of a 3D array (clamp border).
fn convolve_x(src: &Array3<f64>, kernel: &Array1<f64>) -> Array3<f64> {
    let (sz, sy, sx) = (src.shape()[0], src.shape()[1], src.shape()[2]);
    let half = (kernel.len() / 2) as isize;
    let mut out = Array3::zeros((sz, sy, sx));
    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut acc = 0.0;
                for (ki, &kv) in kernel.iter().enumerate() {
                    let nx = ix as isize + ki as isize - half;
                    let cx = clamp_idx(nx, sx.saturating_sub(1));
                    acc += kv * src[[iz, iy, cx]];
                }
                out[[iz, iy, ix]] = acc;
            }
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Public filter API
// ---------------------------------------------------------------------------

/// 3D Gaussian filter (separable).
///
/// Applies a Gaussian blur by convolving the image with a 1D Gaussian kernel
/// independently along each of the three spatial axes (z, y, x).  This is
/// mathematically equivalent to a full 3D Gaussian convolution but runs in
/// O(n × k) time instead of O(n × k³).
///
/// # Arguments
///
/// * `image`    - Input volumetric image.
/// * `sigma`    - Standard deviation of the Gaussian in voxels (must be > 0).
/// * `truncate` - Kernel half-width in units of sigma (typical: 4.0).
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` if `sigma ≤ 0` or `truncate ≤ 0`.
pub fn gaussian_filter3d(
    image: ArrayView3<f64>,
    sigma: f64,
    truncate: f64,
) -> NdimageResult<Array3<f64>> {
    if sigma <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "sigma must be positive".to_string(),
        ));
    }
    if truncate <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "truncate must be positive".to_string(),
        ));
    }
    let kernel = gaussian_kernel1d(sigma, truncate);
    let owned = image.to_owned();
    let tmp1 = convolve_z(&owned, &kernel);
    let tmp2 = convolve_y(&tmp1, &kernel);
    Ok(convolve_x(&tmp2, &kernel))
}

/// 3D median filter.
///
/// Replaces each voxel with the median value in a cubic window of side `size`
/// (must be odd and ≥ 1).  Uses clamp-to-edge border handling.
///
/// # Arguments
///
/// * `image` - Input volumetric image.
/// * `size`  - Cubic window side length (must be odd and ≥ 1).
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` if `size == 0` or `size` is even.
pub fn median_filter3d(image: ArrayView3<f64>, size: usize) -> NdimageResult<Array3<f64>> {
    if size == 0 {
        return Err(NdimageError::InvalidInput(
            "size must be at least 1".to_string(),
        ));
    }
    if size % 2 == 0 {
        return Err(NdimageError::InvalidInput(
            "size must be odd".to_string(),
        ));
    }
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let half = (size / 2) as isize;
    let owned = image.to_owned();
    let mut out = Array3::zeros((sz, sy, sx));

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut window: Vec<f64> = Vec::with_capacity(size * size * size);
                for dz in -half..=half {
                    for dy in -half..=half {
                        for dx in -half..=half {
                            window.push(get_clamped(
                                &owned,
                                iz as isize + dz,
                                iy as isize + dy,
                                ix as isize + dx,
                            ));
                        }
                    }
                }
                window.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                out[[iz, iy, ix]] = window[window.len() / 2];
            }
        }
    }
    Ok(out)
}

/// 3D uniform (box) filter.
///
/// Replaces each voxel with the mean value in a cubic window of side `size`
/// (must be odd and ≥ 1).  Uses clamp-to-edge border handling.
///
/// # Arguments
///
/// * `image` - Input volumetric image.
/// * `size`  - Cubic window side length (must be odd and ≥ 1).
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` if `size == 0` or `size` is even.
pub fn uniform_filter3d(image: ArrayView3<f64>, size: usize) -> NdimageResult<Array3<f64>> {
    if size == 0 {
        return Err(NdimageError::InvalidInput(
            "size must be at least 1".to_string(),
        ));
    }
    if size % 2 == 0 {
        return Err(NdimageError::InvalidInput(
            "size must be odd".to_string(),
        ));
    }
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let half = (size / 2) as isize;
    let owned = image.to_owned();
    let n = (size * size * size) as f64;
    let mut out = Array3::zeros((sz, sy, sx));

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut sum = 0.0;
                for dz in -half..=half {
                    for dy in -half..=half {
                        for dx in -half..=half {
                            sum += get_clamped(
                                &owned,
                                iz as isize + dz,
                                iy as isize + dy,
                                ix as isize + dx,
                            );
                        }
                    }
                }
                out[[iz, iy, ix]] = sum / n;
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Gradient and Laplacian
// ---------------------------------------------------------------------------

/// 3D Sobel gradient magnitude.
///
/// Computes the gradient magnitude using Sobel kernels applied independently
/// along x, y, and z axes, then combines them as
/// `sqrt(Gx² + Gy² + Gz²)`.
///
/// # Arguments
///
/// * `image` - Input volumetric image.
pub fn sobel_gradient3d(image: ArrayView3<f64>) -> NdimageResult<Array3<f64>> {
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let owned = image.to_owned();
    let mut out = Array3::zeros((sz, sy, sx));

    // Sobel kernel: smooth × diff in 3D
    // Gx = sobel along x, Gy along y, Gz along z
    // Each is the outer product of:
    //   smooth = [1, 2, 1] / 4
    //   diff   = [-1, 0, 1]
    // We compute them as separable operations for efficiency.

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let z = iz as isize;
                let y = iy as isize;
                let x = ix as isize;

                // Gx: diff along x, smooth along y and z
                let gx = {
                    let s = |dz: isize, dy: isize, dx: isize| {
                        get_clamped(&owned, z + dz, y + dy, x + dx)
                    };
                    // smooth(z) × smooth(y) × diff(x)
                    let sz_weights = [1.0f64, 2.0, 1.0];
                    let sy_weights = [1.0f64, 2.0, 1.0];
                    let dx_weights = [-1.0f64, 0.0, 1.0];
                    let mut g = 0.0f64;
                    for (dz, &wz) in sz_weights.iter().enumerate() {
                        for (dy, &wy) in sy_weights.iter().enumerate() {
                            for (dx, &wx) in dx_weights.iter().enumerate() {
                                let ndz = dz as isize - 1;
                                let ndy = dy as isize - 1;
                                let ndx = dx as isize - 1;
                                g += wz * wy * wx * s(ndz, ndy, ndx);
                            }
                        }
                    }
                    g / 32.0
                };

                // Gy: smooth along x and z, diff along y
                let gy = {
                    let s = |dz: isize, dy: isize, dx: isize| {
                        get_clamped(&owned, z + dz, y + dy, x + dx)
                    };
                    let sz_weights = [1.0f64, 2.0, 1.0];
                    let dy_weights = [-1.0f64, 0.0, 1.0];
                    let sx_weights = [1.0f64, 2.0, 1.0];
                    let mut g = 0.0f64;
                    for (dz, &wz) in sz_weights.iter().enumerate() {
                        for (dy, &wy) in dy_weights.iter().enumerate() {
                            for (dx, &wx) in sx_weights.iter().enumerate() {
                                let ndz = dz as isize - 1;
                                let ndy = dy as isize - 1;
                                let ndx = dx as isize - 1;
                                g += wz * wy * wx * s(ndz, ndy, ndx);
                            }
                        }
                    }
                    g / 32.0
                };

                // Gz: smooth along x and y, diff along z
                let gz = {
                    let s = |dz: isize, dy: isize, dx: isize| {
                        get_clamped(&owned, z + dz, y + dy, x + dx)
                    };
                    let dz_weights = [-1.0f64, 0.0, 1.0];
                    let sy_weights = [1.0f64, 2.0, 1.0];
                    let sx_weights = [1.0f64, 2.0, 1.0];
                    let mut g = 0.0f64;
                    for (dz, &wz) in dz_weights.iter().enumerate() {
                        for (dy, &wy) in sy_weights.iter().enumerate() {
                            for (dx, &wx) in sx_weights.iter().enumerate() {
                                let ndz = dz as isize - 1;
                                let ndy = dy as isize - 1;
                                let ndx = dx as isize - 1;
                                g += wz * wy * wx * s(ndz, ndy, ndx);
                            }
                        }
                    }
                    g / 32.0
                };

                out[[iz, iy, ix]] = (gx * gx + gy * gy + gz * gz).sqrt();
            }
        }
    }
    Ok(out)
}

/// 3D Laplacian (discrete second-order derivative).
///
/// Uses the 7-point stencil (6-connected neighbors):
/// `L[z,y,x] = sum_neighbors(v) - 6 * v[z,y,x]`
///
/// # Arguments
///
/// * `image` - Input volumetric image.
pub fn laplace3d(image: ArrayView3<f64>) -> NdimageResult<Array3<f64>> {
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let owned = image.to_owned();
    let mut out = Array3::zeros((sz, sy, sx));

    let neighbors: [(isize, isize, isize); 6] = [
        (-1, 0, 0),
        (1, 0, 0),
        (0, -1, 0),
        (0, 1, 0),
        (0, 0, -1),
        (0, 0, 1),
    ];

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let center = owned[[iz, iy, ix]];
                let mut laplacian = -6.0 * center;
                for &(dz, dy, dx) in &neighbors {
                    laplacian += get_clamped(&owned, iz as isize + dz, iy as isize + dy, ix as isize + dx);
                }
                out[[iz, iy, ix]] = laplacian;
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Non-local means denoising
// ---------------------------------------------------------------------------

/// 3D non-local means denoising.
///
/// Each voxel is replaced by a weighted average of voxels with similar patch
/// neighborhoods within a search window.  The weights decay exponentially with
/// the patch dissimilarity.
///
/// # Arguments
///
/// * `image`         - Input volumetric image.
/// * `patch_size`    - Side length of comparison patch cube (must be odd and ≥ 1).
/// * `search_radius` - Half-side of the search window (search cube side = `2 * r + 1`).
/// * `h`             - Filter strength parameter (larger ⇒ smoother).
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` for invalid parameter values.
pub fn non_local_means3d(
    image: ArrayView3<f64>,
    patch_size: usize,
    search_radius: usize,
    h: f64,
) -> NdimageResult<Array3<f64>> {
    if patch_size == 0 || patch_size % 2 == 0 {
        return Err(NdimageError::InvalidInput(
            "patch_size must be a positive odd integer".to_string(),
        ));
    }
    if h <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "h must be positive".to_string(),
        ));
    }
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let owned = image.to_owned();
    let ph = (patch_size / 2) as isize;
    let sr = search_radius as isize;
    let h2 = h * h;
    let patch_vol = patch_size * patch_size * patch_size;

    let mut out = Array3::zeros((sz, sy, sx));

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let mut weight_sum = 0.0f64;
                let mut value_sum = 0.0f64;

                // Iterate over search window
                let z0 = (iz as isize - sr).max(0) as usize;
                let z1 = (iz as isize + sr).min(sz as isize - 1) as usize;
                let y0 = (iy as isize - sr).max(0) as usize;
                let y1 = (iy as isize + sr).min(sy as isize - 1) as usize;
                let x0 = (ix as isize - sr).max(0) as usize;
                let x1 = (ix as isize + sr).min(sx as isize - 1) as usize;

                for jz in z0..=z1 {
                    for jy in y0..=y1 {
                        for jx in x0..=x1 {
                            // Compute squared patch distance
                            let mut dist_sq = 0.0f64;
                            for dz in -ph..=ph {
                                for dy in -ph..=ph {
                                    for dx in -ph..=ph {
                                        let pz_i = iz as isize + dz;
                                        let py_i = iy as isize + dy;
                                        let px_i = ix as isize + dx;
                                        let pz_j = jz as isize + dz;
                                        let py_j = jy as isize + dy;
                                        let px_j = jx as isize + dx;
                                        let vi = get_clamped(&owned, pz_i, py_i, px_i);
                                        let vj = get_clamped(&owned, pz_j, py_j, px_j);
                                        let diff = vi - vj;
                                        dist_sq += diff * diff;
                                    }
                                }
                            }
                            dist_sq /= patch_vol as f64;
                            let w = (-dist_sq / h2).exp();
                            weight_sum += w;
                            value_sum += w * owned[[jz, jy, jx]];
                        }
                    }
                }

                out[[iz, iy, ix]] = if weight_sum > 0.0 {
                    value_sum / weight_sum
                } else {
                    owned[[iz, iy, ix]]
                };
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Anisotropic diffusion (Perona-Malik)
// ---------------------------------------------------------------------------

/// Perona-Malik conductance function variant.
#[inline]
fn pm_conductance(gradient_mag: f64, kappa: f64) -> f64 {
    // Exponential: c = exp(-(|∇I|/kappa)²)
    let k = gradient_mag / kappa;
    (-k * k).exp()
}

/// 3D anisotropic diffusion (Perona-Malik model).
///
/// Iteratively diffuses the image while reducing diffusion across high-gradient
/// regions (edges), thus denoising flat areas while preserving boundaries.
///
/// # Stability
///
/// For 3D stability the time step `gamma` should satisfy `gamma < 1/6`.
/// Values larger than this may produce oscillations.
///
/// # Arguments
///
/// * `image`      - Input volumetric image.
/// * `iterations` - Number of diffusion steps.
/// * `kappa`      - Edge-stopping threshold.  Larger values preserve fewer edges.
/// * `gamma`      - Time step (`< 1/6` for stable 3D diffusion).
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` for non-positive parameters.
pub fn anisotropic_diffusion3d(
    image: ArrayView3<f64>,
    iterations: usize,
    kappa: f64,
    gamma: f64,
) -> NdimageResult<Array3<f64>> {
    if kappa <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "kappa must be positive".to_string(),
        ));
    }
    if gamma <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "gamma must be positive".to_string(),
        ));
    }
    if iterations == 0 {
        return Ok(image.to_owned());
    }

    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let mut current = image.to_owned();

    for _ in 0..iterations {
        let prev = current.clone();
        for iz in 0..sz {
            for iy in 0..sy {
                for ix in 0..sx {
                    let z = iz as isize;
                    let y = iy as isize;
                    let x = ix as isize;
                    let center = prev[[iz, iy, ix]];

                    // 6-connected gradient differences
                    let gz_n = get_clamped(&prev, z - 1, y, x) - center;
                    let gz_p = get_clamped(&prev, z + 1, y, x) - center;
                    let gy_n = get_clamped(&prev, z, y - 1, x) - center;
                    let gy_p = get_clamped(&prev, z, y + 1, x) - center;
                    let gx_n = get_clamped(&prev, z, y, x - 1) - center;
                    let gx_p = get_clamped(&prev, z, y, x + 1) - center;

                    // Conductance coefficients
                    let cn = pm_conductance(gz_n.abs(), kappa);
                    let cp = pm_conductance(gz_p.abs(), kappa);
                    let cyn = pm_conductance(gy_n.abs(), kappa);
                    let cyp = pm_conductance(gy_p.abs(), kappa);
                    let cxn = pm_conductance(gx_n.abs(), kappa);
                    let cxp = pm_conductance(gx_p.abs(), kappa);

                    let flux = cn * gz_n + cp * gz_p + cyn * gy_n + cyp * gy_p
                        + cxn * gx_n + cxp * gx_p;

                    current[[iz, iy, ix]] = center + gamma * flux;
                }
            }
        }
    }
    Ok(current)
}

// ---------------------------------------------------------------------------
// Bilateral filter
// ---------------------------------------------------------------------------

/// 3D bilateral filter.
///
/// Smooths the image while preserving edges by weighting contributions from
/// neighboring voxels by both spatial proximity and intensity similarity.
///
/// The window radius is derived from `sigma_spatial` as
/// `radius = ceil(3 * sigma_spatial)`.
///
/// # Arguments
///
/// * `image`            - Input volumetric image.
/// * `sigma_spatial`    - Spatial Gaussian standard deviation in voxels.
/// * `sigma_intensity`  - Intensity Gaussian standard deviation.
///
/// # Errors
///
/// Returns `NdimageError::InvalidInput` if either sigma is non-positive.
pub fn bilateral_filter3d(
    image: ArrayView3<f64>,
    sigma_spatial: f64,
    sigma_intensity: f64,
) -> NdimageResult<Array3<f64>> {
    if sigma_spatial <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "sigma_spatial must be positive".to_string(),
        ));
    }
    if sigma_intensity <= 0.0 {
        return Err(NdimageError::InvalidInput(
            "sigma_intensity must be positive".to_string(),
        ));
    }
    let (sz, sy, sx) = (image.shape()[0], image.shape()[1], image.shape()[2]);
    let owned = image.to_owned();
    let radius = (3.0 * sigma_spatial).ceil() as isize;
    let two_ss_sq = 2.0 * sigma_spatial * sigma_spatial;
    let two_si_sq = 2.0 * sigma_intensity * sigma_intensity;
    let mut out = Array3::zeros((sz, sy, sx));

    for iz in 0..sz {
        for iy in 0..sy {
            for ix in 0..sx {
                let center_val = owned[[iz, iy, ix]];
                let mut w_sum = 0.0f64;
                let mut v_sum = 0.0f64;

                for dz in -radius..=radius {
                    for dy in -radius..=radius {
                        for dx in -radius..=radius {
                            let nz = iz as isize + dz;
                            let ny = iy as isize + dy;
                            let nx = ix as isize + dx;
                            let v = get_clamped(&owned, nz, ny, nx);
                            let spatial_dist_sq =
                                (dz * dz + dy * dy + dx * dx) as f64;
                            let intensity_diff = v - center_val;
                            let w = (-spatial_dist_sq / two_ss_sq
                                - intensity_diff * intensity_diff / two_si_sq)
                                .exp();
                            w_sum += w;
                            v_sum += w * v;
                        }
                    }
                }

                out[[iz, iy, ix]] = if w_sum > 0.0 {
                    v_sum / w_sum
                } else {
                    center_val
                };
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array3;

    /// Create a uniform 5×5×5 volume filled with `value`.
    fn uniform_volume(value: f64) -> Array3<f64> {
        Array3::from_elem((5, 5, 5), value)
    }

    /// Create a ramp volume where `v[z,y,x] = (z + y + x) as f64`.
    fn ramp_volume() -> Array3<f64> {
        Array3::from_shape_fn((5, 5, 5), |(z, y, x)| (z + y + x) as f64)
    }

    /// Create a noisy step-edge volume: left half ≈ 0, right half ≈ 1.
    fn step_volume() -> Array3<f64> {
        Array3::from_shape_fn((8, 8, 8), |(_, _, x)| if x < 4 { 0.0 } else { 1.0 })
    }

    // ----- Gaussian filter -----

    #[test]
    fn test_gaussian_uniform_preserves_value() {
        let img = uniform_volume(3.0);
        let result = gaussian_filter3d(img.view(), 1.0, 4.0).expect("gaussian failed");
        for &v in result.iter() {
            assert!((v - 3.0).abs() < 1e-9, "expected 3.0, got {}", v);
        }
    }

    #[test]
    fn test_gaussian_sigma_zero_error() {
        let img = uniform_volume(1.0);
        assert!(gaussian_filter3d(img.view(), 0.0, 4.0).is_err());
    }

    #[test]
    fn test_gaussian_truncate_zero_error() {
        let img = uniform_volume(1.0);
        assert!(gaussian_filter3d(img.view(), 1.0, 0.0).is_err());
    }

    #[test]
    fn test_gaussian_smooths_step() {
        let img = step_volume();
        let smoothed = gaussian_filter3d(img.view(), 1.5, 4.0).expect("gaussian failed");
        // The sharp edge at x=4 should be blurred: pixels near edge have intermediate values
        let edge_val = smoothed[[4, 4, 4]]; // near the edge
        assert!(edge_val > 0.01 && edge_val < 0.99, "expected blurred edge, got {}", edge_val);
    }

    #[test]
    fn test_gaussian_output_shape() {
        let img = ramp_volume();
        let result = gaussian_filter3d(img.view(), 1.0, 3.0).expect("gaussian failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Median filter -----

    #[test]
    fn test_median_uniform_preserves_value() {
        let img = uniform_volume(7.0);
        let result = median_filter3d(img.view(), 3).expect("median failed");
        for &v in result.iter() {
            assert!((v - 7.0).abs() < 1e-9);
        }
    }

    #[test]
    fn test_median_removes_spike() {
        let mut img = uniform_volume(0.0);
        img[[2, 2, 2]] = 100.0; // impulse noise
        let result = median_filter3d(img.view(), 3).expect("median failed");
        // Center voxel should be suppressed
        assert!(result[[2, 2, 2]] < 10.0, "spike not removed: {}", result[[2, 2, 2]]);
    }

    #[test]
    fn test_median_even_size_error() {
        let img = uniform_volume(1.0);
        assert!(median_filter3d(img.view(), 2).is_err());
    }

    #[test]
    fn test_median_zero_size_error() {
        let img = uniform_volume(1.0);
        assert!(median_filter3d(img.view(), 0).is_err());
    }

    #[test]
    fn test_median_output_shape() {
        let img = ramp_volume();
        let result = median_filter3d(img.view(), 3).expect("median failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Uniform filter -----

    #[test]
    fn test_uniform_constant_image() {
        let img = uniform_volume(5.0);
        let result = uniform_filter3d(img.view(), 3).expect("uniform failed");
        for &v in result.iter() {
            assert!((v - 5.0).abs() < 1e-9);
        }
    }

    #[test]
    fn test_uniform_output_shape() {
        let img = ramp_volume();
        let result = uniform_filter3d(img.view(), 3).expect("uniform failed");
        assert_eq!(result.shape(), img.shape());
    }

    #[test]
    fn test_uniform_even_size_error() {
        let img = uniform_volume(1.0);
        assert!(uniform_filter3d(img.view(), 4).is_err());
    }

    // ----- Sobel gradient -----

    #[test]
    fn test_sobel_uniform_near_zero() {
        let img = uniform_volume(2.0);
        let result = sobel_gradient3d(img.view()).expect("sobel failed");
        for &v in result.iter() {
            assert!(v.abs() < 1e-9, "expected ~0, got {}", v);
        }
    }

    #[test]
    fn test_sobel_step_detects_edge() {
        let img = step_volume();
        let result = sobel_gradient3d(img.view()).expect("sobel failed");
        // Voxels at the edge (x around 3-4) should have high gradient
        let edge_mag = result[[4, 4, 3]];
        assert!(edge_mag > 0.01, "expected nonzero gradient at edge, got {}", edge_mag);
    }

    #[test]
    fn test_sobel_output_shape() {
        let img = ramp_volume();
        let result = sobel_gradient3d(img.view()).expect("sobel failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Laplacian -----

    #[test]
    fn test_laplace_uniform_zero() {
        let img = uniform_volume(3.0);
        let result = laplace3d(img.view()).expect("laplace failed");
        // Interior voxels should have zero Laplacian for constant image
        // (border voxels may differ due to clamping)
        for iz in 1..4usize {
            for iy in 1..4usize {
                for ix in 1..4usize {
                    assert!(
                        result[[iz, iy, ix]].abs() < 1e-9,
                        "expected 0, got {}",
                        result[[iz, iy, ix]]
                    );
                }
            }
        }
    }

    #[test]
    fn test_laplace_output_shape() {
        let img = ramp_volume();
        let result = laplace3d(img.view()).expect("laplace failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Non-local means -----

    #[test]
    fn test_nlm_uniform_preserves_value() {
        let img = uniform_volume(4.0);
        let result =
            non_local_means3d(img.view(), 3, 2, 0.1).expect("NLM failed");
        for &v in result.iter() {
            assert!((v - 4.0).abs() < 1e-6, "expected 4.0, got {}", v);
        }
    }

    #[test]
    fn test_nlm_invalid_patch_size() {
        let img = uniform_volume(1.0);
        assert!(non_local_means3d(img.view(), 2, 2, 0.1).is_err()); // even size
        assert!(non_local_means3d(img.view(), 0, 2, 0.1).is_err()); // zero size
    }

    #[test]
    fn test_nlm_output_shape() {
        let img = ramp_volume();
        let result = non_local_means3d(img.view(), 3, 1, 0.5).expect("NLM failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Anisotropic diffusion -----

    #[test]
    fn test_aniso_diff_uniform_preserves_value() {
        let img = uniform_volume(5.0);
        let result = anisotropic_diffusion3d(img.view(), 5, 10.0, 0.1)
            .expect("anisotropic diffusion failed");
        for &v in result.iter() {
            assert!(
                (v - 5.0).abs() < 1e-8,
                "expected 5.0, got {}",
                v
            );
        }
    }

    #[test]
    fn test_aniso_diff_zero_iterations_identity() {
        let img = ramp_volume();
        let result = anisotropic_diffusion3d(img.view(), 0, 10.0, 0.1)
            .expect("anisotropic diffusion failed");
        for ((z, y, x), &v) in result.indexed_iter() {
            assert!((v - img[[z, y, x]]).abs() < 1e-12);
        }
    }

    #[test]
    fn test_aniso_diff_invalid_kappa_error() {
        let img = uniform_volume(1.0);
        assert!(anisotropic_diffusion3d(img.view(), 5, 0.0, 0.1).is_err());
    }

    #[test]
    fn test_aniso_diff_output_shape() {
        let img = ramp_volume();
        let result = anisotropic_diffusion3d(img.view(), 3, 10.0, 0.1)
            .expect("anisotropic diffusion failed");
        assert_eq!(result.shape(), img.shape());
    }

    // ----- Bilateral filter -----

    #[test]
    fn test_bilateral_uniform_preserves_value() {
        let img = uniform_volume(6.0);
        let result = bilateral_filter3d(img.view(), 1.5, 1.0)
            .expect("bilateral filter failed");
        for &v in result.iter() {
            assert!((v - 6.0).abs() < 1e-8, "expected 6.0, got {}", v);
        }
    }

    #[test]
    fn test_bilateral_output_shape() {
        let img = ramp_volume();
        let result = bilateral_filter3d(img.view(), 1.5, 1.0)
            .expect("bilateral filter failed");
        assert_eq!(result.shape(), img.shape());
    }

    #[test]
    fn test_bilateral_invalid_sigma_error() {
        let img = uniform_volume(1.0);
        assert!(bilateral_filter3d(img.view(), 0.0, 1.0).is_err());
        assert!(bilateral_filter3d(img.view(), 1.0, 0.0).is_err());
    }

    #[test]
    fn test_bilateral_step_edge_preservation() {
        // Bilateral should preserve the step edge better than Gaussian
        let img = step_volume();
        let bilateral = bilateral_filter3d(img.view(), 1.5, 0.1)
            .expect("bilateral filter failed");
        let gaussian = gaussian_filter3d(img.view(), 1.5, 4.0)
            .expect("gaussian failed");
        // The far interior of the left half should stay close to 0 with bilateral
        let bi_left = bilateral[[4, 4, 0]]; // deep in the left (0) half
        let ga_left = gaussian[[4, 4, 0]];
        // Bilateral should be at least as close to 0 as Gaussian
        assert!(
            bi_left.abs() <= ga_left.abs() + 0.3,
            "bilateral({}) should preserve flat region better than gaussian({})",
            bi_left,
            ga_left
        );
    }
}