2

我正在运行一个脚本来创建一个由一组点定义的圆柱面。我正在使用 Mayavi 来呈现可视化效果。对于以下脚本,我如何找到每个点的坐标?似乎 Mayavi 在数据管道的幕后执行此操作,但我不太确定如何提取它。或者,我尝试做嵌套的 for 循环,但我还没有找到正确的算法......同时我会继续尝试。但是,如果有人知道如何从 Mayavi 管道中提取它们,那就太好了,因为我不必增加任何计算时间。

import numpy as np
import mayavi
from mayavi import mlab

class cylinder:
  def __init__(self, radius, length):
    phi, x = np.mgrid[0:np.pi:50j, 0:length:50j]
    self.z = radius * np.sin(phi)
    self.y = radius * np.cos(phi)
    self.x = x
    self.mesh = mlab.points3d(self.x,self.y,self.z, mode = 'point')


def main():
  cylinder = cylinder(radius=1,length=2)
  mayavi.mlab.axes()
  mlab.show()


main()
4

1 回答 1

1

您传递给 mayavi 的所有数据都将出现在相应的数据源中。但是,我会注意到,我不认为您经常想要对管道中的数据进行逆向工程,因为是您首先传递了这些数据(因此您应该能够跟踪无需深入研究低级 vtk 机器)。

无论如何,假设您的管道中只有一个场景和一个数据源:

engine = mlab.get_engine()
source = engine.scenes[0].children[0]
# choose a scene among engine.scenes if necessary
# choose a source among engine.scenes[0] if necessary

points_data = np.array(source.data.points)

在您的具体示例中,您将三个形状数组传递(50,50)points3d. 我们得到的points_data是一个 shape 数组(2500,3)。可疑的。果然,我们可以将其reshape成合适的形状,以便重构原始数据:

x,y,z = points_data.T
# x,y,z have shape (2500,) now, no way to know the "true" original shape

# if we know what their shape _should_ be, we can fix that too:
x,y,z = points.data.reshape(50,50,3).transpose(2,0,1)

# compare to the original data
radius,length = 1,2
phi, x0 = np.mgrid[0:np.pi:50j, 0:length:50j]
z0 = radius * np.sin(phi)
y0 = radius * np.cos(phi)
print(np.array_equal(x, x0)) # True
print(np.array_equal(y, y0)) # True
print(np.array_equal(z, z0)) # True

由于输入数组的多维结构与 mayavi 无关,我不希望我们可以从管道中重建具有正确形状x,y,z的原始数组。如果我是 mayavi,我会丢失所有可能的多维结构的所有输入数组,我很确定这正是发生的情况。ravel

于 2018-11-01T00:08:44.167 回答