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