1use crate::numtraits::{One, Zero};
4use crate::Real;
5use std::mem::transmute;
6use std::ops::*;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[repr(C)]
11pub struct Vector2<T>(pub T, pub T);
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[repr(C)]
15pub struct Vector3<T>(pub T, pub T, pub T);
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18#[repr(C)]
19pub struct Vector4<T>(pub T, pub T, pub T, pub T);
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22#[repr(C)]
23pub struct Quaternion<T>(pub T, pub T, pub T, pub T);
24
25pub type Vector2F32 = Vector2<f32>;
27pub type Vector3F32 = Vector3<f32>;
29pub type Vector4F32 = Vector4<f32>;
31pub type QuaternionF32 = Quaternion<f32>;
33
34#[derive(Debug, Clone, PartialEq, Eq, Hash)]
36#[repr(C)]
37pub struct Matrix2<T>(pub [T; 2], pub [T; 2]);
38#[derive(Debug, Clone, PartialEq, Eq, Hash)]
40#[repr(C)]
41pub struct Matrix3<T>(pub [T; 3], pub [T; 3], pub [T; 3]);
42#[derive(Debug, Clone, PartialEq, Eq, Hash)]
44#[repr(C)]
45pub struct Matrix4<T>(pub [T; 4], pub [T; 4], pub [T; 4], pub [T; 4]);
46#[derive(Debug, Clone, PartialEq, Eq, Hash)]
48#[repr(C)]
49pub struct Matrix2x3<T>(pub [T; 3], pub [T; 3]);
50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
52#[repr(C)]
53pub struct Matrix3x4<T>(pub [T; 4], pub [T; 4], pub [T; 4]);
54
55pub type Matrix2F32 = Matrix2<f32>;
57pub type Matrix3F32 = Matrix3<f32>;
59pub type Matrix4F32 = Matrix4<f32>;
61pub type Matrix2x3F32 = Matrix2x3<f32>;
63pub type Matrix3x4F32 = Matrix3x4<f32>;
65
66impl<T> Vector2<T> {
67 pub const fn x(&self) -> &T {
68 &self.0
69 }
70
71 pub const fn y(&self) -> &T {
72 &self.1
73 }
74}
75impl<T> Vector3<T> {
76 pub const fn x(&self) -> &T {
77 &self.0
78 }
79
80 pub const fn y(&self) -> &T {
81 &self.1
82 }
83
84 pub const fn z(&self) -> &T {
85 &self.2
86 }
87}
88impl<T> Vector4<T> {
89 pub const fn x(&self) -> &T {
90 &self.0
91 }
92
93 pub const fn y(&self) -> &T {
94 &self.1
95 }
96
97 pub const fn z(&self) -> &T {
98 &self.2
99 }
100
101 pub const fn w(&self) -> &T {
102 &self.3
103 }
104}
105
106impl<T: Zero> Zero for Vector2<T> {
108 const ZERO: Self = Vector2(T::ZERO, T::ZERO);
109}
110impl<T: One> One for Vector2<T> {
111 const ONE: Self = Vector2(T::ONE, T::ONE);
112}
113impl<T: Zero> Zero for Vector3<T> {
114 const ZERO: Self = Vector3(T::ZERO, T::ZERO, T::ZERO);
115}
116impl<T: One> One for Vector3<T> {
117 const ONE: Self = Vector3(T::ONE, T::ONE, T::ONE);
118}
119impl<T: Zero> Zero for Vector4<T> {
120 const ZERO: Self = Vector4(T::ZERO, T::ZERO, T::ZERO, T::ZERO);
121}
122impl<T: One> One for Vector4<T> {
123 const ONE: Self = Vector4(T::ONE, T::ONE, T::ONE, T::ONE);
124}
125
126impl<T: Zero + One> Vector2<T> {
128 pub fn left() -> Self
129 where
130 Self: Neg<Output = Self>,
131 {
132 -Self::right()
133 }
134
135 pub fn down() -> Self
136 where
137 Self: Neg<Output = Self>,
138 {
139 -Self::up()
140 }
141
142 pub const fn right() -> Self {
143 Self(T::ONE, T::ZERO)
144 }
145
146 pub const fn up() -> Self {
147 Self(T::ZERO, T::ONE)
148 }
149}
150
151impl<T: Zero + One> Vector3<T> {
152 pub fn left() -> Self
153 where
154 Self: Neg<Output = Self>,
155 {
156 -Self::right()
157 }
158
159 pub const fn right() -> Self {
160 Self(T::ONE, T::ZERO, T::ZERO)
161 }
162
163 pub fn down() -> Self
164 where
165 Self: Neg<Output = Self>,
166 {
167 -Self::up()
168 }
169
170 pub const fn up() -> Self {
171 Self(T::ZERO, T::ONE, T::ZERO)
172 }
173
174 pub fn back() -> Self
175 where
176 Self: Neg<Output = Self>,
177 {
178 -Self::forward()
179 }
180
181 pub const fn forward() -> Self {
182 Self(T::ZERO, T::ZERO, T::ONE)
183 }
184}
185
186impl<T: Div<T> + Copy> From<Vector4<T>> for Vector3<<T as Div>::Output> {
190 fn from(Vector4(x, y, z, w): Vector4<T>) -> Self {
191 Vector3(x / w, y / w, z / w)
192 }
193}
194
195impl<T> Vector2<T> {
196 pub fn with_z(self, z: T) -> Vector3<T> {
197 Vector3(self.0, self.1, z)
198 }
199}
200impl<T> Vector3<T> {
201 pub fn with_w(self, w: T) -> Vector4<T> {
202 Vector4(self.0, self.1, self.2, w)
203 }
204}
205
206impl<T> AsRef<[T; 2]> for Vector2<T> {
208 fn as_ref(&self) -> &[T; 2] {
209 unsafe { transmute(self) }
210 }
211}
212impl<T> AsRef<[T; 3]> for Vector3<T> {
213 fn as_ref(&self) -> &[T; 3] {
214 unsafe { transmute(self) }
215 }
216}
217impl<T> AsRef<[T; 4]> for Vector4<T> {
218 fn as_ref(&self) -> &[T; 4] {
219 unsafe { transmute(self) }
220 }
221}
222impl<T> AsRef<[T; 4]> for Quaternion<T> {
223 fn as_ref(&self) -> &[T; 4] {
224 unsafe { transmute(self) }
225 }
226}
227
228impl<T: Zero + One> One for Matrix2<T> {
230 const ONE: Self = Matrix2([T::ONE, T::ZERO], [T::ZERO, T::ONE]);
231}
232impl<T: Zero + One> One for Matrix3<T> {
233 const ONE: Self = Matrix3(
234 [T::ONE, T::ZERO, T::ZERO],
235 [T::ZERO, T::ONE, T::ZERO],
236 [T::ZERO, T::ZERO, T::ONE],
237 );
238}
239impl<T: Zero + One> One for Matrix4<T> {
240 const ONE: Self = Matrix4(
241 [T::ONE, T::ZERO, T::ZERO, T::ZERO],
242 [T::ZERO, T::ONE, T::ZERO, T::ZERO],
243 [T::ZERO, T::ZERO, T::ONE, T::ZERO],
244 [T::ZERO, T::ZERO, T::ZERO, T::ONE],
245 );
246}
247impl<T: Zero + One> One for Matrix2x3<T> {
248 const ONE: Self = Matrix2x3([T::ONE, T::ZERO, T::ZERO], [T::ZERO, T::ONE, T::ZERO]);
249}
250impl<T: Zero + One> One for Matrix3x4<T> {
251 const ONE: Self = Matrix3x4(
252 [T::ONE, T::ZERO, T::ZERO, T::ZERO],
253 [T::ZERO, T::ONE, T::ZERO, T::ZERO],
254 [T::ZERO, T::ZERO, T::ONE, T::ZERO],
255 );
256}
257
258impl<T> Matrix2<T> {
260 #[inline(always)]
261 pub fn transpose(self) -> Self {
262 let Self([x00, x01], [x10, x11]) = self;
263 Self([x00, x10], [x01, x11])
264 }
265}
266impl<T> Matrix3<T> {
267 #[inline(always)]
268 pub fn transpose(self) -> Self {
269 let Self([x00, x01, x02], [x10, x11, x12], [x20, x21, x22]) = self;
270 Self([x00, x10, x20], [x01, x11, x21], [x02, x12, x22])
271 }
272}
273impl<T> Matrix4<T> {
274 #[inline(always)]
275 pub fn transpose(self) -> Self {
276 let Self(
277 [x00, x01, x02, x03],
278 [x10, x11, x12, x13],
279 [x20, x21, x22, x23],
280 [x30, x31, x32, x33],
281 ) = self;
282 Self(
283 [x00, x10, x20, x30],
284 [x01, x11, x21, x31],
285 [x02, x12, x22, x32],
286 [x03, x13, x23, x33],
287 )
288 }
289}
290
291impl<T: Zero + One> From<Matrix2<T>> for Matrix3<T> {
293 fn from(Matrix2([a, b], [c, d]): Matrix2<T>) -> Self {
294 Matrix3([a, b, T::ZERO], [c, d, T::ZERO], [T::ZERO, T::ZERO, T::ONE])
295 }
296}
297impl<T: Zero + One> From<Matrix3<T>> for Matrix4<T> {
298 fn from(Matrix3([a, b, c], [d, e, f], [g, h, i]): Matrix3<T>) -> Self {
299 Matrix4(
300 [a, b, c, T::ZERO],
301 [d, e, f, T::ZERO],
302 [g, h, i, T::ZERO],
303 [T::ZERO, T::ZERO, T::ZERO, T::ONE],
304 )
305 }
306}
307
308impl<T: Copy> Mul for Matrix2<T>
310where
311 T: Mul<T>,
312 <T as Mul>::Output: Add,
313{
314 type Output = Matrix2<<<T as Mul>::Output as Add>::Output>;
315 fn mul(self, other: Matrix2<T>) -> Self::Output {
316 let dp =
317 |src: &[T; 2], colindex: usize| src[0] * other.0[colindex] + src[1] * other.1[colindex];
318
319 Matrix2(
320 [dp(&self.0, 0), dp(&self.0, 1)],
321 [dp(&self.1, 0), dp(&self.1, 1)],
322 )
323 }
324}
325impl<T: Copy> Mul for Matrix3<T>
326where
327 T: Mul<T>,
328 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
329{
330 type Output = Matrix3<<T as Mul>::Output>;
331
332 fn mul(self, other: Matrix3<T>) -> Self::Output {
333 let dp = |src: &[T; 3], colindex: usize| {
334 src[0] * other.0[colindex] + src[1] * other.1[colindex] + src[2] * other.2[colindex]
335 };
336
337 Matrix3(
338 [dp(&self.0, 0), dp(&self.0, 1), dp(&self.0, 2)],
339 [dp(&self.1, 0), dp(&self.1, 1), dp(&self.1, 2)],
340 [dp(&self.2, 0), dp(&self.2, 1), dp(&self.2, 2)],
341 )
342 }
343}
344impl<T: Copy> Mul for Matrix4<T>
345where
346 T: Mul<T>,
347 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
348{
349 type Output = Matrix4<<T as Mul>::Output>;
350
351 fn mul(self, other: Matrix4<T>) -> Self::Output {
352 let dp = |src: &[T; 4], colindex: usize| {
353 src[0] * other.0[colindex]
354 + src[1] * other.1[colindex]
355 + src[2] * other.2[colindex]
356 + src[3] * other.3[colindex]
357 };
358
359 Matrix4(
360 [
361 dp(&self.0, 0),
362 dp(&self.0, 1),
363 dp(&self.0, 2),
364 dp(&self.0, 3),
365 ],
366 [
367 dp(&self.1, 0),
368 dp(&self.1, 1),
369 dp(&self.1, 2),
370 dp(&self.1, 3),
371 ],
372 [
373 dp(&self.2, 0),
374 dp(&self.2, 1),
375 dp(&self.2, 2),
376 dp(&self.2, 3),
377 ],
378 [
379 dp(&self.3, 0),
380 dp(&self.3, 1),
381 dp(&self.3, 2),
382 dp(&self.3, 3),
383 ],
384 )
385 }
386}
387
388fn det2<T>(v: &[[T; 2]; 2]) -> T
389where
390 T: Copy + Mul<T, Output = T> + Sub<T, Output = T>,
391{
392 v[0][0] * v[1][1] - v[0][1] * v[1][0]
393}
394
395fn det3<T>(v: &[[T; 3]; 3]) -> T
396where
397 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
398{
399 v[0][0] * det2(&[[v[1][1], v[1][2]], [v[2][1], v[2][2]]])
400 - v[0][1] * det2(&[[v[1][0], v[1][2]], [v[2][0], v[2][2]]])
401 + v[0][2] * det2(&[[v[1][0], v[1][1]], [v[2][0], v[2][1]]])
402}
403
404fn det4<T>(v: &[[T; 4]; 4]) -> T
405where
406 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
407{
408 v[0][0]
409 * det3(&[
410 [v[1][1], v[1][2], v[1][3]],
411 [v[2][1], v[2][2], v[2][3]],
412 [v[3][1], v[3][2], v[3][3]],
413 ])
414 - v[0][1]
415 * det3(&[
416 [v[1][0], v[1][2], v[1][3]],
417 [v[2][0], v[2][2], v[2][3]],
418 [v[3][0], v[3][2], v[3][3]],
419 ])
420 + v[0][2]
421 * det3(&[
422 [v[1][0], v[1][1], v[1][3]],
423 [v[2][0], v[2][1], v[2][3]],
424 [v[3][0], v[3][1], v[3][3]],
425 ])
426 - v[0][3]
427 * det3(&[
428 [v[1][0], v[1][1], v[1][2]],
429 [v[2][0], v[2][1], v[2][2]],
430 [v[3][0], v[3][1], v[3][2]],
431 ])
432}
433
434impl<T> Matrix2<T> {
436 #[inline(always)]
437 pub fn determinant(&self) -> T
438 where
439 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
440 {
441 det2(&[self.0, self.1])
442 }
443}
444impl<T> Matrix3<T> {
445 #[inline(always)]
446 pub fn determinant(&self) -> T
447 where
448 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
449 {
450 det3(&[self.0, self.1, self.2])
451 }
452}
453impl<T> Matrix4<T> {
454 #[inline(always)]
455 pub fn determinant(&self) -> T
456 where
457 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
458 {
459 det4(&[self.0, self.1, self.2, self.3])
460 }
461
462 pub fn minor(&self) -> Self
464 where
465 T: Copy + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T>,
466 {
467 let m00 = det3(&[
468 [self.1[1], self.1[2], self.1[3]],
469 [self.2[1], self.2[2], self.2[3]],
470 [self.3[1], self.3[2], self.3[3]],
471 ]);
472 let m01 = det3(&[
473 [self.1[0], self.1[2], self.1[3]],
474 [self.2[0], self.2[2], self.2[3]],
475 [self.3[0], self.3[2], self.3[3]],
476 ]);
477 let m02 = det3(&[
478 [self.1[0], self.1[1], self.1[3]],
479 [self.2[0], self.2[1], self.2[3]],
480 [self.3[0], self.3[1], self.3[3]],
481 ]);
482 let m03 = det3(&[
483 [self.1[0], self.1[1], self.1[2]],
484 [self.2[0], self.2[1], self.2[2]],
485 [self.3[0], self.3[1], self.3[2]],
486 ]);
487 let m10 = det3(&[
488 [self.0[1], self.0[2], self.0[3]],
489 [self.2[1], self.2[2], self.2[3]],
490 [self.3[1], self.3[2], self.3[3]],
491 ]);
492 let m11 = det3(&[
493 [self.0[0], self.0[2], self.0[3]],
494 [self.2[0], self.2[2], self.2[3]],
495 [self.3[0], self.3[2], self.3[3]],
496 ]);
497 let m12 = det3(&[
498 [self.0[0], self.0[1], self.0[3]],
499 [self.2[0], self.2[1], self.2[3]],
500 [self.3[0], self.3[1], self.3[3]],
501 ]);
502 let m13 = det3(&[
503 [self.0[0], self.0[1], self.0[2]],
504 [self.2[0], self.2[1], self.2[2]],
505 [self.3[0], self.3[1], self.3[2]],
506 ]);
507 let m20 = det3(&[
508 [self.0[1], self.0[2], self.0[3]],
509 [self.1[1], self.1[2], self.1[3]],
510 [self.3[1], self.3[2], self.3[3]],
511 ]);
512 let m21 = det3(&[
513 [self.0[0], self.0[2], self.0[3]],
514 [self.1[0], self.1[2], self.1[3]],
515 [self.3[0], self.3[2], self.3[3]],
516 ]);
517 let m22 = det3(&[
518 [self.0[0], self.0[1], self.0[3]],
519 [self.1[0], self.1[1], self.1[3]],
520 [self.3[0], self.3[1], self.3[3]],
521 ]);
522 let m23 = det3(&[
523 [self.0[0], self.0[1], self.0[2]],
524 [self.1[0], self.1[1], self.1[2]],
525 [self.3[0], self.3[1], self.3[2]],
526 ]);
527 let m30 = det3(&[
528 [self.0[1], self.0[2], self.0[3]],
529 [self.1[1], self.1[2], self.1[3]],
530 [self.2[1], self.2[2], self.2[3]],
531 ]);
532 let m31 = det3(&[
533 [self.0[0], self.0[2], self.0[3]],
534 [self.1[0], self.1[2], self.1[3]],
535 [self.2[0], self.2[2], self.2[3]],
536 ]);
537 let m32 = det3(&[
538 [self.0[0], self.0[1], self.0[3]],
539 [self.1[0], self.1[1], self.1[3]],
540 [self.2[0], self.2[1], self.2[3]],
541 ]);
542 let m33 = det3(&[
543 [self.0[0], self.0[1], self.0[2]],
544 [self.1[0], self.1[1], self.1[2]],
545 [self.2[0], self.2[1], self.2[2]],
546 ]);
547
548 Self(
549 [m00, m01, m02, m03],
550 [m10, m11, m12, m13],
551 [m20, m21, m22, m23],
552 [m30, m31, m32, m33],
553 )
554 }
555
556 pub fn inverse(&self) -> Option<Self>
557 where
558 T: Copy
559 + Mul<T, Output = T>
560 + Sub<T, Output = T>
561 + Add<T, Output = T>
562 + Div<T, Output = T>
563 + Neg<Output = T>
564 + PartialEq<T>
565 + Zero,
566 {
567 let det = self.determinant();
568 if det == T::ZERO {
569 return None;
571 }
572
573 let minor = self.minor();
574 let cofactor = Self(
575 [minor.0[0], -minor.0[1], minor.0[2], -minor.0[3]],
576 [-minor.1[0], minor.1[1], -minor.1[2], minor.1[3]],
577 [minor.2[0], -minor.2[1], minor.2[2], -minor.2[3]],
578 [-minor.3[0], minor.3[1], -minor.3[2], minor.3[3]],
579 );
580 let adjugate = cofactor.transpose();
581 Some(Self(
582 [
583 adjugate.0[0] / det,
584 adjugate.0[1] / det,
585 adjugate.0[2] / det,
586 adjugate.0[3] / det,
587 ],
588 [
589 adjugate.1[0] / det,
590 adjugate.1[1] / det,
591 adjugate.1[2] / det,
592 adjugate.1[3] / det,
593 ],
594 [
595 adjugate.2[0] / det,
596 adjugate.2[1] / det,
597 adjugate.2[2] / det,
598 adjugate.2[3] / det,
599 ],
600 [
601 adjugate.3[0] / det,
602 adjugate.3[1] / det,
603 adjugate.3[2] / det,
604 adjugate.3[3] / det,
605 ],
606 ))
607 }
608}
609
610impl<T> Matrix2<T> {
612 pub fn scale(Vector2(x, y): Vector2<T>) -> Self
613 where
614 T: Zero,
615 {
616 Matrix2([x, T::ZERO], [T::ZERO, y])
617 }
618
619 pub fn rotate(rad: T) -> Self
620 where
621 T: Real + Copy + Neg<Output = T>,
622 {
623 let (s, c) = rad.sin_cos();
624 Matrix2([c, -s], [s, c])
625 }
626}
627
628impl<T> Matrix3<T> {
630 #[inline(always)]
631 pub fn scale(Vector3(x, y, z): Vector3<T>) -> Self
632 where
633 T: Zero,
634 {
635 Matrix3(
636 [x, T::ZERO, T::ZERO],
637 [T::ZERO, y, T::ZERO],
638 [T::ZERO, T::ZERO, z],
639 )
640 }
641
642 #[inline(always)]
643 pub fn translation(Vector2(x, y): Vector2<T>) -> Self
644 where
645 T: One + Zero,
646 {
647 Matrix3(
648 [T::ONE, T::ZERO, x],
649 [T::ZERO, T::ONE, y],
650 [T::ZERO, T::ZERO, T::ONE],
651 )
652 }
653}
654impl<T> Matrix4<T> {
655 #[inline(always)]
656 pub fn scale(Vector4(x, y, z, w): Vector4<T>) -> Self
657 where
658 T: Zero,
659 {
660 Matrix4(
661 [x, T::ZERO, T::ZERO, T::ZERO],
662 [T::ZERO, y, T::ZERO, T::ZERO],
663 [T::ZERO, T::ZERO, z, T::ZERO],
664 [T::ZERO, T::ZERO, T::ZERO, w],
665 )
666 }
667
668 #[inline(always)]
669 pub fn translation(Vector3(x, y, z): Vector3<T>) -> Self
670 where
671 T: One + Zero,
672 {
673 Matrix4(
674 [T::ONE, T::ZERO, T::ZERO, x],
675 [T::ZERO, T::ONE, T::ZERO, y],
676 [T::ZERO, T::ZERO, T::ONE, z],
677 [T::ZERO, T::ZERO, T::ZERO, T::ONE],
678 )
679 }
680}
681
682impl<T> Matrix4<T> {
684 pub fn trs(translate: Vector3<T>, rotate: Quaternion<T>, scale: Vector3<T>) -> Self
685 where
686 T: One + Zero + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T> + Copy,
687 {
688 Matrix4::translation(translate)
689 * Matrix4::from(rotate)
690 * Matrix4::scale(scale.with_w(T::ONE))
691 }
692}
693
694fn dotproduct2<T: Mul + Copy>(a: &[T; 2], b: &[T; 2]) -> <T as Mul>::Output
695where
696 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
697{
698 a[0] * b[0] + a[1] * b[1]
699}
700fn dotproduct3<T: Mul + Copy>(a: &[T; 3], b: &[T; 3]) -> <T as Mul>::Output
701where
702 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
703{
704 a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
705}
706fn dotproduct4<T: Mul + Copy>(a: &[T; 4], b: &[T; 4]) -> <T as Mul>::Output
707where
708 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
709{
710 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
711}
712
713impl<T: Mul + Copy> Mul<Vector2<T>> for Matrix2<T>
715where
716 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
717{
718 type Output = Vector2<<T as Mul>::Output>;
719 fn mul(self, v: Vector2<T>) -> Self::Output {
720 Vector2(
721 dotproduct2(v.as_ref(), &self.0),
722 dotproduct2(v.as_ref(), &self.1),
723 )
724 }
725}
726impl<T: Mul + Copy> Mul<Vector3<T>> for Matrix3<T>
727where
728 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
729{
730 type Output = Vector3<<T as Mul>::Output>;
731 fn mul(self, v: Vector3<T>) -> Self::Output {
732 Vector3(
733 dotproduct3(v.as_ref(), &self.0),
734 dotproduct3(v.as_ref(), &self.1),
735 dotproduct3(v.as_ref(), &self.2),
736 )
737 }
738}
739impl<T: Mul + Copy> Mul<Vector4<T>> for Matrix4<T>
740where
741 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
742{
743 type Output = Vector4<<T as Mul>::Output>;
744 fn mul(self, v: Vector4<T>) -> Self::Output {
745 Vector4(
746 dotproduct4(v.as_ref(), &self.0),
747 dotproduct4(v.as_ref(), &self.1),
748 dotproduct4(v.as_ref(), &self.2),
749 dotproduct4(v.as_ref(), &self.3),
750 )
751 }
752}
753impl<T: Mul + One + Copy> Mul<Vector2<T>> for Matrix2x3<T>
754where
755 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
756{
757 type Output = Vector2<<T as Mul>::Output>;
758 fn mul(self, v: Vector2<T>) -> Self::Output {
759 let v3 = v.with_z(T::ONE);
760 Vector2(
761 dotproduct3(v3.as_ref(), &self.0),
762 dotproduct3(v3.as_ref(), &self.1),
763 )
764 }
765}
766impl<T: Mul + One + Copy> Mul<Vector3<T>> for Matrix3x4<T>
767where
768 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
769{
770 type Output = Vector3<<T as Mul>::Output>;
771 fn mul(self, v: Vector3<T>) -> Self::Output {
772 let v4 = v.with_w(T::ONE);
773 Vector3(
774 dotproduct4(v4.as_ref(), &self.0),
775 dotproduct4(v4.as_ref(), &self.1),
776 dotproduct4(v4.as_ref(), &self.2),
777 )
778 }
779}
780impl<T: Mul + One + Copy> Mul<Vector2<T>> for Matrix3<T>
782where
783 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
784{
785 type Output = <Matrix3<T> as Mul<Vector3<T>>>::Output;
786 fn mul(self, v: Vector2<T>) -> Self::Output {
787 self * v.with_z(T::ONE)
788 }
789}
790impl<T: Mul + One + Copy> Mul<Vector2<T>> for Matrix4<T>
791where
792 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
793{
794 type Output = <Matrix4<T> as Mul<Vector4<T>>>::Output;
795 fn mul(self, v: Vector2<T>) -> Self::Output {
796 self * v.with_z(T::ONE)
797 }
798}
799impl<T: Mul + One + Copy> Mul<Vector3<T>> for Matrix4<T>
800where
801 <T as Mul>::Output: Add<Output = <T as Mul>::Output>,
802{
803 type Output = <Matrix4<T> as Mul<Vector4<T>>>::Output;
804 fn mul(self, v: Vector3<T>) -> Self::Output {
805 self * v.with_w(T::ONE)
806 }
807}
808
809impl<T> Vector2<T>
811where
812 T: Real + Copy,
813{
814 #[inline]
815 pub fn len(&self) -> T {
816 self.len2().sqrt()
817 }
818
819 #[inline]
820 pub fn normalize(&self) -> Self {
821 let l0 = self.len();
822 Vector2(self.0 / l0, self.1 / l0)
823 }
824}
825impl<T> Vector3<T>
826where
827 T: Real + Copy,
828{
829 #[inline]
830 pub fn len(&self) -> T {
831 self.len2().sqrt()
832 }
833
834 #[inline]
835 pub fn normalize(&self) -> Self {
836 let l0 = self.len();
837 Vector3(self.0 / l0, self.1 / l0, self.2 / l0)
838 }
839}
840impl<T> Vector4<T>
841where
842 T: Real + Copy,
843{
844 #[inline]
845 pub fn len(&self) -> T {
846 self.len2().sqrt()
847 }
848
849 #[inline]
850 pub fn normalize(&self) -> Self {
851 let l0 = self.len();
852 Vector4(self.0 / l0, self.1 / l0, self.2 / l0, self.3 / l0)
853 }
854}
855
856impl<T> Vector2<T>
858where
859 T: Copy + Div<T, Output = T> + PartialEq + Zero,
860{
861 #[inline(always)]
862 pub fn aspect_wh(&self) -> T {
863 if self.1 == T::ZERO {
864 T::ZERO
865 } else {
866 self.0 / self.1
867 }
868 }
869
870 #[inline(always)]
871 pub fn aspect_hw(&self) -> T {
872 if self.0 == T::ZERO {
873 T::ZERO
874 } else {
875 self.1 / self.0
876 }
877 }
878}
879
880impl<T: One + Zero> One for Quaternion<T> {
882 const ONE: Self = Quaternion(T::ZERO, T::ZERO, T::ZERO, T::ONE);
883}
884
885impl<T> Quaternion<T> {
886 pub fn new(rad: T, axis: Vector3<T>) -> Self
888 where
889 T: Div<T, Output = T> + Real + One + Copy + Add<T, Output = T> + Mul<T, Output = T>,
890 {
891 let (s, c) = (rad / (T::ONE + T::ONE)).sin_cos();
892 let axis = axis.normalize();
893
894 Self(axis.0 * s, axis.1 * s, axis.2 * s, c)
895 }
896
897 pub fn from_euler_zyx(euler: Vector3<T>) -> Self
899 where
900 T: Copy + Real + One,
901 {
902 let (sz, cz) = (euler.2 / (T::ONE + T::ONE)).sin_cos();
953 let (sy, cy) = (euler.1 / (T::ONE + T::ONE)).sin_cos();
954 let (sx, cx) = (euler.0 / (T::ONE + T::ONE)).sin_cos();
955
956 Self(
957 cz * cy * sx - sz * sy * cx,
958 cz * sy * cx + sz * cy * sx,
959 sz * cy * cx - cz * sy * sx,
960 cz * cy * cx + sz * sy * sx,
961 )
962 }
963
964 pub fn lerp(&self, other: &Self, t: T) -> Self
966 where
967 T: Real
968 + Copy
969 + Mul<T, Output = T>
970 + Add<T, Output = T>
971 + Div<T, Output = T>
972 + Sub<T, Output = T>
973 + One,
974 {
975 let omg = self.angle(other);
976 let (fa, fb) = (
977 (omg * (T::ONE - t)).sin() / omg.sin(),
978 (omg * t).sin() / omg.sin(),
979 );
980
981 Self(
982 fa * self.0 + fb * other.0,
983 fa * self.1 + fb * other.1,
984 fa * self.2 + fb * other.2,
985 fa * self.3 + fb * other.3,
986 )
987 }
988
989 #[inline(always)]
991 pub fn angle(&self, other: &Self) -> T
992 where
993 T: Real + Copy + Mul<T, Output = T> + Add<T, Output = T>,
994 {
995 dotproduct4(self.as_ref(), other.as_ref()).acos()
996 }
997
998 #[inline(always)]
1000 pub fn normalize(self) -> Self
1001 where
1002 T: Copy + Div<T, Output = T> + Mul<T, Output = T> + Add<T, Output = T> + Real,
1003 {
1004 let d = (self.0 * self.0 + self.1 * self.1 + self.2 * self.2 + self.3 * self.3).sqrt();
1005
1006 Self(self.0 / d, self.1 / d, self.2 / d, self.3 / d)
1007 }
1008}
1009
1010impl<T> Mul for Quaternion<T>
1011where
1012 T: Mul<T, Output = T> + Add<T, Output = T> + Sub<T, Output = T> + Copy,
1013{
1014 type Output = Quaternion<T>;
1015
1016 fn mul(self, Quaternion(x2, y2, z2, w2): Quaternion<T>) -> Self::Output {
1017 let Quaternion(x1, y1, z1, w1) = self;
1018
1019 let x0 = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2;
1020 let y0 = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2;
1021 let z0 = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2;
1022 let w0 = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;
1023
1024 Quaternion(x0, y0, z0, w0)
1025 }
1026}
1027
1028impl<T: Neg<Output = T>> Neg for Quaternion<T> {
1030 type Output = Quaternion<T>;
1031 fn neg(self) -> Self {
1032 Quaternion(-self.0, -self.1, -self.2, self.3)
1033 }
1034}
1035
1036impl<T> From<Quaternion<T>> for Matrix3<T>
1037where
1038 T: One + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T> + Copy,
1039{
1040 fn from(Quaternion(x, y, z, w): Quaternion<T>) -> Self {
1041 let two = T::ONE + T::ONE;
1042 let m11 = T::ONE - two * y * y - two * z * z;
1043 let m22 = T::ONE - two * x * x - two * z * z;
1044 let m33 = T::ONE - two * x * x - two * y * y;
1045 let m12 = two * (x * y + w * z);
1046 let m13 = two * (x * z - w * y);
1047 let m21 = two * (x * y - w * z);
1048 let m23 = two * (y * z + w * x);
1049 let m31 = two * (x * z + w * y);
1050 let m32 = two * (y * z - w * x);
1051
1052 Matrix3([m11, m12, m13], [m21, m22, m23], [m31, m32, m33])
1053 }
1054}
1055impl<T> From<Quaternion<T>> for Matrix4<T>
1056where
1057 T: One + Zero + Mul<T, Output = T> + Sub<T, Output = T> + Add<T, Output = T> + Copy,
1058{
1059 fn from(Quaternion(x, y, z, w): Quaternion<T>) -> Self {
1060 let two = T::ONE + T::ONE;
1061 let m11 = T::ONE - two * y * y - two * z * z;
1062 let m22 = T::ONE - two * x * x - two * z * z;
1063 let m33 = T::ONE - two * x * x - two * y * y;
1064 let m12 = two * (x * y + w * z);
1065 let m13 = two * (x * z - w * y);
1066 let m21 = two * (x * y - w * z);
1067 let m23 = two * (y * z + w * x);
1068 let m31 = two * (x * z + w * y);
1069 let m32 = two * (y * z - w * x);
1070
1071 Matrix4(
1072 [m11, m12, m13, T::ZERO],
1073 [m21, m22, m23, T::ZERO],
1074 [m31, m32, m33, T::ZERO],
1075 [T::ZERO, T::ZERO, T::ZERO, T::ONE],
1076 )
1077 }
1078}
1079
1080macro_rules! VariadicElementOps
1082{
1083 (for $e: ident ($($n: tt),*)) =>
1084 {
1085 impl<T: Add> Add for $e<T>
1086 {
1087 type Output = $e<<T as Add>::Output>;
1088 fn add(self, other: Self) -> Self::Output { $e($(self.$n + other.$n),*) }
1089 }
1090 impl<T: AddAssign> AddAssign for $e<T>
1091 {
1092 fn add_assign(&mut self, other: Self) { $(self.$n += other.$n);* }
1093 }
1094
1095 impl<T: Sub> Sub for $e<T>
1096 {
1097 type Output = $e<<T as Sub>::Output>;
1098 fn sub(self, other: Self) -> Self::Output { $e($(self.$n - other.$n),*) }
1099 }
1100 impl<T: SubAssign> SubAssign for $e<T>
1101 {
1102 fn sub_assign(&mut self, other: Self) { $(self.$n -= other.$n);* }
1103 }
1104
1105 impl<T: Mul + Copy> Mul<T> for $e<T>
1106 {
1107 type Output = $e<<T as Mul>::Output>;
1108 fn mul(self, other: T) -> Self::Output { $e($(self.$n * other),*) }
1109 }
1110 impl<T: MulAssign + Copy> MulAssign<T> for $e<T>
1111 {
1112 fn mul_assign(&mut self, other: T) { $(self.$n *= other);* }
1113 }
1114
1115 impl<T: Neg> Neg for $e<T>
1116 {
1117 type Output = $e<<T as Neg>::Output>;
1118 fn neg(self) -> Self::Output { $e($(-self.$n),*) }
1119 }
1120
1121 impl<T: Mul<Output = T> + Add<Output = T> + Copy> $e<T>
1122 {
1123 pub fn dot(self, other: Self) -> T
1125 {
1126 CTSummation!($(self.$n * other.$n),*)
1127 }
1128 }
1129 impl<T: Mul<Output = T> + Add<Output = T> + Copy> $e<T>
1130 {
1131 pub fn len2(self) -> T
1133 {
1134 CTSummation!($(self.$n * self.$n),*)
1135 }
1136 }
1137
1138 impl<T> $e<T> {
1139 pub fn min(self, other: Self) -> Self where T: crate::numtraits::Min<Output = T> {
1141 $e($(self.$n.min(other.$n)),*)
1142 }
1143 pub fn max(self, other: Self) -> Self where T: crate::numtraits::Max<Output = T> {
1145 $e($(self.$n.max(other.$n)),*)
1146 }
1147 }
1148 }
1149}
1150macro_rules! CTSummation
1151{
1152 ($e: expr, $($e2: expr),+) => { $e $(.add($e2))* }
1153}
1154VariadicElementOps!(for Vector2 (0, 1));
1155VariadicElementOps!(for Vector3 (0, 1, 2));
1156VariadicElementOps!(for Vector4 (0, 1, 2, 3));
1157
1158impl<T> Vector2<T>
1159where
1160 T: Mul<Output = T> + Sub<Output = T> + Copy,
1161{
1162 pub fn cross(&self, other: &Self) -> T {
1164 self.0 * other.1 - self.1 * other.0
1165 }
1166}
1167impl<T> Vector3<T>
1168where
1169 T: Mul<T, Output = T> + Sub<T, Output = T> + Copy,
1170{
1171 pub fn cross(&self, other: &Self) -> Self {
1173 let x = self.1 * other.2 - self.2 * other.1;
1174 let y = self.2 * other.0 - self.0 * other.2;
1175 let z = self.0 * other.1 - self.1 * other.0;
1176
1177 Vector3(x, y, z)
1178 }
1179}
1180
1181#[cfg(feature = "bedrock-interop")]
1183mod bedrock_interop;
1184#[cfg(feature = "euclid-interop")]
1185mod euclid_interop;
1186
1187#[cfg(test)]
1188mod tests {
1189 use super::*;
1190
1191 #[test]
1192 fn vector_dimension_transform() {
1193 assert_eq!(Vector2(2, 3).with_z(1), Vector3(2, 3, 1));
1194 assert_eq!(
1195 Vector3(2.5, 3.0, 4.1).with_w(1.0),
1196 Vector4(2.5, 3.0, 4.1, 1.0)
1197 );
1198 assert_eq!(Vector3::from(Vector4(4, 6, 8, 2)), Vector3(2, 3, 4));
1199 }
1200 #[test]
1201 fn matrix_multiplication_identity() {
1202 assert_eq!(Matrix3::ONE * Vector3(1, 2, 3), Vector3(1, 2, 3));
1203 assert_eq!(
1204 Matrix2::ONE * Matrix2::scale(Vector2(2, 3)),
1205 Matrix2::scale(Vector2(2, 3))
1206 );
1207 assert_eq!(
1208 Matrix3::ONE * Matrix3::scale(Vector3(2, 3, 4)),
1209 Matrix3::scale(Vector3(2, 3, 4))
1210 );
1211 assert_eq!(
1212 Matrix4::ONE * Matrix4::scale(Vector4(2, 3, 4, 5)),
1213 Matrix4::scale(Vector4(2, 3, 4, 5))
1214 );
1215 }
1216 #[test]
1217 fn matrix_transforming() {
1218 assert_eq!(
1219 Matrix3([1, 0, 2], [0, 1, 3], [0, 0, 1]) * Vector2(0, 0),
1220 Vector3(2, 3, 1)
1221 );
1222 assert_eq!(
1223 Matrix4([1, 0, 0, 2], [0, 1, 0, 3], [0, 0, 1, 4], [0, 0, 0, 1]) * Vector3(1, 2, 3),
1224 Vector4(3, 5, 7, 1)
1225 );
1226 assert_eq!(Matrix2::scale(Vector2(2, 3)) * Vector2(1, 1), Vector2(2, 3));
1227 }
1228 #[test]
1229 fn matrix_extension() {
1230 assert_eq!(
1231 Matrix3::from(Matrix2([0, 1], [1, 0])),
1232 Matrix3([0, 1, 0], [1, 0, 0], [0, 0, 1])
1233 );
1234 assert_eq!(
1235 Matrix4::from(Matrix3::from(Matrix2([0, 1], [2, 3]))),
1236 Matrix4([0, 1, 0, 0], [2, 3, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1])
1237 );
1238 }
1239 #[test]
1240 #[allow(clippy::no_effect)]
1241 fn vector_ops() {
1242 assert_eq!(Vector3(0, 1, 2) + Vector3(3, 4, 5), Vector3(3, 5, 7));
1243 assert_eq!(Vector3(6, 7, 8) - Vector3(3, 4, 5), Vector3(3, 3, 3));
1244 assert_eq!(Vector3(0, 2, 4) * 3, Vector3(0, 6, 12));
1245 assert_eq!(-Vector3(1, 2, -1), Vector3(-1, -2, 1));
1246 assert_eq!(
1247 Vector3(2, 3, 4).dot(Vector3(5, 6, 7)),
1248 2 * 5 + 3 * 6 + 4 * 7
1249 );
1250 assert_eq!(Vector2(0, 1).dot(Vector2(1, 0)), 0);
1251 #[allow(clippy::identity_op)]
1252 {
1253 assert_eq!(Vector3(1, 2, 3).len2(), 1 * 1 + 2 * 2 + 3 * 3);
1254 }
1255 assert_eq!(Vector2(10, 3).cross(&Vector2(4, 30)), 10 * 30 - 3 * 4);
1256 assert_eq!(
1257 Vector3(2, 3, 4).cross(&Vector3(6, 7, 8)),
1258 Vector3(3 * 8 - 4 * 7, 4 * 6 - 2 * 8, 2 * 7 - 6 * 3)
1259 );
1260 }
1261 #[test]
1262 fn inv_quaternion() {
1263 let q = Quaternion(0.0f32, 1.0, 0.0, 3.0).normalize();
1264 let Quaternion(a, b, c, d) = q * -q;
1265 assert!(a.abs() <= f32::EPSILON);
1267 assert!(b.abs() <= f32::EPSILON);
1268 assert!(c.abs() <= f32::EPSILON);
1269 assert!((1.0 - d).abs() <= f32::EPSILON);
1270 }
1271 #[test]
1272 fn vector_ops_assign() {
1273 let mut v1 = Vector3(0, 1, 2);
1274 v1 += Vector3(1, -3, 2);
1275 assert_eq!(v1, Vector3(0, 1, 2) + Vector3(1, -3, 2));
1276 v1 -= Vector3(1, -3, 2);
1277 assert_eq!(v1, Vector3(0, 1, 2) + Vector3(1, -3, 2) - Vector3(1, -3, 2));
1278 v1 *= 4;
1279 assert_eq!(
1280 v1,
1281 (Vector3(0, 1, 2) + Vector3(1, -3, 2) - Vector3(1, -3, 2)) * 4
1282 );
1283 }
1284}