# Rosario 3D

My developer blog

## Vector class

1 March 2010

Posted by on Difficulty: medium (a lot of template)

I want to write an article to speak about quaternion and how beautiful they are to implement orientation and keyframe interpolation. But before speaking about quaternion is better to start from the base and create a class for vector.

### Vector class

When I speak about vector I refer to maths vector, not an array. We will develop general version of a vector class that can be used to record our geometry data or other multidimensional data.

This implementation is inspired by the nVidia vector class that you can find with the nVidia SDK, is one of the best Vector class implementation I found. Let’s try to make it better.

### Requirement:

- Speed: Vector class should be very fast, in a 3d program we will use a lot of vector maths and the speed of this class is crucial for the whole program.
- Small size: I also want my class to be compact, I will use the vector class to store the vertex attribute, I don’t want any other data in the middle.
- Readability: I want to access to the y coordinate with the .y notation

For the first two reason it’s not wise to use virtual function in our vector class. Virtual function are (slightly) slower, but the most important is that using virtual function will insert an hidden pointer that will increase the size considerably.

Let’s start with the first part of the class, first of all I’ll not create a class but a struct, cause I want all my data to be public, yep I can make the data private and then use getX, getY, setX, setY… but why? This will make only code less readable. A vector is a very simple data that don’t have anything to hide.

template<typename T> struct Vec4Container { static unsigned const len = 4; union{ struct { T x, y, z, w; }; struct { T r, g, b, a; }; struct { T s, t, u, v; }; T data_[len]; }; };

I let you immagine how the Vec3Container and Vec2Container is be made.

I have used a template so my class is type independent, now I can create a vector of float, int and so on. I have used union, so I can access to my value with the .x .y .z notation or if my vec represent a color I can use .r .g .b .a or stuv for texture coordinate. OpenGL use s, t, r, q but we already used r in rgb.

Here the real vector class, the one that will perform the computation

template<class container, class T> struct Vec : public container { public: static size_t size() { return container::len; } Vec() { } explicit Vec(const T *v) { for(size_t i = 0; i < size(); i++) container::data_[i]=v[i]; } explicit Vec(const T &v) { for(size_t i = 0; i < size(); i++) container::data_[i]=v; } Vec(const Vec &vv) { for(size_t i = 0; i < size(); i++) container::data_[i]=vv.data_[i]; } ...

This struct derive from container, so we can access the same data. I also added some constructor, the first one is the default constructor, I don’t want to initialize my vector if I don’t explicitly needed, then I have a constructor from an array, a constructor from a value. Notice that I have added the explicit keyword, cause I don’t want that a single number can be interpreted as a Vec, this can lead to strange behaviors and confusion. I have added a size operator that return the length of the vector, useful for cycles. Note that this struct don’t add any overhead. The only problem is that cause we are using template C++ don’t know where data_ came from, and we must specify it every time.

We need one more constructor, is very useful to specify a vector like this b(4, 3) or w(1, 3, 5), we need some other classes

template<class T> struct Vec3 : public Vec<Vec3Container<T> > { Vec3() {}; explicit Vec3(const T &v) : Vec<Vec3Container<T>, T>(v){ } explicit Vec3(const T *v) : Vec<Vec3Container<T>, T>(v){ } Vec3(const T& v0, const T& v1, const T& v2){ Vec<Vec3Container<T>, T>::x = v0; Vec<Vec3Container<T>, T>::y = v1; Vec<Vec3Container<T>, T>::z = v2; } };

this class will declare only the constructors. The Vec2 and Vec4 are very similar.

Now we can define some basic operation for Vec:

... T& operator[] (int i){ return container::data_[i]; } const T& operator[] (int i) const { return container::data_[i]; } Vec& operator +=(const Vec &rop){ for(size_t i = 0; i < size(); i++) container::data_[i]+= rop.data_[i]; return *this; } Vec& operator -=(const Vec &rop){ for(size_t i = 0; i < size(); i++) container::data_[i]-= rop.data_[i]; return *this; } ...

There is no problem with sum or subtraction, but what we have to do with multiplication?

We can define different type of multiplication for vector, the dot product, the cross product, pairwise multiplication or scalar multiplication, every operation deserve a * operator. For clarity sake I’ll use a named method for dot and cross product and I’ll reserve the * operator for pairwise and scalar product.

... Vec& operator *=(const T &rop){ for(size_t i = 0; i < size(); i++) container::data_[i]*= rop; return *this; } Vec& operator *=(const Vec &rop){ for(size_t i = 0; i < size(); i++) container::data_[i]*= rop.data_[i]; return *this; } ...

Dot product is a binary operation, so it’s better to make it extern to the class, same thing for cross product.

template<class container, typename T> T dot(const Vec<container, T>& rOp, const Vec<container, T>& lOp) { T ret(0); for(size_t i = 0; i < container::len; i++) ret += rOp[i] * lOp[i]; return ret; }

We can specialize the cross funtion to make is more efficient

template<typename T> Vec3<T> cross(const Vec3<T>& rOp, const Vec3<T&>& lOp) { Vec3 res; res.x = rOp.y * lOp.z - rOp.z * lOp.y; res.y = rOp.x * lOp.z - rOp.z * lOp.x; res.z = rOp.x * lOp.y - rOp.y * lOp.x; return res; }

Other function like normalize, length and binary operator are trivial, you can implement them as exercise. 😛

Final touch:

typedef Vec3 Vec3f; typedef Vec3 Vec3i; typedef Vec3 Vec3ui; typedef Vec2 Vec2f; typedef Vec2 Vec2i; ...

Edit: I notice that a lot of compiler don’t like template of template, so I modify the code a little bit.

Advertisements