diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index 246a1607..c5fa4e7b 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -7,9 +7,9 @@ use traits::inv::Inv; use traits::transpose::Transpose; use traits::rotation::Rotation; use traits::delta_transform::DeltaTransform; -use dim1::vec1::{Vec1, vec1}; -use dim2::mat2::{Mat2, mat2}; -use dim3::mat3::{Mat3, mat3}; +use dim1::vec1::Vec1; +use dim2::mat2::Mat2; +use dim3::mat3::Mat3; use dim3::vec3::{Vec3}; #[deriving(Eq, ToStr)] @@ -30,7 +30,7 @@ pub fn rotmat2>(angle: T) -> Rotmat> let sia = angle.sin(); Rotmat - { submat: mat2(coa, -sia, sia, coa) } + { submat: Mat2::new(coa, -sia, sia, coa) } } pub fn rotmat3 + One + Sub + Add + @@ -49,7 +49,7 @@ pub fn rotmat3 + One + Sub + Add + let sin = angle.sin(); Rotmat { - submat: mat3( + submat: Mat3::new( (sqx + (_1 - sqx) * cos), (ux * uy * one_m_cos - uz * sin), (ux * uz * one_m_cos + uy * sin), @@ -68,7 +68,7 @@ impl + Trigonometric + Neg + Mul + Add + Copy> Rotation> for Rotmat> { fn rotation(&self) -> Vec1 - { vec1(-(self.submat.m12 / self.submat.m11).atan()) } + { Vec1::new(-(self.submat.m12 / self.submat.m11).atan()) } fn rotated(&self, rot: &Vec1) -> Rotmat> { rotmat2(rot.x) * *self } diff --git a/src/dim1/mat1.rs b/src/dim1/mat1.rs index 5d5cee66..6d22e5dd 100644 --- a/src/dim1/mat1.rs +++ b/src/dim1/mat1.rs @@ -5,16 +5,19 @@ use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; use traits::workarounds::rlmul::{RMul, LMul}; -use dim1::vec1::{Vec1, vec1}; +use dim1::vec1::Vec1; #[deriving(Eq, ToStr)] pub struct Mat1 { m11: T } -pub fn mat1(m11: T) -> Mat1 +impl Mat1 { - Mat1 - { m11: m11 } + pub fn new(m11: T) -> Mat1 + { + Mat1 + { m11: m11 } + } } impl Dim for Mat1 @@ -26,13 +29,13 @@ impl Dim for Mat1 impl One for Mat1 { fn one() -> Mat1 - { return mat1(One::one()) } + { return Mat1::new(One::one()) } } impl Zero for Mat1 { fn zero() -> Mat1 - { mat1(Zero::zero()) } + { Mat1::new(Zero::zero()) } fn is_zero(&self) -> bool { self.m11.is_zero() } @@ -41,19 +44,19 @@ impl Zero for Mat1 impl + Add> Mul, Mat1> for Mat1 { fn mul(&self, other: &Mat1) -> Mat1 - { mat1(self.m11 * other.m11) } + { Mat1::new(self.m11 * other.m11) } } impl + Mul> RMul> for Mat1 { fn rmul(&self, other: &Vec1) -> Vec1 - { vec1(self.m11 * other.x) } + { Vec1::new(self.m11 * other.x) } } impl + Mul> LMul> for Mat1 { fn lmul(&self, other: &Vec1) -> Vec1 - { vec1(self.m11 * other.x) } + { Vec1::new(self.m11 * other.x) } } impl + Div + Sub + Neg + Zero + One> @@ -100,5 +103,5 @@ impl> ApproxEq for Mat1 impl Rand for Mat1 { fn rand(rng: &mut R) -> Mat1 - { mat1(rng.gen()) } + { Mat1::new(rng.gen()) } } diff --git a/src/dim1/vec1.rs b/src/dim1/vec1.rs index 9de405ed..6e1419a5 100644 --- a/src/dim1/vec1.rs +++ b/src/dim1/vec1.rs @@ -13,8 +13,12 @@ use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub} pub struct Vec1 { x : T } -pub fn vec1(x: T) -> Vec1 -{ Vec1 {x: x} } + +impl Vec1 +{ + pub fn new(x: T) -> Vec1 + { Vec1 {x: x} } +} impl Dim for Vec1 { @@ -25,13 +29,13 @@ impl Dim for Vec1 impl> Add, Vec1> for Vec1 { fn add(&self, other: &Vec1) -> Vec1 - { vec1(self.x + other.x) } + { Vec1::new(self.x + other.x) } } impl> Sub, Vec1> for Vec1 { fn sub(&self, other: &Vec1) -> Vec1 - { vec1(self.x - other.x) } + { Vec1::new(self.x - other.x) } } impl> @@ -109,7 +113,7 @@ Norm for Vec1 { self.sqnorm().sqrt() } fn normalized(&self) -> Vec1 - { vec1(self.x / self.norm()) } + { Vec1::new(self.x / self.norm()) } fn normalize(&mut self) -> T { @@ -124,7 +128,7 @@ Norm for Vec1 impl> Neg> for Vec1 { fn neg(&self) -> Vec1 - { vec1(-self.x) } + { Vec1::new(-self.x) } } impl Zero for Vec1 @@ -132,7 +136,7 @@ impl Zero for Vec1 fn zero() -> Vec1 { let _0 = Zero::zero(); - vec1(_0) + Vec1::new(_0) } fn is_zero(&self) -> bool @@ -142,7 +146,7 @@ impl Zero for Vec1 impl Basis for Vec1 { fn canonical_basis() -> ~[Vec1] - { ~[ vec1(One::one()) ] } // FIXME: this should be static + { ~[ Vec1::new(One::one()) ] } // FIXME: this should be static fn orthogonal_subspace_basis(&self) -> ~[Vec1] { ~[] } @@ -163,5 +167,5 @@ impl> ApproxEq for Vec1 impl Rand for Vec1 { fn rand(rng: &mut R) -> Vec1 - { vec1(rng.gen()) } + { Vec1::new(rng.gen()) } } diff --git a/src/dim2/mat2.rs b/src/dim2/mat2.rs index e3b47af0..9f78c317 100644 --- a/src/dim2/mat2.rs +++ b/src/dim2/mat2.rs @@ -6,7 +6,7 @@ use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; use traits::workarounds::rlmul::{RMul, LMul}; -use dim2::vec2::{Vec2, vec2}; +use dim2::vec2::Vec2; #[deriving(Eq, ToStr)] pub struct Mat2 @@ -15,12 +15,15 @@ pub struct Mat2 m21: T, m22: T } -pub fn mat2(m11: T, m12: T, m21: T, m22: T) -> Mat2 +impl Mat2 { - Mat2 + pub fn new(m11: T, m12: T, m21: T, m22: T) -> Mat2 { - m11: m11, m12: m12, - m21: m21, m22: m22, + Mat2 + { + m11: m11, m12: m12, + m21: m21, m22: m22, + } } } @@ -35,8 +38,8 @@ impl One for Mat2 fn one() -> Mat2 { let (_0, _1) = (Zero::zero(), One::one()); - return mat2(_1, _0, - _0, _1) + return Mat2::new(_1, _0, + _0, _1) } } @@ -45,8 +48,8 @@ impl Zero for Mat2 fn zero() -> Mat2 { let _0 = Zero::zero(); - return mat2(_0, _0, - _0, _0) + return Mat2::new(_0, _0, + _0, _0) } fn is_zero(&self) -> bool @@ -60,11 +63,12 @@ impl + Add> Mul, Mat2> for Mat2 { fn mul(&self, other: &Mat2) -> Mat2 { - mat2 - (self.m11 * other.m11 + self.m12 * other.m21, - self.m11 * other.m12 + self.m12 * other.m22, - self.m21 * other.m11 + self.m22 * other.m21, - self.m21 * other.m12 + self.m22 * other.m22) + Mat2::new( + self.m11 * other.m11 + self.m12 * other.m21, + self.m11 * other.m12 + self.m12 * other.m22, + self.m21 * other.m11 + self.m22 * other.m21, + self.m21 * other.m12 + self.m22 * other.m22 + ) } } @@ -72,7 +76,7 @@ impl + Mul> RMul> for Mat2 { fn rmul(&self, other: &Vec2) -> Vec2 { - vec2( + Vec2::new( self.m11 * other.x + self.m12 * other.y, self.m21 * other.x + self.m22 * other.y ) @@ -83,7 +87,7 @@ impl + Mul> LMul> for Mat2 { fn lmul(&self, other: &Vec2) -> Vec2 { - vec2( + Vec2::new( self.m11 * other.x + self.m21 * other.y, self.m12 * other.x + self.m22 * other.y ) @@ -108,8 +112,8 @@ Inv for Mat2 assert!(!det.is_zero()); - *self = mat2(self.m22 / det , -self.m12 / det, - -self.m21 / det, self.m11 / det) + *self = Mat2::new(self.m22 / det , -self.m12 / det, + -self.m21 / det, self.m11 / det) } } @@ -117,8 +121,8 @@ impl Transpose for Mat2 { fn transposed(&self) -> Mat2 { - mat2(self.m11, self.m21, - self.m12, self.m22) + Mat2::new(self.m11, self.m21, + self.m12, self.m22) } fn transpose(&mut self) @@ -152,5 +156,5 @@ impl> ApproxEq for Mat2 impl Rand for Mat2 { fn rand(rng: &mut R) -> Mat2 - { mat2(rng.gen(), rng.gen(), rng.gen(), rng.gen()) } + { Mat2::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) } } diff --git a/src/dim2/vec2.rs b/src/dim2/vec2.rs index 6f1ec62e..fcb6b4fe 100644 --- a/src/dim2/vec2.rs +++ b/src/dim2/vec2.rs @@ -1,6 +1,6 @@ use std::num::{Zero, One, Algebraic}; use std::rand::{Rand, Rng, RngUtil}; -use dim1::vec1::{Vec1, vec1}; +use dim1::vec1::Vec1; use std::cmp::ApproxEq; use traits::basis::Basis; use traits::cross::Cross; @@ -18,8 +18,11 @@ pub struct Vec2 y : T } -pub fn vec2(x: T, y: T) -> Vec2 -{ Vec2 {x: x, y: y} } +impl Vec2 +{ + pub fn new(x: T, y: T) -> Vec2 + { Vec2 {x: x, y: y} } +} impl Dim for Vec2 { @@ -30,13 +33,13 @@ impl Dim for Vec2 impl> Add, Vec2> for Vec2 { fn add(&self, other: &Vec2) -> Vec2 - { vec2(self.x + other.x, self.y + other.y) } + { Vec2::new(self.x + other.x, self.y + other.y) } } impl> Sub, Vec2> for Vec2 { fn sub(&self, other: &Vec2) -> Vec2 - { vec2(self.x - other.x, self.y - other.y) } + { Vec2::new(self.x - other.x, self.y - other.y) } } impl> @@ -129,7 +132,7 @@ Norm for Vec2 { let l = self.norm(); - vec2(self.x / l, self.y / l) + Vec2::new(self.x / l, self.y / l) } fn normalize(&mut self) -> T @@ -146,13 +149,13 @@ Norm for Vec2 impl + Sub> Cross> for Vec2 { fn cross(&self, other : &Vec2) -> Vec1 - { vec1(self.x * other.y - self.y * other.x) } + { Vec1::new(self.x * other.y - self.y * other.x) } } impl> Neg> for Vec2 { fn neg(&self) -> Vec2 - { vec2(-self.x, -self.y) } + { Vec2::new(-self.x, -self.y) } } impl Zero for Vec2 @@ -160,7 +163,7 @@ impl Zero for Vec2 fn zero() -> Vec2 { let _0 = Zero::zero(); - vec2(_0, _0) + Vec2::new(_0, _0) } fn is_zero(&self) -> bool @@ -172,12 +175,12 @@ impl> Basis for Vec2 fn canonical_basis() -> ~[Vec2] { // FIXME: this should be static - ~[ vec2(One::one(), Zero::zero()), - vec2(Zero::zero(), One::one()) ] + ~[ Vec2::new(One::one(), Zero::zero()), + Vec2::new(Zero::zero(), One::one()) ] } fn orthogonal_subspace_basis(&self) -> ~[Vec2] - { ~[ vec2(-self.y, self.x) ] } + { ~[ Vec2::new(-self.y, self.x) ] } } impl> ApproxEq for Vec2 @@ -198,5 +201,5 @@ impl> ApproxEq for Vec2 impl Rand for Vec2 { fn rand(rng: &mut R) -> Vec2 - { vec2(rng.gen(), rng.gen()) } + { Vec2::new(rng.gen(), rng.gen()) } } diff --git a/src/dim3/mat3.rs b/src/dim3/mat3.rs index 2f22b848..25389ad9 100644 --- a/src/dim3/mat3.rs +++ b/src/dim3/mat3.rs @@ -6,7 +6,7 @@ use traits::dim::Dim; use traits::inv::Inv; use traits::transpose::Transpose; use traits::workarounds::rlmul::{RMul, LMul}; -use dim3::vec3::{Vec3, vec3}; +use dim3::vec3::Vec3; #[deriving(Eq, ToStr)] pub struct Mat3 @@ -16,15 +16,18 @@ pub struct Mat3 m31: T, m32: T, m33: T } -pub fn mat3(m11: T, m12: T, m13: T, - m21: T, m22: T, m23: T, - m31: T, m32: T, m33: T) -> Mat3 +impl Mat3 { - Mat3 + pub fn new(m11: T, m12: T, m13: T, + m21: T, m22: T, m23: T, + m31: T, m32: T, m33: T) -> Mat3 { - m11: m11, m12: m12, m13: m13, - m21: m21, m22: m22, m23: m23, - m31: m31, m32: m32, m33: m33 + Mat3 + { + m11: m11, m12: m12, m13: m13, + m21: m21, m22: m22, m23: m23, + m31: m31, m32: m32, m33: m33 + } } } @@ -39,9 +42,9 @@ impl One for Mat3 fn one() -> Mat3 { let (_0, _1) = (Zero::zero(), One::one()); - return mat3(_1, _0, _0, - _0, _1, _0, - _0, _0, _1) + return Mat3::new(_1, _0, _0, + _0, _1, _0, + _0, _0, _1) } } @@ -50,9 +53,9 @@ impl Zero for Mat3 fn zero() -> Mat3 { let _0 = Zero::zero(); - return mat3(_0, _0, _0, - _0, _0, _0, - _0, _0, _0) + return Mat3::new(_0, _0, _0, + _0, _0, _0, + _0, _0, _0) } fn is_zero(&self) -> bool @@ -67,7 +70,7 @@ impl + Add> Mul, Mat3> for Mat3 { fn mul(&self, other: &Mat3) -> Mat3 { - mat3( + Mat3::new( self.m11 * other.m11 + self.m12 * other.m21 + self.m13 * other.m31, self.m11 * other.m12 + self.m12 * other.m22 + self.m13 * other.m32, self.m11 * other.m13 + self.m12 * other.m23 + self.m13 * other.m33, @@ -87,7 +90,7 @@ impl + Mul> RMul> for Mat3 { fn rmul(&self, other: &Vec3) -> Vec3 { - vec3( + Vec3::new( self.m11 * other.x + self.m12 * other.y + self.m13 * other.z, self.m21 * other.x + self.m22 * other.y + self.m33 * other.z, self.m31 * other.x + self.m32 * other.y + self.m33 * other.z @@ -99,7 +102,7 @@ impl + Mul> LMul> for Mat3 { fn lmul(&self, other: &Vec3) -> Vec3 { - vec3( + Vec3::new( self.m11 * other.x + self.m21 * other.y + self.m31 * other.z, self.m12 * other.x + self.m22 * other.y + self.m32 * other.z, self.m13 * other.x + self.m23 * other.y + self.m33 * other.z @@ -131,7 +134,7 @@ Inv for Mat3 assert!(!det.is_zero()); - *self = mat3( + *self = Mat3::new( (minor_m22_m33 / det), ((self.m13 * self.m32 - self.m33 * self.m12) / det), ((self.m12 * self.m23 - self.m22 * self.m13) / det), @@ -151,9 +154,9 @@ impl Transpose for Mat3 { fn transposed(&self) -> Mat3 { - mat3(self.m11, self.m21, self.m31, - self.m12, self.m22, self.m32, - self.m13, self.m23, self.m33) + Mat3::new(self.m11, self.m21, self.m31, + self.m12, self.m22, self.m32, + self.m13, self.m23, self.m33) } fn transpose(&mut self) @@ -204,8 +207,8 @@ impl Rand for Mat3 { fn rand(rng: &mut R) -> Mat3 { - mat3(rng.gen(), rng.gen(), rng.gen(), - rng.gen(), rng.gen(), rng.gen(), - rng.gen(), rng.gen(), rng.gen()) + Mat3::new(rng.gen(), rng.gen(), rng.gen(), + rng.gen(), rng.gen(), rng.gen(), + rng.gen(), rng.gen(), rng.gen()) } } diff --git a/src/dim3/vec3.rs b/src/dim3/vec3.rs index eca742be..219256af 100644 --- a/src/dim3/vec3.rs +++ b/src/dim3/vec3.rs @@ -18,8 +18,11 @@ pub struct Vec3 z : T } -pub fn vec3(x: T, y: T, z: T) -> Vec3 -{ Vec3 {x: x, y: y, z: z} } +impl Vec3 +{ + pub fn new(x: T, y: T, z: T) -> Vec3 + { Vec3 {x: x, y: y, z: z} } +} impl Dim for Vec3 { @@ -30,13 +33,13 @@ impl Dim for Vec3 impl> Add, Vec3> for Vec3 { fn add(&self, other: &Vec3) -> Vec3 - { vec3(self.x + other.x, self.y + other.y, self.z + other.z) } + { Vec3::new(self.x + other.x, self.y + other.y, self.z + other.z) } } impl> Sub, Vec3> for Vec3 { fn sub(&self, other: &Vec3) -> Vec3 - { vec3(self.x - other.x, self.y - other.y, self.z - other.z) } + { Vec3::new(self.x - other.x, self.y - other.y, self.z - other.z) } } impl> @@ -113,7 +116,7 @@ impl> Translation> for Vec3 impl> Neg> for Vec3 { fn neg(&self) -> Vec3 - { vec3(-self.x, -self.y, -self.z) } + { Vec3::new(-self.x, -self.y, -self.z) } } impl + Add> Dot for Vec3 @@ -141,7 +144,7 @@ Norm for Vec3 { let l = self.norm(); - vec3(self.x / l, self.y / l, self.z / l) + Vec3::new(self.x / l, self.y / l, self.z / l) } fn normalize(&mut self) -> T @@ -160,7 +163,7 @@ impl + Sub> Cross> for Vec3 { fn cross(&self, other : &Vec3) -> Vec3 { - vec3( + Vec3::new( self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, self.x * other.y - self.y * other.x @@ -173,7 +176,7 @@ impl Zero for Vec3 fn zero() -> Vec3 { let _0 = Zero::zero(); - vec3(_0, _0, _0) + Vec3::new(_0, _0, _0) } fn is_zero(&self) -> bool @@ -187,18 +190,18 @@ Basis for Vec3 fn canonical_basis() -> ~[Vec3] { // FIXME: this should be static - ~[ vec3(One::one(), Zero::zero(), Zero::zero()), - vec3(Zero::zero(), One::one(), Zero::zero()), - vec3(Zero::zero(), Zero::zero(), One::one()) ] + ~[ Vec3::new(One::one(), Zero::zero(), Zero::zero()), + Vec3::new(Zero::zero(), One::one(), Zero::zero()), + Vec3::new(Zero::zero(), Zero::zero(), One::one()) ] } fn orthogonal_subspace_basis(&self) -> ~[Vec3] { let a = if (abs(self.x) > abs(self.y)) - { vec3(self.z, Zero::zero(), -self.x).normalized() } + { Vec3::new(self.z, Zero::zero(), -self.x).normalized() } else - { vec3(Zero::zero(), -self.z, self.y).normalized() }; + { Vec3::new(Zero::zero(), -self.z, self.y).normalized() }; ~[ a, a.cross(self) ] } @@ -227,5 +230,5 @@ impl> ApproxEq for Vec3 impl Rand for Vec3 { fn rand(rng: &mut R) -> Vec3 - { vec3(rng.gen(), rng.gen(), rng.gen()) } + { Vec3::new(rng.gen(), rng.gen(), rng.gen()) } } diff --git a/src/tests/mat.rs b/src/tests/mat.rs index 0763fcd6..a23e7f1a 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -11,7 +11,7 @@ use traits::rotation::Rotation; // #[test] // use traits::dim::d7; #[test] -use dim1::vec1::vec1; +use dim1::vec1::Vec1; #[test] use dim1::mat1::Mat1; #[test] @@ -57,7 +57,7 @@ fn test_rotation2() for 10000.times { let randmat = One::one::>>(); - let ang = &vec1(abs::(random()) % Real::pi()); + let ang = &Vec1::new(abs::(random()) % Real::pi()); assert!(randmat.rotated(ang).rotation().approx_eq(ang)); }