【问题标题】:Best way to rotate a 3D grid (nxnxn) of values in Python with interpolation?使用插值在 Python 中旋转 3D 网格(nxnxn)值的最佳方法?
【发布时间】:2020-01-12 10:03:53
【问题描述】:
如果我有一个 nxnxn 值网格,比如 32x32x32,并且我想在 x、y 或 z 轴上以某个旋转角度旋转这个值的立方体网格,并插入缺失值,那么最好的方法是什么不使用包中的任何现有算法(例如 Scipy)如何做到这一点?
当表示为 [n, 3] 矩阵时,我熟悉将 3D 旋转矩阵应用于点的 3D 网格,但我不确定如何在给出表示时应用旋转其 3D 形式为 nxnxn。
我找到了一个prior Stack Overflow post about this topic,但它使用三个 for 循环来实现它的方法,这在速度方面并没有真正扩展。有没有更矢量化的方法可以完成类似的任务?
提前致谢!
【问题讨论】:
标签:
python
numpy
multidimensional-array
geometry
rotational-matrices
【解决方案1】:
我能想到的一种方法如下所示:
- 将 nxnxn 矩阵重塑为包含 n 维点的数组
- 对此阵列应用旋转
- 将数组重新整形为 nxnxn
这是一些代码:
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)