vtk glyphs 3D, indenpently color and rotation
Posted
by
user3684219
on Stack Overflow
See other posts from Stack Overflow
or by user3684219
Published on 2014-05-28T15:21:05Z
Indexed on
2014/05/28
15:24 UTC
Read the original article
Hit count: 401
I try to display thanks to vtk (python wrapper) several glyphs in a scene with each their own colour and rotation. Unfortunately, just the rotation (using vtkTensorGlyph) is taken in consideration by vtk. Reversely, just color is taken in consideration when I use a vtkGlyph3D. Here is a ready to use piece of code with a vtkTensorGlyph. Each cube should have a random color but there all will be in the same color. I read and read again the doc of vtk but I found no solution.
Thanks in advance for any idea
#!/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()
© Stack Overflow or respective owner