Returning a Matrix

This is a discussion on Returning a Matrix within the C++ Programming forums, part of the General Programming Boards category; Well, my C++ teacher said no, you cannot return a matrix by value. So what can I do? I tried ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    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..
    Last edited by Shamino; 02-23-2006 at 07:51 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    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)
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Then you might want to pass the matrix by reference to whichever function needs to use it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Do I need to typedef the matrix to do this?

    Code:
    M[4][4] GetCMatrix()
    {
    return M;
    }
    Sorry for mah nubness
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    hmm... how is your matrix implemented? I had the impression that it was a class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    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!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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;

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I've just decided to use a matrix4 class I found on the internet
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    Last edited by 7stud; 02-23-2006 at 11:27 PM.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Why do you insist on returning it? It's easy enough to pass it as an argument.
    Last edited by SlyMaelstrom; 02-23-2006 at 11:15 PM.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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.

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    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...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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);
    }
    Last edited by VirtualAce; 02-25-2006 at 12:01 AM.

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    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
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21