File: conversion.rs

package info (click to toggle)
rust-nalgebra 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,340 kB
  • sloc: makefile: 2
file content (356 lines) | stat: -rw-r--r-- 14,869 bytes parent folder | download | duplicates (3)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#![cfg(all(feature = "proptest-support"))]
use na::{
    self, Affine3, Isometry3, Matrix2, Matrix2x3, Matrix2x4, Matrix2x5, Matrix2x6, Matrix3,
    Matrix3x2, Matrix3x4, Matrix3x5, Matrix3x6, Matrix4, Matrix4x2, Matrix4x3, Matrix4x5,
    Matrix4x6, Matrix5, Matrix5x2, Matrix5x3, Matrix5x4, Matrix5x6, Matrix6, Matrix6x2, Matrix6x3,
    Matrix6x4, Matrix6x5, Projective3, Rotation3, RowVector1, RowVector2, RowVector3, RowVector4,
    RowVector5, RowVector6, Similarity3, Transform3, UnitQuaternion, Vector1, Vector2, Vector3,
    Vector4, Vector5, Vector6,
};
use na::{DMatrix, DMatrixView, DMatrixViewMut, MatrixView, MatrixViewMut};
use na::{U1, U3, U4};

use crate::proptest::*;
use proptest::{prop_assert, prop_assert_eq, proptest};

proptest! {
    #[test]
    fn translation_conversion(t in translation3(), p in point3()) {
        let iso: Isometry3<f64>   = na::convert(t);
        let sim: Similarity3<f64> = na::convert(t);
        let aff: Affine3<f64>     = na::convert(t);
        let prj: Projective3<f64> = na::convert(t);
        let tr:  Transform3<f64>  = na::convert(t);

        prop_assert_eq!(t, na::try_convert(iso).unwrap());
        prop_assert_eq!(t, na::try_convert(sim).unwrap());
        prop_assert_eq!(t, na::try_convert(aff).unwrap());
        prop_assert_eq!(t, na::try_convert(prj).unwrap());
        prop_assert_eq!(t, na::try_convert(tr).unwrap() );

        prop_assert_eq!(t * p, iso * p);
        prop_assert_eq!(t * p, sim * p);
        prop_assert_eq!(t * p, aff * p);
        prop_assert_eq!(t * p, prj * p);
        prop_assert_eq!(t * p, tr  * p);
    }

    #[test]
    fn rotation_conversion(r in rotation3(), v in vector3(), p in point3()) {
        let uq:  UnitQuaternion<f64> = na::convert(r);
        let iso: Isometry3<f64>      = na::convert(r);
        let sim: Similarity3<f64>    = na::convert(r);
        let aff: Affine3<f64>        = na::convert(r);
        let prj: Projective3<f64>    = na::convert(r);
        let tr:  Transform3<f64>     = na::convert(r);

        prop_assert!(relative_eq!(r, na::try_convert(uq).unwrap(),  epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r, na::try_convert(iso).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r, na::try_convert(sim).unwrap(), epsilon = 1.0e-7));
        prop_assert_eq!(r, na::try_convert(aff).unwrap());
        prop_assert_eq!(r, na::try_convert(prj).unwrap());
        prop_assert_eq!(r, na::try_convert(tr).unwrap() );

        // NOTE: we need relative_eq because Isometry and Similarity use quaternions.
        prop_assert!(relative_eq!(r * v, uq  * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r * v, iso * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r * v, sim * v, epsilon = 1.0e-7));
        prop_assert_eq!(r * v, aff * v);
        prop_assert_eq!(r * v, prj * v);
        prop_assert_eq!(r * v, tr  * v);

        prop_assert!(relative_eq!(r * p, uq  * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r * p, iso * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(r * p, sim * p, epsilon = 1.0e-7));
        prop_assert_eq!(r * p, aff * p);
        prop_assert_eq!(r * p, prj * p);
        prop_assert_eq!(r * p, tr  * p);
    }

    #[test]
    fn unit_quaternion_conversion(uq in unit_quaternion(), v in vector3(), p in point3()) {
        let rot: Rotation3<f64>   = na::convert(uq);
        let iso: Isometry3<f64>   = na::convert(uq);
        let sim: Similarity3<f64> = na::convert(uq);
        let aff: Affine3<f64>     = na::convert(uq);
        let prj: Projective3<f64> = na::convert(uq);
        let tr:  Transform3<f64>  = na::convert(uq);

        prop_assert_eq!(uq, na::try_convert(iso).unwrap());
        prop_assert_eq!(uq, na::try_convert(sim).unwrap());
        prop_assert!(relative_eq!(uq, na::try_convert(rot).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq, na::try_convert(tr).unwrap(), epsilon = 1.0e-7) );

        // NOTE: iso and sim use unit quaternions for the rotation so conversions to them are exact.
        prop_assert!(relative_eq!(uq * v, rot * v, epsilon = 1.0e-7));
        prop_assert_eq!(uq * v, iso * v);
        prop_assert_eq!(uq * v, sim * v);
        prop_assert!(relative_eq!(uq * v, aff * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq * v, prj * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq * v, tr  * v, epsilon = 1.0e-7));

        prop_assert!(relative_eq!(uq * p, rot * p, epsilon = 1.0e-7));
        prop_assert_eq!(uq * p, iso * p);
        prop_assert_eq!(uq * p, sim * p);
        prop_assert!(relative_eq!(uq * p, aff * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq * p, prj * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(uq * p, tr  * p, epsilon = 1.0e-7));
    }

    #[test]
    fn isometry_conversion(iso in isometry3(), v in vector3(), p in point3()) {
        let sim: Similarity3<f64> = na::convert(iso);
        let aff: Affine3<f64>     = na::convert(iso);
        let prj: Projective3<f64> = na::convert(iso);
        let tr:  Transform3<f64>  = na::convert(iso);


        prop_assert_eq!(iso, na::try_convert(sim).unwrap());
        prop_assert!(relative_eq!(iso, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso, na::try_convert(tr).unwrap(), epsilon = 1.0e-7) );

        prop_assert_eq!(iso * v, sim * v);
        prop_assert!(relative_eq!(iso * v, aff * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso * v, prj * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso * v, tr  * v, epsilon = 1.0e-7));

        prop_assert_eq!(iso * p, sim * p);
        prop_assert!(relative_eq!(iso * p, aff * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso * p, prj * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(iso * p, tr  * p, epsilon = 1.0e-7));
    }

    #[test]
    fn similarity_conversion(sim in similarity3(), v in vector3(), p in point3()) {
        let aff: Affine3<f64>     = na::convert(sim);
        let prj: Projective3<f64> = na::convert(sim);
        let tr:  Transform3<f64>  = na::convert(sim);

        prop_assert!(relative_eq!(sim, na::try_convert(aff).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim, na::try_convert(prj).unwrap(), epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim, na::try_convert(tr).unwrap(),  epsilon = 1.0e-7));

        prop_assert!(relative_eq!(sim * v, aff * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim * v, prj * v, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim * v, tr  * v, epsilon = 1.0e-7));

        prop_assert!(relative_eq!(sim * p, aff * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim * p, prj * p, epsilon = 1.0e-7));
        prop_assert!(relative_eq!(sim * p, tr  * p, epsilon = 1.0e-7));
    }

    // XXX test Transform
}

macro_rules! array_vector_conversion(
    ($($array_vector_conversion_i: ident, $Vector: ident, $SZ: expr);* $(;)*) => {$(
        #[test]
        fn $array_vector_conversion_i() {
            let v       = $Vector::from_fn(|i, _| i);
            let arr: [usize; $SZ] = v.into();
            let arr_ref: &[usize; $SZ] = v.as_ref();
            let v2      = $Vector::from(arr);

            for i in 0 .. $SZ {
                assert_eq!(arr[i], i);
                assert_eq!(arr_ref[i], i);
            }

            assert_eq!(v, v2);
        }
    )*}
);

array_vector_conversion!(
    array_vector_conversion_1,  Vector1,  1;
    array_vector_conversion_2,  Vector2,  2;
    array_vector_conversion_3,  Vector3,  3;
    array_vector_conversion_4,  Vector4,  4;
    array_vector_conversion_5,  Vector5,  5;
    array_vector_conversion_6,  Vector6,  6;
);

macro_rules! array_row_vector_conversion(
    ($($array_vector_conversion_i: ident, $Vector: ident, $SZ: expr);* $(;)*) => {$(
        #[test]
        fn $array_vector_conversion_i() {
            let v: $Vector<_>       = $Vector::from_fn(|_, i| i);
            let arr: [usize; $SZ] = v.into();
            let arr_ref: &[_] = v.as_ref();
            let v2      = $Vector::from(arr);

            for i in 0 .. $SZ {
                assert_eq!(arr[i], i);
                assert_eq!(arr_ref[i], i);
            }

            assert_eq!(v, v2);
        }
    )*}
);

array_row_vector_conversion!(
    array_row_vector_conversion_1,  RowVector1,  1;
    array_row_vector_conversion_2,  RowVector2,  2;
    array_row_vector_conversion_3,  RowVector3,  3;
    array_row_vector_conversion_4,  RowVector4,  4;
    array_row_vector_conversion_5,  RowVector5,  5;
    array_row_vector_conversion_6,  RowVector6,  6;
);

macro_rules! array_matrix_conversion(
    ($($array_matrix_conversion_i_j: ident, $Matrix: ident, ($NRows: expr, $NCols: expr));* $(;)*) => {$(
        #[test]
        fn $array_matrix_conversion_i_j() {
            let m       = $Matrix::from_fn(|i, j| i * 10 + j);
            let arr: [[usize; $NRows]; $NCols] = m.into();
            let arr_ref: &[_] = m.as_ref();
            let m2      = $Matrix::from(arr);

            for i in 0 .. $NRows {
                for j in 0 .. $NCols {
                    assert_eq!(arr[j][i], i * 10 + j);
                    assert_eq!(arr_ref[j * $NRows + i], i * 10 + j);
                }
            }

            assert_eq!(m, m2);
        }
    )*}
);

array_matrix_conversion!(
    array_matrix_conversion_2_2, Matrix2,   (2, 2);
    array_matrix_conversion_2_3, Matrix2x3, (2, 3);
    array_matrix_conversion_2_4, Matrix2x4, (2, 4);
    array_matrix_conversion_2_5, Matrix2x5, (2, 5);
    array_matrix_conversion_2_6, Matrix2x6, (2, 6);

    array_matrix_conversion_3_2, Matrix3x2, (3, 2);
    array_matrix_conversion_3_3, Matrix3,   (3, 3);
    array_matrix_conversion_3_4, Matrix3x4, (3, 4);
    array_matrix_conversion_3_5, Matrix3x5, (3, 5);
    array_matrix_conversion_3_6, Matrix3x6, (3, 6);

    array_matrix_conversion_4_2, Matrix4x2, (4, 2);
    array_matrix_conversion_4_3, Matrix4x3, (4, 3);
    array_matrix_conversion_4_4, Matrix4,   (4, 4);
    array_matrix_conversion_4_5, Matrix4x5, (4, 5);
    array_matrix_conversion_4_6, Matrix4x6, (4, 6);

    array_matrix_conversion_5_2, Matrix5x2, (5, 2);
    array_matrix_conversion_5_3, Matrix5x3, (5, 3);
    array_matrix_conversion_5_4, Matrix5x4, (5, 4);
    array_matrix_conversion_5_5, Matrix5,   (5, 5);
    array_matrix_conversion_5_6, Matrix5x6, (5, 6);

    array_matrix_conversion_6_2, Matrix6x2, (6, 2);
    array_matrix_conversion_6_3, Matrix6x3, (6, 3);
    array_matrix_conversion_6_4, Matrix6x4, (6, 4);
    array_matrix_conversion_6_5, Matrix6x5, (6, 5);
    array_matrix_conversion_6_6, Matrix6,   (6, 6);
);

#[test]
fn matrix_slice_from_matrix_ref() {
    let a = Matrix3x4::new(
        11.0, 12.0, 13.0, 14.0, 21.0, 22.0, 23.0, 24.0, 31.0, 32.0, 33.0, 34.0,
    );

    // TODO: What's a more idiomatic/better way to convert a static matrix to a dynamic one?
    let d = DMatrix::from(a.get((0..a.nrows(), 0..a.ncols())).unwrap());

    // Note: these have to be macros, and not functions, because the input type is different
    // across the different tests. Moreover, the output type depends on the stride of the input,
    // which is different for static and dynamic matrices.
    macro_rules! dynamic_view {
        ($mref:expr) => {
            DMatrixView::<_>::from($mref)
        };
    }
    macro_rules! dynamic_view_mut {
        ($mref:expr) => {
            DMatrixViewMut::<_>::from($mref)
        };
    }
    macro_rules! fixed_view {
        ($mref:expr) => {
            MatrixView::<_, U3, U4, U1, U3>::from($mref)
        };
    }
    macro_rules! fixed_view_mut {
        ($mref:expr) => {
            MatrixViewMut::<_, U3, U4, U1, U3>::from($mref)
        };
    }

    // TODO: The `into_owned()` is a result of `PartialEq` not being implemented for different
    // Self and RHS. See issue #674. Once this is implemented, we can remove `into_owned`
    // from the below tests.

    // Construct views from reference to a
    {
        assert_eq!(a, fixed_view!(&a).into_owned());
        assert_eq!(d, dynamic_view!(&a).into_owned());
    }

    // Construct views from mutable reference to a
    {
        let mut a_clone = a.clone();
        assert_eq!(a, fixed_view!(&mut a_clone).into_owned());
        assert_eq!(d, dynamic_view!(&mut a_clone).into_owned());
    }

    // Construct mutable slices from mutable reference to a
    {
        let mut a_clone = a.clone();
        assert_eq!(a, fixed_view_mut!(&mut a_clone).into_owned());
        assert_eq!(d, dynamic_view_mut!(&mut a_clone).into_owned());
    }

    // Construct slices from reference to d
    {
        assert_eq!(a, fixed_view!(&d).into_owned());
        assert_eq!(d, dynamic_view!(&d).into_owned());
    }

    // Construct slices from mutable reference to d
    {
        let mut d_clone = a.clone();
        assert_eq!(a, fixed_view!(&mut d_clone).into_owned());
        assert_eq!(d, dynamic_view!(&mut d_clone).into_owned());
    }

    // Construct mutable slices from mutable reference to d
    {
        let mut d_clone = d.clone();
        assert_eq!(a, fixed_view_mut!(&mut d_clone).into_owned());
        assert_eq!(d, dynamic_view_mut!(&mut d_clone).into_owned());
    }

    // Construct slices from a slice of a
    {
        let mut a_slice = fixed_view!(&a);
        assert_eq!(a, fixed_view!(&a_slice).into_owned());
        assert_eq!(a, fixed_view!(&mut a_slice).into_owned());
        assert_eq!(d, dynamic_view!(&a_slice).into_owned());
        assert_eq!(d, dynamic_view!(&mut a_slice).into_owned());
    }

    // Construct slices from a slice mut of a
    {
        // Need a clone of a here, so that we can both have a mutable borrow and compare equality
        let mut a_clone = a.clone();
        let mut a_slice = fixed_view_mut!(&mut a_clone);

        assert_eq!(a, fixed_view!(&a_slice).into_owned());
        assert_eq!(a, fixed_view!(&mut a_slice).into_owned());
        assert_eq!(d, dynamic_view!(&a_slice).into_owned());
        assert_eq!(d, dynamic_view!(&mut a_slice).into_owned());
        assert_eq!(a, fixed_view_mut!(&mut a_slice).into_owned());
        assert_eq!(d, dynamic_view_mut!(&mut a_slice).into_owned());
    }
}