Add short constructors for points, to match the ones for vectors

This commit is contained in:
Nathan Stoddard 2019-01-14 15:53:28 -08:00
parent a570349a0d
commit 2cf020351e
5 changed files with 21 additions and 7 deletions

View file

@ -80,7 +80,7 @@ pub use vector::{dot, Vector1, Vector2, Vector3, Vector4, vec1, vec2, vec3, vec4
pub use angle::{Deg, Rad}; pub use angle::{Deg, Rad};
pub use euler::Euler; pub use euler::Euler;
pub use point::{Point1, Point2, Point3}; pub use point::{Point1, Point2, Point3, point1, point2, point3};
pub use rotation::*; pub use rotation::*;
pub use transform::*; pub use transform::*;

View file

@ -78,7 +78,7 @@ impl<S: BaseNum> Point3<S> {
} }
macro_rules! impl_point { macro_rules! impl_point {
($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr) => { ($PointN:ident { $($field:ident),+ }, $VectorN:ident, $n:expr, $constructor:ident) => {
impl<S> $PointN<S> { impl<S> $PointN<S> {
/// Construct a new point, using the provided values. /// Construct a new point, using the provided values.
#[inline] #[inline]
@ -96,6 +96,12 @@ macro_rules! impl_point {
} }
} }
/// The short constructor.
#[inline]
pub const fn $constructor<S>($($field: S),+) -> $PointN<S> {
$PointN::new($($field),+)
}
impl<S: BaseNum> Array for $PointN<S> { impl<S: BaseNum> Array for $PointN<S> {
type Element = S; type Element = S;
@ -323,9 +329,9 @@ macro_rules! impl_scalar_ops {
}; };
} }
impl_point!(Point1 { x }, Vector1, 1); impl_point!(Point1 { x }, Vector1, 1, point1);
impl_point!(Point2 { x, y }, Vector2, 2); impl_point!(Point2 { x, y }, Vector2, 2, point2);
impl_point!(Point3 { x, y, z }, Vector3, 3); impl_point!(Point3 { x, y, z }, Vector3, 3, point3);
impl<S: Copy> Point1<S> { impl<S: Copy> Point1<S> {
impl_swizzle_functions!(Point1, Point2, Point3, S, x); impl_swizzle_functions!(Point1, Point2, Point3, S, x);

View file

@ -365,7 +365,7 @@ macro_rules! impl_vector_default {
/// The short constructor. /// The short constructor.
#[inline] #[inline]
pub fn $constructor<S>($($field: S),+) -> $VectorN<S> { pub const fn $constructor<S>($($field: S),+) -> $VectorN<S> {
$VectorN::new($($field),+) $VectorN::new($($field),+)
} }

View file

@ -17,7 +17,14 @@
extern crate approx; extern crate approx;
extern crate cgmath; extern crate cgmath;
use cgmath::{Point1, Point2, Point3}; use cgmath::*;
#[test]
fn test_constructor() {
assert_eq!(point1(1f32), Point1::new(1f32));
assert_eq!(point2(1f32, 2f32), Point2::new(1f32, 2f32));
assert_eq!(point3(1f64, 2f64, 3f64), Point3::new(1f64, 2f64, 3f64));
}
macro_rules! impl_test_mul { macro_rules! impl_test_mul {
($PointN:ident { $($field:ident),+ }, $s:expr, $v:expr) => ( ($PointN:ident { $($field:ident),+ }, $s:expr, $v:expr) => (

View file

@ -23,6 +23,7 @@ use std::iter;
#[test] #[test]
fn test_constructor() { fn test_constructor() {
assert_eq!(vec1(1f32), Vector1::new(1f32));
assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32)); assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32));
assert_eq!(vec3(1f64, 2f64, 3f64), Vector3::new(1f64, 2f64, 3f64)); assert_eq!(vec3(1f64, 2f64, 3f64), Vector3::new(1f64, 2f64, 3f64));
assert_eq!( assert_eq!(