nalgebra
nalgebra copied to clipboard
Component wise multiplication with nalgebra-glm
Cannot multiply two vectors component wise (glm 0.7.0):
use glm::*;
let brick_size = vec2(64f32, 32f32);
let brick_spacing = vec2(8f32, 8f32);
let xxx = brick_size * brick_spacing;
error[E0277]: the trait bound `nalgebra::base::constraint::ShapeConstraint: nalgebra::base::constraint::DimEq<glm::U1, glm::U2>` is not satisfied
--> src/game.rs:72:26
|
72 | let xxx = brick_size * brick_spacing;
| ^ the trait `nalgebra::base::constraint::DimEq<glm::U1, glm::U2>` is not implemented for `nalgebra::base::constraint::ShapeConstraint`
|
= help: the following implementations were found:
<nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<D, D>>
<nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<D, nalgebra::base::dimension::Dynamic>>
<nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<nalgebra::base::dimension::Dynamic, D>>
= note: required because of the requirements on the impl of `nalgebra::base::constraint::AreMultipliable<glm::U2, glm::U1, glm::U2, glm::U1>` for `nalgebra::base::constraint::ShapeConstraint`
= note: required because of the requirements on the impl of `std::ops::Mul` for `nalgebra::base::matrix::Matrix<f32, glm::U2, glm::U1, nalgebra::base::array_storage::ArrayStorage<f32, glm::U2, glm::U1>>`
Hi! Yes, it is not possible because the multiplication operator is already used for matrix multiplication. For componentwise multiplication you can do let xxx = brick_size.component_mul(&brick_spacing)
.
I tripped over this one as well. It became extra confusing when glm::convert(brick_size) * brick_spacing
works, which I added because one was IVec and the other Vec. I assume the glm::convert is capable of flipping rows -> columns?
Anyway, maybe it can be added to https://www.nalgebra.org/rustdoc_glm/nalgebra_glm/index.html#main-differences-compared-to-glm seems like it'll trip up a lot of users.
I also tripped over this issue. Finding this thread was also not easy, because the title "nalgebra-glm" is pretty generic when you are searching for a issue with elementwise multiplication.
Is glm::convert
or component_mut
the best approach ?
@rokonio The recommended approach is component_mut
.