使用Python和VTK我试图渲染10k柱面来可视化渐变方向。我已经在互联网上查看了多个示例,但没有一个示例显示您如何能够同时独立地更改每个渲染柱面的位置,方向,颜色和高度。
我知道每个气缸可以创建一个演员,但是10k的气缸会非常慢。
以下示例代码是我正在进行的有关此问题的工作。我首先为2个柱面定义一些数据然后我尝试使用VTK将它们可视化。
可能的解决方案可能是使用不同的气缸源来达到正确的高度。但我不知道如何应用这些。
也许更有经验的VTK程序员可以启发我?
from vtk import *
# input data, every row is for a different item
positions = [[0, 0, 0],
[1.5, 0, 0]]
orientations = [[1.0, 0.0, 0.0],
[0.0, 1.0, 1.0]]
colors = [[255, 0, 0],
[0, 255, 255]]
heights = [1,
2]
# rendering of those two defined cylinders
points = vtkPoints()
points.InsertNextPoint(*positions[0])
points.InsertNextPoint(*positions[1])
polydata = vtkPolyData()
polydata.SetPoints(points)
color_def = vtkUnsignedCharArray()
color_def.SetNumberOfComponents(3)
color_def.SetNumberOfTuples(polydata.GetNumberOfPoints())
color_def.InsertTuple3(0, *colors[0])
color_def.InsertTuple3(1, *colors[1])
polydata.GetPointData().SetScalars(color_def)
pointNormalsArray = vtkDoubleArray()
pointNormalsArray.SetNumberOfComponents(3)
pointNormalsArray.SetNumberOfTuples(polydata.GetNumberOfPoints())
pointNormalsArray.SetTuple(0, orientations[0])
pointNormalsArray.SetTuple(1, orientations[1])
polydata.GetPointData().SetNormals(pointNormalsArray)
cyl_source = vtkCylinderSource()
cyl_source.SetResolution(10)
cyl_source.SetHeight(0.8)
cyl_source.SetRadius(0.1)
cyl_source.Update()
glyph = vtkGlyph3D()
glyph.SetInputData(polydata)
glyph.SetSourceConnection(cyl_source.GetOutputPort())
glyph.SetColorModeToColorByScalar()
glyph.SetVectorModeToUseNormal()
glyph.ScalingOff()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(glyph.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
ren = vtkRenderer()
ren.AddActor(actor)
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
renwin.Render()
iren.Initialize()
iren.Start()
答案 0 :(得分:0)
似乎http://www.vtk.org/Wiki/VTK/Examples/Python/Visualization/ClampGlyphSizes有类似于你需要的东西(我没试过):
# Tell glyph which attribute arrays to use for what
glyph.SetInputArrayToProcess(0,0,0,0,'Elevation') # scalars
glyph.SetInputArrayToProcess(1,0,0,0,'RTDataGradient') # vectors
# glyph.SetInputArrayToProcess(2,0,0,0,'nothing') # normals
glyph.SetInputArrayToProcess(3,0,0,0,'RTData') # colors
来自文档http://www.vtk.org/doc/nightly/html/classvtkGlyph3D.html#details:
您可以设置用于标量,向量,法线和数据的数组 使用SetInputArrayToProcess方法中的颜色标量 vtkAlgorithm。第一个数组是标量,下一个是矢量,下一个是 法线,最后是颜色标量。
答案 1 :(得分:0)
vtkGlyphMapper 和 vtkGlyph3DMapper 允许您设置 3 维 ScaleArray,然后您可以将其添加到点数据中:
glyph.SetScaleArray("CylinderScales")
scaleArray = vtkDoubleArray()
scaleArray.SetName("CylinderScales")
scaleArray.SetNumberOfComponents(3)
scaleArray.SetNumberOfTuples(polydata.GetNumberOfPoints())
scaleArray.SetTuple(0, (1, 1, 1))
scaleArray.SetTuple(1, (1, 1, 2))
polydata.GetPointData().AddArray(scaleArray)
第二个圆柱体的高度应该是第一个圆柱体的两倍,除非我的轴弄错了。