use super::*;
impl Vec3 {
#[must_use]
pub const fn zero() -> Self {
Self {
x: 0.0,
y: 0.0,
z: 0.0,
}
}
#[must_use]
pub const fn splat(item_val: f32) -> Self {
Self {
x: item_val,
y: item_val,
z: item_val,
}
}
#[inline(always)]
#[must_use]
pub fn scale(self, source_val: f32) -> Self {
Self {
x: self.get_x() * source_val,
y: self.get_y() * source_val,
z: self.get_z() * source_val,
}
}
#[inline(always)]
#[must_use]
pub fn dot(self, rhs: Self) -> f32 {
self.get_x().mul_add(
rhs.get_x(),
self.get_y()
.mul_add(rhs.get_y(), self.get_z() * rhs.get_z()),
)
}
#[inline(always)]
#[must_use]
pub fn cross(self, rhs: Self) -> Self {
Self {
x: self.get_y() * rhs.get_z() - self.get_z() * rhs.get_y(),
y: self.get_z() * rhs.get_x() - self.get_x() * rhs.get_z(),
z: self.get_x() * rhs.get_y() - self.get_y() * rhs.get_x(),
}
}
#[inline(always)]
#[must_use]
pub fn length_sq(self) -> f32 {
self.dot(self)
}
#[inline(always)]
#[must_use]
pub fn length(self) -> f32 {
self.length_sq().sqrt()
}
#[inline(always)]
#[must_use]
pub fn normalize(self) -> Self {
let len: f32 = self.length();
if len > 1e-6 {
self.scale(1.0 / len)
} else {
Self::new(0.0, 1.0, 0.0)
}
}
#[inline(always)]
#[must_use]
pub fn lerp(self, other: Self, blend_factor: f32) -> Self {
Self {
x: (other.get_x() - self.get_x()).mul_add(blend_factor, self.get_x()),
y: (other.get_y() - self.get_y()).mul_add(blend_factor, self.get_y()),
z: (other.get_z() - self.get_z()).mul_add(blend_factor, self.get_z()),
}
}
#[inline(always)]
#[must_use]
pub fn reflect(self, normal: Self) -> Self {
self.sub(normal.scale(2.0 * self.dot(normal)))
}
#[must_use]
pub fn min(self, rhs: Self) -> Self {
Self {
x: self.get_x().min(rhs.get_x()),
y: self.get_y().min(rhs.get_y()),
z: self.get_z().min(rhs.get_z()),
}
}
#[must_use]
pub fn max(self, rhs: Self) -> Self {
Self {
x: self.get_x().max(rhs.get_x()),
y: self.get_y().max(rhs.get_y()),
z: self.get_z().max(rhs.get_z()),
}
}
#[must_use]
pub fn from_color(color_value: Color) -> Self {
Self {
x: (color_value.get_red() as f32) / 255.0,
y: (color_value.get_green() as f32) / 255.0,
z: (color_value.get_blue() as f32) / 255.0,
}
}
#[inline(always)]
#[must_use]
fn aces_channel(item_val: f32) -> u8 {
let pos_x: f32 = item_val.max(0.0);
let num: f32 = pos_x * pos_x.mul_add(2.51, 0.03);
let den: f32 = pos_x.mul_add(pos_x.mul_add(2.43, 0.59), 0.14);
let mapped: f32 = num / den;
let idx: usize = mapped.clamp(0.0, 1.0).mul_add(1024.0, 0.5) as usize;
LINEAR_TO_SRGB_LUT[idx.min(1024)]
}
#[inline(always)]
#[must_use]
pub fn to_aces_rgba8(self) -> [u8; 4] {
[
Self::aces_channel(self.get_x()),
Self::aces_channel(self.get_y()),
Self::aces_channel(self.get_z()),
255,
]
}
#[inline(always)]
#[must_use]
fn aces_indices_4x(vals: [f32; 4]) -> [u32; 4] {
#[cfg(target_arch = "aarch64")]
{
let mut out: [u32; 4] = [0; 4];
unsafe {
let zero: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(0.0);
let one: core::arch::aarch64::float32x4_t = core::arch::aarch64::vdupq_n_f32(1.0);
let pos_x: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmaxq_f32(
core::arch::aarch64::vld1q_f32(vals.as_ptr()),
zero,
);
let num: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmulq_f32(
pos_x,
core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vdupq_n_f32(0.03),
pos_x,
core::arch::aarch64::vdupq_n_f32(2.51),
),
);
let den_inner: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vdupq_n_f32(0.59),
pos_x,
core::arch::aarch64::vdupq_n_f32(2.43),
);
let den: core::arch::aarch64::float32x4_t = core::arch::aarch64::vfmaq_f32(
core::arch::aarch64::vdupq_n_f32(0.14),
pos_x,
den_inner,
);
let den_est: core::arch::aarch64::float32x4_t =
core::arch::aarch64::vrecpeq_f32(den);
let inv_den: core::arch::aarch64::float32x4_t = core::arch::aarch64::vmulq_f32(
den_est,
core::arch::aarch64::vrecpsq_f32(den, den_est),
);
let mapped: core::arch::aarch64::float32x4_t = core::arch::aarch64::vminq_f32(
core::arch::aarch64::vmaxq_f32(
core::arch::aarch64::vmulq_f32(num, inv_den),
zero,
),
one,
);
let scaled: core::arch::aarch64::float32x4_t = core::arch::aarch64::vaddq_f32(
core::arch::aarch64::vmulq_f32(
mapped,
core::arch::aarch64::vdupq_n_f32(1024.0),
),
core::arch::aarch64::vdupq_n_f32(0.5),
);
let idx_u32: core::arch::aarch64::uint32x4_t =
core::arch::aarch64::vcvtq_u32_f32(scaled);
core::arch::aarch64::vst1q_u32(out.as_mut_ptr(), idx_u32);
}
[
out[0].min(1024),
out[1].min(1024),
out[2].min(1024),
out[3].min(1024),
]
}
#[cfg(target_arch = "x86_64")]
{
let mut out: [i32; 4] = [0; 4];
unsafe {
let zero: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(0.0);
let one: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_set1_ps(1.0);
let coord_x: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_max_ps(
core::arch::x86_64::_mm_loadu_ps(vals.as_ptr()),
zero,
);
let num: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_mul_ps(
coord_x,
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
coord_x,
core::arch::x86_64::_mm_set1_ps(2.51),
),
core::arch::x86_64::_mm_set1_ps(0.03),
),
);
let den: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
coord_x,
core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(
coord_x,
core::arch::x86_64::_mm_set1_ps(2.43),
),
core::arch::x86_64::_mm_set1_ps(0.59),
),
),
core::arch::x86_64::_mm_set1_ps(0.14),
);
let mapped: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_min_ps(
core::arch::x86_64::_mm_max_ps(core::arch::x86_64::_mm_div_ps(num, den), zero),
one,
);
let scaled: core::arch::x86_64::__m128 = core::arch::x86_64::_mm_add_ps(
core::arch::x86_64::_mm_mul_ps(mapped, core::arch::x86_64::_mm_set1_ps(1024.0)),
core::arch::x86_64::_mm_set1_ps(0.5),
);
core::arch::x86_64::_mm_storeu_si128(
out.as_mut_ptr().cast::<core::arch::x86_64::__m128i>(),
core::arch::x86_64::_mm_cvttps_epi32(scaled),
);
}
[
(out[0].clamp(0, 1024)) as u32,
(out[1].clamp(0, 1024)) as u32,
(out[2].clamp(0, 1024)) as u32,
(out[3].clamp(0, 1024)) as u32,
]
}
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
{
let mut out: [u32; 4] = [0; 4];
unsafe {
let zero: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(0.0);
let one: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_splat(1.0);
let sample_x: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmax(
core::arch::wasm32::v128_load(vals.as_ptr().cast()),
zero,
);
let num: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_mul(
sample_x,
core::arch::wasm32::f32x4_add(
core::arch::wasm32::f32x4_mul(
sample_x,
core::arch::wasm32::f32x4_splat(2.51),
),
core::arch::wasm32::f32x4_splat(0.03),
),
);
let den: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
core::arch::wasm32::f32x4_mul(
sample_x,
core::arch::wasm32::f32x4_add(
core::arch::wasm32::f32x4_mul(
sample_x,
core::arch::wasm32::f32x4_splat(2.43),
),
core::arch::wasm32::f32x4_splat(0.59),
),
),
core::arch::wasm32::f32x4_splat(0.14),
);
let mapped: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_pmin(
core::arch::wasm32::f32x4_pmax(core::arch::wasm32::f32x4_div(num, den), zero),
one,
);
let scaled: core::arch::wasm32::v128 = core::arch::wasm32::f32x4_add(
core::arch::wasm32::f32x4_mul(mapped, core::arch::wasm32::f32x4_splat(1024.0)),
core::arch::wasm32::f32x4_splat(0.5),
);
core::arch::wasm32::v128_store(
out.as_mut_ptr().cast(),
core::arch::wasm32::u32x4_trunc_sat_f32x4(scaled),
);
}
[
out[0].min(1024),
out[1].min(1024),
out[2].min(1024),
out[3].min(1024),
]
}
#[cfg(not(target_arch = "aarch64"))]
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
{
let mut out: [u32; 4] = [0; 4];
for item_index in 0..4 {
let value_x: f32 = vals[item_index].max(0.0);
let num: f32 = value_x * value_x.mul_add(2.51, 0.03);
let den: f32 = value_x.mul_add(value_x.mul_add(2.43, 0.59), 0.14);
let idx: u32 = ((num / den).clamp(0.0, 1.0).mul_add(1024.0, 0.5)) as u32;
out[item_index] = idx.min(1024);
}
out
}
}
#[inline(always)]
#[must_use]
pub fn aces_rgba8_luma_4x(
red_val: [f32; 4],
green_val: [f32; 4],
blue_val: [f32; 4],
) -> ([[u8; 4]; 4], [u8; 4]) {
let ir: [u32; 4] = Self::aces_indices_4x(red_val);
let ig: [u32; 4] = Self::aces_indices_4x(green_val);
let ib: [u32; 4] = Self::aces_indices_4x(blue_val);
let mut rgba: [[u8; 4]; 4] = [[0, 0, 0, 255]; 4];
let mut luma: [u8; 4] = [0; 4];
for item_index in 0..4 {
let rv: u8 = LINEAR_TO_SRGB_LUT[ir[item_index] as usize];
let gv: u8 = LINEAR_TO_SRGB_LUT[ig[item_index] as usize];
let bv: u8 = LINEAR_TO_SRGB_LUT[ib[item_index] as usize];
rgba[item_index] = [rv, gv, bv, 255];
luma[item_index] =
(((rv as u32) * 77 + (gv as u32) * 150 + (bv as u32) * 29) >> 8) as u8;
}
(rgba, luma)
}
}
impl Add for Vec3 {
type Output = Self;
#[inline(always)]
fn add(self, rhs: Self) -> Self {
Self {
x: self.get_x() + rhs.get_x(),
y: self.get_y() + rhs.get_y(),
z: self.get_z() + rhs.get_z(),
}
}
}
impl Sub for Vec3 {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: Self) -> Self {
Self {
x: self.get_x() - rhs.get_x(),
y: self.get_y() - rhs.get_y(),
z: self.get_z() - rhs.get_z(),
}
}
}
impl Mul for Vec3 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: Self) -> Self {
Self {
x: self.get_x() * rhs.get_x(),
y: self.get_y() * rhs.get_y(),
z: self.get_z() * rhs.get_z(),
}
}
}
impl Mul for Mat4 {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
let alpha_val: [f32; 16] = self.get_elements();
let blue_val: [f32; 16] = rhs.get_elements();
let mut out: [f32; 16] = [0.0; 16];
for col in 0..4 {
for row in 0..4 {
out[col * 4 + row] = alpha_val[row] * blue_val[col * 4]
+ alpha_val[4 + row] * blue_val[col * 4 + 1]
+ alpha_val[8 + row] * blue_val[col * 4 + 2]
+ alpha_val[12 + row] * blue_val[col * 4 + 3];
}
}
Self { elements: out }
}
}
impl Mat4 {
#[must_use]
pub const fn identity() -> Self {
Self {
elements: [
1.0, 0.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, 0.0, 1.0,
],
}
}
#[must_use]
pub fn perspective(fov_rad: f32, aspect: f32, near: f32, far: f32) -> Self {
let factor_val: f32 = 1.0 / (fov_rad * 0.5).tan().max(1e-4);
let nf: f32 = 1.0 / (near - far);
Self {
elements: [
factor_val / aspect.max(0.01),
0.0,
0.0,
0.0,
0.0,
factor_val,
0.0,
0.0,
0.0,
0.0,
(far + near) * nf,
-1.0,
0.0,
0.0,
2.0 * far * near * nf,
0.0,
],
}
}
#[must_use]
pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Self {
let vec_value: Vec3 = target.sub(eye).normalize();
let direction_vec: Vec3 = vec_value.cross(up).normalize();
let position_vec: Vec3 = direction_vec.cross(vec_value);
Self {
elements: [
direction_vec.get_x(),
position_vec.get_x(),
-vec_value.get_x(),
0.0,
direction_vec.get_y(),
position_vec.get_y(),
-vec_value.get_y(),
0.0,
direction_vec.get_z(),
position_vec.get_z(),
-vec_value.get_z(),
0.0,
-direction_vec.dot(eye),
-position_vec.dot(eye),
vec_value.dot(eye),
1.0,
],
}
}
#[must_use]
pub fn from_trs(position: Vec3, yaw_rad: f32, scale: Vec3) -> Self {
let char_item: f32 = yaw_rad.cos();
let source_val: f32 = yaw_rad.sin();
Self {
elements: [
char_item * scale.get_x(),
0.0,
-source_val * scale.get_x(),
0.0,
0.0,
scale.get_y(),
0.0,
0.0,
source_val * scale.get_z(),
0.0,
char_item * scale.get_z(),
0.0,
position.get_x(),
position.get_y(),
position.get_z(),
1.0,
],
}
}
#[must_use]
pub fn from_trs_euler(
position: Vec3,
yaw_rad: f32,
pitch_rad: f32,
roll_rad: f32,
scale: Vec3,
) -> Self {
if pitch_rad.abs() < 1e-6 && roll_rad.abs() < 1e-6 {
return Self::from_trs(position, yaw_rad, scale);
}
let (cy, sy): (f32, f32) = (yaw_rad.cos(), yaw_rad.sin());
let (cp, sp): (f32, f32) = (pitch_rad.cos(), pitch_rad.sin());
let (cr, sr): (f32, f32) = (roll_rad.cos(), roll_rad.sin());
let r00: f32 = cy * cr + sy * sp * sr;
let r10: f32 = cp * sr;
let r20: f32 = -sy * cr + cy * sp * sr;
let r01: f32 = -cy * sr + sy * sp * cr;
let r11: f32 = cp * cr;
let r21: f32 = sy * sr + cy * sp * cr;
let r02: f32 = sy * cp;
let r12: f32 = -sp;
let r22: f32 = cy * cp;
let sx: f32 = scale.get_x();
let sy_sc: f32 = scale.get_y();
let sz: f32 = scale.get_z();
Self {
elements: [
r00 * sx,
r10 * sx,
r20 * sx,
0.0,
r01 * sy_sc,
r11 * sy_sc,
r21 * sy_sc,
0.0,
r02 * sz,
r12 * sz,
r22 * sz,
0.0,
position.get_x(),
position.get_y(),
position.get_z(),
1.0,
],
}
}
#[inline(always)]
#[must_use]
pub fn mul_point4(&self, vec_value: Vec3) -> [f32; 4] {
let matrix_elems: [f32; 16] = self.get_elements();
let px: f32 = vec_value.get_x();
let py: f32 = vec_value.get_y();
let pz: f32 = vec_value.get_z();
[
px.mul_add(
matrix_elems[0],
py.mul_add(
matrix_elems[4],
pz.mul_add(matrix_elems[8], matrix_elems[12]),
),
),
px.mul_add(
matrix_elems[1],
py.mul_add(
matrix_elems[5],
pz.mul_add(matrix_elems[9], matrix_elems[13]),
),
),
px.mul_add(
matrix_elems[2],
py.mul_add(
matrix_elems[6],
pz.mul_add(matrix_elems[10], matrix_elems[14]),
),
),
px.mul_add(
matrix_elems[3],
py.mul_add(
matrix_elems[7],
pz.mul_add(matrix_elems[11], matrix_elems[15]),
),
),
]
}
#[inline(always)]
#[must_use]
pub fn mul_point3(&self, vec_value: Vec3) -> Vec3 {
let matrix_elems: [f32; 16] = self.get_elements();
let px: f32 = vec_value.get_x();
let py: f32 = vec_value.get_y();
let pz: f32 = vec_value.get_z();
Vec3::new(
px.mul_add(
matrix_elems[0],
py.mul_add(
matrix_elems[4],
pz.mul_add(matrix_elems[8], matrix_elems[12]),
),
),
px.mul_add(
matrix_elems[1],
py.mul_add(
matrix_elems[5],
pz.mul_add(matrix_elems[9], matrix_elems[13]),
),
),
px.mul_add(
matrix_elems[2],
py.mul_add(
matrix_elems[6],
pz.mul_add(matrix_elems[10], matrix_elems[14]),
),
),
)
}
#[inline(always)]
#[must_use]
pub fn mul_vec3(&self, vec_value: Vec3) -> Vec3 {
let matrix_elems: [f32; 16] = self.get_elements();
let vx: f32 = vec_value.get_x();
let vy: f32 = vec_value.get_y();
let vz: f32 = vec_value.get_z();
Vec3::new(
vx.mul_add(
matrix_elems[0],
vy.mul_add(matrix_elems[4], vz * matrix_elems[8]),
),
vx.mul_add(
matrix_elems[1],
vy.mul_add(matrix_elems[5], vz * matrix_elems[9]),
),
vx.mul_add(
matrix_elems[2],
vy.mul_add(matrix_elems[6], vz * matrix_elems[10]),
),
)
}
#[inline(always)]
#[must_use]
pub fn transform_bounds(&self, local: AxisAlignedBox3D) -> AxisAlignedBox3D {
let matrix_elems: [f32; 16] = self.get_elements();
let lmin: Vec3 = local.get_min();
let lmax: Vec3 = local.get_max();
let lx0: f32 = lmin.get_x();
let ly0: f32 = lmin.get_y();
let lz0: f32 = lmin.get_z();
let lx1: f32 = lmax.get_x();
let ly1: f32 = lmax.get_y();
let lz1: f32 = lmax.get_z();
let ax0: f32 = matrix_elems[0] * lx0;
let ax1: f32 = matrix_elems[0] * lx1;
let ay0: f32 = matrix_elems[4] * ly0;
let ay1: f32 = matrix_elems[4] * ly1;
let az0: f32 = matrix_elems[8] * lz0;
let az1: f32 = matrix_elems[8] * lz1;
let out_min_x: f32 = matrix_elems[12] + ax0.min(ax1) + ay0.min(ay1) + az0.min(az1);
let out_max_x: f32 = matrix_elems[12] + ax0.max(ax1) + ay0.max(ay1) + az0.max(az1);
let bx0: f32 = matrix_elems[1] * lx0;
let bx1: f32 = matrix_elems[1] * lx1;
let by0: f32 = matrix_elems[5] * ly0;
let by1: f32 = matrix_elems[5] * ly1;
let bz0: f32 = matrix_elems[9] * lz0;
let bz1: f32 = matrix_elems[9] * lz1;
let out_min_y: f32 = matrix_elems[13] + bx0.min(bx1) + by0.min(by1) + bz0.min(bz1);
let out_max_y: f32 = matrix_elems[13] + bx0.max(bx1) + by0.max(by1) + bz0.max(bz1);
let cx0: f32 = matrix_elems[2] * lx0;
let cx1: f32 = matrix_elems[2] * lx1;
let cy0: f32 = matrix_elems[6] * ly0;
let cy1: f32 = matrix_elems[6] * ly1;
let cz0: f32 = matrix_elems[10] * lz0;
let cz1: f32 = matrix_elems[10] * lz1;
let out_min_z: f32 = matrix_elems[14] + cx0.min(cx1) + cy0.min(cy1) + cz0.min(cz1);
let out_max_z: f32 = matrix_elems[14] + cx0.max(cx1) + cy0.max(cy1) + cz0.max(cz1);
AxisAlignedBox3D::new(
Vec3::new(out_min_x, out_min_y, out_min_z),
Vec3::new(out_max_x, out_max_y, out_max_z),
)
}
#[must_use]
pub fn inverse_trs_euler(
position: Vec3,
yaw_rad: f32,
pitch_rad: f32,
roll_rad: f32,
scale: Vec3,
) -> Self {
let isx: f32 = if scale.get_x().abs() > 1e-6 {
1.0 / scale.get_x()
} else {
1.0
};
let isy: f32 = if scale.get_y().abs() > 1e-6 {
1.0 / scale.get_y()
} else {
1.0
};
let isz: f32 = if scale.get_z().abs() > 1e-6 {
1.0 / scale.get_z()
} else {
1.0
};
let (r00, r10, r20, r01, r11, r21, r02, r12, r22): (
f32,
f32,
f32,
f32,
f32,
f32,
f32,
f32,
f32,
) = if pitch_rad.abs() < 1e-6 && roll_rad.abs() < 1e-6 {
let (cy, sy): (f32, f32) = (yaw_rad.cos(), yaw_rad.sin());
(cy, 0.0, -sy, 0.0, 1.0, 0.0, sy, 0.0, cy)
} else {
let (cy, sy): (f32, f32) = (yaw_rad.cos(), yaw_rad.sin());
let (cp, sp): (f32, f32) = (pitch_rad.cos(), pitch_rad.sin());
let (cr, sr): (f32, f32) = (roll_rad.cos(), roll_rad.sin());
(
cy * cr + sy * sp * sr,
cp * sr,
-sy * cr + cy * sp * sr,
-cy * sr + sy * sp * cr,
cp * cr,
sy * sr + cy * sp * cr,
sy * cp,
-sp,
cy * cp,
)
};
let m00: f32 = r00 * isx;
let m01: f32 = r10 * isx;
let m02: f32 = r20 * isx;
let m10: f32 = r01 * isy;
let m11: f32 = r11 * isy;
let m12: f32 = r21 * isy;
let m20: f32 = r02 * isz;
let m21: f32 = r12 * isz;
let m22: f32 = r22 * isz;
let px: f32 = position.get_x();
let py: f32 = position.get_y();
let pz: f32 = position.get_z();
let tx: f32 = -(m00 * px + m01 * py + m02 * pz);
let ty: f32 = -(m10 * px + m11 * py + m12 * pz);
let tz: f32 = -(m20 * px + m21 * py + m22 * pz);
Self {
elements: [
m00, m10, m20, 0.0, m01, m11, m21, 0.0, m02, m12, m22, 0.0, tx, ty, tz, 1.0,
],
}
}
#[must_use]
pub fn normal_basis_from_euler_scale(
yaw_rad: f32,
pitch_rad: f32,
roll_rad: f32,
scale: Vec3,
) -> [Vec3; 3] {
let isx: f32 = if scale.get_x().abs() > 1e-6 {
1.0 / scale.get_x()
} else {
1.0
};
let isy: f32 = if scale.get_y().abs() > 1e-6 {
1.0 / scale.get_y()
} else {
1.0
};
let isz: f32 = if scale.get_z().abs() > 1e-6 {
1.0 / scale.get_z()
} else {
1.0
};
if pitch_rad.abs() < 1e-6 && roll_rad.abs() < 1e-6 {
let (cy, sy): (f32, f32) = (yaw_rad.cos(), yaw_rad.sin());
return [
Vec3::new(cy * isx, 0.0, -sy * isx),
Vec3::new(0.0, isy, 0.0),
Vec3::new(sy * isz, 0.0, cy * isz),
];
}
let (cy, sy): (f32, f32) = (yaw_rad.cos(), yaw_rad.sin());
let (cp, sp): (f32, f32) = (pitch_rad.cos(), pitch_rad.sin());
let (cr, sr): (f32, f32) = (roll_rad.cos(), roll_rad.sin());
[
Vec3::new(
(cy * cr + sy * sp * sr) * isx,
(cp * sr) * isx,
(-sy * cr + cy * sp * sr) * isx,
),
Vec3::new(
(-cy * sr + sy * sp * cr) * isy,
(cp * cr) * isy,
(sy * sr + cy * sp * cr) * isy,
),
Vec3::new((sy * cp) * isz, (-sp) * isz, (cy * cp) * isz),
]
}
#[inline(always)]
#[must_use]
pub fn transform_normal_with_basis(vec_value: Vec3, basis: [Vec3; 3]) -> Vec3 {
let nx: f32 = vec_value.get_x();
let ny: f32 = vec_value.get_y();
let nz: f32 = vec_value.get_z();
Vec3::new(
nx.mul_add(
basis[0].get_x(),
ny.mul_add(basis[1].get_x(), nz * basis[2].get_x()),
),
nx.mul_add(
basis[0].get_y(),
ny.mul_add(basis[1].get_y(), nz * basis[2].get_y()),
),
nx.mul_add(
basis[0].get_z(),
ny.mul_add(basis[1].get_z(), nz * basis[2].get_z()),
),
)
.normalize()
}
}
impl AxisAlignedBox3D {
#[must_use]
pub const fn empty() -> Self {
Self {
min: Vec3::splat(f32::INFINITY),
max: Vec3::splat(f32::NEG_INFINITY),
}
}
pub fn expand_point(&mut self, point: Vec3) {
let mn: Vec3 = self.get_min().min(point);
let mx: Vec3 = self.get_max().max(point);
self.set_min(mn);
self.set_max(mx);
}
pub fn expand_box(&mut self, other: Self) {
let mn: Vec3 = self.get_min().min(other.get_min());
let mx: Vec3 = self.get_max().max(other.get_max());
self.set_min(mn);
self.set_max(mx);
}
#[must_use]
pub fn center(&self) -> Vec3 {
self.get_min().add(self.get_max()).scale(0.5)
}
#[must_use]
pub fn radius(&self) -> f32 {
self.get_max().sub(self.get_min()).length() * 0.5
}
#[must_use]
pub fn surface_area(&self) -> f32 {
let vec_value: Vec3 = self.get_max().sub(self.get_min());
let dx: f32 = vec_value.get_x().max(0.0);
let dy: f32 = vec_value.get_y().max(0.0);
let dz: f32 = vec_value.get_z().max(0.0);
dx * dy + dy * dz + dz * dx
}
#[must_use]
pub fn intersect_ray_near(&self, origin: Vec3, inv_dir: Vec3, t_min: f32, t_max: f32) -> f32 {
let mn: Vec3 = self.get_min();
let mx: Vec3 = self.get_max();
let tx1: f32 = (mn.get_x() - origin.get_x()) * inv_dir.get_x();
let tx2: f32 = (mx.get_x() - origin.get_x()) * inv_dir.get_x();
let mut tmin: f32 = t_min.max(tx1.min(tx2));
let mut tmax: f32 = t_max.min(tx1.max(tx2));
let ty1: f32 = (mn.get_y() - origin.get_y()) * inv_dir.get_y();
let ty2: f32 = (mx.get_y() - origin.get_y()) * inv_dir.get_y();
tmin = tmin.max(ty1.min(ty2));
tmax = tmax.min(ty1.max(ty2));
let tz1: f32 = (mn.get_z() - origin.get_z()) * inv_dir.get_z();
let tz2: f32 = (mx.get_z() - origin.get_z()) * inv_dir.get_z();
tmin = tmin.max(tz1.min(tz2));
tmax = tmax.min(tz1.max(tz2));
if tmax >= tmin && tmax >= 0.0 {
tmin
} else {
f32::INFINITY
}
}
#[must_use]
pub fn intersects_ray(&self, ray: &Ray3D, t_min: f32, t_max: f32) -> bool {
self.intersect_ray_near(ray.get_origin(), ray.get_inv_direction(), t_min, t_max)
.is_finite()
}
}
impl Ray3D {
#[must_use]
fn safe_inv(item_val: f32) -> f32 {
if item_val.abs() < 1e-7 {
if item_val < 0.0 { -1e7 } else { 1e7 }
} else {
1.0 / item_val
}
}
#[must_use]
pub fn safe_inv_vec(dir: Vec3) -> Vec3 {
Vec3::new(
Self::safe_inv(dir.get_x()),
Self::safe_inv(dir.get_y()),
Self::safe_inv(dir.get_z()),
)
}
#[must_use]
pub fn new(origin: Vec3, direction: Vec3) -> Self {
let dir: Vec3 = direction.normalize();
Self {
origin,
direction: dir,
inv_direction: Vec3::new(
Self::safe_inv(dir.get_x()),
Self::safe_inv(dir.get_y()),
Self::safe_inv(dir.get_z()),
),
}
}
}
impl Frustum3D {
#[must_use]
fn normalize_plane(point_val: [f32; 4]) -> [f32; 4] {
let len: f32 = (point_val[0] * point_val[0]
+ point_val[1] * point_val[1]
+ point_val[2] * point_val[2])
.sqrt()
.max(1e-6);
[
point_val[0] / len,
point_val[1] / len,
point_val[2] / len,
point_val[3] / len,
]
}
#[must_use]
pub fn from_view_proj(matrix_value: &Mat4) -> Self {
let matrix_elems: [f32; 16] = matrix_value.get_elements();
let r0: [f32; 4] = [
matrix_elems[0],
matrix_elems[4],
matrix_elems[8],
matrix_elems[12],
];
let r1: [f32; 4] = [
matrix_elems[1],
matrix_elems[5],
matrix_elems[9],
matrix_elems[13],
];
let r2: [f32; 4] = [
matrix_elems[2],
matrix_elems[6],
matrix_elems[10],
matrix_elems[14],
];
let r3: [f32; 4] = [
matrix_elems[3],
matrix_elems[7],
matrix_elems[11],
matrix_elems[15],
];
Self {
planes: [
Self::normalize_plane([r3[0] + r0[0], r3[1] + r0[1], r3[2] + r0[2], r3[3] + r0[3]]),
Self::normalize_plane([r3[0] - r0[0], r3[1] - r0[1], r3[2] - r0[2], r3[3] - r0[3]]),
Self::normalize_plane([r3[0] + r1[0], r3[1] + r1[1], r3[2] + r1[2], r3[3] + r1[3]]),
Self::normalize_plane([r3[0] - r1[0], r3[1] - r1[1], r3[2] - r1[2], r3[3] - r1[3]]),
Self::normalize_plane([r3[0] + r2[0], r3[1] + r2[1], r3[2] + r2[2], r3[3] + r2[3]]),
Self::normalize_plane([r3[0] - r2[0], r3[1] - r2[1], r3[2] - r2[2], r3[3] - r2[3]]),
],
}
}
#[must_use]
pub fn intersects_bounds(&self, bounds_rect: &AxisAlignedBox3D) -> bool {
let mn: Vec3 = bounds_rect.get_min();
let mx: Vec3 = bounds_rect.get_max();
for point_val in self.get_planes().iter() {
let px: f32 = if point_val[0] >= 0.0 {
mx.get_x()
} else {
mn.get_x()
};
let py: f32 = if point_val[1] >= 0.0 {
mx.get_y()
} else {
mn.get_y()
};
let pz: f32 = if point_val[2] >= 0.0 {
mx.get_z()
} else {
mn.get_z()
};
if point_val[0] * px + point_val[1] * py + point_val[2] * pz + point_val[3] < 0.0 {
return false;
}
}
true
}
}
impl Default for Camera3D {
fn default() -> Self {
Self {
position: Vec3::new(0.0, 9.5, 28.0),
yaw: -FRAC_PI_2,
pitch: -0.22,
fov_deg: 60.0,
near: 0.3,
far: 450.0,
}
}
}
impl Camera3D {
#[must_use]
pub fn forward(&self) -> Vec3 {
let yaw: f32 = self.get_yaw();
let pitch: f32 = self.get_pitch();
let cp: f32 = pitch.cos();
Vec3::new(yaw.cos() * cp, pitch.sin(), yaw.sin() * cp).normalize()
}
#[must_use]
pub fn right(&self) -> Vec3 {
self.forward().cross(Vec3::new(0.0, 1.0, 0.0)).normalize()
}
#[must_use]
pub fn focal_length_to_fov_deg(focal_length_mm: f32, sensor_height_mm: f32) -> f32 {
let f_mm: f32 = focal_length_mm.max(8.0);
let s_mm: f32 = sensor_height_mm.max(4.0);
(2.0 * (s_mm / (2.0 * f_mm)).atan().to_degrees()).clamp(12.0, 120.0)
}
#[must_use]
pub fn from_lens(position: Vec3, yaw: f32, pitch: f32, focal_length_mm: f32) -> Self {
Self {
position,
yaw,
pitch: pitch.clamp(-1.4, 1.4),
fov_deg: Self::focal_length_to_fov_deg(focal_length_mm, 24.0),
near: 0.25,
far: 480.0,
}
}
#[must_use]
pub fn ev100(aperture_fstop: f32, shutter_seconds: f32, iso: f32) -> f32 {
let item_count: f32 = aperture_fstop.max(0.7);
let blend_factor: f32 = shutter_seconds.max(1e-5);
let source_val: f32 = iso.max(25.0);
((item_count * item_count) / blend_factor * (100.0 / source_val)).log2()
}
#[must_use]
pub fn exposure_scale_from_ev100(ev100: f32) -> f32 {
let max_lum: f32 = 1.2 * 2.0_f32.powf(ev100);
(1.0 / max_lum.max(1e-4)).clamp(0.05, 8.0)
}
pub fn look_at(&mut self, target: Vec3) {
let dir: Vec3 = target.sub(self.get_position()).normalize();
self.set_yaw(dir.get_z().atan2(dir.get_x()));
self.set_pitch(dir.get_y().clamp(-0.98, 0.98).asin());
}
#[must_use]
pub fn lerp(self, other: Self, alpha: f32) -> Self {
let entry_index: f32 = alpha.clamp(0.0, 1.0);
let diff: f32 = other.get_yaw() - self.get_yaw();
let raw_yaw_delta: f32 = diff - (diff / TAU).floor() * TAU;
let shortest_yaw: f32 = if raw_yaw_delta > PI {
raw_yaw_delta - TAU
} else {
raw_yaw_delta
};
Self {
position: self.get_position().lerp(other.get_position(), entry_index),
yaw: self.get_yaw() + shortest_yaw * entry_index,
pitch: (self.get_pitch() + (other.get_pitch() - self.get_pitch()) * entry_index)
.clamp(-1.4, 1.4),
fov_deg: self.get_fov_deg() + (other.get_fov_deg() - self.get_fov_deg()) * entry_index,
near: self.get_near() + (other.get_near() - self.get_near()) * entry_index,
far: self.get_far() + (other.get_far() - self.get_far()) * entry_index,
}
}
#[must_use]
pub fn effective_vertical_fov_rad(base_fov_deg: f32, aspect: f32) -> f32 {
let base_rad: f32 = base_fov_deg.clamp(15.0, 115.0).to_radians();
let safe_aspect: f32 = aspect.max(0.1);
let base_tan_half_v: f32 = (base_rad * 0.5).tan();
let min_tan_half_v: f32 = (42.0_f32.to_radians() * 0.5).tan().min(base_tan_half_v);
let max_tan_half_h: f32 = 1.65;
let effective_tan_half_v: f32 = if base_tan_half_v * safe_aspect > max_tan_half_h {
(max_tan_half_h / safe_aspect).max(min_tan_half_v)
} else {
base_tan_half_v
};
2.0 * effective_tan_half_v.atan()
}
#[must_use]
pub fn view_projection(&self, aspect: f32) -> Mat4 {
let pos: Vec3 = self.get_position();
let view: Mat4 = Mat4::look_at(pos, pos.add(self.forward()), Vec3::new(0.0, 1.0, 0.0));
let eff_fov_rad: f32 = Self::effective_vertical_fov_rad(self.get_fov_deg(), aspect);
let proj: Mat4 = Mat4::perspective(
eff_fov_rad,
aspect.max(0.1),
self.get_near().max(0.05),
self.get_far().max(10.0),
);
proj.mul(view)
}
}
impl Vec3 {
#[must_use]
pub const fn from_array(values: [f32; 3]) -> Self {
Self {
x: values[0],
y: values[1],
z: values[2],
}
}
#[must_use]
pub fn to_array(&self) -> [f32; 3] {
[self.get_x(), self.get_y(), self.get_z()]
}
#[must_use]
pub fn blender_z_up_to_y_up(self) -> Self {
Self::new(self.get_x(), self.get_z(), -self.get_y())
}
#[must_use]
pub fn y_up_to_blender_z_up(self) -> Self {
Self::new(self.get_x(), -self.get_z(), self.get_y())
}
}
impl Vec2 {
#[must_use]
pub const fn zero() -> Self {
Self { x: 0.0, y: 0.0 }
}
#[must_use]
pub const fn splat(scalar: f32) -> Self {
Self {
x: scalar,
y: scalar,
}
}
#[must_use]
pub const fn from_array(values: [f32; 2]) -> Self {
Self {
x: values[0],
y: values[1],
}
}
#[must_use]
pub fn to_array(&self) -> [f32; 2] {
[self.get_x(), self.get_y()]
}
#[must_use]
pub fn dot(self, rhs: Self) -> f32 {
self.get_x()
.mul_add(rhs.get_x(), self.get_y() * rhs.get_y())
}
#[must_use]
pub fn lerp(self, other: Self, alpha: f32) -> Self {
Self::new(
(other.get_x() - self.get_x()).mul_add(alpha, self.get_x()),
(other.get_y() - self.get_y()).mul_add(alpha, self.get_y()),
)
}
}
impl Vec4 {
#[must_use]
pub const fn zero() -> Self {
Self {
x: 0.0,
y: 0.0,
z: 0.0,
w: 0.0,
}
}
#[must_use]
pub const fn from_array(values: [f32; 4]) -> Self {
Self {
x: values[0],
y: values[1],
z: values[2],
w: values[3],
}
}
#[must_use]
pub fn to_array(&self) -> [f32; 4] {
[self.get_x(), self.get_y(), self.get_z(), self.get_w()]
}
#[must_use]
pub fn xyz(&self) -> Vec3 {
Vec3::new(self.get_x(), self.get_y(), self.get_z())
}
#[must_use]
pub fn lerp(self, other: Self, alpha: f32) -> Self {
Self::new(
(other.get_x() - self.get_x()).mul_add(alpha, self.get_x()),
(other.get_y() - self.get_y()).mul_add(alpha, self.get_y()),
(other.get_z() - self.get_z()).mul_add(alpha, self.get_z()),
(other.get_w() - self.get_w()).mul_add(alpha, self.get_w()),
)
}
}
impl Default for Quat {
fn default() -> Self {
Self::identity()
}
}
impl Quat {
#[must_use]
pub const fn identity() -> Self {
Self {
x: 0.0,
y: 0.0,
z: 0.0,
w: 1.0,
}
}
#[must_use]
pub const fn from_xyzw(x: f32, y: f32, z: f32, w: f32) -> Self {
Self { x, y, z, w }
}
#[must_use]
pub const fn from_array(values: [f32; 4]) -> Self {
Self {
x: values[0],
y: values[1],
z: values[2],
w: values[3],
}
}
#[must_use]
pub fn to_array(&self) -> [f32; 4] {
[self.get_x(), self.get_y(), self.get_z(), self.get_w()]
}
#[must_use]
pub fn dot(self, rhs: Self) -> f32 {
self.get_x() * rhs.get_x()
+ self.get_y() * rhs.get_y()
+ self.get_z() * rhs.get_z()
+ self.get_w() * rhs.get_w()
}
#[must_use]
pub fn normalize(self) -> Self {
let len_sq: f32 = self.dot(self);
if len_sq > 1e-12 {
let inv: f32 = 1.0 / len_sq.sqrt();
Self::from_xyzw(
self.get_x() * inv,
self.get_y() * inv,
self.get_z() * inv,
self.get_w() * inv,
)
} else {
Self::identity()
}
}
#[must_use]
pub fn conjugate(self) -> Self {
Self::from_xyzw(-self.get_x(), -self.get_y(), -self.get_z(), self.get_w())
}
#[must_use]
pub fn mul_quat(self, rhs: Self) -> Self {
let ax: f32 = self.get_x();
let ay: f32 = self.get_y();
let az: f32 = self.get_z();
let aw: f32 = self.get_w();
let bx: f32 = rhs.get_x();
let by: f32 = rhs.get_y();
let bz: f32 = rhs.get_z();
let bw: f32 = rhs.get_w();
Self::from_xyzw(
aw * bx + ax * bw + ay * bz - az * by,
aw * by - ax * bz + ay * bw + az * bx,
aw * bz + ax * by - ay * bx + az * bw,
aw * bw - ax * bx - ay * by - az * bz,
)
}
#[must_use]
pub fn rotate_vec3(self, vec_val: Vec3) -> Vec3 {
let qv: Vec3 = Vec3::new(self.get_x(), self.get_y(), self.get_z());
let uv: Vec3 = qv.cross(vec_val);
let uuv: Vec3 = qv.cross(uv);
vec_val
.add(uv.scale(2.0 * self.get_w()))
.add(uuv.scale(2.0))
}
#[must_use]
pub fn from_axis_angle(axis: Vec3, angle_rad: f32) -> Self {
let unit: Vec3 = axis.normalize();
let half: f32 = angle_rad * 0.5;
let sin_h: f32 = half.sin();
let cos_h: f32 = half.cos();
Self::from_xyzw(
unit.get_x() * sin_h,
unit.get_y() * sin_h,
unit.get_z() * sin_h,
cos_h,
)
.normalize()
}
#[must_use]
pub fn from_euler_yxz(yaw_rad: f32, pitch_rad: f32, roll_rad: f32) -> Self {
let qy: Self = Self::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), yaw_rad);
let qx: Self = Self::from_axis_angle(Vec3::new(1.0, 0.0, 0.0), pitch_rad);
let qz: Self = Self::from_axis_angle(Vec3::new(0.0, 0.0, 1.0), roll_rad);
qy.mul_quat(qx).mul_quat(qz).normalize()
}
#[must_use]
pub fn from_mat3(col0: Vec3, col1: Vec3, col2: Vec3) -> Self {
let m00: f32 = col0.get_x();
let m10: f32 = col0.get_y();
let m20: f32 = col0.get_z();
let m01: f32 = col1.get_x();
let m11: f32 = col1.get_y();
let m21: f32 = col1.get_z();
let m02: f32 = col2.get_x();
let m12: f32 = col2.get_y();
let m22: f32 = col2.get_z();
let trace: f32 = m00 + m11 + m22;
if trace > 0.0 {
let s: f32 = (trace + 1.0).sqrt() * 2.0;
Self::from_xyzw((m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s, 0.25 * s).normalize()
} else if m00 > m11 && m00 > m22 {
let s: f32 = (1.0 + m00 - m11 - m22).max(1e-12).sqrt() * 2.0;
Self::from_xyzw(0.25 * s, (m01 + m10) / s, (m02 + m20) / s, (m21 - m12) / s).normalize()
} else if m11 > m22 {
let s: f32 = (1.0 + m11 - m00 - m22).max(1e-12).sqrt() * 2.0;
Self::from_xyzw((m01 + m10) / s, 0.25 * s, (m12 + m21) / s, (m02 - m20) / s).normalize()
} else {
let s: f32 = (1.0 + m22 - m00 - m11).max(1e-12).sqrt() * 2.0;
Self::from_xyzw((m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s).normalize()
}
}
#[must_use]
pub fn to_mat4(self) -> Mat4 {
let q: Self = self.normalize();
let x: f32 = q.get_x();
let y: f32 = q.get_y();
let z: f32 = q.get_z();
let w: f32 = q.get_w();
let xx: f32 = x * x;
let yy: f32 = y * y;
let zz: f32 = z * z;
let xy: f32 = x * y;
let xz: f32 = x * z;
let yz: f32 = y * z;
let wx: f32 = w * x;
let wy: f32 = w * y;
let wz: f32 = w * z;
Mat4 {
elements: [
1.0 - 2.0 * (yy + zz),
2.0 * (xy + wz),
2.0 * (xz - wy),
0.0,
2.0 * (xy - wz),
1.0 - 2.0 * (xx + zz),
2.0 * (yz + wx),
0.0,
2.0 * (xz + wy),
2.0 * (yz - wx),
1.0 - 2.0 * (xx + yy),
0.0,
0.0,
0.0,
0.0,
1.0,
],
}
}
#[must_use]
pub fn nlerp(self, other: Self, alpha: f32) -> Self {
let t: f32 = alpha.clamp(0.0, 1.0);
let sign: f32 = if self.dot(other) < 0.0 { -1.0 } else { 1.0 };
let inv_t: f32 = 1.0 - t;
Self::from_xyzw(
self.get_x() * inv_t + other.get_x() * sign * t,
self.get_y() * inv_t + other.get_y() * sign * t,
self.get_z() * inv_t + other.get_z() * sign * t,
self.get_w() * inv_t + other.get_w() * sign * t,
)
.normalize()
}
#[must_use]
pub fn slerp(self, other: Self, alpha: f32) -> Self {
let t: f32 = alpha.clamp(0.0, 1.0);
let mut dot_val: f32 = self.dot(other);
let mut target: Self = other;
if dot_val < 0.0 {
dot_val = -dot_val;
target = Self::from_xyzw(
-other.get_x(),
-other.get_y(),
-other.get_z(),
-other.get_w(),
);
}
if dot_val > 0.9995 {
return self.nlerp(target, t);
}
let theta: f32 = dot_val.clamp(-1.0, 1.0).acos();
let sin_theta: f32 = theta.sin().max(1e-6);
let w0: f32 = ((1.0 - t) * theta).sin() / sin_theta;
let w1: f32 = (t * theta).sin() / sin_theta;
Self::from_xyzw(
self.get_x() * w0 + target.get_x() * w1,
self.get_y() * w0 + target.get_y() * w1,
self.get_z() * w0 + target.get_z() * w1,
self.get_w() * w0 + target.get_w() * w1,
)
.normalize()
}
#[must_use]
pub fn blender_z_up_to_y_up(self) -> Self {
Self::from_xyzw(self.get_x(), self.get_z(), -self.get_y(), self.get_w()).normalize()
}
#[must_use]
pub fn y_up_to_blender_z_up(self) -> Self {
Self::from_xyzw(self.get_x(), -self.get_z(), self.get_y(), self.get_w()).normalize()
}
}
impl Default for DualQuat {
fn default() -> Self {
Self::identity()
}
}
impl DualQuat {
#[must_use]
pub const fn identity() -> Self {
Self {
real: Quat::identity(),
dual: Quat::from_xyzw(0.0, 0.0, 0.0, 0.0),
}
}
#[must_use]
pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
let real: Quat = rotation.normalize();
let t_quat: Quat = Quat::from_xyzw(
translation.get_x(),
translation.get_y(),
translation.get_z(),
0.0,
);
let prod: Quat = t_quat.mul_quat(real);
let dual: Quat = Quat::from_xyzw(
prod.get_x() * 0.5,
prod.get_y() * 0.5,
prod.get_z() * 0.5,
prod.get_w() * 0.5,
);
Self { real, dual }
}
#[must_use]
pub fn normalize(self) -> Self {
let r: Quat = self.get_real();
let d: Quat = self.get_dual();
let norm_sq: f32 = r.dot(r);
if norm_sq <= 1e-12 {
return Self::identity();
}
let inv_norm: f32 = 1.0 / norm_sq.sqrt();
let real_unit: Quat = Quat::from_xyzw(
r.get_x() * inv_norm,
r.get_y() * inv_norm,
r.get_z() * inv_norm,
r.get_w() * inv_norm,
);
let dual_scaled: Quat = Quat::from_xyzw(
d.get_x() * inv_norm,
d.get_y() * inv_norm,
d.get_z() * inv_norm,
d.get_w() * inv_norm,
);
let proj: f32 = real_unit.dot(dual_scaled);
let dual_ortho: Quat = Quat::from_xyzw(
dual_scaled.get_x() - real_unit.get_x() * proj,
dual_scaled.get_y() - real_unit.get_y() * proj,
dual_scaled.get_z() - real_unit.get_z() * proj,
dual_scaled.get_w() - real_unit.get_w() * proj,
);
Self {
real: real_unit,
dual: dual_ortho,
}
}
#[must_use]
pub fn translation(&self) -> Vec3 {
let prod: Quat = self.get_dual().mul_quat(self.get_real().conjugate());
Vec3::new(prod.get_x() * 2.0, prod.get_y() * 2.0, prod.get_z() * 2.0)
}
#[must_use]
pub fn blend_weighted(duals: &[Self], weights: &[f32]) -> Self {
let count: usize = duals.len().min(weights.len());
if count == 0 {
return Self::identity();
}
let mut pivot: Option<Quat> = None;
let mut sum_rx: f32 = 0.0;
let mut sum_ry: f32 = 0.0;
let mut sum_rz: f32 = 0.0;
let mut sum_rw: f32 = 0.0;
let mut sum_dx: f32 = 0.0;
let mut sum_dy: f32 = 0.0;
let mut sum_dz: f32 = 0.0;
let mut sum_dw: f32 = 0.0;
for idx in 0..count {
let weight: f32 = weights[idx];
if weight.abs() <= 1e-6 {
continue;
}
let r: Quat = duals[idx].get_real();
let d: Quat = duals[idx].get_dual();
let sign: f32 = match pivot {
Some(ref_q) => {
if ref_q.dot(r) < 0.0 {
-1.0
} else {
1.0
}
}
None => {
pivot = Some(r);
1.0
}
};
let sw: f32 = weight * sign;
sum_rx += r.get_x() * sw;
sum_ry += r.get_y() * sw;
sum_rz += r.get_z() * sw;
sum_rw += r.get_w() * sw;
sum_dx += d.get_x() * sw;
sum_dy += d.get_y() * sw;
sum_dz += d.get_z() * sw;
sum_dw += d.get_w() * sw;
}
Self {
real: Quat::from_xyzw(sum_rx, sum_ry, sum_rz, sum_rw),
dual: Quat::from_xyzw(sum_dx, sum_dy, sum_dz, sum_dw),
}
.normalize()
}
#[must_use]
pub fn transform_point(self, point: Vec3) -> Vec3 {
self.get_real().rotate_vec3(point).add(self.translation())
}
#[must_use]
pub fn transform_vector(self, vector: Vec3) -> Vec3 {
self.get_real().rotate_vec3(vector)
}
}
impl Mat4 {
#[must_use]
pub const fn from_cols_array(elements: [f32; 16]) -> Self {
Self { elements }
}
#[must_use]
pub fn transpose(&self) -> Self {
let e: [f32; 16] = self.get_elements();
Self {
elements: [
e[0], e[4], e[8], e[12], e[1], e[5], e[9], e[13], e[2], e[6], e[10], e[14], e[3],
e[7], e[11], e[15],
],
}
}
#[must_use]
pub fn orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Self {
let rl: f32 = (right - left).max(1e-5);
let tb: f32 = (top - bottom).max(1e-5);
let fn_span: f32 = (far - near).max(1e-5);
Self {
elements: [
2.0 / rl,
0.0,
0.0,
0.0,
0.0,
2.0 / tb,
0.0,
0.0,
0.0,
0.0,
-2.0 / fn_span,
0.0,
-(right + left) / rl,
-(top + bottom) / tb,
-(far + near) / fn_span,
1.0,
],
}
}
#[must_use]
pub fn orthographic_mag(xmag: f32, ymag: f32, near: f32, far: f32) -> Self {
let xm: f32 = xmag.abs().max(1e-4);
let ym: f32 = ymag.abs().max(1e-4);
Self::orthographic(-xm, xm, -ym, ym, near, far)
}
#[must_use]
pub fn from_trs_quat(translation: Vec3, rotation: Quat, scale: Vec3) -> Self {
let rot_elems: [f32; 16] = rotation.to_mat4().get_elements();
let sx: f32 = scale.get_x();
let sy: f32 = scale.get_y();
let sz: f32 = scale.get_z();
Self {
elements: [
rot_elems[0] * sx,
rot_elems[1] * sx,
rot_elems[2] * sx,
0.0,
rot_elems[4] * sy,
rot_elems[5] * sy,
rot_elems[6] * sy,
0.0,
rot_elems[8] * sz,
rot_elems[9] * sz,
rot_elems[10] * sz,
0.0,
translation.get_x(),
translation.get_y(),
translation.get_z(),
1.0,
],
}
}
#[must_use]
pub fn decompose_trs(&self) -> (Vec3, Quat, Vec3) {
let e: [f32; 16] = self.get_elements();
let translation: Vec3 = Vec3::new(e[12], e[13], e[14]);
let col0: Vec3 = Vec3::new(e[0], e[1], e[2]);
let col1: Vec3 = Vec3::new(e[4], e[5], e[6]);
let col2: Vec3 = Vec3::new(e[8], e[9], e[10]);
let mut sx: f32 = col0.length();
let sy: f32 = col1.length();
let sz: f32 = col2.length();
let det3: f32 = col0.dot(col1.cross(col2));
if det3 < 0.0 {
sx = -sx;
}
let r0: Vec3 = if sx.abs() > 1e-6 {
col0.scale(1.0 / sx)
} else {
Vec3::new(1.0, 0.0, 0.0)
};
let r1: Vec3 = if sy.abs() > 1e-6 {
col1.scale(1.0 / sy)
} else {
Vec3::new(0.0, 1.0, 0.0)
};
let r2: Vec3 = if sz.abs() > 1e-6 {
col2.scale(1.0 / sz)
} else {
Vec3::new(0.0, 0.0, 1.0)
};
let rotation: Quat = Quat::from_mat3(r0, r1, r2);
(translation, rotation, Vec3::new(sx, sy, sz))
}
#[must_use]
pub fn inverse(&self) -> Option<Self> {
let m: [f32; 16] = self.get_elements();
let s0: f32 = m[0] * m[5] - m[4] * m[1];
let s1: f32 = m[0] * m[6] - m[4] * m[2];
let s2: f32 = m[0] * m[7] - m[4] * m[3];
let s3: f32 = m[1] * m[6] - m[5] * m[2];
let s4: f32 = m[1] * m[7] - m[5] * m[3];
let s5: f32 = m[2] * m[7] - m[6] * m[3];
let c5: f32 = m[10] * m[15] - m[14] * m[11];
let c4: f32 = m[9] * m[15] - m[13] * m[11];
let c3: f32 = m[9] * m[14] - m[13] * m[10];
let c2: f32 = m[8] * m[15] - m[12] * m[11];
let c1: f32 = m[8] * m[14] - m[12] * m[10];
let c0: f32 = m[8] * m[13] - m[12] * m[9];
let det: f32 = s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0;
if det.abs() < 1e-12 {
return None;
}
let inv_det: f32 = 1.0 / det;
Some(Self {
elements: [
(m[5] * c5 - m[6] * c4 + m[7] * c3) * inv_det,
(-m[1] * c5 + m[2] * c4 - m[3] * c3) * inv_det,
(m[13] * s5 - m[14] * s4 + m[15] * s3) * inv_det,
(-m[9] * s5 + m[10] * s4 - m[11] * s3) * inv_det,
(-m[4] * c5 + m[6] * c2 - m[7] * c1) * inv_det,
(m[0] * c5 - m[2] * c2 + m[3] * c1) * inv_det,
(-m[12] * s5 + m[14] * s2 - m[15] * s1) * inv_det,
(m[8] * s5 - m[10] * s2 + m[11] * s1) * inv_det,
(m[4] * c4 - m[5] * c2 + m[7] * c0) * inv_det,
(-m[0] * c4 + m[1] * c2 - m[3] * c0) * inv_det,
(m[12] * s4 - m[13] * s2 + m[15] * s0) * inv_det,
(-m[8] * s4 + m[9] * s2 - m[11] * s0) * inv_det,
(-m[4] * c3 + m[5] * c1 - m[6] * c0) * inv_det,
(m[0] * c3 - m[1] * c1 + m[2] * c0) * inv_det,
(-m[12] * s3 + m[13] * s1 - m[14] * s0) * inv_det,
(m[8] * s3 - m[9] * s1 + m[10] * s0) * inv_det,
],
})
}
#[must_use]
pub fn normal_matrix_3x3(&self) -> [Vec3; 3] {
let e: [f32; 16] = self.get_elements();
let c0: Vec3 = Vec3::new(e[0], e[1], e[2]);
let c1: Vec3 = Vec3::new(e[4], e[5], e[6]);
let c2: Vec3 = Vec3::new(e[8], e[9], e[10]);
let n0: Vec3 = c1.cross(c2);
let n1: Vec3 = c2.cross(c0);
let n2: Vec3 = c0.cross(c1);
let det: f32 = c0.dot(n0);
if det.abs() > 1e-10 {
let inv_det: f32 = 1.0 / det;
[n0.scale(inv_det), n1.scale(inv_det), n2.scale(inv_det)]
} else {
[
Vec3::new(1.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
]
}
}
#[must_use]
pub const fn blender_z_up_to_y_up() -> Self {
Self {
elements: [
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
],
}
}
#[must_use]
pub const fn y_up_to_blender_z_up() -> Self {
Self {
elements: [
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
],
}
}
}