Python wrapper for running instances of trisurf-ng
Samo Penic
2017-01-05 57b1a6226483e8148c14a1a2a445a7b7291dac08
commit | author | age
8ab985 1 import os,sys
SP 2 from . import trisurf
3 if sys.version_info<(3,0):
4     from vtk import *
5
6
7
8 class MultiRender:
9     def __init__(self,args,host):
10         target_runs=getRargetRunIdxList(args)
11         if target_runs==None:
12             target_runs=list(range(1,len(host['runs'])+1))
13         nruns=len(target_runs)
14         #prepare rendering window
15         self.renderer_window = vtkRenderWindow()
16         self.renderer_window.AddRenderer(self.renderer)
17         self.renderer_window.SetSize(1200,600)
18         interactor = vtkRenderWindowInteractor()
19         interactor.SetRenderWindow(self.renderer_window)
20         interactor.Initialize()
21         interactor.AddObserver("TimerEvent", self.RenderUpdate)
22         timerIDR = interactor.CreateRepeatingTimer(1000)
23         self.filename=[]
24         self.renderer=[]
25         i=0
26         for run in target_runs:    
27             #for each target run calculate renderer location
28             r.vtkRenderer()
29             r.SetBackground(0,0,0)
30             p=1.0/float(nruns)
31             x1=i*p
32             x2=(i+1)*p
33             r.SetViewport(x1,0.0,x2,1.0)
34             self.renderer.Append(r)
35             self.renderer_window.AddRenderer(r)
36             i=i+1
37         #call Renderer object with Run, renderer
38         #start endless loop of interactor
39         interactor.Start()
40
41     def lastVTU(self,run):
42         Dir=trisurf.Directory(maindir=run.maindir,simdir=run.subdir)
43         filename=os.path.join("./",Dir.fullpath(),run.getLastVTU())
44         return filename
45
46     def lastActorForRun(self,run):
47         filename=self.lastVTU(run)
48         reader=vtkXMLUnstructuredGridReader()
49         reader.SetFileName(self.filename)
50         reader.Update() # Needed because of GetScalarRange
51         output = reader.GetOutput()
52         scalar_range = output.GetScalarRange()
53         mapper = vtkDataSetMapper()
54         mapper.SetInput(output)
55         mapper.SetScalarRange(scalar_range)
56
57         # Create the Actor
58         actor = vtkActor()
59         actor.SetMapper(mapper)
60         return actor
61
62
63
64     def RenderUpdate(self, obj, event):
65         i=0
66         for run in runs:
67             if(self.lastVTU(run)!=self.filename[i]):
68                 #print("updejt")
69                 self.renderer.RemoveActor(self.actor)
70                 self.renderer.RemoveActor(self.textactor)
71                 self.actor=self.lastActor()
72                 self.textactor=self.textActor()
73                 self.renderer.AddActor(self.actor)
74                 self.renderer.AddActor(self.textactor)
75                 self.renderer_window.Render()
76             #self.render.RemoveActor(self.actor)
77             i=i+1    
78         return
79
80
81 class Renderer:
82     def __init__(self,args,host):
83         self.host=host
84         self.args=args
85         self.renderer = vtkRenderer()
86         self.actor=self.lastActor()
87         self.textactor=self.textActor()
88         self.renderer.AddActor(self.actor)
89         self.renderer.AddActor(self.textactor)
90         self.renderer.SetBackground(0, 0, 0) # Set background to white
91         # Create the RendererWindow
92         self.renderer_window = vtkRenderWindow()
93         self.renderer_window.AddRenderer(self.renderer)
94         self.renderer_window.SetSize(1200,600)
95     
96         self.renderer.SetViewport(0.0,0.0,0.5,1.0)
97         rend=vtk.vtkRenderer()
98         rend.AddActor(self.actor)
99         rend.SetViewport(0.5,0.0,1.0,1.0)
100         self.renderer_window.AddRenderer(rend)    
101 # Set up a check for aborting rendering.
102         # Create the RendererWindowInteractor and display the vtk_file
103         interactor = vtkRenderWindowInteractor()
104         interactor.SetRenderWindow(self.renderer_window)
105         interactor.Initialize()
427578 106         interactor.AddObserver("TimerEvent", self.RenderUpdate)
8ab985 107         timerIDR = interactor.CreateRepeatingTimer(1000)
SP 108         interactor.Start()
109         return
110
111     def lastVTU(self):
112         Dir=trisurf.Directory(maindir=self.host['runs'][0].maindir,simdir=self.host['runs'][0].subdir)
113         filename=os.path.join("./",Dir.fullpath(),self.host['runs'][0].getLastVTU())
114         return filename
115
116     def textActor(self):
117         textactor=vtkTextActor()
118         textactor.SetInput(self.filename)
119         tp=textactor.GetTextProperty()
120         tp.SetColor(1,1,1)
121         tp.SetFontSize(11)
122         textactor.SetDisplayPosition(20,30)
123         return textactor
124
125     def lastActor(self):
126         self.filename=self.lastVTU()
127         reader=vtkXMLUnstructuredGridReader()
128         reader.SetFileName(self.filename)
129         reader.Update() # Needed because of GetScalarRange
130         output = reader.GetOutput()
131         scalar_range = output.GetScalarRange()
132         mapper = vtkDataSetMapper()
133         mapper.SetInput(output)
134         mapper.SetScalarRange(scalar_range)
135
136         # Create the Actor
137         actor = vtkActor()
138         actor.SetMapper(mapper)
139         return actor
140
141
142     def RenderUpdate(self, obj, event):
143         if(self.lastVTU()!=self.filename):
144             #print("updejt")
145             self.renderer.RemoveActor(self.actor)
146             self.renderer.RemoveActor(self.textactor)
147             self.actor=self.lastActor()
148             self.textactor=self.textActor()
149             self.renderer.AddActor(self.actor)
150             self.renderer.AddActor(self.textactor)
151             self.renderer_window.Render()
152         #self.render.RemoveActor(self.actor)
153         
154         return