简体   繁体   中英

python, vtk glyphs with independent position, orientation, color, and height

Using Python and VTK I am trying to render 10k cylinders to visualize gradient directions. I have reviewed multiple examples on the internet but none of them shows how you can, at the same time, change the position, orientation, color and height of each rendered cylinder independently.

I know it is possible to create an actor per cylinder but with 10k cylinders that would be very slow.

The following example code is my work in progress on this problem. I first define some data for 2 cylinders and next I try to visualize them with VTK.

  • What works is the position, orientation and color of the cylinders.
  • What does not work is the height of each cylinder.

Possible solutions might be to use different cylinder sources to get the heights right. Yet I don't know how to apply those.

Perhaps a more experienced VTK programmer can enlighten me?


    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()

It seems that http://www.vtk.org/Wiki/VTK/Examples/Python/Visualization/ClampGlyphSizes has something similar to what you need (I haven't tried):

# 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

From the documentation http://www.vtk.org/doc/nightly/html/classvtkGlyph3D.html#details :

You can set what arrays to use for the scalars, vectors, normals, and color scalars by using the SetInputArrayToProcess methods in vtkAlgorithm. The first array is scalars, the next vectors, the next normals and finally color scalars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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