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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::ops::{Add, Sub, Mul};
use crate::vec::{Vec, Math};
use crate::vec3::Vec3;
use crate::consts::{Zero, One};

#[cfg(feature = "serialize")]
#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Mat3<T>
{
	pub a1:T, pub a2:T, pub a3:T,
	pub b1:T, pub b2:T, pub b3:T,
	pub c1:T, pub c2:T, pub c3:T,
}

#[cfg(not(feature = "serialize"))]
#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq)]
pub struct Mat3<T>
{
	pub a1:T, pub a2:T, pub a3:T,
	pub b1:T, pub b2:T, pub b3:T,
	pub c1:T, pub c2:T, pub c3:T,
}

impl<T> Add for Mat3<T>  where T:Add<Output=T>
{
	type Output = Self;

	fn add(self, m: Self) -> Self
	{
		Mat3
		{
			a1:self.a1 + m.a1, a2:self.a2 + m.a2, a3:self.a3 + m.a3,
			b1:self.b1 + m.b1, b2:self.b2 + m.b2, b3:self.b3 + m.b3,
			c1:self.c1 + m.c1, c2:self.c2 + m.c2, c3:self.c3 + m.c3,
		}
	}
}

impl<T> Sub for Mat3<T>  where T:Sub<Output=T>
{
	type Output = Self;

	fn sub(self, m: Self) -> Self
	{
		Mat3
		{
			a1:self.a1 - m.a1, a2:self.a2 - m.a2, a3:self.a3 - m.a3,
			b1:self.b1 - m.b1, b2:self.b2 - m.b2, b3:self.b3 - m.b3,
			c1:self.c1 - m.c1, c2:self.c2 - m.c2, c3:self.c3 - m.c3,
		}
	}
}

impl<T> Mul for Mat3<T> where T:Copy + Mul<Output=T> + Add<Output=T>
{
	type Output = Self;

	fn mul(self, m: Self) -> Self
	{
		let a = self;

		Self
		{
			a1 : a.a1 * m.a1 + a.b1 * m.a2 + a.c1 * m.a3,
			a2 : a.a2 * m.a1 + a.b2 * m.a2 + a.c2 * m.a3,
			a3 : a.a3 * m.a1 + a.b3 * m.a2 + a.c3 * m.a3,

			b1 : a.a1 * m.b1 + a.b1 * m.b2 + a.c1 * m.b3,
			b2 : a.a2 * m.b1 + a.b2 * m.b2 + a.c2 * m.b3,
			b3 : a.a3 * m.b1 + a.b3 * m.b2 + a.c3 * m.b3,

			c1 : a.a1 * m.c1 + a.b1 * m.c2 + a.c1 * m.c3,
			c2 : a.a2 * m.c1 + a.b2 * m.c2 + a.c2 * m.c3,
			c3 : a.a3 * m.c1 + a.b3 * m.c2 + a.c3 * m.c3,
		}
	}
}

impl<T> Mat3<T> where T:Copy
{
	pub fn new(
		m11:T, m12:T, m13:T, 
		m21:T, m22:T, m23:T, 
		m31:T, m32:T, m33:T) -> Self
	{
		Self
		{ 
			a1:m11, a2:m12, a3:m13,
			b1:m21, b2:m22, b3:m23,
			c1:m31, c2:m32, c3:m33,
		}
	}

	pub fn right(&self) -> Vec3<T>
	{
		Vec3::new(self.a1, self.a2, self.a3)
	}

	pub fn up(&self) -> Vec3<T>
	{
		Vec3::new(self.b1, self.b2, self.b3)
	}

	pub fn forward(&self) -> Vec3<T>
	{
		Vec3::new(self.c1, self.c2, self.c3)
	}

	pub fn as_ptr(&self) -> *const T
	{
		&self.a1
	}

	pub fn to_array(&self) -> [T; 9]
	{
		[
			self.a1, self.a2, self.a3,
			self.b1, self.b2, self.b3,
			self.c1, self.c2, self.c3,
		]
	}
}

impl<T> Mat3<T> where T:Vec + Math
{
	pub fn rotate_x(theta:T) -> Self
	{
		let (s,c) = theta.sincos();

		let a1 = T::one();
		let a2 = T::zero();
		let a3 = T::zero();

		let b1 = T::zero();
		let b2 = c;
		let b3 = s;

		let c1 = T::zero();
		let c2 =-s;
		let c3 = c;

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3
		)
	}

	pub fn rotate_y(theta:T) -> Self
	{
		let (s,c) = theta.sincos();

		let a1 = c;
		let a2 = T::zero();
		let a3 =-s;

		let b1 = T::zero();
		let b2 = T::one();
		let b3 = T::zero();

		let c1 = s;
		let c2 = T::zero();
		let c3 = c;

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3
		)
	}

	pub fn rotate_z(theta:T) -> Self
	{
		let (s,c) = theta.sincos();

		let a1 = c;
		let a2 = s;
		let a3 = T::zero();

		let b1 =-s;
		let b2 = c;
		let b3 = T::zero();

		let c1 = T::zero();
		let c2 = T::zero();
		let c3 = T::one();

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3, 
		)
	}

	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 tz = t * z;

		let a1 = tx * x + c;
		let a2 = tx * y + s * z;
		let a3 = tx * z - s * y;

		let b1 = tx * y - s * z;
		let b2 = ty * y + c;
		let b3 = ty * z + s * x;

		let c1 = tx * z + s * y;
		let c2 = ty * z - s * x;
		let c3 = tz * z + c;

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3
		)
	}

	pub fn scale(x:T, y:T, z:T) -> Self
	{
		let (a1,a2,a3) = (x, T::zero(), T::zero());
		let (b1,b2,b3) = (T::zero(), y, T::zero());
		let (c1,c2,c3) = (T::zero(), T::zero(), z);

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3,
		)
	}

	pub fn translate(x:T, y:T) -> Self
	{
		let (a1,a2,a3) = (T::one(), T::zero(), T::zero());
		let (b1,b2,b3) = (T::zero(), T::one(), T::one());
		let (c1,c2,c3) = (x, y, T::one());

		Mat3::new(
			a1, a2, a3, 
			b1, b2, b3, 
			c1, c2, c3,
		)
	}
}

impl<T> Zero for Mat3<T> where T:Zero
{
	#[inline(always)]
	fn zero() -> Self
	{
		Self
		{
			a1:T::zero(), a2:T::zero(), a3:T::zero(),
			b1:T::zero(), b2:T::zero(), b3:T::zero(),
			c1:T::zero(), c2:T::zero(), c3:T::zero(),
		}
	}
}

impl<T> One for Mat3<T> where T:One + Zero
{
	#[inline(always)]
	fn one() -> Self
	{
		Self
		{
			a1:T::one(), a2:T::zero(), a3:T::zero(),
			b1:T::zero(), b2:T::one(), b3:T::zero(),
			c1:T::zero(), c2:T::zero(), c3:T::one(),
		}
	}
}