4D position from 1D index?

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

I need to extract a 4D position from a 1D array. I can see how it goes for 2D and 3D but I'm having a hard time wrapping my head around the 4th dimension..

For 2D:

int* array = new int[width * height]; int index = y * width + x; int x = index / height int y = index - x * height; 

For 3D:

int* array = new int[width * height * depth]; int index = z * width * height + y * width + z; int x = index / (height * depth); int y = index - (x * height * depth) / depth; int z = index - (x * height * depth) - (y * depth); 

For 4D ?

int* array = new int[width * height * depth * duration]; int index = w * width * height * depth + z * width * height + y * width + w; int x = index / (height * depth * duration); int y = ?? 

回答1:

The indexing formula is given by the multiplication of any given dimension value with the product of all the previous dimensions.

Index = xn ( D1 * ... * D{n-1} ) + x{n-1} ( D1 * ... * D{n-2} ) + ... + x2 * D1 + x1 

So for 4D

index = x + y * D1 + z * D1 * D2 + t * D1 * D2 * D3; x = Index % D1; y = ( ( Index - x ) / D1 ) %  D2; z = ( ( Index - y * D1 - x ) / (D1 * D2) ) % D3;  t = ( ( Index - z * D2 * D1 - y * D1 - x ) / (D1 * D2 * D3) ) % D4;  /* Technically the last modulus is not required,    since that division SHOULD be bounded by D4 anyways... */ 

The general formula being of the form

xn = ( ( Index - Index( x1, ..., x{n-1} ) ) / Product( D1, ..., D{N-1} ) ) % Dn 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!