bitshifter / glam-rs

A simple and fast linear algebra library for games and graphics
Apache License 2.0
1.5k stars 152 forks source link

Multiply BVecN and VecN? Implement Into/From for BVecN to VecN with 1.0:s and 0.0:s #504

Closed EriKWDev closed 4 months ago

EriKWDev commented 5 months ago

Bitmasks are cool and would be even more useful if they could be multiplied with a vector of same dimension. This could be done either if the BVecN could be converted into a vector of 0.0:s and 1.0:s, or if std::ops::Mul was implemented for the permutations of BVec and all other Vec

let a = vec3a(0.1, -0.5, 10.0);
let mask = a.cmple(Vec3A::splat(0.1);
let b = a * mask; // NOTE: not implemented atm
assert!(b == vec3a(0.1, -0.5, 0.0);

I feel like this could be done for all vecs of same dimensions?

BVec3A * Vec3A, BVec3 * Vec3, BVec3 * IVec3, BVec3 * I16Vec3 and so on..

bitshifter commented 4 months ago

For the most part I only support behaviour that is supported in Rust itself. Rust will not let you multiple a f32 or i32 by a bool.

1.0_f32 * true // compile error
1.0_f32 * f32::from(true) // ok

What it does support is a from conversion from bool to float or bool to integer.

1.0_f32 * f32::from(true) // ok

glam also has from impls for creating a Vec3 or IVec3 from a BVec3 or BVec3A, so while what you are proposing won't work the following will

Vec3A::from(BVec3A) * Vec3A, Vec3::from(BVec3) * Vec3, IVec3::from(BVec3) * IVec3, I16Vec3::from(BVec3) * I16Vec3

Possibly .into() will work in these cases as well if the compiler can work out what type conversion you are after.

As for supporting arithmetic operations on vector masks for other vector types, I don't intend to add that for the reasons given above, that is rust itself doesn't support it.