Archived

This topic is now archived and is closed to further replies.

EbonySeraph

Vector Template

Recommended Posts

EbonySeraph    122
I have a question about a vector template class I want to create. I don''t want to make anything too complicated, but I just want a vector class that can handle ints, doubles, and floats, and also be 2D or 3D. Should I just make a normal template class for a vector and use a constructor''s parameter to set wether or not its 2D or 3D or is there another way to pass a parameter to specify how many dimensions?

Share this post


Link to post
Share on other sites
civguy    308
Hm... it could get pretty complicated if you wanted. Take a look at these two

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-VectQuat&forum=cotd&id=-1

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-UnrollingLoopsMeta&forum=totd&id=-1

The first link shows how to make n-dimensional vector space with quite a lot functionality too. The latter link shows how to unroll the loops with the compiler so that runtime speed is optimal. And it's still not as fast as a hand-written version because it needs to use arrays.

In practice, I suggest you just make 2 separate vector classes, Vector2d and Vector3d, and write them manually. The only template parameter would be the type. And use boost/operators.hpp if you're really lazy . I don't know why you'd want to combine 2d and 3d vectors in to a single class anyway..

[edited by - civguy on September 30, 2002 11:50:59 AM]

Share this post


Link to post
Share on other sites
Oluseyi    2109
quote:
Original post by civguy
The latter link shows how to unroll the loops with the compiler so that runtime speed is optimal. And it''s still not as fast as a hand-written version because it needs to use arrays.

And yet it''s possible to write template vector operations that are faster - with a little assembly help... There''s an article here on GameDev about it.

quote:

I don''t know why you''d want to combine 2d and 3d vectors in to a single class anyway...

It''s not a bad idea. You have less code to write, and only need to specialize certain functions (most of which should not be member functions anyway, like cross and dot product, largely because they obfuscate intent).

Share this post


Link to post
Share on other sites
EbonySeraph    122
Thanks for the insight.

I do realize that a cross product can''t be calculated without certain conditions.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites