pub(crate) const DROPPED: &str = "droppedGeometry";
pub(crate) const UP: [f32; 3] = [0.0, 1.0, 0.0];
#[allow(dead_code, reason = "which readers unroll strips depends on the enabled features")]
pub(crate) fn distinct(t: [u32; 3]) -> bool {
t[0] != t[1] && t[1] != t[2] && t[0] != t[2]
}
pub(crate) fn finite(v: &[[f32; 3]]) -> bool {
let worst = v.as_flattened().iter().fold(0, |m, x| m.max(x.to_bits() & 0x7fff_ffff));
worst < 0x7f80_0000
}
pub(crate) fn sound(positions: &[[f32; 3]], finite: bool, list: &mut Vec<u32>, width: usize) -> usize {
let n = positions.len();
let width = width.max(1);
list.truncate(list.len() - list.len() % width);
let top = list.iter().fold(0, |m, &i| m.max(i));
if list.is_empty() || (finite && (top as usize) < n) {
return 0;
}
let ok = |i: u32| {
positions
.get(i as usize)
.is_some_and(|p| finite || p.iter().all(|c| c.is_finite()))
};
let before = list.len() / width;
let mut kept = 0;
for g in 0..before {
let at = g * width;
if list[at..at + width].iter().all(|&i| ok(i)) {
list.copy_within(at..at + width, kept);
kept += width;
}
}
list.truncate(kept);
before - kept / width
}
pub(crate) fn settle(positions: &mut [[f32; 3]]) {
for p in positions {
if !p.iter().all(|c| c.is_finite()) {
*p = [0.0; 3];
}
}
}
fn sums(positions: &[[f32; 3]], indices: &[u32]) -> Vec<[f32; 3]> {
let mut sum = vec![[0.0f32; 3]; positions.len()];
let extent = positions.as_flattened().iter().fold(0.0f32, |m, x| m.max(x.abs()));
let scale = if (1e-12..=1e12).contains(&extent) || extent == 0.0 || !extent.is_finite() {
1.0
} else {
1.0 / extent
};
for t in indices.as_chunks::<3>().0 {
let [i, j, k] = t.map(|i| i as usize);
let (Some(a), Some(b), Some(c)) = (positions.get(i), positions.get(j), positions.get(k)) else {
continue;
};
let u = [(b[0] - a[0]) * scale, (b[1] - a[1]) * scale, (b[2] - a[2]) * scale];
let v = [(c[0] - a[0]) * scale, (c[1] - a[1]) * scale, (c[2] - a[2]) * scale];
let f = [
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0],
];
if !(f[0].is_finite() && f[1].is_finite() && f[2].is_finite()) {
continue;
}
for v in [i, j, k] {
let s = &mut sum[v];
*s = [s[0] + f[0], s[1] + f[1], s[2] + f[2]];
}
}
sum
}
pub(crate) fn unit(normals: &mut [[f32; 3]], slack: f32) -> bool {
for v in normals {
let sq = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
if (sq - 1.0).abs() <= slack {
continue;
}
let l = sq.sqrt();
if !(l > 0.0 && l.is_finite()) {
return false;
}
*v = v.map(|c| c / l);
}
true
}
fn unit_or_up(v: [f32; 3]) -> [f32; 3] {
let [x, y, z] = v.map(f64::from);
let l = (x * x + y * y + z * z).sqrt();
if l > 0.0 && l.is_finite() {
#[allow(clippy::cast_possible_truncation, reason = "a unit vector's components fit")]
let out = [(x / l) as f32, (y / l) as f32, (z / l) as f32];
out
} else {
UP
}
}
pub(crate) fn smooth(positions: &[[f32; 3]], indices: &[u32]) -> Vec<[f32; 3]> {
let mut out = sums(positions, indices);
for n in &mut out {
*n = unit_or_up(*n);
}
out
}
pub(crate) fn crease(positions: &[[f32; 3]], indices: &mut [u32], degrees: f32) -> (Vec<[f32; 3]>, Vec<u32>) {
let n = positions.len();
let mut first: std::collections::HashMap<[u32; 3], u32> = std::collections::HashMap::new();
let at: Vec<u32> = positions
.iter()
.enumerate()
.map(|(i, p)| {
*first
.entry(p.map(|c| (c + 0.0).to_bits()))
.or_insert(crate::num::u32_of(i))
})
.collect();
drop(first);
let welded: Vec<u32> = indices
.iter()
.map(|&i| at.get(i as usize).copied().unwrap_or(i))
.collect();
let corner = crate::formats::crease::default_normals(positions, &welded, degrees);
drop((welded, at));
let mut normals = vec![[0.0f32; 3]; n];
let mut set = vec![false; n];
let mut added: Vec<u32> = Vec::new();
let mut more: std::collections::HashMap<(u32, [u32; 3]), u32> = std::collections::HashMap::new();
for (i, normal) in indices.iter_mut().zip(&corner) {
let v = *i as usize;
if v >= n || normal.iter().all(|c| *c == 0.0) {
continue;
}
let normal = normal.map(|c| c + 0.0);
if !set[v] {
set[v] = true;
normals[v] = normal;
} else if normals[v].map(f32::to_bits) != normal.map(f32::to_bits) {
let next = crate::num::u32_of(n + added.len());
let at = *more.entry((*i, normal.map(f32::to_bits))).or_insert(next);
if at == next {
added.push(*i);
normals.push(normal);
}
*i = at;
}
}
for (normal, set) in normals.iter_mut().zip(set) {
if !set {
*normal = UP;
}
}
(normals, added)
}
pub(crate) fn follow<T: Copy>(list: &mut Vec<T>, n: usize, added: &[u32]) {
if list.len() == n {
list.reserve_exact(added.len());
for &s in added {
list.push(list[s as usize]);
}
}
}
pub(crate) fn default_normals(p: &mut crate::mesh::Prim, degrees: f32) -> Vec<u32> {
let n = p.positions.len();
let (normals, added) = crease(&p.positions, &mut p.indices, degrees);
p.normals = normals;
p.tangents = Vec::new();
follow(&mut p.positions, n, &added);
follow(&mut p.uvs, n, &added);
follow(&mut p.colors, n, &added);
follow(&mut p.skin_joints, n, &added);
follow(&mut p.skin_weights, n, &added);
for set in &mut p.skin_joint_sets {
follow(set, n, &added);
}
for set in &mut p.skin_weight_sets {
follow(set, n, &added);
}
for set in &mut p.uv_sets {
follow(set, n, &added);
}
for set in &mut p.color_sets {
follow(set, n, &added);
}
added
}
pub(crate) fn normals(positions: &[[f32; 3]], indices: &[u32], normals: &mut [[f32; 3]], slack: f32) {
let mut bad = Vec::new();
for (i, v) in normals.iter_mut().enumerate() {
let sq = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
if (sq - 1.0).abs() <= slack {
continue;
}
let l = sq.sqrt();
if l > 0.0 && l.is_finite() {
*v = v.map(|c| c / l);
} else {
bad.push(i);
}
}
if bad.is_empty() {
return;
}
let sum = sums(positions, indices);
for i in bad {
normals[i] = unit_or_up(sum[i]);
}
}
#[cfg(test)]
#[allow(clippy::float_cmp, clippy::float_cmp_const, reason = "exact directions")]
mod tests {
use super::*;
#[test]
fn what_cannot_be_drawn_is_dropped() {
let mut pos = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [f32::NAN, 0.0, 0.0]];
let mut tris = vec![0, 1, 2, 0, 1, 3, 0, 1, 9, 0, 1, 1, 2];
assert!(!finite(&pos));
assert_eq!(sound(&pos, false, &mut tris, 3), 2);
assert_eq!(tris, [0, 1, 2, 0, 1, 1]);
let mut lines = vec![0, 1, 1, 3, 2, 4];
assert_eq!(sound(&pos, false, &mut lines, 2), 2);
assert_eq!(lines, [0, 1]);
let mut points = vec![3, 2, 7];
assert_eq!(sound(&pos, false, &mut points, 1), 2);
assert_eq!(points, [2]);
settle(&mut pos);
assert_eq!(pos[3], [0.0; 3]);
let mut fine = vec![0, 1, 2];
assert!(finite(&pos));
assert_eq!(sound(&pos, true, &mut fine, 3), 0);
assert_eq!(fine, [0, 1, 2]);
assert_eq!(sound(&[], true, &mut Vec::new(), 3), 0);
}
#[test]
fn the_default_normals_are_weighted_by_area() {
let pos = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[2.0, 0.0, 0.0],
[0.0, 0.0, -2.0],
[5.0, 5.0, 5.0],
];
let n = smooth(&pos, &[0, 1, 2, 0, 3, 4]);
let l = 17f32.sqrt();
assert!((n[0][2] - 1.0 / l).abs() < 1e-6 && (n[0][1] - 4.0 / l).abs() < 1e-6);
assert_eq!(n[1], [0.0, 0.0, 1.0]);
assert_eq!(n[3], [0.0, 1.0, 0.0]);
assert_eq!(n[5], UP);
let big = pos.map(|p| p.map(|c| c * 1e30));
assert_eq!(smooth(&big, &[0, 1, 2])[1], [0.0, 0.0, 1.0]);
let small = pos.map(|p| p.map(|c| c * 1e-30));
assert_eq!(smooth(&small, &[0, 1, 2])[1], [0.0, 0.0, 1.0]);
}
#[test]
fn a_files_normals_are_made_unit_and_broken_ones_replaced() {
let pos = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [9.0, 9.0, 9.0]];
let mut n = vec![[0.0, 0.0, 2.0], [0.0; 3], [f32::NAN, 0.0, 0.0], [0.0; 3]];
normals(&pos, &[0, 1, 2], &mut n, 1e-6);
assert_eq!(n, [[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], UP]);
}
#[test]
fn the_default_normals_split_a_vertex_at_a_hard_edge() {
let mut p = crate::mesh::Prim {
positions: vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[5.0, 5.0, 5.0],
],
uvs: (0..7u8).map(|k| [f32::from(k), 0.0]).collect(),
indices: vec![0, 1, 2, 0, 2, 3, 1, 4, 5, 1, 5, 2],
..crate::mesh::Prim::default()
};
let added = default_normals(&mut p, 30.0);
assert_eq!(added, [1, 2]);
assert_eq!(p.indices, [0, 1, 2, 0, 2, 3, 7, 4, 5, 7, 5, 8]);
assert_eq!((p.positions.len(), p.normals.len(), p.uvs.len()), (9, 9, 9));
assert_eq!((p.positions[7], p.uvs[8]), ([1.0, 0.0, 0.0], [2.0, 0.0]));
assert_eq!((p.normals[1], p.normals[7]), ([0.0, 0.0, 1.0], [-1.0, 0.0, 0.0]));
assert_eq!(p.normals[6], UP);
let (s, c) = 20f32.to_radians().sin_cos();
let mut seam = crate::mesh::Prim {
positions: vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, -c, s],
],
indices: vec![0, 1, 2, 4, 3, 5],
..crate::mesh::Prim::default()
};
assert_eq!(default_normals(&mut seam, 30.0), [0u32; 0]);
assert_eq!((seam.normals[0], seam.normals[1]), (seam.normals[3], seam.normals[4]));
assert!(seam.normals[0][2] < 1.0);
let mut flat = crate::mesh::Prim {
positions: p.positions[..4].to_vec(),
indices: vec![0, 1, 2, 0, 2, 3],
..crate::mesh::Prim::default()
};
assert_eq!(default_normals(&mut flat, 30.0), [0u32; 0]);
assert_eq!(flat.normals, [[0.0, 0.0, 1.0]; 4]);
}
}