1#!/usr/bin/env python
2
3# This example demonstrates the conversion of point data to cell data.
4# The conversion is necessary because we want to threshold data based
5# on cell scalar values.
6
7import vtk
8from vtk.util.misc import vtkGetDataRoot
9VTK_DATA_ROOT = vtkGetDataRoot()
10
11# Read some data with point data attributes. The data is from a
12# plastic blow molding process (e.g., to make plastic bottles) and
13# consists of two logical components: a mold and a parison. The
14# parison is the hot plastic that is being molded, and the mold is
15# clamped around the parison to form its shape.
16reader = vtk.vtkUnstructuredGridReader()
17reader.SetFileName(VTK_DATA_ROOT + "/Data/blow.vtk")
18reader.SetScalarsName("thickness9")
19reader.SetVectorsName("displacement9")
20
21# Convert the point data to cell data. The point data is passed
22# through the filter so it can be warped. The vtkThresholdFilter then
23# thresholds based on cell scalar values and extracts a portion of the
24# parison whose cell scalar values lie between 0.25 and 0.75.
25p2c = vtk.vtkPointDataToCellData()
26p2c.SetInputConnection(reader.GetOutputPort())
27p2c.PassPointDataOn()
28warp = vtk.vtkWarpVector()
29warp.SetInputConnection(p2c.GetOutputPort())
30thresh = vtk.vtkThreshold()
31thresh.SetInputConnection(warp.GetOutputPort())
32thresh.ThresholdBetween(0.25, 0.75)
33thresh.SetInputArrayToProcess(1, 0, 0, 0, "thickness9")
34#thresh.SetAttributeModeToUseCellData()
35
36# This is used to extract the mold from the parison.
37connect = vtk.vtkConnectivityFilter()
38connect.SetInputConnection(thresh.GetOutputPort())
39connect.SetExtractionModeToSpecifiedRegions()
40connect.AddSpecifiedRegion(0)
41connect.AddSpecifiedRegion(1)
42moldMapper = vtk.vtkDataSetMapper()
43moldMapper.SetInputConnection(reader.GetOutputPort())
44moldMapper.ScalarVisibilityOff()
45moldActor = vtk.vtkActor()
46moldActor.SetMapper(moldMapper)
47moldActor.GetProperty().SetColor(.2, .2, .2)
48moldActor.GetProperty().SetRepresentationToWireframe()
49
50# The threshold filter has been used to extract the parison.
51connect2 = vtk.vtkConnectivityFilter()
52connect2.SetInputConnection(thresh.GetOutputPort())
53parison = vtk.vtkGeometryFilter()
54parison.SetInputConnection(connect2.GetOutputPort())
55normals2 = vtk.vtkPolyDataNormals()
56normals2.SetInputConnection(parison.GetOutputPort())
57normals2.SetFeatureAngle(60)
58lut = vtk.vtkLookupTable()
59lut.SetHueRange(0.0, 0.66667)
60parisonMapper = vtk.vtkPolyDataMapper()
61parisonMapper.SetInputConnection(normals2.GetOutputPort())
62parisonMapper.SetLookupTable(lut)
63parisonMapper.SetScalarRange(0.12, 1.0)
64parisonActor = vtk.vtkActor()
65parisonActor.SetMapper(parisonMapper)
66
67# We generate some contour lines on the parison.
68cf = vtk.vtkContourFilter()
69cf.SetInputConnection(connect2.GetOutputPort())
70cf.SetValue(0, .5)
71contourMapper = vtk.vtkPolyDataMapper()
72contourMapper.SetInputConnection(cf.GetOutputPort())
73contours = vtk.vtkActor()
74contours.SetMapper(contourMapper)
75
76# Create graphics stuff
77ren = vtk.vtkRenderer()
78renWin = vtk.vtkRenderWindow()
79renWin.AddRenderer(ren)
80iren = vtk.vtkRenderWindowInteractor()
81iren.SetRenderWindow(renWin)
82
83# Add the actors to the renderer, set the background and size
84ren.AddActor(moldActor)
85ren.AddActor(parisonActor)
86ren.AddActor(contours)
87
88ren.ResetCamera()
89ren.GetActiveCamera().Azimuth(60)
90ren.GetActiveCamera().Roll(-90)
91ren.GetActiveCamera().Dolly(2)
92ren.ResetCameraClippingRange()
93
94ren.SetBackground(1, 1, 1)
95renWin.SetSize(750, 400)
96
97iren.Initialize()
98renWin.Render()
99iren.Start()
100