use crate::meshset;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Frame {
pub normal: [f32; 3],
pub tangent: [f32; 3],
pub sign: f32,
}
const AXES: [(usize, usize); 6] = [(2, 1), (2, 1), (0, 2), (0, 2), (0, 1), (0, 1)];
const REFERENCE: [(usize, f64); 6] = [(2, 1.0), (2, -1.0), (0, 1.0), (0, -1.0), (0, -1.0), (0, 1.0)];
const SCALE: f64 = 255.0 * std::f64::consts::SQRT_2;
const STEPS: u32 = 255;
const _: () = assert!(meshset::usage::TANGENT_SPACE == 52);
#[allow(
clippy::indexing_slicing,
reason = "indices are literals or entries of the const tables, below 3"
)]
fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[allow(clippy::indexing_slicing, reason = "indices are literals below 3")]
fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 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],
]
}
fn unit(a: [f64; 3]) -> Option<[f64; 3]> {
let l = dot(a, a).sqrt();
(l > 1e-12 && l.is_finite()).then(|| a.map(|c| c / l))
}
#[allow(
clippy::indexing_slicing,
reason = "`code` is at most 5 (decode clamps it, encode builds it) and the axes are below 3"
)]
fn normal(code: usize, qi: u32, qj: u32) -> [f64; 3] {
let (i, j) = AXES[code];
let mut n = [0.0; 3];
n[i] = (f64::from(qi) - 256.0) / SCALE;
n[j] = (f64::from(qj) - 256.0) / SCALE;
let rest = (1.0 - n[i] * n[i] - n[j] * n[j]).max(0.0).sqrt();
n[code >> 1] = if code & 1 == 1 { -rest } else { rest };
n
}
#[allow(
clippy::indexing_slicing,
reason = "`code` is at most 5 and the axes are below 3"
)]
fn base(code: usize, n: [f64; 3]) -> [f64; 3] {
let (axis, dir) = REFERENCE[code];
let mut r = [0.0; 3];
r[axis] = dir;
let d = dot(n, r);
let t = [r[0] - n[0] * d, r[1] - n[1] * d, r[2] - n[2] * d];
unit(t).unwrap_or(t)
}
#[allow(clippy::indexing_slicing, reason = "the axes of a [f64; 3] are 0..3")]
#[allow(clippy::cast_possible_truncation, reason = "f64 to the stored f32 precision")]
pub fn decode(b: [u8; 4]) -> Frame {
let [b0, b1, b2, b3] = b;
let code = usize::from(b3 & 7).min(5);
let qi = u32::from(b3 >> 4 & 1) << 8 | u32::from(b0);
let qj = u32::from(b3 >> 5 & 1) << 8 | u32::from(b1);
let n = normal(code, qi, qj);
let t0 = base(code, n);
let degrees = f64::from(b2) * 90.0 / f64::from(STEPS) + f64::from(b3 >> 6) * 90.0;
let (s, c) = degrees.to_radians().sin_cos();
let nt = cross(n, t0);
let t = [0, 1, 2].map(|k| nt[k] * s + t0[k] * c);
Frame {
normal: n.map(|v| v as f32),
tangent: t.map(|v| v as f32),
sign: if b3 & 8 != 0 { -1.0 } else { 1.0 },
}
}
#[allow(clippy::indexing_slicing, reason = "axes are 0..3 and `code` is at most 5")]
#[allow(
clippy::cast_possible_truncation,
reason = "quantisation: values are rounded and clamped into range first"
)]
#[allow(
clippy::arithmetic_side_effects,
reason = "`2 * axis` with axis below 3 and the angle steps below 4 * 255"
)]
pub fn encode(normal_: [f32; 3], tangent: [f32; 3], sign: f32) -> Option<[u8; 4]> {
let n = unit(normal_.map(f64::from))?;
let axis = (0..3).fold(0, |best, k| if n[k].abs() > n[best].abs() { k } else { best });
let code = 2 * axis + usize::from(n[axis] < 0.0);
let (i, j) = AXES[code];
let q = |v: f64| (v * SCALE + 256.0).round().clamp(0.0, 511.0) as u32;
let (qi, qj) = (q(n[i]), q(n[j]));
let nq = normal(code, qi, qj);
let t0 = base(code, nq);
let t = tangent.map(f64::from);
let along = dot(t, nq);
let tp = [t[0] - nq[0] * along, t[1] - nq[1] * along, t[2] - nq[2] * along];
let degrees = match unit(tp) {
Some(tp) => dot(tp, cross(nq, t0))
.atan2(dot(tp, t0))
.to_degrees()
.rem_euclid(360.0),
None => 0.0,
};
let steps = (degrees * f64::from(STEPS) / 90.0).round() as u32 % (4 * STEPS);
let (quadrant, step) = (steps / STEPS, steps % STEPS);
Some([
(qi & 0xff) as u8,
(qj & 0xff) as u8,
step as u8,
code as u8
| u8::from(sign < 0.0) << 3
| ((qi >> 8) as u8) << 4
| ((qj >> 8) as u8) << 5
| (quadrant as u8) << 6,
])
}