Remove ToQuaternion
This commit is contained in:
parent
6ce250b10d
commit
1a6dc52e51
3 changed files with 26 additions and 37 deletions
|
@ -29,7 +29,7 @@ use approx::ApproxEq;
|
||||||
use array::{Array1, Array2, FixedArray};
|
use array::{Array1, Array2, FixedArray};
|
||||||
use num::{BaseFloat, BaseNum};
|
use num::{BaseFloat, BaseNum};
|
||||||
use point::{Point, Point3};
|
use point::{Point, Point3};
|
||||||
use quaternion::{Quaternion, ToQuaternion};
|
use quaternion::Quaternion;
|
||||||
use vector::{Vector, EuclideanVector};
|
use vector::{Vector, EuclideanVector};
|
||||||
use vector::{Vector2, Vector3, Vector4};
|
use vector::{Vector2, Vector3, Vector4};
|
||||||
|
|
||||||
|
@ -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
|
/// 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
|
// http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
|
||||||
let trace = self.trace();
|
let trace = mat.trace();
|
||||||
let half: S = cast(0.5f64).unwrap();
|
let half: S = cast(0.5f64).unwrap();
|
||||||
|
|
||||||
if trace >= zero::<S>() {
|
if trace >= zero::<S>() {
|
||||||
let s = (one::<S>() + trace).sqrt();
|
let s = (one::<S>() + trace).sqrt();
|
||||||
let w = half * s;
|
let w = half * s;
|
||||||
let s = half / s;
|
let s = half / s;
|
||||||
let x = (self[1][2] - self[2][1]) * s;
|
let x = (mat[1][2] - mat[2][1]) * s;
|
||||||
let y = (self[2][0] - self[0][2]) * s;
|
let y = (mat[2][0] - mat[0][2]) * s;
|
||||||
let z = (self[0][1] - self[1][0]) * s;
|
let z = (mat[0][1] - mat[1][0]) * s;
|
||||||
Quaternion::new(w, x, y, z)
|
Quaternion::new(w, x, y, z)
|
||||||
} else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) {
|
} else if (mat[0][0] > mat[1][1]) && (mat[0][0] > mat[2][2]) {
|
||||||
let s = (half + (self[0][0] - self[1][1] - self[2][2])).sqrt();
|
let s = (half + (mat[0][0] - mat[1][1] - mat[2][2])).sqrt();
|
||||||
let w = half * s;
|
let w = half * s;
|
||||||
let s = half / s;
|
let s = half / s;
|
||||||
let x = (self[0][1] - self[1][0]) * s;
|
let x = (mat[0][1] - mat[1][0]) * s;
|
||||||
let y = (self[2][0] - self[0][2]) * s;
|
let y = (mat[2][0] - mat[0][2]) * s;
|
||||||
let z = (self[1][2] - self[2][1]) * s;
|
let z = (mat[1][2] - mat[2][1]) * s;
|
||||||
Quaternion::new(w, x, y, z)
|
Quaternion::new(w, x, y, z)
|
||||||
} else if self[1][1] > self[2][2] {
|
} else if mat[1][1] > mat[2][2] {
|
||||||
let s = (half + (self[1][1] - self[0][0] - self[2][2])).sqrt();
|
let s = (half + (mat[1][1] - mat[0][0] - mat[2][2])).sqrt();
|
||||||
let w = half * s;
|
let w = half * s;
|
||||||
let s = half / s;
|
let s = half / s;
|
||||||
let x = (self[0][1] - self[1][0]) * s;
|
let x = (mat[0][1] - mat[1][0]) * s;
|
||||||
let y = (self[1][2] - self[2][1]) * s;
|
let y = (mat[1][2] - mat[2][1]) * s;
|
||||||
let z = (self[2][0] - self[0][2]) * s;
|
let z = (mat[2][0] - mat[0][2]) * s;
|
||||||
Quaternion::new(w, x, y, z)
|
Quaternion::new(w, x, y, z)
|
||||||
} else {
|
} 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 w = half * s;
|
||||||
let s = half / s;
|
let s = half / s;
|
||||||
let x = (self[2][0] - self[0][2]) * s;
|
let x = (mat[2][0] - mat[0][2]) * s;
|
||||||
let y = (self[1][2] - self[2][1]) * s;
|
let y = (mat[1][2] - mat[2][1]) * s;
|
||||||
let z = (self[0][1] - self[1][0]) * s;
|
let z = (mat[0][1] - mat[1][0]) * s;
|
||||||
Quaternion::new(w, x, y, z)
|
Quaternion::new(w, x, y, z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,12 +37,6 @@ use vector::{Vector3, Vector, EuclideanVector};
|
||||||
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
||||||
pub struct Quaternion<S> { pub s: S, pub v: Vector3<S> }
|
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> {
|
impl<S: Copy + BaseFloat> Array1<S> for Quaternion<S> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn map<F>(&mut self, mut op: F) -> Quaternion<S> where F: FnMut(S) -> S {
|
fn map<F>(&mut self, mut op: F) -> Quaternion<S> where F: FnMut(S) -> S {
|
||||||
|
@ -387,18 +381,13 @@ impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {
|
||||||
fn from(quat: Quaternion<S>) -> Basis3<S> { Basis3::from_quaternion(&quat) }
|
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> {
|
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Quaternion<S> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn identity() -> Quaternion<S> { Quaternion::identity() }
|
fn identity() -> Quaternion<S> { Quaternion::identity() }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Quaternion<S> {
|
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]
|
#[inline]
|
||||||
|
|
|
@ -20,7 +20,7 @@ use matrix::Matrix2;
|
||||||
use matrix::Matrix3;
|
use matrix::Matrix3;
|
||||||
use num::{BaseNum, BaseFloat};
|
use num::{BaseNum, BaseFloat};
|
||||||
use point::{Point, Point2, Point3};
|
use point::{Point, Point2, Point3};
|
||||||
use quaternion::{Quaternion, ToQuaternion};
|
use quaternion::Quaternion;
|
||||||
use ray::Ray;
|
use ray::Ray;
|
||||||
use vector::{Vector, Vector2, Vector3};
|
use vector::{Vector, Vector2, Vector3};
|
||||||
|
|
||||||
|
@ -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>>
|
pub trait Rotation3<S: BaseNum>: Rotation<S, Vector3<S>, Point3<S>>
|
||||||
+ Into<Matrix3<S>>
|
+ Into<Matrix3<S>>
|
||||||
+ Into<Basis3<S>>
|
+ Into<Basis3<S>>
|
||||||
+ ToQuaternion<S>{
|
+ Into<Quaternion<S>> {
|
||||||
/// Create a rotation using an angle around a given axis.
|
/// Create a rotation using an angle around a given axis.
|
||||||
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Self;
|
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Self;
|
||||||
|
|
||||||
|
@ -255,9 +255,9 @@ impl<S: BaseFloat> From<Basis3<S>> for Matrix3<S> {
|
||||||
fn from(b: Basis3<S>) -> Matrix3<S> { b.mat }
|
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]
|
#[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> {
|
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Basis3<S> {
|
||||||
|
|
Loading…
Reference in a new issue