[英]3d array with negative index - c++
I need a 3 dimensional array which supports negative indices. 我需要一个支持负索引的3维数组。 Something similar to boost::multi_array, where I could specify bounds for each dimension, ie:
与boost :: multi_array类似,在这里我可以为每个维度指定范围,即:
int xMin = -5; int xMax = 7;
int yMin = 3; int yMax = 10;
int zMin = -8; int zMax = -2;
SuperArray<float> ar;
ar.setBounds(xMin, xMax, yMin, yMax, zMin, zMax);
ar[-3][5][-5] = 1.0f;
Basically, it's indexing voxel subspace in 3D :) Is there anything ready outthere, or am I to create this by myself ? 基本上,它是在3D中为体素子空间建立索引:)那里是否有准备就绪的文件,或者我可以自己创建它? thanks !
谢谢 !
Why don't you just do a translation? 你为什么不做翻译呢?
Lets say the array size is: 可以说数组大小为:
d1 = 100
d2 = 100
d3 = 100
[d1][d2][d3]
// where index 0 = -50 and index 99 = 50
//Pseudo code
// x = -1; y = 2; z = 2;
value = array[d1/2+x][d2/2 + y][d3/2 +z];
Set the size of each dimension to Max-Min
. 将每个尺寸的大小设置为
Max-Min
。 Then when you want to access an array element, add -Min
to each index. 然后,当您要访问数组元素时,将
-Min
添加到每个索引。 So for your dimensions, you would declare: 因此,对于您的尺寸,您需要声明:
float ar[12][7][6];
Then to access the element you want, you do: 然后,访问所需的元素,您可以执行以下操作:
ar[-3-(-5)][5-3][-5-(-8)] = 1.0f;
You should be able to write a class that hides all these transformations (which is what the Boost library is doing). 您应该能够编写一个隐藏所有这些转换的类(Boost库正在执行的操作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.