def testUnstructured(self): rt = vtk.vtkRTAnalyticSource() rt.SetWholeExtent(-5, 5, -5, 5, -5, 5) t = vtk.vtkThreshold() t.SetInputConnection(rt.GetOutputPort()) t.ThresholdByUpper(-10) s = vtk.vtkSphere() s.SetRadius(2) s.SetCenter(0,0,0) c = vtk.vtkTableBasedClipDataSet() c.SetInputConnection(t.GetOutputPort()) c.SetClipFunction(s) c.SetInsideOut(1) c.Update() self.assertEqual(c.GetOutput().GetNumberOfCells(), 64) eg = vtk.vtkEnSightGoldReader() eg.SetCaseFileName(VTK_DATA_ROOT + "/Data/EnSight/elements.case") eg.Update() pl = vtk.vtkPlane() pl.SetOrigin(3.5, 3.5, 0.5) pl.SetNormal(0, 0, 1) c.SetInputConnection(eg.GetOutputPort()) c.SetClipFunction(pl) c.SetInsideOut(1) c.Update() data = c.GetOutputDataObject(0).GetBlock(0) self.assertEqual(data.GetNumberOfCells(), 75) rw = vtk.vtkRenderWindow() ren = vtk.vtkRenderer() rw.AddRenderer(ren) mapper = vtk.vtkDataSetMapper() mapper.SetInputData(data) actor = vtk.vtkActor() actor.SetMapper(mapper) ren.AddActor(actor) ac = ren.GetActiveCamera() ac.SetPosition(-7.9, 9.7, 14.6) ac.SetFocalPoint(3.5, 3.5, 0.5) ac.SetViewUp(0.08, 0.93, -0.34) rw.Render() ren.ResetCameraClippingRange() rtTester = vtk.vtkTesting() for arg in sys.argv[1:]: rtTester.AddArgument(arg) rtTester.AddArgument("-V") rtTester.AddArgument("tableBasedClip.png") rtTester.SetRenderWindow(rw) rw.Render() rtResult = rtTester.RegressionTest(10)
def compare_images(test_img, baseline_img, tmp_dir="."): """ This function creates a vtkTesting object, and specifies the name of the baseline image file, using a fully qualified path (baseline_img must be fully qualified). Then it calls the vtkTesting method which compares the image (test_img, specified only with a relative path) against the baseline image as well as any other images in the same directory as the baseline image which follow the naming pattern: 'img.png', 'img_1.png', ... , 'img_N.png' test_img: File name of output image to be compared agains baseline. baseline_img: Fully qualified path to first of the baseline images. tmp_dir: Fully qualified path to a temporary directory for storing images. """ # Create a vtkTesting object and specify a baseline image t = vtk.vtkTesting() t.AddArgument("-T") t.AddArgument(tmp_dir) t.AddArgument("-V") t.AddArgument(baseline_img) # Perform the image comparison test and print out the result. return t.RegressionTest(test_img, 0.0)
def testEncodings(self): # Render something cylinder = vtk.vtkCylinderSource() cylinder.SetResolution(8) cylinderMapper = vtk.vtkPolyDataMapper() cylinderMapper.SetInputConnection(cylinder.GetOutputPort()) cylinderActor = vtk.vtkActor() cylinderActor.SetMapper(cylinderMapper) cylinderActor.RotateX(30.0) cylinderActor.RotateY(-45.0) ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) ren.AddActor(cylinderActor) renWin.SetSize(200, 200) ren.ResetCamera() ren.GetActiveCamera().Zoom(1.5) renWin.Render() # Get a vtkImageData with the rendered output w2if = vtk.vtkWindowToImageFilter() w2if.SetInput(renWin) w2if.SetShouldRerender(1) w2if.SetReadFrontBuffer(0) w2if.Update() imgData = w2if.GetOutput() # Use vtkDataEncoder to convert the image to PNG format and Base64 encode it encoder = vtk.vtkDataEncoder() base64String = encoder.EncodeAsBase64Png(imgData).encode('ascii') # Now Base64 decode the string back to PNG image data bytes inputArray = array.array('B', base64String) outputBuffer = bytearray(len(inputArray)) utils = vtk.vtkIOCore.vtkBase64Utilities() actualLength = utils.DecodeSafely(inputArray, len(inputArray), outputBuffer, len(outputBuffer)) outputArray = bytearray(actualLength) outputArray[:] = outputBuffer[0:actualLength] # And write those bytes to the disk as an actual PNG image file with open('TestDataEncoder.png', 'wb') as fd: fd.write(outputArray) # Create a vtkTesting object and specify a baseline image rtTester = vtk.vtkTesting() for arg in sys.argv[1:]: rtTester.AddArgument(arg) rtTester.AddArgument("-V") rtTester.AddArgument("TestDataEncoder.png") # Perform the image comparison test and print out the result. result = rtTester.RegressionTest("TestDataEncoder.png", 0.0) if result == 0: raise Exception("TestDataEncoder failed.")
def testEncodings(self): # Render something cylinder = vtk.vtkCylinderSource() cylinder.SetResolution(8) cylinderMapper = vtk.vtkPolyDataMapper() cylinderMapper.SetInputConnection(cylinder.GetOutputPort()) cylinderActor = vtk.vtkActor() cylinderActor.SetMapper(cylinderMapper) cylinderActor.RotateX(30.0) cylinderActor.RotateY(-45.0) ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) ren.AddActor(cylinderActor) renWin.SetSize(200, 200) ren.ResetCamera() ren.GetActiveCamera().Zoom(1.5) renWin.Render() # Get a vtkImageData with the rendered output w2if = vtk.vtkWindowToImageFilter() w2if.SetInput(renWin) w2if.SetShouldRerender(1) w2if.SetReadFrontBuffer(0) w2if.Update() imgData = w2if.GetOutput() # Use vtkDataEncoder to convert the image to PNG format and Base64 encode it encoder = vtk.vtkDataEncoder() base64String = encoder.EncodeAsBase64Png(imgData).encode('ascii') # Now Base64 decode the string back to PNG image data bytes outputBuffer = bytearray(120000) inputArray = array.array('B', base64String) utils = vtk.vtkIOCore.vtkBase64Utilities() actualLength = utils.Decode(inputArray, 120000, outputBuffer) outputArray = bytearray(actualLength) outputArray[:] = outputBuffer[0:actualLength] # And write those bytes to the disk as an actual PNG image file with open('TestDataEncoder.png', 'wb') as fd: fd.write(outputArray) # Create a vtkTesting object and specify a baseline image rtTester = vtk.vtkTesting() for arg in sys.argv[1:]: rtTester.AddArgument(arg) rtTester.AddArgument("-V") rtTester.AddArgument("TestDataEncoder.png") # Perform the image comparison test and print out the result. result = rtTester.RegressionTest("TestDataEncoder.png", 0.0) if result == 0: raise Exception("TestDataEncoder failed.")
def testPythonItem(self): width = 400 height = 400 view = vtk.vtkContextView() renWin = view.GetRenderWindow() renWin.SetSize(width, height) area = vtk.vtkInteractiveArea() view.GetScene().AddItem(area) drawAreaBounds = vtk.vtkRectd(0.0, 0.0, 1.0, 1.0) vp = [0.05, 0.95, 0.05, 0.95] screenGeometry = vtk.vtkRecti(int(vp[0] * width), int(vp[2] * height), int((vp[1] - vp[0]) * width), int((vp[3] - vp[2]) * height)) item = vtk.vtkPythonItem() item.SetPythonObject(CustomPythonItem(buildPolyData())) item.SetVisible(True) area.GetDrawAreaItem().AddItem(item) area.SetDrawAreaBounds(drawAreaBounds) area.SetGeometry(screenGeometry) area.SetFillViewport(False) area.SetShowGrid(False) axisLeft = area.GetAxis(vtk.vtkAxis.LEFT) axisRight = area.GetAxis(vtk.vtkAxis.RIGHT) axisBottom = area.GetAxis(vtk.vtkAxis.BOTTOM) axisTop = area.GetAxis(vtk.vtkAxis.TOP) axisTop.SetVisible(False) axisRight.SetVisible(False) axisLeft.SetVisible(False) axisBottom.SetVisible(False) axisTop.SetMargins(0, 0) axisRight.SetMargins(0, 0) axisLeft.SetMargins(0, 0) axisBottom.SetMargins(0, 0) renWin.Render() # Create a vtkTesting object rtTester = vtk.vtkTesting() rtTester.SetRenderWindow(renWin) for arg in sys.argv[1:]: rtTester.AddArgument(arg) # Perform the image comparison test and print out the result. result = rtTester.RegressionTest(0.0) if result == 0: raise Exception("TestPythonItem failed.")
def compare_images(test_img, baseline_img): # Create a vtkTesting object, and tell it to use the current directory as # the "baseline root directory", and specify the name of the baseline image # file itself. t = vtk.vtkTesting() t.AddArgument("-B") t.AddArgument(".") t.AddArgument("-V") t.AddArgument(baseline_img) # Perform the image comparison test and print out the result. t.RegressionTest(test_img, 0.0)
try: import Tkinter pythonTk = Tkinter.Tk() pythonTk.withdraw() except: pythonTk = None pass #no hassles if Tk is not present. # setup some common things for testing rtTempObject = vtk.vtkObject() rtExMath = vtk.vtkMath() rtExMath.RandomSeed(6) # create the testing class to do the work rtTester = vtk.vtkTesting() for arg in sys.argv[2:]: rtTester.AddArgument(arg) VTK_DATA_ROOT = rtTester.GetDataRoot() # load in the script test_script = sys.argv[1] # set the default threshold, the Tcl script may change this threshold = -1 # we pass the locals over so that the test script has access to # all the locals we have defined here. exec(compile(open(test_script).read(), test_script, 'exec'), globals(), locals())
import vtk import sys testing = vtk.vtkTesting() for arg in sys.argv: testing.AddArgument(arg) VTK_DATA_ROOT = testing.GetDataRoot() def Test1(datadir): reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName(datadir + "/Data/quadraticTetra01.vtu") reader.UpdateInformation(); reader.GetPointDataArraySelection().EnableAllArrays() reader.GetCellDataArraySelection().EnableAllArrays() f = vtk.vtkIntegrateAttributes() f.SetInputConnection(reader.GetOutputPort()) f.Update() result = f.GetOutputDataObject(0) val = result.GetPointData().GetArray("scalars").GetValue(0) assert (val > 0.0162 and val < 0.01621) val = result.GetCellData().GetArray("Volume").GetValue(0) assert (val > 0.128 and val < 0.1284) def Test2(datadir): reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName(datadir + "/Data/elements.vtu") reader.UpdateInformation();
try: import Tkinter pythonTk = Tkinter.Tk() pythonTk.withdraw() except: pythonTk = None pass #no hassles if Tk is not present. # setup some common things for testing rtTempObject = vtk.vtkObject() rtExMath = vtk.vtkMath() rtExMath.RandomSeed(6) # create the testing class to do the work rtTester = vtk.vtkTesting() for arg in sys.argv[2:]: rtTester.AddArgument(arg) VTK_DATA_ROOT = rtTester.GetDataRoot() if rtTester.IsInteractiveModeSpecified() == 0: vtk.vtkRenderWindowInteractor = vtkTestingInteractor # load in the script test_script = sys.argv[1] # set the default threshold, the Tcl script may change this threshold = -1