From 4582492fda3012083d13d76c102ec728e7c00f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Tue, 14 May 2013 21:08:29 +0000 Subject: [PATCH] Addesome traits and configuration file. --- src/dim2/rotmat2.rs | 92 +++++++++++++++++++++++++ src/nalgebra.rc | 55 +++++++++++++++ src/traits/cross.rs | 4 ++ src/traits/dim.rs | 4 ++ src/traits/transpose.rs | 6 ++ src/traits/workarounds/sqrt.rs | 17 +++++ src/traits/workarounds/trigonometric.rs | 5 ++ 7 files changed, 183 insertions(+) create mode 100644 src/dim2/rotmat2.rs create mode 100644 src/nalgebra.rc create mode 100644 src/traits/cross.rs create mode 100644 src/traits/dim.rs create mode 100644 src/traits/transpose.rs create mode 100644 src/traits/workarounds/sqrt.rs create mode 100644 src/traits/workarounds/trigonometric.rs diff --git a/src/dim2/rotmat2.rs b/src/dim2/rotmat2.rs new file mode 100644 index 00000000..469d8d98 --- /dev/null +++ b/src/dim2/rotmat2.rs @@ -0,0 +1,92 @@ +use core::num::{One, Zero}; +use traits::workarounds::trigonometric::Trigonometric; +use traits::dim::Dim; +use dim2::mat2::Mat2; +// use dim2::vec2::Vec2; + +// FIXME: using a newtype did not compile (due a compiler bug) +// pub type Rotmat2 = (Mat2) +#[deriving(Eq)] +pub struct Rotmat2 +{ + submat: Mat2 +} + +pub fn Rotmat2>(angle: T) -> Rotmat2 +{ + let coa = angle.cos(); + let sia = angle.sin(); + + Rotmat2 + { submat: Mat2(coa, -sia, sia, coa) } +} + +impl Dim for Rotmat2 +{ + fn dim() -> uint + { 2 } +} + +impl One for Rotmat2 +{ + fn one() -> Rotmat2 + { Rotmat2 { submat: One::one() } } +} + +impl + Add> Mul, Rotmat2> for Rotmat2 +{ + fn mul(&self, other: &Rotmat2) -> Rotmat2 + { Rotmat2 { submat: self.submat.mul(&other.submat) } } +} + +// FIXME: implementation of the same classes for the same struct fails +// with "internal compiler error: Asked to compute kind of a type variable". +// +// impl + Add> Mul, Vec2> for Rotmat2 +// { +// fn mul(&self, v: &Vec2) -> Vec2 +// { Vec2(self.m11 * v.x + self.m12 * v.y, self.m21 * v.x + self.m22 * v.y) } +// } + +// FIXME: implementation of the same classes for the same struct (here Vec2) +// fails with "internal compiler error: Asked to compute kind of a type +// variable". +// +// impl + Add> Mul, Vec2> for Vec2 +// { +// fn mul(&self, m: &Rotmat2) -> Vec2 +// { self.mult(&m.submat) } +// } + +/* +impl + Div + Sub + Neg + Eq + Zero> +Inv for Rotmat2 +{ + fn inv(&self) -> Rotmat2 + { + // transpose + Rotmat2( + Mat2( + self.submat.m11, self.submat.m21, + self.submat.m12, self.submat.m22 + ) + ) + } +} +*/ + +/* +impl ToStr for Rotmat2 +{ + fn to_str(&self) -> ~str + { + ~"Rotmat2 {" + + " m11: " + self.m11.to_str() + + " m12: " + self.m12.to_str() + + + " m21: " + self.m21.to_str() + + " m22: " + self.m22.to_str() + + " }" + } +} +*/ diff --git a/src/nalgebra.rc b/src/nalgebra.rc new file mode 100644 index 00000000..d30f326e --- /dev/null +++ b/src/nalgebra.rc @@ -0,0 +1,55 @@ +#[link(name = "nalgebra" + , vers = "0.0" + , author = "Sébastien Crozet" + , uuid = "1E96070F-4778-4EC1-B080-BF69F7048216")]; +#[crate_type = "lib"]; +#[warn(non_camel_case_types)] + +extern mod std; + +pub use dim2::vec2::Vec2; +pub use dim3::vec3::Vec3; +pub use dim2::mat2::Mat2; +pub use dim2::rotmat2::Rotmat2; + +pub use traits::dot::Dot; +pub use traits::cross::Cross; +pub use traits::dim::Dim; + +pub use traits::workarounds::sqrt::Sqrt; +pub use traits::workarounds::trigonometric::Trigonometric; + +pub use traits::inv::Inv; +pub use traits::transpose::Transpose; + +mod dim2 +{ + mod vec2; + mod mat2; + mod rotmat2; +} + +mod dim3 +{ + mod vec3; + mod mat3; +} + +mod ndim +{ +} + +mod traits +{ + mod dot; + mod cross; + mod inv; + mod transpose; + mod dim; + + mod workarounds + { + mod sqrt; + mod trigonometric; + } +} diff --git a/src/traits/cross.rs b/src/traits/cross.rs new file mode 100644 index 00000000..cf17a324 --- /dev/null +++ b/src/traits/cross.rs @@ -0,0 +1,4 @@ +pub trait Cross +{ + fn cross(&self, other : &Self) -> Result; +} diff --git a/src/traits/dim.rs b/src/traits/dim.rs new file mode 100644 index 00000000..5d8f68c9 --- /dev/null +++ b/src/traits/dim.rs @@ -0,0 +1,4 @@ +pub trait Dim +{ + fn dim() -> uint; +} diff --git a/src/traits/transpose.rs b/src/traits/transpose.rs new file mode 100644 index 00000000..459adac2 --- /dev/null +++ b/src/traits/transpose.rs @@ -0,0 +1,6 @@ +// FIXME: valid only for square matrices… +pub trait Transpose +{ + fn transposed(&self) -> Self; + fn transpose(&mut self); +} diff --git a/src/traits/workarounds/sqrt.rs b/src/traits/workarounds/sqrt.rs new file mode 100644 index 00000000..9da09ca6 --- /dev/null +++ b/src/traits/workarounds/sqrt.rs @@ -0,0 +1,17 @@ +// FIXME: this does not seem to exist already +// but it will surely be added someday. + +pub trait Sqrt +{ + fn sqrt(&self) -> Self; +} + +impl Sqrt for f64 +{ + fn sqrt(&self) -> f64 { f64::sqrt(*self) } +} + +impl Sqrt for f32 +{ + fn sqrt(&self) -> f32 { f32::sqrt(*self) } +} diff --git a/src/traits/workarounds/trigonometric.rs b/src/traits/workarounds/trigonometric.rs new file mode 100644 index 00000000..bf8af8cf --- /dev/null +++ b/src/traits/workarounds/trigonometric.rs @@ -0,0 +1,5 @@ +pub trait Trigonometric +{ + fn sin(&self) -> Self; + fn cos(&self) -> Self; +}