1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use super::UnknownUnit;
11 use crate::approxeq::ApproxEq;
12 use crate::approxord::{max, min};
13 use crate::length::Length;
15 use crate::point::{point2, point3, Point2D, Point3D};
16 use crate::scale::Scale;
17 use crate::size::{size2, size3, Size2D, Size3D};
18 use crate::transform2d::Transform2D;
19 use crate::transform3d::Transform3D;
20 use crate::trig::Trig;
22 use core::cmp::{Eq, PartialEq};
26 use core::marker::PhantomData;
27 use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
28 #[cfg(feature = "mint")]
30 use num_traits::real::Real;
31 use num_traits::{Float, NumCast, Signed};
32 #[cfg(feature = "serde")]
35 #[cfg(feature = "bytemuck")]
36 use bytemuck::{Zeroable, Pod};
38 /// A 2d Vector tagged with a unit.
40 pub struct Vector2D<T, U> {
41 /// The `x` (traditionally, horizontal) coordinate.
43 /// The `y` (traditionally, vertical) coordinate.
46 pub _unit: PhantomData<U>,
49 mint_vec!(Vector2D[x, y] = Vector2);
51 impl<T: Copy, U> Copy for Vector2D<T, U> {}
53 impl<T: Clone, U> Clone for Vector2D<T, U> {
54 fn clone(&self) -> Self {
63 #[cfg(feature = "serde")]
64 impl<'de, T, U> serde::Deserialize<'de> for Vector2D<T, U>
66 T: serde::Deserialize<'de>,
68 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70 D: serde::Deserializer<'de>,
72 let (x, y) = serde::Deserialize::deserialize(deserializer)?;
81 #[cfg(feature = "serde")]
82 impl<T, U> serde::Serialize for Vector2D<T, U>
86 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
90 (&self.x, &self.y).serialize(serializer)
94 #[cfg(feature = "arbitrary")]
95 impl<'a, T, U> arbitrary::Arbitrary<'a> for Vector2D<T, U>
97 T: arbitrary::Arbitrary<'a>,
99 fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self>
101 let (x, y) = arbitrary::Arbitrary::arbitrary(u)?;
110 #[cfg(feature = "bytemuck")]
111 unsafe impl<T: Zeroable, U> Zeroable for Vector2D<T, U> {}
113 #[cfg(feature = "bytemuck")]
114 unsafe impl<T: Pod, U: 'static> Pod for Vector2D<T, U> {}
116 impl<T: Eq, U> Eq for Vector2D<T, U> {}
118 impl<T: PartialEq, U> PartialEq for Vector2D<T, U> {
119 fn eq(&self, other: &Self) -> bool {
120 self.x == other.x && self.y == other.y
124 impl<T: Hash, U> Hash for Vector2D<T, U> {
125 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
131 impl<T: Zero, U> Zero for Vector2D<T, U> {
132 /// Constructor, setting all components to zero.
135 Vector2D::new(Zero::zero(), Zero::zero())
139 impl<T: fmt::Debug, U> fmt::Debug for Vector2D<T, U> {
140 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141 f.debug_tuple("").field(&self.x).field(&self.y).finish()
145 impl<T: Default, U> Default for Vector2D<T, U> {
146 fn default() -> Self {
147 Vector2D::new(Default::default(), Default::default())
151 impl<T, U> Vector2D<T, U> {
152 /// Constructor, setting all components to zero.
154 pub fn zero() -> Self
158 Vector2D::new(Zero::zero(), Zero::zero())
161 /// Constructor, setting all components to one.
167 Vector2D::new(One::one(), One::one())
170 /// Constructor taking scalar values directly.
172 pub const fn new(x: T, y: T) -> Self {
180 /// Constructor setting all components to the same value.
182 pub fn splat(v: T) -> Self
193 /// Constructor taking angle and length
194 pub fn from_angle_and_length(angle: Angle<T>, length: T) -> Self
196 T: Trig + Mul<Output = T> + Copy,
198 vec2(length * angle.radians.cos(), length * angle.radians.sin())
201 /// Constructor taking properly Lengths instead of scalar values.
203 pub fn from_lengths(x: Length<T, U>, y: Length<T, U>) -> Self {
207 /// Tag a unit-less value with units.
209 pub fn from_untyped(p: Vector2D<T, UnknownUnit>) -> Self {
213 /// Computes the vector with absolute values of each component.
218 /// # use std::{i32, f32};
219 /// # use euclid::vec2;
222 /// assert_eq!(vec2::<_, U>(-1, 2).abs(), vec2(1, 2));
224 /// let vec = vec2::<_, U>(f32::NAN, -f32::MAX).abs();
225 /// assert!(vec.x.is_nan());
226 /// assert_eq!(vec.y, f32::MAX);
231 /// The behavior for each component follows the scalar type's implementation of
232 /// `num_traits::Signed::abs`.
233 pub fn abs(self) -> Self
237 vec2(self.x.abs(), self.y.abs())
242 pub fn dot(self, other: Self) -> T
244 T: Add<Output = T> + Mul<Output = T>,
246 self.x * other.x + self.y * other.y
249 /// Returns the norm of the cross product [self.x, self.y, 0] x [other.x, other.y, 0].
251 pub fn cross(self, other: Self) -> T
253 T: Sub<Output = T> + Mul<Output = T>,
255 self.x * other.y - self.y * other.x
258 /// Returns the component-wise multiplication of the two vectors.
260 pub fn component_mul(self, other: Self) -> Self
264 vec2(self.x * other.x, self.y * other.y)
267 /// Returns the component-wise division of the two vectors.
269 pub fn component_div(self, other: Self) -> Self
273 vec2(self.x / other.x, self.y / other.y)
277 impl<T: Copy, U> Vector2D<T, U> {
278 /// Create a 3d vector from this one, using the specified z value.
280 pub fn extend(self, z: T) -> Vector3D<T, U> {
281 vec3(self.x, self.y, z)
284 /// Cast this vector into a point.
286 /// Equivalent to adding this vector to the origin.
288 pub fn to_point(self) -> Point2D<T, U> {
298 pub fn yx(self) -> Self {
302 /// Cast this vector into a size.
304 pub fn to_size(self) -> Size2D<T, U> {
305 size2(self.x, self.y)
308 /// Drop the units, preserving only the numeric value.
310 pub fn to_untyped(self) -> Vector2D<T, UnknownUnit> {
316 pub fn cast_unit<V>(self) -> Vector2D<T, V> {
320 /// Cast into an array with x and y.
322 pub fn to_array(self) -> [T; 2] {
326 /// Cast into a tuple with x and y.
328 pub fn to_tuple(self) -> (T, T) {
332 /// Convert into a 3d vector with `z` coordinate equals to `T::zero()`.
334 pub fn to_3d(self) -> Vector3D<T, U>
338 vec3(self.x, self.y, Zero::zero())
341 /// Rounds each component to the nearest integer value.
343 /// This behavior is preserved for negative values (unlike the basic cast).
346 /// # use euclid::vec2;
349 /// assert_eq!(vec2::<_, Mm>(-0.1, -0.8).round(), vec2::<_, Mm>(0.0, -1.0))
353 pub fn round(self) -> Self
357 vec2(self.x.round(), self.y.round())
360 /// Rounds each component to the smallest integer equal or greater than the original value.
362 /// This behavior is preserved for negative values (unlike the basic cast).
365 /// # use euclid::vec2;
368 /// assert_eq!(vec2::<_, Mm>(-0.1, -0.8).ceil(), vec2::<_, Mm>(0.0, 0.0))
372 pub fn ceil(self) -> Self
376 vec2(self.x.ceil(), self.y.ceil())
379 /// Rounds each component to the biggest integer equal or lower than the original value.
381 /// This behavior is preserved for negative values (unlike the basic cast).
384 /// # use euclid::vec2;
387 /// assert_eq!(vec2::<_, Mm>(-0.1, -0.8).floor(), vec2::<_, Mm>(-1.0, -1.0))
391 pub fn floor(self) -> Self
395 vec2(self.x.floor(), self.y.floor())
398 /// Returns the signed angle between this vector and the x axis.
399 /// Positive values counted counterclockwise, where 0 is `+x` axis, `PI/2`
402 /// The returned angle is between -PI and PI.
403 pub fn angle_from_x_axis(self) -> Angle<T>
407 Angle::radians(Trig::fast_atan2(self.y, self.x))
410 /// Creates translation by this vector in vector units.
412 pub fn to_transform(self) -> Transform2D<T, U, U>
416 Transform2D::translation(self.x, self.y)
420 impl<T, U> Vector2D<T, U>
422 T: Copy + Mul<T, Output = T> + Add<T, Output = T>,
424 /// Returns the vector's length squared.
426 pub fn square_length(self) -> T {
427 self.x * self.x + self.y * self.y
430 /// Returns this vector projected onto another one.
432 /// Projecting onto a nil vector will cause a division by zero.
434 pub fn project_onto_vector(self, onto: Self) -> Self
436 T: Sub<T, Output = T> + Div<T, Output = T>,
438 onto * (self.dot(onto) / onto.square_length())
441 /// Returns the signed angle between this vector and another vector.
443 /// The returned angle is between -PI and PI.
444 pub fn angle_to(self, other: Self) -> Angle<T>
446 T: Sub<Output = T> + Trig,
448 Angle::radians(Trig::fast_atan2(self.cross(other), self.dot(other)))
452 impl<T: Float, U> Vector2D<T, U> {
453 /// Return the normalized vector even if the length is larger than the max value of Float.
456 pub fn robust_normalize(self) -> Self {
457 let length = self.length();
458 if length.is_infinite() {
459 let scaled = self / T::max_value();
460 scaled / scaled.length()
466 /// Returns true if all members are finite.
468 pub fn is_finite(self) -> bool {
469 self.x.is_finite() && self.y.is_finite()
473 impl<T: Real, U> Vector2D<T, U> {
474 /// Returns the vector length.
476 pub fn length(self) -> T {
477 self.square_length().sqrt()
480 /// Returns the vector with length of one unit.
483 pub fn normalize(self) -> Self {
487 /// Returns the vector with length of one unit.
489 /// Unlike [`Vector2D::normalize`](#method.normalize), this returns None in the case that the
490 /// length of the vector is zero.
493 pub fn try_normalize(self) -> Option<Self> {
494 let len = self.length();
495 if len == T::zero() {
502 /// Return this vector scaled to fit the provided length.
504 pub fn with_length(self, length: T) -> Self {
505 self.normalize() * length
508 /// Return this vector capped to a maximum length.
510 pub fn with_max_length(self, max_length: T) -> Self {
511 let square_length = self.square_length();
512 if square_length > max_length * max_length {
513 return self * (max_length / square_length.sqrt());
519 /// Return this vector with a minimum length applied.
521 pub fn with_min_length(self, min_length: T) -> Self {
522 let square_length = self.square_length();
523 if square_length < min_length * min_length {
524 return self * (min_length / square_length.sqrt());
530 /// Return this vector with minimum and maximum lengths applied.
532 pub fn clamp_length(self, min: T, max: T) -> Self {
533 debug_assert!(min <= max);
534 self.with_min_length(min).with_max_length(max)
538 impl<T, U> Vector2D<T, U>
540 T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
542 /// Linearly interpolate each component between this vector and another vector.
547 /// use euclid::vec2;
548 /// use euclid::default::Vector2D;
550 /// let from: Vector2D<_> = vec2(0.0, 10.0);
551 /// let to: Vector2D<_> = vec2(8.0, -4.0);
553 /// assert_eq!(from.lerp(to, -1.0), vec2(-8.0, 24.0));
554 /// assert_eq!(from.lerp(to, 0.0), vec2( 0.0, 10.0));
555 /// assert_eq!(from.lerp(to, 0.5), vec2( 4.0, 3.0));
556 /// assert_eq!(from.lerp(to, 1.0), vec2( 8.0, -4.0));
557 /// assert_eq!(from.lerp(to, 2.0), vec2(16.0, -18.0));
560 pub fn lerp(self, other: Self, t: T) -> Self {
561 let one_t = T::one() - t;
562 self * one_t + other * t
565 /// Returns a reflection vector using an incident ray and a surface normal.
567 pub fn reflect(self, normal: Self) -> Self {
568 let two = T::one() + T::one();
569 self - normal * two * self.dot(normal)
573 impl<T: PartialOrd, U> Vector2D<T, U> {
574 /// Returns the vector each component of which are minimum of this vector and another.
576 pub fn min(self, other: Self) -> Self {
577 vec2(min(self.x, other.x), min(self.y, other.y))
580 /// Returns the vector each component of which are maximum of this vector and another.
582 pub fn max(self, other: Self) -> Self {
583 vec2(max(self.x, other.x), max(self.y, other.y))
586 /// Returns the vector each component of which is clamped by corresponding
587 /// components of `start` and `end`.
589 /// Shortcut for `self.max(start).min(end)`.
591 pub fn clamp(self, start: Self, end: Self) -> Self
595 self.max(start).min(end)
598 /// Returns vector with results of "greater than" operation on each component.
600 pub fn greater_than(self, other: Self) -> BoolVector2D {
607 /// Returns vector with results of "lower than" operation on each component.
609 pub fn lower_than(self, other: Self) -> BoolVector2D {
617 impl<T: PartialEq, U> Vector2D<T, U> {
618 /// Returns vector with results of "equal" operation on each component.
620 pub fn equal(self, other: Self) -> BoolVector2D {
622 x: self.x == other.x,
623 y: self.y == other.y,
627 /// Returns vector with results of "not equal" operation on each component.
629 pub fn not_equal(self, other: Self) -> BoolVector2D {
631 x: self.x != other.x,
632 y: self.y != other.y,
637 impl<T: NumCast + Copy, U> Vector2D<T, U> {
638 /// Cast from one numeric representation to another, preserving the units.
640 /// When casting from floating vector to integer coordinates, the decimals are truncated
641 /// as one would expect from a simple cast, but this behavior does not always make sense
642 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
644 pub fn cast<NewT: NumCast>(self) -> Vector2D<NewT, U> {
645 self.try_cast().unwrap()
648 /// Fallible cast from one numeric representation to another, preserving the units.
650 /// When casting from floating vector to integer coordinates, the decimals are truncated
651 /// as one would expect from a simple cast, but this behavior does not always make sense
652 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
653 pub fn try_cast<NewT: NumCast>(self) -> Option<Vector2D<NewT, U>> {
654 match (NumCast::from(self.x), NumCast::from(self.y)) {
655 (Some(x), Some(y)) => Some(Vector2D::new(x, y)),
660 // Convenience functions for common casts.
662 /// Cast into an `f32` vector.
664 pub fn to_f32(self) -> Vector2D<f32, U> {
668 /// Cast into an `f64` vector.
670 pub fn to_f64(self) -> Vector2D<f64, U> {
674 /// Cast into an `usize` vector, truncating decimals if any.
676 /// When casting from floating vector vectors, it is worth considering whether
677 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
678 /// the desired conversion behavior.
680 pub fn to_usize(self) -> Vector2D<usize, U> {
684 /// Cast into an `u32` vector, truncating decimals if any.
686 /// When casting from floating vector vectors, it is worth considering whether
687 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
688 /// the desired conversion behavior.
690 pub fn to_u32(self) -> Vector2D<u32, U> {
694 /// Cast into an i32 vector, truncating decimals if any.
696 /// When casting from floating vector vectors, it is worth considering whether
697 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
698 /// the desired conversion behavior.
700 pub fn to_i32(self) -> Vector2D<i32, U> {
704 /// Cast into an i64 vector, truncating decimals if any.
706 /// When casting from floating vector vectors, it is worth considering whether
707 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
708 /// the desired conversion behavior.
710 pub fn to_i64(self) -> Vector2D<i64, U> {
715 impl<T: Neg, U> Neg for Vector2D<T, U> {
716 type Output = Vector2D<T::Output, U>;
719 fn neg(self) -> Self::Output {
720 vec2(-self.x, -self.y)
724 impl<T: Add, U> Add for Vector2D<T, U> {
725 type Output = Vector2D<T::Output, U>;
728 fn add(self, other: Self) -> Self::Output {
729 Vector2D::new(self.x + other.x, self.y + other.y)
733 impl<T: Add + Copy, U> Add<&Self> for Vector2D<T, U> {
734 type Output = Vector2D<T::Output, U>;
737 fn add(self, other: &Self) -> Self::Output {
738 Vector2D::new(self.x + other.x, self.y + other.y)
742 impl<T: Add<Output = T> + Zero, U> Sum for Vector2D<T, U> {
743 fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
744 iter.fold(Self::zero(), Add::add)
748 impl<'a, T: 'a + Add<Output = T> + Copy + Zero, U: 'a> Sum<&'a Self> for Vector2D<T, U> {
749 fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
750 iter.fold(Self::zero(), Add::add)
754 impl<T: Copy + Add<T, Output = T>, U> AddAssign for Vector2D<T, U> {
756 fn add_assign(&mut self, other: Self) {
757 *self = *self + other
761 impl<T: Sub, U> Sub for Vector2D<T, U> {
762 type Output = Vector2D<T::Output, U>;
765 fn sub(self, other: Self) -> Self::Output {
766 vec2(self.x - other.x, self.y - other.y)
770 impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector2D<T, U>> for Vector2D<T, U> {
772 fn sub_assign(&mut self, other: Self) {
773 *self = *self - other
777 impl<T: Copy + Mul, U> Mul<T> for Vector2D<T, U> {
778 type Output = Vector2D<T::Output, U>;
781 fn mul(self, scale: T) -> Self::Output {
782 vec2(self.x * scale, self.y * scale)
786 impl<T: Copy + Mul<T, Output = T>, U> MulAssign<T> for Vector2D<T, U> {
788 fn mul_assign(&mut self, scale: T) {
789 *self = *self * scale
793 impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Vector2D<T, U1> {
794 type Output = Vector2D<T::Output, U2>;
797 fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output {
798 vec2(self.x * scale.0, self.y * scale.0)
802 impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Vector2D<T, U> {
804 fn mul_assign(&mut self, scale: Scale<T, U, U>) {
810 impl<T: Copy + Div, U> Div<T> for Vector2D<T, U> {
811 type Output = Vector2D<T::Output, U>;
814 fn div(self, scale: T) -> Self::Output {
815 vec2(self.x / scale, self.y / scale)
819 impl<T: Copy + Div<T, Output = T>, U> DivAssign<T> for Vector2D<T, U> {
821 fn div_assign(&mut self, scale: T) {
822 *self = *self / scale
826 impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Vector2D<T, U2> {
827 type Output = Vector2D<T::Output, U1>;
830 fn div(self, scale: Scale<T, U1, U2>) -> Self::Output {
831 vec2(self.x / scale.0, self.y / scale.0)
835 impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Vector2D<T, U> {
837 fn div_assign(&mut self, scale: Scale<T, U, U>) {
843 impl<T: Round, U> Round for Vector2D<T, U> {
844 /// See [`Vector2D::round()`](#method.round)
846 fn round(self) -> Self {
851 impl<T: Ceil, U> Ceil for Vector2D<T, U> {
852 /// See [`Vector2D::ceil()`](#method.ceil)
854 fn ceil(self) -> Self {
859 impl<T: Floor, U> Floor for Vector2D<T, U> {
860 /// See [`Vector2D::floor()`](#method.floor)
862 fn floor(self) -> Self {
867 impl<T: ApproxEq<T>, U> ApproxEq<Vector2D<T, U>> for Vector2D<T, U> {
869 fn approx_epsilon() -> Self {
870 vec2(T::approx_epsilon(), T::approx_epsilon())
874 fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool {
875 self.x.approx_eq_eps(&other.x, &eps.x) && self.y.approx_eq_eps(&other.y, &eps.y)
879 impl<T, U> Into<[T; 2]> for Vector2D<T, U> {
880 fn into(self) -> [T; 2] {
885 impl<T, U> From<[T; 2]> for Vector2D<T, U> {
886 fn from([x, y]: [T; 2]) -> Self {
891 impl<T, U> Into<(T, T)> for Vector2D<T, U> {
892 fn into(self) -> (T, T) {
897 impl<T, U> From<(T, T)> for Vector2D<T, U> {
898 fn from(tuple: (T, T)) -> Self {
899 vec2(tuple.0, tuple.1)
903 impl<T, U> From<Size2D<T, U>> for Vector2D<T, U> {
904 fn from(size: Size2D<T, U>) -> Self {
905 vec2(size.width, size.height)
909 /// A 3d Vector tagged with a unit.
911 pub struct Vector3D<T, U> {
912 /// The `x` (traditionally, horizontal) coordinate.
914 /// The `y` (traditionally, vertical) coordinate.
916 /// The `z` (traditionally, depth) coordinate.
919 pub _unit: PhantomData<U>,
922 mint_vec!(Vector3D[x, y, z] = Vector3);
924 impl<T: Copy, U> Copy for Vector3D<T, U> {}
926 impl<T: Clone, U> Clone for Vector3D<T, U> {
927 fn clone(&self) -> Self {
937 #[cfg(feature = "serde")]
938 impl<'de, T, U> serde::Deserialize<'de> for Vector3D<T, U>
940 T: serde::Deserialize<'de>,
942 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
944 D: serde::Deserializer<'de>,
946 let (x, y, z) = serde::Deserialize::deserialize(deserializer)?;
956 #[cfg(feature = "serde")]
957 impl<T, U> serde::Serialize for Vector3D<T, U>
961 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
963 S: serde::Serializer,
965 (&self.x, &self.y, &self.z).serialize(serializer)
969 #[cfg(feature = "bytemuck")]
970 unsafe impl<T: Zeroable, U> Zeroable for Vector3D<T, U> {}
972 #[cfg(feature = "bytemuck")]
973 unsafe impl<T: Pod, U: 'static> Pod for Vector3D<T, U> {}
975 impl<T: Eq, U> Eq for Vector3D<T, U> {}
977 impl<T: PartialEq, U> PartialEq for Vector3D<T, U> {
978 fn eq(&self, other: &Self) -> bool {
979 self.x == other.x && self.y == other.y && self.z == other.z
983 impl<T: Hash, U> Hash for Vector3D<T, U> {
984 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
991 impl<T: Zero, U> Zero for Vector3D<T, U> {
992 /// Constructor, setting all components to zero.
995 vec3(Zero::zero(), Zero::zero(), Zero::zero())
999 impl<T: fmt::Debug, U> fmt::Debug for Vector3D<T, U> {
1000 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1009 impl<T: Default, U> Default for Vector3D<T, U> {
1010 fn default() -> Self {
1011 Vector3D::new(Default::default(), Default::default(), Default::default())
1015 impl<T, U> Vector3D<T, U> {
1016 /// Constructor, setting all components to zero.
1018 pub fn zero() -> Self
1022 vec3(Zero::zero(), Zero::zero(), Zero::zero())
1025 /// Constructor, setting all components to one.
1027 pub fn one() -> Self
1031 vec3(One::one(), One::one(), One::one())
1034 /// Constructor taking scalar values directly.
1036 pub const fn new(x: T, y: T, z: T) -> Self {
1044 /// Constructor setting all components to the same value.
1046 pub fn splat(v: T) -> Self
1058 /// Constructor taking properly Lengths instead of scalar values.
1060 pub fn from_lengths(x: Length<T, U>, y: Length<T, U>, z: Length<T, U>) -> Vector3D<T, U> {
1064 /// Tag a unitless value with units.
1066 pub fn from_untyped(p: Vector3D<T, UnknownUnit>) -> Self {
1070 /// Computes the vector with absolute values of each component.
1075 /// # use std::{i32, f32};
1076 /// # use euclid::vec3;
1079 /// assert_eq!(vec3::<_, U>(-1, 0, 2).abs(), vec3(1, 0, 2));
1081 /// let vec = vec3::<_, U>(f32::NAN, 0.0, -f32::MAX).abs();
1082 /// assert!(vec.x.is_nan());
1083 /// assert_eq!(vec.y, 0.0);
1084 /// assert_eq!(vec.z, f32::MAX);
1089 /// The behavior for each component follows the scalar type's implementation of
1090 /// `num_traits::Signed::abs`.
1091 pub fn abs(self) -> Self
1095 vec3(self.x.abs(), self.y.abs(), self.z.abs())
1100 pub fn dot(self, other: Self) -> T
1102 T: Add<Output = T> + Mul<Output = T>,
1104 self.x * other.x + self.y * other.y + self.z * other.z
1108 impl<T: Copy, U> Vector3D<T, U> {
1111 pub fn cross(self, other: Self) -> Self
1113 T: Sub<Output = T> + Mul<Output = T>,
1116 self.y * other.z - self.z * other.y,
1117 self.z * other.x - self.x * other.z,
1118 self.x * other.y - self.y * other.x,
1122 /// Returns the component-wise multiplication of the two vectors.
1124 pub fn component_mul(self, other: Self) -> Self
1128 vec3(self.x * other.x, self.y * other.y, self.z * other.z)
1131 /// Returns the component-wise division of the two vectors.
1133 pub fn component_div(self, other: Self) -> Self
1137 vec3(self.x / other.x, self.y / other.y, self.z / other.z)
1140 /// Cast this vector into a point.
1142 /// Equivalent to adding this vector to the origin.
1144 pub fn to_point(self) -> Point3D<T, U> {
1145 point3(self.x, self.y, self.z)
1148 /// Returns a 2d vector using this vector's x and y coordinates
1150 pub fn xy(self) -> Vector2D<T, U> {
1151 vec2(self.x, self.y)
1154 /// Returns a 2d vector using this vector's x and z coordinates
1156 pub fn xz(self) -> Vector2D<T, U> {
1157 vec2(self.x, self.z)
1160 /// Returns a 2d vector using this vector's x and z coordinates
1162 pub fn yz(self) -> Vector2D<T, U> {
1163 vec2(self.y, self.z)
1166 /// Cast into an array with x, y and z.
1168 pub fn to_array(self) -> [T; 3] {
1169 [self.x, self.y, self.z]
1172 /// Cast into an array with x, y, z and 0.
1174 pub fn to_array_4d(self) -> [T; 4]
1178 [self.x, self.y, self.z, Zero::zero()]
1181 /// Cast into a tuple with x, y and z.
1183 pub fn to_tuple(self) -> (T, T, T) {
1184 (self.x, self.y, self.z)
1187 /// Cast into a tuple with x, y, z and 0.
1189 pub fn to_tuple_4d(self) -> (T, T, T, T)
1193 (self.x, self.y, self.z, Zero::zero())
1196 /// Drop the units, preserving only the numeric value.
1198 pub fn to_untyped(self) -> Vector3D<T, UnknownUnit> {
1199 vec3(self.x, self.y, self.z)
1204 pub fn cast_unit<V>(self) -> Vector3D<T, V> {
1205 vec3(self.x, self.y, self.z)
1208 /// Convert into a 2d vector.
1210 pub fn to_2d(self) -> Vector2D<T, U> {
1214 /// Rounds each component to the nearest integer value.
1216 /// This behavior is preserved for negative values (unlike the basic cast).
1219 /// # use euclid::vec3;
1222 /// assert_eq!(vec3::<_, Mm>(-0.1, -0.8, 0.4).round(), vec3::<_, Mm>(0.0, -1.0, 0.0))
1226 pub fn round(self) -> Self
1230 vec3(self.x.round(), self.y.round(), self.z.round())
1233 /// Rounds each component to the smallest integer equal or greater than the original value.
1235 /// This behavior is preserved for negative values (unlike the basic cast).
1238 /// # use euclid::vec3;
1241 /// assert_eq!(vec3::<_, Mm>(-0.1, -0.8, 0.4).ceil(), vec3::<_, Mm>(0.0, 0.0, 1.0))
1245 pub fn ceil(self) -> Self
1249 vec3(self.x.ceil(), self.y.ceil(), self.z.ceil())
1252 /// Rounds each component to the biggest integer equal or lower than the original value.
1254 /// This behavior is preserved for negative values (unlike the basic cast).
1257 /// # use euclid::vec3;
1260 /// assert_eq!(vec3::<_, Mm>(-0.1, -0.8, 0.4).floor(), vec3::<_, Mm>(-1.0, -1.0, 0.0))
1264 pub fn floor(self) -> Self
1268 vec3(self.x.floor(), self.y.floor(), self.z.floor())
1271 /// Creates translation by this vector in vector units
1273 pub fn to_transform(self) -> Transform3D<T, U, U>
1277 Transform3D::translation(self.x, self.y, self.z)
1281 impl<T, U> Vector3D<T, U>
1283 T: Copy + Mul<T, Output = T> + Add<T, Output = T>,
1285 /// Returns the vector's length squared.
1287 pub fn square_length(self) -> T {
1288 self.x * self.x + self.y * self.y + self.z * self.z
1291 /// Returns this vector projected onto another one.
1293 /// Projecting onto a nil vector will cause a division by zero.
1295 pub fn project_onto_vector(self, onto: Self) -> Self
1297 T: Sub<T, Output = T> + Div<T, Output = T>,
1299 onto * (self.dot(onto) / onto.square_length())
1303 impl<T: Float, U> Vector3D<T, U> {
1304 /// Return the normalized vector even if the length is larger than the max value of Float.
1307 pub fn robust_normalize(self) -> Self {
1308 let length = self.length();
1309 if length.is_infinite() {
1310 let scaled = self / T::max_value();
1311 scaled / scaled.length()
1317 /// Returns true if all members are finite.
1319 pub fn is_finite(self) -> bool {
1320 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
1324 impl<T: Real, U> Vector3D<T, U> {
1325 /// Returns the positive angle between this vector and another vector.
1327 /// The returned angle is between 0 and PI.
1328 pub fn angle_to(self, other: Self) -> Angle<T>
1332 Angle::radians(Trig::fast_atan2(
1333 self.cross(other).length(),
1338 /// Returns the vector length.
1340 pub fn length(self) -> T {
1341 self.square_length().sqrt()
1344 /// Returns the vector with length of one unit
1347 pub fn normalize(self) -> Self {
1348 self / self.length()
1351 /// Returns the vector with length of one unit.
1353 /// Unlike [`Vector2D::normalize`](#method.normalize), this returns None in the case that the
1354 /// length of the vector is zero.
1357 pub fn try_normalize(self) -> Option<Self> {
1358 let len = self.length();
1359 if len == T::zero() {
1366 /// Return this vector capped to a maximum length.
1368 pub fn with_max_length(self, max_length: T) -> Self {
1369 let square_length = self.square_length();
1370 if square_length > max_length * max_length {
1371 return self * (max_length / square_length.sqrt());
1377 /// Return this vector with a minimum length applied.
1379 pub fn with_min_length(self, min_length: T) -> Self {
1380 let square_length = self.square_length();
1381 if square_length < min_length * min_length {
1382 return self * (min_length / square_length.sqrt());
1388 /// Return this vector with minimum and maximum lengths applied.
1390 pub fn clamp_length(self, min: T, max: T) -> Self {
1391 debug_assert!(min <= max);
1392 self.with_min_length(min).with_max_length(max)
1396 impl<T, U> Vector3D<T, U>
1398 T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
1400 /// Linearly interpolate each component between this vector and another vector.
1405 /// use euclid::vec3;
1406 /// use euclid::default::Vector3D;
1408 /// let from: Vector3D<_> = vec3(0.0, 10.0, -1.0);
1409 /// let to: Vector3D<_> = vec3(8.0, -4.0, 0.0);
1411 /// assert_eq!(from.lerp(to, -1.0), vec3(-8.0, 24.0, -2.0));
1412 /// assert_eq!(from.lerp(to, 0.0), vec3( 0.0, 10.0, -1.0));
1413 /// assert_eq!(from.lerp(to, 0.5), vec3( 4.0, 3.0, -0.5));
1414 /// assert_eq!(from.lerp(to, 1.0), vec3( 8.0, -4.0, 0.0));
1415 /// assert_eq!(from.lerp(to, 2.0), vec3(16.0, -18.0, 1.0));
1418 pub fn lerp(self, other: Self, t: T) -> Self {
1419 let one_t = T::one() - t;
1420 self * one_t + other * t
1423 /// Returns a reflection vector using an incident ray and a surface normal.
1425 pub fn reflect(self, normal: Self) -> Self {
1426 let two = T::one() + T::one();
1427 self - normal * two * self.dot(normal)
1431 impl<T: PartialOrd, U> Vector3D<T, U> {
1432 /// Returns the vector each component of which are minimum of this vector and another.
1434 pub fn min(self, other: Self) -> Self {
1436 min(self.x, other.x),
1437 min(self.y, other.y),
1438 min(self.z, other.z),
1442 /// Returns the vector each component of which are maximum of this vector and another.
1444 pub fn max(self, other: Self) -> Self {
1446 max(self.x, other.x),
1447 max(self.y, other.y),
1448 max(self.z, other.z),
1452 /// Returns the vector each component of which is clamped by corresponding
1453 /// components of `start` and `end`.
1455 /// Shortcut for `self.max(start).min(end)`.
1457 pub fn clamp(self, start: Self, end: Self) -> Self
1461 self.max(start).min(end)
1464 /// Returns vector with results of "greater than" operation on each component.
1466 pub fn greater_than(self, other: Self) -> BoolVector3D {
1468 x: self.x > other.x,
1469 y: self.y > other.y,
1470 z: self.z > other.z,
1474 /// Returns vector with results of "lower than" operation on each component.
1476 pub fn lower_than(self, other: Self) -> BoolVector3D {
1478 x: self.x < other.x,
1479 y: self.y < other.y,
1480 z: self.z < other.z,
1485 impl<T: PartialEq, U> Vector3D<T, U> {
1486 /// Returns vector with results of "equal" operation on each component.
1488 pub fn equal(self, other: Self) -> BoolVector3D {
1490 x: self.x == other.x,
1491 y: self.y == other.y,
1492 z: self.z == other.z,
1496 /// Returns vector with results of "not equal" operation on each component.
1498 pub fn not_equal(self, other: Self) -> BoolVector3D {
1500 x: self.x != other.x,
1501 y: self.y != other.y,
1502 z: self.z != other.z,
1507 impl<T: NumCast + Copy, U> Vector3D<T, U> {
1508 /// Cast from one numeric representation to another, preserving the units.
1510 /// When casting from floating vector to integer coordinates, the decimals are truncated
1511 /// as one would expect from a simple cast, but this behavior does not always make sense
1512 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
1514 pub fn cast<NewT: NumCast>(self) -> Vector3D<NewT, U> {
1515 self.try_cast().unwrap()
1518 /// Fallible cast from one numeric representation to another, preserving the units.
1520 /// When casting from floating vector to integer coordinates, the decimals are truncated
1521 /// as one would expect from a simple cast, but this behavior does not always make sense
1522 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
1523 pub fn try_cast<NewT: NumCast>(self) -> Option<Vector3D<NewT, U>> {
1525 NumCast::from(self.x),
1526 NumCast::from(self.y),
1527 NumCast::from(self.z),
1529 (Some(x), Some(y), Some(z)) => Some(vec3(x, y, z)),
1534 // Convenience functions for common casts.
1536 /// Cast into an `f32` vector.
1538 pub fn to_f32(self) -> Vector3D<f32, U> {
1542 /// Cast into an `f64` vector.
1544 pub fn to_f64(self) -> Vector3D<f64, U> {
1548 /// Cast into an `usize` vector, truncating decimals if any.
1550 /// When casting from floating vector vectors, it is worth considering whether
1551 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
1552 /// the desired conversion behavior.
1554 pub fn to_usize(self) -> Vector3D<usize, U> {
1558 /// Cast into an `u32` vector, truncating decimals if any.
1560 /// When casting from floating vector vectors, it is worth considering whether
1561 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
1562 /// the desired conversion behavior.
1564 pub fn to_u32(self) -> Vector3D<u32, U> {
1568 /// Cast into an `i32` vector, truncating decimals if any.
1570 /// When casting from floating vector vectors, it is worth considering whether
1571 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
1572 /// the desired conversion behavior.
1574 pub fn to_i32(self) -> Vector3D<i32, U> {
1578 /// Cast into an `i64` vector, truncating decimals if any.
1580 /// When casting from floating vector vectors, it is worth considering whether
1581 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
1582 /// the desired conversion behavior.
1584 pub fn to_i64(self) -> Vector3D<i64, U> {
1589 impl<T: Neg, U> Neg for Vector3D<T, U> {
1590 type Output = Vector3D<T::Output, U>;
1593 fn neg(self) -> Self::Output {
1594 vec3(-self.x, -self.y, -self.z)
1598 impl<T: Add, U> Add for Vector3D<T, U> {
1599 type Output = Vector3D<T::Output, U>;
1602 fn add(self, other: Self) -> Self::Output {
1603 vec3(self.x + other.x, self.y + other.y, self.z + other.z)
1607 impl<'a, T: 'a + Add + Copy, U: 'a> Add<&Self> for Vector3D<T, U> {
1608 type Output = Vector3D<T::Output, U>;
1611 fn add(self, other: &Self) -> Self::Output {
1612 vec3(self.x + other.x, self.y + other.y, self.z + other.z)
1616 impl<T: Add<Output = T> + Zero, U> Sum for Vector3D<T, U> {
1617 fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
1618 iter.fold(Self::zero(), Add::add)
1622 impl<'a, T: 'a + Add<Output = T> + Copy + Zero, U: 'a> Sum<&'a Self> for Vector3D<T, U> {
1623 fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
1624 iter.fold(Self::zero(), Add::add)
1628 impl<T: Copy + Add<T, Output = T>, U> AddAssign for Vector3D<T, U> {
1630 fn add_assign(&mut self, other: Self) {
1631 *self = *self + other
1635 impl<T: Sub, U> Sub for Vector3D<T, U> {
1636 type Output = Vector3D<T::Output, U>;
1639 fn sub(self, other: Self) -> Self::Output {
1640 vec3(self.x - other.x, self.y - other.y, self.z - other.z)
1644 impl<T: Copy + Sub<T, Output = T>, U> SubAssign<Vector3D<T, U>> for Vector3D<T, U> {
1646 fn sub_assign(&mut self, other: Self) {
1647 *self = *self - other
1651 impl<T: Copy + Mul, U> Mul<T> for Vector3D<T, U> {
1652 type Output = Vector3D<T::Output, U>;
1655 fn mul(self, scale: T) -> Self::Output {
1664 impl<T: Copy + Mul<T, Output = T>, U> MulAssign<T> for Vector3D<T, U> {
1666 fn mul_assign(&mut self, scale: T) {
1667 *self = *self * scale
1671 impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Vector3D<T, U1> {
1672 type Output = Vector3D<T::Output, U2>;
1675 fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output {
1684 impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Vector3D<T, U> {
1686 fn mul_assign(&mut self, scale: Scale<T, U, U>) {
1693 impl<T: Copy + Div, U> Div<T> for Vector3D<T, U> {
1694 type Output = Vector3D<T::Output, U>;
1697 fn div(self, scale: T) -> Self::Output {
1706 impl<T: Copy + Div<T, Output = T>, U> DivAssign<T> for Vector3D<T, U> {
1708 fn div_assign(&mut self, scale: T) {
1709 *self = *self / scale
1713 impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Vector3D<T, U2> {
1714 type Output = Vector3D<T::Output, U1>;
1717 fn div(self, scale: Scale<T, U1, U2>) -> Self::Output {
1726 impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Vector3D<T, U> {
1728 fn div_assign(&mut self, scale: Scale<T, U, U>) {
1735 impl<T: Round, U> Round for Vector3D<T, U> {
1736 /// See [`Vector3D::round()`](#method.round)
1738 fn round(self) -> Self {
1743 impl<T: Ceil, U> Ceil for Vector3D<T, U> {
1744 /// See [`Vector3D::ceil()`](#method.ceil)
1746 fn ceil(self) -> Self {
1751 impl<T: Floor, U> Floor for Vector3D<T, U> {
1752 /// See [`Vector3D::floor()`](#method.floor)
1754 fn floor(self) -> Self {
1759 impl<T: ApproxEq<T>, U> ApproxEq<Vector3D<T, U>> for Vector3D<T, U> {
1761 fn approx_epsilon() -> Self {
1763 T::approx_epsilon(),
1764 T::approx_epsilon(),
1765 T::approx_epsilon(),
1770 fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool {
1771 self.x.approx_eq_eps(&other.x, &eps.x)
1772 && self.y.approx_eq_eps(&other.y, &eps.y)
1773 && self.z.approx_eq_eps(&other.z, &eps.z)
1777 impl<T, U> Into<[T; 3]> for Vector3D<T, U> {
1778 fn into(self) -> [T; 3] {
1779 [self.x, self.y, self.z]
1783 impl<T, U> From<[T; 3]> for Vector3D<T, U> {
1784 fn from([x, y, z]: [T; 3]) -> Self {
1789 impl<T, U> Into<(T, T, T)> for Vector3D<T, U> {
1790 fn into(self) -> (T, T, T) {
1791 (self.x, self.y, self.z)
1795 impl<T, U> From<(T, T, T)> for Vector3D<T, U> {
1796 fn from(tuple: (T, T, T)) -> Self {
1797 vec3(tuple.0, tuple.1, tuple.2)
1801 /// A 2d vector of booleans, useful for component-wise logic operations.
1802 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1803 pub struct BoolVector2D {
1808 /// A 3d vector of booleans, useful for component-wise logic operations.
1809 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1810 pub struct BoolVector3D {
1817 /// Returns `true` if all components are `true` and `false` otherwise.
1819 pub fn all(self) -> bool {
1823 /// Returns `true` if any component are `true` and `false` otherwise.
1825 pub fn any(self) -> bool {
1829 /// Returns `true` if all components are `false` and `false` otherwise. Negation of `any()`.
1831 pub fn none(self) -> bool {
1835 /// Returns new vector with by-component AND operation applied.
1837 pub fn and(self, other: Self) -> Self {
1839 x: self.x && other.x,
1840 y: self.y && other.y,
1844 /// Returns new vector with by-component OR operation applied.
1846 pub fn or(self, other: Self) -> Self {
1848 x: self.x || other.x,
1849 y: self.y || other.y,
1853 /// Returns new vector with results of negation operation on each component.
1855 pub fn not(self) -> Self {
1862 /// Returns point, each component of which or from `a`, or from `b` depending on truly value
1863 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1865 pub fn select_point<T, U>(self, a: Point2D<T, U>, b: Point2D<T, U>) -> Point2D<T, U> {
1867 if self.x { a.x } else { b.x },
1868 if self.y { a.y } else { b.y },
1872 /// Returns vector, each component of which or from `a`, or from `b` depending on truly value
1873 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1875 pub fn select_vector<T, U>(self, a: Vector2D<T, U>, b: Vector2D<T, U>) -> Vector2D<T, U> {
1877 if self.x { a.x } else { b.x },
1878 if self.y { a.y } else { b.y },
1882 /// Returns size, each component of which or from `a`, or from `b` depending on truly value
1883 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1885 pub fn select_size<T, U>(self, a: Size2D<T, U>, b: Size2D<T, U>) -> Size2D<T, U> {
1887 if self.x { a.width } else { b.width },
1888 if self.y { a.height } else { b.height },
1894 /// Returns `true` if all components are `true` and `false` otherwise.
1896 pub fn all(self) -> bool {
1897 self.x && self.y && self.z
1900 /// Returns `true` if any component are `true` and `false` otherwise.
1902 pub fn any(self) -> bool {
1903 self.x || self.y || self.z
1906 /// Returns `true` if all components are `false` and `false` otherwise. Negation of `any()`.
1908 pub fn none(self) -> bool {
1912 /// Returns new vector with by-component AND operation applied.
1914 pub fn and(self, other: Self) -> Self {
1916 x: self.x && other.x,
1917 y: self.y && other.y,
1918 z: self.z && other.z,
1922 /// Returns new vector with by-component OR operation applied.
1924 pub fn or(self, other: Self) -> Self {
1926 x: self.x || other.x,
1927 y: self.y || other.y,
1928 z: self.z || other.z,
1932 /// Returns new vector with results of negation operation on each component.
1934 pub fn not(self) -> Self {
1942 /// Returns point, each component of which or from `a`, or from `b` depending on truly value
1943 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1945 pub fn select_point<T, U>(self, a: Point3D<T, U>, b: Point3D<T, U>) -> Point3D<T, U> {
1947 if self.x { a.x } else { b.x },
1948 if self.y { a.y } else { b.y },
1949 if self.z { a.z } else { b.z },
1953 /// Returns vector, each component of which or from `a`, or from `b` depending on truly value
1954 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1956 pub fn select_vector<T, U>(self, a: Vector3D<T, U>, b: Vector3D<T, U>) -> Vector3D<T, U> {
1958 if self.x { a.x } else { b.x },
1959 if self.y { a.y } else { b.y },
1960 if self.z { a.z } else { b.z },
1964 /// Returns size, each component of which or from `a`, or from `b` depending on truly value
1965 /// of corresponding vector component. `true` selects value from `a` and `false` from `b`.
1968 pub fn select_size<T, U>(self, a: Size3D<T, U>, b: Size3D<T, U>) -> Size3D<T, U> {
1970 if self.x { a.width } else { b.width },
1971 if self.y { a.height } else { b.height },
1972 if self.z { a.depth } else { b.depth },
1976 /// Returns a 2d vector using this vector's x and y coordinates.
1978 pub fn xy(self) -> BoolVector2D {
1985 /// Returns a 2d vector using this vector's x and z coordinates.
1987 pub fn xz(self) -> BoolVector2D {
1994 /// Returns a 2d vector using this vector's y and z coordinates.
1996 pub fn yz(self) -> BoolVector2D {
2004 /// Convenience constructor.
2006 pub const fn vec2<T, U>(x: T, y: T) -> Vector2D<T, U> {
2014 /// Convenience constructor.
2016 pub const fn vec3<T, U>(x: T, y: T, z: T) -> Vector3D<T, U> {
2025 /// Shorthand for `BoolVector2D { x, y }`.
2027 pub const fn bvec2(x: bool, y: bool) -> BoolVector2D {
2028 BoolVector2D { x, y }
2031 /// Shorthand for `BoolVector3D { x, y, z }`.
2033 pub const fn bvec3(x: bool, y: bool, z: bool) -> BoolVector3D {
2034 BoolVector3D { x, y, z }
2039 use crate::scale::Scale;
2040 use crate::{default, vec2};
2042 #[cfg(feature = "mint")]
2044 type Vec2 = default::Vector2D<f32>;
2047 pub fn test_scalar_mul() {
2048 let p1: Vec2 = vec2(3.0, 5.0);
2050 let result = p1 * 5.0;
2052 assert_eq!(result, Vec2::new(15.0, 25.0));
2057 let p1: Vec2 = vec2(2.0, 7.0);
2058 let p2: Vec2 = vec2(13.0, 11.0);
2059 assert_eq!(p1.dot(p2), 103.0);
2063 pub fn test_cross() {
2064 let p1: Vec2 = vec2(4.0, 7.0);
2065 let p2: Vec2 = vec2(13.0, 8.0);
2066 let r = p1.cross(p2);
2067 assert_eq!(r, -59.0);
2071 pub fn test_normalize() {
2074 let p0: Vec2 = Vec2::zero();
2075 let p1: Vec2 = vec2(4.0, 0.0);
2076 let p2: Vec2 = vec2(3.0, -4.0);
2077 assert!(p0.normalize().x.is_nan() && p0.normalize().y.is_nan());
2078 assert_eq!(p1.normalize(), vec2(1.0, 0.0));
2079 assert_eq!(p2.normalize(), vec2(0.6, -0.8));
2081 let p3: Vec2 = vec2(::std::f32::MAX, ::std::f32::MAX);
2084 vec2(1.0 / 2.0f32.sqrt(), 1.0 / 2.0f32.sqrt())
2087 p3.robust_normalize(),
2088 vec2(1.0 / 2.0f32.sqrt(), 1.0 / 2.0f32.sqrt())
2091 let p4: Vec2 = Vec2::zero();
2092 assert!(p4.try_normalize().is_none());
2093 let p5: Vec2 = Vec2::new(f32::MIN_POSITIVE, f32::MIN_POSITIVE);
2094 assert!(p5.try_normalize().is_none());
2096 let p6: Vec2 = vec2(4.0, 0.0);
2097 let p7: Vec2 = vec2(3.0, -4.0);
2098 assert_eq!(p6.try_normalize().unwrap(), vec2(1.0, 0.0));
2099 assert_eq!(p7.try_normalize().unwrap(), vec2(0.6, -0.8));
2104 let p1: Vec2 = vec2(1.0, 3.0);
2105 let p2: Vec2 = vec2(2.0, 2.0);
2107 let result = p1.min(p2);
2109 assert_eq!(result, vec2(1.0, 2.0));
2114 let p1: Vec2 = vec2(1.0, 3.0);
2115 let p2: Vec2 = vec2(2.0, 2.0);
2117 let result = p1.max(p2);
2119 assert_eq!(result, vec2(2.0, 3.0));
2123 pub fn test_angle_from_x_axis() {
2124 use crate::approxeq::ApproxEq;
2125 use core::f32::consts::FRAC_PI_2;
2127 let right: Vec2 = vec2(10.0, 0.0);
2128 let down: Vec2 = vec2(0.0, 4.0);
2129 let up: Vec2 = vec2(0.0, -1.0);
2131 assert!(right.angle_from_x_axis().get().approx_eq(&0.0));
2132 assert!(down.angle_from_x_axis().get().approx_eq(&FRAC_PI_2));
2133 assert!(up.angle_from_x_axis().get().approx_eq(&-FRAC_PI_2));
2137 pub fn test_angle_to() {
2138 use crate::approxeq::ApproxEq;
2139 use core::f32::consts::FRAC_PI_2;
2141 let right: Vec2 = vec2(10.0, 0.0);
2142 let right2: Vec2 = vec2(1.0, 0.0);
2143 let up: Vec2 = vec2(0.0, -1.0);
2144 let up_left: Vec2 = vec2(-1.0, -1.0);
2146 assert!(right.angle_to(right2).get().approx_eq(&0.0));
2147 assert!(right.angle_to(up).get().approx_eq(&-FRAC_PI_2));
2148 assert!(up.angle_to(right).get().approx_eq(&FRAC_PI_2));
2152 .approx_eq_eps(&(0.5 * FRAC_PI_2), &0.0005));
2156 pub fn test_with_max_length() {
2157 use crate::approxeq::ApproxEq;
2159 let v1: Vec2 = vec2(0.5, 0.5);
2160 let v2: Vec2 = vec2(1.0, 0.0);
2161 let v3: Vec2 = vec2(0.1, 0.2);
2162 let v4: Vec2 = vec2(2.0, -2.0);
2163 let v5: Vec2 = vec2(1.0, 2.0);
2164 let v6: Vec2 = vec2(-1.0, 3.0);
2166 assert_eq!(v1.with_max_length(1.0), v1);
2167 assert_eq!(v2.with_max_length(1.0), v2);
2168 assert_eq!(v3.with_max_length(1.0), v3);
2169 assert_eq!(v4.with_max_length(10.0), v4);
2170 assert_eq!(v5.with_max_length(10.0), v5);
2171 assert_eq!(v6.with_max_length(10.0), v6);
2173 let v4_clamped = v4.with_max_length(1.0);
2174 assert!(v4_clamped.length().approx_eq(&1.0));
2175 assert!(v4_clamped.normalize().approx_eq(&v4.normalize()));
2177 let v5_clamped = v5.with_max_length(1.5);
2178 assert!(v5_clamped.length().approx_eq(&1.5));
2179 assert!(v5_clamped.normalize().approx_eq(&v5.normalize()));
2181 let v6_clamped = v6.with_max_length(2.5);
2182 assert!(v6_clamped.length().approx_eq(&2.5));
2183 assert!(v6_clamped.normalize().approx_eq(&v6.normalize()));
2187 pub fn test_project_onto_vector() {
2188 use crate::approxeq::ApproxEq;
2190 let v1: Vec2 = vec2(1.0, 2.0);
2191 let x: Vec2 = vec2(1.0, 0.0);
2192 let y: Vec2 = vec2(0.0, 1.0);
2194 assert!(v1.project_onto_vector(x).approx_eq(&vec2(1.0, 0.0)));
2195 assert!(v1.project_onto_vector(y).approx_eq(&vec2(0.0, 2.0)));
2196 assert!(v1.project_onto_vector(-x).approx_eq(&vec2(1.0, 0.0)));
2197 assert!(v1.project_onto_vector(x * 10.0).approx_eq(&vec2(1.0, 0.0)));
2198 assert!(v1.project_onto_vector(v1 * 2.0).approx_eq(&v1));
2199 assert!(v1.project_onto_vector(-v1).approx_eq(&v1));
2202 #[cfg(feature = "mint")]
2204 pub fn test_mint() {
2205 let v1 = Vec2::new(1.0, 3.0);
2206 let vm: mint::Vector2<_> = v1.into();
2207 let v2 = Vec2::from(vm);
2215 pub type Vector2DMm<T> = super::Vector2D<T, Mm>;
2216 pub type Vector2DCm<T> = super::Vector2D<T, Cm>;
2220 let p1 = Vector2DMm::new(1.0, 2.0);
2221 let p2 = Vector2DMm::new(3.0, 4.0);
2223 assert_eq!(p1 + p2, vec2(4.0, 6.0));
2224 assert_eq!(p1 + &p2, vec2(4.0, 6.0));
2230 Vector2DMm::new(1.0, 2.0),
2231 Vector2DMm::new(3.0, 4.0),
2232 Vector2DMm::new(5.0, 6.0)
2234 let sum = Vector2DMm::new(9.0, 12.0);
2235 assert_eq!(vecs.iter().sum::<Vector2DMm<_>>(), sum);
2239 pub fn test_add_assign() {
2240 let mut p1 = Vector2DMm::new(1.0, 2.0);
2241 p1 += vec2(3.0, 4.0);
2243 assert_eq!(p1, vec2(4.0, 6.0));
2247 pub fn test_tpyed_scalar_mul() {
2248 let p1 = Vector2DMm::new(1.0, 2.0);
2249 let cm_per_mm = Scale::<f32, Mm, Cm>::new(0.1);
2251 let result: Vector2DCm<f32> = p1 * cm_per_mm;
2253 assert_eq!(result, vec2(0.1, 0.2));
2257 pub fn test_swizzling() {
2258 let p: default::Vector2D<i32> = vec2(1, 2);
2259 assert_eq!(p.yx(), vec2(2, 1));
2263 pub fn test_reflect() {
2264 use crate::approxeq::ApproxEq;
2265 let a: Vec2 = vec2(1.0, 3.0);
2266 let n1: Vec2 = vec2(0.0, -1.0);
2267 let n2: Vec2 = vec2(1.0, -1.0).normalize();
2269 assert!(a.reflect(n1).approx_eq(&vec2(1.0, -3.0)));
2270 assert!(a.reflect(n2).approx_eq(&vec2(3.0, 1.0)));
2276 use crate::scale::Scale;
2277 use crate::{default, vec2, vec3};
2278 #[cfg(feature = "mint")]
2281 type Vec3 = default::Vector3D<f32>;
2285 let p1 = Vec3::new(1.0, 2.0, 3.0);
2286 let p2 = Vec3::new(4.0, 5.0, 6.0);
2288 assert_eq!(p1 + p2, vec3(5.0, 7.0, 9.0));
2289 assert_eq!(p1 + &p2, vec3(5.0, 7.0, 9.0));
2295 Vec3::new(1.0, 2.0, 3.0),
2296 Vec3::new(4.0, 5.0, 6.0),
2297 Vec3::new(7.0, 8.0, 9.0)
2299 let sum = Vec3::new(12.0, 15.0, 18.0);
2300 assert_eq!(vecs.iter().sum::<Vec3>(), sum);
2305 let p1: Vec3 = vec3(7.0, 21.0, 32.0);
2306 let p2: Vec3 = vec3(43.0, 5.0, 16.0);
2307 assert_eq!(p1.dot(p2), 918.0);
2311 pub fn test_cross() {
2312 let p1: Vec3 = vec3(4.0, 7.0, 9.0);
2313 let p2: Vec3 = vec3(13.0, 8.0, 3.0);
2314 let p3 = p1.cross(p2);
2315 assert_eq!(p3, vec3(-51.0, 105.0, -59.0));
2319 pub fn test_normalize() {
2322 let p0: Vec3 = Vec3::zero();
2323 let p1: Vec3 = vec3(0.0, -6.0, 0.0);
2324 let p2: Vec3 = vec3(1.0, 2.0, -2.0);
2326 p0.normalize().x.is_nan() && p0.normalize().y.is_nan() && p0.normalize().z.is_nan()
2328 assert_eq!(p1.normalize(), vec3(0.0, -1.0, 0.0));
2329 assert_eq!(p2.normalize(), vec3(1.0 / 3.0, 2.0 / 3.0, -2.0 / 3.0));
2331 let p3: Vec3 = vec3(::std::f32::MAX, ::std::f32::MAX, 0.0);
2334 vec3(1.0 / 2.0f32.sqrt(), 1.0 / 2.0f32.sqrt(), 0.0)
2337 p3.robust_normalize(),
2338 vec3(1.0 / 2.0f32.sqrt(), 1.0 / 2.0f32.sqrt(), 0.0)
2341 let p4: Vec3 = Vec3::zero();
2342 assert!(p4.try_normalize().is_none());
2343 let p5: Vec3 = Vec3::new(f32::MIN_POSITIVE, f32::MIN_POSITIVE, f32::MIN_POSITIVE);
2344 assert!(p5.try_normalize().is_none());
2346 let p6: Vec3 = vec3(4.0, 0.0, 3.0);
2347 let p7: Vec3 = vec3(3.0, -4.0, 0.0);
2348 assert_eq!(p6.try_normalize().unwrap(), vec3(0.8, 0.0, 0.6));
2349 assert_eq!(p7.try_normalize().unwrap(), vec3(0.6, -0.8, 0.0));
2354 let p1: Vec3 = vec3(1.0, 3.0, 5.0);
2355 let p2: Vec3 = vec3(2.0, 2.0, -1.0);
2357 let result = p1.min(p2);
2359 assert_eq!(result, vec3(1.0, 2.0, -1.0));
2364 let p1: Vec3 = vec3(1.0, 3.0, 5.0);
2365 let p2: Vec3 = vec3(2.0, 2.0, -1.0);
2367 let result = p1.max(p2);
2369 assert_eq!(result, vec3(2.0, 3.0, 5.0));
2373 pub fn test_clamp() {
2374 let p1: Vec3 = vec3(1.0, -1.0, 5.0);
2375 let p2: Vec3 = vec3(2.0, 5.0, 10.0);
2376 let p3: Vec3 = vec3(-1.0, 2.0, 20.0);
2378 let result = p3.clamp(p1, p2);
2380 assert_eq!(result, vec3(1.0, 2.0, 10.0));
2384 pub fn test_typed_scalar_mul() {
2388 let p1 = super::Vector3D::<f32, Mm>::new(1.0, 2.0, 3.0);
2389 let cm_per_mm = Scale::<f32, Mm, Cm>::new(0.1);
2391 let result: super::Vector3D<f32, Cm> = p1 * cm_per_mm;
2393 assert_eq!(result, vec3(0.1, 0.2, 0.3));
2397 pub fn test_swizzling() {
2398 let p: Vec3 = vec3(1.0, 2.0, 3.0);
2399 assert_eq!(p.xy(), vec2(1.0, 2.0));
2400 assert_eq!(p.xz(), vec2(1.0, 3.0));
2401 assert_eq!(p.yz(), vec2(2.0, 3.0));
2404 #[cfg(feature = "mint")]
2406 pub fn test_mint() {
2407 let v1 = Vec3::new(1.0, 3.0, 5.0);
2408 let vm: mint::Vector3<_> = v1.into();
2409 let v2 = Vec3::from(vm);
2415 pub fn test_reflect() {
2416 use crate::approxeq::ApproxEq;
2417 let a: Vec3 = vec3(1.0, 3.0, 2.0);
2418 let n1: Vec3 = vec3(0.0, -1.0, 0.0);
2419 let n2: Vec3 = vec3(0.0, 1.0, 1.0).normalize();
2421 assert!(a.reflect(n1).approx_eq(&vec3(1.0, -3.0, 2.0)));
2422 assert!(a.reflect(n2).approx_eq(&vec3(1.0, -2.0, -3.0)));
2426 pub fn test_angle_to() {
2427 use crate::approxeq::ApproxEq;
2428 use core::f32::consts::FRAC_PI_2;
2430 let right: Vec3 = vec3(10.0, 0.0, 0.0);
2431 let right2: Vec3 = vec3(1.0, 0.0, 0.0);
2432 let up: Vec3 = vec3(0.0, -1.0, 0.0);
2433 let up_left: Vec3 = vec3(-1.0, -1.0, 0.0);
2435 assert!(right.angle_to(right2).get().approx_eq(&0.0));
2436 assert!(right.angle_to(up).get().approx_eq(&FRAC_PI_2));
2437 assert!(up.angle_to(right).get().approx_eq(&FRAC_PI_2));
2441 .approx_eq_eps(&(0.5 * FRAC_PI_2), &0.0005));
2445 pub fn test_with_max_length() {
2446 use crate::approxeq::ApproxEq;
2448 let v1: Vec3 = vec3(0.5, 0.5, 0.0);
2449 let v2: Vec3 = vec3(1.0, 0.0, 0.0);
2450 let v3: Vec3 = vec3(0.1, 0.2, 0.3);
2451 let v4: Vec3 = vec3(2.0, -2.0, 2.0);
2452 let v5: Vec3 = vec3(1.0, 2.0, -3.0);
2453 let v6: Vec3 = vec3(-1.0, 3.0, 2.0);
2455 assert_eq!(v1.with_max_length(1.0), v1);
2456 assert_eq!(v2.with_max_length(1.0), v2);
2457 assert_eq!(v3.with_max_length(1.0), v3);
2458 assert_eq!(v4.with_max_length(10.0), v4);
2459 assert_eq!(v5.with_max_length(10.0), v5);
2460 assert_eq!(v6.with_max_length(10.0), v6);
2462 let v4_clamped = v4.with_max_length(1.0);
2463 assert!(v4_clamped.length().approx_eq(&1.0));
2464 assert!(v4_clamped.normalize().approx_eq(&v4.normalize()));
2466 let v5_clamped = v5.with_max_length(1.5);
2467 assert!(v5_clamped.length().approx_eq(&1.5));
2468 assert!(v5_clamped.normalize().approx_eq(&v5.normalize()));
2470 let v6_clamped = v6.with_max_length(2.5);
2471 assert!(v6_clamped.length().approx_eq(&2.5));
2472 assert!(v6_clamped.normalize().approx_eq(&v6.normalize()));
2476 pub fn test_project_onto_vector() {
2477 use crate::approxeq::ApproxEq;
2479 let v1: Vec3 = vec3(1.0, 2.0, 3.0);
2480 let x: Vec3 = vec3(1.0, 0.0, 0.0);
2481 let y: Vec3 = vec3(0.0, 1.0, 0.0);
2482 let z: Vec3 = vec3(0.0, 0.0, 1.0);
2484 assert!(v1.project_onto_vector(x).approx_eq(&vec3(1.0, 0.0, 0.0)));
2485 assert!(v1.project_onto_vector(y).approx_eq(&vec3(0.0, 2.0, 0.0)));
2486 assert!(v1.project_onto_vector(z).approx_eq(&vec3(0.0, 0.0, 3.0)));
2487 assert!(v1.project_onto_vector(-x).approx_eq(&vec3(1.0, 0.0, 0.0)));
2489 .project_onto_vector(x * 10.0)
2490 .approx_eq(&vec3(1.0, 0.0, 0.0)));
2491 assert!(v1.project_onto_vector(v1 * 2.0).approx_eq(&v1));
2492 assert!(v1.project_onto_vector(-v1).approx_eq(&v1));
2500 type Vec2 = default::Vector2D<f32>;
2501 type Vec3 = default::Vector3D<f32>;
2506 Vec2::new(1.0, 2.0).greater_than(Vec2::new(2.0, 1.0)),
2511 Vec2::new(1.0, 2.0).lower_than(Vec2::new(2.0, 1.0)),
2516 Vec2::new(1.0, 2.0).equal(Vec2::new(1.0, 3.0)),
2521 Vec2::new(1.0, 2.0).not_equal(Vec2::new(1.0, 3.0)),
2525 assert!(bvec2(true, true).any());
2526 assert!(bvec2(false, true).any());
2527 assert!(bvec2(true, false).any());
2528 assert!(!bvec2(false, false).any());
2529 assert!(bvec2(false, false).none());
2530 assert!(bvec2(true, true).all());
2531 assert!(!bvec2(false, true).all());
2532 assert!(!bvec2(true, false).all());
2533 assert!(!bvec2(false, false).all());
2535 assert_eq!(bvec2(true, false).not(), bvec2(false, true));
2537 bvec2(true, false).and(bvec2(true, true)),
2540 assert_eq!(bvec2(true, false).or(bvec2(true, true)), bvec2(true, true));
2543 bvec2(true, false).select_vector(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0)),
2544 Vec2::new(1.0, 4.0),
2551 Vec3::new(1.0, 2.0, 3.0).greater_than(Vec3::new(3.0, 2.0, 1.0)),
2552 bvec3(false, false, true),
2556 Vec3::new(1.0, 2.0, 3.0).lower_than(Vec3::new(3.0, 2.0, 1.0)),
2557 bvec3(true, false, false),
2561 Vec3::new(1.0, 2.0, 3.0).equal(Vec3::new(3.0, 2.0, 1.0)),
2562 bvec3(false, true, false),
2566 Vec3::new(1.0, 2.0, 3.0).not_equal(Vec3::new(3.0, 2.0, 1.0)),
2567 bvec3(true, false, true),
2570 assert!(bvec3(true, true, false).any());
2571 assert!(bvec3(false, true, false).any());
2572 assert!(bvec3(true, false, false).any());
2573 assert!(!bvec3(false, false, false).any());
2574 assert!(bvec3(false, false, false).none());
2575 assert!(bvec3(true, true, true).all());
2576 assert!(!bvec3(false, true, false).all());
2577 assert!(!bvec3(true, false, false).all());
2578 assert!(!bvec3(false, false, false).all());
2580 assert_eq!(bvec3(true, false, true).not(), bvec3(false, true, false));
2582 bvec3(true, false, true).and(bvec3(true, true, false)),
2583 bvec3(true, false, false)
2586 bvec3(true, false, false).or(bvec3(true, true, false)),
2587 bvec3(true, true, false)
2591 bvec3(true, false, true)
2592 .select_vector(Vec3::new(1.0, 2.0, 3.0), Vec3::new(4.0, 5.0, 6.0)),
2593 Vec3::new(1.0, 5.0, 3.0),