From 551c44c85458eb4bc146f1463cf3f3919bf638e3 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Tue, 23 Oct 2018 20:47:42 +0200 Subject: [PATCH] Implement From for Point. --- CHANGELOG.md | 4 +++ examples/point_construction.rs | 2 +- nalgebra-glm/src/ext/matrix_transform.rs | 8 +++--- src/geometry/isometry_construction.rs | 12 ++++---- src/geometry/point.rs | 7 +++-- src/geometry/point_alga.rs | 8 +++--- src/geometry/point_construction.rs | 20 +++++++------ src/geometry/point_conversion.rs | 20 +++++++++++-- src/geometry/point_ops.rs | 36 ++++++++++++------------ src/geometry/quaternion_ops.rs | 8 +++--- src/geometry/rotation_alga.rs | 2 +- src/geometry/unit_complex_ops.rs | 2 +- 12 files changed, 76 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bae7fa8..6de168c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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. diff --git a/examples/point_construction.rs b/examples/point_construction.rs index 7fc32838..19430997 100644 --- a/examples/point_construction.rs +++ b/examples/point_construction.rs @@ -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); diff --git a/nalgebra-glm/src/ext/matrix_transform.rs b/nalgebra-glm/src/ext/matrix_transform.rs index fb23efc9..1a090bd3 100644 --- a/nalgebra-glm/src/ext/matrix_transform.rs +++ b/nalgebra-glm/src/ext/matrix_transform.rs @@ -39,8 +39,8 @@ pub fn look_at(eye: &TVec3, center: &TVec3, up: &TVec3) -> TMa /// * [`look_at_rh`](fn.look_at_rh.html) pub fn look_at_lh(eye: &TVec3, center: &TVec3, up: &TVec3) -> TMat4 { 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(eye: &TVec3, center: &TVec3, up: &TVec3) -> /// * [`look_at_lh`](fn.look_at_lh.html) pub fn look_at_rh(eye: &TVec3, center: &TVec3, up: &TVec3) -> TMat4 { TMat::look_at_rh( - &Point3::from_coordinates(*eye), - &Point3::from_coordinates(*center), + &Point3::from(*eye), + &Point3::from(*center), up, ) } diff --git a/src/geometry/isometry_construction.rs b/src/geometry/isometry_construction.rs index f333e199..972e80ec 100644 --- a/src/geometry/isometry_construction.rs +++ b/src/geometry/isometry_construction.rs @@ -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, axisangle: Vector3) -> Self { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index d24b2cd4..6c9cd904 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -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::::deserialize(deserializer)?; - Ok(Point::from_coordinates(coords)) + Ok(Point::from(coords)) } } @@ -107,7 +107,7 @@ where DefaultAllocator: Allocator /// Clones this point into one that owns its data. #[inline] pub fn clone(&self) -> Point { - 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 } /// Creates a new point with the given coordinates. + #[deprecated(note = "Use Point::from(vector) instead.")] #[inline] pub fn from_coordinates(coords: VectorN) -> Point { Point { coords: coords } diff --git a/src/geometry/point_alga.rs b/src/geometry/point_alga.rs index 42a1088a..56940dd1 100644 --- a/src/geometry/point_alga.rs +++ b/src/geometry/point_alga.rs @@ -33,7 +33,7 @@ where DefaultAllocator: Allocator #[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)) } } diff --git a/src/geometry/point_construction.rs b/src/geometry/point_construction.rs index c4f6bfc1..dfa38ee1 100644 --- a/src/geometry/point_construction.rs +++ b/src/geometry/point_construction.rs @@ -18,20 +18,20 @@ where DefaultAllocator: Allocator /// 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 { if !v[D::dim()].is_zero() { let coords = v.fixed_slice::(0, 0) / v[D::dim()]; - Some(Self::from_coordinates(coords)) + Some(Self::from(coords)) } else { None } @@ -64,12 +64,12 @@ where DefaultAllocator: Allocator { #[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 { - Point::from_coordinates(rng.gen::>()) + Point::from(rng.gen::>()) } } @@ -92,7 +92,7 @@ where { #[inline] fn arbitrary(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 From<[N; $len]> for Point { fn from (coords: [N; $len]) -> Self { - Point::from_coordinates(coords.into()) + Point { + coords: coords.into() + } } } )*} diff --git a/src/geometry/point_conversion.rs b/src/geometry/point_conversion.rs index 1b1cbaf7..84efab6d 100644 --- a/src/geometry/point_conversion.rs +++ b/src/geometry/point_conversion.rs @@ -33,7 +33,7 @@ where { #[inline] fn to_superset(&self) -> Point { - 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) -> 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>) -> Self { let coords = v.fixed_slice::(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 From> for Point + where + DefaultAllocator: Allocator, +{ + #[inline] + fn from(coords: VectorN) -> Self { + Point { + coords + } + } +} diff --git a/src/geometry/point_ops.rs b/src/geometry/point_ops.rs index 03b07c39..a923fbf9 100644 --- a/src/geometry/point_ops.rs +++ b/src/geometry/point_ops.rs @@ -50,7 +50,7 @@ where DefaultAllocator: Allocator #[inline] fn neg(self) -> Self::Output { - Point::from_coordinates(-self.coords) + Point::from(-self.coords) } } @@ -61,7 +61,7 @@ where DefaultAllocator: Allocator #[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; self: &'a Point, right: &'b Vector, Output = Point; - 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; self: &'a Point, right: Vector, Output = Point; - 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; self: Point, right: &'b Vector, Output = Point; - 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; self: Point, right: Vector, Output = Point; - 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; self: &'a Point, right: &'b Vector, Output = Point; - 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; self: &'a Point, right: Vector, Output = Point; - 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; self: Point, right: &'b Vector, Output = Point; - 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; self: Point, right: Vector, Output = Point; - 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 where ShapeConstraint: AreMultipliable; self: Matrix, right: Point, Output = Point; - [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) } } )*} diff --git a/src/geometry/quaternion_ops.rs b/src/geometry/quaternion_ops.rs index 498a0def..b269335a 100644 --- a/src/geometry/quaternion_ops.rs +++ b/src/geometry/quaternion_ops.rs @@ -428,7 +428,7 @@ quaternion_op_impl!( (U4, U1), (U3, U1); self: &'a UnitQuaternion, rhs: &'b Point3, Output = Point3 => 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, rhs: Point3, Output = Point3 => 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, rhs: &'b Point3, Output = Point3 => 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, rhs: Point3, Output = Point3 => U3, U4; - Point3::from_coordinates(self * rhs.coords); + Point3::from(self * rhs.coords); ); // UnitQuaternion × Unit diff --git a/src/geometry/rotation_alga.rs b/src/geometry/rotation_alga.rs index 4f02bbae..b3bf7477 100644 --- a/src/geometry/rotation_alga.rs +++ b/src/geometry/rotation_alga.rs @@ -89,7 +89,7 @@ where DefaultAllocator: Allocator + Allocator { #[inline] fn inverse_transform_point(&self, pt: &Point) -> Point { - Point::from_coordinates(self.inverse_transform_vector(&pt.coords)) + Point::from(self.inverse_transform_vector(&pt.coords)) } #[inline] diff --git a/src/geometry/unit_complex_ops.rs b/src/geometry/unit_complex_ops.rs index 6456733c..9c840e10 100644 --- a/src/geometry/unit_complex_ops.rs +++ b/src/geometry/unit_complex_ops.rs @@ -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