From 8895654f926481ba74449821a444a608f09897cd Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 19 Feb 2015 14:28:47 +0100 Subject: [PATCH 1/2] Remove dependencies to rand and rand_macros Conflicts: src/cgmath.rs --- Cargo.toml | 2 -- src/angle.rs | 2 -- src/cgmath.rs | 2 -- src/matrix.rs | 3 --- src/quaternion.rs | 1 - src/vector.rs | 1 - 6 files changed, 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index be00d38..49950e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,3 @@ name = "cgmath" [dependencies] rustc-serialize="*" -rand_macros="*" -rand="*" diff --git a/src/angle.rs b/src/angle.rs index 9dc3f24..44c8919 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -24,11 +24,9 @@ use approx::ApproxEq; use num::{BaseFloat, One, one, Zero, zero}; /// An angle, in radians -#[derive_Rand] #[derive(Copy, Clone, PartialEq, PartialOrd, Hash, RustcEncodable, RustcDecodable)] pub struct Rad { pub s: S } /// An angle, in degrees -#[derive_Rand] #[derive(Copy, Clone, PartialEq, PartialOrd, Hash, RustcEncodable, RustcDecodable)] pub struct Deg { pub s: S } diff --git a/src/cgmath.rs b/src/cgmath.rs index f7cc6eb..15a041b 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -16,7 +16,6 @@ #![crate_type = "rlib"] #![crate_type = "dylib"] #![feature(old_impl_check, plugin, core, std_misc, custom_derive)] -#![plugin(rand_macros)] //! Computer graphics-centric math. //! @@ -33,7 +32,6 @@ //! 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 bfbdc50..b79acaa 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -30,17 +30,14 @@ use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; /// A 2 x 2, column major matrix -#[derive_Rand] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] pub struct Matrix2 { pub x: Vector2, pub y: Vector2 } /// A 3 x 3, column major matrix -#[derive_Rand] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 } /// A 4 x 4, column major matrix -#[derive_Rand] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } diff --git a/src/quaternion.rs b/src/quaternion.rs index 3267a66..e97755e 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -30,7 +30,6 @@ use vector::{Vector3, Vector, EuclideanVector}; /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. -#[derive_Rand] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] pub struct Quaternion { pub s: S, pub v: Vector3 } diff --git a/src/vector.rs b/src/vector.rs index 83c26fb..675bb08 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -190,7 +190,6 @@ pub trait Vector: Array1 + Zero + One + Neg { // Utility macro for generating associated functions for the vectors macro_rules! vec( ($Self_:ident <$S:ident> { $($field:ident),+ }, $n:expr, $constructor:ident) => ( - #[derive_Rand] #[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] pub struct $Self_ { $(pub $field: S),+ } From 64fedca8e9cb4bce4d15e48a5b385cc657d03822 Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Sat, 14 Mar 2015 22:53:57 -0400 Subject: [PATCH 2/2] 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()),+ } + } + } ) );