class vtkTest(unittest.TestCase): """A simple default VTK test class that defines a few useful blackbox tests that can be readily used. Derive your test cases from this class and use the following if you'd like to. Note: Unittest instantiates this class (or your subclass) each time it tests a method. So if you do not want that to happen when generating VTK pipelines you should create the pipeline in the class definition as done below for _blackbox. """ _blackbox = BlackBox.Tester(debug=0) # Due to what seems to be a bug in python some objects leak. # Avoid the exit-with-error in vtkDebugLeaks. dl = vtk.vtkDebugLeaks() dl.SetExitError(0) dl = None def _testParse(self, obj): """Does a blackbox test by attempting to parse the class for its various methods using vtkMethodParser. This is a useful test because it gets all the methods of the vtkObject, parses them and sorts them into different classes of objects.""" self._blackbox.testParse(obj) def _testGetSet(self, obj, excluded_methods=[]): """Checks the Get/Set method pairs by setting the value using the current state and making sure that it equals the value it was originally. This effectively calls _testParse internally. """ self._blackbox.testGetSet(obj, excluded_methods) def _testBoolean(self, obj, excluded_methods=[]): """Checks the Boolean methods by setting the value on and off and making sure that the GetMethod returns the the set value. This effectively calls _testParse internally. """ self._blackbox.testBoolean(obj, excluded_methods)
ren.GetActiveCamera().SetPosition(0,1,0) ren.GetActiveCamera().SetFocalPoint(0,0,0) ren.GetActiveCamera().SetViewUp(0,0,1) # # let the renderer compute good position and focal point # ren.ResetCamera() ren.GetActiveCamera().Zoom(1.4) ren1.ResetCameraClippingRange() # render the large image # renderLarge = vtk.vtkRenderLargeImage() renderLarge.SetInput(ren1) renderLarge.SetMagnification(3) renderLarge.Update() viewer = vtk.vtkImageViewer() viewer.SetInputConnection(renderLarge.GetOutputPort()) viewer.SetColorWindow(255) viewer.SetColorLevel(127.5) viewer.Render() # on several opengl X window unix implementations # multiple context deletes cause errors # so we leak the renWin1 in this test for unix if (tcl_platform("platform") == "unix"): renWin1.Register(ren1) dl = vtk.vtkDebugLeaks() dl.SetExitError(0) del dl pass # --- end of script --
# let the renderer compute good position and focal point # ren.ResetCamera() ren.GetActiveCamera().Zoom(1.4) ren1.ResetCameraClippingRange() # render the large image # renderLarge = vtk.vtkRenderLargeImage() renderLarge.SetInput(ren1) renderLarge.SetMagnification(3) renderLarge.Update() viewer = vtk.vtkImageViewer() viewer.SetInputConnection(renderLarge.GetOutputPort()) viewer.SetColorWindow(255) viewer.SetColorLevel(127.5) viewer.Render() # on several opengl X window unix implementations # multiple context deletes cause errors # so we leak the renWin1 in this test for unix if renWin1.IsA('vtkXOpenGLRenderWindow'): renWin1.Register(ren1) dl = vtk.vtkDebugLeaks() dl.SetExitError(0) del dl # iren.Initialize() # iren.Start()
class vtkTest(unittest.TestCase): """A simple default VTK test class that defines a few useful blackbox tests that can be readily used. Derive your test cases from this class and use the following if you'd like to. Note: Unittest instantiates this class (or your subclass) each time it tests a method. So if you do not want that to happen when generating VTK pipelines you should create the pipeline in the class definition as done below for _blackbox. """ _blackbox = BlackBox.Tester(debug=0) # Due to what seems to be a bug in python some objects leak. # Avoid the exit-with-error in vtkDebugLeaks. dl = vtk.vtkDebugLeaks() dl.SetExitError(0) dl = None def _testParse(self, obj): """Does a blackbox test by attempting to parse the class for its various methods using vtkMethodParser. This is a useful test because it gets all the methods of the vtkObject, parses them and sorts them into different classes of objects.""" self._blackbox.testParse(obj) def _testGetSet(self, obj, excluded_methods=[]): """Checks the Get/Set method pairs by setting the value using the current state and making sure that it equals the value it was originally. This effectively calls _testParse internally. """ self._blackbox.testGetSet(obj, excluded_methods) def _testBoolean(self, obj, excluded_methods=[]): """Checks the Boolean methods by setting the value on and off and making sure that the GetMethod returns the the set value. This effectively calls _testParse internally. """ self._blackbox.testBoolean(obj, excluded_methods) def pathToData(self, filename): """Given a filename with no path (i.e., no leading directories prepended), return the full path to a file as specified on the command line with a '-D' option. As an example, if a test is run with "-D /path/to/grid.vtu" then calling self.pathToData('grid.vtu') in your test will return "/path/to/grid.vtu". This is useful in combination with ExternalData, where data may be staged by CTest to a user-configured directory at build time. In order for this method to work, you must specify the JUST_VALID option for your test in CMake. """ global VTK_DATA_PATHS if not filename: return VTK_DATA_PATHS for path in VTK_DATA_PATHS: if filename == os.path.split(path)[-1]: return path return filename def pathToValidatedOutput(self, filename): """Given a filename with no path (i.e., no leading directories prepended), return the full path to a file as specified on the command line with a '-V' option. As an example, if a test is run with "-V /path/to/validImage.png" then calling self.pathToData('validImage.png') in your test will return "/path/to/validImage.png". This is useful in combination with ExternalData, where data may be staged by CTest to a user-configured directory at build time. In order for this method to work, you must specify the JUST_VALID option for your test in CMake. """ global VTK_BASELINE_PATHS if not filename: return VTK_BASELINE_PATHS for path in VTK_BASELINE_PATHS: if filename == os.path.split(path)[-1]: return path return filename def prepareTestImage(self, interactor, **kwargs): import time startTime = time.time() events = [] def onKeyPress(caller, eventId): print('key is "' + caller.GetKeySym() + '"') events.append((time.time() - startTime, eventId, caller.GetKeySym())) def onButton(caller, eventId): events.append((time.time() - startTime, eventId)) def onMovement(caller, eventId): events.append((time.time() - startTime, eventId, caller.GetEventPosition())) interactor.AddObserver(vtk.vtkCommand.KeyPressEvent, onKeyPress) interactor.AddObserver(vtk.vtkCommand.LeftButtonPressEvent, onButton) interactor.AddObserver(vtk.vtkCommand.LeftButtonReleaseEvent, onButton) interactor.AddObserver(vtk.vtkCommand.MouseMoveEvent, onMovement) interactor.Start() rw = interactor.GetRenderWindow() baseline = 'baselineFilename' if 'filename' in kwargs: # Render an image and save it to the given filename w2if = vtk.vtkWindowToImageFilter() w2if.ReadFrontBufferOff() w2if.SetInput(rw) w2if.Update() baselineWithPath = kwargs['filename'] baseline = os.path.split(baselineWithPath)[-1] pngw = vtk.vtkPNGWriter() pngw.SetFileName(baselineWithPath) pngw.SetInputConnection(w2if.GetOutputPort()) try: pngw.Write() except RuntimeError: w2if.ReadFrontBufferOn() pngw.Write() rsz = rw.GetSize() rrc = rw.GetRenderers() rrs = [rrc.GetItemAsObject(i) for i in range(rrc.GetNumberOfItems())] eye = [0,0,1] aim = [0,0,0] up = [0,1,0] if len(rrs) > 0: cam = rrs[0].GetActiveCamera() eye = cam.GetPosition() aim = cam.GetFocalPoint() up = cam.GetViewUp() print(""" Replace prepareTestImage() in your script with the following to make a test: camera.SetPosition({eye[0]}, {eye[1]}, {eye[2]}) camera.SetFocalPoint({aim[0]}, {aim[1]}, {aim[2]}) camera.SetViewUp({up[0]}, {up[1]}, {up[2]}) renwin.SetSize({rsz[0]}, {rsz[1]}) self.assertImageMatch(renwin, '{baseline}') Be sure that "renwin" and "camera" are valid variables (or rename them in the snippet above) referencing the vtkRenderWindow and vtkCamera, respectively. """.format(eye=eye, aim=aim, up=up, rsz=rsz, baseline=baseline)) return events def assertImageMatch(self, renwin, baseline, **kwargs): """Throw an error if a rendering in the render window does not match the baseline image. This method accepts a threshold keyword argument (with a default of 10) that specifies how different a baseline may be before causing a failure. """ absoluteBaseline = baseline try: open(absoluteBaseline, 'r') except: absoluteBaseline = getAbsImagePath(baseline) compareImage(renwin, absoluteBaseline, **kwargs)