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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::ops::{Add, Sub, Mul};
use crate::vec::{Vec, Math};
use crate::vec2::Vec2;
use crate::vec3::Vec3;
use crate::consts::{Zero, One};
#[cfg(feature = "serialize")]
#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Mat2<T>
{
pub a1:T, pub a2:T,
pub b1:T, pub b2:T,
}
#[cfg(not(feature = "serialize"))]
#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq)]
pub struct Mat2<T>
{
pub a1:T, pub a2:T,
pub b1:T, pub b2:T,
}
impl<T> Add for Mat2<T> where T:Add<Output=T>
{
type Output = Self;
fn add(self, b: Self) -> Self
{
Mat2
{
a1:self.a1 + b.a1,
a2:self.a2 + b.a2,
b1:self.b1 + b.b1,
b2:self.b2 + b.b2,
}
}
}
impl<T> Sub for Mat2<T> where T:Sub<Output=T>
{
type Output = Self;
fn sub(self, b: Self) -> Self
{
Mat2
{
a1:self.a1 - b.a1,
a2:self.a2 - b.a2,
b1:self.b1 - b.b1,
b2:self.b2 - b.b2,
}
}
}
impl<T> Mul for Mat2<T> where T:Copy + Mul<Output=T> + Add<Output=T>
{
type Output = Self;
fn mul(self, b: Self) -> Self
{
let a = self;
Mat2
{
a1 : a.a1 * b.a1 + a.b1 * b.a2,
a2 : a.a2 * b.a1 + a.b2 * b.a2,
b1 : a.a1 * b.b1 + a.b1 * b.b2,
b2 : a.a2 * b.b1 + a.b2 * b.b2
}
}
}
impl<T> Mat2<T> where T:Copy
{
pub fn new(m11:T, m12:T, m21:T, m22:T) -> Self
{
Self
{
a1:m11, a2:m12,
b1:m21, b2:m22,
}
}
pub fn right(&self) -> Vec2<T>
{
Vec2::new(self.a1, self.a2)
}
pub fn up(&self) -> Vec2<T>
{
Vec2::new(self.b1, self.b2)
}
pub fn as_ptr(&self) -> *const T
{
&self.a1
}
pub fn to_array(&self) -> [T; 4]
{
[
self.a1, self.a2,
self.b1, self.b2,
]
}
}
impl<T> Mat2<T> where T:Vec + Math
{
pub fn rotate_x(theta:T) -> Self
{
let (_s,c) = theta.sincos();
Mat2::new(T::one(), T::zero(), T::zero(), c)
}
pub fn rotate_y(theta:T) -> Self
{
let (_s,c) = theta.sincos();
Mat2::new(c, T::zero(), T::zero(), T::one())
}
pub fn rotate_z(theta:T) -> Self
{
let (s,c) = theta.sincos();
Mat2::new(c, -s, s, c)
}
pub fn rotate(axis:Vec3<T>, theta:T) -> Self
{
let (s,c) = theta.sincos();
let x = axis.x;
let y = axis.y;
let z = axis.z;
let t = T::one() - c;
let tx = t * x;
let ty = t * y;
let a1 = tx * x + c;
let a2 = tx * y + s * z;
let b1 = tx * y - s * z;
let b2 = ty * y + c;
Mat2
{
a1:a1, a2:a2,
b1:b1, b2:b2
}
}
pub fn scale(x:T, y:T) -> Self
{
Mat2
{
a1:x, a2:T::zero(),
b1:T::zero(), b2:y
}
}
pub fn transpose(&self) -> Self
{
let (a1, a2) = (self.a1, self.b1);
let (b1, b2) = (self.a2, self.b2);
Mat2
{
a1:a1, a2:a2,
b1:b1, b2:b2
}
}
}
impl<T:Vec> Zero for Mat2<T>
{
#[inline(always)]
fn zero() -> Self
{
Mat2
{
a1:T::zero(), a2:T::zero(),
b1:T::zero(), b2:T::zero()
}
}
}
impl<T:Vec> One for Mat2<T>
{
#[inline(always)]
fn one() -> Self
{
Mat2
{
a1:T::one(), a2:T::zero(),
b1:T::zero(), b2:T::one()
}
}
}