3D阵列(一维平)索引 [英] 3D array (1D flat) indexing

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

问题描述

我使用的是坐标系X(宽度),Y(高度),Z(深度)

I am using a coordinate system x (width), y (height), z (Depth)

只是为了澄清混乱是否有任意x&安培; y是一个平面,我使用ž高程。

Just to clear confusion if there is any x & y are a flat plane and I am using Z as elevation.

我将要访问的每秒标杆倍数组数百万表明,采用指数一维数组速度更快,我想挤尽可能多的效益越好,这样其他的东西可以利用这段时间

I am going to be accessing the array millions of times per second and benchmarking shows that a 1D array using index is faster and I would like to squeeze as much efficiency as possible so that other things can use that time

例如二维数组 - >一维数组创建只是

For example a 2D array --> 1D array creation is just

Object[] oneDArray = new Object[width * height]

和索引数组,我就可以使用下面的。

and to index the array I can just use the following.

Object obj = oneDArray[x + y * width]

我没有找到计算器下,但我不完全知道哪一个是正确的 如何与QUOT;扁平化"或QUOT;指数"三维阵列中的一维数组?

I did find the following on stackoverflow but I am not entirely sure which one is correct How to "flatten" or "index" 3D-array in 1D array?

正确回答说索引数组,请执行下列操作

The "Correct" answer says to index the array do the following

Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + WIDTH * (y + DEPTH * z)]

但随后又回答说,正确的答案是错误的,使用以下

But then another answer says that the "Correct" answer is wrong and uses the following

Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + HEIGHT* (y + WIDTH* z)]

什么是阅读一个扁平的三维阵列的正确方法?

What is the correct way to read a flattened 3D array?

推荐答案

这取决于你如何想订购一维数组的三维数据, 如果你想有指标依次为:Z,Y,X那么你的2x2x2的尺寸的3D数据将存储这样的:

This depends on that how you want to order your 3D data in 1D array, if you wanted to have indexes in order: Z, Y, X then your 2x2x2 dimensioned 3D data will be stored like this:

index 0: [z=0,y=0,x=0]
index 1: [z=0,y=0,x=1]
index 2: [z=0,y=1,x=0]
index 3: [z=0,y=1,x=1]
index 4: [z=1,y=0,x=0]
index 5: [z=1,y=0,x=1]
index 6: [z=1,y=1,x=0]
index 7: [z=1,y=1,x=1]

深度尺寸对应于以Z ,高度和宽度 X

该指数的计算将是:指数=高*宽* Z +宽* Y + X

的X不是乘以任何东西,因为下一个x指数之后的previous之一。

The x is not multiplied by anything because the next x index is right after the previous one.

如果你想跳过一个Y列,你必须增加整个行宽,在这种情况下2,例如,如果你是在索引1,其中有Z = 0,Y = 0和x = 1,你添加WIDTH = 2索引,你会得到指数3.只有Y尺寸增加了1。

If you want to skip one Y row, you have to add whole row WIDTH, in this case 2, for example if you are at index 1, which has z=0,y=0 and x=1 and you add WIDTH=2 to index, you'll get index 3. Only y dimension has increased by 1.

从ž移动= 0到z = 1,你要跳过4个指标(抬头看指数上市),数量为高*宽(在本例中2 * 2)。

To move from z=0 to z=1, you have to skip 4 indexes (look up at the index listing), the number is HEIGHT*WIDTH (in this example 2*2).

性能

要获得速度最大努力与Z,Y,X坐标递增的顺序处理3D数据,所以你不必重新计算该指数经常。例如:

To gain speed its best to process your 3D data with z,y,x coordinates incrementing in a sequence so you don't have to recalculate the index so often. For example:

int z = 1, y=1, x=0;
int index = HEIGHT*WIDTH*z + WIDTH*y;
int data;

for(x=0;x<WIDTH;x++)
{
    Object obj = oneDArray[index+x];
}

在理想的情况下,所有处理的数据是相互独立的,你不必即使计算指数,只是增加一个索引槽全 oneDArray 。什么是可能的,以precompute取决于您的使用。

In ideal case, all processing of data is independent from each other and you don't have to even calculate the index, just increment one index trough whole oneDArray. What's possible to precompute depends on your usage.

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

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