简体   繁体   English

使用插值在 Python 中旋转 3D 网格 (nxnxn) 值的最佳方法?

[英]Best way to rotate a 3D grid (nxnxn) of values in Python with interpolation?

If I have a nxnxn grid of values, say 32x32x32, and I want to rotate this cube grid of values by some rotation angle in either the x, y, or z axes, and interpolate missing values, what would be the best way to go about doing this without using any existing algorithms from packages (such as Scipy)?如果我有一个 nxnxn 值网格,比如 32x32x32,并且我想在 x、y 或 z 轴上以某个旋转角度旋转这个值的立方体网格,并插入缺失值,那么最好的方法是什么关于在不使用包(例如 Scipy)中的任何现有算法的情况下执行此操作

I'm familiar with applying a 3D rotation matrix to a 3D grid of points when it's represented as a [n, 3] matrix, but I'm not sure how to go about applying a rotation when the representation is given in its 3D form as nxnxn.当它表示为 [n, 3] 矩阵时,我熟悉将 3D 旋转矩阵应用于点的 3D 网格,但我不确定当表示以 3D 形式给出时如何应用旋转作为 nxnxn。

I found a prior Stack Overflow post about this topic , but it uses three for loops for its approach, which doesn't really scale in terms of speed.我找到了之前关于这个主题的 Stack Overflow 帖子,但它的方法使用了三个 for 循环,这在速度方面并没有真正扩展。 Is there a more vectorized approach that can accomplish a similar task?是否有更矢量化的方法可以完成类似的任务?

Thanks in advance!提前致谢!

One way I could think of would look like this:我能想到的一种方法是这样的:

  1. reshape nxnxn matrix to an array containing n-dimensional points将 nxnxn 矩阵重塑为包含 n 维点的数组
  2. apply rotation on this array在这个数组上应用旋转
  3. reshape array back to nxnxn将数组重塑回 nxnxn

Here is some code:这是一些代码:

import numpy as np

#just a way to create some nxnxn matrix
n = 4
a = np.arange(n)
b = np.array([a]*n)
mat = np.array([b]*n)

#creating an array containg n-dimensional points
flat_mat = mat.reshape((int(mat.size/n),n))

#just a random matrix we will use as a rotation
rot = np.eye(n) + 2

#apply the rotation on each n-dimensional point
result = np.array([rot.dot(x) for x in flat_mat])
#return to original shape
result=result.reshape((n,n,n))
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM