2016-12-05 05:44:42 +08:00
|
|
|
use alga::general::{AbstractMagma, AbstractGroup, AbstractLoop, AbstractMonoid, AbstractQuasigroup,
|
|
|
|
AbstractSemigroup, Real, Inverse, Multiplicative, Identity};
|
2017-08-03 01:37:44 +08:00
|
|
|
use alga::linear::{Transformation, AffineTransformation, Rotation, ProjectiveTransformation};
|
|
|
|
use alga::linear::Similarity as AlgaSimilarity;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use core::{DefaultAllocator, VectorN};
|
|
|
|
use core::dimension::DimName;
|
|
|
|
use core::allocator::Allocator;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use geometry::{Similarity, Translation, Point};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Algebraic structures.
|
|
|
|
*
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> Identity<Multiplicative> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn identity() -> Self {
|
|
|
|
Self::identity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> Inverse<Multiplicative> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn inverse(&self) -> Self {
|
|
|
|
self.inverse()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inverse_mut(&mut self) {
|
|
|
|
self.inverse_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> AbstractMagma<Multiplicative> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn operate(&self, rhs: &Self) -> Self {
|
|
|
|
self * rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_multiplicative_structures(
|
|
|
|
($($marker: ident<$operator: ident>),* $(,)*) => {$(
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> $marker<$operator> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> { }
|
2016-12-05 05:44:42 +08:00
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
impl_multiplicative_structures!(
|
|
|
|
AbstractSemigroup<Multiplicative>,
|
|
|
|
AbstractMonoid<Multiplicative>,
|
|
|
|
AbstractQuasigroup<Multiplicative>,
|
|
|
|
AbstractLoop<Multiplicative>,
|
|
|
|
AbstractGroup<Multiplicative>
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Transformation groups.
|
|
|
|
*
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> Transformation<Point<N, D>> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn transform_point(&self, pt: &Point<N, D>) -> Point<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self * pt
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn transform_vector(&self, v: &VectorN<N, D>) -> VectorN<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self * v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> ProjectiveTransformation<Point<N, D>> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn inverse_transform_point(&self, pt: &Point<N, D>) -> Point<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.isometry.inverse_transform_point(pt) / self.scaling()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn inverse_transform_vector(&self, v: &VectorN<N, D>) -> VectorN<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.isometry.inverse_transform_vector(v) / self.scaling()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> AffineTransformation<Point<N, D>> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
type NonUniformScaling = N;
|
|
|
|
type Rotation = R;
|
2017-08-03 01:37:44 +08:00
|
|
|
type Translation = Translation<N, D>;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn decompose(&self) -> (Translation<N, D>, R, N, R) {
|
2016-12-05 05:44:42 +08:00
|
|
|
(self.isometry.translation.clone(), self.isometry.rotation.clone(), self.scaling(), R::identity())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn append_translation(&self, t: &Self::Translation) -> Self {
|
|
|
|
t * self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_translation(&self, t: &Self::Translation) -> Self {
|
|
|
|
self * t
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn append_rotation(&self, r: &Self::Rotation) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
Similarity::from_isometry(self.isometry.append_rotation(r), self.scaling())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_rotation(&self, r: &Self::Rotation) -> Self {
|
|
|
|
self * r
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn append_scaling(&self, s: &Self::NonUniformScaling) -> Self {
|
|
|
|
self.append_scaling(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prepend_scaling(&self, s: &Self::NonUniformScaling) -> Self {
|
|
|
|
self.prepend_scaling(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn append_rotation_wrt_point(&self, r: &Self::Rotation, p: &Point<N, D>) -> Option<Self> {
|
2016-12-05 05:44:42 +08:00
|
|
|
let mut res = self.clone();
|
|
|
|
res.append_rotation_wrt_point_mut(r, p);
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real, D: DimName, R> AlgaSimilarity<Point<N, D>> for Similarity<N, D, R>
|
|
|
|
where R: Rotation<Point<N, D>>,
|
|
|
|
DefaultAllocator: Allocator<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
type Scaling = N;
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn translation(&self) -> Translation<N, D> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.isometry.translation()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn rotation(&self) -> R {
|
|
|
|
self.isometry.rotation()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn scaling(&self) -> N {
|
|
|
|
self.scaling()
|
|
|
|
}
|
|
|
|
}
|