From 4e98e9a577c240e6cd9583ff27ecab338a2664d9 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 13 Oct 2023 16:27:36 -0700 Subject: [PATCH] const const const --- src/integer.rs | 30 +++++++++++++++++------------- src/primitives.rs | 11 ++++++++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/integer.rs b/src/integer.rs index f155163..fe445d8 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -78,7 +78,7 @@ impl std::ops::Div for Time{ } #[inline] -fn gcd(mut a:u64,mut b:u64)->u64{ +const fn gcd(mut a:u64,mut b:u64)->u64{ while b!=0{ (a,b)=(b,a.rem_euclid(b)); }; @@ -93,7 +93,7 @@ impl Ratio64{ pub const ZERO:Self=Ratio64{num:0,den:unsafe{std::num::NonZeroU64::new_unchecked(1)}}; pub const ONE:Self=Ratio64{num:1,den:unsafe{std::num::NonZeroU64::new_unchecked(1)}}; #[inline] - pub fn new(num:i64,den:u64)->Option{ + pub const fn new(num:i64,den:u64)->Option{ match std::num::NonZeroU64::new(den){ Some(_)=>{ let d=gcd(num.unsigned_abs(),den); @@ -306,6 +306,7 @@ impl Angle32{ } */ } +const ANGLE32_FLOAT64_PI:f64=(1i64<<31) as f64; impl Into for Angle32{ #[inline] fn into(self)->f32{ @@ -380,22 +381,25 @@ impl Planar64{ pub const ZERO:Self=Self(0); pub const ONE:Self=Self(1<<32); #[inline] - pub fn int(num:i32)->Self{ + pub const fn int(num:i32)->Self{ Self(Self::ONE.0*num as i64) } #[inline] - pub fn raw(num:i64)->Self{ + pub const fn raw(num:i64)->Self{ Self(num) } #[inline] - pub fn get(&self)->i64{ + pub const fn get(&self)->i64{ self.0 } } +const PLANAR64_FLOAT32_ONE:f32=(1u64<<32) as f32; +const PLANAR64_FLOAT32_MUL:f32=1.0/PLANAR64_FLOAT32_ONE; +const PLANAR64_FLOAT64_ONE:f64=(1u64<<32) as f64; impl Into for Planar64{ #[inline] fn into(self)->f32{ - self.0 as f32*(1.0/(1<<32) as f32) + self.0 as f32*PLANAR64_FLOAT32_MUL } } impl From for Planar64{ @@ -519,7 +523,7 @@ impl Planar64Vec3{ pub const MIN:Self=Planar64Vec3(glam::I64Vec3::MIN); pub const MAX:Self=Planar64Vec3(glam::I64Vec3::MAX); #[inline] - pub fn int(x:i32,y:i32,z:i32)->Self{ + pub const fn int(x:i32,y:i32,z:i32)->Self{ Self(glam::i64vec3((x as i64)<<32,(y as i64)<<32,(z as i64)<<32)) } #[inline] @@ -592,7 +596,7 @@ impl Into for Planar64Vec3{ self.0.x as f32, self.0.y as f32, self.0.z as f32, - )*(1.0/(1<<32) as f32) + )*PLANAR64_FLOAT32_MUL } } impl TryFrom<[f32;3]> for Planar64Vec3{ @@ -731,7 +735,7 @@ impl Planar64Mat3{ z_axis, } } - pub fn int_from_cols_array(array:[i32;9])->Self{ + pub const fn int_from_cols_array(array:[i32;9])->Self{ Self{ x_axis:Planar64Vec3::int(array[0],array[1],array[2]), y_axis:Planar64Vec3::int(array[3],array[4],array[5]), @@ -740,9 +744,9 @@ impl Planar64Mat3{ } #[inline] pub fn from_rotation_y(angle:Angle32)->Self{ - let theta=angle.0 as f64*(std::f64::consts::PI/((1<<31) as f64)); + let theta=angle.0 as f64*(std::f64::consts::PI/ANGLE32_FLOAT64_PI); let (s,c)=theta.sin_cos(); - let (c,s)=(c*((1<<32) as f64),s*((1<<32) as f64)); + let (c,s)=(c*PLANAR64_FLOAT64_ONE,s*PLANAR64_FLOAT64_ONE); //TODO: fix this rounding towards 0 let (c,s):(i64,i64)=(unsafe{c.to_int_unchecked()},unsafe{s.to_int_unchecked()}); Self::from_cols( @@ -814,8 +818,8 @@ impl Into for Planar64Affine3{ self.matrix3.x_axis.0.x as f32,self.matrix3.x_axis.0.x as f32,self.matrix3.x_axis.0.x as f32,0.0, self.matrix3.y_axis.0.x as f32,self.matrix3.y_axis.0.x as f32,self.matrix3.y_axis.0.x as f32,0.0, self.matrix3.z_axis.0.x as f32,self.matrix3.z_axis.0.x as f32,self.matrix3.z_axis.0.x as f32,0.0, - self.translation.0.x as f32,self.translation.0.y as f32,self.translation.0.z as f32,(1<<32) as f32 - ])*(1.0/(1<<32) as f32) + self.translation.0.x as f32,self.translation.0.y as f32,self.translation.0.z as f32,PLANAR64_FLOAT32_ONE + ])*PLANAR64_FLOAT32_MUL } } impl TryFrom for Planar64Affine3{ diff --git a/src/primitives.rs b/src/primitives.rs index fc37c47..f010926 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -18,7 +18,12 @@ pub enum CubeFace{ Bottom, Front, } -const CUBE_DEFAULT_TEXTURE_COORDS:[TextureCoordinate;4]=[[0.0,0.0].into(),[1.0,0.0].into(),[1.0,1.0].into(),[0.0,1.0].into()]; +const CUBE_DEFAULT_TEXTURE_COORDS:[TextureCoordinate;4]=[ + TextureCoordinate::new(0.0,0.0), + TextureCoordinate::new(1.0,0.0), + TextureCoordinate::new(1.0,1.0), + TextureCoordinate::new(0.0,1.0), +]; const CUBE_DEFAULT_VERTICES:[Planar64Vec3;8]=[ Planar64Vec3::int(-1,-1, 1),//0 left bottom back Planar64Vec3::int( 1,-1, 1),//1 right bottom back @@ -140,11 +145,11 @@ pub fn unit_cube()->crate::model::IndexedModel{ t.insert(CubeFace::Front,FaceDescription::default()); generate_partial_unit_cube(t) } -const TEAPOT_TRANSFORM:crate::integer::Planar64Mat3=crate::integer::Planar64Mat3::int_from_cols_array([0,1,0, -1,0,0, 0,0,1])/10; +const TEAPOT_TRANSFORM:crate::integer::Planar64Mat3=crate::integer::Planar64Mat3::int_from_cols_array([0,1,0, -1,0,0, 0,0,1]); pub fn unit_cylinder()->crate::model::IndexedModel{ let mut indexed_model=crate::model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap(),Color4::ONE).remove(0); for pos in indexed_model.unique_pos.iter_mut(){ - *pos=TEAPOT_TRANSFORM*(*pos); + *pos=TEAPOT_TRANSFORM*(*pos)/10; } indexed_model }