vec3f_cast

A4956f14412244c2942ff6746b81fc04
0
squint 101 Jun 20, 2013 at 14:09

There are loads of engines and libraries out there that have their own 3D vector class, and they all have different ways of accessing the content (.x, .x(), .getX(), [0] etc) but they all have in common a constructor which takes 3 reals.

I think it would be nice if each library had a standard function, written in whatever way works with that library:

template <typename T>
T vec3f_cast(const LibraryVectorType & vec)
{
    return T(vec.x, vec.y, vec.z);
}

because then it wouldn’t matter how many libraries I was using, I would always be able to cast any vector of any type to the type I actually needed:

Ogre::Vector3 pos = vec3f_cast<Ogre::Vector3>(body->getPosition());

Yes, I have written it myself for each library I’m using, but wouldn’t it be nice if it was always already there?

4 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jun 20, 2013 at 17:28

How about an operator const float * and a constructor that takes a const float * in each vector class? :) I agree, more interoperability would be good.

A4956f14412244c2942ff6746b81fc04
0
squint 101 Jun 20, 2013 at 17:52

I’ve hit a snag with Quaternions, for which the same solution would be nice, since Bullet and Ogre disagree on whether W comes first or last in the constructor. Hi ho!

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Jun 21, 2013 at 09:02

Sadly you would need some kind of “Official” body to manage this sort of thing.

Can you imagine what a mess that would create?

Imagine you write a really nice library that does something we all use in games, you write it for your own use but are so happy with it you want to make it open source. At the moment you can just go ahead and do that, and if it is as good as you think, you get a lot of love and respect back for doing so.

Imagine you have to send it to some official body first, and they send it back to you because you defined your vector class to have public members X,Y,Z when they insist you define x,y,z.

So you are really kind and go through all your code re-factoring it and send it back to them.

They send it back because you have defined a method CrossProduct and they have defined Cross

You change it

They send it back because you have the wrong copyright message

You give up, call them all a bunch of troglodytes , and the world never sees your wonderful code.

BE CAREFUL WHAT YOU WISH FOR, you might just get it. ;)

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jun 21, 2013 at 13:32

@squint

cast any vector of any type to the type I actually needed

Adaptorsare designed to be used for a specific purpose and hence their reuse is low. If library X enjoys using a class a certain way, it doesn’t make much sense for them to write adaptors for end users to change the way their data is accessed. It’s better to just have their end users do it themselves at the cost of extra cycles to perform the added operations. It will keep their code clean and would even discourage developers from using more than one vector class, which let’s face it, is also a design problem.