I'm attempting to use a union for a couple of structs, specfically aligning a 2D (4 by 4) float array with 4 Vector4's.
From my understanding, union looks at the sequential bytes from the first. If I have a 4x4 array and want to get a whole column, I would need to skip 16 bytes to get to the next row.
Example:
Matrix = {{a, b, c, d},
{e, f, g, h},
{h, i, j, k},
{l, m, n, o};
if the data is given such that a 1D array is {a,b,c,d,e,..., o}
, how would I get the last column (d,h,k,o)?
So how would I get the bytes: 12->16, 28->32, 44->48, 60->64?
the code I have that returns the last row (not what I want), is:
//matrix.h
struct Matrix4 {
union{
float values[4][4];
Vector4 column[4];
};
//matrix methods etc
}
//main.cpp
Matrix4 position = Matrix3::translation(Vector3(1,2,3));
Vector4 column = position.column[2];
std::cout << (column) << std::endl;
The output matrix would be (don't remember how to use LaTex formatting sorry):
| 1 0 0 1 |
| 0 1 0 2 |
| 0 0 1 3 |
| 0 0 0 1 |
So the column I want is:
|1|
|2|
|3|
|1|
and the union gives:
|0|
|0|
|0|
|1|
(the last row, not column).
Would it just be easier to refactor my code to use a 1D array to get the sequential bytes or should I change how my matricies are constructed? or can I handle this with some other thing in union?
EDIT: My question is different to this other question because I already have created a matrix class and allocated memory. My question looks at the functionality of "union" and its limits, as well as using my definied struct 'Vector4' as a means of representing some data in a matrix. I'm looking into the possibility of skipping bytes to receive data that doesn't directly follow each other, not how to allocate said bytes.