# Returning a Matrix

• 02-23-2006
Shamino
Returning a Matrix
Well, my C++ teacher said no, you cannot return a matrix by value.

So what can I do? I tried using pointers but the syntax is all goofy, can someone help meh out?

EDIT: Maybe I said the wrong words, "return by value"

I mean this

Code:

``` m[4][4] GetCMatrix() {     return m[4][4]; }```
Sorry for confusion..
• 02-23-2006
laserlight
Why not create a matrix class, and return matrix objects by value? Of course, whether this is a good idea depends on what exactly you are trying to do.
• 02-23-2006
Shamino
It needs to be FAST, thats the only requirement really, as long as it gets returned and it is fast it would be fine.

(Game setting)
• 02-23-2006
laserlight
Then you might want to pass the matrix by reference to whichever function needs to use it.
• 02-23-2006
Shamino
Do I need to typedef the matrix to do this?

Code:

```M[4][4] GetCMatrix() { return M; }```
Sorry for mah nubness :D
• 02-23-2006
laserlight
hmm... how is your matrix implemented? I had the impression that it was a class.
• 02-23-2006
Shamino
Nuhhuh, I have no matrix object, I was trying to use a raw matrix..

But apparently, others are telling me I need to create my own matrix class...

Bleh!
• 02-23-2006
Darryl
I'll assume raw matrix means an array of arrays defined something like
Code:

```int Matrix[4][4]; if that is the case you can pass it as a pointer to a pointer like int** pM = Matrix; A function might be like int** GetCMatrix() { return Matrix;} and used like pM = GetCMatrix(); you then can use the pointer as you would the matrix pM[4][4] = 10;```
• 02-23-2006
Shamino
I've just decided to use a matrix4 class I found on the internet :D
• 02-23-2006
7stud
Quote:

int Matrix[4][4];
if that is the case you can pass it as a pointer to a pointer like
int** pM = Matrix;
It doesn't work that way. When you declare a pointer variable without an array dimension, you can only make it point to a one dimensional array, e.g.:
Code:

```int Matrix[10] = {0}; int* ptr = Matrix; cout<<ptr[0]<<endl;```
Or,
Code:

```int arr1[] = {1,2,3}; int arr2[] = {3,4,5}; int* p1 = arr1; int* p2 = arr2; int* ptrArr[] = {p1, p2}; int** ptr = ptrArr; cout<<ptr[1][0]<<endl;```
With a one dimensional array, the dimension is not part of the type, so the ptr variable does not require a dimension.

However, as soon as you declare a 2d array, the second dimension(the one on the right) becomes part of the type, and your ptr variable must reflect that type:
Code:

```int arr[3][2] = {0}; int(*ptr)[2] = arr;         cout<<ptr[0][0]<<endl;```
• 02-23-2006
SlyMaelstrom
Why do you insist on returning it? It's easy enough to pass it as an argument.
• 02-24-2006
grumpy
It is not possible to directly return an array (multi-dimensional or otherwise) from a function. Returning a pointer arguably comes close (assuming the pointer is set up so it points at the right stuff).

But it is possible to return a structure that contains such an array though;
Code:

```struct ArrayContainer {     int x[4][4]; }; ArrayContainer Matrix() {     ArrayContainer retval;     // set a couple of values     retval.x[0][0] = 41;     retval.x[1][2] = 42;     return retval; }```
This uses the fact that structs (like classes, which are actually a type of struct) can be returned by value. Which is why, if you want to represent a matrix (mult-dimensional array) it is a good idea to wrap it up as a class.
• 02-24-2006
Shamino
Because it would require 20x more work to use polymorphism to pass it down through functions than to create a matrix4 object and return it...
• 02-24-2006
VirtualAce
You can do this Shamino.

You will be working with 4x4 matrices nearly all the time.

Code:

```void MatrixMultiply(float matrix1[4][4],float matrix2[4][4],float result[4][4]) {   for (int i=0;i<4;i++)   {     for (int j=0;j<4;j++)     {       for (int k=0;k<4;k++)       {           result[i][j]+=matrix1[i][k]*matrix2[k][j];       }     }   } }```
I would recommend unrolling the loops however or coding it like:

Code:

`matrix[0][0]=matrix1[..][..]*matrix2[...][...];`
I would give you a matrix library, but mine are for DX and yours will be for OpenGL. The matrices are diff for each of those.

For vectors you can do this:

Code:

```//Multiplys a inVec by matrix and returns result in outVec //Transforms a vector by a matrix void Vec3TransformCoord(Vector3 &inVec,float matrix[4][4],Vector3 &outVec) {     outVec.x=inVec.x*matrix[0][0]+inVec.y*matrix[0][1]+inVec.z*matrix[0][2]+matrix[0][3];     outVec.y=inVec.x*matrix[1][0]+inVec.y*matrix[1][1]+inVec.z*matrix[1][2]+matrix[1][3];     outVec.z=inVec.x*matrix[2][0]+inVec.y*matrix[2][1]+inVec.z*matrix[2][2]+matrix[2][3]; }```
Code:

```//Normalizes a vector - normalized vector is outVec void Vec3Normalize(Vec3 &outVec,Vec3 &inVec) {   float length=sqrtf(inVec.x*inVec.x+inVec.y*inVec.y+inVec.z*inVec.z);   float oneOverLength=1.0f/length;   outVec.x=inVec.x*oneOverLength;   outVec.y=inVec.y*oneOverLength;   outVec.z=inVec.z*oneOverLength; } //Returns dot product of vec1 and vec2 //vec1 and vec2 are assumed to be non-normal float Vec3NormalDot(Vec3 &vec1,Vec3 &vec2) {     Vec3 toVec2=Vec3(vec2.x-vec1.x,vec2.y-vec1.y,vec2.z-vec1.z);   Vec3Normalize(&toVec2,&toVec2);   return (toVec2.x*toVec2.x+toVec2.y*toVec2.y+toVec2.z*toVec2.z); }```
• 02-25-2006
Shamino
I got this from gamedev, and it seems pretty generic (not ogl or dx specific)

It isn't a resource manager, but just a matrix4 object and a vect4 object to work with with a ton of overloaded operators and nifty functions..

I'll probably create my own matrix library eventually to manage them, ykno, push/pop, load, etc etc.. :)

http://www.gamedev.net/reference/articles/605/math3d.h