Files
materialcraft/crates/nodes/src/function/trigonometry.rs
T

95 lines
4.3 KiB
Rust

//! Runtime sine and cosine for pixel and uniform function scopes.
use super::fixed_point::square_fraction;
use std::sync::LazyLock;
const QUARTER: u32 = 1 << 23;
struct Polynomial {
constant: i64,
slope: i64,
curvature: i64,
}
// Derive each quadratic from the sine function over one 64th of the quadrant.
static QUARTER_WAVE: LazyLock<[Polynomial; 64]> = LazyLock::new(|| std::array::from_fn(quarter_polynomial));
fn quarter_polynomial(index: usize) -> Polynomial {
let width = std::f64::consts::PI / 128.0;
let (sine, cosine) = ((index as f64 + 0.5) * width).sin_cos();
// The linear minimax approximation of the centered cubic on [-1/2, 1/2] is 3t/16.
let center_slope = 4_194_304.0 * (width * cosine - width.powi(3) * cosine / 32.0);
let slope = ((center_slope + 4_194_304.0 * width * width * sine * 0.5) / 4.0).round() as i64;
let curvature = center_slope.round() as i64 - 4 * slope;
let mut low = f64::INFINITY;
let mut high = f64::NEG_INFINITY;
for step in 0..=64 {
let t = f64::from(step) / 64.0;
let residual = 33_554_432.0 * ((index as f64 + t) * width).sin() - 32.0 * slope as f64 * t - 8.0 * curvature as f64 * t * t;
low = low.min(residual);
high = high.max(residual);
}
let midpoint = ((low + high) * 0.5).round() as i64;
let constant = match index {
0 => 0,
// These three measured intervals start one constant unit below the rounded midpoint.
2 | 18 | 46 => midpoint - 1,
_ => midpoint,
};
Polynomial { constant, slope, curvature }
}
// One turn spans 25 bits. Retaining the product word before the exponent shift
// reproduces the loss of phase resolution at large angles.
fn phase(bits: u32) -> u32 {
let exponent = ((bits >> 23) & 255) as i32 - 127;
let mantissa = (bits & 0x7f_ffff) | (1 << 23);
let word = ((u64::from(mantissa) * 10_680_707) >> 16) as u32;
let shift = exponent - 8;
let shifted = if shift >= 0 { word.checked_shl(shift as u32) } else { word.checked_shr((-shift) as u32) };
shifted.unwrap_or(0) & (4 * QUARTER - 1)
}
pub(super) fn component(angle: f32, cosine: bool, scope: super::FunctionScope, literal: bool) -> f32 {
if literal {
return if cosine { angle.cos() } else { angle.sin() };
}
match scope {
super::FunctionScope::PixelProcessor => pixel_component(angle, cosine),
super::FunctionScope::Node | super::FunctionScope::ValueProcessor | super::FunctionScope::FxMap => uniform_component(angle, cosine),
}
}
fn pixel_component(angle: f32, cosine: bool) -> f32 {
if !angle.is_finite() {
return f32::NAN;
}
let phase = phase(angle.to_bits()) + if cosine { QUARTER } else { 0 };
let half = phase & (2 * QUARTER - 1);
let quarter = if half < QUARTER { half } else { 2 * QUARTER - 1 - half };
let bin = &QUARTER_WAVE[(quarter >> 17) as usize];
let fraction = quarter & 0x1_ffff;
let fixed = (bin.constant << 12) + bin.slope * i64::from(fraction) + bin.curvature * i64::from(square_fraction(fraction >> 1));
// The quarter-wave magnitude truncates to the next representable float below the polynomial.
let exact = fixed as f64 / 137_438_953_472.0;
let nearest = exact as f32;
let magnitude = if f64::from(nearest) > exact { f32::from_bits(nearest.to_bits().saturating_sub(1)) } else { nearest };
if (phase & (2 * QUARTER) != 0) ^ (!cosine && angle.is_sign_negative()) { -magnitude } else { magnitude }
}
fn uniform_component(angle: f32, cosine: bool) -> f32 {
const QUADRANT: f64 = std::f64::consts::FRAC_PI_2;
const CUBIC: f64 = -QUADRANT * QUADRANT * QUADRANT / 6.0;
const FIFTH: f64 = -CUBIC * QUADRANT * QUADRANT / 20.0;
// The measured seventh-order term targets one minus a float epsilon at the quadrant endpoint.
const SEVENTH: f32 = (1.0 - f32::EPSILON as f64 - QUADRANT - CUBIC - FIFTH) as f32;
let turns = angle * (std::f32::consts::FRAC_1_PI * 0.5) + if cosine { 0.5 } else { 0.25 };
let x = 4.0 * (turns - turns.round_ties_even()).abs() - 1.0;
let square = x * x;
let cube = x * square;
let fifth = cube * square;
let seventh = fifth * square;
// Each odd power and each sum rounds separately.
x * std::f32::consts::FRAC_PI_2 + cube * CUBIC as f32 + fifth * FIFTH as f32 + seventh * SEVENTH
}