use thiserror::Error;
#[derive(Debug, Error)]
pub enum NormalError {
#[error("Invalid dimensions: {0}")]
InvalidDimensions(String),
#[error("Invalid depth: {0}")]
InvalidDepth(String),
#[error("Empty map: no pixels provided")]
EmptyMap,
}
#[inline]
fn is_valid_normal(n: &[f32; 3]) -> bool {
let len_sq = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
len_sq > 0.25 }
#[inline]
fn normalize3(v: [f32; 3]) -> Option<[f32; 3]> {
let len_sq = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
if len_sq < 1e-16 {
return None;
}
let inv = len_sq.sqrt().recip();
Some([v[0] * inv, v[1] * inv, v[2] * inv])
}
#[inline]
fn cross3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
#[inline]
fn dot3(a: [f32; 3], b: [f32; 3]) -> f32 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline]
fn sub3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
#[derive(Debug, Clone)]
pub struct NormalMap {
pub width: u32,
pub height: u32,
pub normals: Vec<f32>,
}
impl NormalMap {
pub fn new(width: u32, height: u32) -> Self {
let n = (width as usize) * (height as usize);
let mut normals = vec![0.0_f32; n * 3];
for i in 0..n {
normals[i * 3 + 2] = 1.0;
}
Self {
width,
height,
normals,
}
}
pub fn pixel(&self, x: u32, y: u32) -> [f32; 3] {
if x >= self.width || y >= self.height {
return [0.0, 0.0, 1.0];
}
let base = ((y as usize) * (self.width as usize) + x as usize) * 3;
[
self.normals[base],
self.normals[base + 1],
self.normals[base + 2],
]
}
pub fn set_pixel(&mut self, x: u32, y: u32, normal: [f32; 3]) {
if x >= self.width || y >= self.height {
return;
}
let base = ((y as usize) * (self.width as usize) + x as usize) * 3;
self.normals[base] = normal[0];
self.normals[base + 1] = normal[1];
self.normals[base + 2] = normal[2];
}
pub fn to_rgb_u8(&self) -> Vec<u8> {
self.normals
.iter()
.map(|&v| ((v * 0.5 + 0.5) * 255.0).clamp(0.0, 255.0) as u8)
.collect()
}
pub fn to_rgb_f32(&self) -> Vec<f32> {
self.normals.iter().map(|&v| v * 0.5 + 0.5).collect()
}
pub fn num_valid(&self) -> usize {
let n_pixels = (self.width as usize) * (self.height as usize);
(0..n_pixels)
.filter(|&i| {
let b = i * 3;
let px = [self.normals[b], self.normals[b + 1], self.normals[b + 2]];
is_valid_normal(&px)
})
.count()
}
pub fn mean_normal(&self) -> [f32; 3] {
let n_pixels = (self.width as usize) * (self.height as usize);
let mut sum = [0.0_f32; 3];
let mut count = 0usize;
for i in 0..n_pixels {
let b = i * 3;
let px = [self.normals[b], self.normals[b + 1], self.normals[b + 2]];
if is_valid_normal(&px) {
sum[0] += px[0];
sum[1] += px[1];
sum[2] += px[2];
count += 1;
}
}
if count == 0 {
return [0.0, 0.0, 1.0];
}
let inv = (count as f32).recip();
let mean_raw = [sum[0] * inv, sum[1] * inv, sum[2] * inv];
normalize3(mean_raw).unwrap_or([0.0, 0.0, 1.0])
}
pub fn angular_deviation(&self, reference: [f32; 3]) -> Vec<f32> {
self.normals
.chunks_exact(3)
.map(|chunk| {
let px = [chunk[0], chunk[1], chunk[2]];
if is_valid_normal(&px) {
dot3(px, reference).clamp(-1.0, 1.0).acos()
} else {
0.0
}
})
.collect()
}
pub fn mean_angular_deviation(&self, reference: [f32; 3]) -> f32 {
let mut sum = 0.0_f32;
let mut count = 0usize;
for chunk in self.normals.chunks_exact(3) {
let px = [chunk[0], chunk[1], chunk[2]];
if is_valid_normal(&px) {
sum += dot3(px, reference).clamp(-1.0, 1.0).acos();
count += 1;
}
}
if count == 0 {
0.0
} else {
sum / count as f32
}
}
}
#[inline]
fn unproject(px: u32, py: u32, d: f32, fx: f32, fy: f32, cx: f32, cy: f32) -> [f32; 3] {
[(px as f32 - cx) * d / fx, (py as f32 - cy) * d / fy, d]
}
#[derive(Clone, Copy)]
struct CameraIntrinsics {
fx: f32,
fy: f32,
cx: f32,
cy: f32,
}
#[inline]
fn neighbour_pos(
nx: i64,
ny: i64,
width: u32,
height: u32,
depth: &[f32],
cam: CameraIntrinsics,
) -> Option<[f32; 3]> {
if nx < 0 || ny < 0 || nx >= width as i64 || ny >= height as i64 {
return None;
}
let idx = (ny as usize) * (width as usize) + nx as usize;
let d = depth[idx];
if !d.is_finite() || d <= 0.0 {
return None;
}
Some(unproject(
nx as u32, ny as u32, d, cam.fx, cam.fy, cam.cx, cam.cy,
))
}
pub fn estimate_normals_cross_product(
depth: &[f32],
width: u32,
height: u32,
fx: f32,
fy: f32,
cx: f32,
cy: f32,
) -> Result<NormalMap, NormalError> {
if width == 0 || height == 0 {
return Err(NormalError::EmptyMap);
}
let expected = (width as usize) * (height as usize);
if depth.len() != expected {
return Err(NormalError::InvalidDimensions(format!(
"depth slice length {} does not match {}×{}={}",
depth.len(),
width,
height,
expected
)));
}
for (i, &d) in depth.iter().enumerate() {
if d.is_finite() && d < 0.0 {
return Err(NormalError::InvalidDepth(format!(
"negative depth {d} at index {i}"
)));
}
}
let cam = CameraIntrinsics { fx, fy, cx, cy };
let mut map = NormalMap {
width,
height,
normals: vec![0.0_f32; expected * 3],
};
for y in 0..height {
for x in 0..width {
let idx = (y as usize) * (width as usize) + x as usize;
let d = depth[idx];
if !d.is_finite() || d <= 0.0 {
continue;
}
let p_center = unproject(x, y, d, cam.fx, cam.fy, cam.cx, cam.cy);
let xi = x as i64;
let yi = y as i64;
let xp = neighbour_pos(xi + 1, yi, width, height, depth, cam);
let xm = neighbour_pos(xi - 1, yi, width, height, depth, cam);
let yp = neighbour_pos(xi, yi + 1, width, height, depth, cam);
let ym = neighbour_pos(xi, yi - 1, width, height, depth, cam);
let dx_vec = match (xp, xm) {
(Some(a), Some(b)) => sub3(a, b),
(Some(a), None) => sub3(a, p_center),
(None, Some(b)) => sub3(p_center, b),
(None, None) => {
continue;
}
};
let dy_vec = match (yp, ym) {
(Some(a), Some(b)) => sub3(a, b),
(Some(a), None) => sub3(a, p_center),
(None, Some(b)) => sub3(p_center, b),
(None, None) => {
continue;
}
};
let raw_normal = cross3(dx_vec, dy_vec);
let normal = match normalize3(raw_normal) {
Some(n) => n,
None => continue,
};
let oriented = if normal[2] < 0.0 {
[-normal[0], -normal[1], -normal[2]]
} else {
normal
};
map.set_pixel(x, y, oriented);
}
}
Ok(map)
}
#[inline]
fn sobel_sample(nx: i64, ny: i64, width: u32, height: u32, depth: &[f32]) -> Option<f32> {
if nx < 0 || ny < 0 || nx >= width as i64 || ny >= height as i64 {
return None;
}
let d = depth[(ny as usize) * (width as usize) + nx as usize];
if d.is_finite() {
Some(d)
} else {
None
}
}
pub fn estimate_normals_sobel(
depth: &[f32],
width: u32,
height: u32,
) -> Result<NormalMap, NormalError> {
if width == 0 || height == 0 {
return Err(NormalError::EmptyMap);
}
let expected = (width as usize) * (height as usize);
if depth.len() != expected {
return Err(NormalError::InvalidDimensions(format!(
"depth slice length {} does not match {}×{}={}",
depth.len(),
width,
height,
expected
)));
}
const GX: [[f32; 3]; 3] = [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]];
const GY: [[f32; 3]; 3] = [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]];
let mut map = NormalMap::new(width, height);
for y in 0..height {
for x in 0..width {
if x == 0 || y == 0 || x == width - 1 || y == height - 1 {
map.set_pixel(x, y, [0.0, 0.0, 0.0]);
continue;
}
let xi = x as i64;
let yi = y as i64;
let mut grad_x = 0.0_f32;
let mut grad_y = 0.0_f32;
for dr in -1i64..=1 {
for dc in -1i64..=1 {
if let Some(d) = sobel_sample(xi + dc, yi + dr, width, height, depth) {
let r = (dr + 1) as usize;
let c = (dc + 1) as usize;
grad_x += GX[r][c] * d;
grad_y += GY[r][c] * d;
}
}
}
let center_d = depth[(y as usize) * (width as usize) + x as usize];
if !center_d.is_finite() {
map.set_pixel(x, y, [0.0, 0.0, 0.0]);
continue;
}
let raw = [-grad_x, -grad_y, 1.0_f32];
let normal = normalize3(raw).unwrap_or([0.0, 0.0, 0.0]);
map.set_pixel(x, y, normal);
}
}
Ok(map)
}
fn gaussian_kernel_1d(sigma: f32) -> Vec<f32> {
if sigma <= 0.0 {
return vec![1.0_f32];
}
let radius = (3.0_f32 * sigma).ceil() as usize;
let size = 2 * radius + 1;
let mut kernel = Vec::with_capacity(size);
let two_sigma_sq = 2.0 * sigma * sigma;
let mut sum = 0.0_f32;
for i in 0..size {
let x = i as f32 - radius as f32;
let val = (-x * x / two_sigma_sq).exp();
kernel.push(val);
sum += val;
}
if sum > 0.0 {
for v in &mut kernel {
*v /= sum;
}
}
kernel
}
pub fn smooth_normals(normal_map: &NormalMap, sigma: f32) -> NormalMap {
if sigma <= 0.0 {
return normal_map.clone();
}
let w = normal_map.width as usize;
let h = normal_map.height as usize;
let n_pixels = w * h;
let kernel = gaussian_kernel_1d(sigma);
let radius = kernel.len() / 2;
let mut h_sum = vec![0.0_f32; n_pixels * 3];
let mut h_weight = vec![0.0_f32; n_pixels];
for y in 0..h {
for x in 0..w {
let mut acc = [0.0_f32; 3];
let mut wsum = 0.0_f32;
for (ki, &kw) in kernel.iter().enumerate() {
let sx = (x as i64 + ki as i64 - radius as i64).clamp(0, w as i64 - 1) as usize;
let b = (y * w + sx) * 3;
let n = [
normal_map.normals[b],
normal_map.normals[b + 1],
normal_map.normals[b + 2],
];
if is_valid_normal(&n) {
acc[0] += kw * n[0];
acc[1] += kw * n[1];
acc[2] += kw * n[2];
wsum += kw;
}
}
let out_b = (y * w + x) * 3;
h_sum[out_b] = acc[0];
h_sum[out_b + 1] = acc[1];
h_sum[out_b + 2] = acc[2];
h_weight[y * w + x] = wsum;
}
}
let mut out_normals = vec![0.0_f32; n_pixels * 3];
for y in 0..h {
for x in 0..w {
let mut acc = [0.0_f32; 3];
let mut wsum = 0.0_f32;
for (ki, &kw) in kernel.iter().enumerate() {
let sy = (y as i64 + ki as i64 - radius as i64).clamp(0, h as i64 - 1) as usize;
let b = (sy * w + x) * 3;
let src_w = h_weight[sy * w + x];
acc[0] += kw * h_sum[b];
acc[1] += kw * h_sum[b + 1];
acc[2] += kw * h_sum[b + 2];
wsum += kw * src_w;
}
let out_b = (y * w + x) * 3;
let n = if wsum > 1e-8 {
let inv = 1.0 / wsum;
normalize3([acc[0] * inv, acc[1] * inv, acc[2] * inv]).unwrap_or([0.0, 0.0, 0.0])
} else {
[0.0, 0.0, 0.0]
};
out_normals[out_b] = n[0];
out_normals[out_b + 1] = n[1];
out_normals[out_b + 2] = n[2];
}
}
NormalMap {
width: normal_map.width,
height: normal_map.height,
normals: out_normals,
}
}
pub fn normal_consistency_loss(map_a: &NormalMap, map_b: &NormalMap) -> Result<f32, NormalError> {
if map_a.width != map_b.width || map_a.height != map_b.height {
return Err(NormalError::InvalidDimensions(format!(
"map_a is {}×{} but map_b is {}×{}",
map_a.width, map_a.height, map_b.width, map_b.height
)));
}
let n_pixels = (map_a.width as usize) * (map_a.height as usize);
let mut sum = 0.0_f32;
let mut count = 0usize;
for i in 0..n_pixels {
let b = i * 3;
let na = [map_a.normals[b], map_a.normals[b + 1], map_a.normals[b + 2]];
let nb = [map_b.normals[b], map_b.normals[b + 1], map_b.normals[b + 2]];
if is_valid_normal(&na) && is_valid_normal(&nb) {
let d = dot3(na, nb).abs();
sum += 1.0 - d;
count += 1;
}
}
if count == 0 {
Ok(0.0)
} else {
Ok(sum / count as f32)
}
}
#[derive(Debug, Clone)]
pub struct NormalStats {
pub mean_normal: [f32; 3],
pub std_deviation: f32,
pub roughness: f32,
pub num_valid_pixels: usize,
pub coverage: f32,
}
pub fn compute_normal_stats(normal_map: &NormalMap) -> NormalStats {
let total_pixels = (normal_map.width as usize) * (normal_map.height as usize);
let mean_normal = normal_map.mean_normal();
let num_valid_pixels = normal_map.num_valid();
if num_valid_pixels == 0 || total_pixels == 0 {
return NormalStats {
mean_normal,
std_deviation: 0.0,
roughness: 0.0,
num_valid_pixels: 0,
coverage: 0.0,
};
}
let devs = normal_map.angular_deviation(mean_normal);
let valid_devs: Vec<f32> = normal_map
.normals
.chunks_exact(3)
.zip(devs.iter().copied())
.filter_map(|(chunk, dev)| {
let px = [chunk[0], chunk[1], chunk[2]];
if is_valid_normal(&px) {
Some(dev)
} else {
None
}
})
.collect();
let mean_dev = valid_devs.iter().copied().sum::<f32>() / valid_devs.len() as f32;
let variance = valid_devs
.iter()
.map(|&d| (d - mean_dev) * (d - mean_dev))
.sum::<f32>()
/ valid_devs.len() as f32;
let std_dev = variance.sqrt();
NormalStats {
mean_normal,
std_deviation: std_dev,
roughness: mean_dev.to_degrees(),
num_valid_pixels,
coverage: num_valid_pixels as f32 / total_pixels as f32,
}
}
pub fn estimate_curvature(normal_map: &NormalMap) -> Vec<f32> {
let w = normal_map.width as usize;
let h = normal_map.height as usize;
let n_pixels = w * h;
let mut curvature = vec![0.0_f32; n_pixels];
for y in 0..h {
for x in 0..w {
let idx = y * w + x;
let center = {
let b = idx * 3;
[
normal_map.normals[b],
normal_map.normals[b + 1],
normal_map.normals[b + 2],
]
};
if !is_valid_normal(¢er) {
continue;
}
let dnx_dx = if x + 1 < w && x >= 1 {
let right = {
let b = (y * w + x + 1) * 3;
normal_map.normals[b]
};
let left = {
let b = (y * w + x - 1) * 3;
normal_map.normals[b]
};
let nr = [
normal_map.normals[(y * w + x + 1) * 3],
normal_map.normals[(y * w + x + 1) * 3 + 1],
normal_map.normals[(y * w + x + 1) * 3 + 2],
];
let nl = [
normal_map.normals[(y * w + x - 1) * 3],
normal_map.normals[(y * w + x - 1) * 3 + 1],
normal_map.normals[(y * w + x - 1) * 3 + 2],
];
if is_valid_normal(&nr) && is_valid_normal(&nl) {
(right - left) * 0.5
} else {
0.0
}
} else {
0.0
};
let dny_dy = if y + 1 < h && y >= 1 {
let ny_component_down = {
let b = ((y + 1) * w + x) * 3 + 1;
normal_map.normals[b]
};
let ny_component_up = {
let b = ((y - 1) * w + x) * 3 + 1;
normal_map.normals[b]
};
let nd = [
normal_map.normals[((y + 1) * w + x) * 3],
normal_map.normals[((y + 1) * w + x) * 3 + 1],
normal_map.normals[((y + 1) * w + x) * 3 + 2],
];
let nu = [
normal_map.normals[((y - 1) * w + x) * 3],
normal_map.normals[((y - 1) * w + x) * 3 + 1],
normal_map.normals[((y - 1) * w + x) * 3 + 2],
];
if is_valid_normal(&nd) && is_valid_normal(&nu) {
(ny_component_down - ny_component_up) * 0.5
} else {
0.0
}
} else {
0.0
};
curvature[idx] = (dnx_dx + dny_dy) * 0.5;
}
}
curvature
}
#[cfg(test)]
mod tests {
use super::*;
const EPSILON: f32 = 1e-4;
fn flat_depth_map(w: u32, h: u32, d: f32) -> Vec<f32> {
vec![d; (w * h) as usize]
}
#[test]
fn test_normal_map_new_initialised_to_up() {
let map = NormalMap::new(4, 3);
assert_eq!(map.width, 4);
assert_eq!(map.height, 3);
for y in 0..3 {
for x in 0..4 {
let p = map.pixel(x, y);
assert!((p[0]).abs() < EPSILON, "nx should be 0");
assert!((p[1]).abs() < EPSILON, "ny should be 0");
assert!((p[2] - 1.0).abs() < EPSILON, "nz should be 1");
}
}
}
#[test]
fn test_normal_map_pixel_out_of_bounds() {
let map = NormalMap::new(3, 3);
let oob = map.pixel(10, 10);
assert_eq!(oob, [0.0, 0.0, 1.0]);
}
#[test]
fn test_to_rgb_u8_up_normal() {
let map = NormalMap::new(1, 1);
let rgb = map.to_rgb_u8();
assert_eq!(rgb.len(), 3);
assert!(
rgb[0] >= 127 && rgb[0] <= 128,
"r should be ~128, got {}",
rgb[0]
);
assert!(
rgb[1] >= 127 && rgb[1] <= 128,
"g should be ~128, got {}",
rgb[1]
);
assert_eq!(rgb[2], 255, "b should be 255");
}
#[test]
fn test_to_rgb_f32_up_normal() {
let map = NormalMap::new(1, 1);
let rgb = map.to_rgb_f32();
assert_eq!(rgb.len(), 3);
assert!((rgb[0] - 0.5).abs() < EPSILON);
assert!((rgb[1] - 0.5).abs() < EPSILON);
assert!((rgb[2] - 1.0).abs() < EPSILON);
}
#[test]
fn test_sobel_flat_depth() {
let depth = flat_depth_map(8, 8, 2.5);
let map = estimate_normals_sobel(&depth, 8, 8).unwrap();
for y in 1..7 {
for x in 1..7 {
let n = map.pixel(x, y);
assert!(
(n[0]).abs() < EPSILON,
"nx@({x},{y}) should be 0, got {}",
n[0]
);
assert!(
(n[1]).abs() < EPSILON,
"ny@({x},{y}) should be 0, got {}",
n[1]
);
assert!(
(n[2] - 1.0).abs() < EPSILON,
"nz@({x},{y}) should be 1, got {}",
n[2]
);
}
}
}
#[test]
fn test_sobel_empty_returns_err() {
let result = estimate_normals_sobel(&[], 0, 0);
assert!(matches!(result, Err(NormalError::EmptyMap)));
}
#[test]
fn test_sobel_slope_gives_nontrivial_normals() {
let w = 9u32;
let h = 9u32;
let depth: Vec<f32> = (0..h)
.flat_map(|_y| (0..w).map(|x| 1.0 + 0.1 * x as f32))
.collect();
let map = estimate_normals_sobel(&depth, w, h).unwrap();
let n = map.pixel(4, 4);
assert!(n[0].abs() > 0.01, "expected non-trivial nx, got {}", n[0]);
}
#[test]
fn test_cross_product_flat_depth() {
let depth = flat_depth_map(6, 6, 3.0);
let map = estimate_normals_cross_product(&depth, 6, 6, 200.0, 200.0, 3.0, 3.0).unwrap();
for y in 1..5 {
for x in 1..5 {
let n = map.pixel(x, y);
assert!((n[0]).abs() < EPSILON, "nx@({x},{y}) = {}", n[0]);
assert!((n[1]).abs() < EPSILON, "ny@({x},{y}) = {}", n[1]);
assert!((n[2] - 1.0).abs() < EPSILON, "nz@({x},{y}) = {}", n[2]);
}
}
}
#[test]
fn test_cross_product_empty_returns_err() {
let result = estimate_normals_cross_product(&[], 0, 0, 1.0, 1.0, 0.0, 0.0);
assert!(matches!(result, Err(NormalError::EmptyMap)));
}
#[test]
fn test_cross_product_mismatched_len_returns_err() {
let depth = vec![1.0_f32; 10]; let result = estimate_normals_cross_product(&depth, 4, 4, 200.0, 200.0, 2.0, 2.0);
assert!(matches!(result, Err(NormalError::InvalidDimensions(_))));
}
#[test]
fn test_num_valid_after_flat_estimation() {
let depth = flat_depth_map(5, 5, 1.0);
let map = estimate_normals_cross_product(&depth, 5, 5, 100.0, 100.0, 2.5, 2.5).unwrap();
assert!(map.num_valid() > 0);
}
#[test]
fn test_all_infinity_depth_no_valid_normals() {
let depth = vec![f32::INFINITY; 16];
let map = estimate_normals_cross_product(&depth, 4, 4, 100.0, 100.0, 2.0, 2.0).unwrap();
assert_eq!(map.num_valid(), 0);
for y in 0..4 {
for x in 0..4 {
let n = map.pixel(x, y);
assert_eq!(n, [0.0, 0.0, 0.0]);
}
}
}
#[test]
fn test_smooth_flat_map_unchanged() {
let map = NormalMap::new(8, 8);
let smoothed = smooth_normals(&map, 1.0);
for y in 0..8 {
for x in 0..8 {
let n = smoothed.pixel(x, y);
assert!(
(n[2] - 1.0).abs() < EPSILON,
"nz@({x},{y}) should be 1, got {}",
n[2]
);
}
}
}
#[test]
fn test_smooth_reduces_angular_deviation() {
let w = 16u32;
let h = 16u32;
let mut map = NormalMap::new(w, h);
for y in 0..h {
for x in 0..w {
let n = if (x + y) % 2 == 0 {
normalize3([0.3, 0.0, 1.0]).unwrap()
} else {
normalize3([-0.3, 0.0, 1.0]).unwrap()
};
map.set_pixel(x, y, n);
}
}
let ref_normal = [0.0_f32, 0.0, 1.0];
let mad_before = map.mean_angular_deviation(ref_normal);
let smoothed = smooth_normals(&map, 2.0);
let mad_after = smoothed.mean_angular_deviation(ref_normal);
assert!(
mad_after < mad_before,
"smoothing should reduce MAD: before={mad_before}, after={mad_after}"
);
}
#[test]
fn test_consistency_loss_self() {
let depth = flat_depth_map(5, 5, 2.0);
let map = estimate_normals_cross_product(&depth, 5, 5, 100.0, 100.0, 2.5, 2.5).unwrap();
let loss = normal_consistency_loss(&map, &map).unwrap();
assert!(
loss.abs() < EPSILON,
"self-consistency loss should be 0, got {loss}"
);
}
#[test]
fn test_consistency_loss_dimension_mismatch() {
let map_a = NormalMap::new(4, 4);
let map_b = NormalMap::new(5, 5);
let result = normal_consistency_loss(&map_a, &map_b);
assert!(matches!(result, Err(NormalError::InvalidDimensions(_))));
}
#[test]
fn test_consistency_loss_one_all_zero() {
let depth_inf = vec![f32::INFINITY; 16];
let depth_valid = flat_depth_map(4, 4, 1.0);
let map_zero =
estimate_normals_cross_product(&depth_inf, 4, 4, 100.0, 100.0, 2.0, 2.0).unwrap();
let map_valid =
estimate_normals_cross_product(&depth_valid, 4, 4, 100.0, 100.0, 2.0, 2.0).unwrap();
let loss = normal_consistency_loss(&map_zero, &map_valid).unwrap();
assert!(
loss.abs() < EPSILON,
"loss with all-invalid map should be 0, got {loss}"
);
}
#[test]
fn test_mean_normal_uniform_map() {
let map = NormalMap::new(4, 4);
let mean = map.mean_normal();
assert!((mean[0]).abs() < EPSILON);
assert!((mean[1]).abs() < EPSILON);
assert!((mean[2] - 1.0).abs() < EPSILON);
}
#[test]
fn test_angular_deviation_zero() {
let map = NormalMap::new(3, 3);
let devs = map.angular_deviation([0.0, 0.0, 1.0]);
for &d in &devs {
assert!(d.abs() < EPSILON, "expected 0.0, got {d}");
}
}
#[test]
fn test_angular_deviation_opposite() {
let map = NormalMap::new(2, 2);
let devs = map.angular_deviation([0.0, 0.0, -1.0]);
for &d in &devs {
assert!(
(d - std::f32::consts::PI).abs() < EPSILON,
"expected π, got {d}"
);
}
}
#[test]
fn test_normal_stats_flat_map() {
let map = NormalMap::new(8, 8);
let stats = compute_normal_stats(&map);
assert!(
stats.roughness < 0.01,
"roughness should be ~0 for flat map, got {}",
stats.roughness
);
assert_eq!(stats.num_valid_pixels, 64);
assert!((stats.coverage - 1.0).abs() < EPSILON);
}
#[test]
fn test_curvature_flat_surface() {
let map = NormalMap::new(8, 8);
let curv = estimate_curvature(&map);
for &c in &curv {
assert!(
c.abs() < EPSILON,
"curvature should be ~0 for flat surface, got {c}"
);
}
}
#[test]
fn test_curvature_output_size() {
let w = 7u32;
let h = 5u32;
let map = NormalMap::new(w, h);
let curv = estimate_curvature(&map);
assert_eq!(curv.len(), (w * h) as usize);
}
#[test]
fn test_set_pixel_out_of_bounds_ignored() {
let mut map = NormalMap::new(4, 4);
map.set_pixel(100, 100, [1.0, 0.0, 0.0]);
for y in 0..4 {
for x in 0..4 {
let n = map.pixel(x, y);
assert!((n[2] - 1.0).abs() < EPSILON);
}
}
}
#[test]
fn test_cross_product_negative_depth_err() {
let depth = vec![-1.0_f32; 4];
let result = estimate_normals_cross_product(&depth, 2, 2, 100.0, 100.0, 1.0, 1.0);
assert!(matches!(result, Err(NormalError::InvalidDepth(_))));
}
#[test]
fn test_smooth_zero_sigma_returns_clone() {
let depth = flat_depth_map(5, 5, 1.5);
let map = estimate_normals_sobel(&depth, 5, 5).unwrap();
let smoothed = smooth_normals(&map, 0.0);
for y in 0..5 {
for x in 0..5 {
let orig = map.pixel(x, y);
let sm = smoothed.pixel(x, y);
assert_eq!(orig, sm);
}
}
}
#[test]
fn test_sobel_border_pixels_are_invalid_sentinel_and_excluded() {
let depth = flat_depth_map(8, 8, 2.5);
let map = estimate_normals_sobel(&depth, 8, 8).unwrap();
for x in 0..8u32 {
assert_eq!(map.pixel(x, 0), [0.0, 0.0, 0.0], "top border ({x},0)");
assert_eq!(map.pixel(x, 7), [0.0, 0.0, 0.0], "bottom border ({x},7)");
}
for y in 0..8u32 {
assert_eq!(map.pixel(0, y), [0.0, 0.0, 0.0], "left border (0,{y})");
assert_eq!(map.pixel(7, y), [0.0, 0.0, 0.0], "right border (7,{y})");
}
assert_eq!(map.num_valid(), 36);
}
#[test]
fn test_cross_product_isolated_pixel_with_no_neighbours_stays_invalid() {
let mut depth = vec![f32::INFINITY; 9];
depth[4] = 2.0; let map = estimate_normals_cross_product(&depth, 3, 3, 100.0, 100.0, 1.5, 1.5).unwrap();
assert_eq!(map.pixel(1, 1), [0.0, 0.0, 0.0]);
assert_eq!(map.num_valid(), 0);
}
#[test]
fn test_smooth_normals_excludes_invalid_neighbours() {
let w = 6u32;
let h = 1u32;
let mut map = NormalMap {
width: w,
height: h,
normals: vec![0.0_f32; (w * h * 3) as usize],
};
for x in 0..3u32 {
map.set_pixel(x, 0, [0.0, 0.0, 1.0]);
}
let smoothed = smooth_normals(&map, 2.0);
let n0 = smoothed.pixel(0, 0);
let len0 = (n0[0] * n0[0] + n0[1] * n0[1] + n0[2] * n0[2]).sqrt();
assert!(
(len0 - 1.0).abs() < 1e-3,
"expected near-unit length after excluding invalid neighbours, got {len0}, n={n0:?}"
);
assert!(
(n0[2] - 1.0).abs() < 1e-3,
"expected the valid direction [0,0,1] to be preserved, got {n0:?}"
);
}
#[test]
fn test_smooth_normals_all_invalid_window_stays_invalid() {
let map = NormalMap {
width: 4,
height: 1,
normals: vec![0.0_f32; 4 * 3], };
let smoothed = smooth_normals(&map, 1.0);
for x in 0..4u32 {
assert_eq!(smoothed.pixel(x, 0), [0.0, 0.0, 0.0]);
}
}
}