Python wrapper for running instances of trisurf-ng
Miha
2019-03-25 f039f4c3e44d77456521013030fb3e955e7d9a61
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")
a4f8eb 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:
d55834 83     def __init__(self,args,host,run, timestep=-1, scalar_field='vertices_idx'):
8ab985 84         self.host=host
SP 85         self.args=args
f49271 86         self.run=run
5f0a35 87         self.timestep=timestep
d55834 88         self.scalar_field=scalar_field
8ab985 89         self.renderer = vtkRenderer()
SP 90         self.actor=self.lastActor()
91         self.textactor=self.textActor()
92         self.renderer.AddActor(self.actor)
93         self.renderer.AddActor(self.textactor)
282bda 94         self.renderer.SetBackground(81/255, 87/255, 110/255) # Set background to nicely grey
8ab985 95         # Create the RendererWindow
SP 96         self.renderer_window = vtkRenderWindow()
97         self.renderer_window.AddRenderer(self.renderer)
282bda 98         self.renderer_window.SetSize(800,800)
8ab985 99     
f49271 100 #        self.renderer.SetViewport(0.0,0.0,0.5,1.0)
SP 101 #        rend=vtk.vtkRenderer()
102 #        rend.AddActor(self.actor)
103 #        rend.SetViewport(0.5,0.0,1.0,1.0)
104 #        self.renderer_window.AddRenderer(rend)    
8ab985 105 # Set up a check for aborting rendering.
SP 106         # Create the RendererWindowInteractor and display the vtk_file
107         interactor = vtkRenderWindowInteractor()
108         interactor.SetRenderWindow(self.renderer_window)
109         interactor.Initialize()
427578 110         interactor.AddObserver("TimerEvent", self.RenderUpdate)
f49271 111         timerIDR = interactor.CreateRepeatingTimer(10000)
8ab985 112         interactor.Start()
SP 113         return
114
115     def lastVTU(self):
f49271 116         #Dir=trisurf.Directory(maindir=self.host['runs'][self.run].maindir,simdir=self.host['runs'][self.run].subdir)
SP 117         Dir=self.run.Dir
118         #print(self.run.getLastVTU())
119         filename=os.path.join("./",Dir.fullpath(),self.run.getLastVTU())
120         #filename=os.path.join("./",Dir.fullpath(),self.host['runs'][self.run].getLastVTU())
8ab985 121         return filename
SP 122
123     def textActor(self):
124         textactor=vtkTextActor()
125         textactor.SetInput(self.filename)
126         tp=textactor.GetTextProperty()
127         tp.SetColor(1,1,1)
128         tp.SetFontSize(11)
129         textactor.SetDisplayPosition(20,30)
130         return textactor
131
132     def lastActor(self):
5f0a35 133         if(self.timestep<0):
SP 134             self.filename=self.lastVTU()
135         else:
136             self.filename=os.path.join("./",self.run.Dir.fullpath(),'timestep_{:06d}.vtu'.format(self.timestep))
8ab985 137         reader=vtkXMLUnstructuredGridReader()
SP 138         reader.SetFileName(self.filename)
139         reader.Update() # Needed because of GetScalarRange
140         output = reader.GetOutput()
d55834 141         output.GetPointData().SetActiveScalars(self.scalar_field)
8ab985 142         scalar_range = output.GetScalarRange()
SP 143         mapper = vtkDataSetMapper()
8b7022 144         mapper.SetInputData(output)
8ab985 145         mapper.SetScalarRange(scalar_range)
d55834 146         
SP 147         #color lookuptables
148         #lut = vtk.vtkLookupTable()
149         #lut.SetHueRange(0.5, 0.6)
150         #lut.SetSaturationRange(0, 1)
151         #lut.SetValueRange(0, 1)
152         #mapper.SetLookupTable(lut)
8ab985 153
282bda 154         #advanced lookuptables...
SP 155         lut = vtk.vtkLookupTable()
156         lutNum = 256
157         lut.SetNumberOfTableValues(lutNum)
158         ctf = vtk.vtkColorTransferFunction()
159         ctf.SetColorSpaceToDiverging()
160         ctf.AddRGBPoint(0.0, 0.230, 0.299, 0.754)
161         ctf.AddRGBPoint(1.0, 0.706, 0.016, 0.150)
162         for ii,ss in enumerate([float(xx)/float(lutNum) for xx in range(lutNum)]):
163             cc = ctf.GetColor(ss)
164             lut.SetTableValue(ii,cc[0],cc[1],cc[2],1.0)
165         mapper.SetLookupTable(lut)
8ab985 166         # Create the Actor
SP 167         actor = vtkActor()
168         actor.SetMapper(mapper)
169         return actor
170
171
172     def RenderUpdate(self, obj, event):
173         if(self.lastVTU()!=self.filename):
174             #print("updejt")
175             self.renderer.RemoveActor(self.actor)
176             self.renderer.RemoveActor(self.textactor)
177             self.actor=self.lastActor()
178             self.textactor=self.textActor()
179             self.renderer.AddActor(self.actor)
180             self.renderer.AddActor(self.textactor)
181             self.renderer_window.Render()
182         #self.render.RemoveActor(self.actor)
183         
184         return
83999e 185
SP 186
187 class RenderVTUFile(Renderer):
188     def __init__(self,args,filename=None,scalar_field='vertices_idx'):
189         self.filename=filename
190         Renderer.__init__(self,args,host=None, run=None, timestep=-1, scalar_field=scalar_field)
191
192     def lastVTU(self):
193         if(self.filename):
194             return self.filename
195         else:
196             raise Exception('No file specified')
197