diff --git a/src/dim_macros.rs b/src/dim_macros.rs index 55bd13f..8584b75 100644 --- a/src/dim_macros.rs +++ b/src/dim_macros.rs @@ -189,11 +189,11 @@ macro_rules! impl_dimensional_fns( macro_rules! impl_swap( ($Self:ident) => ( - impl $Self { + impl $Self { #[inline] pub fn swap(&mut self, a: uint, b: uint) { - let tmp = copy *self.index(a); - *self.index_mut(a) = copy *self.index(b); + let tmp = self.index(a).clone(); + *self.index_mut(a) = self.index(b).clone(); *self.index_mut(b) = tmp; } } @@ -202,7 +202,7 @@ macro_rules! impl_swap( macro_rules! impl_approx( ($Self:ident) => ( - impl> ApproxEq for $Self { + impl> ApproxEq for $Self { #[inline] pub fn approx_epsilon() -> T { ApproxEq::approx_epsilon::() @@ -215,8 +215,7 @@ macro_rules! impl_approx( #[inline] pub fn approx_eq_eps(&self, other: &$Self, epsilon: &T) -> bool { - - self.zip(other, |a, b| a.approx_eq_eps(b, epsilon)).all(|&x| x) + self.zip(other, |a, b| a.approx_eq_eps(b, epsilon)).iter().all(|&x| x) } } ) diff --git a/src/frustum.rs b/src/frustum.rs index 77f8b28..2ea28f7 100644 --- a/src/frustum.rs +++ b/src/frustum.rs @@ -19,7 +19,7 @@ use point::Point3; mod num_macros; -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Frustum { left: Plane, right: Plane, @@ -29,7 +29,7 @@ pub struct Frustum { far: Plane, } -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct FrustumPoints { near_top_left: Point3, near_top_right: Point3, @@ -41,7 +41,7 @@ pub struct FrustumPoints { far_bottom_right: Point3, } -impl Frustum { +impl Frustum { /// Constructs a frustum pub fn from_planes(left: Plane, right: Plane, bottom: Plane, top: Plane, @@ -80,7 +80,7 @@ impl Frustum { } } -impl> Frustum { +impl> Frustum { /// Computes where the frustum planes intersect to form corners and returns /// a struct containing the eight resulting position vectors. pub fn to_points(&self) -> FrustumPoints { @@ -97,7 +97,7 @@ impl> Frustum { } } -impl> ApproxEq for Frustum { +impl> ApproxEq for Frustum { #[inline] pub fn approx_epsilon() -> T { ApproxEq::approx_epsilon::() diff --git a/src/mat.rs b/src/mat.rs index 435238e..3f13ad8 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -22,7 +22,7 @@ mod num_macros; mod dim_macros; mod mat_macros; -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Mat2 { x: Vec2, y: Vec2, @@ -42,7 +42,7 @@ impl_dimensional_fns!(Mat2, Vec2, 2) impl_approx!(Mat2) impl_mat!(Mat2, Vec2) -impl_mat_copyable!(Mat2, Vec2) +impl_mat_clonable!(Mat2, Vec2) impl_mat_numeric!(Mat2, Vec2) impl_mat_approx_numeric!(Mat2) impl_mat_neg!(Mat2) @@ -66,33 +66,33 @@ impl Mat2 { } } -impl ToMat3 for Mat2 { +impl ToMat3 for Mat2 { #[inline] pub fn to_mat3(&self) -> Mat3 { - Mat3::new(copy *self.elem(0, 0), copy *self.elem(0, 1), zero!(T), - copy *self.elem(1, 0), copy *self.elem(1, 1), zero!(T), + Mat3::new(self.elem(0, 0).clone(), self.elem(0, 1).clone(), zero!(T), + self.elem(1, 0).clone(), self.elem(1, 1).clone(), zero!(T), zero!(T), zero!(T), one!(T)) } } -impl ToMat4 for Mat2 { +impl ToMat4 for Mat2 { #[inline] pub fn to_mat4(&self) -> Mat4 { - Mat4::new(copy *self.elem(0, 0), copy *self.elem(0, 1), zero!(T), zero!(T), - copy *self.elem(1, 0), copy *self.elem(1, 1), zero!(T), zero!(T), + Mat4::new(self.elem(0, 0).clone(), self.elem(0, 1).clone(), zero!(T), zero!(T), + self.elem(1, 0).clone(), self.elem(1, 1).clone(), zero!(T), zero!(T), zero!(T), zero!(T), one!(T), zero!(T), zero!(T), zero!(T), zero!(T), one!(T)) } } -impl Mat2 { +impl Mat2 { #[inline] pub fn from_angle(radians: T) -> Mat2 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat2::new(copy cos_theta, copy -sin_theta, - copy sin_theta, copy cos_theta) + Mat2::new(cos_theta.clone(), -sin_theta.clone(), + sin_theta.clone(), cos_theta.clone()) } } @@ -265,7 +265,7 @@ mod mat2_tests{ } } -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Mat3 { x: Vec3, y: Vec3, @@ -286,7 +286,7 @@ impl_dimensional_fns!(Mat3, Vec3, 3) impl_approx!(Mat3) impl_mat!(Mat3, Vec3) -impl_mat_copyable!(Mat3, Vec3) +impl_mat_clonable!(Mat3, Vec3) impl_mat_numeric!(Mat3, Vec3) impl_mat_approx_numeric!(Mat3) impl_mat_neg!(Mat3) @@ -313,17 +313,17 @@ impl Mat3 { } } -impl ToMat4 for Mat3 { +impl ToMat4 for Mat3 { #[inline] pub fn to_mat4(&self) -> Mat4 { - Mat4::new(copy *self.elem(0, 0), copy *self.elem(0, 1), copy *self.elem(0, 2), zero!(T), - copy *self.elem(1, 0), copy *self.elem(1, 1), copy *self.elem(1, 2), zero!(T), - copy *self.elem(2, 0), copy *self.elem(2, 1), copy *self.elem(2, 2), zero!(T), + Mat4::new(self.elem(0, 0).clone(), self.elem(0, 1).clone(), self.elem(0, 2).clone(), zero!(T), + self.elem(1, 0).clone(), self.elem(1, 1).clone(), self.elem(1, 2).clone(), zero!(T), + self.elem(2, 0).clone(), self.elem(2, 1).clone(), self.elem(2, 2).clone(), zero!(T), zero!(T), zero!(T), zero!(T), one!(T)) } } -impl Mat3 { +impl Mat3 { /// Construct a matrix from an angular rotation around the `x` axis pub fn from_angle_x(radians: T) -> Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations @@ -331,8 +331,8 @@ impl Mat3 { let sin_theta = radians.sin(); Mat3::new(one!(T), zero!(T), zero!(T), - zero!(T), copy cos_theta, copy sin_theta, - zero!(T), copy -sin_theta, copy cos_theta) + zero!(T), cos_theta.clone(), sin_theta.clone(), + zero!(T), -sin_theta.clone(), cos_theta.clone()) } /// Construct a matrix from an angular rotation around the `y` axis @@ -341,9 +341,9 @@ impl Mat3 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat3::new(copy cos_theta, zero!(T), copy -sin_theta, + Mat3::new(cos_theta.clone(), zero!(T), -sin_theta.clone(), zero!(T), one!(T), zero!(T), - copy sin_theta, zero!(T), copy cos_theta) + sin_theta.clone(), zero!(T), cos_theta.clone()) } /// Construct a matrix from an angular rotation around the `z` axis @@ -352,8 +352,8 @@ impl Mat3 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat3::new(copy cos_theta, copy sin_theta, zero!(T), - copy -sin_theta, copy cos_theta, zero!(T), + Mat3::new(cos_theta.clone(), sin_theta.clone(), zero!(T), + -sin_theta.clone(), cos_theta.clone(), zero!(T), zero!(T), zero!(T), one!(T)) } @@ -384,9 +384,9 @@ impl Mat3 { let s = radians.sin(); let _1_c = one!(T) - c; - let x = copy axis.x; - let y = copy axis.y; - let z = copy axis.z; + let x = axis.x.clone(); + let y = axis.y.clone(); + let z = axis.z.clone(); Mat3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y, _1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x, @@ -407,7 +407,7 @@ impl Mat3 { } } -impl ToQuat for Mat3 { +impl ToQuat for Mat3 { /// Convert the matrix to a quaternion pub fn to_quat(&self) -> Quat { // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's @@ -663,7 +663,7 @@ mod mat3_tests{ } } -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Mat4 { x: Vec4, y: Vec4, @@ -685,7 +685,7 @@ impl_dimensional_fns!(Mat4, Vec4, 4) impl_approx!(Mat4) impl_mat!(Mat4, Vec4) -impl_mat_copyable!(Mat4, Vec4) +impl_mat_clonable!(Mat4, Vec4) impl_mat_numeric!(Mat4, Vec4) impl_mat_approx_numeric!(Mat4) impl_mat_neg!(Mat4) diff --git a/src/mat_macros.rs b/src/mat_macros.rs index 9717a72..91265b5 100644 --- a/src/mat_macros.rs +++ b/src/mat_macros.rs @@ -41,18 +41,18 @@ macro_rules! impl_mat( ) ) -macro_rules! impl_mat_copyable( +macro_rules! impl_mat_clonable( ($Mat:ident, $Vec:ident) => ( - impl $Mat { + impl $Mat { #[inline] pub fn row(&self, i: uint) -> $Vec { - $Vec::from_slice(self.map(|c| copy *c.index(i))) + $Vec::from_slice(self.map(|c| c.index(i).clone())) } #[inline] pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = copy *self.col(a); - *self.col_mut(a) = copy *self.col(b); + let tmp = self.col(a).clone(); + *self.col_mut(a) = self.col(b).clone(); *self.col_mut(b) = tmp; } @@ -63,8 +63,8 @@ macro_rules! impl_mat_copyable( #[inline] pub fn swap_elem(&mut self, (ai, aj): (uint, uint), (bi, bj): (uint, uint)) { - let tmp = copy *self.elem(ai, aj); - *self.elem_mut(ai, aj) = copy *self.elem(bi, bj); + let tmp = self.elem(ai, aj).clone(); + *self.elem_mut(ai, aj) = self.elem(bi, bj).clone(); *self.elem_mut(bi, bj) = tmp; } @@ -77,19 +77,19 @@ macro_rules! impl_mat_copyable( macro_rules! mat_transpose( (Mat2) => ( - Mat2::new(copy *self.elem(0, 0), copy *self.elem(1, 0), - copy *self.elem(0, 1), copy *self.elem(1, 1)) + Mat2::new(self.elem(0, 0).clone(), self.elem(1, 0).clone(), + self.elem(0, 1).clone(), self.elem(1, 1).clone()) ); (Mat3) => ( - Mat3::new(copy *self.elem(0, 0), copy *self.elem(1, 0), copy *self.elem(2, 0), - copy *self.elem(0, 1), copy *self.elem(1, 1), copy *self.elem(2, 1), - copy *self.elem(0, 2), copy *self.elem(1, 2), copy *self.elem(2, 2)) + Mat3::new(self.elem(0, 0).clone(), self.elem(1, 0).clone(), self.elem(2, 0).clone(), + self.elem(0, 1).clone(), self.elem(1, 1).clone(), self.elem(2, 1).clone(), + self.elem(0, 2).clone(), self.elem(1, 2).clone(), self.elem(2, 2).clone()) ); (Mat4) => ( - Mat4::new(copy *self.elem(0, 0), copy *self.elem(1, 0), copy *self.elem(2, 0), copy *self.elem(3, 0), - copy *self.elem(0, 1), copy *self.elem(1, 1), copy *self.elem(2, 1), copy *self.elem(3, 1), - copy *self.elem(0, 2), copy *self.elem(1, 2), copy *self.elem(2, 2), copy *self.elem(3, 2), - copy *self.elem(0, 3), copy *self.elem(1, 3), copy *self.elem(2, 3), copy *self.elem(3, 3)) + Mat4::new(self.elem(0, 0).clone(), self.elem(1, 0).clone(), self.elem(2, 0).clone(), self.elem(3, 0).clone(), + self.elem(0, 1).clone(), self.elem(1, 1).clone(), self.elem(2, 1).clone(), self.elem(3, 1).clone(), + self.elem(0, 2).clone(), self.elem(1, 2).clone(), self.elem(2, 2).clone(), self.elem(3, 2).clone(), + self.elem(0, 3).clone(), self.elem(1, 3).clone(), self.elem(2, 3).clone(), self.elem(3, 3).clone()) ); ) @@ -129,7 +129,7 @@ macro_rules! mat_transpose_self( macro_rules! impl_mat_numeric( ($Mat:ident, $Vec:ident) => ( - impl $Mat { + impl $Mat { #[inline] pub fn from_value(value: T) -> $Mat { mat_from_value!($Mat) } @@ -141,7 +141,7 @@ macro_rules! impl_mat_numeric( #[inline] pub fn mul_t(&self, value: T) -> $Mat { - $Mat::from_slice(self.map(|&c| c.mul_t(copy value))) + $Mat::from_slice(self.map(|&c| c.mul_t(value.clone()))) } #[inline] @@ -166,7 +166,7 @@ macro_rules! impl_mat_numeric( #[inline] pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| x.mul_self_t(copy value)) + self.map_mut(|x| x.mul_self_t(value.clone())) } #[inline] @@ -206,19 +206,19 @@ macro_rules! impl_mat_numeric( macro_rules! mat_from_value( (Mat2) => ( - Mat2::new(copy value, zero!(T), - zero!(T), copy value) + Mat2::new(value.clone(), zero!(T), + zero!(T), value.clone()) ); (Mat3) => ( - Mat3::new(copy value, zero!(T), zero!(T), - zero!(T), copy value, zero!(T), - zero!(T), zero!(T), copy value) + Mat3::new(value.clone(), zero!(T), zero!(T), + zero!(T), value.clone(), zero!(T), + zero!(T), zero!(T), value.clone()) ); (Mat4) => ( - Mat4::new(copy value, zero!(T), zero!(T), zero!(T), - zero!(T), copy value, zero!(T), zero!(T), - zero!(T), zero!(T), copy value, zero!(T), - zero!(T), zero!(T), zero!(T), copy value) + Mat4::new(value.clone(), zero!(T), zero!(T), zero!(T), + zero!(T), value.clone(), zero!(T), zero!(T), + zero!(T), zero!(T), value.clone(), zero!(T), + zero!(T), zero!(T), zero!(T), value.clone()) ); ) @@ -292,18 +292,18 @@ macro_rules! mat_determinant( self.col(0).dot(&self.col(1).cross(self.col(2))) ); (Mat4) => ({ - let m0 = Mat3::new(copy *self.elem(1, 1), copy *self.elem(2, 1), copy *self.elem(3, 1), - copy *self.elem(1, 2), copy *self.elem(2, 2), copy *self.elem(3, 2), - copy *self.elem(1, 3), copy *self.elem(2, 3), copy *self.elem(3, 3)); - let m1 = Mat3::new(copy *self.elem(0, 1), copy *self.elem(2, 1), copy *self.elem(3, 1), - copy *self.elem(0, 2), copy *self.elem(2, 2), copy *self.elem(3, 2), - copy *self.elem(0, 3), copy *self.elem(2, 3), copy *self.elem(3, 3)); - let m2 = Mat3::new(copy *self.elem(0, 1), copy *self.elem(1, 1), copy *self.elem(3, 1), - copy *self.elem(0, 2), copy *self.elem(1, 2), copy *self.elem(3, 2), - copy *self.elem(0, 3), copy *self.elem(1, 3), copy *self.elem(3, 3)); - let m3 = Mat3::new(copy *self.elem(0, 1), copy *self.elem(1, 1), copy *self.elem(2, 1), - copy *self.elem(0, 2), copy *self.elem(1, 2), copy *self.elem(2, 2), - copy *self.elem(0, 3), copy *self.elem(1, 3), copy *self.elem(2, 3)); + let m0 = Mat3::new(self.elem(1, 1).clone(), self.elem(2, 1).clone(), self.elem(3, 1).clone(), + self.elem(1, 2).clone(), self.elem(2, 2).clone(), self.elem(3, 2).clone(), + self.elem(1, 3).clone(), self.elem(2, 3).clone(), self.elem(3, 3).clone()); + let m1 = Mat3::new(self.elem(0, 1).clone(), self.elem(2, 1).clone(), self.elem(3, 1).clone(), + self.elem(0, 2).clone(), self.elem(2, 2).clone(), self.elem(3, 2).clone(), + self.elem(0, 3).clone(), self.elem(2, 3).clone(), self.elem(3, 3).clone()); + let m2 = Mat3::new(self.elem(0, 1).clone(), self.elem(1, 1).clone(), self.elem(3, 1).clone(), + self.elem(0, 2).clone(), self.elem(1, 2).clone(), self.elem(3, 2).clone(), + self.elem(0, 3).clone(), self.elem(1, 3).clone(), self.elem(3, 3).clone()); + let m3 = Mat3::new(self.elem(0, 1).clone(), self.elem(1, 1).clone(), self.elem(2, 1).clone(), + self.elem(0, 2).clone(), self.elem(1, 2).clone(), self.elem(2, 2).clone(), + self.elem(0, 3).clone(), self.elem(1, 3).clone(), self.elem(2, 3).clone()); self.elem(0, 0) * m0.determinant() - self.elem(1, 0) * m1.determinant() + @@ -320,7 +320,7 @@ macro_rules! mat_trace( macro_rules! impl_mat_approx_numeric( ($Mat:ident) => ( - impl> $Mat { + impl> $Mat { #[inline] pub fn inverse(&self) -> Option<$Mat> { mat_inverse!($Mat) @@ -374,9 +374,9 @@ macro_rules! mat_inverse( if d.approx_eq(&zero!(T)) { None } else { - Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(copy d), - self.col(2).cross(self.col(0)).div_t(copy d), - self.col(0).cross(self.col(1)).div_t(copy d)).transpose()) + Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(d.clone()), + self.col(2).cross(self.col(0)).div_t(d.clone()), + self.col(0).cross(self.col(1)).div_t(d.clone())).transpose()) } }); (Mat4) => ({ @@ -390,7 +390,7 @@ macro_rules! mat_inverse( // So take this matrix, A, augmented with the identity // and essentially reduce [A|I] - let mut A = copy *self; + let mut A = self.clone(); let mut I = Mat4::identity::(); for uint::range(0, 4) |j| { @@ -408,16 +408,16 @@ macro_rules! mat_inverse( I.swap_cols(i1, j); // Scale col j to have a unit diagonal - let ajj = copy *A.elem(j, j); - I.col_mut(j).div_self_t(copy ajj); - A.col_mut(j).div_self_t(copy ajj); + let ajj = A.elem(j, j).clone(); + I.col_mut(j).div_self_t(ajj.clone()); + A.col_mut(j).div_self_t(ajj.clone()); // Eliminate off-diagonal elems in col j of A, // doing identical ops to I for uint::range(0, 4) |i| { if i != j { - let ij_mul_aij = I.col(j).mul_t(copy *A.elem(i, j)); - let aj_mul_aij = A.col(j).mul_t(copy *A.elem(i, j)); + let ij_mul_aij = I.col(j).mul_t(A.elem(i, j).clone()); + let aj_mul_aij = A.col(j).mul_t(A.elem(i, j).clone()); I.col_mut(i).sub_self_v(&ij_mul_aij); A.col_mut(i).sub_self_v(&aj_mul_aij); } @@ -498,7 +498,7 @@ macro_rules! mat_is_symmetric( macro_rules! impl_mat_neg( ($Mat:ident) => ( - impl Neg<$Mat> for $Mat { + impl Neg<$Mat> for $Mat { #[inline] pub fn neg(&self) -> $Mat { $Mat::from_slice(self.map(|&x| -x)) diff --git a/src/plane.rs b/src/plane.rs index 274213b..eb45371 100644 --- a/src/plane.rs +++ b/src/plane.rs @@ -31,7 +31,7 @@ mod dim_macros; /// - `n.y`: corresponds to `B` in the plane equation /// - `n.z`: corresponds to `C` in the plane equation /// - `d`: the distance value, corresponding to `D` in the plane equation -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Plane { norm: Vec3, dist: T, @@ -42,7 +42,7 @@ impl_dimensional_fns!(Plane, T, 4) impl_swap!(Plane) impl_approx!(Plane) -impl Plane { +impl Plane { /// # Arguments /// /// - `a`: the `x` component of the normal @@ -63,7 +63,7 @@ impl Plane { /// Construct a plane from the components of a four-dimensional vector pub fn from_vec4(vec: Vec4) -> Plane { - Plane::from_abcd(copy vec.x, copy vec.y, copy vec.z, copy vec.w) + Plane::from_abcd(vec.x.clone(), vec.y.clone(), vec.z.clone(), vec.w.clone()) } /// Compute the distance from the plane to the point @@ -87,7 +87,7 @@ impl Plane { } } -impl> Plane { +impl> Plane { /// Constructs a plane that passes through the the three points `a`, `b` and `c` pub fn from_3p(a: Point3, b: Point3, @@ -123,10 +123,10 @@ impl> Plane { } else { // The end-point of the ray is at the three-plane intersection between // `self`, `other`, and a tempory plane positioned at the origin - do Plane::from_nd(copy ray_dir, zero!(T)).intersection_3pl(self, other).map |ray_pos| { + do Plane::from_nd(ray_dir.clone(), zero!(T)).intersection_3pl(self, other).map |ray_pos| { Ray3 { - pos: copy *ray_pos, - dir: copy ray_dir, + pos: ray_pos.clone(), + dir: ray_dir.clone(), } } } @@ -140,11 +140,13 @@ impl> Plane { /// - `None`: No valid intersection was found. The normals of the three /// planes are probably coplanar. pub fn intersection_3pl(&self, other_a: &Plane, other_b: &Plane) -> Option> { - let mx = Mat3::new(copy self.norm.x, copy other_a.norm.x, copy other_b.norm.x, - copy self.norm.y, copy other_a.norm.y, copy other_b.norm.y, - copy self.norm.z, copy other_a.norm.z, copy other_b.norm.z); + let mx = Mat3::new(self.norm.x.clone(), other_a.norm.x.clone(), other_b.norm.x.clone(), + self.norm.y.clone(), other_a.norm.y.clone(), other_b.norm.y.clone(), + self.norm.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone()); do mx.inverse().map |m| { - Point3(m.mul_v(&Vec3::new(copy self.dist, copy other_a.dist, copy other_b.dist))) + Point3(m.mul_v(&Vec3::new(self.dist.clone(), + other_a.dist.clone(), + other_b.dist.clone()))) } } } diff --git a/src/point.rs b/src/point.rs index f8a3a43..b223b66 100644 --- a/src/point.rs +++ b/src/point.rs @@ -25,7 +25,7 @@ pub trait Point: Eq + ApproxEq + ToStr { } /// A two-dimensional point -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Point2(Vec2); impl_dimensional!(Point2, T, 2) @@ -38,7 +38,7 @@ impl Point2 { } } -impl Point> for Point2 { +impl Point> for Point2 { pub fn translate(&self, offset: &Vec2) -> Point2 { Point2(self.add_v(offset)) } @@ -55,7 +55,7 @@ impl ToStr for Point2 { } /// A three-dimensional point -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Point3(Vec3); impl_dimensional!(Point3, T, 3) @@ -68,7 +68,7 @@ impl Point3 { } } -impl Point> for Point3 { +impl Point> for Point3 { pub fn translate(&self, offset: &Vec3) -> Point3 { Point3(self.add_v(offset)) } diff --git a/src/projection.rs b/src/projection.rs index 1ab7a55..b9dfb96 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -27,7 +27,7 @@ mod num_macros; /// This is the equivalent of the gluPerspective function, the algorithm of which /// can be found [here](http://www.opengl.org/wiki/GluPerspective_code). /// -pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { +pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { let ymax = near * (fovy / two!(T)).to_radians().tan(); let xmax = ymax * aspectRatio; @@ -40,7 +40,7 @@ pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> M /// This is the equivalent of the now deprecated [glFrustrum] /// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. /// -pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let c0r0 = (two!(T) * near) / (right - left); let c0r1 = zero!(T); let c0r2 = zero!(T); @@ -73,7 +73,7 @@ pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far /// This is the equivalent of the now deprecated [glOrtho] /// (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function. /// -pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let c0r0 = two!(T) / (right - left); let c0r1 = zero!(T); let c0r2 = zero!(T); @@ -101,13 +101,13 @@ pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: } pub trait Projection { - pub fn if_valid(&self, f: &fn() -> U) -> Result; + pub fn if_valid(&self, f: &fn() -> U) -> Result; pub fn to_mat4(&self) -> Result, ~str>; pub fn to_frustum(&self) -> Result, ~str>; } /// A symmetrical perspective projection based on a field-of-view angle -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct PerspectiveFOV { fovy: T, //radians aspect: T, @@ -115,7 +115,7 @@ pub struct PerspectiveFOV { far: T, } -impl PerspectiveFOV { +impl PerspectiveFOV { pub fn to_perspective(&self) -> Result, ~str> { do self.if_valid { let angle = self.fovy / two!(T); @@ -127,15 +127,15 @@ impl PerspectiveFOV { right: xmax, bottom: -ymax, top: ymax, - near: copy self.near, - far: copy self.far, + near: self.near.clone(), + far: self.far.clone(), } } } } -impl Projection for PerspectiveFOV { - pub fn if_valid(&self, f: &fn() -> U) -> Result { +impl Projection for PerspectiveFOV { + pub fn if_valid(&self, f: &fn() -> U) -> Result { let frac_pi_2: T = Real::frac_pi_2(); cond! ( (self.fovy < zero!(T)) { Err(fmt!("The vertical field of view cannot be below zero, found: %?", self.fovy)) } @@ -158,7 +158,7 @@ impl Projection for PerspectiveFOV { } /// A perspective projection with arbitrary left/right/bottom/top distances -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Perspective { left: T, right: T, @@ -168,8 +168,8 @@ pub struct Perspective { far: T, } -impl Projection for Perspective { - pub fn if_valid(&self, f: &fn() -> U) -> Result { +impl Projection for Perspective { + pub fn if_valid(&self, f: &fn() -> U) -> Result { cond! ( (self.left > self.right) { Err(fmt!("`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right)) } (self.bottom > self.top) { Err(fmt!("`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top)) } @@ -256,15 +256,15 @@ impl Projection for Perspective { right: Plane::from_abcd(theta_r.cos(), zero!(T), theta_r.sin(), zero!(T)), bottom: Plane::from_abcd(zero!(T), theta_b.cos(), theta_b.sin(), zero!(T)), top: Plane::from_abcd(zero!(T), theta_t.cos(), theta_t.sin(), zero!(T)), - near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), copy -self.near), - far: Plane::from_abcd(zero!(T), zero!(T), one!(T), copy self.far), + near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), -self.near.clone()), + far: Plane::from_abcd(zero!(T), zero!(T), one!(T), self.far.clone()), } } } } /// An orthographic projection with arbitrary left/right/bottom/top distances -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Ortho { left: T, right: T, @@ -274,8 +274,8 @@ pub struct Ortho { far: T, } -impl Projection for Ortho { - pub fn if_valid(&self, f: &fn() -> U) -> Result { +impl Projection for Ortho { + pub fn if_valid(&self, f: &fn() -> U) -> Result { cond! ( (self.left > self.right) { Err(fmt!("`left` cannot be greater than `right`, found: left: %? right: %?", self.left, self.right)) } (self.bottom > self.top) { Err(fmt!("`bottom` cannot be greater than `top`, found: bottom: %? top: %?", self.bottom, self.top)) } @@ -316,12 +316,12 @@ impl Projection for Ortho { pub fn to_frustum(&self) -> Result, ~str> { do self.if_valid { Frustum { - left: Plane::from_abcd(one!(T), zero!(T), zero!(T), copy self.left), - right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), copy self.right), - bottom: Plane::from_abcd(zero!(T), one!(T), zero!(T), copy self.bottom), - top: Plane::from_abcd(zero!(T), -one!(T), zero!(T), copy self.top), - near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), copy self.near), - far: Plane::from_abcd(zero!(T), zero!(T), one!(T),copy self.far), + left: Plane::from_abcd(one!(T), zero!(T), zero!(T), self.left.clone()), + right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), self.right.clone()), + bottom: Plane::from_abcd(zero!(T), one!(T), zero!(T), self.bottom.clone()), + top: Plane::from_abcd(zero!(T), -one!(T), zero!(T), self.top.clone()), + near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), self.near.clone()), + far: Plane::from_abcd(zero!(T), zero!(T), one!(T),self.far.clone()), } } } diff --git a/src/quat.rs b/src/quat.rs index 3367bb4..320c6fa 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -33,7 +33,7 @@ pub type Quatf32 = Quat; pub type Quatf64 = Quat; /// A quaternion in scalar/vector form -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Quat { s: T, v: Vec3 } impl_dimensional!(Quat, T, 4) @@ -72,7 +72,7 @@ impl Quat { } } -impl Quat { +impl Quat { /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` #[inline] pub fn identity() -> Quat { @@ -136,7 +136,7 @@ impl Quat { /// The result of multiplying the quaternion by a vector #[inline] pub fn mul_v(&self, vec: &Vec3) -> Vec3 { - let tmp = self.v.cross(vec).add_v(&vec.mul_t(copy self.s)); + let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s.clone())); self.v.cross(&tmp).mul_t(two!(T)).add_v(vec) } @@ -175,7 +175,7 @@ impl Quat { /// The conjugate of the quaternion #[inline] pub fn conjugate(&self) -> Quat { - Quat::from_sv(copy self.s, copy -self.v) + Quat::from_sv(self.s.clone(), -self.v.clone()) } /// The multiplicative inverse of the quaternion @@ -220,7 +220,7 @@ impl Quat { } } -impl ToMat3 for Quat { +impl ToMat3 for Quat { /// Convert the quaternion to a 3 x 3 rotation matrix pub fn to_mat3(&self) -> Mat3 { let x2 = self.v.x + self.v.x; @@ -247,14 +247,14 @@ impl ToMat3 for Quat { } } -impl Neg> for Quat { +impl Neg> for Quat { #[inline] pub fn neg(&self) -> Quat { Quat::from_slice(self.map(|&x| -x)) } } -impl Quat { +impl Quat { #[inline] pub fn look_at(dir: &Vec3, up: &Vec3) -> Quat { Mat3::look_at(dir, up).to_quat() diff --git a/src/ray.rs b/src/ray.rs index 12a3a76..6ee7770 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -22,13 +22,13 @@ use point::Point3; /// /// - `pos`: the endpoint of the ray /// - `dir`: the direction vector -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Ray3 { pos: Point3, dir: Vec3, } -impl> ApproxEq for Ray3 { +impl> ApproxEq for Ray3 { #[inline] pub fn approx_epsilon() -> T { ApproxEq::approx_epsilon::() diff --git a/src/vec.rs b/src/vec.rs index 6d93642..30d9e51 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -19,7 +19,7 @@ mod num_macros; mod dim_macros; mod vec_macros; -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Vec2 { x: T, y: T } // GLSL-style type aliases @@ -51,7 +51,7 @@ impl_swap!(Vec2) impl_approx!(Vec2) impl_vec!(Vec2 { x, y }) -impl_vec_copyable!(Vec2) +impl_vec_clonable!(Vec2) impl_vec_numeric!(Vec2) impl_vec_neg!(Vec2) impl_vec_euclidean!(Vec2) @@ -60,7 +60,7 @@ impl_vec_eq!(Vec2) impl_vec_bool!(Vec2) impl_vec_not!(Vec2) -impl Vec2 { +impl Vec2 { #[inline] pub fn unit_x() -> Vec2 { Vec2::new(one!(T), zero!(T)) } #[inline] pub fn unit_y() -> Vec2 { Vec2::new(zero!(T), one!(T)) } @@ -204,7 +204,7 @@ mod vec2_tests { } } -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Vec3 { x: T, y: T, z: T } // GLSL-style type aliases @@ -236,7 +236,7 @@ impl_swap!(Vec3) impl_approx!(Vec3) impl_vec!(Vec3 { x, y, z }) -impl_vec_copyable!(Vec3) +impl_vec_clonable!(Vec3) impl_vec_numeric!(Vec3) impl_vec_neg!(Vec3) impl_vec_euclidean!(Vec3) @@ -245,7 +245,7 @@ impl_vec_eq!(Vec3) impl_vec_bool!(Vec3) impl_vec_not!(Vec3) -impl Vec3 { +impl Vec3 { #[inline] pub fn unit_x() -> Vec3 { Vec3::new(one!(T), zero!(T), zero!(T)) } #[inline] pub fn unit_y() -> Vec3 { Vec3::new(zero!(T), one!(T), zero!(T)) } #[inline] pub fn unit_z() -> Vec3 { Vec3::new(zero!(T), zero!(T), one!(T)) } @@ -411,7 +411,7 @@ mod vec3_tests{ } } -#[deriving(Eq)] +#[deriving(Clone, Eq)] pub struct Vec4 { x: T, y: T, z: T, w: T } // GLSL-style type aliases @@ -443,7 +443,7 @@ impl_approx!(Vec4) impl_swap!(Vec4) impl_vec!(Vec4 { x, y, z, w }) -impl_vec_copyable!(Vec4) +impl_vec_clonable!(Vec4) impl_vec_numeric!(Vec4) impl_vec_neg!(Vec4) impl_vec_euclidean!(Vec4) @@ -452,7 +452,7 @@ impl_vec_eq!(Vec4) impl_vec_bool!(Vec4) impl_vec_not!(Vec4) -impl Vec4 { +impl Vec4 { #[inline] pub fn unit_x() -> Vec4 { Vec4::new(one!(T), zero!(T), zero!(T), zero!(T)) } #[inline] pub fn unit_y() -> Vec4 { Vec4::new(zero!(T), one!(T), zero!(T), zero!(T)) } #[inline] pub fn unit_z() -> Vec4 { Vec4::new(zero!(T), zero!(T), one!(T), zero!(T)) } diff --git a/src/vec_macros.rs b/src/vec_macros.rs index 410d0ae..9b5abeb 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -26,9 +26,9 @@ macro_rules! impl_vec( ) ) -macro_rules! impl_vec_copyable( +macro_rules! impl_vec_clonable( ($Vec:ident) => ( - impl $Vec { + impl $Vec { #[inline] pub fn from_value(value: T) -> $Vec { vec_from_value!($Vec) @@ -38,14 +38,14 @@ macro_rules! impl_vec_copyable( ) macro_rules! vec_from_value( - (Vec2) => (Vec2::new(copy value, copy value)); - (Vec3) => (Vec3::new(copy value, copy value, copy value)); - (Vec4) => (Vec4::new(copy value, copy value, copy value, copy value)); + (Vec2) => (Vec2::new(value.clone(), value.clone())); + (Vec3) => (Vec3::new(value.clone(), value.clone(), value.clone())); + (Vec4) => (Vec4::new(value.clone(), value.clone(), value.clone(), value.clone())); ) macro_rules! impl_vec_numeric( ($Vec:ident) => ( - impl $Vec { + impl $Vec { #[inline] pub fn identity() -> $Vec { $Vec::from_value(one!(T)) } #[inline] pub fn zero() -> $Vec { $Vec::from_value(zero!(T)) } @@ -62,11 +62,11 @@ macro_rules! impl_vec_numeric( #[inline] pub fn rem_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a % b)) } #[inline] pub fn neg_self(&mut self) { self.map_mut(|x| *x = -*x) } - #[inline] pub fn add_self_t(&mut self, value: T) { self.map_mut(|x| *x += copy value) } - #[inline] pub fn sub_self_t(&mut self, value: T) { self.map_mut(|x| *x -= copy value) } - #[inline] pub fn mul_self_t(&mut self, value: T) { self.map_mut(|x| *x *= copy value) } - #[inline] pub fn div_self_t(&mut self, value: T) { self.map_mut(|x| *x /= copy value) } - #[inline] pub fn rem_self_t(&mut self, value: T) { self.map_mut(|x| *x %= copy value) } + #[inline] pub fn add_self_t(&mut self, value: T) { self.map_mut(|x| *x += value.clone()) } + #[inline] pub fn sub_self_t(&mut self, value: T) { self.map_mut(|x| *x -= value.clone()) } + #[inline] pub fn mul_self_t(&mut self, value: T) { self.map_mut(|x| *x *= value.clone()) } + #[inline] pub fn div_self_t(&mut self, value: T) { self.map_mut(|x| *x /= value.clone()) } + #[inline] pub fn rem_self_t(&mut self, value: T) { self.map_mut(|x| *x %= value.clone()) } #[inline] pub fn add_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a += b) } #[inline] pub fn sub_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a -= b) } @@ -99,7 +99,7 @@ macro_rules! vec_dot( macro_rules! impl_vec_neg( ($Vec:ident) => ( - impl Neg<$Vec> for $Vec { + impl Neg<$Vec> for $Vec { #[inline] pub fn neg(&self) -> $Vec { $Vec::from_slice(self.map(|&x| -x)) @@ -110,7 +110,7 @@ macro_rules! impl_vec_neg( macro_rules! impl_vec_euclidean( ($Vec:ident) => ( - impl $Vec { + impl $Vec { #[inline] pub fn length2(&self) -> T { self.dot(self) @@ -179,7 +179,7 @@ macro_rules! vec_angle( macro_rules! impl_vec_ord( ($Vec:ident) => ( - impl $Vec { + impl $Vec { #[inline] pub fn lt_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x < value)) } #[inline] pub fn le_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x <= value)) } #[inline] pub fn ge_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x >= value)) } @@ -195,7 +195,7 @@ macro_rules! impl_vec_ord( macro_rules! impl_vec_eq( ($Vec:ident) => ( - impl $Vec { + impl $Vec { #[inline] pub fn eq_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x == value)) } #[inline] pub fn ne_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x != value)) } @@ -236,7 +236,7 @@ macro_rules! vec_all( macro_rules! impl_vec_not( ($Vec:ident) => ( - impl> Not<$Vec> for $Vec { + impl> Not<$Vec> for $Vec { pub fn not(&self) -> $Vec { $Vec::from_slice(self.map(|&x| !x)) }