```  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
```
```#[cfg(feature="nalgebra")] use nalgebra::core::Matrix3;

use std::{
ops::Mul,
f32::consts::PI,
fmt,
default::Default,
cmp::{Eq, PartialEq}
};

/// A 2D transformation represented by a matrix
///
/// Transforms can be composed together through matrix multiplication, and are applied to Vectors
/// through multiplication, meaning the notation used is the '*' operator. A property of matrix
/// multiplication is that for some matrices A, B, C and vector V is
/// ```text
/// Transform = A * B * C
/// Transform * V = A * (B * (C * V))
/// ```
///
/// This property allows encoding multiple transformations in a single matrix. A transformation
/// that involves rotating a shape 30 degrees and then moving it six units up could be written as
/// ```no_run
/// use quicksilver::geom::{Transform, Vector};
/// let transform = Transform::rotate(30) * Transform::translate(Vector::new(0, -6));
/// ```
/// and then applied to a Vector
/// ```no_run
/// # use quicksilver::geom::{Transform, Vector};
/// # let transform  = Transform::rotate(30) * Transform::translate(Vector::new(0, -6));
/// transform * Vector::new(5, 5)
/// # ;
/// ```
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct Transform([[f32; 3]; 3]);

impl Transform {
///The identity transformation
pub const IDENTITY: Transform = Transform([[1f32, 0f32, 0f32],
[0f32, 1f32, 0f32],
[0f32, 0f32, 1f32]]);

///Create a Transform from an arbitrary matrix in a column-major matrix
pub fn from_array(array: [[f32; 3]; 3]) -> Transform {
Transform(array)
}

///Create a rotation transformation
pub fn rotate<T: Scalar>(angle: T) -> Transform {
let angle = angle.float();
let c = (angle * PI / 180f32).cos();
let s = (angle * PI / 180f32).sin();
Transform([[c, -s, 0f32],
[s, c, 0f32],
[0f32, 0f32, 1f32]])
}

///Create a translation transformation
pub fn translate(vec: impl Into<Vector>) -> Transform {
let vec = vec.into();
Transform([[1f32, 0f32, vec.x],
[0f32, 1f32, vec.y],
[0f32, 0f32, 1f32]])
}

///Create a scale transformation
pub fn scale(vec: impl Into<Vector>) -> Transform {
let vec = vec.into();
Transform([[vec.x, 0f32, 0f32],
[0f32, vec.y, 0f32],
[0f32, 0f32, 1f32]])
}

#[cfg(feature="nalgebra")]
///Convert the Transform into an nalgebra Matrix3
pub fn into_matrix(self) -> Matrix3<f32> {
Matrix3::new(
self.0[0][0], self.0[0][1], self.0[0][2],
self.0[1][0], self.0[1][1], self.0[1][2],
self.0[2][0], self.0[2][1], self.0[2][2],
)
}

///Find the inverse of a Transform
///
/// A transform's inverse will cancel it out when multplied with it, as seen below:
/// ```
/// # use quicksilver::geom::{Transform, Vector};
/// let transform = Transform::translate(Vector::new(4, 5));
/// let inverse = transform.inverse();
/// let vector = Vector::new(10, 10);
/// assert_eq!(vector, transform * inverse * vector);
/// assert_eq!(vector, inverse * transform * vector);
/// ```
#[must_use]
pub fn inverse(&self) -> Transform {
let det =
self.0[0][0] * (self.0[1][1] * self.0[2][2] - self.0[2][1] * self.0[1][2])
- self.0[0][1] * (self.0[1][0] * self.0[2][2] - self.0[1][2] * self.0[2][0])
+ self.0[0][2] * (self.0[1][0] * self.0[2][1] - self.0[1][1] * self.0[2][0]);

let inv_det = det.recip();

let mut inverse = Transform::IDENTITY;
inverse.0[0][0] = self.0[1][1] * self.0[2][2] - self.0[2][1] * self.0[1][2];
inverse.0[0][1] = self.0[0][2] * self.0[2][1] - self.0[0][1] * self.0[2][2];
inverse.0[0][2] = self.0[0][1] * self.0[1][2] - self.0[0][2] * self.0[1][1];
inverse.0[1][0] = self.0[1][2] * self.0[2][0] - self.0[1][0] * self.0[2][2];
inverse.0[1][1] = self.0[0][0] * self.0[2][2] - self.0[0][2] * self.0[2][0];
inverse.0[1][2] = self.0[1][0] * self.0[0][2] - self.0[0][0] * self.0[1][2];
inverse.0[2][0] = self.0[1][0] * self.0[2][1] - self.0[2][0] * self.0[1][1];
inverse.0[2][1] = self.0[2][0] * self.0[0][1] - self.0[0][0] * self.0[2][1];
inverse.0[2][2] = self.0[0][0] * self.0[1][1] - self.0[1][0] * self.0[0][1];
inverse * inv_det
}
}

///Concat two transforms A and B such that A * B * v = A * (B * v)
impl Mul<Transform> for Transform {
type Output = Transform;

fn mul(self, other: Transform) -> Transform {
let mut returnval = Transform::IDENTITY;
for i in 0..3 {
for j in 0..3 {
returnval.0[i][j] = 0f32;
for k in 0..3 {
returnval.0[i][j] += other.0[k][j] * self.0[i][k];
}
}
}
returnval
}
}

///Transform a vector
impl Mul<Vector> for Transform {
type Output = Vector;

fn mul(self, other: Vector) -> Vector {
Vector::new(
other.x * self.0[0][0] + other.y * self.0[0][1] + self.0[0][2],
other.x * self.0[1][0] + other.y * self.0[1][1] + self.0[1][2],
)
}
}

/// Scale all of the internal values of the Transform matrix
///
/// Note this will NOT scale vectors multiplied by this transform, and generally you shouldn't need
/// to use this.
impl<T: Scalar> Mul<T> for Transform {
type Output = Transform;

fn mul(self, other: T) -> Transform {
let other = other.float();
let mut ret = Transform::IDENTITY;
for i in 0..3 {
for j in 0..3 {
ret.0[i][j] = self.0[i][j] * other;
}
}
ret
}
}

impl fmt::Display for Transform {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?;
for i in 0..3 {
for j in 0..3 {
write!(f, "{},", self.0[i][j])?;
}
write!(f, "\n")?;
}
write!(f, "]")
}
}

impl Default for Transform {
fn default() -> Transform {
Transform::IDENTITY
}
}

impl PartialEq for Transform {
fn eq(&self, other: &Transform) -> bool {
for i in 0..3 {
for j in 0..3 {
return false;
}
}
}
true
}
}

impl Eq for Transform {}

#[cfg(feature="nalgebra")]
impl From<Matrix3<f32>> for Transform {
fn from(other: Matrix3<f32>) -> Transform {
Transform([[other[0], other[1], other[2]],
[other[3], other[4], other[5]],
[other[6], other[7], other[8]]])
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn equality() {
assert_eq!(Transform::IDENTITY, Transform::IDENTITY);
assert_eq!(Transform::rotate(5), Transform::rotate(5));
}

#[test]
fn inverse() {
let vec = Vector::new(2, 4);
let translate = Transform::scale(Vector::ONE * 0.5);
let inverse = translate.inverse();
let transformed = inverse * vec;
let expected = vec * 2;
assert_eq!(transformed, expected);
}

#[test]
fn scale() {
let trans = Transform::scale(Vector::ONE * 2);
let vec = Vector::new(2, 5);
let scaled = trans * vec;
let expected = vec * 2;
assert_eq!(scaled, expected);
}

#[test]
fn translate() {
let translate = Vector::new(3, 4);
let trans = Transform::translate(translate);
let vec = Vector::ONE;
let translated = trans * vec;
let expected = vec + translate;
assert_eq!(translated, expected);
}

#[test]
fn identity() {
let trans = Transform::IDENTITY * Transform::translate(Vector::ZERO) *
Transform::rotate(0f32) * Transform::scale(Vector::ONE);
let vec = Vector::new(15, 12);
assert_eq!(vec, trans * vec);
}

#[test]
fn complex_inverse() {
let a = Transform::rotate(5f32) * Transform::scale(Vector::new(0.2, 1.23)) *
Transform::translate(Vector::ONE * 100f32);
let a_inv = a.inverse();
let vec = Vector::new(120f32, 151f32);
assert_eq!(vec, a * a_inv * vec);
assert_eq!(vec, a_inv * a * vec);
}

#[test]
#[cfg(feature="nalgebra")]
fn conversion() {
let transform = Transform::rotate(5);
let vector = Vector::new(1, 2);
let na_matrix = transform.into_matrix();
let na_vector = vector.into_vector();
assert_eq!(transform * vector, (na_matrix.transform_vector(&na_vector)).into());
}
}
```