cgmath/src/transform.rs

255 lines
7.5 KiB
Rust
Raw Normal View History

// Copyright 2014 The CGMath Developers. For a full listing of the authors,
2013-10-13 00:00:51 +00:00
// 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 std::fmt;
2014-01-09 00:26:50 +00:00
use approx::ApproxEq;
use matrix::{Matrix, ToMatrix3, Matrix4, ToMatrix4};
use num::{BaseNum, BaseFloat, zero, one};
use point::{Point, Point3};
use ray::Ray;
use rotation::{Rotation, Rotation3};
use std::marker::PhantomFn;
use vector::{Vector, Vector3};
2014-05-25 11:17:26 +00:00
/// A trait representing an [affine
/// transformation](https://en.wikipedia.org/wiki/Affine_transformation) that
/// can be applied to points or vectors. An affine transformation is one which
pub trait Transform<S: BaseNum, V: Vector<S>, P: Point<S, V>>: Sized + PhantomFn<S> {
2014-05-25 11:17:26 +00:00
/// Create an identity transformation. That is, a transformation which
/// does nothing.
fn identity() -> Self;
2014-05-25 11:17:26 +00:00
/// Create a transformation that rotates a vector to look at `center` from
/// `eye`, using `up` for orientation.
fn look_at(eye: &P, center: &P, up: &V) -> Self;
2013-11-02 13:11:13 +00:00
2014-05-25 11:17:26 +00:00
/// Transform a vector using this transform.
fn transform_vector(&self, vec: &V) -> V;
2014-05-25 11:17:26 +00:00
/// Transform a point using this transform.
fn transform_point(&self, point: &P) -> P;
2014-05-25 11:17:26 +00:00
/// Transform a ray using this transform.
#[inline]
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P, V> {
2014-06-15 18:18:31 +00:00
Ray::new(self.transform_point(&ray.origin), self.transform_vector(&ray.direction))
}
2014-05-25 11:17:26 +00:00
/// Transform a vector as a point using this transform.
2013-11-02 14:18:37 +00:00
#[inline]
2014-05-25 11:17:26 +00:00
fn transform_as_point(&self, vec: &V) -> V {
2014-06-15 18:18:31 +00:00
self.transform_point(&Point::from_vec(vec)).to_vec()
2013-11-02 14:18:37 +00:00
}
2014-05-25 11:17:26 +00:00
/// Combine this transform with another, yielding a new transformation
/// which has the effects of both.
2013-11-02 13:11:13 +00:00
fn concat(&self, other: &Self) -> Self;
2014-05-25 11:17:26 +00:00
/// Create a transform that "un-does" this one.
2013-11-02 13:11:13 +00:00
fn invert(&self) -> Option<Self>;
2014-05-25 11:17:26 +00:00
/// Combine this transform with another, in-place.
2013-11-02 13:11:13 +00:00
#[inline]
fn concat_self(&mut self, other: &Self) {
*self = Transform::concat(self, other);
2013-11-02 13:11:13 +00:00
}
2014-05-25 11:17:26 +00:00
/// Invert this transform in-place, failing if the transformation is not
/// invertible.
#[inline]
2014-05-25 11:17:26 +00:00
fn invert_self(&mut self) {
*self = self.invert().unwrap()
}
2013-10-13 00:00:51 +00:00
}
/// A generic transformation consisting of a rotation,
2013-10-13 00:00:51 +00:00
/// displacement vector and scale amount.
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
2014-06-15 18:18:31 +00:00
pub struct Decomposed<S, V, R> {
2014-04-01 11:00:17 +00:00
pub scale: S,
pub rot: R,
pub disp: V,
2013-10-13 00:00:51 +00:00
}
impl<
S: BaseFloat,
V: Vector<S>,
P: Point<S, V>,
R: Rotation<S, V, P>
> Transform<S, V, P> for Decomposed<S, V, R> {
#[inline]
2014-06-15 18:18:31 +00:00
fn identity() -> Decomposed<S, V, R> {
Decomposed {
scale: one(),
rot: Rotation::identity(),
disp: zero(),
}
}
#[inline]
2014-06-15 18:18:31 +00:00
fn look_at(eye: &P, center: &P, up: &V) -> Decomposed<S, V, R> {
let origin: P = Point::origin();
let rot: R = Rotation::look_at(&center.sub_p(eye), up);
let disp: V = rot.rotate_vector(&origin.sub_p(eye));
Decomposed {
scale: one(),
rot: rot,
disp: disp,
}
}
#[inline]
fn transform_vector(&self, vec: &V) -> V {
2014-06-15 18:18:31 +00:00
self.rot.rotate_vector(&vec.mul_s(self.scale.clone()))
}
#[inline]
2013-11-09 01:15:51 +00:00
fn transform_point(&self, point: &P) -> P {
2014-06-15 18:18:31 +00:00
self.rot.rotate_point(&point.mul_s(self.scale.clone())).add_v(&self.disp)
}
2014-06-15 18:18:31 +00:00
fn concat(&self, other: &Decomposed<S, V, R>) -> Decomposed<S, V, R> {
2013-11-02 13:11:13 +00:00
Decomposed {
scale: self.scale * other.scale,
2014-06-15 18:18:31 +00:00
rot: self.rot.concat(&other.rot),
disp: self.transform_as_point(&other.disp),
2013-11-02 13:11:13 +00:00
}
}
2014-06-15 18:18:31 +00:00
fn invert(&self) -> Option<Decomposed<S, V, R>> {
if self.scale.approx_eq(&zero()) {
None
2014-06-15 18:18:31 +00:00
} else {
let s = one::<S>() / self.scale;
let r = self.rot.invert();
2014-06-15 18:18:31 +00:00
let d = r.rotate_vector(&self.disp).mul_s(-s);
Some(Decomposed {
scale: s,
rot: r,
disp: d,
})
}
}
}
pub trait Transform3<S>: Transform<S, Vector3<S>, Point3<S>> + ToMatrix4<S> {}
impl<
S: BaseFloat + 'static,
R: Rotation3<S>
> ToMatrix4<S> for Decomposed<S, Vector3<S>, R> {
fn to_matrix4(&self) -> Matrix4<S> {
2014-06-15 18:18:31 +00:00
let mut m = self.rot.to_matrix3().mul_s(self.scale.clone()).to_matrix4();
m.w = self.disp.extend(one());
m
}
}
impl<
S: BaseFloat + 'static,
R: Rotation3<S>
> Transform3<S> for Decomposed<S, Vector3<S>, R> {}
impl<
S: BaseFloat,
R: fmt::Debug + Rotation3<S>
> fmt::Debug for Decomposed<S, Vector3<S>, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2015-01-09 22:06:45 +00:00
write!(f, "(scale({:?}), rot({:?}), disp{:?})",
self.scale, self.rot, self.disp)
}
}
/// A homogeneous transformation matrix.
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct AffineMatrix3<S> {
pub mat: Matrix4<S>,
}
impl<S: BaseFloat + 'static> Transform<S, Vector3<S>, Point3<S>> for AffineMatrix3<S> {
#[inline]
2013-11-09 01:15:51 +00:00
fn identity() -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::identity() }
}
#[inline]
fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vector3<S>) -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::look_at(eye, center, up) }
}
2014-05-25 11:17:26 +00:00
#[inline]
fn transform_vector(&self, vec: &Vector3<S>) -> Vector3<S> {
self.mat.mul_v(&vec.extend(zero())).truncate()
}
#[inline]
2013-11-09 01:15:51 +00:00
fn transform_point(&self, point: &Point3<S>) -> Point3<S> {
2014-06-15 18:18:31 +00:00
Point3::from_homogeneous(&self.mat.mul_v(&point.to_homogeneous()))
}
2013-11-02 13:11:13 +00:00
#[inline]
2013-11-09 01:15:51 +00:00
fn concat(&self, other: &AffineMatrix3<S>) -> AffineMatrix3<S> {
2014-06-15 18:18:31 +00:00
AffineMatrix3 { mat: self.mat.mul_m(&other.mat) }
2013-11-02 13:11:13 +00:00
}
#[inline]
2013-11-09 01:15:51 +00:00
fn invert(&self) -> Option<AffineMatrix3<S>> {
self.mat.invert().map(|m| AffineMatrix3{ mat: m })
2014-05-25 11:17:26 +00:00
}
}
2014-05-26 17:10:04 +00:00
impl<S: BaseNum> ToMatrix4<S> for AffineMatrix3<S> {
#[inline] fn to_matrix4(&self) -> Matrix4<S> { self.mat.clone() }
}
2014-12-15 12:49:57 +00:00
impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> where S: 'static {}
/// A trait that allows extracting components (rotation, translation, scale)
/// from an arbitrary transformation/
pub trait ToComponents<S, V: Vector<S>, P: Point<S, V>> {
/// Associated rotation type
type Rotation;
/// Extract translation component
fn to_translation(&self) -> V;
/// Extract rotation component
fn to_rotation(&self) -> Self::Rotation;
/// Extract scale component
fn to_scale(&self) -> V;
}
impl<
S: BaseFloat,
V: Vector<S> + Clone,
P: Point<S, V>,
R: Rotation<S, V, P> + Clone,
> ToComponents<S, V, P> for Decomposed<S, V, R> {
type Rotation = R;
fn to_translation(&self) -> V {
self.disp.clone()
}
fn to_rotation(&self) -> R {
self.rot.clone()
}
fn to_scale(&self) -> V {
Vector::from_value(self.scale)
}
}
pub trait ToComponents3<S>: ToComponents<S, Vector3<S>, Point3<S>>
where Self::Rotation: ToMatrix3<S> {}