1#!/usr/bin/env python
2import os
3import vtk
4from vtk.test import Testing
5from vtk.util.misc import vtkGetDataRoot
6VTK_DATA_ROOT = vtkGetDataRoot()
7
8def GetRGBColor(colorName):
9    '''
10        Return the red, green and blue components for a
11        color as doubles.
12    '''
13    rgb = [0.0, 0.0, 0.0]  # black
14    vtk.vtkNamedColors().GetColorRGB(colorName, rgb)
15    return rgb
16
17# Demonstrate the generation of a structured grid from field data. The output
18# should be similar to combIso.tcl.
19#
20# NOTE: This test only works if the current directory is writable
21#
22try:
23    channel = open("combsg.vtk", "wb")
24    channel.close()
25    channel = open("SGridField.vtk", "wb")
26    channel.close()
27
28    # Create a reader and write out the field
29    comb = vtk.vtkMultiBlockPLOT3DReader()
30    comb.SetXYZFileName(VTK_DATA_ROOT + "/Data/combxyz.bin")
31    comb.SetQFileName(VTK_DATA_ROOT + "/Data/combq.bin")
32    comb.SetScalarFunctionNumber(100)
33    comb.Update()
34
35    output = comb.GetOutput().GetBlock(0)
36
37    wsg = vtk.vtkStructuredGridWriter()
38    wsg.SetInputData(output)
39    wsg.SetFileTypeToBinary()
40    wsg.SetFileName("combsg.vtk")
41    wsg.Write()
42
43    pl3d = vtk.vtkStructuredGridReader()
44    pl3d.SetFileName("combsg.vtk")
45
46    ds2do = vtk.vtkDataSetToDataObjectFilter()
47    ds2do.SetInputConnection(pl3d.GetOutputPort())
48
49    writer = vtk.vtkDataObjectWriter()
50    writer.SetInputConnection(ds2do.GetOutputPort())
51    writer.SetFileName("SGridField.vtk")
52    writer.Write()
53
54    # read the field
55    dor = vtk.vtkDataObjectReader()
56    dor.SetFileName("SGridField.vtk")
57
58    do2ds = vtk.vtkDataObjectToDataSetFilter()
59    do2ds.SetInputConnection(dor.GetOutputPort())
60    do2ds.SetDataSetTypeToStructuredGrid()
61    do2ds.SetDimensionsComponent("Dimensions", 0)
62    do2ds.SetPointComponent(0, "Points", 0)
63    do2ds.SetPointComponent(1, "Points", 1)
64    do2ds.SetPointComponent(2, "Points", 2)
65    do2ds.Update()
66
67    fd2ad = vtk.vtkFieldDataToAttributeDataFilter()
68    fd2ad.SetInputData(do2ds.GetStructuredGridOutput())
69    fd2ad.SetInputFieldToDataObjectField()
70    fd2ad.SetOutputAttributeDataToPointData()
71    fd2ad.SetVectorComponent(0, "Momentum", 0)
72    fd2ad.SetVectorComponent(1, "Momentum", 1)
73    fd2ad.SetVectorComponent(2, "Momentum", 2)
74    fd2ad.SetScalarComponent(0, "Density", 0)
75    fd2ad.Update()
76
77    # create pipeline
78    #
79    iso = vtk.vtkContourFilter()
80    iso.SetInputConnection(fd2ad.GetOutputPort())
81    iso.SetValue(0, .38)
82
83    normals = vtk.vtkPolyDataNormals()
84    normals.SetInputConnection(iso.GetOutputPort())
85    normals.SetFeatureAngle(45)
86
87    isoMapper = vtk.vtkPolyDataMapper()
88    isoMapper.SetInputConnection(normals.GetOutputPort())
89    isoMapper.ScalarVisibilityOff()
90    isoActor = vtk.vtkActor()
91    isoActor.SetMapper(isoMapper)
92    isoActor.GetProperty().SetColor(GetRGBColor('bisque'))
93
94    outline = vtk.vtkStructuredGridOutlineFilter()
95    outline.SetInputData(fd2ad.GetStructuredGridOutput())
96
97    outlineMapper = vtk.vtkPolyDataMapper()
98    outlineMapper.SetInputConnection(outline.GetOutputPort())
99
100    outlineActor = vtk.vtkActor()
101    outlineActor.SetMapper(outlineMapper)
102
103    # Create the RenderWindow, Renderer and both Actors
104    #
105    ren1 = vtk.vtkRenderer()
106    renWin = vtk.vtkRenderWindow()
107    renWin.AddRenderer(ren1)
108    iren = vtk.vtkRenderWindowInteractor()
109    iren.SetRenderWindow(renWin)
110
111    # Add the actors to the renderer, set the background and size
112    #
113    ren1.AddActor(outlineActor)
114    ren1.AddActor(isoActor)
115    ren1.SetBackground(1, 1, 1)
116
117    renWin.SetSize(250, 250)
118
119    ren1.SetBackground(0.1, 0.2, 0.4)
120
121    cam1 = ren1.GetActiveCamera()
122    cam1.SetClippingRange(3.95297, 50)
123    cam1.SetFocalPoint(9.71821, 0.458166, 29.3999)
124    cam1.SetPosition(2.7439, -37.3196, 38.7167)
125    cam1.SetViewUp(-0.16123, 0.264271, 0.950876)
126
127    # render the image
128    #
129    renWin.Render()
130
131    # cleanup
132    #
133    try:
134        os.remove("combsg.vtk")
135    except OSError:
136        pass
137    try:
138        os.remove("SGridField.vtk")
139    except OSError:
140        pass
141
142#    iren.Start()
143
144except IOError:
145    print  "Couldn't open combsg.vtk or SGridField.vtk for writing."
146