一维索引的4D位置? [英] 4D position from 1D index?

查看:113
本文介绍了一维索引的4D位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从1D数组中提取4D位置.我可以看到2D和3D的效果如何,但是我很难将头围在第4维上.

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

对于2D:

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

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

对于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 = ??

推荐答案

索引公式是将任何给定维度值与所有先前维度的乘积相乘得出的.

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

所以对于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... */

一般公式的格式为

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

这篇关于一维索引的4D位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆