1use std::ops::{Add, Sub, Mul};
2use crate::vec::{Vec, Math};
3use crate::vec3::Vec3;
4use crate::consts::{Zero, One};
5
6#[repr(C)]
7#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq)]
8pub struct Mat3<T>
9{
10 pub a1:T, pub a2:T, pub a3:T,
11 pub b1:T, pub b2:T, pub b3:T,
12 pub c1:T, pub c2:T, pub c3:T,
13}
14
15impl<T> Add for Mat3<T> where T:Add<Output=T>
16{
17 type Output = Self;
18
19 fn add(self, m: Self) -> Self
20 {
21 Mat3
22 {
23 a1:self.a1 + m.a1, a2:self.a2 + m.a2, a3:self.a3 + m.a3,
24 b1:self.b1 + m.b1, b2:self.b2 + m.b2, b3:self.b3 + m.b3,
25 c1:self.c1 + m.c1, c2:self.c2 + m.c2, c3:self.c3 + m.c3,
26 }
27 }
28}
29
30impl<T> Sub for Mat3<T> where T:Sub<Output=T>
31{
32 type Output = Self;
33
34 fn sub(self, m: Self) -> Self
35 {
36 Mat3
37 {
38 a1:self.a1 - m.a1, a2:self.a2 - m.a2, a3:self.a3 - m.a3,
39 b1:self.b1 - m.b1, b2:self.b2 - m.b2, b3:self.b3 - m.b3,
40 c1:self.c1 - m.c1, c2:self.c2 - m.c2, c3:self.c3 - m.c3,
41 }
42 }
43}
44
45impl<T> Mul for Mat3<T> where T:Copy + Mul<Output=T> + Add<Output=T>
46{
47 type Output = Self;
48
49 fn mul(self, m: Self) -> Self
50 {
51 let a = self;
52
53 Self
54 {
55 a1 : a.a1 * m.a1 + a.b1 * m.a2 + a.c1 * m.a3,
56 a2 : a.a2 * m.a1 + a.b2 * m.a2 + a.c2 * m.a3,
57 a3 : a.a3 * m.a1 + a.b3 * m.a2 + a.c3 * m.a3,
58
59 b1 : a.a1 * m.b1 + a.b1 * m.b2 + a.c1 * m.b3,
60 b2 : a.a2 * m.b1 + a.b2 * m.b2 + a.c2 * m.b3,
61 b3 : a.a3 * m.b1 + a.b3 * m.b2 + a.c3 * m.b3,
62
63 c1 : a.a1 * m.c1 + a.b1 * m.c2 + a.c1 * m.c3,
64 c2 : a.a2 * m.c1 + a.b2 * m.c2 + a.c2 * m.c3,
65 c3 : a.a3 * m.c1 + a.b3 * m.c2 + a.c3 * m.c3,
66 }
67 }
68}
69
70impl<T> Mat3<T> where T:Copy
71{
72 pub fn new(
73 m11:T, m12:T, m13:T,
74 m21:T, m22:T, m23:T,
75 m31:T, m32:T, m33:T) -> Self
76 {
77 Self
78 {
79 a1:m11, a2:m12, a3:m13,
80 b1:m21, b2:m22, b3:m23,
81 c1:m31, c2:m32, c3:m33,
82 }
83 }
84
85 pub fn right(&self) -> Vec3<T>
86 {
87 Vec3::new(self.a1, self.a2, self.a3)
88 }
89
90 pub fn up(&self) -> Vec3<T>
91 {
92 Vec3::new(self.b1, self.b2, self.b3)
93 }
94
95 pub fn forward(&self) -> Vec3<T>
96 {
97 Vec3::new(self.c1, self.c2, self.c3)
98 }
99
100 pub fn as_ptr(&self) -> *const T
101 {
102 &self.a1
103 }
104
105 pub fn to_array(&self) -> [T; 9]
106 {
107 [
108 self.a1, self.a2, self.a3,
109 self.b1, self.b2, self.b3,
110 self.c1, self.c2, self.c3,
111 ]
112 }
113}
114
115impl<T> Mat3<T> where T:Vec + Math
116{
117 pub fn rotate_x(theta:T) -> Self
118 {
119 let (s,c) = theta.sincos();
120
121 let a1 = T::one();
122 let a2 = T::zero();
123 let a3 = T::zero();
124
125 let b1 = T::zero();
126 let b2 = c;
127 let b3 = s;
128
129 let c1 = T::zero();
130 let c2 =-s;
131 let c3 = c;
132
133 Mat3::new(
134 a1, a2, a3,
135 b1, b2, b3,
136 c1, c2, c3
137 )
138 }
139
140 pub fn rotate_y(theta:T) -> Self
141 {
142 let (s,c) = theta.sincos();
143
144 let a1 = c;
145 let a2 = T::zero();
146 let a3 =-s;
147
148 let b1 = T::zero();
149 let b2 = T::one();
150 let b3 = T::zero();
151
152 let c1 = s;
153 let c2 = T::zero();
154 let c3 = c;
155
156 Mat3::new(
157 a1, a2, a3,
158 b1, b2, b3,
159 c1, c2, c3
160 )
161 }
162
163 pub fn rotate_z(theta:T) -> Self
164 {
165 let (s,c) = theta.sincos();
166
167 let a1 = c;
168 let a2 = s;
169 let a3 = T::zero();
170
171 let b1 =-s;
172 let b2 = c;
173 let b3 = T::zero();
174
175 let c1 = T::zero();
176 let c2 = T::zero();
177 let c3 = T::one();
178
179 Mat3::new(
180 a1, a2, a3,
181 b1, b2, b3,
182 c1, c2, c3,
183 )
184 }
185
186 pub fn rotate(axis:Vec3<T>, theta:T) -> Self
187 {
188 let (s,c) = theta.sincos();
189
190 let x = axis.x;
191 let y = axis.y;
192 let z = axis.z;
193
194 let t = T::one() - c;
195 let tx = t * x;
196 let ty = t * y;
197 let tz = t * z;
198
199 let a1 = tx * x + c;
200 let a2 = tx * y + s * z;
201 let a3 = tx * z - s * y;
202
203 let b1 = tx * y - s * z;
204 let b2 = ty * y + c;
205 let b3 = ty * z + s * x;
206
207 let c1 = tx * z + s * y;
208 let c2 = ty * z - s * x;
209 let c3 = tz * z + c;
210
211 Mat3::new(
212 a1, a2, a3,
213 b1, b2, b3,
214 c1, c2, c3
215 )
216 }
217
218 pub fn scale(x:T, y:T, z:T) -> Self
219 {
220 let (a1,a2,a3) = (x, T::zero(), T::zero());
221 let (b1,b2,b3) = (T::zero(), y, T::zero());
222 let (c1,c2,c3) = (T::zero(), T::zero(), z);
223
224 Mat3::new(
225 a1, a2, a3,
226 b1, b2, b3,
227 c1, c2, c3,
228 )
229 }
230
231 pub fn translate(x:T, y:T) -> Self
232 {
233 let (a1,a2,a3) = (T::one(), T::zero(), T::zero());
234 let (b1,b2,b3) = (T::zero(), T::one(), T::one());
235 let (c1,c2,c3) = (x, y, T::one());
236
237 Mat3::new(
238 a1, a2, a3,
239 b1, b2, b3,
240 c1, c2, c3,
241 )
242 }
243}
244
245impl<T> Zero for Mat3<T> where T:Zero
246{
247 #[inline(always)]
248 fn zero() -> Self
249 {
250 Self
251 {
252 a1:T::zero(), a2:T::zero(), a3:T::zero(),
253 b1:T::zero(), b2:T::zero(), b3:T::zero(),
254 c1:T::zero(), c2:T::zero(), c3:T::zero(),
255 }
256 }
257}
258
259impl<T> One for Mat3<T> where T:One + Zero
260{
261 #[inline(always)]
262 fn one() -> Self
263 {
264 Self
265 {
266 a1:T::one(), a2:T::zero(), a3:T::zero(),
267 b1:T::zero(), b2:T::one(), b3:T::zero(),
268 c1:T::zero(), c2:T::zero(), c3:T::one(),
269 }
270 }
271}
272
273impl<T> From<[T;9]> for Mat3<T> where T:Copy
274{
275 fn from(v:[T;9]) -> Self
276 {
277 Self
278 {
279 a1:v[0],a2:v[1],a3:v[2],
280 b1:v[3],b2:v[4],b3:v[5],
281 c1:v[6],c2:v[7],c3:v[8],
282 }
283 }
284}
285
286impl<T> From<(T,T,T,T,T,T,T,T,T)> for Mat3<T> where T:Copy
287{
288 fn from(v:(T,T,T,T,T,T,T,T,T)) -> Self
289 {
290 Self
291 {
292 a1:v.0,a2:v.1,a3:v.2,
293 b1:v.3,b2:v.4,b3:v.5,
294 c1:v.6,c2:v.7,c3:v.8,
295 }
296 }
297}
298
299impl<T> AsRef<Mat3<T>> for Mat3<T>
300{
301 fn as_ref(&self) -> &Mat3<T>
302 {
303 self
304 }
305}
306
307impl<T> AsMut<Mat3<T>> for Mat3<T>
308{
309 fn as_mut(&mut self) -> &mut Mat3<T>
310 {
311 self
312 }
313}