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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use std::any::Any;
use std::fmt::Debug;
use std::marker::PhantomData;
#[cfg(feature = "serde-serialize")]
use serde;
use alga::general::Real;
use core::{DefaultAllocator, MatrixN};
use core::dimension::{DimName, DimNameAdd, DimNameSum, U1};
use core::storage::Owned;
use core::allocator::Allocator;
pub trait TCategory: Any + Debug + Copy + PartialEq + Send {
#[inline]
fn has_normalizer() -> bool {
true
}
fn check_homogeneous_invariants<N: Real, D: DimName>(mat: &MatrixN<N, D>) -> bool
where N::Epsilon: Copy,
DefaultAllocator: Allocator<N, D, D>;
}
pub trait TCategoryMul<Other: TCategory>: TCategory {
type Representative: TCategory;
}
pub trait SuperTCategoryOf<Other: TCategory>: TCategory { }
pub trait SubTCategoryOf<Other: TCategory>: TCategory { }
impl<T1, T2> SubTCategoryOf<T2> for T1
where T1: TCategory,
T2: SuperTCategoryOf<T1> {
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum TGeneral { }
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum TProjective { }
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum TAffine { }
impl TCategory for TGeneral {
#[inline]
fn check_homogeneous_invariants<N: Real, D: DimName>(_: &MatrixN<N, D>) -> bool
where N::Epsilon: Copy,
DefaultAllocator: Allocator<N, D, D> {
true
}
}
impl TCategory for TProjective {
#[inline]
fn check_homogeneous_invariants<N: Real, D: DimName>(mat: &MatrixN<N, D>) -> bool
where N::Epsilon: Copy,
DefaultAllocator: Allocator<N, D, D> {
mat.is_invertible()
}
}
impl TCategory for TAffine {
#[inline]
fn has_normalizer() -> bool {
false
}
#[inline]
fn check_homogeneous_invariants<N: Real, D: DimName>(mat: &MatrixN<N, D>) -> bool
where N::Epsilon: Copy,
DefaultAllocator: Allocator<N, D, D> {
let last = D::dim() - 1;
mat.is_invertible() &&
mat[(last, last)] == N::one() &&
(0 .. last).all(|i| mat[(last, i)].is_zero())
}
}
macro_rules! category_mul_impl(
($($a: ident * $b: ident => $c: ty);* $(;)*) => {$(
impl TCategoryMul<$a> for $b {
type Representative = $c;
}
)*}
);
impl<T: TCategory> TCategoryMul<T> for T {
type Representative = T;
}
category_mul_impl!(
TGeneral * TProjective => TGeneral;
TGeneral * TAffine => TGeneral;
TProjective * TGeneral => TGeneral;
TProjective * TAffine => TProjective;
TAffine * TGeneral => TGeneral;
TAffine * TProjective => TProjective;
);
macro_rules! super_tcategory_impl(
($($a: ident >= $b: ident);* $(;)*) => {$(
impl SuperTCategoryOf<$b> for $a { }
)*}
);
impl<T: TCategory> SuperTCategoryOf<T> for T { }
super_tcategory_impl!(
TGeneral >= TProjective;
TGeneral >= TAffine;
TProjective >= TAffine;
);
#[repr(C)]
#[derive(Debug)]
pub struct Transform<N: Real, D: DimNameAdd<U1>, C: TCategory>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> {
matrix: MatrixN<N, DimNameSum<D, U1>>,
_phantom: PhantomData<C>
}
impl<N: Real, D: DimNameAdd<U1> + Copy, C: TCategory> Copy for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: Copy {
}
impl<N: Real, D: DimNameAdd<U1>, C: TCategory> Clone for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> {
#[inline]
fn clone(&self) -> Self {
Transform::from_matrix_unchecked(self.matrix.clone())
}
}
#[cfg(feature = "serde-serialize")]
impl<N: Real, D: DimNameAdd<U1>, C: TCategory> serde::Serialize for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: serde::Serialize {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
self.matrix.serialize(serializer)
}
}
#[cfg(feature = "serde-serialize")]
impl<'a, N: Real, D: DimNameAdd<U1>, C: TCategory> serde::Deserialize<'a> for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>,
Owned<N, DimNameSum<D, U1>, DimNameSum<D, U1>>: serde::Deserialize<'a> {
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
where Des: serde::Deserializer<'a> {
let matrix = MatrixN::<N, DimNameSum<D, U1>>::deserialize(deserializer)?;
Ok(Transform::from_matrix_unchecked(matrix))
}
}
impl<N: Real + Eq, D: DimNameAdd<U1>, C: TCategory> Eq for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> { }
impl<N: Real, D: DimNameAdd<U1>, C: TCategory> PartialEq for Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> {
#[inline]
fn eq(&self, right: &Self) -> bool {
self.matrix == right.matrix
}
}
impl<N: Real, D: DimNameAdd<U1>, C: TCategory> Transform<N, D, C>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> {
#[inline]
pub fn from_matrix_unchecked(matrix: MatrixN<N, DimNameSum<D, U1>>) -> Self {
Transform {
matrix: matrix,
_phantom: PhantomData
}
}
#[inline]
pub fn unwrap(self) -> MatrixN<N, DimNameSum<D, U1>> {
self.matrix
}
#[inline]
pub fn matrix(&self) -> &MatrixN<N, DimNameSum<D, U1>> {
&self.matrix
}
#[inline]
pub fn matrix_mut_unchecked(&mut self) -> &mut MatrixN<N, DimNameSum<D, U1>> {
&mut self.matrix
}
#[inline]
pub fn set_category<CNew: SuperTCategoryOf<C>>(self) -> Transform<N, D, CNew> {
Transform::from_matrix_unchecked(self.matrix)
}
#[inline]
#[deprecated(note = "This method is a no-op and will be removed in a future release.")]
pub fn clone_owned(&self) -> Transform<N, D, C> {
Transform::from_matrix_unchecked(self.matrix.clone_owned())
}
#[inline]
pub fn to_homogeneous(&self) -> MatrixN<N, DimNameSum<D, U1>> {
self.matrix().clone_owned()
}
#[inline]
pub fn try_inverse(self) -> Option<Transform<N, D, C>> {
if let Some(m) = self.matrix.try_inverse() {
Some(Transform::from_matrix_unchecked(m))
}
else {
None
}
}
#[inline]
pub fn inverse(self) -> Transform<N, D, C>
where C: SubTCategoryOf<TProjective> {
Transform::from_matrix_unchecked(self.matrix.try_inverse().unwrap())
}
#[inline]
pub fn try_inverse_mut(&mut self) -> bool {
self.matrix.try_inverse_mut()
}
#[inline]
pub fn inverse_mut(&mut self)
where C: SubTCategoryOf<TProjective> {
let _ = self.matrix.try_inverse_mut();
}
}
impl<N: Real, D: DimNameAdd<U1>> Transform<N, D, TGeneral>
where DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>> {
#[inline]
pub fn matrix_mut(&mut self) -> &mut MatrixN<N, DimNameSum<D, U1>> {
self.matrix_mut_unchecked()
}
}
#[cfg(test)]
mod tests {
use super::*;
use ::core::Matrix4;
#[test]
fn checks_homogeneous_invariants_of_square_identity_matrix() {
assert!(TAffine::check_homogeneous_invariants(&Matrix4::<f32>::identity()));
}
}