TransformSphere
Repository source: TransformSphere
Description¶
This example extends the pipeline of the ColoredSphere.py example and shows the effects of type checking on the connectivity of process objects. We add a transform filter (vtkTransformFilter) to nonuniformly scale the sphere in the x-y-z directions. The transform filter only operates on objects with explicit point coordinate representation (i.e., a subclass of vtkPointSet ). However, the elevation filter generates the more general form vtkDataSet as output. Hence we cannot connect the transform filter to the elevation filter. But we can connect the transform filter to the sphere source, and then the elevation filter to the transform filter. (Note: an alternative method is to use vtkCastToConcrete to perform run-time casting.)
Info
See Figure 4-20 in Chapter 4 the VTK Textbook.
Other languages
See (Cxx), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
TransformSphere.py
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkLookupTable
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersCore import vtkElevationFilter
from vtkmodules.vtkFiltersGeneral import vtkTransformFilter
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
renderer = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
sphere = vtkSphereSource()
sphere.SetThetaResolution(12)
sphere.SetPhiResolution(12)
aTransform = vtkTransform()
aTransform.Scale(1, 1.5, 2)
transFilter = vtkTransformFilter()
transFilter.SetInputConnection(sphere.GetOutputPort())
transFilter.SetTransform(aTransform)
colorIt = vtkElevationFilter()
colorIt.SetInputConnection(transFilter.GetOutputPort())
colorIt.SetLowPoint(0, 0, -1)
colorIt.SetHighPoint(0, 0, 1)
lut = vtkLookupTable()
lut.SetHueRange(0.667, 0)
lut.SetSaturationRange(1, 1)
lut.SetValueRange(1, 1)
mapper = vtkDataSetMapper()
mapper.SetLookupTable(lut)
mapper.SetInputConnection(colorIt.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d("SlateGray"))
renderer.ResetCamera()
renderer.GetActiveCamera().Elevation(60.0)
renderer.GetActiveCamera().Azimuth(30.0)
renderer.ResetCameraClippingRange()
renWin.SetSize(640, 480)
renWin.SetWindowName('TransformSphere')
renWin.Render()
# Interact with the data.
iren.Start()
if __name__ == '__main__':
main()