#### Archived

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

# 3-d dynamic array of type...uhhh

This topic is 5857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i''m doing a simple voxel in my engine, and i need to have a 3 dimensional dynamic array, whose elements are only 1 bit in size. how could i do this. thanks.

##### Share on other sites
I don''t know anything about voxels, but you might look at the STL''s std::bitset. It might do you what you''re wanting. Then you could make a 3d array of them.

Russell

##### Share on other sites
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 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

 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 on other sites
why not store as a load of textures layered on top of each other?

##### 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 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 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 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 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

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633379
• Total Posts
3011568
• ### Who's Online (See full list)

There are no registered users currently online

×