1#!/usr/bin/env python
2import vtk
3from math import cos, sin, pi
4from vtk.util.misc import vtkGetDataRoot
5VTK_DATA_ROOT = vtkGetDataRoot()
6
7# Create the RenderWindow, Renderer and both Actors
8#
9ren1 = vtk.vtkRenderer()
10renWin = vtk.vtkRenderWindow()
11renWin.AddRenderer(ren1)
12iren = vtk.vtkRenderWindowInteractor()
13iren.SetRenderWindow(renWin)
14
15math = vtk.vtkMath()
16
17# Generate some random colors
18def MakeColors (lut, n):
19    lut.SetNumberOfColors(n)
20    lut.SetTableRange(0, n - 1)
21    lut.SetScaleToLinear()
22    lut.Build()
23    lut.SetTableValue(0, 0, 0, 0, 1)
24    math.RandomSeed(5071)
25    i = 1
26    while i < n:
27        lut.SetTableValue(i, math.Random(.2, 1),
28          math.Random(.2, 1), math.Random(.2, 1), 1)
29        i += 1
30
31lut = vtk.vtkLookupTable()
32MakeColors(lut, 256)
33n = 20
34radius = 10
35
36# This has been moved outside the loop so that the code can be correctly
37# translated to python
38blobImage = vtk.vtkImageData()
39
40i = 0
41while i < n:
42    sphere = vtk.vtkSphere()
43    sphere.SetRadius(radius)
44    max = 50 - radius
45    sphere.SetCenter(int(math.Random(-max, max)),
46      int(math.Random(-max, max)), int(math.Random(-max, max)))
47
48    sampler = vtk.vtkSampleFunction()
49    sampler.SetImplicitFunction(sphere)
50    sampler.SetOutputScalarTypeToFloat()
51    sampler.SetSampleDimensions(51, 51, 51)
52    sampler.SetModelBounds(-50, 50, -50, 50, -50, 50)
53
54    thres = vtk.vtkImageThreshold()
55    thres.SetInputConnection(sampler.GetOutputPort())
56    thres.ThresholdByLower(radius * radius)
57    thres.ReplaceInOn()
58    thres.ReplaceOutOn()
59    thres.SetInValue(i + 1)
60    thres.SetOutValue(0)
61    thres.Update()
62    if (i == 0):
63        blobImage.DeepCopy(thres.GetOutput())
64
65    maxValue = vtk.vtkImageMathematics()
66    maxValue.SetInputData(0, blobImage)
67    maxValue.SetInputData(1, thres.GetOutput())
68    maxValue.SetOperationToMax()
69    maxValue.Modified()
70    maxValue.Update()
71
72    blobImage.DeepCopy(maxValue.GetOutput())
73
74    i += 1
75
76angle = pi/6
77orientation = [
78  -cos(angle), 0, sin(angle),
79  0, 1, 0,
80  sin(angle), 0, cos(angle),
81]
82blobImage.SetDirectionMatrix(orientation)
83
84# Extract labeled blobs
85discrete = vtk.vtkDiscreteMarchingCubes()
86discrete.SetInputData(blobImage)
87discrete.GenerateValues(n, 1, n)
88
89mapper = vtk.vtkPolyDataMapper()
90mapper.SetInputConnection(discrete.GetOutputPort())
91mapper.SetLookupTable(lut)
92mapper.SetScalarRange(0, lut.GetNumberOfColors())
93
94actor = vtk.vtkActor()
95actor.SetMapper(mapper)
96
97# Put an outline around it
98outline = vtk.vtkImageDataOutlineFilter()
99outline.SetInputData(blobImage)
100
101outlineMapper = vtk.vtkPolyDataMapper()
102outlineMapper.SetInputConnection(outline.GetOutputPort())
103
104outlineActor = vtk.vtkActor()
105outlineActor.SetMapper(outlineMapper)
106outlineActor.GetProperty().SetColor(1,1,1)
107
108ren1.AddActor(actor)
109ren1.AddActor(outlineActor)
110
111renWin.Render()
112iren.Start()
113