1use crate::matrix::{Matrix, Row};
2use std::f32;
3use std::ops::*;
4
5pub trait Vector: Sized + Div<f32, Output = Self> {
6 fn zero() -> Self;
7 fn one() -> Self;
8 fn infinity() -> Self;
9 fn nan() -> Self;
10 fn epsilon() -> Self;
11 fn replicate(value: f32) -> Self;
12
13 fn is_nan(&self) -> bool;
17 fn is_infinite(&self) -> bool;
18 fn is_finite(&self) -> bool {
19 !self.is_infinite()
20 }
21
22 fn dot(&self, other: &Self) -> f32;
26 fn length_sq(&self) -> f32 {
27 self.dot(self)
28 }
29 fn length(&self) -> f32 {
30 self.length_sq().sqrt()
31 }
32 fn normalize(self) -> Self {
33 let len = self.length();
34 self / len
35 }
36
37 fn swizzle(&self, e0: usize, e1: usize, e2: usize, e3: usize) -> Self;
38 fn permute(
39 &self,
40 other: &Self,
41 permute_x: usize,
42 permute_y: usize,
43 permute_w: usize,
44 permute_z: usize,
45 ) -> Self;
46
47 fn transform(&self, matrix: &Matrix) -> Self;
48
49 fn min(&self, other: &Self) -> Self;
50 fn max(&self, other: &Self) -> Self;
51
52 fn round(&self) -> Self;
53 fn trunc(&self) -> Self;
54 fn floor(&self) -> Self;
55 fn ceil(&self) -> Self;
56 fn clamp(&self, min: &Self, max: &Self) -> Self;
57
58 fn multiply_add(&self, mul: &Self, add: &Self) -> Self;
59
60 fn splat_x(&self) -> Self;
61 fn splat_y(&self) -> Self;
62 fn splat_z(&self) -> Self;
63 fn splat_w(&self) -> Self;
64}
65
66#[derive(Clone, Copy, PartialEq, Debug)]
67pub struct Vector2 {
68 pub x: f32,
69 pub y: f32,
70 z: f32,
71 w: f32,
72}
73
74#[derive(Clone, Copy, PartialEq, Debug)]
75pub struct Vector3 {
76 pub x: f32,
77 pub y: f32,
78 pub z: f32,
79 w: f32,
80}
81
82#[derive(Clone, Copy, PartialEq, Debug)]
83pub struct Vector4 {
84 pub x: f32,
85 pub y: f32,
86 pub z: f32,
87 pub w: f32,
88}
89
90impl Vector2 {
91 pub fn new(x: f32, y: f32) -> Self {
92 Vector2 {
93 x,
94 y,
95 z: 0.0,
96 w: 0.0,
97 }
98 }
99}
100
101impl Vector3 {
102 pub fn new(x: f32, y: f32, z: f32) -> Self {
103 Vector3 { x, y, z, w: 0.0 }
104 }
105
106 pub fn cross(&self, other: &Self) -> Self {
107 Vector3::new(
108 self.y * other.z - self.z * other.y,
109 self.z * other.x - self.x * other.z,
110 self.x * other.y - self.y * other.x,
111 )
112 }
113}
114
115impl Vector4 {
116 pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
117 Vector4 { x, y, z, w }
118 }
119}
120
121impl Vector for Vector2 {
122 fn zero() -> Self {
123 Self::replicate(0.0)
124 }
125
126 fn one() -> Self {
127 Self::replicate(1.0)
128 }
129
130 fn infinity() -> Self {
131 Self::replicate(f32::INFINITY)
132 }
133
134 fn nan() -> Self {
135 Self::replicate(f32::NAN)
136 }
137
138 fn epsilon() -> Self {
139 Self::replicate(f32::EPSILON)
140 }
141
142 fn replicate(value: f32) -> Self {
143 Self::new(value, value)
144 }
145
146 fn is_nan(&self) -> bool {
147 self.x.is_nan() || self.y.is_nan()
148 }
149
150 fn is_infinite(&self) -> bool {
151 self.x.is_infinite() || self.y.is_infinite()
152 }
153
154 fn dot(&self, other: &Self) -> f32 {
155 self.x * other.x + self.y * other.y
156 }
157
158 fn swizzle(&self, e0: usize, e1: usize, _e2: usize, _e3: usize) -> Self {
159 assert!(e0 < 4);
160 assert!(e1 < 4);
161 Self::new(self[e0], self[e1])
162 }
163
164 fn permute(
165 &self,
166 other: &Self,
167 permute_x: usize,
168 permute_y: usize,
169 _permute_z: usize,
170 _permute_w: usize,
171 ) -> Self {
172 assert!(permute_x < 8);
173 assert!(permute_y < 8);
174 let x = if permute_x < 4 {
175 self[permute_x]
176 } else {
177 other[permute_x - 4]
178 };
179 let y = if permute_y < 4 {
180 self[permute_y]
181 } else {
182 other[permute_y - 4]
183 };
184 Self::new(x, y)
185 }
186
187 fn transform(&self, matrix: &Matrix) -> Self {
188 let x = self.splat_x();
189 let y = self.splat_y();
190
191 let m0 = Self::from(matrix[0]);
192 let m1 = Self::from(matrix[1]);
193 let m3 = Self::from(matrix[3]);
194
195 x * m0 + y * m1 + m3
196 }
197
198 fn min(&self, other: &Self) -> Self {
199 let x = self.x.min(other.x);
200 let y = self.y.min(other.y);
201 Self::new(x, y)
202 }
203 fn max(&self, other: &Self) -> Self {
204 let x = self.x.max(other.x);
205 let y = self.y.max(other.y);
206 Self::new(x, y)
207 }
208
209 fn round(&self) -> Self {
210 let x = self.x.round();
211 let y = self.y.round();
212 Self::new(x, y)
213 }
214 fn trunc(&self) -> Self {
215 let x = self.x.trunc();
216 let y = self.y.trunc();
217 Self::new(x, y)
218 }
219 fn floor(&self) -> Self {
220 let x = self.x.floor();
221 let y = self.y.floor();
222 Self::new(x, y)
223 }
224 fn ceil(&self) -> Self {
225 let x = self.x.ceil();
226 let y = self.y.ceil();
227 Self::new(x, y)
228 }
229 fn clamp(&self, min: &Self, max: &Self) -> Self {
230 assert!(min.x < max.x);
231 assert!(min.y < max.y);
232 self.max(min).min(max)
233 }
234
235 fn multiply_add(&self, mul: &Self, add: &Self) -> Self {
236 *self * *mul + *add
237 }
238
239 fn splat_x(&self) -> Self {
240 Self::replicate(self.x)
241 }
242 fn splat_y(&self) -> Self {
243 Self::replicate(self.y)
244 }
245 fn splat_z(&self) -> Self {
246 Self::replicate(0.0)
247 }
248 fn splat_w(&self) -> Self {
249 Self::replicate(0.0)
250 }
251}
252
253impl Vector for Vector3 {
254 fn zero() -> Self {
255 Self::replicate(0.0)
256 }
257 fn one() -> Self {
258 Self::replicate(1.0)
259 }
260
261 fn infinity() -> Self {
262 Self::replicate(f32::INFINITY)
263 }
264
265 fn nan() -> Self {
266 Self::replicate(f32::NAN)
267 }
268
269 fn epsilon() -> Self {
270 Self::replicate(f32::EPSILON)
271 }
272
273 fn replicate(value: f32) -> Self {
274 Self::new(value, value, value)
275 }
276
277 fn is_nan(&self) -> bool {
278 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
279 }
280
281 fn is_infinite(&self) -> bool {
282 self.x.is_infinite() || self.y.is_infinite() || self.z.is_infinite()
283 }
284
285 fn dot(&self, other: &Self) -> f32 {
286 self.x * other.x + self.y * other.y + self.z * other.z
287 }
288
289 fn swizzle(&self, e0: usize, e1: usize, e2: usize, _e3: usize) -> Self {
290 assert!(e0 < 4);
291 assert!(e1 < 4);
292 assert!(e2 < 4);
293 Self::new(self[e0], self[e1], self[e2])
294 }
295
296 fn permute(
297 &self,
298 other: &Self,
299 permute_x: usize,
300 permute_y: usize,
301 permute_z: usize,
302 _permute_w: usize,
303 ) -> Self {
304 assert!(permute_x < 8);
305 assert!(permute_y < 8);
306 assert!(permute_z < 8);
307 let x = if permute_x < 4 {
308 self[permute_x]
309 } else {
310 other[permute_x - 4]
311 };
312 let y = if permute_y < 4 {
313 self[permute_y]
314 } else {
315 other[permute_y - 4]
316 };
317 let z = if permute_z < 4 {
318 self[permute_z]
319 } else {
320 other[permute_z - 4]
321 };
322 Self::new(x, y, z)
323 }
324
325 fn transform(&self, matrix: &Matrix) -> Self {
326 let x = self.splat_x();
327 let y = self.splat_y();
328 let z = self.splat_z();
329
330 let m0 = Self::from(matrix[0]);
331 let m1 = Self::from(matrix[1]);
332 let m2 = Self::from(matrix[2]);
333 let m3 = Self::from(matrix[3]);
334
335 x * m0 + y * m1 + z * m2 + m3
336 }
337
338 fn min(&self, other: &Self) -> Self {
339 let x = self.x.min(other.x);
340 let y = self.y.min(other.y);
341 let z = self.z.min(other.z);
342 Self::new(x, y, z)
343 }
344 fn max(&self, other: &Self) -> Self {
345 let x = self.x.max(other.x);
346 let y = self.y.max(other.y);
347 let z = self.z.max(other.z);
348 Self::new(x, y, z)
349 }
350
351 fn round(&self) -> Self {
352 let x = self.x.round();
353 let y = self.y.round();
354 let z = self.z.round();
355 Self::new(x, y, z)
356 }
357 fn trunc(&self) -> Self {
358 let x = self.x.trunc();
359 let y = self.y.trunc();
360 let z = self.z.trunc();
361 Self::new(x, y, z)
362 }
363 fn floor(&self) -> Self {
364 let x = self.x.floor();
365 let y = self.y.floor();
366 let z = self.z.floor();
367 Self::new(x, y, z)
368 }
369 fn ceil(&self) -> Self {
370 let x = self.x.ceil();
371 let y = self.y.ceil();
372 let z = self.z.ceil();
373 Self::new(x, y, z)
374 }
375 fn clamp(&self, min: &Self, max: &Self) -> Self {
376 assert!(min.x < max.x);
377 assert!(min.y < max.y);
378 assert!(min.z < max.z);
379 self.max(min).min(max)
380 }
381
382 fn multiply_add(&self, mul: &Self, add: &Self) -> Self {
383 *self * *mul + *add
384 }
385
386 fn splat_x(&self) -> Self {
387 Self::replicate(self.x)
388 }
389 fn splat_y(&self) -> Self {
390 Self::replicate(self.y)
391 }
392 fn splat_z(&self) -> Self {
393 Self::replicate(self.z)
394 }
395 fn splat_w(&self) -> Self {
396 Self::replicate(0.0)
397 }
398}
399
400impl Vector for Vector4 {
401 fn zero() -> Self {
402 Self::replicate(0.0)
403 }
404 fn one() -> Self {
405 Self::replicate(1.0)
406 }
407
408 fn infinity() -> Self {
409 Self::replicate(f32::INFINITY)
410 }
411
412 fn nan() -> Self {
413 Self::replicate(f32::NAN)
414 }
415
416 fn epsilon() -> Self {
417 Self::replicate(f32::EPSILON)
418 }
419
420 fn replicate(value: f32) -> Self {
421 Self::new(value, value, value, value)
422 }
423
424 fn is_nan(&self) -> bool {
425 self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
426 }
427
428 fn is_infinite(&self) -> bool {
429 self.x.is_infinite() || self.y.is_infinite() || self.z.is_infinite() || self.w.is_infinite()
430 }
431
432 fn dot(&self, other: &Self) -> f32 {
433 self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
434 }
435
436 fn swizzle(&self, e0: usize, e1: usize, e2: usize, e3: usize) -> Self {
437 assert!(e0 < 4);
438 assert!(e1 < 4);
439 assert!(e2 < 4);
440 assert!(e3 < 4);
441 Self::new(self[e0], self[e1], self[e2], self[e3])
442 }
443
444 fn permute(
445 &self,
446 other: &Self,
447 permute_x: usize,
448 permute_y: usize,
449 permute_z: usize,
450 permute_w: usize,
451 ) -> Self {
452 assert!(permute_x < 8);
453 assert!(permute_y < 8);
454 assert!(permute_z < 8);
455 assert!(permute_w < 8);
456 let x = if permute_x < 4 {
457 self[permute_x]
458 } else {
459 other[permute_x - 4]
460 };
461 let y = if permute_y < 4 {
462 self[permute_y]
463 } else {
464 other[permute_y - 4]
465 };
466 let z = if permute_z < 4 {
467 self[permute_z]
468 } else {
469 other[permute_z - 4]
470 };
471 let w = if permute_w < 4 {
472 self[permute_w]
473 } else {
474 other[permute_w - 4]
475 };
476 Self::new(x, y, z, w)
477 }
478
479 fn transform(&self, matrix: &Matrix) -> Self {
480 let x = self.splat_x();
481 let y = self.splat_y();
482 let z = self.splat_z();
483 let w = self.splat_w();
484
485 let m0 = Self::from(matrix[0]);
486 let m1 = Self::from(matrix[1]);
487 let m2 = Self::from(matrix[2]);
488 let m3 = Self::from(matrix[3]);
489
490 x * m0 + y * m1 + z * m2 + w * m3
491 }
492
493 fn min(&self, other: &Self) -> Self {
494 let x = self.x.min(other.x);
495 let y = self.y.min(other.y);
496 let z = self.z.min(other.z);
497 let w = self.w.min(other.w);
498 Self::new(x, y, z, w)
499 }
500 fn max(&self, other: &Self) -> Self {
501 let x = self.x.max(other.x);
502 let y = self.y.max(other.y);
503 let z = self.z.max(other.z);
504 let w = self.w.max(other.w);
505 Self::new(x, y, z, w)
506 }
507
508 fn round(&self) -> Self {
509 let x = self.x.round();
510 let y = self.y.round();
511 let z = self.z.round();
512 let w = self.w.round();
513 Self::new(x, y, z, w)
514 }
515 fn trunc(&self) -> Self {
516 let x = self.x.trunc();
517 let y = self.y.trunc();
518 let z = self.z.trunc();
519 let w = self.w.trunc();
520 Self::new(x, y, z, w)
521 }
522 fn floor(&self) -> Self {
523 let x = self.x.floor();
524 let y = self.y.floor();
525 let z = self.z.floor();
526 let w = self.w.floor();
527 Self::new(x, y, z, w)
528 }
529 fn ceil(&self) -> Self {
530 let x = self.x.ceil();
531 let y = self.y.ceil();
532 let z = self.z.ceil();
533 let w = self.w.ceil();
534 Self::new(x, y, z, w)
535 }
536 fn clamp(&self, min: &Self, max: &Self) -> Self {
537 assert!(min.x < max.x);
538 assert!(min.y < max.y);
539 assert!(min.z < max.z);
540 assert!(min.w < max.w);
541 self.max(min).min(max)
542 }
543
544 fn multiply_add(&self, mul: &Self, add: &Self) -> Self {
545 *self * *mul + *add
546 }
547
548 fn splat_x(&self) -> Self {
549 Self::replicate(self.x)
550 }
551 fn splat_y(&self) -> Self {
552 Self::replicate(self.y)
553 }
554 fn splat_z(&self) -> Self {
555 Self::replicate(self.z)
556 }
557 fn splat_w(&self) -> Self {
558 Self::replicate(self.w)
559 }
560}
561
562impl Add for Vector2 {
567 type Output = Self;
568 fn add(self, rhs: Vector2) -> Self::Output {
569 let x = self.x + rhs.x;
570 let y = self.y + rhs.y;
571 Self::new(x, y)
572 }
573}
574impl Add for Vector3 {
575 type Output = Self;
576 fn add(self, rhs: Vector3) -> Self::Output {
577 let x = self.x + rhs.x;
578 let y = self.y + rhs.y;
579 let z = self.z + rhs.z;
580 Self::new(x, y, z)
581 }
582}
583impl Add for Vector4 {
584 type Output = Self;
585 fn add(self, rhs: Vector4) -> Self::Output {
586 let x = self.x + rhs.x;
587 let y = self.y + rhs.y;
588 let z = self.z + rhs.z;
589 let w = self.w + rhs.w;
590 Self::new(x, y, z, w)
591 }
592}
593
594impl AddAssign for Vector2 {
595 fn add_assign(&mut self, rhs: Self) {
596 self.x += rhs.x;
597 self.y += rhs.y;
598 }
599}
600impl AddAssign for Vector3 {
601 fn add_assign(&mut self, rhs: Self) {
602 self.x += rhs.x;
603 self.y += rhs.y;
604 self.z += rhs.z;
605 }
606}
607impl AddAssign for Vector4 {
608 fn add_assign(&mut self, rhs: Self) {
609 self.x += rhs.x;
610 self.y += rhs.y;
611 self.z += rhs.z;
612 self.w += rhs.w;
613 }
614}
615
616impl Sub for Vector2 {
617 type Output = Self;
618 fn sub(self, rhs: Vector2) -> Self::Output {
619 let x = self.x - rhs.x;
620 let y = self.y - rhs.y;
621 Self::new(x, y)
622 }
623}
624impl Sub for Vector3 {
625 type Output = Self;
626 fn sub(self, rhs: Vector3) -> Self::Output {
627 let x = self.x - rhs.x;
628 let y = self.y - rhs.y;
629 let z = self.z - rhs.z;
630 Self::new(x, y, z)
631 }
632}
633impl Sub for Vector4 {
634 type Output = Self;
635 fn sub(self, rhs: Vector4) -> Self::Output {
636 let x = self.x - rhs.x;
637 let y = self.y - rhs.y;
638 let z = self.z - rhs.z;
639 let w = self.w - rhs.w;
640 Self::new(x, y, z, w)
641 }
642}
643
644impl SubAssign for Vector2 {
645 fn sub_assign(&mut self, rhs: Self) {
646 self.x -= rhs.x;
647 self.y -= rhs.y;
648 }
649}
650impl SubAssign for Vector3 {
651 fn sub_assign(&mut self, rhs: Self) {
652 self.x -= rhs.x;
653 self.y -= rhs.y;
654 self.z -= rhs.z;
655 }
656}
657impl SubAssign for Vector4 {
658 fn sub_assign(&mut self, rhs: Self) {
659 self.x -= rhs.x;
660 self.y -= rhs.y;
661 self.z -= rhs.z;
662 self.w -= rhs.w;
663 }
664}
665
666impl Div for Vector2 {
667 type Output = Self;
668 fn div(self, rhs: Vector2) -> Self::Output {
669 let x = self.x / rhs.x;
670 let y = self.y / rhs.y;
671 Self::new(x, y)
672 }
673}
674impl Div for Vector3 {
675 type Output = Self;
676 fn div(self, rhs: Vector3) -> Self::Output {
677 let x = self.x / rhs.x;
678 let y = self.y / rhs.y;
679 let z = self.z / rhs.z;
680 Self::new(x, y, z)
681 }
682}
683impl Div for Vector4 {
684 type Output = Self;
685 fn div(self, rhs: Vector4) -> Self::Output {
686 let x = self.x / rhs.x;
687 let y = self.y / rhs.y;
688 let z = self.z / rhs.z;
689 let w = self.w / rhs.w;
690 Self::new(x, y, z, w)
691 }
692}
693
694impl DivAssign for Vector2 {
695 fn div_assign(&mut self, rhs: Self) {
696 self.x /= rhs.x;
697 self.y /= rhs.y;
698 }
699}
700impl DivAssign for Vector3 {
701 fn div_assign(&mut self, rhs: Self) {
702 self.x /= rhs.x;
703 self.y /= rhs.y;
704 self.z /= rhs.z;
705 }
706}
707impl DivAssign for Vector4 {
708 fn div_assign(&mut self, rhs: Self) {
709 self.x /= rhs.x;
710 self.y /= rhs.y;
711 self.z /= rhs.z;
712 self.w /= rhs.w;
713 }
714}
715
716impl Div<f32> for Vector2 {
717 type Output = Self;
718 fn div(self, rhs: f32) -> Self::Output {
719 let x = self.x / rhs;
720 let y = self.y / rhs;
721 Self::new(x, y)
722 }
723}
724impl Div<f32> for Vector3 {
725 type Output = Self;
726 fn div(self, rhs: f32) -> Self::Output {
727 let x = self.x / rhs;
728 let y = self.y / rhs;
729 let z = self.z / rhs;
730 Self::new(x, y, z)
731 }
732}
733impl Div<f32> for Vector4 {
734 type Output = Self;
735 fn div(self, rhs: f32) -> Self::Output {
736 let x = self.x / rhs;
737 let y = self.y / rhs;
738 let z = self.z / rhs;
739 let w = self.w / rhs;
740 Self::new(x, y, z, w)
741 }
742}
743
744impl DivAssign<f32> for Vector2 {
745 fn div_assign(&mut self, rhs: f32) {
746 self.x /= rhs;
747 self.y /= rhs;
748 }
749}
750impl DivAssign<f32> for Vector3 {
751 fn div_assign(&mut self, rhs: f32) {
752 self.x /= rhs;
753 self.y /= rhs;
754 self.z /= rhs;
755 }
756}
757impl DivAssign<f32> for Vector4 {
758 fn div_assign(&mut self, rhs: f32) {
759 self.x /= rhs;
760 self.y /= rhs;
761 self.z /= rhs;
762 self.w /= rhs;
763 }
764}
765
766impl Mul for Vector2 {
767 type Output = Self;
768 fn mul(self, rhs: Vector2) -> Self::Output {
769 let x = self.x * rhs.x;
770 let y = self.y * rhs.y;
771 Self::new(x, y)
772 }
773}
774impl Mul for Vector3 {
775 type Output = Self;
776 fn mul(self, rhs: Vector3) -> Self::Output {
777 let x = self.x * rhs.x;
778 let y = self.y * rhs.y;
779 let z = self.z * rhs.z;
780 Self::new(x, y, z)
781 }
782}
783impl Mul for Vector4 {
784 type Output = Self;
785 fn mul(self, rhs: Vector4) -> Self::Output {
786 let x = self.x * rhs.x;
787 let y = self.y * rhs.y;
788 let z = self.z * rhs.z;
789 let w = self.w * rhs.w;
790 Self::new(x, y, z, w)
791 }
792}
793
794impl MulAssign for Vector2 {
795 fn mul_assign(&mut self, rhs: Self) {
796 self.x *= rhs.x;
797 self.y *= rhs.y;
798 }
799}
800impl MulAssign for Vector3 {
801 fn mul_assign(&mut self, rhs: Self) {
802 self.x *= rhs.x;
803 self.y *= rhs.y;
804 self.z *= rhs.z;
805 }
806}
807impl MulAssign for Vector4 {
808 fn mul_assign(&mut self, rhs: Self) {
809 self.x *= rhs.x;
810 self.y *= rhs.y;
811 self.z *= rhs.z;
812 self.w *= rhs.w;
813 }
814}
815
816impl Mul<f32> for Vector2 {
817 type Output = Self;
818 fn mul(self, rhs: f32) -> Self::Output {
819 let x = self.x * rhs;
820 let y = self.y * rhs;
821 Self::new(x, y)
822 }
823}
824impl Mul<f32> for Vector3 {
825 type Output = Self;
826 fn mul(self, rhs: f32) -> Self::Output {
827 let x = self.x * rhs;
828 let y = self.y * rhs;
829 let z = self.z * rhs;
830 Self::new(x, y, z)
831 }
832}
833impl Mul<f32> for Vector4 {
834 type Output = Self;
835 fn mul(self, rhs: f32) -> Self::Output {
836 let x = self.x * rhs;
837 let y = self.y * rhs;
838 let z = self.z * rhs;
839 let w = self.w * rhs;
840 Self::new(x, y, z, w)
841 }
842}
843
844impl MulAssign<f32> for Vector2 {
845 fn mul_assign(&mut self, rhs: f32) {
846 self.x *= rhs;
847 self.y *= rhs;
848 }
849}
850impl MulAssign<f32> for Vector3 {
851 fn mul_assign(&mut self, rhs: f32) {
852 self.x *= rhs;
853 self.y *= rhs;
854 self.z *= rhs;
855 }
856}
857impl MulAssign<f32> for Vector4 {
858 fn mul_assign(&mut self, rhs: f32) {
859 self.x *= rhs;
860 self.y *= rhs;
861 self.z *= rhs;
862 self.w *= rhs;
863 }
864}
865
866impl Mul<Vector2> for f32 {
867 type Output = Vector2;
868 fn mul(self, rhs: Vector2) -> Self::Output {
869 let x = self * rhs.x;
870 let y = self * rhs.y;
871 Self::Output::new(x, y)
872 }
873}
874impl Mul<Vector3> for f32 {
875 type Output = Vector3;
876 fn mul(self, rhs: Vector3) -> Self::Output {
877 let x = self * rhs.x;
878 let y = self * rhs.y;
879 let z = self * rhs.z;
880 Self::Output::new(x, y, z)
881 }
882}
883impl Mul<Vector4> for f32 {
884 type Output = Vector4;
885 fn mul(self, rhs: Vector4) -> Self::Output {
886 let x = self * rhs.x;
887 let y = self * rhs.y;
888 let z = self * rhs.z;
889 let w = self * rhs.w;
890 Self::Output::new(x, y, z, w)
891 }
892}
893
894impl Neg for Vector2 {
895 type Output = Self;
896 fn neg(self) -> Self::Output {
897 let x = -self.x;
898 let y = -self.y;
899 Self::new(x, y)
900 }
901}
902impl Neg for Vector3 {
903 type Output = Self;
904 fn neg(self) -> Self::Output {
905 let x = -self.x;
906 let y = -self.y;
907 let z = -self.z;
908 Self::new(x, y, z)
909 }
910}
911impl Neg for Vector4 {
912 type Output = Self;
913 fn neg(self) -> Self::Output {
914 let x = -self.x;
915 let y = -self.y;
916 let z = -self.z;
917 let w = -self.w;
918 Self::new(x, y, z, w)
919 }
920}
921
922const ZERO: &f32 = &0.0;
923
924impl Index<usize> for Vector2 {
925 type Output = f32;
926 fn index(&self, index: usize) -> &Self::Output {
927 match index {
928 0 => &self.x,
929 1 => &self.y,
930 2 => ZERO,
931 3 => ZERO,
932 _ => panic!("index must be between 0~3, but {}", index),
933 }
934 }
935}
936impl Index<usize> for Vector3 {
937 type Output = f32;
938 fn index(&self, index: usize) -> &Self::Output {
939 match index {
940 0 => &self.x,
941 1 => &self.y,
942 2 => &self.z,
943 3 => ZERO,
944 _ => panic!("index must be between 0~3, but {}", index),
945 }
946 }
947}
948impl Index<usize> for Vector4 {
949 type Output = f32;
950 fn index(&self, index: usize) -> &Self::Output {
951 match index {
952 0 => &self.x,
953 1 => &self.y,
954 2 => &self.z,
955 3 => &self.w,
956 _ => panic!("index must be between 0~3, but {}", index),
957 }
958 }
959}
960
961impl From<Row> for Vector2 {
962 fn from(row: Row) -> Self {
963 Self::new(row[0], row[1])
964 }
965}
966
967impl From<Row> for Vector3 {
968 fn from(row: Row) -> Self {
969 Self::new(row[0], row[1], row[2])
970 }
971}
972
973impl From<Row> for Vector4 {
974 fn from(row: Row) -> Self {
975 Self::new(row[0], row[1], row[2], row[3])
976 }
977}
978
979#[cfg(feature = "glium-support")]
980mod glium_support {
981 use super::{Vector2, Vector3, Vector4};
982 use glium::vertex::{Attribute, AttributeType};
983
984 unsafe impl Attribute for Vector2 {
985 fn get_type() -> AttributeType {
986 AttributeType::F32F32
987 }
988 }
989 unsafe impl Attribute for Vector3 {
990 fn get_type() -> AttributeType {
991 AttributeType::F32F32F32
992 }
993 }
994 unsafe impl Attribute for Vector4 {
995 fn get_type() -> AttributeType {
996 AttributeType::F32F32F32F32
997 }
998 }
999}