octoon_math/
mat2.rs

1use std::ops::{Add, Sub, Mul};
2use crate::vec::{Vec, Math};
3use crate::vec2::Vec2;
4use crate::vec3::Vec3;
5use crate::consts::{Zero, One};
6
7#[repr(C)]
8#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq)]
9pub struct Mat2<T>
10{
11	pub a1:T, pub a2:T,
12	pub b1:T, pub b2:T,
13}
14
15impl<T> Add for Mat2<T>  where T:Add<Output=T>
16{
17	type Output = Self;
18
19	fn add(self, b: Self) -> Self
20	{
21		Mat2
22		{
23			a1:self.a1 + b.a1,
24			a2:self.a2 + b.a2,
25			b1:self.b1 + b.b1,
26			b2:self.b2 + b.b2,
27		}
28	}
29}
30
31impl<T> Sub for Mat2<T>  where T:Sub<Output=T>
32{
33	type Output = Self;
34
35	fn sub(self, b: Self) -> Self
36	{
37		Mat2
38		{
39			a1:self.a1 - b.a1,
40			a2:self.a2 - b.a2,
41			b1:self.b1 - b.b1,
42			b2:self.b2 - b.b2,
43		}
44	}
45}
46
47impl<T> Mul for Mat2<T> where T:Copy + Mul<Output=T> + Add<Output=T>
48{
49	type Output = Self;
50
51	fn mul(self, b: Self) -> Self
52	{
53		let a = self;
54
55		Mat2
56		{ 
57			a1 : a.a1 * b.a1 + a.b1 * b.a2,
58			a2 : a.a2 * b.a1 + a.b2 * b.a2,
59			b1 : a.a1 * b.b1 + a.b1 * b.b2,
60			b2 : a.a2 * b.b1 + a.b2 * b.b2
61		}
62	}
63}
64
65impl<T> Mat2<T> where T:Copy
66{
67	pub fn new(m11:T, m12:T, m21:T, m22:T) -> Self
68	{
69		Self
70		{ 
71			a1:m11, a2:m12,
72			b1:m21, b2:m22,
73		}
74	}
75
76	pub fn right(&self) -> Vec2<T>
77	{
78		Vec2::new(self.a1, self.a2)
79	}
80
81	pub fn up(&self) -> Vec2<T>
82	{
83		Vec2::new(self.b1, self.b2)
84	}
85
86	pub fn as_ptr(&self) -> *const T
87	{
88		&self.a1
89	}
90
91	pub fn to_array(&self) -> [T; 4]
92	{
93		[
94			self.a1, self.a2,
95			self.b1, self.b2,
96		]
97	}
98}
99
100impl<T> Mat2<T> where T:Vec + Math
101{
102	pub fn rotate_x(theta:T) -> Self
103	{
104		let (_s,c) = theta.sincos();
105		Mat2::new(T::one(), T::zero(), T::zero(), c)
106	}
107
108	pub fn rotate_y(theta:T) -> Self
109	{
110		let (_s,c) = theta.sincos();
111		Mat2::new(c, T::zero(), T::zero(), T::one())
112	}
113
114	pub fn rotate_z(theta:T) -> Self
115	{
116		let (s,c) = theta.sincos();
117		Mat2::new(c, -s, s, c)
118	}
119
120	pub fn rotate(axis:Vec3<T>, theta:T) -> Self
121	{
122		let (s,c) = theta.sincos();
123
124		let x = axis.x;
125		let y = axis.y;
126		let z = axis.z;
127
128		let t = T::one() - c;
129		let tx = t * x;
130		let ty = t * y;
131
132		let a1 = tx * x + c;
133		let a2 = tx * y + s * z;
134
135		let b1 = tx * y - s * z;
136		let b2 = ty * y + c;
137
138		Mat2
139		{
140			a1:a1, a2:a2,
141			b1:b1, b2:b2
142		}
143	}
144
145	pub fn scale(x:T, y:T) -> Self
146	{
147		Mat2
148		{
149			a1:x, a2:T::zero(),
150			b1:T::zero(), b2:y
151		}
152	}
153
154	pub fn transpose(&self) -> Self
155	{
156		let (a1, a2) = (self.a1, self.b1);
157		let (b1, b2) = (self.a2, self.b2);
158
159		Mat2
160		{
161			a1:a1, a2:a2,
162			b1:b1, b2:b2
163		}
164	}
165}
166
167impl<T:Vec> Zero for Mat2<T>
168{
169	#[inline(always)]
170	fn zero() -> Self
171	{
172		Mat2
173		{
174			a1:T::zero(), a2:T::zero(),
175			b1:T::zero(), b2:T::zero()
176		}
177	}
178}
179
180impl<T:Vec> One for Mat2<T>
181{
182	#[inline(always)]
183	fn one() -> Self
184	{
185		Mat2
186		{
187			a1:T::one(), a2:T::zero(),
188			b1:T::zero(), b2:T::one()
189		}
190	}
191}
192
193impl<T> From<[T;4]> for Mat2<T> where T:Copy
194{
195	fn from(v:[T;4]) -> Self
196	{
197		Self
198		{
199			a1:v[0],a2:v[1],
200			b1:v[2],b2:v[3],
201		}
202	}
203}
204
205impl<T> From<(T,T,T,T)> for Mat2<T> where T:Copy
206{
207	fn from(v:(T,T,T,T)) -> Self
208	{
209		Self
210		{
211			a1:v.0,
212			a2:v.1,
213			b1:v.2,
214			b2:v.3,
215		}
216	}
217}
218
219impl<T> AsRef<Mat2<T>> for Mat2<T>
220{
221	fn as_ref(&self) -> &Mat2<T>
222	{
223		self
224	}
225}
226
227impl<T> AsMut<Mat2<T>> for Mat2<T>
228{
229	fn as_mut(&mut self) -> &mut Mat2<T>
230	{
231		self
232	}
233}