Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Casting struct array to float[] in C#

Started by vEEcEE Apr 6, 2005 at 5:56 PM 5 replies 6.8k views
Original Post
vEEcEE
vEEcEE
I'm kind of new to C# and .Net and I was wondering how (if possible) to cast a structure, like this:

[ StructLayout( LayoutKind.Sequential, Pack = 1)]
public struct Vertex
{
    public float x, y, z;
}

into a single dimensional array of floats (float[]). In memory, it should already be stored like a linear array, so I'd think it'd be possible, but I don't know how to do the cast. Currently I'm converting them in a loop something like this:

for (int i = 0; i < numVerts; i++)
{
    vert[3 * i + 0] = vertex.x;
    vert[3 * i + 1] = vertex.y;
    vert[3 * i + 2] = vertex.z;
}

but this really seems unnecessary to me. Can anyone help?
turnpast
turnpast
So far as I know this cannot be done with a cast (Oh, if only it could -- my life would be that much less painfull). You should be able to do the conversion with pointers though -- pin the struct array using fixed to a float* -- if it is really worth the bother. You can also do a bytewise copy using the methods in Marshal.

hope this helps.
pTymN
pTymN
and that, my friends, is why C++ is still around, bit fiddling and doing questionablely unsafe things is easy :)
 
turnpast
turnpast
Quote:
Original post by Anonymous Poster
It is possible. The layout specification you have is close.
Try:

[StructLayout(LayoutKind.Explicit)]
public struct Vertex
{
[FieldOffset(0)]
public float X;
[FieldOffset(sizeof(float)]
public float Y;
[FieldOffset(2*sizeof(float))]
public float Z;
[FieldOffset(0)]
public float[] V[3];
}

I havn't had reason to use it myself yet but the layout kind explicit is how C# does unions.
~Chris


Nope. When you put an array inside of a struct like that it lives in the struct memory as a reference. .NET 2.0 introduces the concept of fiexd size arrays using the 'fixed' keyword. Fixed size arrays do layout in struct memory as a block and not a reference, but they still require an unsafe context to use. If you do it like the AP suggested you will get unexpected results and will still not be able to cast from an array of your struct to an array of float.
vEEcEE
vEEcEE
Thanks for the info Turnpast, and Chris (anon poster). I guess a union is what I was looking for, but as Turnpast basically said there's problems with using arrays in structs for this kind of thing. I haven't updated to .NET 2.0, and I really don't care to use unsafe blocks if I don't have to. The only reason I wanted to do this was for the clarity of my code, so for example I can use vertex.z instead of vertex[3 * i + 2]. Managed DirectX uses a float[] for the vertex buffer, so that's the reason why I needed to convert it.

What I'm doing is making a class to read an old 3d file format of mine, and for me, it makes most sense to read these things into a struct, but I guess I'll just have to maybe convert to a float[] afterwards.

So anyway, thanks for your help (and time) Turnpast, and Chris, I really appreciate it.
Holy Fuzz
Holy Fuzz
I *strongly* recommend breaking out the unsafe code and doing it with pointers. It's MUCH faster.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.