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
use vec::*;
use mat::*;
use num_traits::{Num, Float, Signed};

macro_rules! affine {
	($Map:ident, $Mat:ident, $Vec:ident) => (
		#[derive(Clone, Debug, PartialEq)]
		pub struct $Map<T> where T: Copy + Num + Float + Signed {
			pub linear: $Mat<T>,
			pub shift: $Vec<T>,
		}

		impl<T> $Map<T> where T: Copy + Num + Float + Signed {
			pub fn new() -> Self {
				Self { linear: $Mat::one(), shift: $Vec::zero() }
			}
			pub fn from(m: $Mat<T>, v: $Vec<T>) -> Self {
				Self { linear: m, shift: v }
			}
			pub fn inverse(&self) -> Self {
				let inv = self.linear.inverse();
				Self {
					linear: inv,
					shift: -inv.dot(self.shift),
				}
			}
			pub fn map_vec(&self, v: $Vec<T>) -> $Vec<T> {
				self.linear.dot(v) + self.shift
			}
			pub fn map_mat(&self, m: $Mat<T>) -> $Mat<T> {
				self.linear.dot(m)
			}
			pub fn chain(&self, a: &$Map<T>) -> Self {
				Self::from(self.map_mat(a.linear), self.map_vec(a.shift))
			}
		}
	)
}

affine!(Affine2, Mat2, Vec2);
affine!(Affine3, Mat3, Vec3);
affine!(Affine4, Mat4, Vec4);