2014-01-25 15:54:54 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-11-20 15:28:02 +00:00
|
|
|
use std::fmt;
|
2014-01-29 02:01:57 +00:00
|
|
|
|
2015-09-29 11:36:57 +00:00
|
|
|
use rust_num::{Zero, One};
|
2015-04-05 01:19:11 +00:00
|
|
|
|
2014-01-09 00:26:50 +00:00
|
|
|
use approx::ApproxEq;
|
2015-03-01 06:29:47 +00:00
|
|
|
use matrix::*;
|
2015-03-02 04:05:47 +00:00
|
|
|
use num::*;
|
|
|
|
use point::*;
|
|
|
|
use rotation::*;
|
|
|
|
use vector::*;
|
2013-10-31 21:36:21 +00:00
|
|
|
|
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
|
2015-11-03 04:23:22 +00:00
|
|
|
pub trait Transform<P: Point>: Sized {
|
2014-05-25 11:17:26 +00:00
|
|
|
/// Create an identity transformation. That is, a transformation which
|
|
|
|
/// does nothing.
|
2015-10-01 08:52:44 +00:00
|
|
|
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.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn look_at(eye: P, center: P, up: P::Vector) -> Self;
|
2013-11-02 13:11:13 +00:00
|
|
|
|
2014-05-25 11:17:26 +00:00
|
|
|
/// Transform a vector using this transform.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_vector(&self, vec: P::Vector) -> P::Vector;
|
2014-05-25 11:17:26 +00:00
|
|
|
|
|
|
|
/// Transform a point using this transform.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_point(&self, point: P) -> P;
|
2013-10-31 21:36:21 +00:00
|
|
|
|
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]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_as_point(&self, vec: P::Vector) -> P::Vector {
|
|
|
|
self.transform_point(P::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) {
|
2015-09-29 11:36:57 +00:00
|
|
|
*self = Self::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.
|
2013-11-01 23:42:09 +00:00
|
|
|
#[inline]
|
2014-05-25 11:17:26 +00:00
|
|
|
fn invert_self(&mut self) {
|
|
|
|
*self = self.invert().unwrap()
|
2013-11-01 23:42:09 +00:00
|
|
|
}
|
2013-10-13 00:00:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 12:29:01 +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)]
|
2015-11-03 04:23:22 +00:00
|
|
|
pub struct Decomposed<V: Vector, 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
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
|
2015-11-03 04:40:52 +00:00
|
|
|
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
2015-11-03 04:50:07 +00:00
|
|
|
<P as Point>::Scalar: BaseFloat,
|
2015-12-12 12:24:14 +00:00
|
|
|
// FIXME: Investigate why this is needed!
|
|
|
|
<P as Point>::Vector: Vector,
|
2015-11-03 04:23:22 +00:00
|
|
|
{
|
2013-11-01 23:42:09 +00:00
|
|
|
#[inline]
|
2015-11-03 04:23:22 +00:00
|
|
|
fn one() -> Decomposed<P::Vector, R> {
|
2013-11-01 23:42:09 +00:00
|
|
|
Decomposed {
|
2015-11-03 04:50:07 +00:00
|
|
|
scale: <P as Point>::Scalar::one(),
|
2015-10-01 08:52:44 +00:00
|
|
|
rot: R::one(),
|
2015-10-03 05:17:09 +00:00
|
|
|
disp: P::Vector::zero(),
|
2013-11-01 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-25 15:54:54 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn look_at(eye: P, center: P, up: P::Vector) -> Decomposed<P::Vector, R> {
|
2015-12-12 11:31:10 +00:00
|
|
|
let rot = R::look_at(center - eye, up);
|
|
|
|
let disp = rot.rotate_vector(P::origin() - eye);
|
2014-01-25 15:54:54 +00:00
|
|
|
Decomposed {
|
2015-11-03 04:50:07 +00:00
|
|
|
scale: <P as Point>::Scalar::one(),
|
2014-01-25 15:54:54 +00:00
|
|
|
rot: rot,
|
|
|
|
disp: disp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 21:36:21 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_vector(&self, vec: P::Vector) -> P::Vector {
|
2015-12-12 12:24:14 +00:00
|
|
|
self.rot.rotate_vector(vec * self.scale)
|
2013-10-31 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_point(&self, point: P) -> P {
|
2015-12-12 11:31:10 +00:00
|
|
|
self.rot.rotate_point(point * self.scale) + self.disp
|
2013-10-31 21:36:21 +00:00
|
|
|
}
|
2013-11-01 23:42:09 +00:00
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
fn concat(&self, other: &Decomposed<P::Vector, R>) -> Decomposed<P::Vector, 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),
|
2015-11-09 09:12:04 +00:00
|
|
|
disp: self.transform_as_point(other.disp.clone()),
|
2013-11-02 13:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
fn invert(&self) -> Option<Decomposed<P::Vector, R>> {
|
2015-11-03 04:50:07 +00:00
|
|
|
if self.scale.approx_eq(&<P as Point>::Scalar::zero()) {
|
2013-11-01 23:42:09 +00:00
|
|
|
None
|
2014-06-15 18:18:31 +00:00
|
|
|
} else {
|
2015-11-03 04:50:07 +00:00
|
|
|
let s = <P as Point>::Scalar::one() / self.scale;
|
2013-11-01 23:42:09 +00:00
|
|
|
let r = self.rot.invert();
|
2015-12-12 12:24:14 +00:00
|
|
|
let d = r.rotate_vector(self.disp.clone()) * -s;
|
2014-06-15 18:18:31 +00:00
|
|
|
Some(Decomposed {
|
2013-11-01 23:42:09 +00:00
|
|
|
scale: s,
|
|
|
|
rot: r,
|
|
|
|
disp: d,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
pub trait Transform2<S: BaseNum>: Transform<Point2<S>> + Into<Matrix3<S>> {}
|
|
|
|
pub trait Transform3<S: BaseNum>: Transform<Point3<S>> + Into<Matrix4<S>> {}
|
2013-11-01 14:32:29 +00:00
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
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();
|
2015-11-08 06:41:57 +00:00
|
|
|
let mut m: Matrix3<_> = (&m * dec.scale).into();
|
2015-09-29 11:36:57 +00:00
|
|
|
m.z = dec.disp.extend(S::one());
|
2015-03-02 04:05:47 +00:00
|
|
|
m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
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();
|
2015-11-08 06:41:57 +00:00
|
|
|
let mut m: Matrix4<_> = (&m * dec.scale).into();
|
2015-09-29 11:36:57 +00:00
|
|
|
m.w = dec.disp.extend(S::one());
|
2013-11-01 14:32:29 +00:00
|
|
|
m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R> {}
|
2015-03-02 04:05:47 +00:00
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}
|
2013-11-01 14:32:29 +00:00
|
|
|
|
|
|
|
/// A homogeneous transformation matrix.
|
2015-01-03 21:29:26 +00:00
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
|
2013-11-01 14:32:29 +00:00
|
|
|
pub struct AffineMatrix3<S> {
|
2014-04-14 01:30:24 +00:00
|
|
|
pub mat: Matrix4<S>,
|
2013-11-01 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat> Transform<Point3<S>> for AffineMatrix3<S> {
|
2013-11-01 23:42:09 +00:00
|
|
|
#[inline]
|
2015-10-01 08:52:44 +00:00
|
|
|
fn one() -> AffineMatrix3<S> {
|
2015-12-06 08:36:38 +00:00
|
|
|
AffineMatrix3 { mat: Matrix4::identity() }
|
2013-11-01 23:42:09 +00:00
|
|
|
}
|
2014-01-25 15:54:54 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn look_at(eye: Point3<S>, center: Point3<S>, up: Vector3<S>) -> AffineMatrix3<S> {
|
2014-04-14 01:30:24 +00:00
|
|
|
AffineMatrix3 { mat: Matrix4::look_at(eye, center, up) }
|
2014-01-25 15:54:54 +00:00
|
|
|
}
|
2014-05-25 11:17:26 +00:00
|
|
|
|
2013-11-01 14:32:29 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_vector(&self, vec: Vector3<S>) -> Vector3<S> {
|
2015-12-21 10:02:40 +00:00
|
|
|
(self.mat * vec.extend(S::zero())).truncate()
|
2013-11-01 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn transform_point(&self, point: Point3<S>) -> Point3<S> {
|
2015-12-21 10:02:40 +00:00
|
|
|
Point3::from_homogeneous(self.mat * point.to_homogeneous())
|
2013-11-01 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2015-12-21 10:02:40 +00:00
|
|
|
AffineMatrix3 { mat: self.mat * other.mat }
|
2013-11-02 13:11:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 23:42:09 +00:00
|
|
|
#[inline]
|
2013-11-09 01:15:51 +00:00
|
|
|
fn invert(&self) -> Option<AffineMatrix3<S>> {
|
2013-11-01 23:42:09 +00:00
|
|
|
self.mat.invert().map(|m| AffineMatrix3{ mat: m })
|
2014-05-25 11:17:26 +00:00
|
|
|
}
|
2013-11-01 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 08:27:52 +00:00
|
|
|
impl<S: BaseNum> From<AffineMatrix3<S>> for Matrix4<S> {
|
|
|
|
#[inline] fn from(aff: AffineMatrix3<S>) -> Matrix4<S> { aff.mat }
|
2013-11-01 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 05:17:09 +00:00
|
|
|
impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {}
|
2015-12-29 10:50:43 +00:00
|
|
|
|
|
|
|
impl<S: fmt::Debug> fmt::Debug for AffineMatrix3<S> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
try!(write!(f, "AffineMatrix3 "));
|
|
|
|
<[[S; 4]; 4] as fmt::Debug>::fmt(self.mat.as_ref(), f)
|
|
|
|
}
|
|
|
|
}
|