ShrinkCube
Repository source: ShrinkCube
Description¶
Generates a cube using vtkCubeSource, then a shrink filter is applied.
vtkShrinkFilter object shrinks cells composing an arbitrary data set towards their centroid. The centroid of a cell is computed as the average position of the cell points. Shrinking results in disconnecting the cells from one another.
Seealso
TessellatedBoxSource generates multiple quads or triangles per side.
Info
See Figure 5-17 in Chapter 5 the VTK Textbook.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ShrinkCube.py
#!/usr/bin/env python
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersGeneral import vtkShrinkFilter
from vtkmodules.vtkFiltersSources import vtkCubeSource
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkProperty,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
# Create a cube.
cubeSource = vtkCubeSource()
shrink = vtkShrinkFilter()
shrink.SetInputConnection(cubeSource.GetOutputPort())
shrink.SetShrinkFactor(0.9)
# Create a mapper and actor.
mapper = vtkDataSetMapper()
mapper.SetInputConnection(shrink.GetOutputPort())
back = vtkProperty()
back.SetColor(colors.GetColor3d('Tomato'))
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().EdgeVisibilityOn()
actor.GetProperty().SetColor(colors.GetColor3d('Banana'))
actor.SetBackfaceProperty(back)
# Create a renderer, render window, and interactor.
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Add the actors to the scene
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d('Silver'))
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(30)
renderer.GetActiveCamera().Elevation(30)
renderer.ResetCameraClippingRange()
# Render and interact
renderWindow.SetWindowName('ShrinkCube')
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
main()