Extraction of a Vector3 from a Vector6


#1

Hi there,

I can’t find a way to extract a Vector3 from a subset of a Vector6. I always get some kind of type mismatch error.

Here is some code to reproduce the issue:

 let vec6 = nalgebra::Vector6::<f64>::zeros();
 let vec3: nalgebra::Vector3<f64> = vec6.fixed_slice::<nalgebra::U3, nalgebra::U1>(0, 0);

and the associated error:

error[E0308]: mismatched types                                                                                                                                                                                                                                                             
  --> src/sva/utility.rs:15:44                                                                                                                                                                                                                                                             
   |                                                                                                                                                                                                                                                                                       
15 |         let vec3: nalgebra::Vector3<f64> = vec6.fixed_slice::<nalgebra::U3, nalgebra::U1>(0, 0);                                                                                                                                                                                      
   |                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `nalgebra::MatrixArray`, found struct `nalgebra::SliceStorage`                                                                                                        
   |                                                                                                                                                                                                                                                                                       
   = note: expected type `nalgebra::Matrix<_, _, _, nalgebra::MatrixArray<f64, nalgebra::U3, nalgebra::U1>>`                                                                                                                                                                               
              found type `nalgebra::Matrix<_, _, _, nalgebra::SliceStorage<'_, f64, nalgebra::U3, nalgebra::U1, nalgebra::U1, nalgebra::U6>>`                             

Coming from the C++ Eigen world, I would have imagined that since the dimensions are given at compile time, there should be no issues assigning a fixed size slice to a fixed size vector. But I’m very new to Rust and nalgebra so I might be missing something.

Can someone point me in the right direction?


#2

Hi!

One difference between C++ and Rust is that Rust does not allow implicit conversion. Therefore you cannot implicitly convert a vector slice to a plain vector. To perform the conversion explicitly you can call .into() on the slice (or equivalently Vector3::from(your_slice)). Also note that you may use .fixed_rows::<U3>(0) instead of the more general .fixed_slice.

An alternative which is the easiest way of extracting the first three components of your vector is to use swizzling: let vec3 = vec6.xyz();.


#3

Ok thanks for the explanations, I get it now.

Didn’t know about he vec6.xyz() function, is there something similar for the last 3 components? I need to extract both.


#4

No, swizzling is currently limited to the x, y, and z components. Note that you can access individual components of the 6D vector though, so the following will work too for the last three components: Vector3::new(vec6.w, vec6.a, vec6.b).


#5

Thanks, but I think I’ll stick to fixed_rows for both cases because using w, a and b is not very intuitive to me compared to x, y and z and might look strange in the code