Package pyllar :: Module Figure
[hide private]
[frames] | no frames]

Source Code for Module pyllar.Figure

  1  """ The Figure class as well as many wrapper functions for Matlab-style syntax.
 
  2  """ 
  3  
 
  4  
 
  5  import os 
  6  from Axes import Axes 
  7  import wx 
  8  import wx.lib.buttonpanel as bp 
  9  from wxVTKRenderWindow import wxVTKRenderWindow 
 10  import vtk 
 11  
 
 12  currentFigure = 0 # keep track of current figure 
 13  """ Handle to the current figure window.
 
 14  """ 
 15  
 
 16  
 
 17  pyllarPath = os.path.split(os.path.join(os.getcwd(), __file__))[0] 
 18  """ Path of the pillar package.
 
 19  """ 
 20  
 
 21  
 
22 -class Figure:
23 """ The figure is the top level window that contains the axes and figure controls. 24 """
25 - def __init__(self):
26 """ Create a new instance of the Figure class. 27 """ 28 self.app = wx.PySimpleApp() 29 30 # create the frame (top-level window), panel, and sizer 31 self.frame = wx.Frame(None, -1, "Pyllar Figure", size=wx.Size(600,600)) 32 self.panel = wx.Panel(self.frame, -1) 33 self.sizer = wx.BoxSizer(wx.VERTICAL) 34 self.panel.SetSizer(self.sizer) 35 36 # create the figure toolbar, an intance of FigToolbar 37 self.toolbar = FigToolbar(self.panel, self) 38 39 # create the render window 40 self.widget = wxVTKRenderWindow(self.panel,-1) 41 self.widget.Enable(1) 42 self.renWin = self.widget.GetRenderWindow() 43 44 # create an axes 45 self.axes = Axes(self.renWin) 46 47 # add everything to the sizer 48 self.sizer.Add(self.widget, 1, wx.EXPAND|wx.ALL, 5) 49 50 self.sizer.Layout() 51 52 currentFigure = self
53
54 - def show(self):
55 """ Show the contents of the figure. 56 57 Makes the axes update its bounds based on its contents. 58 This should be called at the end of a script so the figure contents are displayed. 59 """ 60 self.frame.Show(1) 61 62 self.axes.UpdateBounds() 63 self.axes.ScaleChildren() 64 self.widget.Render() # hack to make the axes in the far corner (doesn't work with wxPython now) 65 self.axes.camera.SetPosition(5, 2, 1.5) 66 67 self.app.MainLoop()
68 69 70 71 ## End of the Figure class definition ## 72 73 74 75
76 -class FigToolbar:
77 - def __init__(self, panel, fig):
78 """ Create a new instance of the figure toolbar. 79 80 The toolbar contains controls for saving the axes to a png, 81 moving the camera to a pre-defined position, and adjusting 82 the camera projection (parallel of perspective). 83 """ 84 self.panel = panel 85 self.fig = fig 86 87 # create the toolbar 88 self.toolbar = bp.ButtonPanel(panel, -1, "", bp.BP_ALIGN_TOP) 89 self.toolbar.style = bp.BP_USE_GRADIENT 90 91 # export button 92 exportButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/export.png"), wx.BITMAP_TYPE_PNG)) 93 self.toolbar.AddButton(exportButton) 94 self.panel.Bind(wx.EVT_BUTTON, self.OnExport, exportButton) 95 96 self.toolbar.AddSeparator() 97 98 # standard view button 99 standardViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/standardView.png"), wx.BITMAP_TYPE_PNG)) 100 self.toolbar.AddButton(standardViewButton) 101 self.panel.Bind(wx.EVT_BUTTON, self.OnStandardView, standardViewButton) 102 103 # front view button 104 frontViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/frontView.png"), wx.BITMAP_TYPE_PNG)) 105 self.toolbar.AddButton(frontViewButton) 106 self.panel.Bind(wx.EVT_BUTTON, self.OnFrontView, frontViewButton) 107 108 # back view button 109 backViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/backView.png"), wx.BITMAP_TYPE_PNG)) 110 self.toolbar.AddButton(backViewButton) 111 self.panel.Bind(wx.EVT_BUTTON, self.OnBackView, backViewButton) 112 113 # left view button 114 leftViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/leftView.png"), wx.BITMAP_TYPE_PNG)) 115 self.toolbar.AddButton(leftViewButton) 116 self.panel.Bind(wx.EVT_BUTTON, self.OnLeftView, leftViewButton) 117 118 # right view button 119 rightViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/rightView.png"), wx.BITMAP_TYPE_PNG)) 120 self.toolbar.AddButton(rightViewButton) 121 self.panel.Bind(wx.EVT_BUTTON, self.OnRightView, rightViewButton) 122 123 # top view button 124 topViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/topView.png"), wx.BITMAP_TYPE_PNG)) 125 self.toolbar.AddButton(topViewButton) 126 self.panel.Bind(wx.EVT_BUTTON, self.OnTopView, topViewButton) 127 128 # bottom view button 129 bottomViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/bottomView.png"), wx.BITMAP_TYPE_PNG)) 130 self.toolbar.AddButton(bottomViewButton) 131 self.panel.Bind(wx.EVT_BUTTON, self.OnBottonView, bottomViewButton) 132 133 self.toolbar.AddSeparator() 134 135 # projection style choice 136 self.projectionChoice = wx.Choice(self.toolbar, -1, choices=["Perspective", "Parallel"]) 137 self.toolbar.AddControl(self.projectionChoice) 138 self.panel.Bind(wx.EVT_CHOICE, self.OnChangeProjection, self.projectionChoice) 139 140 141 # set the toolbar colors 142 bpArt = self.toolbar.GetBPArt() 143 144 145 # set the color the text is drawn with 146 bpArt.SetColor(bp.BP_TEXT_COLOR, wx.WHITE) 147 148 # These default to white and whatever is set in the system 149 # settings for the wx.SYS_COLOUR_ACTIVECAPTION. We'll use 150 # some specific settings to ensure a consistent look for the 151 # demo. 152 bpArt.SetColor(bp.BP_BORDER_COLOR, wx.Colour(224,224,224)) 153 bpArt.SetColor(bp.BP_GRADIENT_COLOR_FROM, wx.Colour(112,112,112)) 154 bpArt.SetColor(bp.BP_GRADIENT_COLOR_TO, wx.Colour(224,224,224)) 155 bpArt.SetColor(bp.BP_BUTTONTEXT_COLOR, wx.Colour(70,143,255)) 156 bpArt.SetColor(bp.BP_SEPARATOR_COLOR, bp.BrightenColour(wx.Colour(60, 11, 112), 0.85)) 157 bpArt.SetColor(bp.BP_SELECTION_BRUSH_COLOR, wx.Color(225, 225, 255)) 158 bpArt.SetColor(bp.BP_SELECTION_PEN_COLOR, wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION)) 159 160 161 bpArt.SetGradientType( bp.BP_GRADIENT_VERTICAL) 162 163 self.toolbar.SetStyle(self.toolbar.style) 164 165 # add the panel to the sizer 166 fig.sizer.Add(self.toolbar, 0, wx.EXPAND)
167 168
169 - def OnExport(self, evt):
170 """ Exports the axes to a bitmap. 171 172 Opens a dialog that prompts the user for the bitmap's name. 173 """ 174 # get the file name 175 fileDialog = wx.FileDialog(self.fig.frame, "Export Figure", "", "export.png", "PNG file (*.png)|BMP file (*.bmp)", wx.FD_SAVE) 176 fileDialog.ShowModal() 177 178 path = fileDialog.GetPath() 179 180 if len(path)>0: 181 # write the image 182 w2i = vtk.vtkWindowToImageFilter() 183 writer = vtk.vtkPNGWriter() 184 w2i.SetInput(self.fig.renWin) 185 w2i.Update() 186 writer.SetInputConnection(w2i.GetOutputPort()) 187 writer.SetFileName(path) 188 self.fig.renWin.Render() 189 writer.Write()
190 191
192 - def OnStandardView(self, evt):
193 """ Set the camera to the standard 3D view. 194 """ 195 self.fig.axes.SetViewDirection(Axes.standardDirection) 196 self.fig.axes.SetViewUp(Axes.standardViewUp) 197 self.fig.widget.Render()
198
199 - def OnFrontView(self, evt):
200 """ Set the camera to the front view. 201 """ 202 self.fig.axes.SetViewDirection(Axes.frontDirection) 203 self.fig.axes.SetViewUp(Axes.frontViewUp) 204 self.fig.widget.Render()
205
206 - def OnBackView(self, evt):
207 """ Set the camera to the rear view. 208 """ 209 self.fig.axes.SetViewDirection(Axes.backDirection) 210 self.fig.axes.SetViewUp(Axes.backViewUp) 211 self.fig.widget.Render()
212
213 - def OnLeftView(self, evt):
214 """ Set the camera to the left view. 215 """ 216 self.fig.axes.SetViewDirection(Axes.leftDirection) 217 self.fig.axes.SetViewUp(Axes.leftViewUp) 218 self.fig.widget.Render()
219
220 - def OnRightView(self, evt):
221 """ Set the camera to the right view. 222 """ 223 self.fig.axes.SetViewDirection(Axes.rightDirection) 224 self.fig.axes.SetViewUp(Axes.rightViewUp) 225 self.fig.widget.Render()
226
227 - def OnTopView(self, evt):
228 """ Set the camera to the top view. 229 """ 230 self.fig.axes.SetViewDirection(Axes.topDirection) 231 self.fig.axes.SetViewUp(Axes.topViewUp) 232 self.fig.widget.Render()
233
234 - def OnBottonView(self, evt):
235 """ Set the camera to the bottom view. 236 """ 237 self.fig.axes.SetViewDirection(Axes.bottomDirection) 238 self.fig.axes.SetViewUp(Axes.bottomViewUp) 239 self.fig.widget.Render()
240
241 - def OnChangeProjection(self, evt):
242 """ Changes the projection between parallel and projection. 243 """ 244 if self.projectionChoice.GetCurrentSelection()==0: 245 self.fig.axes.camera.ParallelProjectionOff() 246 self.fig.axes.SetDistance(Axes.perspectiveDistance) 247 else: 248 self.fig.axes.camera.ParallelProjectionOn() 249 self.fig.axes.SetDistance(Axes.parallelDistance) 250 self.fig.axes.SetViewDirection(Axes.standardDirection) 251 self.fig.axes.SetViewUp(Axes.standardViewUp) 252 self.fig.widget.Render()
253
254 - def OnAbout(self, evt):
255 """ Opens the "about" dialog. 256 """ 257 wx.MessageBox("Pyllar figure window.")
258
259 - def OnExit(self, evt):
260 """ Closes the figure window. 261 """ 262 self.frame.Destroy()
263 264 265 ## End FigMenu class definition ## 266 267 268 269 ### The following functions map Matlab commands to the axes object. ### 270 271 # returns a handle to the current figure
272 -def gcf():
273 """ Get the current figure. 274 275 The current figure will be returned. 276 If no figures are present, one will be created. 277 """ 278 if currentFigure is 0: 279 Figure() 280 return currentFigure
281 282 # returns a handle to the current axes
283 -def gca():
284 """ Get the current axes. 285 286 The current axes will be returned. 287 If no axes are present, one will be created. 288 """ 289 if Axes.num == 0: 290 Figure() 291 return Axes.current
292 293 294 # x label interface
295 -def xlabel(label=""):
296 """ Set the label on the x axis of the current axes. 297 """ 298 if label is "": # there was no label specified 299 return gca().GetXLabel() 300 else: 301 gca().SetXLabel(label)
302 303 # y label interface
304 -def ylabel(label=""):
305 """ Set the label on the y axis of the current axes. 306 """ 307 308 if label is "": # there was no label specified 309 return gca().GetYLabel() 310 else: 311 gca().SetYLabel(label)
312 313 # z label interface
314 -def zlabel(label=""):
315 """ Set the label on the z axis of the current axes. 316 """ 317 if label is "": # there was no label specified 318 return gca().GetZLabel() 319 else: 320 gca().SetZLabel(label)
321 322 # x limits interface
323 -def xlim(lim=[0,0]):
324 """ Set the limis of the x axis of the current axes. 325 326 Calling this function will override the automatic limit sizing of the axes. 327 """ 328 if lim is not [0,0]: 329 gca().SetXLim(lim) 330 else: 331 return gca().GetXLim()
332 333 # y limits interface
334 -def ylim(lim=[0,0]):
335 """ Set the limis of the y axis of the current axes. 336 337 Calling this function will override the automatic limit sizing of the axes. 338 """ 339 if lim is not [0,0]: 340 gca().SetYLim(lim) 341 else: 342 return gca().GetYLim()
343 344 # z limits interface
345 -def zlim(lim=[0,0]):
346 """ Set the limis of the z axis of the current axes. 347 348 Calling this function will override the automatic limit sizing of the axes. 349 """ 350 if lim is not [0,0]: 351 gca().SetZLim(lim) 352 else: 353 return gca().GetZLim()
354