cgmath/src/transform.rs

150 lines
4.9 KiB
Rust
Raw Normal View History

// Copyright 2014 The CGMath Developers. For a full listing of the authors,
2015-03-14 02:49:46 +00:00
// refer to the Cargo.toml file at the top-level directory of this distribution.
2013-10-13 00:00:51 +00:00
//
// 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.
2016-04-19 10:51:40 +00:00
use structure::*;
2014-01-09 00:26:50 +00:00
use approx::ApproxEq;
2016-04-19 10:51:40 +00:00
use matrix::{Matrix2, Matrix3, Matrix4};
use num::{BaseFloat, BaseNum};
use point::{Point2, Point3};
use rotation::*;
2016-04-19 10:51:40 +00:00
use vector::{Vector2, 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
2016-04-08 09:56:30 +00:00
pub trait Transform<P: EuclideanSpace>: Sized {
2014-05-25 11:17:26 +00:00
/// Create an identity transformation. That is, a transformation which
/// does nothing.
fn one() -> 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.
2016-04-08 05:35:11 +00:00
fn look_at(eye: P, center: P, up: P::Diff) -> Self;
2013-11-02 13:11:13 +00:00
2014-05-25 11:17:26 +00:00
/// Transform a vector using this transform.
2016-04-08 05:35:11 +00:00
fn transform_vector(&self, vec: P::Diff) -> P::Diff;
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
/// 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.
fn inverse_transform(&self) -> Option<Self>;
2013-11-02 13:11:13 +00:00
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 = Self::concat(self, other);
2013-11-02 13:11:13 +00:00
}
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-12-29 10:50:43 +00:00
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Decomposed<V: VectorSpace, R> {
pub scale: V::Scalar,
2014-04-01 11:00:17 +00:00
pub rot: R,
pub disp: V,
2013-10-13 00:00:51 +00:00
}
2016-04-08 09:56:30 +00:00
impl<P: EuclideanSpace, R: Rotation<P>> Transform<P> for Decomposed<P::Diff, R> where
2015-11-03 04:40:52 +00:00
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
2016-04-08 09:56:30 +00:00
<P as EuclideanSpace>::Scalar: BaseFloat,
// FIXME: Investigate why this is needed!
2016-04-08 09:56:30 +00:00
<P as EuclideanSpace>::Diff: VectorSpace,
{
#[inline]
2016-04-08 05:35:11 +00:00
fn one() -> Decomposed<P::Diff, R> {
Decomposed {
2016-04-19 10:51:40 +00:00
scale: P::Scalar::one(),
rot: R::one(),
2016-04-08 05:35:11 +00:00
disp: P::Diff::zero(),
}
}
#[inline]
2016-04-08 05:35:11 +00:00
fn look_at(eye: P, center: P, up: P::Diff) -> Decomposed<P::Diff, R> {
let rot = R::look_at(center - eye, up);
let disp = rot.rotate_vector(P::origin() - eye);
Decomposed {
2016-04-19 10:51:40 +00:00
scale: P::Scalar::one(),
rot: rot,
disp: disp,
}
}
#[inline]
2016-04-08 05:35:11 +00:00
fn transform_vector(&self, vec: P::Diff) -> P::Diff {
self.rot.rotate_vector(vec * self.scale)
}
#[inline]
fn transform_point(&self, point: P) -> P {
self.rot.rotate_point(point * self.scale) + self.disp
}
2016-04-08 05:35:11 +00:00
fn concat(&self, other: &Decomposed<P::Diff, R>) -> Decomposed<P::Diff, 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),
2016-05-11 22:12:34 +00:00
disp: self.disp + other.disp,
2013-11-02 13:11:13 +00:00
}
}
fn inverse_transform(&self) -> Option<Decomposed<P::Diff, R>> {
2016-04-19 10:51:40 +00:00
if self.scale.approx_eq(&P::Scalar::zero()) {
None
2014-06-15 18:18:31 +00:00
} else {
2016-04-19 10:51:40 +00:00
let s = P::Scalar::one() / self.scale;
let r = self.rot.invert();
let d = r.rotate_vector(self.disp.clone()) * -s;
2014-06-15 18:18:31 +00:00
Some(Decomposed {
scale: s,
rot: r,
disp: d,
})
}
}
}
pub trait Transform2<S: BaseNum>: Transform<Point2<S>> + Into<Matrix3<S>> {}
pub trait Transform3<S: BaseNum>: Transform<Point3<S>> + Into<Matrix4<S>> {}
impl<S: BaseFloat, R: Rotation2<S>> From<Decomposed<Vector2<S>, R>> for Matrix3<S> {
fn from(dec: Decomposed<Vector2<S>, R>) -> Matrix3<S> {
2015-05-06 08:27:52 +00:00
let m: Matrix2<_> = dec.rot.into();
let mut m: Matrix3<_> = (&m * dec.scale).into();
m.z = dec.disp.extend(S::one());
m
}
}
impl<S: BaseFloat, R: Rotation3<S>> From<Decomposed<Vector3<S>, R>> for Matrix4<S> {
fn from(dec: Decomposed<Vector3<S>, R>) -> Matrix4<S> {
2015-05-06 08:27:52 +00:00
let m: Matrix3<_> = dec.rot.into();
let mut m: Matrix4<_> = (&m * dec.scale).into();
m.w = dec.disp.extend(S::one());
m
}
}
impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R> {}
impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}