2018-11-08 14:51:43 +08:00
|
|
|
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
|
2016-12-05 05:44:42 +08:00
|
|
|
use std::any::Any;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2017-05-04 10:02:30 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-10-22 13:00:10 +08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2017-05-04 10:02:30 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::allocator::Allocator;
|
|
|
|
use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
|
|
|
|
use crate::base::storage::Owned;
|
2019-03-31 16:48:59 +08:00
|
|
|
use crate::base::{DefaultAllocator, MatrixN, VectorN};
|
2019-02-25 00:29:27 +08:00
|
|
|
|
2019-03-31 16:48:59 +08:00
|
|
|
use crate::geometry::Point;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/// Trait implemented by phantom types identifying the projective transformation type.
|
|
|
|
///
|
2018-11-08 14:51:43 +08:00
|
|
|
/// NOTE: this trait is not intended to be implemented outside of the `nalgebra` crate.
|
2016-12-05 05:44:42 +08:00
|
|
|
pub trait TCategory: Any + Debug + Copy + PartialEq + Send {
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Indicates whether a `Transform` with the category `Self` has a bottom-row different from
|
|
|
|
/// `0 0 .. 1`.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn has_normalizer() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that the given matrix is a valid homogeneous representation of an element of the
|
|
|
|
/// category `Self`.
|
2019-03-25 18:21:41 +08:00
|
|
|
fn check_homogeneous_invariants<N: RealField, D: DimName>(mat: &MatrixN<N, D>) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, D, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Traits that gives the `Transform` category that is compatible with the result of the
|
2016-12-05 05:44:42 +08:00
|
|
|
/// multiplication of transformations with categories `Self` and `Other`.
|
|
|
|
pub trait TCategoryMul<Other: TCategory>: TCategory {
|
2017-02-13 01:17:09 +08:00
|
|
|
/// The transform category that results from the multiplication of a `Transform<Self>` to a
|
|
|
|
/// `Transform<Other>`. This is usually equal to `Self` or `Other`, whichever is the most
|
|
|
|
/// general category.
|
2016-12-05 05:44:42 +08:00
|
|
|
type Representative: TCategory;
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Indicates that `Self` is a more general `Transform` category than `Other`.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub trait SuperTCategoryOf<Other: TCategory>: TCategory {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Indicates that `Self` is a more specific `Transform` category than `Other`.
|
2016-12-05 05:44:42 +08:00
|
|
|
///
|
|
|
|
/// Automatically implemented based on `SuperTCategoryOf`.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub trait SubTCategoryOf<Other: TCategory>: TCategory {}
|
2016-12-05 05:44:42 +08:00
|
|
|
impl<T1, T2> SubTCategoryOf<T2> for T1
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
T1: TCategory,
|
|
|
|
T2: SuperTCategoryOf<T1>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Tag representing the most general (not necessarily inversible) `Transform` type.
|
2017-02-16 05:04:34 +08:00
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
2018-10-22 13:00:10 +08:00
|
|
|
pub enum TGeneral {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Tag representing the most general inversible `Transform` type.
|
2017-02-16 05:04:34 +08:00
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
2018-10-22 13:00:10 +08:00
|
|
|
pub enum TProjective {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Tag representing an affine `Transform`. Its bottom-row is equal to `(0, 0 ... 0, 1)`.
|
2017-02-16 05:04:34 +08:00
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
2018-10-22 13:00:10 +08:00
|
|
|
pub enum TAffine {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
impl TCategory for TGeneral {
|
|
|
|
#[inline]
|
2019-03-25 18:21:41 +08:00
|
|
|
fn check_homogeneous_invariants<N: RealField, D: DimName>(_: &MatrixN<N, D>) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TCategory for TProjective {
|
|
|
|
#[inline]
|
2019-03-25 18:21:41 +08:00
|
|
|
fn check_homogeneous_invariants<N: RealField, D: DimName>(mat: &MatrixN<N, D>) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
mat.is_invertible()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TCategory for TAffine {
|
|
|
|
#[inline]
|
|
|
|
fn has_normalizer() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-03-25 18:21:41 +08:00
|
|
|
fn check_homogeneous_invariants<N: RealField, D: DimName>(mat: &MatrixN<N, D>) -> bool
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
|
|
|
{
|
2017-07-29 00:57:44 +08:00
|
|
|
let last = D::dim() - 1;
|
2018-10-22 13:00:10 +08:00
|
|
|
mat.is_invertible()
|
|
|
|
&& mat[(last, last)] == N::one()
|
2018-02-02 19:26:35 +08:00
|
|
|
&& (0..last).all(|i| mat[(last, i)].is_zero())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! category_mul_impl(
|
|
|
|
($($a: ident * $b: ident => $c: ty);* $(;)*) => {$(
|
|
|
|
impl TCategoryMul<$a> for $b {
|
|
|
|
type Representative = $c;
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
// We require stability uppon multiplication.
|
|
|
|
impl<T: TCategory> TCategoryMul<T> for T {
|
|
|
|
type Representative = T;
|
|
|
|
}
|
|
|
|
|
|
|
|
category_mul_impl!(
|
2017-08-03 01:37:44 +08:00
|
|
|
// TGeneral * TGeneral => TGeneral;
|
|
|
|
TGeneral * TProjective => TGeneral;
|
|
|
|
TGeneral * TAffine => TGeneral;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
TProjective * TGeneral => TGeneral;
|
|
|
|
// TProjective * TProjective => TProjective;
|
|
|
|
TProjective * TAffine => TProjective;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
TAffine * TGeneral => TGeneral;
|
|
|
|
TAffine * TProjective => TProjective;
|
|
|
|
// TAffine * TAffine => TAffine;
|
2016-12-05 05:44:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! super_tcategory_impl(
|
|
|
|
($($a: ident >= $b: ident);* $(;)*) => {$(
|
|
|
|
impl SuperTCategoryOf<$b> for $a { }
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
impl<T: TCategory> SuperTCategoryOf<T> for T {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
super_tcategory_impl!(
|
2017-08-03 01:37:44 +08:00
|
|
|
TGeneral >= TProjective;
|
|
|
|
TGeneral >= TAffine;
|
2016-12-05 05:44:42 +08:00
|
|
|
TProjective >= TAffine;
|
|
|
|
);
|
|
|
|
|
|
|
|
/// A transformation matrix in homogeneous coordinates.
|
|
|
|
///
|
|
|
|
/// It is stored as a matrix with dimensions `(D + 1, D + 1)`, e.g., it stores a 4x4 matrix for a
|
|
|
|
/// 3D transformation.
|
|
|
|
#[repr(C)]
|
2017-08-03 01:37:44 +08:00
|
|
|
#[derive(Debug)]
|
2019-03-25 18:21:41 +08:00
|
|
|
pub struct Transform<N: RealField, D: DimNameAdd<U1>, C: TCategory>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
|
|
|
matrix: MatrixN<N, DimNameSum<D, U1>>,
|
|
|
|
_phantom: PhantomData<C>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
// FIXME
|
2019-03-25 18:21:41 +08:00
|
|
|
// impl<N: RealField + hash::Hash, D: DimNameAdd<U1> + hash::Hash, C: TCategory> hash::Hash for Transform<N, D, C>
|
2017-08-03 01:37:44 +08:00
|
|
|
// where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
// Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: hash::Hash {
|
|
|
|
// fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
// self.matrix.hash(state);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1> + Copy, C: TCategory> Copy for Transform<N, D, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: Copy,
|
2018-11-08 14:51:43 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> Clone for Transform<N, D, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Transform::from_matrix_unchecked(self.matrix.clone())
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> Serialize for Transform<N, D, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-09-13 12:55:58 +08:00
|
|
|
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: Serialize,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix.serialize(serializer)
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<'a, N: RealField, D: DimNameAdd<U1>, C: TCategory> Deserialize<'a> for Transform<N, D, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-09-13 12:55:58 +08:00
|
|
|
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: Deserialize<'a>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
Des: Deserializer<'a>,
|
|
|
|
{
|
2018-02-02 19:26:35 +08:00
|
|
|
let matrix = MatrixN::<N, DimNameSum<D, U1>>::deserialize(deserializer)?;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
Ok(Transform::from_matrix_unchecked(matrix))
|
|
|
|
}
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<N: RealField + Eq, D: DimNameAdd<U1>, C: TCategory> Eq for Transform<N, D, C> where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
{
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> PartialEq for Transform<N, D, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, right: &Self) -> bool {
|
|
|
|
self.matrix == right.matrix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> Transform<N, D, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new transformation from the given homogeneous matrix. The transformation category
|
|
|
|
/// of `Self` is not checked to be verified by the given matrix.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn from_matrix_unchecked(matrix: MatrixN<N, DimNameSum<D, U1>>) -> Self {
|
|
|
|
Transform {
|
2020-10-11 16:57:26 +08:00
|
|
|
matrix,
|
2018-02-02 19:26:35 +08:00
|
|
|
_phantom: PhantomData,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:51:43 +08:00
|
|
|
/// Retrieves the underlying matrix.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(1.0, 2.0, 0.0,
|
|
|
|
/// 3.0, 4.0, 0.0,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
2018-12-10 04:24:08 +08:00
|
|
|
/// assert_eq!(t.into_inner(), m);
|
2018-11-08 14:51:43 +08:00
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2018-12-10 04:24:08 +08:00
|
|
|
pub fn into_inner(self) -> MatrixN<N, DimNameSum<D, U1>> {
|
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the underlying matrix.
|
|
|
|
/// Deprecated: Use [Transform::into_inner] instead.
|
2020-03-21 19:16:46 +08:00
|
|
|
#[deprecated(note = "use `.into_inner()` instead")]
|
2018-12-10 04:24:08 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn unwrap(self) -> MatrixN<N, DimNameSum<D, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// A reference to the underlying matrix.
|
2018-11-08 14:51:43 +08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(1.0, 2.0, 0.0,
|
|
|
|
/// 3.0, 4.0, 0.0,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// assert_eq!(*t.matrix(), m);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn matrix(&self) -> &MatrixN<N, DimNameSum<D, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
&self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A mutable reference to the underlying matrix.
|
|
|
|
///
|
|
|
|
/// It is `_unchecked` because direct modifications of this matrix may break invariants
|
|
|
|
/// identified by this transformation category.
|
2018-11-08 14:51:43 +08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(1.0, 2.0, 0.0,
|
|
|
|
/// 3.0, 4.0, 0.0,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let mut t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// t.matrix_mut_unchecked().m12 = 42.0;
|
|
|
|
/// t.matrix_mut_unchecked().m23 = 90.0;
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let expected = Matrix3::new(1.0, 42.0, 0.0,
|
|
|
|
/// 3.0, 4.0, 90.0,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// assert_eq!(*t.matrix(), expected);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn matrix_mut_unchecked(&mut self) -> &mut MatrixN<N, DimNameSum<D, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
&mut self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the category of this transform.
|
|
|
|
///
|
|
|
|
/// This can be done only if the new category is more general than the current one, e.g., a
|
|
|
|
/// transform with category `TProjective` cannot be converted to a transform with category
|
|
|
|
/// `TAffine` because not all projective transformations are affine (the other way-round is
|
|
|
|
/// valid though).
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn set_category<CNew: SuperTCategoryOf<C>>(self) -> Transform<N, D, CNew> {
|
|
|
|
Transform::from_matrix_unchecked(self.matrix)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones this transform into one that owns its data.
|
|
|
|
#[inline]
|
2018-11-08 14:51:43 +08:00
|
|
|
#[deprecated(
|
|
|
|
note = "This method is redundant with automatic `Copy` and the `.clone()` method and will be removed in a future release."
|
|
|
|
)]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn clone_owned(&self) -> Transform<N, D, C> {
|
|
|
|
Transform::from_matrix_unchecked(self.matrix.clone_owned())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts this transform into its equivalent homogeneous transformation matrix.
|
2018-11-08 14:51:43 +08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(1.0, 2.0, 0.0,
|
|
|
|
/// 3.0, 4.0, 0.0,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
2018-12-10 04:24:08 +08:00
|
|
|
/// assert_eq!(t.into_inner(), m);
|
2018-11-08 14:51:43 +08:00
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn to_homogeneous(&self) -> MatrixN<N, DimNameSum<D, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix().clone_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to invert this transformation. You may use `.inverse` instead of this
|
2018-11-08 14:51:43 +08:00
|
|
|
/// transformation has a subcategory of `TProjective` (i.e. if it is a `Projective{2,3}` or `Affine{2,3}`).
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(2.0, 2.0, -0.3,
|
|
|
|
/// 3.0, 4.0, 0.1,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// let inv_t = t.try_inverse().unwrap();
|
|
|
|
/// assert_relative_eq!(t * inv_t, Transform2::identity());
|
|
|
|
/// assert_relative_eq!(inv_t * t, Transform2::identity());
|
|
|
|
///
|
|
|
|
/// // Non-invertible case.
|
|
|
|
/// let m = Matrix3::new(0.0, 2.0, 1.0,
|
|
|
|
/// 3.0, 0.0, 5.0,
|
|
|
|
/// 0.0, 0.0, 0.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// assert!(t.try_inverse().is_none());
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2019-06-06 05:04:04 +08:00
|
|
|
#[must_use = "Did you mean to use try_inverse_mut()?"]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn try_inverse(self) -> Option<Transform<N, D, C>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
if let Some(m) = self.matrix.try_inverse() {
|
2017-08-03 01:37:44 +08:00
|
|
|
Some(Transform::from_matrix_unchecked(m))
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2016-12-05 05:44:42 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inverts this transformation. Use `.try_inverse` if this transform has the `TGeneral`
|
2018-11-08 14:51:43 +08:00
|
|
|
/// category (i.e., a `Transform{2,3}` may not be invertible).
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Matrix3, Projective2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(2.0, 2.0, -0.3,
|
|
|
|
/// 3.0, 4.0, 0.1,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let proj = Projective2::from_matrix_unchecked(m);
|
|
|
|
/// let inv_t = proj.inverse();
|
|
|
|
/// assert_relative_eq!(proj * inv_t, Projective2::identity());
|
|
|
|
/// assert_relative_eq!(inv_t * proj, Projective2::identity());
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2019-06-06 05:04:04 +08:00
|
|
|
#[must_use = "Did you mean to use inverse_mut()?"]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn inverse(self) -> Transform<N, D, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
C: SubTCategoryOf<TProjective>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
// FIXME: specialize for TAffine?
|
2017-08-03 01:37:44 +08:00
|
|
|
Transform::from_matrix_unchecked(self.matrix.try_inverse().unwrap())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to invert this transformation in-place. You may use `.inverse_mut` instead of this
|
|
|
|
/// transformation has a subcategory of `TProjective`.
|
2018-11-08 14:51:43 +08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Matrix3, Transform2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(2.0, 2.0, -0.3,
|
|
|
|
/// 3.0, 4.0, 0.1,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// let mut inv_t = t;
|
|
|
|
/// assert!(inv_t.try_inverse_mut());
|
|
|
|
/// assert_relative_eq!(t * inv_t, Transform2::identity());
|
|
|
|
/// assert_relative_eq!(inv_t * t, Transform2::identity());
|
|
|
|
///
|
|
|
|
/// // Non-invertible case.
|
|
|
|
/// let m = Matrix3::new(0.0, 2.0, 1.0,
|
|
|
|
/// 3.0, 0.0, 5.0,
|
|
|
|
/// 0.0, 0.0, 0.0);
|
|
|
|
/// let mut t = Transform2::from_matrix_unchecked(m);
|
|
|
|
/// assert!(!t.try_inverse_mut());
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn try_inverse_mut(&mut self) -> bool {
|
|
|
|
self.matrix.try_inverse_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inverts this transformation in-place. Use `.try_inverse_mut` if this transform has the
|
|
|
|
/// `TGeneral` category (it may not be invertible).
|
2018-11-08 14:51:43 +08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Matrix3, Projective2};
|
|
|
|
///
|
|
|
|
/// let m = Matrix3::new(2.0, 2.0, -0.3,
|
|
|
|
/// 3.0, 4.0, 0.1,
|
|
|
|
/// 0.0, 0.0, 1.0);
|
|
|
|
/// let proj = Projective2::from_matrix_unchecked(m);
|
|
|
|
/// let mut inv_t = proj;
|
|
|
|
/// inv_t.inverse_mut();
|
|
|
|
/// assert_relative_eq!(proj * inv_t, Projective2::identity());
|
|
|
|
/// assert_relative_eq!(inv_t * proj, Projective2::identity());
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn inverse_mut(&mut self)
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
C: SubTCategoryOf<TProjective>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
let _ = self.matrix.try_inverse_mut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 00:29:27 +08:00
|
|
|
impl<N, D: DimNameAdd<U1>, C> Transform<N, D, C>
|
|
|
|
where
|
2019-03-31 16:48:59 +08:00
|
|
|
N: RealField,
|
2019-02-25 00:29:27 +08:00
|
|
|
C: TCategory,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N, D, D>
|
|
|
|
+ Allocator<N, D>,
|
|
|
|
{
|
|
|
|
/// Transform the given point by this transformation.
|
2019-03-15 22:50:47 +08:00
|
|
|
///
|
|
|
|
/// This is the same as the multiplication `self * pt`.
|
2019-02-25 00:29:27 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn transform_point(&self, pt: &Point<N, D>) -> Point<N, D> {
|
|
|
|
self * pt
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transform the given vector by this transformation, ignoring the
|
|
|
|
/// translational component of the transformation.
|
2019-03-15 22:50:47 +08:00
|
|
|
///
|
|
|
|
/// This is the same as the multiplication `self * v`.
|
2019-02-25 00:29:27 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn transform_vector(&self, v: &VectorN<N, D>) -> VectorN<N, D> {
|
|
|
|
self * v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 16:48:59 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> Transform<N, D, C>
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
|
|
|
C: SubTCategoryOf<TProjective>,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>
|
2019-02-25 00:29:27 +08:00
|
|
|
+ Allocator<N, DimNameSum<D, U1>>
|
|
|
|
+ Allocator<N, D, D>
|
|
|
|
+ Allocator<N, D>,
|
|
|
|
{
|
|
|
|
/// Transform the given point by the inverse of this transformation.
|
|
|
|
/// This may be cheaper than inverting the transformation and transforming
|
|
|
|
/// the point.
|
|
|
|
#[inline]
|
|
|
|
pub fn inverse_transform_point(&self, pt: &Point<N, D>) -> Point<N, D> {
|
2020-03-21 19:16:46 +08:00
|
|
|
self.clone().inverse() * pt
|
2019-02-25 00:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transform the given vector by the inverse of this transformation.
|
|
|
|
/// This may be cheaper than inverting the transformation and transforming
|
|
|
|
/// the vector.
|
|
|
|
#[inline]
|
|
|
|
pub fn inverse_transform_vector(&self, v: &VectorN<N, D>) -> VectorN<N, D> {
|
2020-03-21 19:16:46 +08:00
|
|
|
self.clone().inverse() * v
|
2019-02-25 00:29:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>> Transform<N, D, TGeneral>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
/// A mutable reference to underlying matrix. Use `.matrix_mut_unchecked` instead if this
|
|
|
|
/// transformation category is not `TGeneral`.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn matrix_mut(&mut self) -> &mut MatrixN<N, DimNameSum<D, U1>> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix_mut_unchecked()
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 00:57:44 +08:00
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> AbsDiffEq for Transform<N, D, C>
|
2018-11-08 14:51:43 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
{
|
|
|
|
type Epsilon = N::Epsilon;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn default_epsilon() -> Self::Epsilon {
|
|
|
|
N::default_epsilon()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
|
|
|
|
self.matrix.abs_diff_eq(&other.matrix, epsilon)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> RelativeEq for Transform<N, D, C>
|
2018-11-08 14:51:43 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn default_max_relative() -> Self::Epsilon {
|
|
|
|
N::default_max_relative()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn relative_eq(
|
|
|
|
&self,
|
|
|
|
other: &Self,
|
|
|
|
epsilon: Self::Epsilon,
|
|
|
|
max_relative: Self::Epsilon,
|
2020-04-06 00:49:48 +08:00
|
|
|
) -> bool {
|
2018-11-08 14:51:43 +08:00
|
|
|
self.matrix
|
|
|
|
.relative_eq(&other.matrix, epsilon, max_relative)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: RealField, D: DimNameAdd<U1>, C: TCategory> UlpsEq for Transform<N, D, C>
|
2018-11-08 14:51:43 +08:00
|
|
|
where
|
|
|
|
N::Epsilon: Copy,
|
|
|
|
DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn default_max_ulps() -> u32 {
|
|
|
|
N::default_max_ulps()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
|
|
|
|
self.matrix.ulps_eq(&other.matrix, epsilon, max_ulps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:57:44 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::Matrix4;
|
2017-07-29 00:57:44 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn checks_homogeneous_invariants_of_square_identity_matrix() {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(TAffine::check_homogeneous_invariants(
|
|
|
|
&Matrix4::<f32>::identity()
|
|
|
|
));
|
2017-07-29 00:57:44 +08:00
|
|
|
}
|
|
|
|
}
|