Jump to content
  • Advertisement
Sign in to follow this  
stu_pb

Vector3 class, to template or not?

This topic is 5411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are there any good reasons to use a vector of integers over a vector of floats? What are some situations where a vector of ints makes more sense than a vector of floats? I am trying to decide how to implement my vector class! Here are some uses of vectors and their types that I can think of. Position - Float View - Float Up - Float Color - Float (0.0 - 1.0) Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You, sir, are suffering from Templater's Syndrome (made up on the spot), where you engineer unnecessary genericity into your code despite the fact that you don't clearly intend to use it.

When last did you see 3D calculations involving anything other than floating-point values, except on, say, the GBA or PocketPC?

Share this post


Link to post
Share on other sites
Well, at some point you'll have to convert that vector of colour floats into a vector of colour ints (well, depending on the platform).

Really, there's nothing stopping you from doing it as a template and then typedefing yourself a float type to prefer.

Share this post


Link to post
Share on other sites
I think you should just make it templated, and provide a templated coversion copy constructor & assignment operators so you can work with a vector3 of doubles with a vector3 of floats or int or short etc e.g.


template < typename T >
struct vector3 {

T x, y, z;

/* .... * /

vector3(const T& _x = T(),
const T& _y = T(),
const T& _z = T())
: x(_x), y(_y), z(_z) {}


template < typename U >
vector3(const vector3<U>& u)
: x(u.x), y(u.y), z(u.z) {}

template < typename U >
vector3(const vector3<U>& u) { /* .... * / }
/* .... * /
};






add some wizardry:


#include <cstddef>

template < typename T >
struct vector3 {

typedef size_t size_type

T x, y, z;

/* .... * /
private:

typedef T vector3<T>::* const p3[3];
static const p3 v; //<-- wizardry, don't worry its magic

vector3(const T& _x = T(),
const T& _y = T(),
const T& _z = T())
: x(_x), y(_y), z(_z) {}


template < typename U >
vector3(const vector3& u)
: x(u.x), y(u.y), z(u.z) {}

template < typename U >
vector3(const vector3& u) { /* .... * / }

const T& vector3<T>::operator[](size_type i) const {
return this->*v; //<-- this is where the magic happens
}

T& vector3<T>::operator[](size_type i) {
return this->*v; //<-- this is where the magic happens
}

/* .... * /
};

template < typename T >
const typename vector3<T>::p2 vector3<T>::v = { &vector3<T>::x, &vector3<T>::y, &vector3<T>::z };


and your good to go...

[Edited by - snk_kid on September 29, 2004 2:27:38 AM]

Share this post


Link to post
Share on other sites
Since nobody mentioned any reasons why I would need ints (other data types other than float) I think I will just stick with a non-templated class. If I change my mind later its not that bad to find and replace Vector3 with Vector3<type>.

Thanks for the replies.

Share this post


Link to post
Share on other sites
I've never used a vector of integers, but I've used a vector of floats for game 3D rendering, a vector of doubles for raytracing rendering when I was getting accuracy problems, a vector of fixed point numbers for collision detection code where accuracy was very important and a vector of complex numbers for geometry problems. Unless you have any immediate need for it though, just write your class using floats, then later you can template it and replace your original float class with a typedef. Replacing all the floats with a template parameter shouldn't take you very long, so I wouldn't waste time doing it in advance. Making templates out of every can be classed as good reusability in some cases and over engineering in others.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
Well, at some point you'll have to convert that vector of colour floats into a vector of colour ints (well, depending on the platform).
Really, there's nothing stopping you from doing it as a template and then typedefing yourself a float type to prefer.


Hello,

While I agree that a RGB color can be represented as a 3 coordinate vector, I totally disagree with the "use a Vector3 for all 3 coord vector" design, which would end up using pixel.y instead of pixel.g (or point.r instead of point.x ?). Plus, color operations are not the same as vector operations since (at least) colors may be saturated (min 0.0f and max 1.0f, as stu_pb already noticed) and vector won't.

From my experience, having a template Vector3 class is not that useful. A VectorN<TYPE,N> class (the mathematical one, not a container) may be more useful f you really plan to do hard math things.

Regards,

Share this post


Link to post
Share on other sites
I made a "VECTOR<order,TYPE=float>" template, with specialisations for order-{1,2,3,4} vectors.

The question I asked was: is it harder to type "TYPE" in the definition of the VECTOR's members than it is to type "float"? If not, why not make it templated?

Assuming you could never need a vector of ints is a bad move. If it later turns out you could make do with ints somewhere, and your vector only permits floats, it's going to be difficult to even check if ints would suffice.

Share this post


Link to post
Share on other sites
Can't think of a reason for using a vector of ints, but I can think of several places where a vector of doubles would be handy.

If you go to www.fluidstudios.com you'll find a very nice matrix template class where vectors are actually 3x1 (or 4x1) matrices. That way you only need to write code for matrix-matrix operations and the vector-vector and matrix-vector fall out of that. Kinda cool.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!