Debian Codegraph @

debian/missing-sources/nalgebra/tests/core/conversion.rs

raw
1 #![cfg(all(feature = "proptest-support"))]
2 use na::{
3 self, Affine3, Isometry3, Matrix2, Matrix2x3, Matrix2x4, Matrix2x5, Matrix2x6, Matrix3,
4 Matrix3x2, Matrix3x4, Matrix3x5, Matrix3x6, Matrix4, Matrix4x2, Matrix4x3, Matrix4x5,
5 Matrix4x6, Matrix5, Matrix5x2, Matrix5x3, Matrix5x4, Matrix5x6, Matrix6, Matrix6x2, Matrix6x3,
6 Matrix6x4, Matrix6x5, Projective3, Rotation3, RowVector1, RowVector2, RowVector3, RowVector4,
7 RowVector5, RowVector6, Similarity3, Transform3, UnitQuaternion, Vector1, Vector2, Vector3,
8 Vector4, Vector5, Vector6,
9 };
10 use na::{DMatrix, DMatrixView, DMatrixViewMut, MatrixView, MatrixViewMut};
11 use na::{U1, U3, U4};
12
13 use crate::proptest::*;
14 use proptest::{prop_assert, prop_assert_eq, proptest};
15
16 proptest! {
17 #[test]
18 fn translation_conversion(t in translation3(), p in point3()) {
19 let iso: Isometry3<f64> = na::convert(t);
20 let sim: Similarity3<f64> = na::convert(t);
21 let aff: Affine3<f64> = na::convert(t);
22 let prj: Projective3<f64> = na::convert(t);
23 let tr: Transform3<f64> = na::convert(t);
24
25 prop_assert_eq!(t, na::try_convert(iso).unwrap());
26 prop_assert_eq!(t, na::try_convert(sim).unwrap());
27 prop_assert_eq!(t, na::try_convert(aff).unwrap());
28 prop_assert_eq!(t, na::try_convert(prj).unwrap());
29 prop_assert_eq!(t, na::try_convert(tr).unwrap() );
30
31 prop_assert_eq!(t * p, iso * p);
32 prop_assert_eq!(t * p, sim * p);
33 prop_assert_eq!(t * p, aff * p);
34 prop_assert_eq!(t * p, prj * p);
35 prop_assert_eq!(t * p, tr * p);
36 }
37
38 #[test]
39 fn rotation_conversion(r in rotation3(), v in vector3(), p in point3()) {
40 let uq: UnitQuaternion<f64> = na::convert(r);
41 let iso: Isometry3<f64> = na::convert(r);
42 let sim: Similarity3<f64> = na::convert(r);
43 let aff: Affine3<f64> = na::convert(r);
44 let prj: Projective3<f64> = na::convert(r);
45 let tr: Transform3<f64> = na::convert(r);
46
47 prop_assert!(relative_eq!(r, na::try_convert(uq).unwrap(), epsilon = 1.0e-7));
48 prop_assert!(relative_eq!(r, na::try_convert(iso).unwrap(), epsilon = 1.0e-7));
49 prop_assert!(relative_eq!(r, na::try_convert(sim).unwrap(), epsilon = 1.0e-7));
50 prop_assert_eq!(r, na::try_convert(aff).unwrap());
51 prop_assert_eq!(r, na::try_convert(prj).unwrap());
52 prop_assert_eq!(r, na::try_convert(tr).unwrap() );
53
54 // NOTE: we need relative_eq because Isometry and Similarity use quaternions.
55 prop_assert!(relative_eq!(r * v, uq * v, epsilon = 1.0e-7));
56 prop_assert!(relative_eq!(r * v, iso * v, epsilon = 1.0e-7));
57 prop_assert!(relative_eq!(r * v, sim * v, epsilon = 1.0e-7));
58 prop_assert_eq!(r * v, aff * v);
59 prop_assert_eq!(r * v, prj * v);
60 prop_assert_eq!(r * v, tr * v);
61
62 prop_assert!(relative_eq!(r * p, uq * p, epsilon = 1.0e-7));
63 prop_assert!(relative_eq!(r * p, iso * p, epsilon = 1.0e-7));
64 prop_assert!(relative_eq!(r * p, sim * p, epsilon = 1.0e-7));
65 prop_assert_eq!(r * p, aff * p);
66 prop_assert_eq!(r * p, prj * p);
67 prop_assert_eq!(r * p, tr * p);
68 }
69
70 #[test]
71 fn unit_quaternion_conversion(uq in unit_quaternion(), v in vector3(), p in point3()) {
72 let rot: Rotation3<f64> = na::convert(uq);
73 let iso: Isometry3<f64> = na::convert(uq);
74 let sim: Similarity3<f64> = na::convert(uq);
75 let aff: Affine3<f64> = na::convert(uq);
76 let prj: Projective3<f64> = na::convert(uq);
77 let tr: Transform3<f64> = na::convert(uq);
78
79 prop_assert_eq!(uq, na::try_convert(iso).unwrap());
80 prop_assert_eq!(uq, na::try_convert(sim).unwrap());
81 prop_assert!(relative_eq!(uq, na::try_convert(rot).unwrap(), epsilon = 1.0e-7));
82 prop_assert!(relative_eq!(uq, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
83 prop_assert!(relative_eq!(uq, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
84 prop_assert!(relative_eq!(uq, na::try_convert(tr).unwrap(), epsilon = 1.0e-7) );
85
86 // NOTE: iso and sim use unit quaternions for the rotation so conversions to them are exact.
87 prop_assert!(relative_eq!(uq * v, rot * v, epsilon = 1.0e-7));
88 prop_assert_eq!(uq * v, iso * v);
89 prop_assert_eq!(uq * v, sim * v);
90 prop_assert!(relative_eq!(uq * v, aff * v, epsilon = 1.0e-7));
91 prop_assert!(relative_eq!(uq * v, prj * v, epsilon = 1.0e-7));
92 prop_assert!(relative_eq!(uq * v, tr * v, epsilon = 1.0e-7));
93
94 prop_assert!(relative_eq!(uq * p, rot * p, epsilon = 1.0e-7));
95 prop_assert_eq!(uq * p, iso * p);
96 prop_assert_eq!(uq * p, sim * p);
97 prop_assert!(relative_eq!(uq * p, aff * p, epsilon = 1.0e-7));
98 prop_assert!(relative_eq!(uq * p, prj * p, epsilon = 1.0e-7));
99 prop_assert!(relative_eq!(uq * p, tr * p, epsilon = 1.0e-7));
100 }
101
102 #[test]
103 fn isometry_conversion(iso in isometry3(), v in vector3(), p in point3()) {
104 let sim: Similarity3<f64> = na::convert(iso);
105 let aff: Affine3<f64> = na::convert(iso);
106 let prj: Projective3<f64> = na::convert(iso);
107 let tr: Transform3<f64> = na::convert(iso);
108
109
110 prop_assert_eq!(iso, na::try_convert(sim).unwrap());
111 prop_assert!(relative_eq!(iso, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
112 prop_assert!(relative_eq!(iso, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
113 prop_assert!(relative_eq!(iso, na::try_convert(tr).unwrap(), epsilon = 1.0e-7) );
114
115 prop_assert_eq!(iso * v, sim * v);
116 prop_assert!(relative_eq!(iso * v, aff * v, epsilon = 1.0e-7));
117 prop_assert!(relative_eq!(iso * v, prj * v, epsilon = 1.0e-7));
118 prop_assert!(relative_eq!(iso * v, tr * v, epsilon = 1.0e-7));
119
120 prop_assert_eq!(iso * p, sim * p);
121 prop_assert!(relative_eq!(iso * p, aff * p, epsilon = 1.0e-7));
122 prop_assert!(relative_eq!(iso * p, prj * p, epsilon = 1.0e-7));
123 prop_assert!(relative_eq!(iso * p, tr * p, epsilon = 1.0e-7));
124 }
125
126 #[test]
127 fn similarity_conversion(sim in similarity3(), v in vector3(), p in point3()) {
128 let aff: Affine3<f64> = na::convert(sim);
129 let prj: Projective3<f64> = na::convert(sim);
130 let tr: Transform3<f64> = na::convert(sim);
131
132 prop_assert!(relative_eq!(sim, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
133 prop_assert!(relative_eq!(sim, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
134 prop_assert!(relative_eq!(sim, na::try_convert(tr).unwrap(), epsilon = 1.0e-7));
135
136 prop_assert!(relative_eq!(sim * v, aff * v, epsilon = 1.0e-7));
137 prop_assert!(relative_eq!(sim * v, prj * v, epsilon = 1.0e-7));
138 prop_assert!(relative_eq!(sim * v, tr * v, epsilon = 1.0e-7));
139
140 prop_assert!(relative_eq!(sim * p, aff * p, epsilon = 1.0e-7));
141 prop_assert!(relative_eq!(sim * p, prj * p, epsilon = 1.0e-7));
142 prop_assert!(relative_eq!(sim * p, tr * p, epsilon = 1.0e-7));
143 }
144
145 // XXX test Transform
146 }
147
148 macro_rules! array_vector_conversion(
149 ($($array_vector_conversion_i: ident, $Vector: ident, $SZ: expr);* $(;)*) => {$(
150 #[test]
151 fn $array_vector_conversion_i() {
152 let v = $Vector::from_fn(|i, _| i);
153 let arr: [usize; $SZ] = v.into();
154 let arr_ref: &[usize; $SZ] = v.as_ref();
155 let v2 = $Vector::from(arr);
156
157 for i in 0 .. $SZ {
158 assert_eq!(arr[i], i);
159 assert_eq!(arr_ref[i], i);
160 }
161
162 assert_eq!(v, v2);
163 }
164 )*}
165 );
166
167 array_vector_conversion!(
168 array_vector_conversion_1, Vector1, 1;
169 array_vector_conversion_2, Vector2, 2;
170 array_vector_conversion_3, Vector3, 3;
171 array_vector_conversion_4, Vector4, 4;
172 array_vector_conversion_5, Vector5, 5;
173 array_vector_conversion_6, Vector6, 6;
174 );
175
176 macro_rules! array_row_vector_conversion(
177 ($($array_vector_conversion_i: ident, $Vector: ident, $SZ: expr);* $(;)*) => {$(
178 #[test]
179 fn $array_vector_conversion_i() {
180 let v: $Vector<_> = $Vector::from_fn(|_, i| i);
181 let arr: [usize; $SZ] = v.into();
182 let arr_ref: &[_] = v.as_ref();
183 let v2 = $Vector::from(arr);
184
185 for i in 0 .. $SZ {
186 assert_eq!(arr[i], i);
187 assert_eq!(arr_ref[i], i);
188 }
189
190 assert_eq!(v, v2);
191 }
192 )*}
193 );
194
195 array_row_vector_conversion!(
196 array_row_vector_conversion_1, RowVector1, 1;
197 array_row_vector_conversion_2, RowVector2, 2;
198 array_row_vector_conversion_3, RowVector3, 3;
199 array_row_vector_conversion_4, RowVector4, 4;
200 array_row_vector_conversion_5, RowVector5, 5;
201 array_row_vector_conversion_6, RowVector6, 6;
202 );
203
204 macro_rules! array_matrix_conversion(
205 ($($array_matrix_conversion_i_j: ident, $Matrix: ident, ($NRows: expr, $NCols: expr));* $(;)*) => {$(
206 #[test]
207 fn $array_matrix_conversion_i_j() {
208 let m = $Matrix::from_fn(|i, j| i * 10 + j);
209 let arr: [[usize; $NRows]; $NCols] = m.into();
210 let arr_ref: &[_] = m.as_ref();
211 let m2 = $Matrix::from(arr);
212
213 for i in 0 .. $NRows {
214 for j in 0 .. $NCols {
215 assert_eq!(arr[j][i], i * 10 + j);
216 assert_eq!(arr_ref[j * $NRows + i], i * 10 + j);
217 }
218 }
219
220 assert_eq!(m, m2);
221 }
222 )*}
223 );
224
225 array_matrix_conversion!(
226 array_matrix_conversion_2_2, Matrix2, (2, 2);
227 array_matrix_conversion_2_3, Matrix2x3, (2, 3);
228 array_matrix_conversion_2_4, Matrix2x4, (2, 4);
229 array_matrix_conversion_2_5, Matrix2x5, (2, 5);
230 array_matrix_conversion_2_6, Matrix2x6, (2, 6);
231
232 array_matrix_conversion_3_2, Matrix3x2, (3, 2);
233 array_matrix_conversion_3_3, Matrix3, (3, 3);
234 array_matrix_conversion_3_4, Matrix3x4, (3, 4);
235 array_matrix_conversion_3_5, Matrix3x5, (3, 5);
236 array_matrix_conversion_3_6, Matrix3x6, (3, 6);
237
238 array_matrix_conversion_4_2, Matrix4x2, (4, 2);
239 array_matrix_conversion_4_3, Matrix4x3, (4, 3);
240 array_matrix_conversion_4_4, Matrix4, (4, 4);
241 array_matrix_conversion_4_5, Matrix4x5, (4, 5);
242 array_matrix_conversion_4_6, Matrix4x6, (4, 6);
243
244 array_matrix_conversion_5_2, Matrix5x2, (5, 2);
245 array_matrix_conversion_5_3, Matrix5x3, (5, 3);
246 array_matrix_conversion_5_4, Matrix5x4, (5, 4);
247 array_matrix_conversion_5_5, Matrix5, (5, 5);
248 array_matrix_conversion_5_6, Matrix5x6, (5, 6);
249
250 array_matrix_conversion_6_2, Matrix6x2, (6, 2);
251 array_matrix_conversion_6_3, Matrix6x3, (6, 3);
252 array_matrix_conversion_6_4, Matrix6x4, (6, 4);
253 array_matrix_conversion_6_5, Matrix6x5, (6, 5);
254 array_matrix_conversion_6_6, Matrix6, (6, 6);
255 );
256
257 #[test]
258 fn matrix_slice_from_matrix_ref() {
259 let a = Matrix3x4::new(
260 11.0, 12.0, 13.0, 14.0, 21.0, 22.0, 23.0, 24.0, 31.0, 32.0, 33.0, 34.0,
261 );
262
263 // TODO: What's a more idiomatic/better way to convert a static matrix to a dynamic one?
264 let d = DMatrix::from(a.get((0..a.nrows(), 0..a.ncols())).unwrap());
265
266 // Note: these have to be macros, and not functions, because the input type is different
267 // across the different tests. Moreover, the output type depends on the stride of the input,
268 // which is different for static and dynamic matrices.
269 macro_rules! dynamic_view {
270 ($mref:expr) => {
271 DMatrixView::<_>::from($mref)
272 };
273 }
274 macro_rules! dynamic_view_mut {
275 ($mref:expr) => {
276 DMatrixViewMut::<_>::from($mref)
277 };
278 }
279 macro_rules! fixed_view {
280 ($mref:expr) => {
281 MatrixView::<_, U3, U4, U1, U3>::from($mref)
282 };
283 }
284 macro_rules! fixed_view_mut {
285 ($mref:expr) => {
286 MatrixViewMut::<_, U3, U4, U1, U3>::from($mref)
287 };
288 }
289
290 // TODO: The `into_owned()` is a result of `PartialEq` not being implemented for different
291 // Self and RHS. See issue #674. Once this is implemented, we can remove `into_owned`
292 // from the below tests.
293
294 // Construct views from reference to a
295 {
296 assert_eq!(a, fixed_view!(&a).into_owned());
297 assert_eq!(d, dynamic_view!(&a).into_owned());
298 }
299
300 // Construct views from mutable reference to a
301 {
302 let mut a_clone = a.clone();
303 assert_eq!(a, fixed_view!(&mut a_clone).into_owned());
304 assert_eq!(d, dynamic_view!(&mut a_clone).into_owned());
305 }
306
307 // Construct mutable slices from mutable reference to a
308 {
309 let mut a_clone = a.clone();
310 assert_eq!(a, fixed_view_mut!(&mut a_clone).into_owned());
311 assert_eq!(d, dynamic_view_mut!(&mut a_clone).into_owned());
312 }
313
314 // Construct slices from reference to d
315 {
316 assert_eq!(a, fixed_view!(&d).into_owned());
317 assert_eq!(d, dynamic_view!(&d).into_owned());
318 }
319
320 // Construct slices from mutable reference to d
321 {
322 let mut d_clone = a.clone();
323 assert_eq!(a, fixed_view!(&mut d_clone).into_owned());
324 assert_eq!(d, dynamic_view!(&mut d_clone).into_owned());
325 }
326
327 // Construct mutable slices from mutable reference to d
328 {
329 let mut d_clone = d.clone();
330 assert_eq!(a, fixed_view_mut!(&mut d_clone).into_owned());
331 assert_eq!(d, dynamic_view_mut!(&mut d_clone).into_owned());
332 }
333
334 // Construct slices from a slice of a
335 {
336 let mut a_slice = fixed_view!(&a);
337 assert_eq!(a, fixed_view!(&a_slice).into_owned());
338 assert_eq!(a, fixed_view!(&mut a_slice).into_owned());
339 assert_eq!(d, dynamic_view!(&a_slice).into_owned());
340 assert_eq!(d, dynamic_view!(&mut a_slice).into_owned());
341 }
342
343 // Construct slices from a slice mut of a
344 {
345 // Need a clone of a here, so that we can both have a mutable borrow and compare equality
346 let mut a_clone = a.clone();
347 let mut a_slice = fixed_view_mut!(&mut a_clone);
348
349 assert_eq!(a, fixed_view!(&a_slice).into_owned());
350 assert_eq!(a, fixed_view!(&mut a_slice).into_owned());
351 assert_eq!(d, dynamic_view!(&a_slice).into_owned());
352 assert_eq!(d, dynamic_view!(&mut a_slice).into_owned());
353 assert_eq!(a, fixed_view_mut!(&mut a_slice).into_owned());
354 assert_eq!(d, dynamic_view_mut!(&mut a_slice).into_owned());
355 }
356 }