Sign in to follow this  

Indexing into a 3D Array

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Argh I am at my limit. I just read this last weekend the math to compute the index of a 3D array, and know I can't find it. Could some one please point the correct notation. 2D is this index = (y * y + x); index = (z * (x * y) + y * x); but this seems wrong if I have a 3d array like so int *array = new int[128*128*128*4]; how do i get access to the 4 elements? all I can come up is with this index = (z * (x * y) + y * x + 1); where 1 is 1-4? Thanks [Edited by - MARS_999 on November 8, 2006 12:23:49 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MARS_999
Argh I am at my limit. I just read this last weekend the math to compute the index of a 3D array, and know I can't find it. Could some one please point the correct notation.

2D is this

index = (y * y + x);

index = (z * (x * y) + y * x);

but this seems wrong if I have a 3d array like so

int *array = new int[128*128*128*4];

how do i get access to the 4 elements?

all I can come up is with this

index = (z * (x * y) + y * x + 1); where 1 is 1-4?

Thanks


That looks like a 4D array to me. I would access it like this:
index = t*(width*length*height) + z*(width*length) + y*width + x
where t is the 4th dimension, z is height, y is length, and x is width

One way with the numbers plugged in:
index = t*128*128*4 + z*128*4 + y*4 + x, where x = 0-3 and the rest are 0-127

Another way is:
index = t*128*128*128 + z*128*128 + y*128 + x, this time t = 0-4 and the rest are 0-127

Share this post


Link to post
Share on other sites
Using width = 128, height = 128, elementsize = 4...

index = z*width*height*elementsize + y*width*elementsize + x*elementsize

In other words: there are 4 bytes per element, width*4 bytes per line, and width*height*4 bytes per plane, and width*height*depth*4 bytes per box (which leads back to the amount used with new[]).

I still find this particular task to be the most ultimate in annoying, so don't feel bad.

Share this post


Link to post
Share on other sites
Thanks for the replies.

So is this wrong then?

index = (z * (128* 128) + y * 128+ x + 1);

So Taby with your code


index = z*width*height*elementsize + y*width*elementsize + x*elementsize

would I do this to access each 0-3 then?

index = (z*width*height*elementsize + y*width*elementsize + x*elementsize)
index = (z*width*height*elementsize + y*width*elementsize + x*elementsize) + 1
index = (z*width*height*elementsize + y*width*elementsize + x*elementsize) + 2
index = (z*width*height*elementsize + y*width*elementsize + x*elementsize) + 3

Share this post


Link to post
Share on other sites
Yes, that is perfect. Should do the trick nicely.

If you want to save on the number of variables, you could also do the nearly equivalent of:

index = z*width*height*elementsize + y*width*elementsize + x*elementsize

Then:

array[index + 0] = ...
array[index + 1] = ...
array[index + 2] = ...
array[index + 3] = ...

Just a matter of syntactical preference. :)

Share this post


Link to post
Share on other sites


index = (Z * Xsize * Ysize) + ( Y * Xsize) + X

array[index]


If you can force the array coords to be powers of 2 you can use
shifts and ANDs to build the index (and even better if the sizes are
constants)

Zshift = Zpower + Ypower
index = (Z << Zshift) & ( Y << Ypower) & X


valid ranges for x y z are assumesd and should be assured elsewhere....

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this