1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::Vec3;
/// A 3*3 matrix.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Mat33 {
r0: Vec3,
r1: Vec3,
r2: Vec3,
}
impl Mat33 {
/// Creates a 3*3 matrix from the given rows components.
pub fn new(r0: Vec3, r1: Vec3, r2: Vec3) -> Self {
Self { r0, r1, r2 }
}
/// Returns the first row of this matrix.
#[inline]
pub fn row0(&self) -> Vec3 {
self.r0
}
/// Returns the second row of this matrix.
#[inline]
pub fn row1(&self) -> Vec3 {
self.r1
}
/// Returns the third row of this matrix.
#[inline]
pub fn row2(&self) -> Vec3 {
self.r2
}
/// Transposes this 3*3 matrix.
///
/// # Examples
///
/// ```
/// use jord::{Mat33, Vec3};
///
/// let m = Mat33::new(
/// Vec3::new(1.0, 2.0, 3.0),
/// Vec3::new(4.0, 5.0, 6.0),
/// Vec3::new(7.0, 8.0, 9.0)
/// );
/// let e = Mat33::new(
/// Vec3::new(1.0, 4.0, 7.0),
/// Vec3::new(2.0, 5.0, 8.0),
/// Vec3::new(3.0, 6.0, 9.0)
/// );
/// assert_eq!(e, m.transpose());
/// ```
pub fn transpose(&self) -> Self {
Mat33::new(
Vec3::new(self.r0.x(), self.r1.x(), self.r2.x()),
Vec3::new(self.r0.y(), self.r1.y(), self.r2.y()),
Vec3::new(self.r0.z(), self.r1.z(), self.r2.z()),
)
}
}
impl ::std::ops::Mul<Mat33> for Vec3 {
type Output = Self;
fn mul(self, rhs: Mat33) -> Self {
let x = self.dot_prod(rhs.r0);
let y = self.dot_prod(rhs.r1);
let z = self.dot_prod(rhs.r2);
Vec3::new(x, y, z)
}
}
impl ::std::ops::Mul<Mat33> for Mat33 {
type Output = Self;
fn mul(self, rhs: Mat33) -> Self {
let t2 = rhs.transpose();
let m1r0 = self.r0;
let m1r1 = self.r1;
let m1r2 = self.r2;
let t2r0 = t2.r0;
let t2r1 = t2.r1;
let t2r2 = t2.r2;
let mr0 = Vec3::new(
m1r0.dot_prod(t2r0),
m1r0.dot_prod(t2r1),
m1r0.dot_prod(t2r2),
);
let mr1 = Vec3::new(
m1r1.dot_prod(t2r0),
m1r1.dot_prod(t2r1),
m1r1.dot_prod(t2r2),
);
let mr2 = Vec3::new(
m1r2.dot_prod(t2r0),
m1r2.dot_prod(t2r1),
m1r2.dot_prod(t2r2),
);
Mat33::new(mr0, mr1, mr2)
}
}