我试图使用python包装器来显示一个场景中的几个glyph,每个glyph都有自己的颜色和旋转。不幸的是,vtk只考虑旋转(使用vtkTensorGlyph
)。相反,当我使用vtkGlyph3D
时,只考虑颜色。
下面是一段可以使用的代码。每个立方体应该有一个随机的颜色,但是它们都是相同的颜色。我反复阅读了VTK的文档,但没有找到任何解决方案。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import vtk
import scipy.linalg as sc
import random as ra
import numpy as np
import itertools
points = vtk.vtk.vtkPoints() # where to locate each glyph in the scene
tensors = vtk.vtkDoubleArray() # rotation for each glyph
tensors.SetNumberOfComponents(9)
colors = vtk.vtkUnsignedCharArray() # should be the color for each glyph
colors.SetNumberOfComponents(3)
# let's make 10 cubes in the scene
for i in range(0, 50, 5):
points.InsertNextPoint(i, i, i) # position of a glyph
colors.InsertNextTuple3(ra.randint(0, 255), ra.randint(0, 255), ra.randint(0, 255) ) # pick random color
rot = list(itertools.chain(*np.reshape(sc.orth(np.random.rand(3, 3)).transpose(), (1, 9)).tolist())) # random rotation matrix (row major)
tensors.InsertNextTuple9(*rot)
polydata = vtk.vtkPolyData() # create the polydatas
polydata.SetPoints(points)
polydata.GetPointData().SetTensors(tensors)
polydata.GetPointData().SetScalars(colors)
cubeSource = vtk.vtkCubeSource()
cubeSource.Update()
glyphTensor = vtk.vtkTensorGlyph()
glyphTensor.SetColorModeToScalars() # is it really work ?
try:
glyphTensor.SetInput(polydata)
except AttributeError:
glyphTensor.SetInputData(polydata)
glyphTensor.SetSourceConnection(cubeSource.GetOutputPort())
glyphTensor.ColorGlyphsOn() # should not color all cubes independently ?
glyphTensor.ThreeGlyphsOff()
glyphTensor.ExtractEigenvaluesOff()
glyphTensor.Update()
# next is usual vtk code
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(glyphTensor.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
ren = vtk.vtkRenderer()
ren.SetBackground(0.2, 0.5, 0.3)
ren.AddActor(actor)
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.SetRenderWindow(renwin)
renwin.Render()
iren.Initialize()
renwin.Render()
iren.Start()
最佳答案
我终于做了点什么。
如果我很理解,字形需要一个曲面,尤其是构成这个曲面的每个点上的法线。每个法线都给出每个字形的方向。
vtk sources中Examples/VisualizationAlgorithms/Python/spikeF.py
处的示例说明了我的意思。
这里有一个小例子,颜色和旋转独立的标志符号。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from vtk import *
# Let's create a surface with 3 points unconnected.
points = vtkPoints()
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(0, 0, 0)
points.InsertNextPoint(0, 1, 0)
polydata = vtkPolyData()
polydata.SetPoints(points)
colors = vtkUnsignedCharArray()
colors.SetNumberOfComponents(3)
colors.SetNumberOfTuples(polydata.GetNumberOfPoints())
colors.InsertTuple3(0, 147, 25, 98)
colors.InsertTuple3(1, 32, 84, 247)
colors.InsertTuple3(2, 198, 214, 36)
polydata.GetPointData().SetScalars(colors)
# Now, let's control normal on each point composing or 'fake' surface
# We should say, let's give a direction to each point, a normal in strange for a point.
pointNormalsArray = vtkDoubleArray()
pointNormalsArray.SetNumberOfComponents(3)
pointNormalsArray.SetNumberOfTuples(polydata.GetNumberOfPoints())
pN1 = [1.0, 0.0, 0.0]
pN2 = [0.0, 1.0, 0.0]
pN3 = [0.0, 0.0, 1.0]
pointNormalsArray.SetTuple(0, pN1)
pointNormalsArray.SetTuple(1, pN2)
pointNormalsArray.SetTuple(2, pN3)
polydata.GetPointData().SetNormals(pointNormalsArray)
sources = vtkConeSource()
sources.SetResolution(6)
sources.Update()
glyph = vtkGlyph3D()
try:
glyph.SetInput(polydata)
except AttributeError:
glyph.SetInputData(polydata)
glyph.SetSourceConnection(sources.GetOutputPort())
glyph.SetColorModeToColorByScalar()
glyph.SetVectorModeToUseNormal()
glyph.ScalingOff()
glyph.Update()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(glyph.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
ren = vtkRenderer()
ren.SetBackground(0.2, 0.5, 0.3)
ren.AddActor(actor)
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.SetRenderWindow(renwin)
renwin.Render()
iren.Initialize()
renwin.Render()
iren.Start()
顺便说一下,有一个更简单的解决方案,包括创建一个源,以及所需的任何映射器和参与者。事实上,给每个演员一个颜色和方向是很容易的。问题是,对于很多演员(在我的机器上大约有5000人)来说,表演确实不利于互动。