nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Component wise multiplication with nalgebra-glm

Open ciechowoj opened this issue 4 years ago • 6 comments

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;

ciechowoj avatar Apr 10 '20 09:04 ciechowoj

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>>`

ciechowoj avatar Apr 10 '20 10:04 ciechowoj

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).

sebcrozet avatar Apr 12 '20 12:04 sebcrozet

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.

therocode avatar Jul 07 '20 13:07 therocode

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.

Tastaturtaste avatar Jun 13 '21 13:06 Tastaturtaste

Is glm::convert or component_mut the best approach ?

rokonio avatar Dec 29 '21 18:12 rokonio

@rokonio The recommended approach is component_mut.

sebcrozet avatar Dec 30 '21 09:12 sebcrozet