cgmath
cgmath copied to clipboard
Restore operators for component wise vector mul and div
I just ran into this when upgrading from cgmath 0.7 to cgmath 0.10 (I put it off because a lot of my code broke). While it may not be arithmetically valid to do element wise multiply and divisions it is quite common and convenient to do so, which is why glsl and hlsl vector types support this, as does glm.
Some examples of where I was using this:
// normalise mouse velocity by screen_size - mouse_delta and screen_size are vec2
mouse_delta * 100.0 / screen_size; // was this
(mouse_delta * 100.0).div_element_wise(screen_size); // now this
// initialise particle properties with random variation - min_offset and offset_range are vec3
let offset = min_offset + offset_range * rng.gen::<Vector3<f32>>(); // was this
let offset = min_offset + offset_range.mul_element_wise(rng.gen::<Vector3<f32>>()); // now this
On 0.15.0, the old way should work since mouse_delta and offset_range both implement the VectorSpace trait.
I agree, and along with component-wise multiplication, it would be nice to have a One impl for Vector* for the multiplicative identity.
On 0.15.0, the old way should work since
mouse_deltaandoffset_rangeboth implement theVectorSpacetrait.
AFAICT, this doesn't solve the problem, as VectorSpace doesn't guarantee an implementation of Mul<Self>