Manually add rand trait
This commit is contained in:
parent
8895654f92
commit
64fedca8e9
6 changed files with 63 additions and 0 deletions
|
@ -22,3 +22,4 @@ name = "cgmath"
|
|||
|
||||
[dependencies]
|
||||
rustc-serialize="*"
|
||||
rand="*"
|
||||
|
|
19
src/angle.rs
19
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<S> for Deg<S> {
|
|||
self.s.approx_eq_eps(&other.s, epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + PartialOrd + SampleRange + Rand> Rand for Rad<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Rad<S> {
|
||||
let angle: S = rng.gen_range(cast(-f64::consts::PI).unwrap(), cast(f64::consts::PI).unwrap());
|
||||
rad(angle)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + PartialOrd + SampleRange + Rand> Rand for Deg<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Deg<S> {
|
||||
let angle: S = rng.gen_range(cast(-180f64).unwrap(), cast(180f64).unwrap());
|
||||
deg(angle)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
//! These are provided for convenience.
|
||||
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate rand;
|
||||
|
||||
// Re-exports
|
||||
|
||||
|
|
|
@ -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<S: BaseNum> fmt::Debug for Matrix4<S> {
|
|||
self[3][0], self[3][1], self[3][2], self[3][3])
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + Rand> Rand for Matrix2<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Matrix2<S> {
|
||||
Matrix2{ x: rng.gen(), y: rng.gen() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + Rand> Rand for Matrix3<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Matrix3<S> {
|
||||
Matrix3{ x: rng.gen(), y: rng.gen(), z: rng.gen() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + Rand> Rand for Matrix4<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Matrix4<S> {
|
||||
Matrix4{ x: rng.gen(), y: rng.gen(), z: rng.gen(), w: rng.gen() }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<S: BaseFloat> Rotation3<S> for Quaternion<S> where S: 'static {
|
|||
c1 * s2 * c3 - s1 * c2 * s3)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + Rand> Rand for Quaternion<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> Quaternion<S> {
|
||||
Quaternion::from_sv(rng.gen(), rng.gen())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<S: BaseFloat + Rand> Rand for $Self_<S> {
|
||||
#[inline]
|
||||
fn rand<R: Rng>(rng: &mut R) -> $Self_<S> {
|
||||
$Self_ { $($field: rng.gen()),+ }
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in a new issue