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

时间:2018-07-03 15:12:36

标签: c++ matrix coordinates linear-algebra

我有一个线性索引,按列主顺序展平,并且我想取回3D坐标[x,y,z]。我在行专业https://math.stackexchange.com/questions/19765/calculating-coordinates-from-a-flattened-3d-array-when-you-know-the-size-index中找到了此功能,但是无法找出列专业?

2 个答案:

答案 0 :(得分:0)

如果

index = x + (y + (z * ZSIZE)) * YSIZE 

然后:

x = index % YSIZE
Y = ((index - x) / YSIZE) % ZSIZE
Z = (index - x - y*YSIZE) / (ZSIZE * YSIZE)

答案 1 :(得分:0)

给予

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

然后作为一维数组,如果 x> = 0且x = 0且y = 0且z

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);