Implement to_mat4 for Transform3D and quaternion
Add a translate builder for Transform3D
This commit is contained in:
parent
7435ae16bf
commit
fb13dc4566
4 changed files with 70 additions and 3 deletions
|
@ -19,7 +19,7 @@ use std::num::{zero, one, cast, sqrt};
|
|||
use angle::{Angle, Rad, acos, cos, sin, sin_cos};
|
||||
use approx::ApproxEq;
|
||||
use array::{Array, build};
|
||||
use matrix::{Mat3, ToMat3};
|
||||
use matrix::{Mat3, ToMat3, ToMat4, Mat4};
|
||||
use vector::{Vec3, Vector, EuclideanVector};
|
||||
|
||||
/// A quaternion in scalar/vector form
|
||||
|
@ -291,6 +291,33 @@ ToMat3<S> for Quat<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Float + ApproxEq<S>>
|
||||
ToMat4<S> for Quat<S> {
|
||||
/// Convert the quaternion to a 4 x 4 rotation matrix
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
let x2 = self.v.x + self.v.x;
|
||||
let y2 = self.v.y + self.v.y;
|
||||
let z2 = self.v.z + self.v.z;
|
||||
|
||||
let xx2 = x2 * self.v.x;
|
||||
let xy2 = x2 * self.v.y;
|
||||
let xz2 = x2 * self.v.z;
|
||||
|
||||
let yy2 = y2 * self.v.y;
|
||||
let yz2 = y2 * self.v.z;
|
||||
let zz2 = z2 * self.v.z;
|
||||
|
||||
let sy2 = y2 * self.s;
|
||||
let sz2 = z2 * self.s;
|
||||
let sx2 = x2 * self.s;
|
||||
|
||||
Mat4::new(one::<S>() - yy2 - zz2, xy2 + sz2, xz2 - sy2, zero::<S>(),
|
||||
xy2 - sz2, one::<S>() - xx2 - zz2, yz2 + sx2, zero::<S>(),
|
||||
xz2 + sy2, yz2 - sx2, one::<S>() - xx2 - yy2, zero::<S>(),
|
||||
zero::<S>(), zero::<S>(), zero::<S>(), one::<S>())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Float + ApproxEq<S>>
|
||||
Neg<Quat<S>> for Quat<S> {
|
||||
#[inline]
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
use std::{fmt,num};
|
||||
|
||||
use std::num::one;
|
||||
|
||||
use approx::ApproxEq;
|
||||
use matrix::{Matrix, Mat4, ToMat4};
|
||||
use point::{Point, Point3};
|
||||
|
@ -215,14 +217,24 @@ Transform3<S> for AffineMatrix3<S> {}
|
|||
/// displacement vector and scale amount.
|
||||
pub struct Transform3D<S>( Decomposed<S,Vec3<S>,Quat<S>> );
|
||||
|
||||
impl<S: Float> Transform3D<S> {
|
||||
impl<S: Float + ApproxEq<S>> Transform3D<S> {
|
||||
#[inline]
|
||||
pub fn new(scale: S, rot: Quat<S>, disp: Vec3<S>) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: scale, rot: rot, disp: disp })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn translate(x: S, y: S, z: S) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: one(), rot: Quat::zero(), disp: Vec3::new(x, y, z) })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get<'a>(&'a self) -> &'a Decomposed<S,Vec3<S>,Quat<S>> {
|
||||
let &Transform3D(ref d) = self;
|
||||
d
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Float + ApproxEq<S>> ToMat4<S> for Transform3D<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> { self.get().to_mat4() }
|
||||
}
|
||||
|
|
28
src/test/quaternion.rs
Normal file
28
src/test/quaternion.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2013 The CGMath Developers. For a full listing of the authors,
|
||||
// refer to the AUTHORS file at the top-level directory of this distribution.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use cgmath::matrix::{ToMat4, ToMat3};
|
||||
use cgmath::quaternion::Quat;
|
||||
|
||||
#[test]
|
||||
fn to_mat4()
|
||||
{
|
||||
let quat = Quat::new(2f32, 3f32, 4f32, 5f32);
|
||||
|
||||
let mat_short = quat.to_mat4();
|
||||
let mat_long = quat.to_mat3().to_mat4();
|
||||
|
||||
assert!(mat_short == mat_long);
|
||||
}
|
|
@ -20,7 +20,7 @@ extern mod cgmath;
|
|||
|
||||
// pub mod array;
|
||||
pub mod matrix;
|
||||
// pub mod quaternion;
|
||||
pub mod quaternion;
|
||||
pub mod vector;
|
||||
|
||||
pub mod angle;
|
||||
|
|
Loading…
Reference in a new issue