Created
August 9, 2018 18:25
-
-
Save subhacom/508e55b043f4216e379c941fa5319b08 to your computer and use it in GitHub Desktop.
Simple script showing how to set size and color of glyphs using different scalar arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vtk_size_color.py --- | |
# Author: Subhasis Ray | |
# Created: Thu Aug 9 11:04:16 2018 (-0400) | |
# Last-Updated: Thu Aug 9 14:24:15 2018 (-0400) | |
# By: Subhasis Ray | |
# Version: $Id$ | |
# Code: | |
"""Test simultaneous radius and color settings in glyph. | |
The key conceptual clarification came from VTK mailing list: | |
According to https://www.vtk.org/pipermail/vtkusers/2004-August/026366.html | |
* Create a sub-class of vtkDataArray for each scalar you have | |
* (optional) Assign them names with SetName() | |
* Add them to point or cell data with AddArray: | |
output->GetPointData()->AddArray(array) | |
* Set one of them to be the default scalars: | |
output->GetPointData()->SetActiveScalars(array_name) | |
Alternatively, you can add the default scalars with | |
output->GetPointData()->SetScalars() | |
which internally (sort of) does the following: | |
output->GetPointData()->AddArray(array) | |
output->GetPointData()->SetActiveScalars(array) | |
To later set the default scalars in a VTK pipeline, use | |
vtkAssignAttribute. To set which scalar to color by, use | |
one of the methods in the mapper: | |
SetScalarModeToXXX() | |
ColorByComponent() | |
SelectColorArray() | |
""" | |
import vtk | |
ctf = vtk.vtkColorTransferFunction() | |
ctf.AddRGBPoint(0.0, 1.0, 0, 0) | |
ctf.AddRGBPoint(0.5, 0.0, 1.0, 0) | |
ctf.AddRGBPoint(1, 0.0, 0, 1.0) | |
sphere = vtk.vtkSphereSource() | |
polydata = vtk.vtkPolyData() | |
points = vtk.vtkPoints() | |
points.SetNumberOfPoints(2) | |
points.SetPoint(0, [0, 0, 0]) | |
points.SetPoint(1, [1, 1, 0]) | |
polydata.SetPoints(points) | |
radii = vtk.vtkFloatArray() | |
radii.SetName('Radius') | |
for ii in range(2): | |
r = (2 - ii)/2.0 | |
radii.InsertNextTuple1(r) | |
print('Radius {} = {}'.format(ii, r)) | |
polydata.GetPointData().AddArray(radii) | |
# The following sets the "ActiveScalar", i.e. default scalar used by | |
# VTK (and is must for setting radius of each sphere by corresponding | |
# value in radii array). But what tells VTK to use it for scaling? | |
polydata.GetPointData().SetActiveScalars(radii.GetName()) | |
colors = vtk.vtkFloatArray() | |
colors.SetName('Color') | |
for ii in range(2): | |
cval = ii/2.0 | |
colors.InsertNextTuple1(cval) | |
print('Color {} = {}, r:{}, g:{}, b:{}'.format(ii, cval, | |
ctf.GetRedValue(cval), | |
ctf.GetGreenValue(cval), | |
ctf.GetBlueValue(cval))) | |
print('colors array name:', colors.GetName()) | |
polydata.GetPointData().AddArray(colors) | |
glyph = vtk.vtkGlyph3D() | |
glyph.SetSourceConnection(sphere.GetOutputPort()) | |
glyph.SetInputData(polydata) | |
# print('Scale mode:', glyph.GetScaleMode()) # prints 0, default | |
glyph.SetScaleModeToScaleByScalar() # Works fine without this! - because this is default | |
print('Scale mode:', glyph.GetScaleMode()) # prints 0, default | |
# glyph.SetScaleModeToDataScalingOff() | |
# print('Scale mode:', glyph.GetScaleMode()) # This prints 3 and the spheres are same radius | |
mapper = vtk.vtkPolyDataMapper() | |
mapper.SetInputConnection(glyph.GetOutputPort()) | |
# Right! | |
# This is the correct way of setting color by the color array attached | |
# to the pointdata | |
mapper.SetScalarModeToUsePointFieldData() | |
# Wrong! | |
# The following will use radii for color look up | |
# So 0-th sphere will be blue (r=1.0, blue in CTF) | |
# and 1st sphere will be green (r=0.5, green in CTF) | |
# mapper.SetScalarModeToUsePointData() | |
# mapper.ScalarVisibilityOn() # Is this required? No. | |
mapper.SelectColorArray(colors.GetName()) | |
mapper.SetLookupTable(ctf) | |
actor = vtk.vtkActor() | |
actor.SetMapper(mapper) | |
renderer = vtk.vtkRenderer() | |
renderer.AddActor(actor) | |
win = vtk.vtkRenderWindow() | |
win.AddRenderer(renderer) | |
interactor = vtk.vtkRenderWindowInteractor() | |
interactor.SetRenderWindow(win) | |
interactor.Initialize() | |
interactor.Start() | |
# | |
# vtk_size_color.py ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment