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

#[allow(unused_imports)]
use array_init::array_init;
use num_traits::identities::One;
use num_traits::identities::Zero;
use std::ops::Div;
use std::ops::Mul;

use crate::data::Point;
use crate::data::Polygon;
use crate::data::Vector;
use crate::matrix::{Matrix, MatrixMul};

pub trait TransformScalar: One + Zero + Div<Output = Self> + MatrixMul {}
impl<T> TransformScalar for T where T: One + Zero + Div<Output = Self> + MatrixMul {}

// Sigh, can't use nalgebra::Transform because it requires RealField + Copy.
// ndarray also requires Copy.
// Use const generics once const_generics and const_evaluatable_checked are stable.
#[derive(Clone, Debug)]
pub struct Transform<T, const N: usize>(Matrix<T>);

impl<T, const N: usize> Transform<T, N>
where
T: TransformScalar,
{
fn new(m: Matrix<T>) -> Transform<T, N> {
assert_eq!(m.ncols(), N + 1);
assert_eq!(m.nrows(), N + 1);
Transform(m)
}
pub fn translate(vec: Vector<T, N>) -> Transform<T, N> {
let mut m = Matrix::new(N + 1, N + 1);
for i in 0..N {
m[(i, i)] = T::one();
m[(i, N)] = vec[i].clone();
}
m[(N, N)] = T::one();
Transform::new(m)
}

pub fn scale(vec: Vector<T, N>) -> Transform<T, N>
where
T: One + Zero,
{
let mut m = Matrix::new(N + 1, N + 1);
for i in 0..N {
m[(i, i)] = vec[i].clone();
}
m[(N, N)] = T::one();
Transform::new(m)
}

pub fn uniform_scale(v: T) -> Transform<T, N>
where
T: One + Zero,
{
let mut m = Matrix::new(N + 1, N + 1);
for i in 0..N {
m[(i, i)] = v.clone();
}
m[(N, N)] = T::one();
Transform::new(m)
}
}

impl<T, const N: usize> Mul for Transform<T, N>
where
T: TransformScalar,
{
type Output = Transform<T, N>;
fn mul(self, other: Transform<T, N>) -> Transform<T, N> {
Transform::new(self.0 * other.0)
}
}

impl<'a, 'b, T, const N: usize> Mul<&'b Transform<T, N>> for &'a Transform<T, N>
where
T: TransformScalar,
{
type Output = Transform<T, N>;
fn mul(self, other: &Transform<T, N>) -> Transform<T, N> {
Transform::new(&self.0 * &other.0)
}
}

impl<'a, 'b, T, const N: usize> Mul<&'b Point<T, N>> for &'a Transform<T, N>
where
T: TransformScalar,
{
type Output = Point<T, N>;
fn mul(self, other: &Point<T, N>) -> Point<T, N> {
let v: Vector<T, N> = self * Vector::from(other.clone());
v.into()
}
}

// &t * &v = v
impl<'a, 'b, T, const N: usize> Mul<&'b Vector<T, N>> for &'a Transform<T, N>
where
T: TransformScalar,
{
type Output = Vector<T, N>;
fn mul(self, other: &Vector<T, N>) -> Vector<T, N> {
let mut v = Matrix::new(N + 1, 1);
for i in 0..N {
v[(i, 0)] = other.0[i].clone()
}
v[(N, 0)] = T::one();
let ret = &self.0 * v;
let normalizer = ret[(N, 0)].clone();
Vector(array_init(|i| ret[(i, 0)].clone() / normalizer.clone()))
}
}

// &t * v = v
impl<'a, T, const N: usize> Mul<Vector<T, N>> for &'a Transform<T, N>
where
T: TransformScalar,
{
type Output = Vector<T, N>;
fn mul(self, other: Vector<T, N>) -> Vector<T, N> {
self.mul(&other)
}
}

impl<'a, T, const N: usize> Mul<&'a Vector<T, N>> for Transform<T, N>
where
T: TransformScalar,
{
type Output = Vector<T, N>;
fn mul(self, other: &Vector<T, N>) -> Vector<T, N> {
(&self).mul(other)
}
}

impl<T, const N: usize> Mul<Point<T, N>> for Transform<T, N>
where
T: TransformScalar,
{
type Output = Point<T, N>;
fn mul(self, other: Point<T, N>) -> Point<T, N> {
&self * &other
}
}

impl<'a, T, const N: usize> Mul<Point<T, N>> for &'a Transform<T, N>
where
T: TransformScalar,
{
type Output = Point<T, N>;
fn mul(self, other: Point<T, N>) -> Point<T, N> {
self * &other
}
}

impl<'a, 'b, T> Mul<&'b Polygon<T>> for &'a Transform<T, 2>
where
T: TransformScalar,
{
type Output = Polygon<T>;
fn mul(self, other: &Polygon<T>) -> Polygon<T> {
other.clone().map_points(|p| self * p)
}
}

impl<'a, T> Mul<Polygon<T>> for &'a Transform<T, 2>
where
T: TransformScalar,
{
type Output = Polygon<T>;
fn mul(self, mut other: Polygon<T>) -> Polygon<T> {
for pt in other.iter_mut() {
*pt = self * pt.clone();
}
other
}
}

impl<T> Mul<Polygon<T>> for Transform<T, 2>
where
T: TransformScalar,
{
type Output = Polygon<T>;
fn mul(self, other: Polygon<T>) -> Polygon<T> {
(&self).mul(other)
}
}

impl<'a, T> Mul<&'a Polygon<T>> for Transform<T, 2>
where
T: TransformScalar,
{
type Output = Polygon<T>;
fn mul(self, other: &Polygon<T>) -> Polygon<T> {
(&self).mul(other)
}
}