3D 数组(1D 平面)索引 [英] 3D array (1D flat) indexing

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

问题描述

我正在使用坐标系 x(宽度)、y(高度)、z(深度)

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

只是为了消除混淆,如果有任何 x &y 是平面,我使用 Z 作为高程.

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]

我确实在 stackoverflow 上找到了以下内容,但我不完全确定哪个是正确的如何展平"或索引"一维数组中的 3D 数组?

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

读取扁平 3D 数组的正确方法是什么?

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

推荐答案

这取决于您希望如何在一维数组中对 3D 数据进行排序,如果您想按顺序排列索引: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]

DEPTH 维度对应z,HEIGHT 对应y,WIDTH 对应x

DEPTH dimension corresponds to z, HEIGHT to y and WIDTH to x

索引计算为:index = HEIGHT*WIDTH*z + WIDTH*y + x.

x 没有乘以任何东西,因为下一个 x 索引紧跟在前一个索引之后.

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

如果要跳过 Y 行,则必须添加整行 WIDTH,在本例中为 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.

要从 z=0 移动到 z=1,您必须跳过 4 个索引(查看索引列表),数字是 HEIGHT*WIDTH(在本例中为 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 中增加一个索引即可.可以预先计算的内容取决于您的使用情况.

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 数组(1D 平面)索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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