Archived

This topic is now archived and is closed to further replies.

Grizwald

3-d dynamic array of type...uhhh

Recommended Posts

i was thinking more along the lines of storing it as
byte [sizex/8,sizey/8,sizex/8] and then reading the individual bits of the byte. but even then i don''t know how to do that (it involves bit shifting i think )

Share this post


Link to post
Share on other sites
You know what may be more efficient is to group your voxels into "chunks" so that you can store them in larger data types. You could group 8 voxels together in a byte (2x2x2), and have these "chunks" as the base element in an octree hierarchy. Or even pack 4x4x4 into a 64-bit integer ("long long", or whatever the hell the def is in C). Then to read/write to a particular voxel (bit), just use bitmasks.

Just an idea to toy with...

PS: sory for the vagueness of my reply... just got outta bed


[edit] it seems you posted while my half-asleep arse was still typing...

yeah, get at the individual bits, you use something called bitmasking . Basically, you use bitwise & (to test) and | (to set)

eg Test Least Significant Bit
if( myByteVar & 1 ) {...}

Set LSB
myByteVar = myByteVar | 1;

For each bit, you simply use a power of 2 to address it, i.e. the next bits you would go: myByteVar & 2, myByteVar & 4, myByteVar & 8, etc (up to 128 for a byte)

Um... i'm really too spaced out to give a better explanation... I'm sure someone will help you out though



[edited by - Bad Monkey on November 3, 2002 9:25:25 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Grizwald
i was thinking more along the lines of storing it as
byte [sizex/8,sizey/8,sizex/8] and then reading the individual bits of the byte. but even then i don''t know how to do that (it involves bit shifting i think )
Using bits instead of bytes won''t save you that much memory. The best you could do is [sizex/8,sizey,sizez]. But Bad Monkey''s idea of 2x2x2 boxes would probably work best. Depends on your overall implementation.. Maybe you could first implement it as 1 byte = 1 voxel, and change it later when you get things working.

Share this post


Link to post
Share on other sites
If you want to save memory, check out std::vector of bool. It it intended to pack bits together, and i would use it like so:


    
class VoxelGrid
{
public:
VoxelGrid(int x,int y,int z):
grid(x*y*z)
{
xx=x;
yy=y;
zz=z;
}
int Index(int x,int y,int z)
{
return x*(yy*zz)+y*zz+z;
}
bool GetValue(int x,int y,int z)
{
return grid[Index(x,y,z)];
}
void SetValue(int x,int y,int z,bool value)
{
grid[Index(x,y,z)]=value;
private:
vector<bool> grid;
int xx,yy,zz;
}


Anyway, something else for you to check out, you may find that other representations are better.

edit: it ate my brackets!

[edited by - sjelkjd on November 4, 2002 5:53:08 AM]

Share this post


Link to post
Share on other sites
You could use the bit struct:

// Each member is this struct contains 1 bit donoted by the : 1
// and since I use unsigned char as the data the total data
// struct is 1 byte or 7 bits
struct MyStruct
{
unsigned char b1: 1;
unsigned char b2: 1;
unsigned char b3: 1;
unsigned char b4: 1;
unsigned char b5: 1;
unsigned char b6: 1;
unsigned char b7: 1;
unsigned char b8: 1;
};

void main()
{
MyStruct instance;
instance.b1=1; instance.b1=true;
}

hope this helped

Share this post


Link to post
Share on other sites
Thank you all so much. I think i''m leaning towards Bad Monkey''s approach though. lets say i have a function called:
GetVoxel (x,y,z) how would i index the bits in the 2x2x2 byte cube thingy ? thanks

Share this post


Link to post
Share on other sites
Um... just off the top of my head (why I come here while I''m eating breakfast and still half asleep, I''ll never know ) you could get at the appropriate "chunk" (byte) in your array by dividing your x, y, and z indices by 2. As they are integers you could do this by shifting them one place (same as divide by 2).

Then, to determine which bit you want, you can simply bit-test each index with the value 1 (i.e. x & 1, y & 1, z & 1)... this will tell you which bit you want in each dimension of the 2x2x2 chunk. So say we arbitrarily decide that the bottom-left-front voxel in the chunk is 0,0,0 and the top-right-back voxel is 1,1,1 ...these 3 values will give us our "coordinates" of the voxel we are after within the chunk... or something like that...

Man, I am so random

Anyho, you could set up the function like this:


  
// Have to return value of bit in a byte (unsigned char)

unsigned char GetVoxel(int x, int y, int z)
{
unsigned char chunk = globalVoxelArray[x>>1][y>>1][z>>1]; // fetch the chunk the voxel will be in...



if (x & 1)
{
if(y & 1)
{
if(z & 1)
{
return (chunk & 128); // 8th bit

}
else
{
return (chunk & 64); // 7th bit

}
}
else
{
if(z & 1)
{
return (chunk & 32); // 6th bit

}
else
{
return (chunk & 16); // 5th bit

}

}
}
else
{
if(y & 1)
{
if(z & 1)
{
return (chunk & 8); // 4th bit

}
else
{
return (chunk & 4); // 3rd bit

}
}
else
{
if(z & 1)
{
return (chunk & 2); // 2nd bit

}
else
{
return (chunk & 1); // 1st bit

}

}

}
}



Looking back at that, it looks hideous, but I think it should work

Share this post


Link to post
Share on other sites