1use num_traits::{Bounded, Float, NumCast};
17#[cfg(feature = "rand")]
18use rand::{
19 distributions::{Distribution, Standard},
20 Rng,
21};
22use std::fmt;
23use std::iter;
24use std::mem;
25use std::ops::*;
26
27use structure::*;
28
29use angle::Rad;
30use approx;
31use num::{BaseFloat, BaseNum};
32
33#[cfg(feature = "mint")]
34use mint;
35
36#[repr(C)]
40#[derive(PartialEq, Eq, Copy, Clone, Hash)]
41#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42pub struct Vector1<S> {
43 pub x: S,
45}
46
47#[repr(C)]
51#[derive(PartialEq, Eq, Copy, Clone, Hash)]
52#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
53pub struct Vector2<S> {
54 pub x: S,
56 pub y: S,
58}
59
60#[repr(C)]
64#[derive(PartialEq, Eq, Copy, Clone, Hash)]
65#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
66pub struct Vector3<S> {
67 pub x: S,
69 pub y: S,
71 pub z: S,
73}
74
75#[repr(C)]
79#[derive(PartialEq, Eq, Copy, Clone, Hash)]
80#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
81pub struct Vector4<S> {
82 pub x: S,
84 pub y: S,
86 pub z: S,
88 pub w: S,
90}
91
92macro_rules! impl_vector {
94 ($VectorN:ident { $($field:ident),+ }, $n:expr, $constructor:ident) => {
95 impl<S> $VectorN<S> {
96 #[inline]
98 pub const fn new($($field: S),+) -> $VectorN<S> {
99 $VectorN { $($field: $field),+ }
100 }
101
102 #[inline]
105 pub fn map<U, F>(self, mut f: F) -> $VectorN<U>
106 where F: FnMut(S) -> U
107 {
108 $VectorN { $($field: f(self.$field)),+ }
109 }
110
111 #[inline]
115 pub fn zip<S2, S3, F>(self, v2: $VectorN<S2>, mut f: F) -> $VectorN<S3>
116 where F: FnMut(S, S2) -> S3
117 {
118 $VectorN { $($field: f(self.$field, v2.$field)),+ }
119 }
120 }
121
122 #[inline]
124 pub const fn $constructor<S>($($field: S),+) -> $VectorN<S> {
125 $VectorN::new($($field),+)
126 }
127
128 impl<S: NumCast + Copy> $VectorN<S> {
129 #[inline]
131 pub fn cast<T: NumCast>(&self) -> Option<$VectorN<T>> {
132 $(
133 let $field = match NumCast::from(self.$field) {
134 Some(field) => field,
135 None => return None
136 };
137 )+
138 Some($VectorN { $($field),+ })
139 }
140 }
141
142 impl<S: BaseNum> MetricSpace for $VectorN<S> {
143 type Metric = S;
144
145 #[inline]
146 fn distance2(self, other: Self) -> S {
147 (other - self).magnitude2()
148 }
149 }
150
151 impl<S: Copy> Array for $VectorN<S> {
152 type Element = S;
153
154 #[inline]
155 fn len() -> usize {
156 $n
157 }
158
159 #[inline]
160 fn from_value(scalar: S) -> $VectorN<S> {
161 $VectorN { $($field: scalar),+ }
162 }
163
164 #[inline]
165 fn sum(self) -> S where S: Add<Output = S> {
166 fold_array!(add, { $(self.$field),+ })
167 }
168
169 #[inline]
170 fn product(self) -> S where S: Mul<Output = S> {
171 fold_array!(mul, { $(self.$field),+ })
172 }
173
174 fn is_finite(&self) -> bool where S: Float {
175 $(self.$field.is_finite())&&+
176 }
177 }
178
179 impl<S: BaseNum> Zero for $VectorN<S> {
180 #[inline]
181 fn zero() -> $VectorN<S> {
182 $VectorN::from_value(S::zero())
183 }
184
185 #[inline]
186 fn is_zero(&self) -> bool {
187 *self == $VectorN::zero()
188 }
189 }
190
191 impl<S: BaseNum> iter::Sum<$VectorN<S>> for $VectorN<S> {
192 #[inline]
193 fn sum<I: Iterator<Item=$VectorN<S>>>(iter: I) -> $VectorN<S> {
194 iter.fold($VectorN::zero(), Add::add)
195 }
196 }
197
198 impl<'a, S: 'a + BaseNum> iter::Sum<&'a $VectorN<S>> for $VectorN<S> {
199 #[inline]
200 fn sum<I: Iterator<Item=&'a $VectorN<S>>>(iter: I) -> $VectorN<S> {
201 iter.fold($VectorN::zero(), Add::add)
202 }
203 }
204
205 impl<S: BaseNum> VectorSpace for $VectorN<S> {
206 type Scalar = S;
207 }
208
209 impl<S: Neg<Output = S>> Neg for $VectorN<S> {
210 type Output = $VectorN<S>;
211
212 #[inline]
213 default_fn!( neg(self) -> $VectorN<S> { $VectorN::new($(-self.$field),+) } );
214 }
215
216 impl<S: BaseFloat> approx::AbsDiffEq for $VectorN<S> {
217 type Epsilon = S::Epsilon;
218
219 #[inline]
220 fn default_epsilon() -> S::Epsilon {
221 S::default_epsilon()
222 }
223
224 #[inline]
225 fn abs_diff_eq(&self, other: &Self, epsilon: S::Epsilon) -> bool {
226 $(S::abs_diff_eq(&self.$field, &other.$field, epsilon))&&+
227 }
228 }
229
230 impl<S: BaseFloat> approx::RelativeEq for $VectorN<S> {
231 #[inline]
232 fn default_max_relative() -> S::Epsilon {
233 S::default_max_relative()
234 }
235
236 #[inline]
237 fn relative_eq(&self, other: &Self, epsilon: S::Epsilon, max_relative: S::Epsilon) -> bool {
238 $(S::relative_eq(&self.$field, &other.$field, epsilon, max_relative))&&+
239 }
240 }
241
242 impl<S: BaseFloat> approx::UlpsEq for $VectorN<S> {
243 #[inline]
244 fn default_max_ulps() -> u32 {
245 S::default_max_ulps()
246 }
247
248 #[inline]
249 fn ulps_eq(&self, other: &Self, epsilon: S::Epsilon, max_ulps: u32) -> bool {
250 $(S::ulps_eq(&self.$field, &other.$field, epsilon, max_ulps))&&+
251 }
252 }
253
254 #[cfg(feature = "rand")]
255 impl<S> Distribution<$VectorN<S>> for Standard
256 where Standard: Distribution<S>,
257 S: BaseFloat {
258 #[inline]
259 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $VectorN<S> {
260 $VectorN { $($field: rng.gen()),+ }
261 }
262 }
263
264 impl<S: Bounded> Bounded for $VectorN<S> {
265 #[inline]
266 fn min_value() -> $VectorN<S> {
267 $VectorN { $($field: S::min_value()),+ }
268 }
269
270 #[inline]
271 fn max_value() -> $VectorN<S> {
272 $VectorN { $($field: S::max_value()),+ }
273 }
274 }
275
276 impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $VectorN<S> {
277 fn add(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field + rhs.$field),+) }
278 });
279 impl_assignment_operator!(<S: BaseNum> AddAssign<$VectorN<S> > for $VectorN<S> {
280 fn add_assign(&mut self, other) { $(self.$field += other.$field);+ }
281 });
282
283 impl_operator!(<S: BaseNum> Sub<$VectorN<S> > for $VectorN<S> {
284 fn sub(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field - rhs.$field),+) }
285 });
286 impl_assignment_operator!(<S: BaseNum> SubAssign<$VectorN<S> > for $VectorN<S> {
287 fn sub_assign(&mut self, other) { $(self.$field -= other.$field);+ }
288 });
289
290 impl_operator!(<S: BaseNum> Mul<S> for $VectorN<S> {
291 fn mul(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field * scalar),+) }
292 });
293 impl_assignment_operator!(<S: BaseNum> MulAssign<S> for $VectorN<S> {
294 fn mul_assign(&mut self, scalar) { $(self.$field *= scalar);+ }
295 });
296
297 impl_operator!(<S: BaseNum> Div<S> for $VectorN<S> {
298 fn div(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field / scalar),+) }
299 });
300 impl_assignment_operator!(<S: BaseNum> DivAssign<S> for $VectorN<S> {
301 fn div_assign(&mut self, scalar) { $(self.$field /= scalar);+ }
302 });
303
304 impl_operator!(<S: BaseNum> Rem<S> for $VectorN<S> {
305 fn rem(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field % scalar),+) }
306 });
307 impl_assignment_operator!(<S: BaseNum> RemAssign<S> for $VectorN<S> {
308 fn rem_assign(&mut self, scalar) { $(self.$field %= scalar);+ }
309 });
310
311 impl<S: BaseNum> ElementWise for $VectorN<S> {
312 #[inline] default_fn!( add_element_wise(self, rhs: $VectorN<S>) -> $VectorN<S> { $VectorN::new($(self.$field + rhs.$field),+) } );
313 #[inline] default_fn!( sub_element_wise(self, rhs: $VectorN<S>) -> $VectorN<S> { $VectorN::new($(self.$field - rhs.$field),+) } );
314 #[inline] default_fn!( mul_element_wise(self, rhs: $VectorN<S>) -> $VectorN<S> { $VectorN::new($(self.$field * rhs.$field),+) } );
315 #[inline] default_fn!( div_element_wise(self, rhs: $VectorN<S>) -> $VectorN<S> { $VectorN::new($(self.$field / rhs.$field),+) } );
316 #[inline] fn rem_element_wise(self, rhs: $VectorN<S>) -> $VectorN<S> { $VectorN::new($(self.$field % rhs.$field),+) }
317
318 #[inline] default_fn!( add_assign_element_wise(&mut self, rhs: $VectorN<S>) { $(self.$field += rhs.$field);+ } );
319 #[inline] default_fn!( sub_assign_element_wise(&mut self, rhs: $VectorN<S>) { $(self.$field -= rhs.$field);+ } );
320 #[inline] default_fn!( mul_assign_element_wise(&mut self, rhs: $VectorN<S>) { $(self.$field *= rhs.$field);+ } );
321 #[inline] default_fn!( div_assign_element_wise(&mut self, rhs: $VectorN<S>) { $(self.$field /= rhs.$field);+ } );
322 #[inline] fn rem_assign_element_wise(&mut self, rhs: $VectorN<S>) { $(self.$field %= rhs.$field);+ }
323 }
324
325 impl<S: BaseNum> ElementWise<S> for $VectorN<S> {
326 #[inline] default_fn!( add_element_wise(self, rhs: S) -> $VectorN<S> { $VectorN::new($(self.$field + rhs),+) } );
327 #[inline] default_fn!( sub_element_wise(self, rhs: S) -> $VectorN<S> { $VectorN::new($(self.$field - rhs),+) } );
328 #[inline] default_fn!( mul_element_wise(self, rhs: S) -> $VectorN<S> { $VectorN::new($(self.$field * rhs),+) } );
329 #[inline] default_fn!( div_element_wise(self, rhs: S) -> $VectorN<S> { $VectorN::new($(self.$field / rhs),+) } );
330 #[inline] fn rem_element_wise(self, rhs: S) -> $VectorN<S> { $VectorN::new($(self.$field % rhs),+) }
331
332 #[inline] default_fn!( add_assign_element_wise(&mut self, rhs: S) { $(self.$field += rhs);+ } );
333 #[inline] default_fn!( sub_assign_element_wise(&mut self, rhs: S) { $(self.$field -= rhs);+ } );
334 #[inline] default_fn!( mul_assign_element_wise(&mut self, rhs: S) { $(self.$field *= rhs);+ } );
335 #[inline] default_fn!( div_assign_element_wise(&mut self, rhs: S) { $(self.$field /= rhs);+ } );
336 #[inline] fn rem_assign_element_wise(&mut self, rhs: S) { $(self.$field %= rhs);+ }
337 }
338
339 impl_scalar_ops!($VectorN<usize> { $($field),+ });
340 impl_scalar_ops!($VectorN<u8> { $($field),+ });
341 impl_scalar_ops!($VectorN<u16> { $($field),+ });
342 impl_scalar_ops!($VectorN<u32> { $($field),+ });
343 impl_scalar_ops!($VectorN<u64> { $($field),+ });
344 impl_scalar_ops!($VectorN<isize> { $($field),+ });
345 impl_scalar_ops!($VectorN<i8> { $($field),+ });
346 impl_scalar_ops!($VectorN<i16> { $($field),+ });
347 impl_scalar_ops!($VectorN<i32> { $($field),+ });
348 impl_scalar_ops!($VectorN<i64> { $($field),+ });
349 impl_scalar_ops!($VectorN<f32> { $($field),+ });
350 impl_scalar_ops!($VectorN<f64> { $($field),+ });
351
352 impl_index_operators!($VectorN<S>, $n, S, usize);
353 impl_index_operators!($VectorN<S>, $n, [S], Range<usize>);
354 impl_index_operators!($VectorN<S>, $n, [S], RangeTo<usize>);
355 impl_index_operators!($VectorN<S>, $n, [S], RangeFrom<usize>);
356 impl_index_operators!($VectorN<S>, $n, [S], RangeFull);
357 }
358}
359
360macro_rules! impl_scalar_ops {
361 ($VectorN:ident<$S:ident> { $($field:ident),+ }) => {
362 impl_operator!(Mul<$VectorN<$S>> for $S {
363 fn mul(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar * vector.$field),+) }
364 });
365 impl_operator!(Div<$VectorN<$S>> for $S {
366 fn div(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar / vector.$field),+) }
367 });
368 impl_operator!(Rem<$VectorN<$S>> for $S {
369 fn rem(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar % vector.$field),+) }
370 });
371 };
372}
373
374impl_vector!(Vector1 { x }, 1, vec1);
375impl_vector!(Vector2 { x, y }, 2, vec2);
376impl_vector!(Vector3 { x, y, z }, 3, vec3);
377impl_vector!(Vector4 { x, y, z, w }, 4, vec4);
378
379impl_fixed_array_conversions!(Vector1<S> { x: 0 }, 1);
380impl_fixed_array_conversions!(Vector2<S> { x: 0, y: 1 }, 2);
381impl_fixed_array_conversions!(Vector3<S> { x: 0, y: 1, z: 2 }, 3);
382impl_fixed_array_conversions!(Vector4<S> { x: 0, y: 1, z: 2, w: 3 }, 4);
383
384impl_tuple_conversions!(Vector1<S> { x }, (S,));
385impl_tuple_conversions!(Vector2<S> { x, y }, (S, S));
386impl_tuple_conversions!(Vector3<S> { x, y, z }, (S, S, S));
387impl_tuple_conversions!(Vector4<S> { x, y, z, w }, (S, S, S, S));
388
389impl<S: BaseNum> Vector1<S> {
390 #[inline]
392 pub fn unit_x() -> Vector1<S> {
393 Vector1::new(S::one())
394 }
395
396 impl_swizzle_functions!(Vector1, Vector2, Vector3, Vector4, S, x);
397}
398
399impl<S: BaseNum> Vector2<S> {
400 #[inline]
402 pub fn unit_x() -> Vector2<S> {
403 Vector2::new(S::one(), S::zero())
404 }
405
406 #[inline]
408 pub fn unit_y() -> Vector2<S> {
409 Vector2::new(S::zero(), S::one())
410 }
411
412 #[inline]
414 pub fn perp_dot(self, other: Vector2<S>) -> S {
415 (self.x * other.y) - (self.y * other.x)
416 }
417
418 #[inline]
421 pub fn extend(self, z: S) -> Vector3<S> {
422 Vector3::new(self.x, self.y, z)
423 }
424
425 impl_swizzle_functions!(Vector1, Vector2, Vector3, Vector4, S, xy);
426}
427
428impl<S: BaseNum> Vector3<S> {
429 #[inline]
431 pub fn unit_x() -> Vector3<S> {
432 Vector3::new(S::one(), S::zero(), S::zero())
433 }
434
435 #[inline]
437 pub fn unit_y() -> Vector3<S> {
438 Vector3::new(S::zero(), S::one(), S::zero())
439 }
440
441 #[inline]
443 pub fn unit_z() -> Vector3<S> {
444 Vector3::new(S::zero(), S::zero(), S::one())
445 }
446
447 #[inline]
449 pub fn cross(self, other: Vector3<S>) -> Vector3<S> {
450 Vector3::new(
451 (self.y * other.z) - (self.z * other.y),
452 (self.z * other.x) - (self.x * other.z),
453 (self.x * other.y) - (self.y * other.x),
454 )
455 }
456
457 #[inline]
460 pub fn extend(self, w: S) -> Vector4<S> {
461 Vector4::new(self.x, self.y, self.z, w)
462 }
463
464 #[inline]
466 pub fn truncate(self) -> Vector2<S> {
467 Vector2::new(self.x, self.y)
468 }
469
470 impl_swizzle_functions!(Vector1, Vector2, Vector3, Vector4, S, xyz);
471}
472
473impl<S: BaseNum> Vector4<S> {
474 #[inline]
476 pub fn unit_x() -> Vector4<S> {
477 Vector4::new(S::one(), S::zero(), S::zero(), S::zero())
478 }
479
480 #[inline]
482 pub fn unit_y() -> Vector4<S> {
483 Vector4::new(S::zero(), S::one(), S::zero(), S::zero())
484 }
485
486 #[inline]
488 pub fn unit_z() -> Vector4<S> {
489 Vector4::new(S::zero(), S::zero(), S::one(), S::zero())
490 }
491
492 #[inline]
494 pub fn unit_w() -> Vector4<S> {
495 Vector4::new(S::zero(), S::zero(), S::zero(), S::one())
496 }
497
498 #[inline]
500 pub fn truncate(self) -> Vector3<S> {
501 Vector3::new(self.x, self.y, self.z)
502 }
503
504 #[inline]
506 pub fn truncate_n(&self, n: isize) -> Vector3<S> {
507 match n {
508 0 => Vector3::new(self.y, self.z, self.w),
509 1 => Vector3::new(self.x, self.z, self.w),
510 2 => Vector3::new(self.x, self.y, self.w),
511 3 => Vector3::new(self.x, self.y, self.z),
512 _ => panic!("{:?} is out of range", n),
513 }
514 }
515
516 impl_swizzle_functions!(Vector1, Vector2, Vector3, Vector4, S, xyzw);
517}
518
519#[inline]
521pub fn dot<V: InnerSpace>(a: V, b: V) -> V::Scalar
522where
523 V::Scalar: BaseFloat,
524{
525 V::dot(a, b)
526}
527
528impl<S: BaseNum> InnerSpace for Vector1<S> {
529 #[inline]
530 fn dot(self, other: Vector1<S>) -> S {
531 Vector1::mul_element_wise(self, other).sum()
532 }
533}
534
535impl<S: BaseNum> InnerSpace for Vector2<S> {
536 #[inline]
537 fn dot(self, other: Vector2<S>) -> S {
538 Vector2::mul_element_wise(self, other).sum()
539 }
540
541 #[inline]
542 fn angle(self, other: Vector2<S>) -> Rad<S>
543 where
544 S: BaseFloat,
545 {
546 Rad::atan2(Self::perp_dot(self, other), Self::dot(self, other))
547 }
548}
549
550impl<S: BaseNum> InnerSpace for Vector3<S> {
551 #[inline]
552 fn dot(self, other: Vector3<S>) -> S {
553 Vector3::mul_element_wise(self, other).sum()
554 }
555
556 #[inline]
557 fn angle(self, other: Vector3<S>) -> Rad<S>
558 where
559 S: BaseFloat,
560 {
561 Rad::atan2(self.cross(other).magnitude(), Self::dot(self, other))
562 }
563}
564
565impl<S: BaseNum> InnerSpace for Vector4<S> {
566 #[inline]
567 fn dot(self, other: Vector4<S>) -> S {
568 Vector4::mul_element_wise(self, other).sum()
569 }
570}
571
572impl<S: fmt::Debug> fmt::Debug for Vector1<S> {
573 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
574 write!(f, "Vector1 ")?;
575 <[S; 1] as fmt::Debug>::fmt(self.as_ref(), f)
576 }
577}
578
579impl<S: fmt::Debug> fmt::Debug for Vector2<S> {
580 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
581 write!(f, "Vector2 ")?;
582 <[S; 2] as fmt::Debug>::fmt(self.as_ref(), f)
583 }
584}
585
586impl<S: fmt::Debug> fmt::Debug for Vector3<S> {
587 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
588 write!(f, "Vector3 ")?;
589 <[S; 3] as fmt::Debug>::fmt(self.as_ref(), f)
590 }
591}
592
593impl<S: fmt::Debug> fmt::Debug for Vector4<S> {
594 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
595 write!(f, "Vector4 ")?;
596 <[S; 4] as fmt::Debug>::fmt(self.as_ref(), f)
597 }
598}
599
600#[cfg(feature = "mint")]
601impl_mint_conversions!(Vector2 { x, y }, Vector2);
602#[cfg(feature = "mint")]
603impl_mint_conversions!(Vector3 { x, y, z }, Vector3);
604#[cfg(feature = "mint")]
605impl_mint_conversions!(Vector4 { x, y, z, w }, Vector4);
606
607#[cfg(test)]
608mod tests {
609 mod vector2 {
610 use vector::*;
611
612 const VECTOR2: Vector2<i32> = Vector2 { x: 1, y: 2 };
613
614 #[test]
615 fn test_index() {
616 assert_eq!(VECTOR2[0], VECTOR2.x);
617 assert_eq!(VECTOR2[1], VECTOR2.y);
618 }
619
620 #[test]
621 fn test_index_mut() {
622 let mut v = VECTOR2;
623 *&mut v[0] = 0;
624 assert_eq!(v, [0, 2].into());
625 }
626
627 #[test]
628 #[should_panic]
629 fn test_index_out_of_bounds() {
630 VECTOR2[2];
631 }
632
633 #[test]
634 fn test_index_range() {
635 assert_eq!(&VECTOR2[..0], &[]);
636 assert_eq!(&VECTOR2[..1], &[1]);
637 assert_eq!(VECTOR2[..0].len(), 0);
638 assert_eq!(VECTOR2[..1].len(), 1);
639 assert_eq!(&VECTOR2[2..], &[]);
640 assert_eq!(&VECTOR2[1..], &[2]);
641 assert_eq!(VECTOR2[2..].len(), 0);
642 assert_eq!(VECTOR2[1..].len(), 1);
643 assert_eq!(&VECTOR2[..], &[1, 2]);
644 assert_eq!(VECTOR2[..].len(), 2);
645 }
646
647 #[test]
648 fn test_into() {
649 let v = VECTOR2;
650 {
651 let v: [i32; 2] = v.into();
652 assert_eq!(v, [1, 2]);
653 }
654 {
655 let v: (i32, i32) = v.into();
656 assert_eq!(v, (1, 2));
657 }
658 }
659
660 #[test]
661 fn test_as_ref() {
662 let v = VECTOR2;
663 {
664 let v: &[i32; 2] = v.as_ref();
665 assert_eq!(v, &[1, 2]);
666 }
667 {
668 let v: &(i32, i32) = v.as_ref();
669 assert_eq!(v, &(1, 2));
670 }
671 }
672
673 #[test]
674 fn test_as_mut() {
675 let mut v = VECTOR2;
676 {
677 let v: &mut [i32; 2] = v.as_mut();
678 assert_eq!(v, &mut [1, 2]);
679 }
680 {
681 let v: &mut (i32, i32) = v.as_mut();
682 assert_eq!(v, &mut (1, 2));
683 }
684 }
685
686 #[test]
687 fn test_from() {
688 assert_eq!(Vector2::from([1, 2]), VECTOR2);
689 {
690 let v = &[1, 2];
691 let v: &Vector2<_> = From::from(v);
692 assert_eq!(v, &VECTOR2);
693 }
694 {
695 let v = &mut [1, 2];
696 let v: &mut Vector2<_> = From::from(v);
697 assert_eq!(v, &VECTOR2);
698 }
699 assert_eq!(Vector2::from((1, 2)), VECTOR2);
700 {
701 let v = &(1, 2);
702 let v: &Vector2<_> = From::from(v);
703 assert_eq!(v, &VECTOR2);
704 }
705 {
706 let v = &mut (1, 2);
707 let v: &mut Vector2<_> = From::from(v);
708 assert_eq!(v, &VECTOR2);
709 }
710 }
711
712 #[test]
713 fn test_is_finite() {
714 use num_traits::Float;
715 assert!(!Vector2::from([Float::nan(), 1.0]).is_finite());
716 assert!(!Vector2::from([1.0, Float::infinity()]).is_finite());
717 assert!(Vector2::from([-1.0, 1.0]).is_finite());
718 }
719
720 #[test]
721 fn test_zip() {
722 assert_eq!(
723 Vector2::new(true, false),
724 Vector2::new(-2, 1).zip(Vector2::new(-1, -1), |a, b| a < b)
725 );
726 }
727 }
728
729 mod vector3 {
730 use vector::*;
731
732 const VECTOR3: Vector3<i32> = Vector3 { x: 1, y: 2, z: 3 };
733
734 #[test]
735 fn test_index() {
736 assert_eq!(VECTOR3[0], VECTOR3.x);
737 assert_eq!(VECTOR3[1], VECTOR3.y);
738 assert_eq!(VECTOR3[2], VECTOR3.z);
739 }
740
741 #[test]
742 fn test_index_mut() {
743 let mut v = VECTOR3;
744 *&mut v[1] = 0;
745 assert_eq!(v, [1, 0, 3].into());
746 }
747
748 #[test]
749 #[should_panic]
750 fn test_index_out_of_bounds() {
751 VECTOR3[3];
752 }
753
754 #[test]
755 fn test_index_range() {
756 assert_eq!(&VECTOR3[..1], &[1]);
757 assert_eq!(&VECTOR3[..2], &[1, 2]);
758 assert_eq!(VECTOR3[..1].len(), 1);
759 assert_eq!(VECTOR3[..2].len(), 2);
760 assert_eq!(&VECTOR3[2..], &[3]);
761 assert_eq!(&VECTOR3[1..], &[2, 3]);
762 assert_eq!(VECTOR3[2..].len(), 1);
763 assert_eq!(VECTOR3[1..].len(), 2);
764 assert_eq!(&VECTOR3[..], &[1, 2, 3]);
765 assert_eq!(VECTOR3[..].len(), 3);
766 }
767
768 #[test]
769 fn test_into() {
770 let v = VECTOR3;
771 {
772 let v: [i32; 3] = v.into();
773 assert_eq!(v, [1, 2, 3]);
774 }
775 {
776 let v: (i32, i32, i32) = v.into();
777 assert_eq!(v, (1, 2, 3));
778 }
779 }
780
781 #[test]
782 fn test_as_ref() {
783 let v = VECTOR3;
784 {
785 let v: &[i32; 3] = v.as_ref();
786 assert_eq!(v, &[1, 2, 3]);
787 }
788 {
789 let v: &(i32, i32, i32) = v.as_ref();
790 assert_eq!(v, &(1, 2, 3));
791 }
792 }
793
794 #[test]
795 fn test_as_mut() {
796 let mut v = VECTOR3;
797 {
798 let v: &mut [i32; 3] = v.as_mut();
799 assert_eq!(v, &mut [1, 2, 3]);
800 }
801 {
802 let v: &mut (i32, i32, i32) = v.as_mut();
803 assert_eq!(v, &mut (1, 2, 3));
804 }
805 }
806
807 #[test]
808 fn test_from() {
809 assert_eq!(Vector3::from([1, 2, 3]), VECTOR3);
810 {
811 let v = &[1, 2, 3];
812 let v: &Vector3<_> = From::from(v);
813 assert_eq!(v, &VECTOR3);
814 }
815 {
816 let v = &mut [1, 2, 3];
817 let v: &mut Vector3<_> = From::from(v);
818 assert_eq!(v, &VECTOR3);
819 }
820 assert_eq!(Vector3::from((1, 2, 3)), VECTOR3);
821 {
822 let v = &(1, 2, 3);
823 let v: &Vector3<_> = From::from(v);
824 assert_eq!(v, &VECTOR3);
825 }
826 {
827 let v = &mut (1, 2, 3);
828 let v: &mut Vector3<_> = From::from(v);
829 assert_eq!(v, &VECTOR3);
830 }
831 }
832
833 #[test]
834 fn test_is_finite() {
835 use num_traits::Float;
836 assert!(!Vector3::from([Float::nan(), 1.0, 1.0]).is_finite());
837 assert!(!Vector3::from([1.0, 1.0, Float::infinity()]).is_finite());
838 assert!(Vector3::from([-1.0, 1.0, 1.0]).is_finite());
839 }
840
841 #[test]
842 fn test_zip() {
843 assert_eq!(
844 Vector3::new(true, false, false),
845 Vector3::new(-2, 1, 0).zip(Vector3::new(-1, -1, -1), |a, b| a < b)
846 );
847 }
848 }
849
850 mod vector4 {
851 use vector::*;
852
853 const VECTOR4: Vector4<i32> = Vector4 {
854 x: 1,
855 y: 2,
856 z: 3,
857 w: 4,
858 };
859
860 #[test]
861 fn test_index() {
862 assert_eq!(VECTOR4[0], VECTOR4.x);
863 assert_eq!(VECTOR4[1], VECTOR4.y);
864 assert_eq!(VECTOR4[2], VECTOR4.z);
865 assert_eq!(VECTOR4[3], VECTOR4.w);
866 }
867
868 #[test]
869 fn test_index_mut() {
870 let mut v = VECTOR4;
871 *&mut v[2] = 0;
872 assert_eq!(v, [1, 2, 0, 4].into());
873 }
874
875 #[test]
876 #[should_panic]
877 fn test_index_out_of_bounds() {
878 VECTOR4[4];
879 }
880
881 #[test]
882 fn test_index_range() {
883 assert_eq!(&VECTOR4[..2], &[1, 2]);
884 assert_eq!(&VECTOR4[..3], &[1, 2, 3]);
885 assert_eq!(VECTOR4[..2].len(), 2);
886 assert_eq!(VECTOR4[..3].len(), 3);
887 assert_eq!(&VECTOR4[2..], &[3, 4]);
888 assert_eq!(&VECTOR4[1..], &[2, 3, 4]);
889 assert_eq!(VECTOR4[2..].len(), 2);
890 assert_eq!(VECTOR4[1..].len(), 3);
891 assert_eq!(&VECTOR4[..], &[1, 2, 3, 4]);
892 assert_eq!(VECTOR4[..].len(), 4);
893 }
894
895 #[test]
896 fn test_into() {
897 let v = VECTOR4;
898 {
899 let v: [i32; 4] = v.into();
900 assert_eq!(v, [1, 2, 3, 4]);
901 }
902 {
903 let v: (i32, i32, i32, i32) = v.into();
904 assert_eq!(v, (1, 2, 3, 4));
905 }
906 }
907
908 #[test]
909 fn test_as_ref() {
910 let v = VECTOR4;
911 {
912 let v: &[i32; 4] = v.as_ref();
913 assert_eq!(v, &[1, 2, 3, 4]);
914 }
915 {
916 let v: &(i32, i32, i32, i32) = v.as_ref();
917 assert_eq!(v, &(1, 2, 3, 4));
918 }
919 }
920
921 #[test]
922 fn test_as_mut() {
923 let mut v = VECTOR4;
924 {
925 let v: &mut [i32; 4] = v.as_mut();
926 assert_eq!(v, &mut [1, 2, 3, 4]);
927 }
928 {
929 let v: &mut (i32, i32, i32, i32) = v.as_mut();
930 assert_eq!(v, &mut (1, 2, 3, 4));
931 }
932 }
933
934 #[test]
935 fn test_from() {
936 assert_eq!(Vector4::from([1, 2, 3, 4]), VECTOR4);
937 {
938 let v = &[1, 2, 3, 4];
939 let v: &Vector4<_> = From::from(v);
940 assert_eq!(v, &VECTOR4);
941 }
942 {
943 let v = &mut [1, 2, 3, 4];
944 let v: &mut Vector4<_> = From::from(v);
945 assert_eq!(v, &VECTOR4);
946 }
947 assert_eq!(Vector4::from((1, 2, 3, 4)), VECTOR4);
948 {
949 let v = &(1, 2, 3, 4);
950 let v: &Vector4<_> = From::from(v);
951 assert_eq!(v, &VECTOR4);
952 }
953 {
954 let v = &mut (1, 2, 3, 4);
955 let v: &mut Vector4<_> = From::from(v);
956 assert_eq!(v, &VECTOR4);
957 }
958 }
959
960 #[test]
961 fn test_is_finite() {
962 use num_traits::Float;
963 assert!(!Vector4::from([0.0, Float::nan(), 1.0, 1.0]).is_finite());
964 assert!(!Vector4::from([1.0, 1.0, Float::neg_infinity(), 0.0]).is_finite());
965 assert!(Vector4::from([-1.0, 0.0, 1.0, 1.0]).is_finite());
966 }
967
968 #[test]
969 fn test_zip() {
970 assert_eq!(
971 Vector4::new(true, false, false, false),
972 Vector4::new(-2, 1, 0, 1).zip(Vector4::new(-1, -1, -1, -1), |a, b| a < b)
973 );
974 }
975
976 #[test]
977 fn test_dot() {
978 assert_eq!(vec3(1.0, 2.0, 3.0).dot(vec3(4.0, 5.0, 6.0)), 32.0);
979 assert_eq!(vec3(1, 2, 3).dot(vec3(4, 5, 6)), 32);
980 }
981 }
982}