Sign in to follow this  
jregan

dynamic array setting values elegance problem

Recommended Posts

jregan    140
hi everyone, first off I want to say I've been really enjoying reading and learning on this site since I created an account last month. My request for assistance is about the dynamic array implementation that I have been working on, thanks to some help from folks here. I am trying to use it as the container part of a Matrix class, i.e. the matrix has dynamic_array<double> m[rows][cols] members. I had before a method: void SetValues(double m_11, double m_12, double m_13, double m_14, double m_21, double m_22, double m_23, double m_24, double m_31, double m_32, double m_33, double m_34, double m_41, double m_42, double m_43, double m_44); however I am making it a general matrix class for all sizes, so I have changed it to: void SetValues(dynamic_array<double> &vals); How can I actually create this new dynamic array to set it to without having a bunch of vals[x][y] = value; statements? For some cases I can have things like for i for j vals[i][j] = value; but not all the time. With regular arrays you could do things like double[][] = { { a, b, c } , { d, e, f } }; How can I emulate this with my dynamic_array class? Also, I'm getting to the point where I have been looking at the matrix template library that is available online and also the boost library. The mtl library doesn't suit me right now because I don't really understand it.. I don't see why they have these row matrices and column matrices or "shaped" matrices... anyhow, would it be better in this case to start using the boost variable-dimension array? I know that it indicates that it is faster than the vector<vector> implementation that mine is based on, but I don't know if they have nice, simple ways of setting the values like I want. Any help on this is greatly appreciated!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
In a real world scenario, you never come across a case where you want to specify each individual element in the matrix. Everything is either a result of performing some operation upon the matrix, or it is a special case such as setting it to the identity matrix, or a translation only matrix, or a projection matrix of some kind, or a specific 180 or 90 degree rotation etc.

These special case operations are performed by functions designated for this purpose. Their paramters range from none (setting to identity), to near/far clipping planes etc for setting a projection matrix. If you have a special case in mind, such as listed above, or some other special case, then you should write a special function inside the matrix class, for setting up all of the elements.

So, the long and short of it is: You don't need to do what you think you need to do.
What is it that you are trying to achieve through the use of such a function?

Share this post


Link to post
Share on other sites
jregan    140
That makes good sense to me, I know what you mean about using the formulas for rotation, translation, etc. but I am still not sure I understand how to do it.. I have ones for the viewing volume that in particular I don't see how to apply the formula to setting the values.

What I had previously was:

if(ORTHO)
ProjMatrix.SetValues(2/(r-l), 0.0, 0.0, -((r+l)/(r-l)),
0.0, 2/(t-b), 0.0, -((t+b)/(t-b)),
0.0, 0.0, -2/(f-n), -((f+n)/(f-n)),
0.0, 0.0, 0.0, 1.0);

This is just straight-up plugging in the formula to create the viewing box. Sorry but I don't see how to avoid doing it like

val.SetValue(1,1,2/(r-l));
val.SetValue(1,2, 0.0);

etc.

Share this post


Link to post
Share on other sites
iMalc    2466
Okay, well the point is that the calling program simply wants to set up an orthographic projection matrix right? That calling code shouldn't have to know how to set up an otrhographic projection matrix. It is the responsibility of the Matrix class to know how to set it up. The caller shouldn't have to know about which entries are zeros, and which are ones, and which are the result of some calculation.

btw, Having an arbitrary size allowing matrix class can have some flexibality advantages, but you'll probably find that for specific tasks such as 3D graphics you'll never end up using it for anything but a handful of sizes. In fact most often only 4x4 is ever used. It's okay to specialise for this size, and create functions that only apply to 4x4, in the same way that cross-product only applies to 3-component vectors.

What you do is have a member funtion in the matrix class:
void Matrix4x4::SetOrtho(double t, double l, double b, double r, double n, double f) {
m_11 = 2/(r-l); m_12 = 0.0; m_13 = 0.0; m_14 = -((r+l)/(r-l));
m_21 = 0.0; m_22 = 2/(t-b); m_23 = 0.0; m_24 = -((t+b)/(t-b));
m_31 = 0.0; m_32 = 0.0; m_33 = -2/(f-n); m_34 = -((f+n)/(f-n));
m_41 = 0.0; m_42 = 0.0; m_43 = 0.0; m_44 = 1.0;
}

//Then elsewhere in your program, for example...
Matrix4x4 ProjMatrix;
ProjMatrix.SetOrtho(-100, -120, 100, 120, 10, 300);
(Oh, and that was me above btw)
I'm surprised no one else is posting in this thread yet.

Share this post


Link to post
Share on other sites
Deyja    920
Boost has a library that does exactly what you want. It allows you to initialize most stl containers using native-array initializer syntax. I think it's called assign. I'm not sure; I don't use it. I do like iMalc said. :D

Share this post


Link to post
Share on other sites
jregan    140
Quote:
Original post by iMalc
Okay, well the point is that the calling program simply wants to set up an orthographic projection matrix right? That calling code shouldn't have to know how to set up an otrhographic projection matrix. It is the responsibility of the Matrix class to know how to set it up. The caller shouldn't have to know about which entries are zeros, and which are ones, and which are the result of some calculation.

btw, Having an arbitrary size allowing matrix class can have some flexibality advantages, but you'll probably find that for specific tasks such as 3D graphics you'll never end up using it for anything but a handful of sizes. In fact most often only 4x4 is ever used. It's okay to specialise for this size, and create functions that only apply to 4x4, in the same way that cross-product only applies to 3-component vectors.

What you do is have a member funtion in the matrix class:
void Matrix4x4::SetOrtho(double t, double l, double b, double r, double n, double f) {
m_11 = 2/(r-l); m_12 = 0.0; m_13 = 0.0; m_14 = -((r+l)/(r-l));
m_21 = 0.0; m_22 = 2/(t-b); m_23 = 0.0; m_24 = -((t+b)/(t-b));
m_31 = 0.0; m_32 = 0.0; m_33 = -2/(f-n); m_34 = -((f+n)/(f-n));
m_41 = 0.0; m_42 = 0.0; m_43 = 0.0; m_44 = 1.0;
}

//Then elsewhere in your program, for example...
Matrix4x4 ProjMatrix;
ProjMatrix.SetOrtho(-100, -120, 100, 120, 10, 300);
(Oh, and that was me above btw)
I'm surprised no one else is posting in this thread yet.




Thanks for clarifying. I follow what you mean about setting the members as you have above, and I always had it this way for a long time where I just had a bunch of member vars named that way, but I hated the way it made me have to write certain other operations like the matrix multiplication function. I also can see that having a very general matrix class is not that good for graphics, but I was so hoping it would be =( oh well... I was using it also to create my 3DVector and Vertex classes so they could be 1x4 matrices.

I looked in the ogre code and they had only two, 3x3 and 4x4 matrices so that is probably the best bet. Good thing I kept a copy of that old code. I've got to take a look at boost, I don't like worrying about this stuff I'd rather be moving forward with the graphics algorithms and leave this matrix math behind.

Share this post


Link to post
Share on other sites
Teknofreek    331
This following doesn't pose anything useful to the exact question posted by the OP, but I feel the urge to chime in anyway =)

In most 3D games, the following things are used: Vector2, Vector3, Vector4, Matrix4x4, and *maybe* a Matrix3x3. That's it! Well, there's usually also a Quaternion class, but that's somewhat unrelated to what's being discussed here.

Anyway, my point is that I see very little point in making all of this templated. Typically, you will want special case the code and/of functions for these variants. For example, if you use your Matrix3x3 for only rotations then you KNOW that you can just use the transpose to get the inverse matrix. In addition, you can make multiplying a matrix and a vector simple if you don't try to make it generic.

So personally, I think you'd be better off just writing these hard-coded classes instead of trying to make a templated matrix class and then specializing it.

Just my 2 cents...

-John

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