Initial commit copy all translation_x files into scale_x files

This commit is contained in:
Yuri Edward 2021-10-19 14:02:07 +02:00
parent 2e9c8aef20
commit 8c6ad490bc
6 changed files with 655 additions and 0 deletions

View File

@ -0,0 +1,19 @@
use crate::geometry::Translation;
/// A 1-dimensional translation.
pub type Translation1<T> = Translation<T, 1>;
/// A 2-dimensional translation.
pub type Translation2<T> = Translation<T, 2>;
/// A 3-dimensional translation.
pub type Translation3<T> = Translation<T, 3>;
/// A 4-dimensional translation.
pub type Translation4<T> = Translation<T, 4>;
/// A 5-dimensional translation.
pub type Translation5<T> = Translation<T, 5>;
/// A 6-dimensional translation.
pub type Translation6<T> = Translation<T, 6>;

View File

@ -0,0 +1,123 @@
#[cfg(feature = "arbitrary")]
use crate::base::storage::Owned;
#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
use num::{One, Zero};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
Rng,
};
use simba::scalar::{ClosedAdd, SupersetOf};
use crate::base::{SVector, Scalar};
use crate::geometry::Translation;
impl<T: Scalar, const D: usize> Translation<T, D> {
/// Creates a new identity translation.
///
/// # Example
/// ```
/// # use nalgebra::{Point2, Point3, Translation2, Translation3};
/// let t = Translation2::identity();
/// let p = Point2::new(1.0, 2.0);
/// assert_eq!(t * p, p);
///
/// // Works in all dimensions.
/// let t = Translation3::identity();
/// let p = Point3::new(1.0, 2.0, 3.0);
/// assert_eq!(t * p, p);
/// ```
#[inline]
pub fn identity() -> Translation<T, D>
where
T: Zero,
{
Self::from(SVector::<T, D>::from_element(T::zero()))
}
/// Cast the components of `self` to another type.
///
/// # Example
/// ```
/// # use nalgebra::Translation2;
/// let tra = Translation2::new(1.0f64, 2.0);
/// let tra2 = tra.cast::<f32>();
/// assert_eq!(tra2, Translation2::new(1.0f32, 2.0));
/// ```
pub fn cast<To: Scalar>(self) -> Translation<To, D>
where
Translation<To, D>: SupersetOf<Self>,
{
crate::convert(self)
}
}
impl<T: Scalar + Zero + ClosedAdd, const D: usize> One for Translation<T, D> {
#[inline]
fn one() -> Self {
Self::identity()
}
}
#[cfg(feature = "rand-no-std")]
impl<T: Scalar, const D: usize> Distribution<Translation<T, D>> for Standard
where
Standard: Distribution<T>,
{
/// Generate an arbitrary random variate for testing purposes.
#[inline]
fn sample<G: Rng + ?Sized>(&self, rng: &mut G) -> Translation<T, D> {
Translation::from(rng.gen::<SVector<T, D>>())
}
}
#[cfg(feature = "arbitrary")]
impl<T: Scalar + Arbitrary + Send, const D: usize> Arbitrary for Translation<T, D>
where
Owned<T, crate::Const<D>>: Send,
{
#[inline]
fn arbitrary(rng: &mut Gen) -> Self {
let v: SVector<T, D> = Arbitrary::arbitrary(rng);
Self::from(v)
}
}
/*
*
* Small translation construction from components.
*
*/
macro_rules! componentwise_constructors_impl(
($($doc: expr; $D: expr, $($args: ident:$irow: expr),*);* $(;)*) => {$(
impl<T> Translation<T, $D>
{
#[doc = "Initializes this translation from its components."]
#[doc = "# Example\n```"]
#[doc = $doc]
#[doc = "```"]
#[inline]
pub const fn new($($args: T),*) -> Self {
Self { vector: SVector::<T, $D>::new($($args),*) }
}
}
)*}
);
componentwise_constructors_impl!(
"# use nalgebra::Translation1;\nlet t = Translation1::new(1.0);\nassert!(t.vector.x == 1.0);";
1, x:0;
"# use nalgebra::Translation2;\nlet t = Translation2::new(1.0, 2.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0);";
2, x:0, y:1;
"# use nalgebra::Translation3;\nlet t = Translation3::new(1.0, 2.0, 3.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0);";
3, x:0, y:1, z:2;
"# use nalgebra::Translation4;\nlet t = Translation4::new(1.0, 2.0, 3.0, 4.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0);";
4, x:0, y:1, z:2, w:3;
"# use nalgebra::Translation5;\nlet t = Translation5::new(1.0, 2.0, 3.0, 4.0, 5.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0 && t.vector.a == 5.0);";
5, x:0, y:1, z:2, w:3, a:4;
"# use nalgebra::Translation6;\nlet t = Translation6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);\nassert!(t.vector.x == 1.0 && t.vector.y == 2.0 && t.vector.z == 3.0 && t.vector.w == 4.0 && t.vector.a == 5.0 && t.vector.b == 6.0);";
6, x:0, y:1, z:2, w:3, a:4, b:5;
);

View File

@ -0,0 +1,306 @@
use num::{One, Zero};
use simba::scalar::{RealField, SubsetOf, SupersetOf};
use simba::simd::PrimitiveSimdValue;
use crate::base::allocator::Allocator;
use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
use crate::base::{Const, DefaultAllocator, DimName, OMatrix, OVector, SVector, Scalar};
use crate::geometry::{
AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation,
Translation3, UnitDualQuaternion, UnitQuaternion,
};
use crate::Point;
/*
* This file provides the following conversions:
* =============================================
*
* Translation -> Translation
* Translation -> Isometry
* Translation3 -> UnitDualQuaternion
* Translation -> Similarity
* Translation -> Transform
* Translation -> Matrix (homogeneous)
*/
impl<T1, T2, const D: usize> SubsetOf<Translation<T2, D>> for Translation<T1, D>
where
T1: Scalar,
T2: Scalar + SupersetOf<T1>,
{
#[inline]
fn to_superset(&self) -> Translation<T2, D> {
Translation::from(self.vector.to_superset())
}
#[inline]
fn is_in_subset(rot: &Translation<T2, D>) -> bool {
crate::is_convertible::<_, SVector<T1, D>>(&rot.vector)
}
#[inline]
fn from_superset_unchecked(rot: &Translation<T2, D>) -> Self {
Translation {
vector: rot.vector.to_subset_unchecked(),
}
}
}
impl<T1, T2, R, const D: usize> SubsetOf<Isometry<T2, R, D>> for Translation<T1, D>
where
T1: RealField,
T2: RealField + SupersetOf<T1>,
R: AbstractRotation<T2, D>,
{
#[inline]
fn to_superset(&self) -> Isometry<T2, R, D> {
Isometry::from_parts(self.to_superset(), R::identity())
}
#[inline]
fn is_in_subset(iso: &Isometry<T2, R, D>) -> bool {
iso.rotation == R::identity()
}
#[inline]
fn from_superset_unchecked(iso: &Isometry<T2, R, D>) -> Self {
Self::from_superset_unchecked(&iso.translation)
}
}
impl<T1, T2> SubsetOf<UnitDualQuaternion<T2>> for Translation3<T1>
where
T1: RealField,
T2: RealField + SupersetOf<T1>,
{
#[inline]
fn to_superset(&self) -> UnitDualQuaternion<T2> {
let dq = UnitDualQuaternion::<T1>::from_parts(self.clone(), UnitQuaternion::identity());
dq.to_superset()
}
#[inline]
fn is_in_subset(dq: &UnitDualQuaternion<T2>) -> bool {
crate::is_convertible::<_, Translation<T1, 3>>(&dq.translation())
&& dq.rotation() == UnitQuaternion::identity()
}
#[inline]
fn from_superset_unchecked(dq: &UnitDualQuaternion<T2>) -> Self {
let dq: UnitDualQuaternion<T1> = crate::convert_ref_unchecked(dq);
dq.translation()
}
}
impl<T1, T2, R, const D: usize> SubsetOf<Similarity<T2, R, D>> for Translation<T1, D>
where
T1: RealField,
T2: RealField + SupersetOf<T1>,
R: AbstractRotation<T2, D>,
{
#[inline]
fn to_superset(&self) -> Similarity<T2, R, D> {
Similarity::from_parts(self.to_superset(), R::identity(), T2::one())
}
#[inline]
fn is_in_subset(sim: &Similarity<T2, R, D>) -> bool {
sim.isometry.rotation == R::identity() && sim.scaling() == T2::one()
}
#[inline]
fn from_superset_unchecked(sim: &Similarity<T2, R, D>) -> Self {
Self::from_superset_unchecked(&sim.isometry.translation)
}
}
impl<T1, T2, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Translation<T1, D>
where
T1: RealField,
T2: RealField + SupersetOf<T1>,
C: SuperTCategoryOf<TAffine>,
Const<D>: DimNameAdd<U1>,
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
{
#[inline]
fn to_superset(&self) -> Transform<T2, C, D> {
Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
}
#[inline]
fn is_in_subset(t: &Transform<T2, C, D>) -> bool {
<Self as SubsetOf<_>>::is_in_subset(t.matrix())
}
#[inline]
fn from_superset_unchecked(t: &Transform<T2, C, D>) -> Self {
Self::from_superset_unchecked(t.matrix())
}
}
impl<T1, T2, const D: usize>
SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>> for Translation<T1, D>
where
T1: RealField,
T2: RealField + SupersetOf<T1>,
Const<D>: DimNameAdd<U1>,
DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
+ Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
// + Allocator<T1, D>
// + Allocator<T2, D>
{
#[inline]
fn to_superset(&self) -> OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> {
self.to_homogeneous().to_superset()
}
#[inline]
fn is_in_subset(m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>) -> bool {
let id = m.generic_slice((0, 0), (DimNameSum::<Const<D>, U1>::name(), Const::<D>));
// Scalar types agree.
m.iter().all(|e| SupersetOf::<T1>::is_in_subset(e)) &&
// The block part does nothing.
id.is_identity(T2::zero()) &&
// The normalization factor is one.
m[(D, D)] == T2::one()
}
#[inline]
fn from_superset_unchecked(
m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
) -> Self {
let t = m.fixed_slice::<D, 1>(0, D);
Self {
vector: crate::convert_unchecked(t.into_owned()),
}
}
}
impl<T: Scalar + Zero + One, const D: usize> From<Translation<T, D>>
for OMatrix<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
where
Const<D>: DimNameAdd<U1>,
DefaultAllocator:
Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> + Allocator<T, Const<D>>,
{
#[inline]
fn from(t: Translation<T, D>) -> Self {
t.to_homogeneous()
}
}
impl<T: Scalar, const D: usize> From<OVector<T, Const<D>>> for Translation<T, D> {
#[inline]
fn from(vector: OVector<T, Const<D>>) -> Self {
Translation { vector }
}
}
impl<T: Scalar, const D: usize> From<[T; D]> for Translation<T, D> {
#[inline]
fn from(coords: [T; D]) -> Self {
Translation {
vector: coords.into(),
}
}
}
impl<T: Scalar, const D: usize> From<Point<T, D>> for Translation<T, D> {
#[inline]
fn from(pt: Point<T, D>) -> Self {
Translation { vector: pt.coords }
}
}
impl<T: Scalar, const D: usize> From<Translation<T, D>> for [T; D] {
#[inline]
fn from(t: Translation<T, D>) -> Self {
t.vector.into()
}
}
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 2]>
for Translation<T, D>
where
T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
T::Element: Scalar,
{
#[inline]
fn from(arr: [Translation<T::Element, D>; 2]) -> Self {
Self::from(OVector::from([
arr[0].vector.clone(),
arr[1].vector.clone(),
]))
}
}
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 4]>
for Translation<T, D>
where
T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
T::Element: Scalar,
{
#[inline]
fn from(arr: [Translation<T::Element, D>; 4]) -> Self {
Self::from(OVector::from([
arr[0].vector.clone(),
arr[1].vector.clone(),
arr[2].vector.clone(),
arr[3].vector.clone(),
]))
}
}
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 8]>
for Translation<T, D>
where
T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
T::Element: Scalar,
{
#[inline]
fn from(arr: [Translation<T::Element, D>; 8]) -> Self {
Self::from(OVector::from([
arr[0].vector.clone(),
arr[1].vector.clone(),
arr[2].vector.clone(),
arr[3].vector.clone(),
arr[4].vector.clone(),
arr[5].vector.clone(),
arr[6].vector.clone(),
arr[7].vector.clone(),
]))
}
}
impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 16]>
for Translation<T, D>
where
T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
T::Element: Scalar,
{
#[inline]
fn from(arr: [Translation<T::Element, D>; 16]) -> Self {
Self::from(OVector::from([
arr[0].vector.clone(),
arr[1].vector.clone(),
arr[2].vector.clone(),
arr[3].vector.clone(),
arr[4].vector.clone(),
arr[5].vector.clone(),
arr[6].vector.clone(),
arr[7].vector.clone(),
arr[8].vector.clone(),
arr[9].vector.clone(),
arr[10].vector.clone(),
arr[11].vector.clone(),
arr[12].vector.clone(),
arr[13].vector.clone(),
arr[14].vector.clone(),
arr[15].vector.clone(),
]))
}
}

View File

@ -0,0 +1,39 @@
use std::ops::{Deref, DerefMut};
use crate::base::coordinates::{X, XY, XYZ, XYZW, XYZWA, XYZWAB};
use crate::base::Scalar;
use crate::geometry::Translation;
/*
*
* Give coordinates to Translation{1 .. 6}
*
*/
macro_rules! deref_impl(
($D: expr, $Target: ident $(, $comps: ident)*) => {
impl<T: Scalar> Deref for Translation<T, $D> {
type Target = $Target<T>;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Translation<T, $D> as *const Self::Target) }
}
}
impl<T: Scalar> DerefMut for Translation<T, $D> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Translation<T, $D> as *mut Self::Target) }
}
}
}
);
deref_impl!(1, X, x);
deref_impl!(2, XY, x, y);
deref_impl!(3, XYZ, x, y, z);
deref_impl!(4, XYZW, x, y, z, w);
deref_impl!(5, XYZWA, x, y, z, w, a);
deref_impl!(6, XYZWAB, x, y, z, w, a, b);

119
src/geometry/scale_ops.rs Normal file
View File

@ -0,0 +1,119 @@
use std::ops::{Div, DivAssign, Mul, MulAssign};
use simba::scalar::{ClosedAdd, ClosedSub};
use crate::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
use crate::base::dimension::U1;
use crate::base::{Const, Scalar};
use crate::geometry::{Point, Translation};
// Translation × Translation
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: &'b Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(&self.vector + &right.vector) };
'a, 'b);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(&self.vector + right.vector) };
'a);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: &'b Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(self.vector + &right.vector) };
'b);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(self.vector + right.vector) }; );
// Translation ÷ Translation
// TODO: instead of calling inverse explicitly, could we just add a `mul_tr` or `mul_inv` method?
add_sub_impl!(Div, div, ClosedSub;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: &'b Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(&self.vector - &right.vector) };
'a, 'b);
add_sub_impl!(Div, div, ClosedSub;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(&self.vector - right.vector) };
'a);
add_sub_impl!(Div, div, ClosedSub;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: &'b Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(self.vector - &right.vector) };
'b);
add_sub_impl!(Div, div, ClosedSub;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: Translation<T, D>, Output = Translation<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { Translation::from(self.vector - right.vector) }; );
// Translation × Point
// TODO: we don't handle properly non-zero origins here. Do we want this to be the intended
// behavior?
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: &'b Point<T, D>, Output = Point<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { right + &self.vector };
'a, 'b);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: &'a Translation<T, D>, right: Point<T, D>, Output = Point<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { right + &self.vector };
'a);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: &'b Point<T, D>, Output = Point<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { right + self.vector };
'b);
add_sub_impl!(Mul, mul, ClosedAdd;
(Const<D>, U1), (Const<D>, U1) -> (Const<D>, U1)
const D; for; where;
self: Translation<T, D>, right: Point<T, D>, Output = Point<T, D>;
#[allow(clippy::suspicious_arithmetic_impl)] { right + self.vector }; );
// Translation *= Translation
add_sub_assign_impl!(MulAssign, mul_assign, ClosedAdd;
const D;
self: Translation<T, D>, right: &'b Translation<T, D>;
#[allow(clippy::suspicious_op_assign_impl)] { self.vector += &right.vector };
'b);
add_sub_assign_impl!(MulAssign, mul_assign, ClosedAdd;
const D;
self: Translation<T, D>, right: Translation<T, D>;
#[allow(clippy::suspicious_op_assign_impl)] { self.vector += right.vector }; );
add_sub_assign_impl!(DivAssign, div_assign, ClosedSub;
const D;
self: Translation<T, D>, right: &'b Translation<T, D>;
#[allow(clippy::suspicious_op_assign_impl)] { self.vector -= &right.vector };
'b);
add_sub_assign_impl!(DivAssign, div_assign, ClosedSub;
const D;
self: Translation<T, D>, right: Translation<T, D>;
#[allow(clippy::suspicious_op_assign_impl)] { self.vector -= right.vector }; );

49
src/geometry/scale_simba.rs Executable file
View File

@ -0,0 +1,49 @@
use simba::simd::SimdValue;
use crate::base::OVector;
use crate::Scalar;
use crate::geometry::Translation;
impl<T: Scalar + SimdValue, const D: usize> SimdValue for Translation<T, D>
where
T::Element: Scalar,
{
type Element = Translation<T::Element, D>;
type SimdBool = T::SimdBool;
#[inline]
fn lanes() -> usize {
T::lanes()
}
#[inline]
fn splat(val: Self::Element) -> Self {
OVector::splat(val.vector).into()
}
#[inline]
fn extract(&self, i: usize) -> Self::Element {
self.vector.extract(i).into()
}
#[inline]
unsafe fn extract_unchecked(&self, i: usize) -> Self::Element {
self.vector.extract_unchecked(i).into()
}
#[inline]
fn replace(&mut self, i: usize, val: Self::Element) {
self.vector.replace(i, val.vector)
}
#[inline]
unsafe fn replace_unchecked(&mut self, i: usize, val: Self::Element) {
self.vector.replace_unchecked(i, val.vector)
}
#[inline]
fn select(self, cond: Self::SimdBool, other: Self) -> Self {
self.vector.select(cond, other.vector).into()
}
}