Skip to content

Commit

Permalink
Implement approx and rand for new types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Feb 17, 2024
1 parent 33b4844 commit a92e8f5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 116 deletions.
12 changes: 10 additions & 2 deletions src/features/impl_approx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
Affine2, Affine3A, DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, Mat2,
Mat3, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4,
Mat2A, Mat3, Mat3A, Mat4, Mat4A, Quat, QuatA, Vec2, Vec3, Vec3A, Vec4, Vec4A,
};
use approx::{AbsDiffEq, RelativeEq, UlpsEq};

Expand Down Expand Up @@ -132,13 +132,17 @@ macro_rules! impl_approx_xzyw_axes {
}

impl_approx_as_ref!(f32, Mat2);
impl_approx_as_ref!(f32, Mat2A);
impl_approx_as_ref!(f32, Mat3);
impl_approx_as_ref!(f32, Mat4);
impl_approx_as_ref!(f32, Mat4A);
impl_approx_as_ref!(f32, Quat);
impl_approx_as_ref!(f32, QuatA);
impl_approx_as_ref!(f32, Vec2);
impl_approx_as_ref!(f32, Vec3);
impl_approx_as_ref!(f32, Vec4);
impl_approx_as_ref!(f32, Vec3A);
impl_approx_as_ref!(f32, Vec4);
impl_approx_as_ref!(f32, Vec4A);

impl_approx_xzy_axes!(f32, Affine2);
impl_approx_xzyw_axes!(f32, Affine3A);
Expand Down Expand Up @@ -194,11 +198,15 @@ mod test {
impl_approx_test!(f32, Vec3);
impl_approx_test!(f32, Vec3A);
impl_approx_test!(f32, Vec4);
impl_approx_test!(f32, Vec4A);
impl_approx_test!(f32, Quat, Quat::from_slice(&ONESF32));
impl_approx_test!(f32, QuatA, QuatA::from_slice(&ONESF32));
impl_approx_test!(f32, Mat2, Mat2::from_cols_slice(&ONESF32));
impl_approx_test!(f32, Mat2A, Mat2A::from_cols_slice(&ONESF32));
impl_approx_test!(f32, Mat3, Mat3::from_cols_slice(&ONESF32));
impl_approx_test!(f32, Mat3A, Mat3A::from_cols_slice(&ONESF32));
impl_approx_test!(f32, Mat4, Mat4::from_cols_slice(&ONESF32));
impl_approx_test!(f32, Mat4A, Mat4A::from_cols_slice(&ONESF32));

const ONESF64: [f64; 16] = [1.0; 16];
impl_approx_test!(f64, DVec2);
Expand Down
165 changes: 51 additions & 114 deletions src/features/impl_rand.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,49 @@
macro_rules! impl_vec_types {
($t:ty, $vec2:ident, $vec3:ident, $vec4:ident) => {
impl Distribution<$vec2> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec2 {
rng.gen::<[$t; 2]>().into()
}
}

impl Distribution<$vec3> for Standard {
macro_rules! impl_vec_type {
($test:ident, $t:ident, $vec:ident, $dim:literal) => {
impl Distribution<$vec> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec3 {
rng.gen::<[$t; 3]>().into()
}
}

impl Distribution<$vec4> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec4 {
rng.gen::<[$t; 4]>().into()
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $vec {
rng.gen::<[$t; $dim]>().into()
}
}

#[test]
fn test_vec2_rand() {
fn $test() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a: ($t, $t) = rng1.gen();
let a: [$t; $dim] = rng1.gen();
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b: $vec2 = rng2.gen();
assert_eq!(a, b.into());
let b: $vec = rng2.gen();
assert_eq!(a, Into::<[$t; $dim]>::into(b));
}
}
}

#[test]
fn test_vec3_rand() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a: ($t, $t, $t) = rng1.gen();
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b: $vec3 = rng2.gen();
assert_eq!(a, b.into());
macro_rules! impl_mat_type {
($test:ident, $t:ident, $mat:ident, $n:literal) => {
impl Distribution<$mat> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat {
$mat::from_cols_array(&rng.gen())
}
}

#[test]
fn test_vec4_rand() {
fn $test() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a: ($t, $t, $t, $t) = rng1.gen();
let a = $mat::from_cols_array(&rng1.gen::<[$t; $n]>());
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b: $vec4 = rng2.gen();
assert_eq!(a, b.into());
let b = rng2.gen::<$mat>();
assert_eq!(a, b);
}
};
}
}

macro_rules! impl_float_types {
($t:ident, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
impl_vec_types!($t, $vec2, $vec3, $vec4);

impl Distribution<$mat2> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat2 {
$mat2::from_cols_array(&rng.gen())
}
}

impl Distribution<$mat3> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat3 {
$mat3::from_cols_array(&rng.gen())
}
}

impl Distribution<$mat4> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $mat4 {
$mat4::from_cols_array(&rng.gen())
}
}

macro_rules! impl_quat_type {
($test:ident, $t:ident, $quat:ident) => {
impl Distribution<$quat> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $quat {
Expand All @@ -92,40 +55,7 @@ macro_rules! impl_float_types {
}

#[test]
fn test_mat2_rand() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a = $mat2::from_cols_array(&rng1.gen::<[$t; 4]>());
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b = rng2.gen::<$mat2>();
assert_eq!(a, b);
}

#[test]
fn test_mat3_rand() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a = $mat3::from_cols_array(&rng1.gen::<[$t; 9]>());
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b = rng2.gen::<$mat3>();
assert_eq!(a, b);
}

#[test]
fn test_mat4_rand() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a = $mat4::from_cols_array(&rng1.gen::<[$t; 16]>());
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b = rng2.gen::<$mat4>();
assert_eq!(a, b);
}

#[test]
fn test_quat_rand() {
fn $test() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
Expand All @@ -135,11 +65,29 @@ macro_rules! impl_float_types {
let b: $quat = rng2.gen();
assert_eq!(a, b);
}
}
}

macro_rules! impl_vec_types {
($t:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
impl_vec_type!(test_vec2_rand, $t, $vec2, 2);
impl_vec_type!(test_vec3_rand, $t, $vec3, 3);
impl_vec_type!(test_vec4_rand, $t, $vec4, 4);
};
}

macro_rules! impl_float_types {
($t:ident, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
impl_vec_types!($t, $vec2, $vec3, $vec4);
impl_mat_type!(test_mat2_rand, $t, $mat2, 4);
impl_mat_type!(test_mat3_rand, $t, $mat3, 9);
impl_mat_type!(test_mat4_rand, $t, $mat4, 16);
impl_quat_type!(test_quat_rand, $t, $quat);
};
}

mod f32 {
use crate::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A, Vec4};
use crate::{Mat2, Mat2A, Mat3, Mat3A, Mat4, Mat4A, Quat, QuatA, Vec2, Vec3, Vec3A, Vec4, Vec4A};
use core::f32::consts::PI;
use rand::{
distributions::{Distribution, Standard},
Expand All @@ -148,23 +96,12 @@ mod f32 {

impl_float_types!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4);

impl Distribution<Vec3A> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec3A {
rng.gen::<[f32; 3]>().into()
}
}

#[test]
fn test_vec3a_rand() {
use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256Plus;
let mut rng1 = Xoshiro256Plus::seed_from_u64(0);
let a: (f32, f32, f32) = rng1.gen();
let mut rng2 = Xoshiro256Plus::seed_from_u64(0);
let b: Vec3A = rng2.gen();
assert_eq!(a, b.into());
}
impl_vec_type!(test_vec3a_rand, f32, Vec3A, 3);
impl_vec_type!(test_vec4a_rand, f32, Vec4A, 4);
impl_mat_type!(test_mat2a_rand, f32, Mat2A, 4);
impl_mat_type!(test_mat3a_rand, f32, Mat3A, 9);
impl_mat_type!(test_mat4a_rand, f32, Mat4A, 16);
impl_quat_type!(test_quata_rand, f32, QuatA);
}

mod f64 {
Expand Down

0 comments on commit a92e8f5

Please sign in to comment.