Implement From<Vector> for Translation and Quaternion.

This commit is contained in:
sebcrozet 2018-10-28 07:33:39 +01:00 committed by Sébastien Crozet
parent 96db8e564a
commit 911ddca588
16 changed files with 83 additions and 70 deletions

View File

@ -113,7 +113,7 @@ pub fn mat4_to_mat2<N: Scalar>(m: &TMat4<N>) -> TMat2<N> {
/// Creates a quaternion from a slice arranged as `[x, y, z, w]`.
pub fn make_quat<N: Real>(ptr: &[N]) -> Qua<N> {
Quaternion::from_vector(TVec4::from_column_slice(ptr))
Quaternion::from(TVec4::from_column_slice(ptr))
}
/// Creates a 1D vector from a slice.

View File

@ -143,10 +143,7 @@ where
#[inline]
fn append_rotation(&self, r: &Self::Rotation) -> Self {
let shift = r.transform_vector(&self.translation.vector);
Isometry::from_parts(
Translation::from_vector(shift),
r.clone() * self.rotation.clone(),
)
Isometry::from_parts(Translation::from(shift), r.clone() * self.rotation.clone())
}
#[inline]

View File

@ -58,7 +58,7 @@ where DefaultAllocator: Allocator<N, D>
#[inline]
pub fn rotation_wrt_point(r: R, p: Point<N, D>) -> Self {
let shift = r.transform_vector(&-&p.coords);
Self::from_parts(Translation::from_vector(shift + p.coords), r)
Self::from_parts(Translation::from(shift + p.coords), r)
}
}
@ -122,7 +122,7 @@ impl<N: Real> Isometry<N, U2, Rotation2<N>> {
#[inline]
pub fn new(translation: Vector2<N>, angle: N) -> Self {
Self::from_parts(
Translation::from_vector(translation),
Translation::from(translation),
Rotation::<N, U2>::new(angle),
)
}
@ -145,7 +145,7 @@ impl<N: Real> Isometry<N, U2, UnitComplex<N>> {
#[inline]
pub fn new(translation: Vector2<N>, angle: N) -> Self {
Self::from_parts(
Translation::from_vector(translation),
Translation::from(translation),
UnitComplex::from_angle(angle),
)
}
@ -183,7 +183,7 @@ macro_rules! isometry_construction_impl(
#[inline]
pub fn new(translation: Vector3<N>, axisangle: Vector3<N>) -> Self {
Self::from_parts(
Translation::from_vector(translation),
Translation::from(translation),
$RotId::<$($RotParams),*>::from_scaled_axis(axisangle))
}
@ -225,7 +225,7 @@ macro_rules! isometry_construction_impl(
up: &Vector3<N>)
-> Self {
Self::from_parts(
Translation::from_vector(eye.coords.clone()),
Translation::from(eye.coords.clone()),
$RotId::new_observer_frame(&(target - eye), up))
}
@ -270,7 +270,7 @@ macro_rules! isometry_construction_impl(
let rotation = $RotId::look_at_rh(&(target - eye), up);
let trans = &rotation * (-eye);
Self::from_parts(Translation::from_vector(trans.coords), rotation)
Self::from_parts(Translation::from(trans.coords), rotation)
}
/// Builds a left-handed look-at view matrix.
@ -314,7 +314,7 @@ macro_rules! isometry_construction_impl(
let rotation = $RotId::look_at_lh(&(target - eye), up);
let trans = &rotation * (-eye);
Self::from_parts(Translation::from_vector(trans.coords), rotation)
Self::from_parts(Translation::from(trans.coords), rotation)
}
}
}

View File

@ -141,7 +141,9 @@ where
#[inline]
unsafe fn from_superset_unchecked(m: &MatrixN<N2, DimNameSum<D, U1>>) -> Self {
let t = m.fixed_slice::<D, U1>(0, D::dim()).into_owned();
let t = Translation::from_vector(::convert_unchecked(t));
let t = Translation {
vector: ::convert_unchecked(t),
};
Self::from_parts(t, ::convert_unchecked(m.clone_owned()))
}

View File

@ -142,7 +142,7 @@ isometry_binop_impl_all!(
[ref ref] => {
let shift = self.rotation.transform_vector(&rhs.translation.vector);
Isometry::from_parts(Translation::from_vector(&self.translation.vector + shift),
Isometry::from_parts(Translation::from(&self.translation.vector + shift),
self.rotation.clone() * rhs.rotation.clone()) // FIXME: too bad we have to clone.
};
);
@ -267,7 +267,7 @@ isometry_binop_impl_all!(
[val ref] => &self * right;
[ref ref] => {
let new_tr = &self.translation.vector + self.rotation.transform_vector(&right.vector);
Isometry::from_parts(Translation::from_vector(new_tr), self.rotation.clone())
Isometry::from_parts(Translation::from(new_tr), self.rotation.clone())
};
);
@ -339,10 +339,10 @@ isometry_from_composition_impl_all!(
Mul, mul;
(D, D), (D, U1) for D: DimName;
self: Rotation<N, D>, right: Translation<N, D>, Output = Isometry<N, D, Rotation<N, D>>;
[val val] => Isometry::from_parts(Translation::from_vector(&self * right.vector), self);
[ref val] => Isometry::from_parts(Translation::from_vector(self * right.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from_vector(&self * &right.vector), self);
[ref ref] => Isometry::from_parts(Translation::from_vector(self * &right.vector), self.clone());
[val val] => Isometry::from_parts(Translation::from(&self * right.vector), self);
[ref val] => Isometry::from_parts(Translation::from(self * right.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from(&self * &right.vector), self);
[ref ref] => Isometry::from_parts(Translation::from(self * &right.vector), self.clone());
);
// UnitQuaternion × Translation
@ -351,10 +351,10 @@ isometry_from_composition_impl_all!(
(U4, U1), (U3, U1);
self: UnitQuaternion<N>, right: Translation<N, U3>,
Output = Isometry<N, U3, UnitQuaternion<N>>;
[val val] => Isometry::from_parts(Translation::from_vector(&self * right.vector), self);
[ref val] => Isometry::from_parts(Translation::from_vector( self * right.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from_vector(&self * &right.vector), self);
[ref ref] => Isometry::from_parts(Translation::from_vector( self * &right.vector), self.clone());
[val val] => Isometry::from_parts(Translation::from(&self * right.vector), self);
[ref val] => Isometry::from_parts(Translation::from( self * right.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from(&self * &right.vector), self);
[ref ref] => Isometry::from_parts(Translation::from( self * &right.vector), self.clone());
);
// Rotation × Isometry
@ -368,7 +368,7 @@ isometry_from_composition_impl_all!(
[val ref] => &self * right;
[ref ref] => {
let shift = self * &right.translation.vector;
Isometry::from_parts(Translation::from_vector(shift), self * &right.rotation)
Isometry::from_parts(Translation::from(shift), self * &right.rotation)
};
);
@ -396,7 +396,7 @@ isometry_from_composition_impl_all!(
[val ref] => &self * right;
[ref ref] => {
let shift = self * &right.translation.vector;
Isometry::from_parts(Translation::from_vector(shift), self * &right.rotation)
Isometry::from_parts(Translation::from(shift), self * &right.rotation)
};
);

View File

@ -158,7 +158,7 @@ macro_rules! componentwise_constructors_impl(
($($doc: expr; $D: ty, $($args: ident:$irow: expr),*);* $(;)*) => {$(
impl<N: Scalar> Point<N, $D>
where DefaultAllocator: Allocator<N, $D> {
#[doc = "Initializes this matrix from its components."]
#[doc = "Initializes this point from its components."]
#[doc = "# Example\n```"]
#[doc = $doc]
#[doc = "```"]

View File

@ -68,7 +68,7 @@ impl<N: Real> Copy for Quaternion<N> {}
impl<N: Real> Clone for Quaternion<N> {
#[inline]
fn clone(&self) -> Self {
Quaternion::from_vector(self.coords.clone())
Quaternion::from(self.coords.clone())
}
}
@ -90,7 +90,7 @@ where Owned<N, U4>: Deserialize<'a>
where Des: Deserializer<'a> {
let coords = Vector4::<N>::deserialize(deserializer)?;
Ok(Quaternion::from_vector(coords))
Ok(Quaternion::from(coords))
}
}
@ -106,7 +106,7 @@ impl<N: Real> Quaternion<N> {
#[inline]
#[deprecated(note = "This method is a no-op and will be removed in a future release.")]
pub fn clone_owned(&self) -> Quaternion<N> {
Quaternion::from_vector(self.coords.clone_owned())
Quaternion::from(self.coords.clone_owned())
}
/// Normalizes this quaternion.
@ -122,7 +122,7 @@ impl<N: Real> Quaternion<N> {
/// ```
#[inline]
pub fn normalize(&self) -> Quaternion<N> {
Quaternion::from_vector(self.coords.normalize())
Quaternion::from(self.coords.normalize())
}
/// The conjugate of this quaternion.
@ -142,7 +142,7 @@ impl<N: Real> Quaternion<N> {
-self.coords[2],
self.coords[3],
);
Quaternion::from_vector(v)
Quaternion::from(v)
}
/// Inverts this quaternion if it is not zero.
@ -166,7 +166,7 @@ impl<N: Real> Quaternion<N> {
/// ```
#[inline]
pub fn try_inverse(&self) -> Option<Quaternion<N>> {
let mut res = Quaternion::from_vector(self.coords.clone_owned());
let mut res = Quaternion::from(self.coords.clone_owned());
if res.try_inverse_mut() {
Some(res)
@ -405,7 +405,6 @@ impl<N: Real> Quaternion<N> {
}
}
/// Raise the quaternion to a given floating power.
///
/// # Example
@ -585,14 +584,18 @@ pub type UnitQuaternion<N> = Unit<Quaternion<N>>;
impl<N: Real> UnitQuaternion<N> {
/// Moves this unit quaternion into one that owns its data.
#[inline]
#[deprecated(note = "This method is unnecessary and will be removed in a future release. Use `.clone()` instead.")]
#[deprecated(
note = "This method is unnecessary and will be removed in a future release. Use `.clone()` instead."
)]
pub fn into_owned(self) -> UnitQuaternion<N> {
self
}
/// Clones this unit quaternion into one that owns its data.
#[inline]
#[deprecated(note = "This method is unnecessary and will be removed in a future release. Use `.clone()` instead.")]
#[deprecated(
note = "This method is unnecessary and will be removed in a future release. Use `.clone()` instead."
)]
pub fn clone_owned(&self) -> UnitQuaternion<N> {
*self
}
@ -741,7 +744,7 @@ impl<N: Real> UnitQuaternion<N> {
/// is not well-defined). Use `.try_slerp` instead to avoid the panic.
#[inline]
pub fn slerp(&self, other: &UnitQuaternion<N>, t: N) -> UnitQuaternion<N> {
Unit::new_unchecked(Quaternion::from_vector(
Unit::new_unchecked(Quaternion::from(
Unit::new_unchecked(self.coords)
.slerp(&Unit::new_unchecked(other.coords), t)
.unwrap(),
@ -768,7 +771,7 @@ impl<N: Real> UnitQuaternion<N> {
{
Unit::new_unchecked(self.coords)
.try_slerp(&Unit::new_unchecked(other.coords), t, epsilon)
.map(|q| Unit::new_unchecked(Quaternion::from_vector(q.unwrap())))
.map(|q| Unit::new_unchecked(Quaternion::from(q.unwrap())))
}
/// Compute the conjugate of this unit quaternion in-place.
@ -993,7 +996,6 @@ impl<N: Real> UnitQuaternion<N> {
self.to_rotation_matrix().to_euler_angles()
}
/// Retrieves the euler angles corresponding to this unit quaternion.
///
/// The angles are produced in the form (roll, pitch, yaw).

View File

@ -98,7 +98,7 @@ impl<N: Real> FiniteDimVectorSpace for Quaternion<N> {
#[inline]
fn canonical_basis_element(i: usize) -> Self {
Self::from_vector(Vector4::canonical_basis_element(i))
Self::from(Vector4::canonical_basis_element(i))
}
#[inline]
@ -131,7 +131,7 @@ impl<N: Real> NormedSpace for Quaternion<N> {
#[inline]
fn normalize(&self) -> Self {
let v = self.coords.normalize();
Self::from_vector(v)
Self::from(v)
}
#[inline]
@ -142,7 +142,7 @@ impl<N: Real> NormedSpace for Quaternion<N> {
#[inline]
fn try_normalize(&self, min_norm: N) -> Option<Self> {
if let Some(v) = self.coords.try_normalize(min_norm) {
Some(Self::from_vector(v))
Some(Self::from(v))
} else {
None
}

View File

@ -23,6 +23,7 @@ impl<N: Real> Quaternion<N> {
/// Creates a quaternion from a 4D vector. The quaternion scalar part corresponds to the `w`
/// vector component.
#[inline]
#[deprecated(note = "Use `::from` instead.")]
pub fn from_vector(vector: Vector4<N>) -> Self {
Quaternion { coords: vector }
}
@ -34,7 +35,7 @@ impl<N: Real> Quaternion<N> {
#[inline]
pub fn new(w: N, x: N, y: N, z: N) -> Self {
let v = Vector4::<N>::new(x, y, z, w);
Self::from_vector(v)
Self::from(v)
}
/// Creates a new quaternion from its scalar and vector parts. Note that the arguments order does

View File

@ -39,7 +39,7 @@ where
{
#[inline]
fn to_superset(&self) -> Quaternion<N2> {
Quaternion::from_vector(self.coords.to_superset())
Quaternion::from(self.coords.to_superset())
}
#[inline]
@ -49,7 +49,9 @@ where
#[inline]
unsafe fn from_superset_unchecked(q: &Quaternion<N2>) -> Self {
Self::from_vector(q.coords.to_subset_unchecked())
Self {
coords: q.coords.to_subset_unchecked(),
}
}
}
@ -229,3 +231,10 @@ impl<N: Real> From<UnitQuaternion<N>> for Matrix3<N> {
q.to_rotation_matrix().unwrap()
}
}
impl<N: Real> From<Vector4<N>> for Quaternion<N> {
#[inline]
fn from(coords: Vector4<N>) -> Self {
Quaternion { coords }
}
}

View File

@ -103,28 +103,28 @@ quaternion_op_impl!(
Add, add;
(U4, U1), (U4, U1);
self: &'a Quaternion<N>, rhs: &'b Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(&self.coords + &rhs.coords);
Quaternion::from(&self.coords + &rhs.coords);
'a, 'b);
quaternion_op_impl!(
Add, add;
(U4, U1), (U4, U1);
self: &'a Quaternion<N>, rhs: Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(&self.coords + rhs.coords);
Quaternion::from(&self.coords + rhs.coords);
'a);
quaternion_op_impl!(
Add, add;
(U4, U1), (U4, U1);
self: Quaternion<N>, rhs: &'b Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(self.coords + &rhs.coords);
Quaternion::from(self.coords + &rhs.coords);
'b);
quaternion_op_impl!(
Add, add;
(U4, U1), (U4, U1);
self: Quaternion<N>, rhs: Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(self.coords + rhs.coords);
Quaternion::from(self.coords + rhs.coords);
);
// Quaternion - Quaternion
@ -132,28 +132,28 @@ quaternion_op_impl!(
Sub, sub;
(U4, U1), (U4, U1);
self: &'a Quaternion<N>, rhs: &'b Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(&self.coords - &rhs.coords);
Quaternion::from(&self.coords - &rhs.coords);
'a, 'b);
quaternion_op_impl!(
Sub, sub;
(U4, U1), (U4, U1);
self: &'a Quaternion<N>, rhs: Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(&self.coords - rhs.coords);
Quaternion::from(&self.coords - rhs.coords);
'a);
quaternion_op_impl!(
Sub, sub;
(U4, U1), (U4, U1);
self: Quaternion<N>, rhs: &'b Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(self.coords - &rhs.coords);
Quaternion::from(self.coords - &rhs.coords);
'b);
quaternion_op_impl!(
Sub, sub;
(U4, U1), (U4, U1);
self: Quaternion<N>, rhs: Quaternion<N>, Output = Quaternion<N>;
Quaternion::from_vector(self.coords - rhs.coords);
Quaternion::from(self.coords - rhs.coords);
);
// Quaternion × Quaternion
@ -495,7 +495,7 @@ macro_rules! scalar_op_impl(
#[inline]
fn $op(self, n: N) -> Self::Output {
Quaternion::from_vector(self.coords.$op(n))
Quaternion::from(self.coords.$op(n))
}
}
@ -504,7 +504,7 @@ macro_rules! scalar_op_impl(
#[inline]
fn $op(self, n: N) -> Self::Output {
Quaternion::from_vector((&self.coords).$op(n))
Quaternion::from((&self.coords).$op(n))
}
}
@ -530,7 +530,7 @@ macro_rules! left_scalar_mul_impl(
#[inline]
fn mul(self, right: Quaternion<$T>) -> Self::Output {
Quaternion::from_vector(self * right.coords)
Quaternion::from(self * right.coords)
}
}
@ -539,7 +539,7 @@ macro_rules! left_scalar_mul_impl(
#[inline]
fn mul(self, right: &'b Quaternion<$T>) -> Self::Output {
Quaternion::from_vector(self * &right.coords)
Quaternion::from(self * &right.coords)
}
}
)*}
@ -552,7 +552,7 @@ impl<N: Real> Neg for Quaternion<N> {
#[inline]
fn neg(self) -> Self::Output {
Quaternion::from_vector(-self.coords)
Quaternion::from(-self.coords)
}
}
@ -561,7 +561,7 @@ impl<'a, N: Real> Neg for &'a Quaternion<N> {
#[inline]
fn neg(self) -> Self::Output {
Quaternion::from_vector(-&self.coords)
Quaternion::from(-&self.coords)
}
}

View File

@ -184,7 +184,7 @@ where
);
Self::from_parts(
Translation::from_vector(&self.isometry.translation.vector * scaling),
Translation::from(&self.isometry.translation.vector * scaling),
self.isometry.rotation.clone(),
self.scaling * scaling,
)

View File

@ -70,7 +70,7 @@ where
#[inline]
pub fn rotation_wrt_point(r: R, p: Point<N, D>, scaling: N) -> Self {
let shift = r.transform_vector(&-&p.coords);
Self::from_parts(Translation::from_vector(shift + p.coords), r, scaling)
Self::from_parts(Translation::from(shift + p.coords), r, scaling)
}
}
@ -105,7 +105,7 @@ impl<N: Real> Similarity<N, U2, Rotation2<N>> {
#[inline]
pub fn new(translation: Vector2<N>, angle: N, scaling: N) -> Self {
Self::from_parts(
Translation::from_vector(translation),
Translation::from(translation),
Rotation2::new(angle),
scaling,
)
@ -117,7 +117,7 @@ impl<N: Real> Similarity<N, U2, UnitComplex<N>> {
#[inline]
pub fn new(translation: Vector2<N>, angle: N, scaling: N) -> Self {
Self::from_parts(
Translation::from_vector(translation),
Translation::from(translation),
UnitComplex::new(angle),
scaling,
)

View File

@ -155,7 +155,9 @@ where
}
let t = m.fixed_slice::<D, U1>(0, D::dim()).into_owned();
let t = Translation::from_vector(::convert_unchecked(t));
let t = Translation {
vector: ::convert_unchecked(t),
};
Self::from_parts(t, ::convert_unchecked(mm), ::convert_unchecked(scale))
}

View File

@ -269,7 +269,7 @@ similarity_binop_impl_all!(
[ref ref] => {
let shift = self.isometry.rotation.transform_vector(&rhs.translation.vector) * self.scaling();
Similarity::from_parts(
Translation::from_vector(&self.isometry.translation.vector + shift),
Translation::from(&self.isometry.translation.vector + shift),
self.isometry.rotation.clone() * rhs.rotation.clone(),
self.scaling())
};
@ -352,7 +352,7 @@ similarity_binop_impl_all!(
[ref ref] => {
let shift = self.isometry.rotation.transform_vector(&right.vector) * self.scaling();
Similarity::from_parts(
Translation::from_vector(&self.isometry.translation.vector + shift),
Translation::from(&self.isometry.translation.vector + shift),
self.isometry.rotation.clone(),
self.scaling())
};

View File

@ -260,7 +260,7 @@ complex_op_impl_all!(
[val ref] => &self * rhs;
[ref ref] => {
let shift = self * &rhs.translation.vector;
Isometry::from_parts(Translation::from_vector(shift), self * &rhs.rotation)
Isometry::from_parts(Translation::from(shift), self * &rhs.rotation)
};
);
@ -282,10 +282,10 @@ complex_op_impl_all!(
(U2, U1);
self: UnitComplex<N>, rhs: Translation<N, U2>,
Output = Isometry<N, U2, UnitComplex<N>>;
[val val] => Isometry::from_parts(Translation::from_vector(&self * rhs.vector), self);
[ref val] => Isometry::from_parts(Translation::from_vector( self * rhs.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from_vector(&self * &rhs.vector), self);
[ref ref] => Isometry::from_parts(Translation::from_vector( self * &rhs.vector), self.clone());
[val val] => Isometry::from_parts(Translation::from(&self * rhs.vector), self);
[ref val] => Isometry::from_parts(Translation::from( self * rhs.vector), self.clone());
[val ref] => Isometry::from_parts(Translation::from(&self * &rhs.vector), self);
[ref ref] => Isometry::from_parts(Translation::from( self * &rhs.vector), self.clone());
);
// Translation × UnitComplex