我如何计算坐标,反之亦然一个3D的线性指标? [英] How do I compute the linear index of a 3D coordinate and vice versa?
问题描述
如果我有一个点(x,y z)的,我怎么找到线性指标,我该点?我的编号方案将是(0,0,0)为0,(1,0,0)是1,。 。 ,(0,1,0)是最大-x维度,....
另外,如果我有一个线性的坐标,我,我怎么觉得(X,Y,Z)?
我似乎无法找到这对谷歌,所有的结果都充满了其他无关的东西。谢谢!
If I have a point (x, y z), how do I find the linear index, i for that point? My numbering scheme would be (0,0,0) is 0, (1, 0, 0) is 1, . . ., (0, 1, 0) is the max-x-dimension, .... Also, if I have a linear coordinate, i, how do I find (x, y, z)? I can't seem to find this on google, all the results are filled with other irrelevant stuff. Thank you!
推荐答案
有几个方法来映射一个三维坐标到一个单一的数字。这里有一种方法。
There are a few ways to map a 3d coordinate to a single number. Here's one way.
一些函数f(x,Y,Z)给出坐标的线性索引(X,Y,Z)。它有我们希望派生,所以我们可以写一个有用的转换函数一些常量A,B,C,D。
some function f(x,y,z) gives the linear index of coordinate(x,y,z). It has some constants a,b,c,d which we want to derive so we can write a useful conversion function.
f(x,y,z) = a*x + b*y + c*z + d
您已经指定了(0,0,0)映射到0.所以:
You've specified that (0,0,0) maps to 0. So:
f(0,0,0) = a*0 + b*0 + c*0 + d = 0
d = 0
f(x,y,z) = a*x + b*y + c*z
这是公司的D解决。
您已经指定了(1,0,0)映射到1,所以:
That's d solved. You've specified that (1,0,0) maps to 1. So:
f(1,0,0) = a*1 + b*0 + c*0 = 1
a = 1
f(x,y,z) = x + b*y + c*z
这是一个解决。
让我们任意决定后下一个最高数目(MAX_X,0,0)是(0,1,0)。
That's a solved. Let's arbitrarily decide that the next highest number after (MAX_X, 0, 0) is (0,1,0).
f(MAX_X, 0, 0) = MAX_X
f(0, 1, 0) = 0 + b*1 + c*0 = MAX_X + 1
b = MAX_X + 1
f(x,y,z) = x + (MAX_X + 1)*y + c*z
这'S B解决。
让我们任意决定后下一个最高数目(MAX_X,MAX_Y,0)是(0,0,1)。
That's b solved. Let's arbitrarily decide that the next highest number after (MAX_X, MAX_Y, 0) is (0,0,1).
f(MAX_X, MAX_Y, 0) = MAX_X + MAX_Y * (MAX_X + 1)
f(0,0,1) = 0 + (MAX_X + 1) * 0 + c*1 = MAX_X + MAX_Y * (MAX_X + 1) + 1
c = MAX_X + MAX_Y * (MAX_X + 1) + 1
c = (MAX_X + 1) + MAX_Y * (MAX_X + 1)
c = (MAX_X + 1) * (MAX_Y + 1)
现在我们知道A,B,C和D,我们可以按如下方式编写功能:
now that we know a, b, c, and d, we can write your function as follows:
function linearIndexFromCoordinate(x,y,z, max_x, max_y){
a = 1
b = max_x + 1
c = (max_x + 1) * (max_y + 1)
d = 0
return a*x + b*y + c*z + d
}
您可以通过类似的逻辑,线性指标的坐标。我有这样一个真正了不起的演示,此页面是太小,无法容纳。所以我会跳过数学讲座,只是给你的最终方法。
You can get the coordinate from the linear index by similar logic. I have a truly marvelous demonstration of this, which this page is too small to contain. So I'll skip the math lecture and just give you the final method.
function coordinateFromLinearIndex(idx, max_x, max_y){
x = idx % (max_x+1)
idx /= (max_x+1)
y = idx % (max_y+1)
idx /= (max_y+1)
z = idx
return (x,y,z)
}
这篇关于我如何计算坐标,反之亦然一个3D的线性指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!