2014-05-26 17:10:04 +00:00
|
|
|
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
|
2015-03-14 02:49:46 +00:00
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2013-09-03 03:54:03 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-07-29 06:05:15 +00:00
|
|
|
//! Types and traits for two, three, and four-dimensional vectors.
|
|
|
|
//!
|
|
|
|
//! ## Working with Vectors
|
|
|
|
//!
|
|
|
|
//! Vectors can be created in several different ways. There is, of course, the
|
2015-10-01 08:52:44 +00:00
|
|
|
//! traditional `new()` method, but unit vectors, zero vectors, and an one
|
2014-07-29 06:05:15 +00:00
|
|
|
//! vector are also provided:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-09-30 07:37:52 +00:00
|
|
|
//! use cgmath::{Vector, Vector2, Vector3, Vector4, vec2, vec3};
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! assert_eq!(Vector2::new(1.0f64, 0.0f64), Vector2::unit_x());
|
2015-09-30 07:37:52 +00:00
|
|
|
//! assert_eq!(vec3(0.0f64, 0.0f64, 0.0f64), Vector3::zero());
|
2015-04-05 02:14:03 +00:00
|
|
|
//! assert_eq!(Vector2::from_value(1.0f64), vec2(1.0, 1.0));
|
2014-07-29 06:05:15 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Vectors can be manipulated with typical mathematical operations (addition,
|
|
|
|
//! subtraction, element-wise multiplication, element-wise division, negation)
|
2015-09-30 07:37:52 +00:00
|
|
|
//! using the built-in operators.
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-09-30 07:37:52 +00:00
|
|
|
//! use cgmath::{Vector, Vector2, Vector3, Vector4};
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! let a: Vector2<f64> = Vector2::new(3.0, 4.0);
|
|
|
|
//! let b: Vector2<f64> = Vector2::new(-3.0, -4.0);
|
|
|
|
//!
|
2015-11-09 09:12:04 +00:00
|
|
|
//! assert_eq!(a + b, Vector2::zero());
|
|
|
|
//! assert_eq!(-(a * b), Vector2::new(9.0f64, 16.0f64));
|
|
|
|
//! assert_eq!(a / Vector2::one(), a);
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! // As with Rust's `int` and `f32` types, Vectors of different types cannot
|
|
|
|
//! // be added and so on with impunity. The following will fail to compile:
|
|
|
|
//! // let c = a + Vector3::new(1.0, 0.0, 2.0);
|
|
|
|
//!
|
|
|
|
//! // Instead, we need to convert the Vector2 to a Vector3 by "extending" it
|
|
|
|
//! // with the value for the last coordinate:
|
2015-11-09 09:12:04 +00:00
|
|
|
//! let c: Vector3<f64> = a.extend(0.0) + Vector3::new(1.0, 0.0, 2.0);
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! // Similarly, we can "truncate" a Vector4 down to a Vector3:
|
2015-11-09 09:12:04 +00:00
|
|
|
//! let d: Vector3<f64> = c + Vector4::unit_x().truncate();
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! assert_eq!(d, Vector3::new(5.0f64, 4.0f64, 2.0f64));
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Vectors also provide methods for typical operations such as
|
|
|
|
//! [scalar multiplication](http://en.wikipedia.org/wiki/Scalar_multiplication),
|
|
|
|
//! [dot products](http://en.wikipedia.org/wiki/Dot_product),
|
|
|
|
//! and [cross products](http://en.wikipedia.org/wiki/Cross_product).
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-09-30 07:37:52 +00:00
|
|
|
//! use cgmath::{Vector, Vector2, Vector3, Vector4, dot};
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! // All vectors implement the dot product as a method:
|
|
|
|
//! let a: Vector2<f64> = Vector2::new(3.0, 6.0);
|
|
|
|
//! let b: Vector2<f64> = Vector2::new(-2.0, 1.0);
|
2015-11-09 09:12:04 +00:00
|
|
|
//! assert_eq!(a.dot(b), 0.0);
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! // But there is also a top-level function:
|
2015-11-09 09:12:04 +00:00
|
|
|
//! assert_eq!(a.dot(b), dot(a, b));
|
2014-07-29 06:05:15 +00:00
|
|
|
//!
|
|
|
|
//! // Scalar multiplication can return a new object, or be done in place
|
|
|
|
//! // to avoid an allocation:
|
2015-04-05 02:14:03 +00:00
|
|
|
//! let mut c = Vector4::from_value(3f64);
|
2014-07-29 06:05:15 +00:00
|
|
|
//! let d: Vector4<f64> = c.mul_s(2.0);
|
|
|
|
//! c.mul_self_s(2.0);
|
|
|
|
//! assert_eq!(c, d);
|
|
|
|
//!
|
|
|
|
//! // Cross products are defined for 3-dimensional vectors:
|
|
|
|
//! let e: Vector3<f64> = Vector3::unit_x();
|
|
|
|
//! let f: Vector3<f64> = Vector3::unit_y();
|
2015-11-09 09:12:04 +00:00
|
|
|
//! assert_eq!(e.cross(f), Vector3::unit_z());
|
2014-07-29 06:05:15 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Several other useful methods are provided as well. Vector fields can be
|
|
|
|
//! accessed using array syntax (i.e. `vector[0] == vector.x`), or by using
|
2015-11-14 10:58:11 +00:00
|
|
|
//! the methods provided by the [`Array`](../array/trait.Array.html) trait.
|
2014-07-29 06:05:15 +00:00
|
|
|
//! This trait also provides a `map()` method for applying arbitrary functions.
|
|
|
|
//!
|
|
|
|
//! The [`Vector`](../trait.Vector.html) trait presents the most general
|
|
|
|
//! features of the vectors, while [`EuclideanVector`]
|
|
|
|
//! (../array/trait.EuclideanVector.html) is more specific to Euclidean space.
|
|
|
|
|
2013-10-19 14:00:44 +00:00
|
|
|
use std::fmt;
|
2014-05-28 01:59:03 +00:00
|
|
|
use std::mem;
|
2015-01-03 21:29:26 +00:00
|
|
|
use std::ops::*;
|
2013-09-03 03:54:03 +00:00
|
|
|
|
2015-03-15 02:53:57 +00:00
|
|
|
use rand::{Rand, Rng};
|
|
|
|
|
2015-09-29 11:36:57 +00:00
|
|
|
use rust_num::{NumCast, Zero, One};
|
2015-04-05 01:19:11 +00:00
|
|
|
|
2013-09-06 06:39:15 +00:00
|
|
|
use angle::{Rad, atan2, acos};
|
2014-01-09 00:26:50 +00:00
|
|
|
use approx::ApproxEq;
|
2015-11-14 10:58:11 +00:00
|
|
|
use array::Array;
|
2015-11-14 01:03:12 +00:00
|
|
|
use num::{BaseNum, BaseFloat, PartialOrd};
|
2013-09-03 03:54:03 +00:00
|
|
|
|
2013-09-03 06:37:06 +00:00
|
|
|
/// A trait that specifies a range of numeric operations for vectors. Not all
|
|
|
|
/// of these make sense from a linear algebra point of view, but are included
|
|
|
|
/// for pragmatic reasons.
|
2015-11-09 09:12:04 +00:00
|
|
|
pub trait Vector: Copy + Clone where
|
2015-11-03 04:40:52 +00:00
|
|
|
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
2015-11-14 10:58:11 +00:00
|
|
|
Self: Array<Element = <Self as Vector>::Scalar>,
|
2015-09-30 07:37:52 +00:00
|
|
|
// FIXME: blocked by rust-lang/rust#20671
|
|
|
|
//
|
|
|
|
// for<'a, 'b> &'a Self: Add<&'b Self, Output = Self>,
|
|
|
|
// for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
|
|
|
|
// for<'a, 'b> &'a Self: Mul<&'b Self, Output = Self>,
|
|
|
|
// for<'a, 'b> &'a Self: Div<&'b Self, Output = Self>,
|
|
|
|
// for<'a, 'b> &'a Self: Rem<&'b Self, Output = Self>,
|
|
|
|
// for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
|
|
|
|
//
|
|
|
|
// for<'a> &'a Self: Add<S, Output = Self>,
|
|
|
|
// for<'a> &'a Self: Sub<S, Output = Self>,
|
|
|
|
// for<'a> &'a Self: Mul<S, Output = Self>,
|
|
|
|
// for<'a> &'a Self: Div<S, Output = Self>,
|
|
|
|
// for<'a> &'a Self: Rem<S, Output = Self>,
|
|
|
|
{
|
2015-11-03 04:23:22 +00:00
|
|
|
/// The associated scalar.
|
2015-11-03 03:30:59 +00:00
|
|
|
type Scalar: BaseNum;
|
|
|
|
|
2015-03-01 06:05:22 +00:00
|
|
|
/// Construct a vector from a single value, replicating it.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn from_value(scalar: Self::Scalar) -> Self;
|
2015-09-30 07:37:52 +00:00
|
|
|
|
|
|
|
/// The zero vector (with all components set to zero)
|
|
|
|
#[inline]
|
2015-11-03 03:30:59 +00:00
|
|
|
fn zero() -> Self { Self::from_value(Self::Scalar::zero()) }
|
2015-09-30 07:37:52 +00:00
|
|
|
/// The identity vector (with all components set to one)
|
|
|
|
#[inline]
|
2015-11-03 03:30:59 +00:00
|
|
|
fn one() -> Self { Self::from_value(Self::Scalar::one()) }
|
2015-09-30 07:37:52 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Add a scalar to this vector, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn add_s(self, scalar: Self::Scalar) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Subtract a scalar from this vector, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn sub_s(self, scalar: Self::Scalar) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Multiply this vector by a scalar, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn mul_s(self, scalar: Self::Scalar) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Divide this vector by a scalar, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn div_s(self, scalar: Self::Scalar) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Take the remainder of this vector by a scalar, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn rem_s(self, scalar: Self::Scalar) -> Self;
|
2013-09-14 01:58:19 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Add this vector to another, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn add_v(self, v: Self) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Subtract another vector from this one, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn sub_v(self, v: Self) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Multiply this vector by another, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn mul_v(self, v: Self) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Divide this vector by another, returning a new vector.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn div_v(self, v: Self) -> Self;
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Take the remainder of this vector by another, returning a new scalar.
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn rem_v(self, v: Self) -> Self;
|
2013-09-06 06:53:37 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Add a scalar to this vector in-place.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn add_self_s(&mut self, scalar: Self::Scalar);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Subtract a scalar from this vector, in-place.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn sub_self_s(&mut self, scalar: Self::Scalar);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Multiply this vector by a scalar, in-place.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn mul_self_s(&mut self, scalar: Self::Scalar);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Divide this vector by a scalar, in-place.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn div_self_s(&mut self, scalar: Self::Scalar);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Take the remainder of this vector by a scalar, in-place.
|
2015-11-03 03:30:59 +00:00
|
|
|
fn rem_self_s(&mut self, scalar: Self::Scalar);
|
2013-09-06 06:53:37 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Add another vector to this one, in-place.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn add_self_v(&mut self, v: Self);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Subtract another vector from this one, in-place.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn sub_self_v(&mut self, v: Self);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Multiply this matrix by another, in-place.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn mul_self_v(&mut self, v: Self);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Divide this matrix by anothor, in-place.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn div_self_v(&mut self, v: Self);
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Take the remainder of this vector by another, in-place.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn rem_self_v(&mut self, v: Self);
|
2013-09-03 06:37:06 +00:00
|
|
|
|
2015-11-14 01:03:12 +00:00
|
|
|
/// Vector dot product
|
|
|
|
fn dot(self, other: Self) -> Self::Scalar;
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Dot product of two vectors.
|
2015-11-09 09:12:04 +00:00
|
|
|
#[inline] pub fn dot<V: Vector>(a: V, b: V) -> V::Scalar { a.dot(b) }
|
2013-09-14 01:53:12 +00:00
|
|
|
|
2014-01-23 16:13:53 +00:00
|
|
|
// Utility macro for generating associated functions for the vectors
|
2015-09-20 15:32:53 +00:00
|
|
|
macro_rules! vec {
|
2015-10-02 04:54:33 +00:00
|
|
|
($VectorN:ident <$S:ident> { $($field:ident),+ }, $n:expr, $constructor:ident) => {
|
2015-02-08 18:25:42 +00:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)]
|
2015-10-02 04:54:33 +00:00
|
|
|
pub struct $VectorN<S> { $(pub $field: S),+ }
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2015-10-02 04:54:33 +00:00
|
|
|
impl<$S> $VectorN<$S> {
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Construct a new vector, using the provided values.
|
2014-01-23 16:13:53 +00:00
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
pub fn new($($field: $S),+) -> $VectorN<$S> {
|
|
|
|
$VectorN { $($field: $field),+ }
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
2014-05-28 01:59:03 +00:00
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2015-10-02 04:54:33 +00:00
|
|
|
impl<$S: Copy + Neg<Output = $S>> $VectorN<$S> {
|
2015-04-05 00:15:00 +00:00
|
|
|
/// Negate this vector in-place (multiply by -1).
|
|
|
|
#[inline]
|
|
|
|
pub fn neg_self(&mut self) {
|
|
|
|
$(self.$field = -self.$field);+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:16:22 +00:00
|
|
|
/// The short constructor.
|
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
pub fn $constructor<S>($($field: S),+) -> $VectorN<S> {
|
|
|
|
$VectorN::new($($field),+)
|
2015-02-06 05:16:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 04:54:33 +00:00
|
|
|
impl<$S: NumCast + Copy> $VectorN<$S> {
|
2014-11-20 14:24:38 +00:00
|
|
|
/// Component-wise casting to another type
|
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
pub fn cast<T: NumCast>(&self) -> $VectorN<T> {
|
|
|
|
$VectorN { $($field: NumCast::from(self.$field).unwrap()),+ }
|
2014-11-20 14:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 10:58:11 +00:00
|
|
|
impl<S: Copy> Array for $VectorN<S> {
|
2015-11-03 03:15:11 +00:00
|
|
|
type Element = S;
|
2015-11-14 01:03:12 +00:00
|
|
|
|
|
|
|
#[inline] fn sum(self) -> S where S: Add<Output = S> { fold!(add, { $(self.$field),+ }) }
|
|
|
|
#[inline] fn product(self) -> S where S: Mul<Output = S> { fold!(mul, { $(self.$field),+ }) }
|
|
|
|
#[inline] fn min(self) -> S where S: PartialOrd { fold!(partial_min, { $(self.$field),+ }) }
|
|
|
|
#[inline] fn max(self) -> S where S: PartialOrd { fold!(partial_max, { $(self.$field),+ }) }
|
2015-11-03 03:15:11 +00:00
|
|
|
}
|
2014-05-28 01:59:03 +00:00
|
|
|
|
2015-11-03 03:30:59 +00:00
|
|
|
impl<S: BaseNum> Vector for $VectorN<S> {
|
|
|
|
type Scalar = S;
|
|
|
|
|
|
|
|
#[inline] fn from_value(scalar: S) -> $VectorN<S> { $VectorN { $($field: scalar),+ } }
|
2015-09-30 07:37:52 +00:00
|
|
|
|
2015-11-09 09:12:04 +00:00
|
|
|
#[inline] fn add_s(self, scalar: S) -> $VectorN<S> { self + scalar }
|
|
|
|
#[inline] fn sub_s(self, scalar: S) -> $VectorN<S> { self - scalar }
|
|
|
|
#[inline] fn mul_s(self, scalar: S) -> $VectorN<S> { self * scalar }
|
|
|
|
#[inline] fn div_s(self, scalar: S) -> $VectorN<S> { self / scalar }
|
|
|
|
#[inline] fn rem_s(self, scalar: S) -> $VectorN<S> { self % scalar }
|
2015-09-30 07:37:52 +00:00
|
|
|
|
2015-11-09 09:12:04 +00:00
|
|
|
#[inline] fn add_v(self, v: $VectorN<S>) -> $VectorN<S> { self + v }
|
|
|
|
#[inline] fn sub_v(self, v: $VectorN<S>) -> $VectorN<S> { self - v }
|
|
|
|
#[inline] fn mul_v(self, v: $VectorN<S>) -> $VectorN<S> { self * v }
|
|
|
|
#[inline] fn div_v(self, v: $VectorN<S>) -> $VectorN<S> { self / v }
|
|
|
|
#[inline] fn rem_v(self, v: $VectorN<S>) -> $VectorN<S> { self % v }
|
2015-09-30 07:37:52 +00:00
|
|
|
|
2015-11-03 03:30:59 +00:00
|
|
|
#[inline] fn add_self_s(&mut self, scalar: S) { *self = &*self + scalar; }
|
|
|
|
#[inline] fn sub_self_s(&mut self, scalar: S) { *self = &*self - scalar; }
|
|
|
|
#[inline] fn mul_self_s(&mut self, scalar: S) { *self = &*self * scalar; }
|
|
|
|
#[inline] fn div_self_s(&mut self, scalar: S) { *self = &*self / scalar; }
|
|
|
|
#[inline] fn rem_self_s(&mut self, scalar: S) { *self = &*self % scalar; }
|
2015-09-30 07:37:52 +00:00
|
|
|
|
2015-11-09 09:12:04 +00:00
|
|
|
#[inline] fn add_self_v(&mut self, v: $VectorN<S>) { *self = &*self + v; }
|
|
|
|
#[inline] fn sub_self_v(&mut self, v: $VectorN<S>) { *self = &*self - v; }
|
|
|
|
#[inline] fn mul_self_v(&mut self, v: $VectorN<S>) { *self = &*self * v; }
|
|
|
|
#[inline] fn div_self_v(&mut self, v: $VectorN<S>) { *self = &*self / v; }
|
|
|
|
#[inline] fn rem_self_v(&mut self, v: $VectorN<S>) { *self = &*self % v; }
|
2014-05-28 01:59:03 +00:00
|
|
|
|
2015-11-14 01:03:12 +00:00
|
|
|
#[inline] fn dot(self, other: $VectorN<S>) -> S { (self * other).sum() }
|
2014-05-28 01:59:03 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 04:54:33 +00:00
|
|
|
impl<S: Neg<Output = S>> Neg for $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
2015-01-05 01:56:01 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
fn neg(self) -> $VectorN<S> { $VectorN::new($(-self.$field),+) }
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
2013-09-06 06:53:37 +00:00
|
|
|
|
2015-11-03 03:00:39 +00:00
|
|
|
impl<S: BaseFloat> ApproxEq for $VectorN<S> {
|
|
|
|
type Epsilon = S;
|
|
|
|
|
2015-01-05 01:56:01 +00:00
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
fn approx_eq_eps(&self, other: &$VectorN<S>, epsilon: &S) -> bool {
|
2015-09-30 07:37:52 +00:00
|
|
|
$(self.$field.approx_eq_eps(&other.$field, epsilon))&&+
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
2013-09-06 06:39:15 +00:00
|
|
|
|
2015-10-02 04:54:33 +00:00
|
|
|
impl<S: BaseFloat + Rand> Rand for $VectorN<S> {
|
2015-01-05 01:56:01 +00:00
|
|
|
#[inline]
|
2015-10-02 04:54:33 +00:00
|
|
|
fn rand<R: Rng>(rng: &mut R) -> $VectorN<S> {
|
|
|
|
$VectorN { $($field: rng.gen()),+ }
|
2015-09-30 07:37:52 +00:00
|
|
|
}
|
2014-07-29 05:21:58 +00:00
|
|
|
}
|
2015-09-30 07:37:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-29 05:21:58 +00:00
|
|
|
|
2015-09-30 07:37:52 +00:00
|
|
|
macro_rules! impl_binary_operator {
|
|
|
|
($Binop:ident :: $binop:ident, $VectorN:ident { $($field:ident),+ }) => {
|
2015-11-08 06:41:57 +00:00
|
|
|
impl<S: BaseNum> $Binop<S> for $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, scalar: S) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(scalar)),+)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 07:37:52 +00:00
|
|
|
impl<'a, S: BaseNum> $Binop<S> for &'a $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
2014-07-29 05:21:58 +00:00
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
#[inline]
|
2015-11-08 06:41:57 +00:00
|
|
|
fn $binop(self, scalar: S) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(scalar)),+)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: BaseNum> $Binop<$VectorN<S>> for $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $VectorN<S>) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseNum> $Binop<&'a $VectorN<S>> for $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: &'a $VectorN<S>) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: BaseNum> $Binop<$VectorN<S>> for &'a $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $binop(self, other: $VectorN<S>) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
2014-05-28 01:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-15 02:53:57 +00:00
|
|
|
|
2015-09-30 07:37:52 +00:00
|
|
|
impl<'a, 'b, S: BaseNum> $Binop<&'a $VectorN<S>> for &'b $VectorN<S> {
|
|
|
|
type Output = $VectorN<S>;
|
|
|
|
|
2015-03-15 02:53:57 +00:00
|
|
|
#[inline]
|
2015-09-30 07:37:52 +00:00
|
|
|
fn $binop(self, other: &'a $VectorN<S>) -> $VectorN<S> {
|
|
|
|
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
2015-03-15 02:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-23 16:00:24 +00:00
|
|
|
|
2015-09-30 07:37:52 +00:00
|
|
|
impl_binary_operator!(Add::add, Vector2 { x, y });
|
|
|
|
impl_binary_operator!(Add::add, Vector3 { x, y, z });
|
|
|
|
impl_binary_operator!(Add::add, Vector4 { x, y, z, w });
|
|
|
|
impl_binary_operator!(Sub::sub, Vector2 { x, y });
|
|
|
|
impl_binary_operator!(Sub::sub, Vector3 { x, y, z });
|
|
|
|
impl_binary_operator!(Sub::sub, Vector4 { x, y, z, w });
|
|
|
|
impl_binary_operator!(Mul::mul, Vector2 { x, y });
|
|
|
|
impl_binary_operator!(Mul::mul, Vector3 { x, y, z });
|
|
|
|
impl_binary_operator!(Mul::mul, Vector4 { x, y, z, w });
|
|
|
|
impl_binary_operator!(Div::div, Vector2 { x, y });
|
|
|
|
impl_binary_operator!(Div::div, Vector3 { x, y, z });
|
|
|
|
impl_binary_operator!(Div::div, Vector4 { x, y, z, w });
|
|
|
|
impl_binary_operator!(Rem::rem, Vector2 { x, y });
|
|
|
|
impl_binary_operator!(Rem::rem, Vector3 { x, y, z });
|
|
|
|
impl_binary_operator!(Rem::rem, Vector4 { x, y, z, w });
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
macro_rules! fold {
|
|
|
|
(&$method:ident, { $x:expr, $y:expr }) => { $x.$method(&$y) };
|
|
|
|
(&$method:ident, { $x:expr, $y:expr, $z:expr }) => { $x.$method(&$y).$method(&$z) };
|
|
|
|
(&$method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { $x.$method(&$y).$method(&$z).$method(&$w) };
|
|
|
|
($method:ident, { $x:expr, $y:expr }) => { $x.$method($y) };
|
|
|
|
($method:ident, { $x:expr, $y:expr, $z:expr }) => { $x.$method($y).$method($z) };
|
|
|
|
($method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { $x.$method($y).$method($z).$method($w) };
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:16:22 +00:00
|
|
|
vec!(Vector2<S> { x, y }, 2, vec2);
|
|
|
|
vec!(Vector3<S> { x, y, z }, 3, vec3);
|
|
|
|
vec!(Vector4<S> { x, y, z, w }, 4, vec4);
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2015-09-20 15:32:53 +00:00
|
|
|
macro_rules! fixed_array_conversions {
|
2015-09-27 03:01:14 +00:00
|
|
|
($VectorN:ident <$S:ident> { $($field:ident : $index:expr),+ }, $n:expr) => {
|
2015-09-20 15:32:53 +00:00
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> Into<[$S; $n]> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn into(self) -> [$S; $n] {
|
2015-09-27 03:01:14 +00:00
|
|
|
match self { $VectorN { $($field),+ } => [$($field),+] }
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> AsRef<[$S; $n]> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &[$S; $n] {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> AsMut<[$S; $n]> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut [$S; $n] {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S: Clone> From<[$S; $n]> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: [$S; $n]) -> $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
// We need to use a clone here because we can't pattern match on arrays yet
|
2015-09-27 03:01:14 +00:00
|
|
|
$VectorN { $($field: v[$index].clone()),+ }
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<'a, $S> From<&'a [$S; $n]> for &'a $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: &'a [$S; $n]) -> &'a $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<'a, $S> From<&'a mut [$S; $n]> for &'a mut $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: &'a mut [$S; $n]) -> &'a mut $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed_array_conversions!(Vector2<S> { x:0, y:1 }, 2);
|
|
|
|
fixed_array_conversions!(Vector3<S> { x:0, y:1, z:2 }, 3);
|
|
|
|
fixed_array_conversions!(Vector4<S> { x:0, y:1, z:2, w:3 }, 4);
|
|
|
|
|
|
|
|
macro_rules! tuple_conversions {
|
2015-09-27 03:01:14 +00:00
|
|
|
($VectorN:ident <$S:ident> { $($field:ident),+ }, $Tuple:ty) => {
|
|
|
|
impl<$S> Into<$Tuple> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn into(self) -> $Tuple {
|
2015-09-27 03:01:14 +00:00
|
|
|
match self { $VectorN { $($field),+ } => ($($field),+) }
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> AsRef<$Tuple> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &$Tuple {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> AsMut<$Tuple> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
|
|
|
fn as_mut(&mut self) -> &mut $Tuple {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<$S> From<$Tuple> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: $Tuple) -> $VectorN<$S> {
|
|
|
|
match v { ($($field),+) => $VectorN { $($field: $field),+ } }
|
2015-09-20 15:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<'a, $S> From<&'a $Tuple> for &'a $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: &'a $Tuple) -> &'a $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:01:14 +00:00
|
|
|
impl<'a, $S> From<&'a mut $Tuple> for &'a mut $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-27 03:01:14 +00:00
|
|
|
fn from(v: &'a mut $Tuple) -> &'a mut $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
unsafe { mem::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple_conversions!(Vector2<S> { x, y }, (S, S));
|
|
|
|
tuple_conversions!(Vector3<S> { x, y, z }, (S, S, S));
|
|
|
|
tuple_conversions!(Vector4<S> { x, y, z, w }, (S, S, S, S));
|
|
|
|
|
|
|
|
macro_rules! index_operators {
|
2015-09-20 21:56:03 +00:00
|
|
|
($VectorN:ident<$S:ident>, $n:expr, $Output:ty, $I:ty) => {
|
|
|
|
impl<$S> Index<$I> for $VectorN<$S> {
|
|
|
|
type Output = $Output;
|
2015-09-20 15:32:53 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-09-20 21:56:03 +00:00
|
|
|
fn index<'a>(&'a self, i: $I) -> &'a $Output {
|
2015-09-20 15:32:53 +00:00
|
|
|
let v: &[$S; $n] = self.as_ref(); &v[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:56:03 +00:00
|
|
|
impl<$S> IndexMut<$I> for $VectorN<$S> {
|
2015-09-20 15:32:53 +00:00
|
|
|
#[inline]
|
2015-09-20 21:56:03 +00:00
|
|
|
fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output {
|
2015-09-20 15:32:53 +00:00
|
|
|
let v: &mut [$S; $n] = self.as_mut(); &mut v[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:56:03 +00:00
|
|
|
index_operators!(Vector2<S>, 2, S, usize);
|
|
|
|
index_operators!(Vector3<S>, 3, S, usize);
|
|
|
|
index_operators!(Vector4<S>, 4, S, usize);
|
|
|
|
index_operators!(Vector2<S>, 2, [S], Range<usize>);
|
|
|
|
index_operators!(Vector3<S>, 3, [S], Range<usize>);
|
|
|
|
index_operators!(Vector4<S>, 4, [S], Range<usize>);
|
|
|
|
index_operators!(Vector2<S>, 2, [S], RangeTo<usize>);
|
|
|
|
index_operators!(Vector3<S>, 3, [S], RangeTo<usize>);
|
|
|
|
index_operators!(Vector4<S>, 4, [S], RangeTo<usize>);
|
|
|
|
index_operators!(Vector2<S>, 2, [S], RangeFrom<usize>);
|
|
|
|
index_operators!(Vector3<S>, 3, [S], RangeFrom<usize>);
|
|
|
|
index_operators!(Vector4<S>, 4, [S], RangeFrom<usize>);
|
|
|
|
index_operators!(Vector2<S>, 2, [S], RangeFull);
|
|
|
|
index_operators!(Vector3<S>, 3, [S], RangeFull);
|
|
|
|
index_operators!(Vector4<S>, 4, [S], RangeFull);
|
2015-09-20 15:32:53 +00:00
|
|
|
|
2013-09-03 03:54:03 +00:00
|
|
|
/// Operations specific to numeric two-dimensional vectors.
|
2014-05-26 17:10:04 +00:00
|
|
|
impl<S: BaseNum> Vector2<S> {
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `x` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_x() -> Vector2<S> {
|
|
|
|
Vector2::new(S::one(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `y` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_y() -> Vector2<S> {
|
|
|
|
Vector2::new(S::zero(), S::one())
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2013-09-03 03:54:03 +00:00
|
|
|
/// The perpendicular dot product of the vector and `other`.
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn perp_dot(self, other: Vector2<S>) -> S {
|
2013-09-03 03:54:03 +00:00
|
|
|
(self.x * other.y) - (self.y * other.x)
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Create a `Vector3`, using the `x` and `y` values from this vector, and the
|
|
|
|
/// provided `z`.
|
2014-01-23 16:13:53 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn extend(self, z: S)-> Vector3<S> {
|
2014-07-29 18:06:31 +00:00
|
|
|
Vector3::new(self.x, self.y, z)
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Operations specific to numeric three-dimensional vectors.
|
2014-05-26 17:10:04 +00:00
|
|
|
impl<S: BaseNum> Vector3<S> {
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `x` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_x() -> Vector3<S> {
|
|
|
|
Vector3::new(S::one(), S::zero(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `y` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_y() -> Vector3<S> {
|
|
|
|
Vector3::new(S::zero(), S::one(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `w` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_z() -> Vector3<S> {
|
|
|
|
Vector3::new(S::zero(), S::zero(), S::one())
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2013-09-03 03:54:03 +00:00
|
|
|
/// Returns the cross product of the vector and `other`.
|
|
|
|
#[inline]
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn cross(self, other: Vector3<S>) -> Vector3<S> {
|
2014-04-14 01:30:24 +00:00
|
|
|
Vector3::new((self.y * other.z) - (self.z * other.y),
|
2014-07-29 18:06:31 +00:00
|
|
|
(self.z * other.x) - (self.x * other.z),
|
|
|
|
(self.x * other.y) - (self.y * other.x))
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
2013-09-06 02:32:07 +00:00
|
|
|
|
|
|
|
/// Calculates the cross product of the vector and `other`, then stores the
|
|
|
|
/// result in `self`.
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn cross_self(&mut self, other: Vector3<S>) {
|
2013-09-06 02:32:07 +00:00
|
|
|
*self = self.cross(other)
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Create a `Vector4`, using the `x`, `y` and `z` values from this vector, and the
|
|
|
|
/// provided `w`.
|
2014-01-23 16:13:53 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn extend(self, w: S)-> Vector4<S> {
|
2014-07-29 18:06:31 +00:00
|
|
|
Vector4::new(self.x, self.y, self.z, w)
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Create a `Vector2`, dropping the `z` value.
|
2014-01-23 16:13:53 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn truncate(self)-> Vector2<S> {
|
2014-07-29 18:06:31 +00:00
|
|
|
Vector2::new(self.x, self.y)
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Operations specific to numeric four-dimensional vectors.
|
2014-05-26 17:10:04 +00:00
|
|
|
impl<S: BaseNum> Vector4<S> {
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `x` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_x() -> Vector4<S> {
|
|
|
|
Vector4::new(S::one(), S::zero(), S::zero(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `y` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_y() -> Vector4<S> {
|
|
|
|
Vector4::new(S::zero(), S::one(), S::zero(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `z` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_z() -> Vector4<S> {
|
|
|
|
Vector4::new(S::zero(), S::zero(), S::one(), S::zero())
|
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// A unit vector in the `w` direction.
|
2015-09-29 11:36:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn unit_w() -> Vector4<S> {
|
|
|
|
Vector4::new(S::zero(), S::zero(), S::zero(), S::one())
|
|
|
|
}
|
2014-01-23 16:13:53 +00:00
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Create a `Vector3`, dropping the `w` value.
|
2014-01-23 16:13:53 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
pub fn truncate(self)-> Vector3<S> {
|
2014-07-29 18:06:31 +00:00
|
|
|
Vector3::new(self.x, self.y, self.z)
|
2014-01-23 16:13:53 +00:00
|
|
|
}
|
2014-06-05 07:58:09 +00:00
|
|
|
|
|
|
|
/// Create a `Vector3`, dropping the nth element
|
|
|
|
#[inline]
|
2015-01-09 23:16:39 +00:00
|
|
|
pub fn truncate_n(&self, n: isize)-> Vector3<S> {
|
2014-06-05 07:58:09 +00:00
|
|
|
match n {
|
2014-07-29 18:06:31 +00:00
|
|
|
0 => Vector3::new(self.y, self.z, self.w),
|
|
|
|
1 => Vector3::new(self.x, self.z, self.w),
|
|
|
|
2 => Vector3::new(self.x, self.y, self.w),
|
|
|
|
3 => Vector3::new(self.x, self.y, self.z),
|
2015-01-09 22:06:45 +00:00
|
|
|
_ => panic!("{:?} is out of range", n)
|
2014-06-05 07:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 06:37:06 +00:00
|
|
|
/// Specifies geometric operations for vectors. This is only implemented for
|
|
|
|
/// 2-dimensional and 3-dimensional vectors.
|
2015-11-03 04:23:22 +00:00
|
|
|
pub trait EuclideanVector: Vector + Sized where
|
2015-11-03 04:40:52 +00:00
|
|
|
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
2015-11-03 04:23:22 +00:00
|
|
|
<Self as Vector>::Scalar: BaseFloat,
|
|
|
|
Self: ApproxEq<Epsilon = <Self as Vector>::Scalar>,
|
|
|
|
{
|
2014-05-25 09:43:51 +00:00
|
|
|
/// Returns `true` if the vector is perpendicular (at right angles) to the
|
|
|
|
/// other vector.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn is_perpendicular(self, other: Self) -> bool {
|
2015-11-03 04:23:22 +00:00
|
|
|
self.dot(other).approx_eq(&Self::Scalar::zero())
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the squared length of the vector. This does not perform an
|
|
|
|
/// expensive square root operation like in the `length` method and can
|
|
|
|
/// therefore be more efficient for comparing the lengths of two vectors.
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn length2(self) -> Self::Scalar {
|
2013-09-03 03:54:03 +00:00
|
|
|
self.dot(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The norm of the vector.
|
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn length(self) -> Self::Scalar {
|
2015-11-03 04:40:52 +00:00
|
|
|
// Not sure why these annotations are needed
|
2015-11-03 04:23:22 +00:00
|
|
|
<<Self as Vector>::Scalar as ::rust_num::Float>::sqrt(self.dot(self))
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:43:51 +00:00
|
|
|
/// The angle between the vector and `other`, in radians.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn angle(self, other: Self) -> Rad<Self::Scalar>;
|
2013-09-03 03:54:03 +00:00
|
|
|
|
|
|
|
/// Returns a vector with the same direction, but with a `length` (or
|
|
|
|
/// `norm`) of `1`.
|
|
|
|
#[inline]
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn normalize(self) -> Self {
|
2015-11-03 04:23:22 +00:00
|
|
|
self.normalize_to(Self::Scalar::one())
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector with the same direction and a given `length`.
|
|
|
|
#[inline]
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn normalize_to(self, length: Self::Scalar) -> Self {
|
2013-09-03 03:54:03 +00:00
|
|
|
self.mul_s(length / self.length())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the result of linarly interpolating the length of the vector
|
2013-09-03 12:12:54 +00:00
|
|
|
/// towards the length of `other` by the specified amount.
|
2013-09-03 03:54:03 +00:00
|
|
|
#[inline]
|
2015-02-22 21:33:16 +00:00
|
|
|
#[must_use]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn lerp(self, other: Self, amount: Self::Scalar) -> Self {
|
|
|
|
self.add_v(other.sub_v(self).mul_s(amount))
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
2013-09-03 12:12:54 +00:00
|
|
|
|
|
|
|
/// Normalises the vector to a length of `1`.
|
|
|
|
#[inline]
|
|
|
|
fn normalize_self(&mut self) {
|
2015-11-03 04:40:52 +00:00
|
|
|
// Not sure why these annotations are needed
|
2015-11-03 04:23:22 +00:00
|
|
|
let rlen = <<Self as Vector>::Scalar as ::rust_num::Float>::recip(self.length());
|
2013-09-03 12:12:54 +00:00
|
|
|
self.mul_self_s(rlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalizes the vector to `length`.
|
|
|
|
#[inline]
|
2015-11-03 04:23:22 +00:00
|
|
|
fn normalize_self_to(&mut self, length: Self::Scalar) {
|
2013-09-03 12:12:54 +00:00
|
|
|
let n = length / self.length();
|
|
|
|
self.mul_self_s(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Linearly interpolates the length of the vector towards the length of
|
|
|
|
/// `other` by the specified amount.
|
2015-11-09 09:12:04 +00:00
|
|
|
fn lerp_self(&mut self, other: Self, amount: Self::Scalar) {
|
|
|
|
let v = other.sub_v(*self).mul_s(amount);
|
|
|
|
self.add_self_v(v);
|
2013-09-03 12:12:54 +00:00
|
|
|
}
|
2013-09-03 03:54:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat> EuclideanVector for Vector2<S> {
|
2013-09-03 03:54:03 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn angle(self, other: Vector2<S>) -> Rad<S> {
|
2013-09-03 03:54:03 +00:00
|
|
|
atan2(self.perp_dot(other), self.dot(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat> EuclideanVector for Vector3<S> {
|
2013-09-03 03:54:03 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn angle(self, other: Vector3<S>) -> Rad<S> {
|
2013-09-03 03:54:03 +00:00
|
|
|
atan2(self.cross(other).length(), self.dot(other))
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 07:33:33 +00:00
|
|
|
|
2015-11-03 04:23:22 +00:00
|
|
|
impl<S: BaseFloat> EuclideanVector for Vector4<S> {
|
2013-09-06 06:39:15 +00:00
|
|
|
#[inline]
|
2015-11-09 09:12:04 +00:00
|
|
|
fn angle(self, other: Vector4<S>) -> Rad<S> {
|
2013-09-06 06:39:15 +00:00
|
|
|
acos(self.dot(other) / (self.length() * other.length()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:19:32 +00:00
|
|
|
impl<S: BaseNum> fmt::Debug for Vector2<S> {
|
2014-02-25 08:56:22 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-09 22:06:45 +00:00
|
|
|
write!(f, "[{:?}, {:?}]", self.x, self.y)
|
2013-09-03 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:19:32 +00:00
|
|
|
impl<S: BaseNum> fmt::Debug for Vector3<S> {
|
2014-02-25 08:56:22 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-09 22:06:45 +00:00
|
|
|
write!(f, "[{:?}, {:?}, {:?}]", self.x, self.y, self.z)
|
2013-09-03 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:19:32 +00:00
|
|
|
impl<S: BaseNum> fmt::Debug for Vector4<S> {
|
2014-02-25 08:56:22 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-09 22:06:45 +00:00
|
|
|
write!(f, "[{:?}, {:?}, {:?}, {:?}]", self.x, self.y, self.z, self.w)
|
2013-09-03 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-27 03:01:14 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
mod vector2 {
|
|
|
|
use vector::*;
|
|
|
|
|
|
|
|
const VECTOR2: Vector2<i32> = Vector2 { x: 1, y: 2 };
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
|
|
|
assert_eq!(VECTOR2[0], VECTOR2.x);
|
|
|
|
assert_eq!(VECTOR2[1], VECTOR2.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_mut() {
|
|
|
|
let mut v = VECTOR2;
|
|
|
|
*&mut v[0] = 0;
|
|
|
|
assert_eq!(v, [0, 2].into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_index_out_of_bounds() {
|
|
|
|
VECTOR2[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_range() {
|
|
|
|
assert_eq!(&VECTOR2[..0], &[]);
|
|
|
|
assert_eq!(&VECTOR2[..1], &[1]);
|
|
|
|
assert_eq!(VECTOR2[..0].len(), 0);
|
|
|
|
assert_eq!(VECTOR2[..1].len(), 1);
|
|
|
|
assert_eq!(&VECTOR2[2..], &[]);
|
|
|
|
assert_eq!(&VECTOR2[1..], &[2]);
|
|
|
|
assert_eq!(VECTOR2[2..].len(), 0);
|
|
|
|
assert_eq!(VECTOR2[1..].len(), 1);
|
|
|
|
assert_eq!(&VECTOR2[..], &[1, 2]);
|
|
|
|
assert_eq!(VECTOR2[..].len(), 2);
|
|
|
|
}
|
2015-09-27 07:20:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into() {
|
|
|
|
let v = VECTOR2;
|
|
|
|
{
|
|
|
|
let v: [i32; 2] = v.into();
|
|
|
|
assert_eq!(v, [1, 2]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: (i32, i32) = v.into();
|
|
|
|
assert_eq!(v, (1, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_ref() {
|
|
|
|
let v = VECTOR2;
|
|
|
|
{
|
|
|
|
let v: &[i32; 2] = v.as_ref();
|
|
|
|
assert_eq!(v, &[1, 2]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &(i32, i32) = v.as_ref();
|
|
|
|
assert_eq!(v, &(1, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_mut() {
|
|
|
|
let mut v = VECTOR2;
|
|
|
|
{
|
|
|
|
let v: &mut [i32; 2] = v.as_mut();
|
|
|
|
assert_eq!(v, &mut [1, 2]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &mut (i32, i32) = v.as_mut();
|
|
|
|
assert_eq!(v, &mut (1, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
assert_eq!(Vector2::from([1, 2]), VECTOR2);
|
|
|
|
{
|
|
|
|
let v = &[1, 2];
|
|
|
|
let v: &Vector2<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut [1, 2];
|
|
|
|
let v: &mut Vector2<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR2);
|
|
|
|
}
|
|
|
|
assert_eq!(Vector2::from((1, 2)), VECTOR2);
|
|
|
|
{
|
|
|
|
let v = &(1, 2);
|
|
|
|
let v: &Vector2<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut (1, 2);
|
|
|
|
let v: &mut Vector2<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR2);
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 03:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod vector3 {
|
|
|
|
use vector::*;
|
|
|
|
|
|
|
|
const VECTOR3: Vector3<i32> = Vector3 { x: 1, y: 2, z: 3 };
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
|
|
|
assert_eq!(VECTOR3[0], VECTOR3.x);
|
|
|
|
assert_eq!(VECTOR3[1], VECTOR3.y);
|
|
|
|
assert_eq!(VECTOR3[2], VECTOR3.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_mut() {
|
|
|
|
let mut v = VECTOR3;
|
|
|
|
*&mut v[1] = 0;
|
|
|
|
assert_eq!(v, [1, 0, 3].into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_index_out_of_bounds() {
|
|
|
|
VECTOR3[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_range() {
|
|
|
|
assert_eq!(&VECTOR3[..1], &[1]);
|
|
|
|
assert_eq!(&VECTOR3[..2], &[1, 2]);
|
|
|
|
assert_eq!(VECTOR3[..1].len(), 1);
|
|
|
|
assert_eq!(VECTOR3[..2].len(), 2);
|
|
|
|
assert_eq!(&VECTOR3[2..], &[3]);
|
|
|
|
assert_eq!(&VECTOR3[1..], &[2, 3]);
|
|
|
|
assert_eq!(VECTOR3[2..].len(), 1);
|
|
|
|
assert_eq!(VECTOR3[1..].len(), 2);
|
|
|
|
assert_eq!(&VECTOR3[..], &[1, 2, 3]);
|
|
|
|
assert_eq!(VECTOR3[..].len(), 3);
|
|
|
|
}
|
2015-09-27 07:20:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into() {
|
|
|
|
let v = VECTOR3;
|
|
|
|
{
|
|
|
|
let v: [i32; 3] = v.into();
|
|
|
|
assert_eq!(v, [1, 2, 3]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: (i32, i32, i32) = v.into();
|
|
|
|
assert_eq!(v, (1, 2, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_ref() {
|
|
|
|
let v = VECTOR3;
|
|
|
|
{
|
|
|
|
let v: &[i32; 3] = v.as_ref();
|
|
|
|
assert_eq!(v, &[1, 2, 3]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &(i32, i32, i32) = v.as_ref();
|
|
|
|
assert_eq!(v, &(1, 2, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_mut() {
|
|
|
|
let mut v = VECTOR3;
|
|
|
|
{
|
|
|
|
let v: &mut [i32; 3] = v.as_mut();
|
|
|
|
assert_eq!(v, &mut [1, 2, 3]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &mut (i32, i32, i32) = v.as_mut();
|
|
|
|
assert_eq!(v, &mut (1, 2, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
assert_eq!(Vector3::from([1, 2, 3]), VECTOR3);
|
|
|
|
{
|
|
|
|
let v = &[1, 2, 3];
|
|
|
|
let v: &Vector3<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR3);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut [1, 2, 3];
|
|
|
|
let v: &mut Vector3<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR3);
|
|
|
|
}
|
|
|
|
assert_eq!(Vector3::from((1, 2, 3)), VECTOR3);
|
|
|
|
{
|
|
|
|
let v = &(1, 2, 3);
|
|
|
|
let v: &Vector3<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR3);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut (1, 2, 3);
|
|
|
|
let v: &mut Vector3<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR3);
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 03:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod vector4 {
|
|
|
|
use vector::*;
|
|
|
|
|
|
|
|
const VECTOR4: Vector4<i32> = Vector4 { x: 1, y: 2, z: 3, w: 4 };
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
|
|
|
assert_eq!(VECTOR4[0], VECTOR4.x);
|
|
|
|
assert_eq!(VECTOR4[1], VECTOR4.y);
|
|
|
|
assert_eq!(VECTOR4[2], VECTOR4.z);
|
|
|
|
assert_eq!(VECTOR4[3], VECTOR4.w);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_mut() {
|
|
|
|
let mut v = VECTOR4;
|
|
|
|
*&mut v[2] = 0;
|
|
|
|
assert_eq!(v, [1, 2, 0, 4].into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_index_out_of_bounds() {
|
|
|
|
VECTOR4[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index_range() {
|
|
|
|
assert_eq!(&VECTOR4[..2], &[1, 2]);
|
|
|
|
assert_eq!(&VECTOR4[..3], &[1, 2, 3]);
|
|
|
|
assert_eq!(VECTOR4[..2].len(), 2);
|
|
|
|
assert_eq!(VECTOR4[..3].len(), 3);
|
|
|
|
assert_eq!(&VECTOR4[2..], &[3, 4]);
|
|
|
|
assert_eq!(&VECTOR4[1..], &[2, 3, 4]);
|
|
|
|
assert_eq!(VECTOR4[2..].len(), 2);
|
|
|
|
assert_eq!(VECTOR4[1..].len(), 3);
|
|
|
|
assert_eq!(&VECTOR4[..], &[1, 2, 3, 4]);
|
|
|
|
assert_eq!(VECTOR4[..].len(), 4);
|
|
|
|
}
|
2015-09-27 07:20:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into() {
|
|
|
|
let v = VECTOR4;
|
|
|
|
{
|
|
|
|
let v: [i32; 4] = v.into();
|
|
|
|
assert_eq!(v, [1, 2, 3, 4]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: (i32, i32, i32, i32) = v.into();
|
|
|
|
assert_eq!(v, (1, 2, 3, 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_ref() {
|
|
|
|
let v = VECTOR4;
|
|
|
|
{
|
|
|
|
let v: &[i32; 4] = v.as_ref();
|
|
|
|
assert_eq!(v, &[1, 2, 3, 4]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &(i32, i32, i32, i32) = v.as_ref();
|
|
|
|
assert_eq!(v, &(1, 2, 3, 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_mut() {
|
|
|
|
let mut v = VECTOR4;
|
|
|
|
{
|
|
|
|
let v: &mut[i32; 4] = v.as_mut();
|
|
|
|
assert_eq!(v, &mut [1, 2, 3, 4]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v: &mut(i32, i32, i32, i32) = v.as_mut();
|
|
|
|
assert_eq!(v, &mut (1, 2, 3, 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
assert_eq!(Vector4::from([1, 2, 3, 4]), VECTOR4);
|
|
|
|
{
|
|
|
|
let v = &[1, 2, 3, 4];
|
|
|
|
let v: &Vector4<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR4);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut [1, 2, 3, 4];
|
|
|
|
let v: &mut Vector4<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR4);
|
|
|
|
}
|
|
|
|
assert_eq!(Vector4::from((1, 2, 3, 4)), VECTOR4);
|
|
|
|
{
|
|
|
|
let v = &(1, 2, 3, 4);
|
|
|
|
let v: &Vector4<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR4);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let v = &mut (1, 2, 3, 4);
|
|
|
|
let v: &mut Vector4<_> = From::from(v);
|
|
|
|
assert_eq!(v, &VECTOR4);
|
|
|
|
}
|
|
|
|
}
|
2015-09-27 03:01:14 +00:00
|
|
|
}
|
|
|
|
}
|