# Indexing into a 3D Array

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

## 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 on other sites
hmmm would it be z * (width * height) + y * width + x?

assuming width is the x length and height is the y length

##### Share on other sites
Quote:
 Original post by MARS_999Argh 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 thisindex = (y * y + x);index = (z * (x * y) + y * x);but this seems wrong if I have a 3d array like soint *array = new int[128*128*128*4];how do i get access to the 4 elements?all I can come up is with thisindex = (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 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 on other sites
Thanks for the replies.

So is this wrong then?

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

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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631768
• Total Posts
3002244
×