Sign in to follow this  
SaLiVa

Storing Matrix and Vectors

Recommended Posts

SaLiVa    100
Hi,

I am currently making a simple header files for Matrix operations and Vector operations. Since this is just a learning experience I wanted to go through with this option. Much of the inspiration for these header files comes from SVL (Simple Vector Library) which separates the Matrix formats to Mat2, Mat3, and Mat4 and the Vector formats to Vec2, Vec3, Vec4.

Looking at my Mat2 header file and Vec2 header file for simplicity sake, I have put down the Vec2 as float x, y (I don't care too much about the type just yet). In the SVL library he uses an array elt[2]. In both cases they're Protected.

That's all fine and dandy, I just overloaded the [] operator in the Vec2 header to return the proper values when called.

Both SVL and my header files use Vec2 for the rows of the matrices in Mat2 (ie. Vec2 Rows[2]). Here's where I get lost. In his class he's able to call Rows[1][1] to access the value. I suspect since I used two separate floats as my x and y values I cannot access them in such a fashion. Also since it is trying to access a Protected value the old -> or . trick doesn't work either.

So my question is, am I going about this the wrong way? What is the general approach to storing 2D/3D vectors? And are Matrices normally sorted as rows or columns of Vectors?

Thanks guys.

Share this post


Link to post
Share on other sites
Mussi    4407
Quote:
I suspect since I used two separate floats as my x and y values I cannot access them in such a fashion.

You can. Rows[n] is a member of type Vec2, the Vec2 class has an overloaded operator that allows you to get the protected x and y values. Rows[n][m] is perfectly valid;

Share this post


Link to post
Share on other sites
SaLiVa    100
Thanks Mussi, that gave me confidence to keep going on.

The problem was I put many consts in the overloading function of Vec2. So I made one with and without the consts.

And the bigger problem was I did not have a constructor for the array types. So instead I put them into the normal Vec2 constructor.

So it ended up being something like this.


mat2 operator*(const mat2 &n) const {
mat2 r;
r.m[0] = Vec2(m[0][0] * n.m[0][0] + m[0][1] * n.m[1][0], // r[0][0]
m[0][0] * n.m[0][1] + m[0][1] * n.m[1][1]); // r[0][1]
r.m[1] = Vec2(m[1][0] * n.m[0][0] + m[1][1] * n.m[1][0], // r[1][0]
m[1][0] * n.m[0][1] + m[1][1] * n.m[1][1]); // r[1][1]
return(r);
}



It would be nice if I could find out how to make an array constructor. Can anyone help with this?

Also I found a more efficient way of handling vectors according to Jim Blinn and of course it involves Templates... Found here:
http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml

I guess I'd have to delve into Templates sometime in the future (But I am so reluctant to).

Share this post


Link to post
Share on other sites
SaLiVa    100
Okay a little more digging and I found that a [][] approach for a Matrix class (Or any other similar multidimensional references) will just bring about more pain than good.

Instead it is a lot easier and performance minded to use the () operator instead.

This is according to this website.
[URL]http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10[/URL]

Hope this helps if anyone EVER takes this approach to making a Matrix library (LoL if you do).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this