从展平的 3D 数组计算 3D 坐标

calculating 3D coordinates from a flattened 3D array

我有一个线性索引,按列主要顺序展平,我想取回 3D 坐标 [x,y,z]。我为行 major https://math.stackexchange.com/questions/19765/calculating-coordinates-from-a-flattened-3d-array-when-you-know-the-size-index 找到了这个,但无法弄清楚列 major?

给出

sometype array[XSIZE][YSIZE][ZSIZE];

然后作为一维数组,那么如果你有 x >= 0 且 x < XSIZE,y >= 0 且 y < YSIZE 且 z >= 0 且 z < ZSIZE,则

Index = ((x * YSIZE + y) * ZSIZE) + z;      // Row major order, C/C++
Index = ((z * YSIZE + y) * XSIZE) + x;      // Col major order

为了计算指数,给定 x、y、z:

// For Row major order
z = Index % ZSIZE;
y = (Index / ZSIZE) % YSIZE;
x = Index / (ZSIZE * YSIZE);

// For Col major order
x = Index % XSIZE;
y = (Index / XSIZE) % YSIZE;
z = Index / (XSIZE * YSIZE);