跟随网格中的3D矢量

我有3D网格和3D单位矢量(随机3d方向)。
我需要从空间的某个点开始沿着这个方向循环遍历我们要进入的每个细胞。

我需要获取我们经过的每个单元格,并且此循环必须快速。

我想到了一种快速获得所有这些职位的方法。我需要获得在矢量方向上经过的最大距离,以在1个轴上传递1个单位的距离:
假设我的像元大小是1,斧头是'X'
我需要知道从矢量X到X = 1到X = 1的距离。

有了这个值,我就可以轻松获得一些其他信息:
从X = floor(my_pos_X)到X = floor(my_pos_X)+1必须走(向矢量方向)的距离。

这是我项目(python)中的代码:

#Get my Starting Point
pos = GET_POS #Getting my 3D pos / no need to explain all the project
#Creation of the grid
grid = THE_GRID #almost as simple as a 3d array,with min,max and cell size
#Getting the size of the cell / it is a cube,one size is needed
size = grid.size

#Getting my random direction in a 3d unit vector
dir = get_random_dir() #I am sure the result is a 3d vector of size 1.

#Get the coordinates in my table containing the cell
(x,y,z) = grid.get_discretized_pos(pos) #floor( (pos[0]-min[0]) / size )

# direction of the vector. 1 if positive in the axes,-1 if not
dx = 1 if dir[0] > 0 else -1
dy = 1 if dir[1] > 0 else -1
dz = 1 if dir[2] > 0 else -1

# The distance to go following `dir` to get from X=0 to X=1
# this is where I need help

# mx mean "Maximum distance before change in X position"
mx = (size / (dir[0])) * math.sqrt(dir[1]**2+dir[2]**2) * dx
my = (size / (dir[1])) * math.sqrt(dir[2]**2+dir[0]**2) * dy
mz = (size / (dir[2])) * math.sqrt(dir[0]**2+dir[1]**2) * dz

# The distance I have to go from the current pos until the next change in the grid position
# if dx == 1 : size - pos[0]%size
# else: pos[0]%size
# then /size <- get the proportion from the size
# * mx <- apply this proportion to the max distance

# nx mean "Next change in the X position" and contain the distance until this next change
nx = ( (size*(dx+1)/2) - dx*(pos[0]%size) ) / size * mx
ny = ( (size*(dy+1)/2) - dy*(pos[1]%size) ) / size * my
nz = ( (size*(dz+1)/2) - dz*(pos[2]%size) ) / size * mz

#Follow the direction until stop
is_ended = False
while ( not is_ended ):

    #get the minimum distance before changing pos in grid
    if nx < ny:
        if nx < nz:#X axes is the minimum
            d = nx #distance I need to travel until next grid position change is `nx`
            nx = mx #next time will be the maximum distance
            ny -= d #reduce the remaining distance by what I am travelling
            nz -= d #same
            x += dx #Updating the position in the grid
        else : #Z axe is the minimum
            d = nz
            nz = mz
            nx -= d
            ny -= d
            z += dz
    else:
        if ny < nz: #Y axe is the minimum
            d = ny
            ny = my
            nx -= d
            nz -= d
            y += dy
        else : #Z axe is the minimum
            d = nz
            nz = mz
            nx -= d
            ny -= d
            z += dz
        
    # The test my program need to exit the loop
    if MY_TEST(x,z):
        is_ended = True

在获取mxnx(y&z)值时,我似乎有些问题。或者,如果问题是由于计算机对float的近似影响,那么这对我有好处,但我不认为这是问题所在。

红色球体是我的方向。绿色的多维数据集就是我所获得的所有多维数据集(使用MY_TEST时,我只是注册每个位置)。

这是失败的: Fail while following- Another Point of View
与其他POV一起,我看到我已经接近了,但我需要保持准确,而不是接近。

这是一个“胜利”: Follow successfull- Another Point of View

iCMS 回答:跟随网格中的3D矢量

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1842938.html

大家都在问