Implement From<Vector> for Point.

This commit is contained in:
sebcrozet 2018-10-23 20:47:42 +02:00 committed by Sébastien Crozet
parent 6d63a0a5f1
commit 551c44c854
12 changed files with 76 additions and 53 deletions

View File

@ -19,6 +19,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Add `Point*::from_slice` to create a point from a slice.
* Add `.map_with_location` to matrices to apply a map which passes the component indices to the user-defined closure alongide
the component itself.
* Add `From<Vector>` impl for `Point`.
### Modified
* The `Point::from_coordinates` methods is deprecated. Use `Point::from` instead.
## [0.16.0]
All dependencies have been updated to their latest versions.

View File

@ -8,7 +8,7 @@ fn main() {
// Build from a coordinates vector.
let coords = Vector3::new(2.0, 3.0, 4.0);
let p1 = Point3::from_coordinates(coords);
let p1 = Point3::from(coords);
// Build by translating the origin.
let translation = Vector3::new(2.0, 3.0, 4.0);

View File

@ -39,8 +39,8 @@ pub fn look_at<N: Real>(eye: &TVec3<N>, center: &TVec3<N>, up: &TVec3<N>) -> TMa
/// * [`look_at_rh`](fn.look_at_rh.html)
pub fn look_at_lh<N: Real>(eye: &TVec3<N>, center: &TVec3<N>, up: &TVec3<N>) -> TMat4<N> {
TMat::look_at_lh(
&Point3::from_coordinates(*eye),
&Point3::from_coordinates(*center),
&Point3::from(*eye),
&Point3::from(*center),
up,
)
}
@ -59,8 +59,8 @@ pub fn look_at_lh<N: Real>(eye: &TVec3<N>, center: &TVec3<N>, up: &TVec3<N>) ->
/// * [`look_at_lh`](fn.look_at_lh.html)
pub fn look_at_rh<N: Real>(eye: &TVec3<N>, center: &TVec3<N>, up: &TVec3<N>) -> TMat4<N> {
TMat::look_at_rh(
&Point3::from_coordinates(*eye),
&Point3::from_coordinates(*center),
&Point3::from(*eye),
&Point3::from(*center),
up,
)
}

View File

@ -166,17 +166,19 @@ macro_rules! isometry_construction_impl(
/// # use nalgebra::{Isometry3, IsometryMatrix3, Point3, Vector3};
/// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
/// let translation = Vector3::new(1.0, 2.0, 3.0);
/// // Point and vector being transformed in the tests.
/// let pt = Point3::new(4.0, 5.0, 6.0);
/// let vec = Vector3::new(4.0, 5.0, 6.0);
///
/// // Isometry with its rotation part represented as a UnitQuaternion
/// let iso = Isometry3::new(translation, axisangle);
/// assert_relative_eq!(iso * Point3::new(4.0, 5.0, 6.0), Point3::new(7.0, 7.0, -1.0), epsilon = 1.0e-6);
/// assert_relative_eq!(iso * Vector3::new(4.0, 5.0, 6.0), Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
/// ```
/// assert_relative_eq!(iso * pt, Point3::new(7.0, 7.0, -1.0), epsilon = 1.0e-6);
/// assert_relative_eq!(iso * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
///
/// // Isometry with its rotation part represented as a Rotation3 (a 3x3 rotation matrix).
/// let iso = IsometryMatrix3::new(translation, axisangle);
/// assert_relative_eq!(iso * Point3::new(4.0, 5.0, 6.0), Point3::new(7.0, 7.0, -1.0), epsilon = 1.0e-6);
/// assert_relative_eq!(iso * Vector3::new(4.0, 5.0, 6.0), Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
/// assert_relative_eq!(iso * pt, Point3::new(7.0, 7.0, -1.0), epsilon = 1.0e-6);
/// assert_relative_eq!(iso * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
/// ```
#[inline]
pub fn new(translation: Vector3<N>, axisangle: Vector3<N>) -> Self {

View File

@ -50,7 +50,7 @@ where
{
#[inline]
fn clone(&self) -> Self {
Point::from_coordinates(self.coords.clone())
Point::from(self.coords.clone())
}
}
@ -76,7 +76,7 @@ where
where Des: Deserializer<'a> {
let coords = VectorN::<N, D>::deserialize(deserializer)?;
Ok(Point::from_coordinates(coords))
Ok(Point::from(coords))
}
}
@ -107,7 +107,7 @@ where DefaultAllocator: Allocator<N, D>
/// Clones this point into one that owns its data.
#[inline]
pub fn clone(&self) -> Point<N, D> {
Point::from_coordinates(self.coords.clone_owned())
Point::from(self.coords.clone_owned())
}
/// Converts this point into a vector in homogeneous coordinates, i.e., appends a `1` at the
@ -127,6 +127,7 @@ where DefaultAllocator: Allocator<N, D>
}
/// Creates a new point with the given coordinates.
#[deprecated(note = "Use Point::from(vector) instead.")]
#[inline]
pub fn from_coordinates(coords: VectorN<N, D>) -> Point<N, D> {
Point { coords: coords }

View File

@ -33,7 +33,7 @@ where DefaultAllocator: Allocator<N, D>
#[inline]
fn from_coordinates(coords: Self::Coordinates) -> Self {
Self::from_coordinates(coords)
Self::from(coords)
}
#[inline]
@ -54,7 +54,7 @@ where
{
#[inline]
fn meet(&self, other: &Self) -> Self {
Point::from_coordinates(self.coords.meet(&other.coords))
Point::from(self.coords.meet(&other.coords))
}
}
@ -65,7 +65,7 @@ where
{
#[inline]
fn join(&self, other: &Self) -> Self {
Point::from_coordinates(self.coords.join(&other.coords))
Point::from(self.coords.join(&other.coords))
}
}
@ -78,6 +78,6 @@ where
fn meet_join(&self, other: &Self) -> (Self, Self) {
let (meet, join) = self.coords.meet_join(&other.coords);
(Point::from_coordinates(meet), Point::from_coordinates(join))
(Point::from(meet), Point::from(join))
}
}

View File

@ -18,20 +18,20 @@ where DefaultAllocator: Allocator<N, D>
/// Creates a new point with uninitialized coordinates.
#[inline]
pub unsafe fn new_uninitialized() -> Self {
Self::from_coordinates(VectorN::new_uninitialized())
Self::from(VectorN::new_uninitialized())
}
/// Creates a new point with all coordinates equal to zero.
#[inline]
pub fn origin() -> Self
where N: Zero {
Self::from_coordinates(VectorN::from_element(N::zero()))
Self::from(VectorN::from_element(N::zero()))
}
/// Creates a new point from a slice.
#[inline]
pub fn from_slice(components: &[N]) -> Self {
Self::from_coordinates(VectorN::from_row_slice(components))
Self::from(VectorN::from_row_slice(components))
}
/// Creates a new point from its homogeneous vector representation.
@ -47,7 +47,7 @@ where DefaultAllocator: Allocator<N, D>
{
if !v[D::dim()].is_zero() {
let coords = v.fixed_slice::<D, U1>(0, 0) / v[D::dim()];
Some(Self::from_coordinates(coords))
Some(Self::from(coords))
} else {
None
}
@ -64,12 +64,12 @@ where DefaultAllocator: Allocator<N, D>
{
#[inline]
fn max_value() -> Self {
Self::from_coordinates(VectorN::max_value())
Self::from(VectorN::max_value())
}
#[inline]
fn min_value() -> Self {
Self::from_coordinates(VectorN::min_value())
Self::from(VectorN::min_value())
}
}
@ -80,7 +80,7 @@ where
{
#[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> {
Point::from_coordinates(rng.gen::<VectorN<N, D>>())
Point::from(rng.gen::<VectorN<N, D>>())
}
}
@ -92,7 +92,7 @@ where
{
#[inline]
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Point::from_coordinates(VectorN::arbitrary(g))
Point::from(VectorN::arbitrary(g))
}
}
@ -132,7 +132,9 @@ macro_rules! from_array_impl(
($($D: ty, $len: expr);*) => {$(
impl <N: Scalar> From<[N; $len]> for Point<N, $D> {
fn from (coords: [N; $len]) -> Self {
Point::from_coordinates(coords.into())
Point {
coords: coords.into()
}
}
}
)*}

View File

@ -33,7 +33,7 @@ where
{
#[inline]
fn to_superset(&self) -> Point<N2, D> {
Point::from_coordinates(self.coords.to_superset())
Point::from(self.coords.to_superset())
}
#[inline]
@ -45,7 +45,7 @@ where
#[inline]
unsafe fn from_superset_unchecked(m: &Point<N2, D>) -> Self {
Point::from_coordinates(Matrix::from_superset_unchecked(&m.coords))
Point::from(Matrix::from_superset_unchecked(&m.coords))
}
}
@ -73,7 +73,9 @@ where
#[inline]
unsafe fn from_superset_unchecked(v: &VectorN<N2, DimNameSum<D, U1>>) -> Self {
let coords = v.fixed_slice::<D, U1>(0, 0) / v[D::dim()];
Self::from_coordinates(::convert_unchecked(coords))
Self {
coords: ::convert_unchecked(coords)
}
}
}
@ -138,3 +140,15 @@ where
t.to_homogeneous()
}
}
impl<N: Scalar, D: DimName> From<VectorN<N, D>> for Point<N, D>
where
DefaultAllocator: Allocator<N, D>,
{
#[inline]
fn from(coords: VectorN<N, D>) -> Self {
Point {
coords
}
}
}

View File

@ -50,7 +50,7 @@ where DefaultAllocator: Allocator<N, D>
#[inline]
fn neg(self) -> Self::Output {
Point::from_coordinates(-self.coords)
Point::from(-self.coords)
}
}
@ -61,7 +61,7 @@ where DefaultAllocator: Allocator<N, D>
#[inline]
fn neg(self) -> Self::Output {
Point::from_coordinates(-&self.coords)
Point::from(-&self.coords)
}
}
@ -96,43 +96,43 @@ add_sub_impl!(Sub, sub, ClosedSub;
add_sub_impl!(Sub, sub, ClosedSub;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: &'a Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(&self.coords - right); 'a, 'b);
Self::Output::from(&self.coords - right); 'a, 'b);
add_sub_impl!(Sub, sub, ClosedSub;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: &'a Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(&self.coords - &right); 'a); // FIXME: should not be a ref to `right`.
Self::Output::from(&self.coords - &right); 'a); // FIXME: should not be a ref to `right`.
add_sub_impl!(Sub, sub, ClosedSub;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(self.coords - right); 'b);
Self::Output::from(self.coords - right); 'b);
add_sub_impl!(Sub, sub, ClosedSub;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(self.coords - right); );
Self::Output::from(self.coords - right); );
// Point + Vector
add_sub_impl!(Add, add, ClosedAdd;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: &'a Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(&self.coords + right); 'a, 'b);
Self::Output::from(&self.coords + right); 'a, 'b);
add_sub_impl!(Add, add, ClosedAdd;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: &'a Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(&self.coords + &right); 'a); // FIXME: should not be a ref to `right`.
Self::Output::from(&self.coords + &right); 'a); // FIXME: should not be a ref to `right`.
add_sub_impl!(Add, add, ClosedAdd;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: Point<N, D1>, right: &'b Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(self.coords + right); 'b);
Self::Output::from(self.coords + right); 'b);
add_sub_impl!(Add, add, ClosedAdd;
(D1, U1), (D2, U1) -> (D1) for D1: DimName, D2: Dim, SB: Storage<N, D2>;
self: Point<N, D1>, right: Vector<N, D2, SB>, Output = Point<N, D1>;
Self::Output::from_coordinates(self.coords + right); );
Self::Output::from(self.coords + right); );
// XXX: replace by the shared macro: add_sub_assign_impl
macro_rules! op_assign_impl(
@ -178,10 +178,10 @@ md_impl_all!(
(R1, C1), (D2, U1) for R1: DimName, C1: Dim, D2: DimName, SA: Storage<N, R1, C1>
where ShapeConstraint: AreMultipliable<R1, C1, D2, U1>;
self: Matrix<N, R1, C1, SA>, right: Point<N, D2>, Output = Point<N, R1>;
[val val] => Point::from_coordinates(self * right.coords);
[ref val] => Point::from_coordinates(self * right.coords);
[val ref] => Point::from_coordinates(self * &right.coords);
[ref ref] => Point::from_coordinates(self * &right.coords);
[val val] => Point::from(self * right.coords);
[ref val] => Point::from(self * right.coords);
[val ref] => Point::from(self * &right.coords);
[ref ref] => Point::from(self * &right.coords);
);
/*
@ -198,7 +198,7 @@ macro_rules! componentwise_scalarop_impl(
#[inline]
fn $method(self, right: N) -> Self::Output {
Point::from_coordinates(self.coords.$method(right))
Point::from(self.coords.$method(right))
}
}
@ -208,7 +208,7 @@ macro_rules! componentwise_scalarop_impl(
#[inline]
fn $method(self, right: N) -> Self::Output {
Point::from_coordinates((&self.coords).$method(right))
Point::from((&self.coords).$method(right))
}
}
@ -233,7 +233,7 @@ macro_rules! left_scalar_mul_impl(
#[inline]
fn mul(self, right: Point<$T, D>) -> Self::Output {
Point::from_coordinates(self * right.coords)
Point::from(self * right.coords)
}
}
@ -243,7 +243,7 @@ macro_rules! left_scalar_mul_impl(
#[inline]
fn mul(self, right: &'b Point<$T, D>) -> Self::Output {
Point::from_coordinates(self * &right.coords)
Point::from(self * &right.coords)
}
}
)*}

View File

@ -428,7 +428,7 @@ quaternion_op_impl!(
(U4, U1), (U3, U1);
self: &'a UnitQuaternion<N>, rhs: &'b Point3<N>,
Output = Point3<N> => U3, U4;
Point3::from_coordinates(self * &rhs.coords);
Point3::from(self * &rhs.coords);
'a, 'b);
quaternion_op_impl!(
@ -436,7 +436,7 @@ quaternion_op_impl!(
(U4, U1), (U3, U1);
self: &'a UnitQuaternion<N>, rhs: Point3<N>,
Output = Point3<N> => U3, U4;
Point3::from_coordinates(self * rhs.coords);
Point3::from(self * rhs.coords);
'a);
quaternion_op_impl!(
@ -444,7 +444,7 @@ quaternion_op_impl!(
(U4, U1), (U3, U1);
self: UnitQuaternion<N>, rhs: &'b Point3<N>,
Output = Point3<N> => U3, U4;
Point3::from_coordinates(self * &rhs.coords);
Point3::from(self * &rhs.coords);
'b);
quaternion_op_impl!(
@ -452,7 +452,7 @@ quaternion_op_impl!(
(U4, U1), (U3, U1);
self: UnitQuaternion<N>, rhs: Point3<N>,
Output = Point3<N> => U3, U4;
Point3::from_coordinates(self * rhs.coords);
Point3::from(self * rhs.coords);
);
// UnitQuaternion × Unit<Vector>

View File

@ -89,7 +89,7 @@ where DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>
{
#[inline]
fn inverse_transform_point(&self, pt: &Point<N, D>) -> Point<N, D> {
Point::from_coordinates(self.inverse_transform_vector(&pt.coords))
Point::from(self.inverse_transform_vector(&pt.coords))
}
#[inline]

View File

@ -220,7 +220,7 @@ complex_op_impl_all!(
[val val] => &self * &rhs;
[ref val] => self * &rhs;
[val ref] => &self * rhs;
[ref ref] => Point2::from_coordinates(self * &rhs.coords);
[ref ref] => Point2::from(self * &rhs.coords);
);
// UnitComplex × Vector