QuantLib/VtkIntegration

From Wikiversity
Jump to navigation Jump to search

This is the page for discussion of research integrating QuantLib with Vtk

VTK First Example[edit | edit source]

The VTK source code comes with a multitude of examples, including a tutorial in the scripting language of your choice (currently supported are Tcl, Python and Java). They are located in the Examples directory that can be downloaded with the source code distribution.

Assuming you are interested in something a little bit more elaborate, you can download here a very simple example that uses Tkinter to produce the viewer shown below:

Below is the entire source code used to produce this output. In my opinion, the main contribution of the script in terms of learning to use VTK is the organization of the code and the integration with Tkinter (which can take a little while to figure out). Other than that, it is quite easy to see that the actual VTK code is very thin. You can test the code yourself by using the input surface provided here .


import Tkinter

import vtk
from vtk.tk.vtkTkRenderWindowInteractor import \
     vtkTkRenderWindowInteractor

class Application:

    def __init__(self,root,
                 w=600,h=600):
        self.root = root

        # VTK setup
        self.ren = vtk.vtkRenderer()
        self.renWin = vtk.vtkRenderWindow()
        self.renWin.AddRenderer(self.ren)

        # setup main actor
        self.SetupActor()

        # initialize GUI
        self.InitializeGUI(w,h)

        # initialize the interactor
        iren = self.renderWidget.GetRenderWindow().GetInteractor()
        self.ren.SetBackground( .5, .5, .5)

        self.ren.ResetCamera()

        iren.Initialize()

        self.renWin.Render()
        iren.Start()

    def InitializeGUI(self,w,h):

        self.top = Tkinter.Toplevel(self.root)

        # make sure exit happens gracefully
        def quit(obj=self.root) : obj.quit()
        self.top.protocol("WM_DELETE_WINDOW", quit)

        self.frmDisplay = Tkinter.Frame(self.top)
        self.frmRender = Tkinter.Frame(self.frmDisplay)

        for f in (self.frmDisplay,self.frmRender):
            f.pack(padx=3,pady=3,
                   side="top",
                   anchor="n",
                   fill="both",
                   expand="false")

        rwi = vtkTkRenderWindowInteractor(self.frmRender,
                                          rw=self.renWin,
                                          width=w,height=h)
        rwi.pack()
        rwi.Render()
        self.renderWidget = rwi
        self.root.update()

        self.SetupMenubar()

    def SetupMenubar(self):

        self.menu = Tkinter.Menu(self.top)

        # File menu
        mnFile = Tkinter.Menu(self.menu, tearoff=0)

        def SaveCapture():
            w2i = vtk.vtkWindowToImageFilter()
            writer = vtk.vtkTIFFWriter()
            w2i.SetInput( self.renWin)
            writer.SetInputConnection( w2i.GetOutputPort() )
            writer.SetFileName( 'snapshot.tif' )
            self.renWin.Render()
            writer.Write()
        mnFile.add_command(label="Save TIF",
                           command = SaveCapture)

        mnFile.add_command(label="Exit",command=self.top.quit)
        self.menu.add_cascade(label="File",
                              menu = mnFile)

        # set application menu
        self.top.config(menu=self.menu)

    def SetupActor(self):

        mapper = vtk.vtkPolyDataMapper()

        # load input surface
        reader = vtk.vtkPolyDataReader()
        reader.SetFileName( 'surf.vtk')
        reader.Update()

        mapper.SetInput( reader.GetOutput() )

        self.actor = vtk.vtkActor()
        self.actor.SetMapper(mapper)

        self.ren.AddActor( self.actor )

##################3

root = Tkinter.Tk()
root.withdraw()

app = Application(root,700,700)
root.mainloop()

Building VTK with Python support[edit | edit source]

When downloading the Windows binaries for VTK, for example, the only scripting language that is automatically supported is Tcl (the same happens under MacOS, to the best of my knowledge). To enable Python support, you have to enable the appropriate settings when building VTK with cmake. Namely, you have to enable VTK_WRAP_PYTHON. This also means you will have to build shared libraries, but the cmake process will kindly guide you through it.

Once built, you can learn more on how to actually use VTK through Python reading the VTK/Wrapping/Python/README.txt.