MARS_999 1627 Report post Posted November 8, 2006 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] 0 Share this post Link to post Share on other sites
Dancin_Fool 785 Report post Posted November 8, 2006 hmmm would it be z * (width * height) + y * width + x?assuming width is the x length and height is the y length 0 Share this post Link to post Share on other sites
nicksterdomus 270 Report post Posted November 8, 2006 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?ThanksThat looks like a 4D array to me. I would access it like this:index = t*(width*length*height) + z*(width*length) + y*width + xwhere t is the 4th dimension, z is height, y is length, and x is widthOne 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-127Another way is:index = t*128*128*128 + z*128*128 + y*128 + x, this time t = 0-4 and the rest are 0-127 0 Share this post Link to post Share on other sites
taby 1265 Report post Posted November 8, 2006 Using width = 128, height = 128, elementsize = 4...index = z*width*height*elementsize + y*width*elementsize + x*elementsizeIn 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. 0 Share this post Link to post Share on other sites
MARS_999 1627 Report post Posted November 8, 2006 Thanks for the replies.So is this wrong then?index = (z * (128* 128) + y * 128+ x + 1);So Taby with your codeindex = z*width*height*elementsize + y*width*elementsize + x*elementsizewould 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) + 1index = (z*width*height*elementsize + y*width*elementsize + x*elementsize) + 2index = (z*width*height*elementsize + y*width*elementsize + x*elementsize) + 3 0 Share this post Link to post Share on other sites
taby 1265 Report post Posted November 8, 2006 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*elementsizeThen:array[index + 0] = ...array[index + 1] = ...array[index + 2] = ...array[index + 3] = ...Just a matter of syntactical preference. :) 0 Share this post Link to post Share on other sites
wodinoneeye 1689 Report post Posted November 8, 2006 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.... 0 Share this post Link to post Share on other sites