Skip to content

Commit

Permalink
Remove ToQuaternion
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed May 6, 2015
1 parent ff722b5 commit 1d522c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
42 changes: 21 additions & 21 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -1358,44 +1358,44 @@ impl<S: BaseFloat> From<Matrix3<S>> for Matrix4<S> {
}
}

impl<S: BaseFloat> ToQuaternion<S> for Matrix3<S> {
impl<S: BaseFloat> From<Matrix3<S>> for Quaternion<S> {
/// Convert the matrix to a quaternion
fn to_quaternion(&self) -> Quaternion<S> {
fn from(mat: Matrix3<S>) -> Quaternion<S> {
// 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::<S>() {
let s = (one::<S>() + 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)
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ use vector::{Vector3, Vector, EuclideanVector};
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Quaternion<S> { pub s: S, pub v: Vector3<S> }

/// Represents types which can be expressed as a quaternion.
pub trait ToQuaternion<S: BaseFloat> {
/// Convert this value to a quaternion.
fn to_quaternion(&self) -> Quaternion<S>;
}

impl<S: Copy + BaseFloat> Array1<S> for Quaternion<S> {
#[inline]
fn map<F>(&mut self, mut op: F) -> Quaternion<S> where F: FnMut(S) -> S {
Expand Down Expand Up @@ -387,18 +381,13 @@ impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {
fn from(quat: Quaternion<S>) -> Basis3<S> { Basis3::from_quaternion(&quat) }
}

impl<S: BaseFloat> ToQuaternion<S> for Quaternion<S> {
#[inline]
fn to_quaternion(&self) -> Quaternion<S> { self.clone() }
}

impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Quaternion<S> {
#[inline]
fn identity() -> Quaternion<S> { Quaternion::identity() }

#[inline]
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Quaternion<S> {
Matrix3::look_at(dir, up).to_quaternion()
Matrix3::look_at(dir, up).into()
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -86,7 +86,7 @@ pub trait Rotation2<S>: Rotation<S, Vector2<S>, Point2<S>>
pub trait Rotation3<S: BaseNum>: Rotation<S, Vector3<S>, Point3<S>>
+ Into<Matrix3<S>>
+ Into<Basis3<S>>
+ ToQuaternion<S>{
+ Into<Quaternion<S>> {
/// Create a rotation using an angle around a given axis.
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Self;

Expand Down Expand Up @@ -255,9 +255,9 @@ impl<S: BaseFloat> From<Basis3<S>> for Matrix3<S> {
fn from(b: Basis3<S>) -> Matrix3<S> { b.mat }
}

impl<S: BaseFloat + 'static> ToQuaternion<S> for Basis3<S> {
impl<S: BaseFloat + 'static> From<Basis3<S>> for Quaternion<S> {
#[inline]
fn to_quaternion(&self) -> Quaternion<S> { self.mat.to_quaternion() }
fn from(b: Basis3<S>) -> Quaternion<S> { b.mat.into() }
}

impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Basis3<S> {
Expand Down

0 comments on commit 1d522c1

Please sign in to comment.