-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkviz_2d.py
81 lines (72 loc) · 2.51 KB
/
vtkviz_2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import vtk
import numpy as np
import multneighbors_conv as sim
class vtkTimerCallback():
def __init__(self, simulation, steps, scalars, actor, iren):
self.timer_count = 0
self.simulation = simulation
self.steps = steps
self.scalars = scalars
self.npoints = scalars.GetNumberOfValues()
self.actor = actor
self.iren = iren
self.timerId = None
def MakeLut(self, inputArr):
colorSeries = vtk.vtkColorSeries()
colorSeries.SetColorScheme(vtk.vtkColorSeries.BREWER_DIVERGING_SPECTRAL_11)
lut = vtk.vtkColorTransferFunction()
lut.SetColorSpaceToHSV()
nColors = colorSeries.GetNumberOfColors()
zMin = np.min(inputArr)
zMax = np.max(inputArr)
for i in range(0, nColors):
color = colorSeries.GetColor(i)
color = [c/255.0 for c in color]
t = zMin + float(zMax - zMin)/(nColors - 1) * i
lut.AddRGBPoint(t, color[0], color[1], color[2])
return lut
def execute(self, obj, event):
for step in range(self.steps):
z = self.simulation[step].flatten()
for i in range(self.npoints):
scalars.SetValue(i, z[i])
lut_intm = self.MakeLut(z)
self.actor.GetMapper().SetLookupTable(lut_intm)
scalars.Modified()
iren = obj
iren.GetRenderWindow().Render()
self.timer_count += 1
# Simulation.
steps = 250
sim.nx = 512
sim.ny = 512
simulation = sim.simulate(steps)
# Render window and interactor.
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetWindowName('Multiple Neighborhoods CA')
renderWindow.AddRenderer(renderer)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderWindow)
# Plane to be filled with Double Array
plane = vtk.vtkPlaneSource()
plane.SetResolution(sim.nx-1, sim.ny-1)
plane.Update()
nPoints = plane.GetOutput().GetNumberOfPoints()
scalars = vtk.vtkDoubleArray()
scalars.SetNumberOfValues(nPoints)
plane.GetOutput().GetPointData().SetScalars(scalars)
# Mapper & Actor.
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(plane.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer.AddActor(actor)
# Animation callback.
interactor.Initialize()
cb = vtkTimerCallback(simulation, steps, scalars, actor, interactor)
interactor.AddObserver("TimerEvent", cb.execute)
cb.timerId = interactor.CreateRepeatingTimer(steps)
# Update out-stream
renderWindow.Render()
interactor.Start()