This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Packing a matrix4f in char array

Tags: None
(comma "," separated)
Markingg
Registered Member
Posts
1
Karma
0
OS

Packing a matrix4f in char array

Tue May 15, 2012 10:37 am
Using GCC compiler on a Playstaion 3.

I am trying to store my Eigen::Matrix4f in a char array to store in a file (and send over network).
But this seems to fail...

Code: Select all
char data[192];
Matrix4f mat = Matrix4f::Identity(); //Or an actual matrix...
memcpy(&data[0],  &mat, sizeof(Matrix4f)); //Also tried mat.data()

//Load it back
char out[192];
//Method one
Matrix4f mat2;
memcpy(&mat2, ((Matrix4f *)&out[0]), sizeof(Matrix4f ));
//Try two
Matrix4f mat3;
mat3 = *(Matrix4f*)&msg.data[0];

The data I get is
Code: Select all
in:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Returns:
1.54143e-44 0 0 0
1 0 0 0
0 1 0 0
0 0 1 0


Why is it 16 bytes off? I could probably use a for loop to iterate over each float and store it manually, but that just seems so inefficient. Is this because of alignment issues, and if so, how can I solve this in a correct way?
Thanks for the help!

Edit: Using EIGEN_DONT_ALIGN_STATICALLY solved this problem, but it does seem like a lame way of solving this. Still not a good approach, how could I solve this in a more decent way?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You should really use .data() to get a pointer to the data, then you should also take into account that your char* buffers might not even satisfies the alignment required for float, so your second option is clearly wrong. using memcpy should be safe though:

char* out;
memcpy(out, mat.data(), sizeof(Matrix4f));

char* in;
memcpy(mat.data(), in, sizeof(Matrix4f));


You should also makes sure that on your platform sizeof(Matrix4f)==16*sizeof(float). This should be guaranteed by Eigen but we never tried on the PS3 platform.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]