From a51744f86b0f01758237a739b599836d71155a17 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Tue, 22 May 2018 23:58:14 +0200 Subject: [PATCH] Update to rand 0.5. --- Cargo.toml | 6 +-- src/base/construction.rs | 24 ++++----- src/base/helper.rs | 10 ++-- src/geometry/isometry_construction.rs | 26 +++++----- src/geometry/orthographic.rs | 22 +++++---- src/geometry/perspective.rs | 18 ++++--- src/geometry/point_construction.rs | 12 +++-- src/geometry/quaternion_construction.rs | 28 ++++++----- src/geometry/rotation_specialization.rs | 59 ++++++++++++----------- src/geometry/similarity_construction.rs | 26 +++++----- src/geometry/translation_construction.rs | 18 ++++--- src/geometry/unit_complex_construction.rs | 12 +++-- 12 files changed, 150 insertions(+), 111 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a7f9bc8b..713910ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,11 +26,11 @@ debug = [ ] [dependencies] typenum = "1.10" generic-array = "0.11" -rand = { version = "0.4", default-features = false } +rand = { version = "0.5", default-features = false } num-traits = { version = "0.2", default-features = false } num-complex = { version = "0.2.0-git", git = "https://github.com/rust-num/num-complex", default-features = false } -approx = { version = "0.1", default-features = false } -alga = { version = "0.5", default-features = false } +approx = { version = "0.2", default-features = false } +alga = { version = "0.5", path = "../alga/alga", default-features = false } matrixmultiply = { version = "0.1", optional = true } serde = { version = "1.0", optional = true } serde_derive = { version = "1.0", optional = true } diff --git a/src/base/construction.rs b/src/base/construction.rs index c85d2d8f..d0e14cc7 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -4,7 +4,8 @@ use base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::{Bounded, One, Zero}; -use rand::{self, distributions::Sample, Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::{self, Rng}; use std::iter; use typenum::{self, Cmp, Greater}; @@ -231,14 +232,14 @@ where #[cfg(feature = "std")] pub fn new_random_generic(nrows: R, ncols: C) -> Self where - N: Rand, + Standard: Distribution, { Self::from_fn_generic(nrows, ncols, |_, _| rand::random()) } /// Creates a matrix filled with random values from the given distribution. #[inline] - pub fn from_distribution_generic + ?Sized, G: Rng>( + pub fn from_distribution_generic + ?Sized, G: Rng + ?Sized>( nrows: R, ncols: C, distribution: &mut Distr, @@ -371,7 +372,7 @@ macro_rules! impl_constructors( /// Creates a matrix filled with random values from the given distribution. #[inline] - pub fn from_distribution + ?Sized, G: Rng>( + pub fn from_distribution + ?Sized, G: Rng + ?Sized>( $($args: usize,)* distribution: &mut Distr, rng: &mut G, @@ -380,8 +381,10 @@ macro_rules! impl_constructors( } } - impl MatrixMN - where DefaultAllocator: Allocator { + impl MatrixMN + where + DefaultAllocator: Allocator, + Standard: Distribution { /// Creates a matrix filled with random values. #[inline] @@ -462,18 +465,17 @@ where } } -impl Rand for MatrixMN +impl Distribution> for Standard where DefaultAllocator: Allocator, + Standard: Distribution, { #[inline] - fn rand(rng: &mut G) -> Self { + fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> MatrixMN { let nrows = R::try_to_usize().unwrap_or(rng.gen_range(0, 10)); let ncols = C::try_to_usize().unwrap_or(rng.gen_range(0, 10)); - Self::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| { - rng.gen() - }) + MatrixMN::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| rng.gen()) } } diff --git a/src/base/helper.rs b/src/base/helper.rs index 0492dd6a..de601fb6 100644 --- a/src/base/helper.rs +++ b/src/base/helper.rs @@ -1,6 +1,7 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; /// Simple helper function for rejection sampling #[cfg(feature = "arbitrary")] @@ -16,7 +17,10 @@ pub fn reject bool, T: Arbitrary>(g: &mut G, f: F) -> T #[doc(hidden)] #[inline] -pub fn reject_rand bool, T: Rand>(g: &mut G, f: F) -> T { +pub fn reject_rand bool, T>(g: &mut G, f: F) -> T +where + Standard: Distribution, +{ use std::iter; - iter::repeat(()).map(|_| Rand::rand(g)).find(f).unwrap() + iter::repeat(()).map(|_| g.gen()).find(f).unwrap() } diff --git a/src/geometry/isometry_construction.rs b/src/geometry/isometry_construction.rs index 704b9e66..9d6c88b7 100644 --- a/src/geometry/isometry_construction.rs +++ b/src/geometry/isometry_construction.rs @@ -1,20 +1,23 @@ #[cfg(feature = "arbitrary")] -use quickcheck::{Arbitrary, Gen}; -#[cfg(feature = "arbitrary")] use base::storage::Owned; +#[cfg(feature = "arbitrary")] +use quickcheck::{Arbitrary, Gen}; use num::One; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::Real; use alga::linear::Rotation as AlgaRotation; -use base::{DefaultAllocator, Vector2, Vector3}; -use base::dimension::{DimName, U2, U3}; use base::allocator::Allocator; +use base::dimension::{DimName, U2, U3}; +use base::{DefaultAllocator, Vector2, Vector3}; -use geometry::{Isometry, Point, Point3, Rotation, Rotation2, Rotation3, Translation, UnitComplex, - UnitQuaternion}; +use geometry::{ + Isometry, Point, Point3, Rotation, Rotation2, Rotation3, Translation, UnitComplex, + UnitQuaternion, +}; impl>> Isometry where @@ -46,14 +49,15 @@ where } } -impl Rand for Isometry +impl Distribution> for Standard where - R: AlgaRotation> + Rand, + R: AlgaRotation>, + Standard: Distribution + Distribution, DefaultAllocator: Allocator, { #[inline] - fn rand(rng: &mut G) -> Self { - Self::from_parts(rng.gen(), rng.gen()) + fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Isometry { + Isometry::from_parts(rng.gen(), rng.gen()) } } diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index ee743b5b..75eb92ff 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -1,16 +1,17 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; #[cfg(feature = "serde-serialize")] use serde; use std::fmt; use alga::general::Real; -use base::{Matrix4, Vector, Vector3}; use base::dimension::U3; -use base::storage::Storage; use base::helper; +use base::storage::Storage; +use base::{Matrix4, Vector, Vector3}; use geometry::Point3; @@ -310,16 +311,19 @@ impl Orthographic3 { } } -impl Rand for Orthographic3 { - fn rand(r: &mut R) -> Self { - let left = Rand::rand(r); +impl Distribution> for Standard +where + Standard: Distribution, +{ + fn sample(&self, r: &mut R) -> Orthographic3 { + let left = r.gen(); let right = helper::reject_rand(r, |x: &N| *x > left); - let bottom = Rand::rand(r); + let bottom = r.gen(); let top = helper::reject_rand(r, |x: &N| *x > bottom); - let znear = Rand::rand(r); + let znear = r.gen(); let zfar = helper::reject_rand(r, |x: &N| *x > znear); - Self::new(left, right, bottom, top, znear, zfar) + Orthographic3::new(left, right, bottom, top, znear, zfar) } } diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index afed785d..933a7329 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -1,6 +1,7 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; #[cfg(feature = "serde-serialize")] use serde; @@ -8,10 +9,10 @@ use std::fmt; use alga::general::Real; -use base::{Matrix4, Scalar, Vector, Vector3}; use base::dimension::U3; -use base::storage::Storage; use base::helper; +use base::storage::Storage; +use base::{Matrix4, Scalar, Vector, Vector3}; use geometry::Point3; @@ -245,13 +246,16 @@ impl Perspective3 { } } -impl Rand for Perspective3 { - fn rand(r: &mut R) -> Self { - let znear = Rand::rand(r); +impl Distribution> for Standard +where + Standard: Distribution, +{ + fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3 { + let znear = r.gen(); let zfar = helper::reject_rand(r, |&x: &N| !(x - znear).is_zero()); let aspect = helper::reject_rand(r, |&x: &N| !x.is_zero()); - Self::new(aspect, Rand::rand(r), znear, zfar) + Perspective3::new(aspect, r.gen(), znear, zfar) } } diff --git a/src/geometry/point_construction.rs b/src/geometry/point_construction.rs index 3ffff968..8f06629f 100644 --- a/src/geometry/point_construction.rs +++ b/src/geometry/point_construction.rs @@ -1,13 +1,14 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::{Rand, Rng}; use num::{Bounded, One, Zero}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::ClosedDiv; -use base::{DefaultAllocator, Scalar, VectorN}; use base::allocator::Allocator; use base::dimension::{DimName, DimNameAdd, DimNameSum, U1, U2, U3, U4, U5, U6}; +use base::{DefaultAllocator, Scalar, VectorN}; use geometry::Point; @@ -70,13 +71,14 @@ where } } -impl Rand for Point +impl Distribution> for Standard where DefaultAllocator: Allocator, + Standard: Distribution, { #[inline] - fn rand(rng: &mut G) -> Self { - Point::from_coordinates(rng.gen()) + fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point { + Point::from_coordinates(rng.gen::>()) } } diff --git a/src/geometry/quaternion_construction.rs b/src/geometry/quaternion_construction.rs index 6e403b2d..e18c9948 100644 --- a/src/geometry/quaternion_construction.rs +++ b/src/geometry/quaternion_construction.rs @@ -1,18 +1,19 @@ #[cfg(feature = "arbitrary")] -use quickcheck::{Arbitrary, Gen}; +use base::dimension::U4; #[cfg(feature = "arbitrary")] use base::storage::Owned; #[cfg(feature = "arbitrary")] -use base::dimension::U4; +use quickcheck::{Arbitrary, Gen}; -use rand::{Rand, Rng}; use num::{One, Zero}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::Real; -use base::{Unit, Vector, Vector3, Vector4}; -use base::storage::Storage; use base::dimension::U3; +use base::storage::Storage; +use base::{Unit, Vector, Vector3, Vector4}; use geometry::{Quaternion, Rotation, UnitQuaternion}; @@ -86,9 +87,12 @@ impl Zero for Quaternion { } } -impl Rand for Quaternion { +impl Distribution> for Standard +where + Standard: Distribution, +{ #[inline] - fn rand(rng: &mut R) -> Self { + fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Quaternion { Quaternion::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) } } @@ -407,11 +411,13 @@ impl One for UnitQuaternion { } } -impl Rand for UnitQuaternion { +impl Distribution> for Standard +where + Standard: Distribution, +{ #[inline] - fn rand(rng: &mut R) -> Self { - let axisangle = Vector3::rand(rng); - UnitQuaternion::from_scaled_axis(axisangle) + fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> UnitQuaternion { + UnitQuaternion::from_scaled_axis(rng.gen::>() * N::two_pi()) } } diff --git a/src/geometry/rotation_specialization.rs b/src/geometry/rotation_specialization.rs index 9b6deb59..337883a3 100644 --- a/src/geometry/rotation_specialization.rs +++ b/src/geometry/rotation_specialization.rs @@ -1,16 +1,17 @@ #[cfg(feature = "arbitrary")] -use quickcheck::{Arbitrary, Gen}; -#[cfg(feature = "arbitrary")] use base::storage::Owned; +#[cfg(feature = "arbitrary")] +use quickcheck::{Arbitrary, Gen}; -use std::ops::Neg; -use num::Zero; -use rand::{Rand, Rng}; use alga::general::Real; +use num::Zero; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; +use std::ops::Neg; -use base::{MatrixN, Unit, Vector, Vector1, Vector3, VectorN}; use base::dimension::{U1, U2, U3}; use base::storage::Storage; +use base::{MatrixN, Unit, Vector, Vector1, Vector3, VectorN}; use geometry::{Rotation2, Rotation3, UnitComplex}; @@ -97,10 +98,13 @@ impl Rotation2 { } } -impl Rand for Rotation2 { +impl Distribution> for Standard +where + Standard: Distribution, +{ #[inline] - fn rand(rng: &mut R) -> Self { - Self::new(rng.gen()) + fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Rotation2 { + Rotation2::new(rng.gen()) } } @@ -195,15 +199,19 @@ impl Rotation3 { pub fn to_euler_angles(&self) -> (N, N, N) { // Implementation informed by "Computing Euler angles from a rotation matrix", by Gregory G. Slabaugh // http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.371.6578 - if self[(2,0)].abs() != N::one() { - let yaw = -self[(2,0)].asin(); - let roll = (self[(2,1)] / yaw.cos()).atan2(self[(2,2)] / yaw.cos()); - let pitch = (self[(1,0)] / yaw.cos()).atan2(self[(0,0)] / yaw.cos()); + if self[(2, 0)].abs() != N::one() { + let yaw = -self[(2, 0)].asin(); + let roll = (self[(2, 1)] / yaw.cos()).atan2(self[(2, 2)] / yaw.cos()); + let pitch = (self[(1, 0)] / yaw.cos()).atan2(self[(0, 0)] / yaw.cos()); (roll, yaw, pitch) - } else if self[(2,0)] == -N::one() { - (self[(0,1)].atan2(self[(0,2)]), N::frac_pi_2(), N::zero()) + } else if self[(2, 0)] == -N::one() { + (self[(0, 1)].atan2(self[(0, 2)]), N::frac_pi_2(), N::zero()) } else { - (-self[(0,1)].atan2(-self[(0,2)]), -N::frac_pi_2(), N::zero()) + ( + -self[(0, 1)].atan2(-self[(0, 2)]), + -N::frac_pi_2(), + N::zero(), + ) } } @@ -228,15 +236,7 @@ impl Rotation3 { let yaxis = zaxis.cross(&xaxis).normalize(); Self::from_matrix_unchecked(MatrixN::::new( - xaxis.x, - yaxis.x, - zaxis.x, - xaxis.y, - yaxis.y, - zaxis.y, - xaxis.z, - yaxis.z, - zaxis.z, + xaxis.x, yaxis.x, zaxis.x, xaxis.y, yaxis.y, zaxis.y, xaxis.z, yaxis.z, zaxis.z, )) } @@ -382,10 +382,13 @@ impl Rotation3 { } } -impl Rand for Rotation3 { +impl Distribution> for Standard +where + Standard: Distribution, +{ #[inline] - fn rand(rng: &mut R) -> Self { - Self::new(VectorN::rand(rng)) + fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> Rotation3 { + Rotation3::new(rng.gen::>() * N::two_pi()) } } diff --git a/src/geometry/similarity_construction.rs b/src/geometry/similarity_construction.rs index 30004eb2..0e74dd23 100644 --- a/src/geometry/similarity_construction.rs +++ b/src/geometry/similarity_construction.rs @@ -1,20 +1,23 @@ #[cfg(feature = "arbitrary")] -use quickcheck::{Arbitrary, Gen}; -#[cfg(feature = "arbitrary")] use base::storage::Owned; +#[cfg(feature = "arbitrary")] +use quickcheck::{Arbitrary, Gen}; use num::One; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::Real; use alga::linear::Rotation as AlgaRotation; -use base::{DefaultAllocator, Vector2, Vector3}; -use base::dimension::{DimName, U2, U3}; use base::allocator::Allocator; +use base::dimension::{DimName, U2, U3}; +use base::{DefaultAllocator, Vector2, Vector3}; -use geometry::{Isometry, Point, Point3, Rotation2, Rotation3, Similarity, Translation, - UnitComplex, UnitQuaternion}; +use geometry::{ + Isometry, Point, Point3, Rotation2, Rotation3, Similarity, Translation, UnitComplex, + UnitQuaternion, +}; impl Similarity where @@ -40,19 +43,20 @@ where } } -impl Rand for Similarity +impl Distribution> for Standard where - R: AlgaRotation> + Rand, + R: AlgaRotation>, DefaultAllocator: Allocator, + Standard: Distribution + Distribution, { #[inline] - fn rand(rng: &mut G) -> Self { + fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Similarity { let mut s = rng.gen(); while relative_eq!(s, N::zero()) { s = rng.gen() } - Self::from_isometry(rng.gen(), s) + Similarity::from_isometry(rng.gen(), s) } } diff --git a/src/geometry/translation_construction.rs b/src/geometry/translation_construction.rs index d867637c..df97872a 100644 --- a/src/geometry/translation_construction.rs +++ b/src/geometry/translation_construction.rs @@ -1,16 +1,17 @@ #[cfg(feature = "arbitrary")] -use quickcheck::{Arbitrary, Gen}; -#[cfg(feature = "arbitrary")] use base::storage::Owned; +#[cfg(feature = "arbitrary")] +use quickcheck::{Arbitrary, Gen}; use num::{One, Zero}; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::ClosedAdd; -use base::{DefaultAllocator, Scalar, VectorN}; -use base::dimension::{DimName, U1, U2, U3, U4, U5, U6}; use base::allocator::Allocator; +use base::dimension::{DimName, U1, U2, U3, U4, U5, U6}; +use base::{DefaultAllocator, Scalar, VectorN}; use geometry::Translation; @@ -35,13 +36,14 @@ where } } -impl Rand for Translation +impl Distribution> for Standard where DefaultAllocator: Allocator, + Standard: Distribution, { #[inline] - fn rand(rng: &mut G) -> Self { - Self::from_vector(rng.gen()) + fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Translation { + Translation::from_vector(rng.gen::>()) } } diff --git a/src/geometry/unit_complex_construction.rs b/src/geometry/unit_complex_construction.rs index f7f96311..ca4d6d21 100644 --- a/src/geometry/unit_complex_construction.rs +++ b/src/geometry/unit_complex_construction.rs @@ -3,7 +3,8 @@ use quickcheck::{Arbitrary, Gen}; use num::One; use num_complex::Complex; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use alga::general::Real; use base::allocator::Allocator; @@ -150,10 +151,13 @@ impl One for UnitComplex { } } -impl Rand for UnitComplex { +impl Distribution> for Standard +where + Standard: Distribution, +{ #[inline] - fn rand(rng: &mut R) -> Self { - UnitComplex::from_angle(N::rand(rng)) + fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex { + UnitComplex::from_angle(rng.gen() * N::two_pi()) } }