From 1d522c13e5922e9e7dc12ffc4ae11617548424d0 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 6 May 2015 10:38:15 +0200 Subject: [PATCH] Remove ToQuaternion --- src/matrix.rs | 42 +++++++++++++++++++++--------------------- src/quaternion.rs | 13 +------------ src/rotation.rs | 8 ++++---- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 85bd3ac1..d683b73e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -29,7 +29,7 @@ use approx::ApproxEq; use array::{Array1, Array2, FixedArray}; use num::{BaseFloat, BaseNum}; use point::{Point, Point3}; -use quaternion::{Quaternion, ToQuaternion}; +use quaternion::Quaternion; use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; @@ -1358,44 +1358,44 @@ impl From> for Matrix4 { } } -impl ToQuaternion for Matrix3 { +impl From> for Quaternion { /// Convert the matrix to a quaternion - fn to_quaternion(&self) -> Quaternion { + fn from(mat: Matrix3) -> Quaternion { // http://www.cs.ucr.edu/~vbz/resources/quatut.pdf - let trace = self.trace(); + let trace = mat.trace(); let half: S = cast(0.5f64).unwrap(); if trace >= zero::() { let s = (one::() + trace).sqrt(); let w = half * s; let s = half / s; - let x = (self[1][2] - self[2][1]) * s; - let y = (self[2][0] - self[0][2]) * s; - let z = (self[0][1] - self[1][0]) * s; + let x = (mat[1][2] - mat[2][1]) * s; + let y = (mat[2][0] - mat[0][2]) * s; + let z = (mat[0][1] - mat[1][0]) * s; Quaternion::new(w, x, y, z) - } else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) { - let s = (half + (self[0][0] - self[1][1] - self[2][2])).sqrt(); + } else if (mat[0][0] > mat[1][1]) && (mat[0][0] > mat[2][2]) { + let s = (half + (mat[0][0] - mat[1][1] - mat[2][2])).sqrt(); let w = half * s; let s = half / s; - let x = (self[0][1] - self[1][0]) * s; - let y = (self[2][0] - self[0][2]) * s; - let z = (self[1][2] - self[2][1]) * s; + let x = (mat[0][1] - mat[1][0]) * s; + let y = (mat[2][0] - mat[0][2]) * s; + let z = (mat[1][2] - mat[2][1]) * s; Quaternion::new(w, x, y, z) - } else if self[1][1] > self[2][2] { - let s = (half + (self[1][1] - self[0][0] - self[2][2])).sqrt(); + } else if mat[1][1] > mat[2][2] { + let s = (half + (mat[1][1] - mat[0][0] - mat[2][2])).sqrt(); let w = half * s; let s = half / s; - let x = (self[0][1] - self[1][0]) * s; - let y = (self[1][2] - self[2][1]) * s; - let z = (self[2][0] - self[0][2]) * s; + let x = (mat[0][1] - mat[1][0]) * s; + let y = (mat[1][2] - mat[2][1]) * s; + let z = (mat[2][0] - mat[0][2]) * s; Quaternion::new(w, x, y, z) } else { - let s = (half + (self[2][2] - self[0][0] - self[1][1])).sqrt(); + let s = (half + (mat[2][2] - mat[0][0] - mat[1][1])).sqrt(); let w = half * s; let s = half / s; - let x = (self[2][0] - self[0][2]) * s; - let y = (self[1][2] - self[2][1]) * s; - let z = (self[0][1] - self[1][0]) * s; + let x = (mat[2][0] - mat[0][2]) * s; + let y = (mat[1][2] - mat[2][1]) * s; + let z = (mat[0][1] - mat[1][0]) * s; Quaternion::new(w, x, y, z) } } diff --git a/src/quaternion.rs b/src/quaternion.rs index 46c0025c..03b3e1ac 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -37,12 +37,6 @@ use vector::{Vector3, Vector, EuclideanVector}; #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] pub struct Quaternion { pub s: S, pub v: Vector3 } -/// Represents types which can be expressed as a quaternion. -pub trait ToQuaternion { - /// Convert this value to a quaternion. - fn to_quaternion(&self) -> Quaternion; -} - impl Array1 for Quaternion { #[inline] fn map(&mut self, mut op: F) -> Quaternion where F: FnMut(S) -> S { @@ -387,18 +381,13 @@ impl From> for Basis3 { fn from(quat: Quaternion) -> Basis3 { Basis3::from_quaternion(&quat) } } -impl ToQuaternion for Quaternion { - #[inline] - fn to_quaternion(&self) -> Quaternion { self.clone() } -} - impl Rotation, Point3> for Quaternion { #[inline] fn identity() -> Quaternion { Quaternion::identity() } #[inline] fn look_at(dir: &Vector3, up: &Vector3) -> Quaternion { - Matrix3::look_at(dir, up).to_quaternion() + Matrix3::look_at(dir, up).into() } #[inline] diff --git a/src/rotation.rs b/src/rotation.rs index f9ad20fa..6c5419ab 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -20,7 +20,7 @@ use matrix::Matrix2; use matrix::Matrix3; use num::{BaseNum, BaseFloat}; use point::{Point, Point2, Point3}; -use quaternion::{Quaternion, ToQuaternion}; +use quaternion::Quaternion; use ray::Ray; use vector::{Vector, Vector2, Vector3}; @@ -86,7 +86,7 @@ pub trait Rotation2: Rotation, Point2> pub trait Rotation3: Rotation, Point3> + Into> + Into> - + ToQuaternion{ + + Into> { /// Create a rotation using an angle around a given axis. fn from_axis_angle(axis: &Vector3, angle: Rad) -> Self; @@ -255,9 +255,9 @@ impl From> for Matrix3 { fn from(b: Basis3) -> Matrix3 { b.mat } } -impl ToQuaternion for Basis3 { +impl From> for Quaternion { #[inline] - fn to_quaternion(&self) -> Quaternion { self.mat.to_quaternion() } + fn from(b: Basis3) -> Quaternion { b.mat.into() } } impl Rotation, Point3> for Basis3 {