From 64fedca8e9cb4bce4d15e48a5b385cc657d03822 Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Sat, 14 Mar 2015 22:53:57 -0400 Subject: [PATCH] Manually add rand trait --- Cargo.toml | 1 + src/angle.rs | 19 +++++++++++++++++++ src/cgmath.rs | 1 + src/matrix.rs | 23 +++++++++++++++++++++++ src/quaternion.rs | 10 ++++++++++ src/vector.rs | 9 +++++++++ 6 files changed, 63 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 49950e6..2a372f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ name = "cgmath" [dependencies] rustc-serialize="*" +rand="*" diff --git a/src/angle.rs b/src/angle.rs index 44c8919..eacd671 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -20,6 +20,9 @@ use std::f64; use std::num::{cast, Float}; use std::ops::*; +use rand::{Rand, Rng}; +use rand::distributions::range::SampleRange; + use approx::ApproxEq; use num::{BaseFloat, One, one, Zero, zero}; @@ -310,3 +313,19 @@ ApproxEq for Deg { self.s.approx_eq_eps(&other.s, epsilon) } } + +impl Rand for Rad { + #[inline] + fn rand(rng: &mut R) -> Rad { + let angle: S = rng.gen_range(cast(-f64::consts::PI).unwrap(), cast(f64::consts::PI).unwrap()); + rad(angle) + } +} + +impl Rand for Deg { + #[inline] + fn rand(rng: &mut R) -> Deg { + let angle: S = rng.gen_range(cast(-180f64).unwrap(), cast(180f64).unwrap()); + deg(angle) + } +} diff --git a/src/cgmath.rs b/src/cgmath.rs index 15a041b..c8335da 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -32,6 +32,7 @@ //! These are provided for convenience. extern crate "rustc-serialize" as rustc_serialize; +extern crate rand; // Re-exports diff --git a/src/matrix.rs b/src/matrix.rs index b79acaa..18a1b66 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -20,6 +20,8 @@ use std::mem; use std::num::cast; use std::ops::*; +use rand::{Rand, Rng}; + use angle::{Rad, sin, cos, sin_cos}; use approx::ApproxEq; use array::{Array1, Array2, FixedArray}; @@ -1409,3 +1411,24 @@ impl fmt::Debug for Matrix4 { self[3][0], self[3][1], self[3][2], self[3][3]) } } + +impl Rand for Matrix2 { + #[inline] + fn rand(rng: &mut R) -> Matrix2 { + Matrix2{ x: rng.gen(), y: rng.gen() } + } +} + +impl Rand for Matrix3 { + #[inline] + fn rand(rng: &mut R) -> Matrix3 { + Matrix3{ x: rng.gen(), y: rng.gen(), z: rng.gen() } + } +} + +impl Rand for Matrix4 { + #[inline] + fn rand(rng: &mut R) -> Matrix4 { + Matrix4{ x: rng.gen(), y: rng.gen(), z: rng.gen(), w: rng.gen() } + } +} diff --git a/src/quaternion.rs b/src/quaternion.rs index e97755e..bcc2d26 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -19,6 +19,8 @@ use std::f64; use std::num::{cast, Float}; use std::ops::*; +use rand::{Rand, Rng}; + use angle::{Angle, Rad, acos, sin, sin_cos, rad}; use approx::ApproxEq; use array::Array1; @@ -28,6 +30,7 @@ use point::Point3; use rotation::{Rotation, Rotation3, Basis3, ToBasis3}; use vector::{Vector3, Vector, EuclideanVector}; + /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] @@ -442,3 +445,10 @@ impl Rotation3 for Quaternion where S: 'static { c1 * s2 * c3 - s1 * c2 * s3) } } + +impl Rand for Quaternion { + #[inline] + fn rand(rng: &mut R) -> Quaternion { + Quaternion::from_sv(rng.gen(), rng.gen()) + } +} diff --git a/src/vector.rs b/src/vector.rs index 675bb08..897d49f 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -101,6 +101,8 @@ use std::mem; use std::num::NumCast; use std::ops::*; +use rand::{Rand, Rng}; + use angle::{Rad, atan2, acos}; use approx::ApproxEq; use array::{Array1, FixedArray}; @@ -366,6 +368,13 @@ macro_rules! vec( $(self.$field.approx_eq_eps(&other.$field, epsilon))&&+ } } + + impl Rand for $Self_ { + #[inline] + fn rand(rng: &mut R) -> $Self_ { + $Self_ { $($field: rng.gen()),+ } + } + } ) );