cgmath icon indicating copy to clipboard operation
cgmath copied to clipboard

Add type defs for common types

Open bitshifter opened this issue 9 years ago • 4 comments

It's quite verbose having to type the full type signature for common vector and matrix types when type inference cannot be used, e.g. writing structs or non generic functions. For this reason it would be good to have aliases for commonly used types (like VectorN, MatrixN etc). GLM is a good example of doing this, it's also one of the recommendations of this post on writing good vector math libraries for graphics http://www.reedbeta.com/blog/2013/12/28/on-vector-math-libraries/.

For example, I have the following struct and function:

pub struct ParticleData {
    position: Vec<cgmath::Vector3<f32>>,
    velocity: Vec<cgmath::Vector3<f32>>,
}

It would be a lot less verbose as (to use GLM format):

pub struct ParticleData {
    position: Vec<cgmath::Vec3>,
    velocity: Vec<cgmath::Vec3>,
}

The GLM typedefs for vec are listed here http://glm.g-truc.net/0.9.7/api/a00132.html and for matrices are here http://glm.g-truc.net/0.9.7/api/a00120_source.html#l00344. I'm not necessarily recommending adding all of those, GLM is mimicking all the types available in GLSL.

In my experience, game math uses float 99% of the time, so it would be good to provide type defs for the f32 types at least, VecN and MatN (for square matrices) seem like common choices.

bitshifter avatar Nov 09 '15 12:11 bitshifter

Hmm, yeah. Might be useful. Also, nice article!

brendanzab avatar Nov 09 '15 20:11 brendanzab

Yeah, that article was a good find, glad you liked it.

bitshifter avatar Nov 10 '15 08:11 bitshifter

Perhaps the scalar type parameters could also default to f32. That is,

struct Vector3<S = f32> { ... }

let foo: Vec<Vector3> = vec![];

brendanzab avatar Nov 11 '15 00:11 brendanzab

Yeah, that could work.

bitshifter avatar Nov 11 '15 01:11 bitshifter