Merge pull request #188 from csherratt/master

Manually implment Rand.
This commit is contained in:
Colin Sherratt 2015-03-14 23:34:59 -04:00
commit 1aadf245e8
6 changed files with 61 additions and 9 deletions

View file

@ -22,5 +22,4 @@ name = "cgmath"
[dependencies]
rustc-serialize="*"
rand_macros="*"
rand="*"

View file

@ -20,15 +20,16 @@ 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};
/// An angle, in radians
#[derive_Rand]
#[derive(Copy, Clone, PartialEq, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub struct Rad<S> { pub s: S }
/// An angle, in degrees
#[derive_Rand]
#[derive(Copy, Clone, PartialEq, PartialOrd, Hash, RustcEncodable, RustcDecodable)]
pub struct Deg<S> { pub s: S }
@ -312,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)
}
}

View file

@ -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.
//!

View file

@ -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};
@ -30,17 +32,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<S> { pub x: Vector2<S>, pub y: Vector2<S> }
/// A 3 x 3, column major matrix
#[derive_Rand]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Matrix3<S> { pub x: Vector3<S>, pub y: Vector3<S>, pub z: Vector3<S> }
/// A 4 x 4, column major matrix
#[derive_Rand]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Matrix4<S> { pub x: Vector4<S>, pub y: Vector4<S>, pub z: Vector4<S>, pub w: Vector4<S> }
@ -1412,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() }
}
}

View file

@ -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,9 +30,9 @@ 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_Rand]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Quaternion<S> { pub s: S, pub v: Vector3<S> }
@ -443,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())
}
}

View file

@ -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};
@ -190,7 +192,6 @@ pub trait Vector<S: BaseNum>: Array1<S> + Zero + One + Neg<Output=Self> {
// 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_<S> { $(pub $field: S),+ }
@ -367,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()),+ }
}
}
)
);