Matrix Stack

Started by
5 comments, last by Zakwayda 15 years, 6 months ago
Hello, I have a Matrix class. Now i want to create matrix stack: PushMatrix() and PopMatrix(). I need that API independent. Is that possible? And what do i need to if its possible? Thanks, Kasya
Advertisement
Quote:Original post by Kasya
Hello,
I have a Matrix class. Now i want to create matrix stack: PushMatrix() and PopMatrix(). I need that API independent.

Is that possible? And what do i need to if its possible?

Thanks,
Kasya
Sure it's possible :)

Normally, a matrix stack would be implemented as follows:

1. The first thing you need (naturally) is a stack of matrices. You can use whatever container is convenient and appropriate (e.g. std::stack from the SC++L).

2. Initialize the stack with a single matrix, set to identity.

3. Implement a 'push' function that makes a copy of the matrix at the top of the staff, and then pushes it onto the stack.

4. Provide a means to access and modify the matrix at the top of the stack.

5. Implement a 'pop' function that pops the top matrix off of the stack.

And that's about all there is to it.
Right. Basically, not much to add on this one (obviously, there's not much to add to jyk's replies now, is there?), however, if you really want to be independant from any external stuff, you could just use a single linked list and do memory management yourself.

best regards,


Lukas
Quote:Original post by dae_coderonin
Right. Basically, not much to add on this one (obviously, there's not much to add to jyk's replies now, is there?), however, if you really want to be independant from any external stuff, you could just use a single linked list and do memory management yourself.

best regards,


Lukas


Hmm, I see no reason why should one prefer to use a linked list over a plain array to implement a stack... It would reduce locality of reference, needlessly increase memory consumption and incur memory management performance penalties. The elementary operations on a linked list are more expensive as well. Also, if the OP wants to feed the matrices into SSE registers, proper node alignment might require to further increase the node size. Anyways, as many wise guys say here in Gamedev, using the SC++L does not make you "API dependent", because the SC++L is part of C++ :)

You're absolutely right on that one.
However, I didn't say "If you want to be truly API/OS/whatever independant", I said "if you really want to be independant from any external stuff", which sometimes is needed, e.g. in university projects (at least, at our university, we often have to do that kind of stuff, for a variety of reasons).

Just pointing out a different (if slower, less efficient, etc.) possiblilty.

best regards,


Lukas
Hello,
Can i use std::vector? what i need to have in the source?

void Stack::Push(Matrix mat) {matList.push_back(mat);}


void Stack::Pop() {for(MatrixList::iterator i = matList.begin(); i != matList.end(); i++) matList.pop_back(i);}}


Am i right?
Quote:Original post by Kasya
Hello,
Can i use std::vector? what i need to have in the source?

*** Source Snippet Removed ***

*** Source Snippet Removed ***

Am i right?
Two questions:

1. Have you tried compiling the above code?

2. What, conceptually speaking, do you think the 'pop' operation should do to the stack to which it's applied?

This topic is closed to new replies.

Advertisement