From ed78807a9f21d762bbcb1366ec33d4fda108b794 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 1 Oct 2024 16:19:58 -0700 Subject: [PATCH] integer from float --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/integer.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac6b907..c5f206f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,9 +22,9 @@ checksum = "50202def95bf36cb7d1d7a7962cea1c36a3f8ad42425e5d2b71d7acb8041b5b8" [[package]] name = "fixed_wide" -version = "0.1.0" +version = "0.1.1" source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/" -checksum = "7a8d6e10c51c9df39ead915c62288afbc41d13e00368e526037e530ee5c58e13" +checksum = "d9c2cf115b3785ede870fada07e8b1aeba3378345b4ca86fe3c772ecabc05c0f" dependencies = [ "arrayvec", "bnum", diff --git a/Cargo.toml b/Cargo.toml index 0982a51..9354e4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ authors = ["Rhys Lloyd "] [dependencies] arrayvec = "0.7.4" bitflags = "2.6.0" -fixed_wide = { version = "0.1.0", registry = "strafesnet", features = ["deferred-division","zeroes","wide-mul"] } +fixed_wide = { version = "0.1.1", registry = "strafesnet", features = ["deferred-division","zeroes","wide-mul"] } linear_ops = { version = "0.1.0", registry = "strafesnet", features = ["deferred-division","named-fields"] } ratio_ops = { version = "0.1.0", registry = "strafesnet" } glam = "0.28.0" diff --git a/src/integer.rs b/src/integer.rs index 887e1cf..670102f 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -525,6 +525,7 @@ impl TryFrom<[f32;3]> for Unit32Vec3{ } */ +pub type Planar64TryFromFloatError=fixed_wide::fixed::FixedFromFloatError; pub type Planar64=fixed_wide::types::I32F32; pub type Planar64Vec3=linear_ops::types::Vector3; pub type Planar64Mat3=linear_ops::types::Matrix3; @@ -543,23 +544,52 @@ pub mod vec3{ pub const NEG_Y:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::NEG_ONE,Planar64::ZERO]); pub const NEG_Z:Planar64Vec3=Planar64Vec3::new([Planar64::ZERO,Planar64::ZERO,Planar64::NEG_ONE]); pub const NEG_ONE:Planar64Vec3=Planar64Vec3::new([Planar64::NEG_ONE,Planar64::NEG_ONE,Planar64::NEG_ONE]); - pub fn int(x:i32,y:i32,z:i32)->Planar64Vec3{ - Planar64Vec3::new([Planar64::from(x),Planar64::from(y),Planar64::from(z)]) + #[inline] + pub const fn int(x:i32,y:i32,z:i32)->Planar64Vec3{ + Planar64Vec3::new([Planar64::raw((x as i64)<<32),Planar64::raw((y as i64)<<32),Planar64::raw((z as i64)<<32)]) } + #[inline] pub fn raw_array(array:[i64;3])->Planar64Vec3{ Planar64Vec3::new(array.map(Planar64::raw)) } + #[inline] pub fn raw_xyz(x:i64,y:i64,z:i64)->Planar64Vec3{ Planar64Vec3::new([Planar64::raw(x),Planar64::raw(y),Planar64::raw(z)]) } + #[inline] + pub fn try_from_f32_array([x,y,z]:[f32;3])->Result{ + Ok(Planar64Vec3::new([ + try_from_f32(x)?, + try_from_f32(y)?, + try_from_f32(z)?, + ])) + } } +#[inline] pub fn int(value:i32)->Planar64{ Planar64::from(value) } +#[inline] +pub fn try_from_f32(value:f32)->Result{ + let result:Result=value.try_into(); + match result{ + Ok(ok)=>Ok(ok), + Err(e)=>e.underflow_to_zero(), + } +} pub mod mat3{ use super::*; pub use linear_ops::types::Matrix3; + #[inline] + pub const fn identity()->Planar64Mat3{ + Planar64Mat3::new([ + [Planar64::ONE,Planar64::ZERO,Planar64::ZERO], + [Planar64::ZERO,Planar64::ONE,Planar64::ZERO], + [Planar64::ZERO,Planar64::ZERO,Planar64::ONE], + ]) + } + #[inline] pub fn from_diagonal(diag:Planar64Vec3)->Planar64Mat3{ Planar64Mat3::new([ [diag.x,Planar64::ZERO,Planar64::ZERO], @@ -586,6 +616,14 @@ pub mod mat3{ Planar64Vec3::new([s,Planar64::ZERO,c]), ]) } + #[inline] + pub fn try_from_f32_array_2d([x_axis,y_axis,z_axis]:[[f32;3];3])->Result{ + Ok(Planar64Mat3::new([ + vec3::try_from_f32_array(x_axis)?.to_array(), + vec3::try_from_f32_array(y_axis)?.to_array(), + vec3::try_from_f32_array(z_axis)?.to_array(), + ])) + } } #[derive(Clone,Copy,Default,Hash,Eq,PartialEq)]