1"""Miscellaneous functions and classes that dont fit into specific
2categories."""
3
4import sys, os, vtk
5
6def calldata_type(type):
7    """set_call_data_type(type) -- convenience decorator to easily set the CallDataType attribute
8    for python function used as observer callback.
9    For example:
10
11    import vtk
12
13    @vtk.calldata_type(vtk.VTK_STRING)
14    def onError(caller, event, calldata):
15        print("caller: %s - event: %s - msg: %s" % (caller.GetClassName(), event, calldata))
16
17    lt = vtk.vtkLookupTable()
18    lt.AddObserver(vtk.vtkCommand.ErrorEvent, onError)
19    lt.SetTableRange(2,1)
20    """
21    supported_call_data_types = ['string0', vtk.VTK_STRING, vtk.VTK_OBJECT, vtk.VTK_INT, vtk.VTK_LONG, vtk.VTK_DOUBLE, vtk.VTK_FLOAT]
22    if type not in supported_call_data_types:
23        raise TypeError("'%s' is not a supported VTK call data type. Supported types are: %s" % (type, supported_call_data_types))
24    def wrap(f):
25        f.CallDataType = type
26        return f
27    return wrap
28
29#----------------------------------------------------------------------
30# the following functions are for the vtk regression testing and examples
31
32def vtkGetDataRoot():
33    """vtkGetDataRoot() -- return vtk example data directory
34    """
35    dataIndex=-1;
36    for i in range(0, len(sys.argv)):
37        if sys.argv[i] == '-D' and i < len(sys.argv)-1:
38            dataIndex = i+1
39
40    if dataIndex != -1:
41        dataRoot = sys.argv[dataIndex]
42    else:
43        try:
44            dataRoot = os.environ['VTK_DATA_ROOT']
45        except KeyError:
46            dataRoot = '../../../../VTKData'
47
48    return dataRoot
49
50def vtkGetTempDir():
51    """vtkGetTempDir() -- return vtk testing temp dir
52    """
53    tempIndex=-1;
54    for i in range(0, len(sys.argv)):
55        if sys.argv[i] == '-T' and i < len(sys.argv)-1:
56            tempIndex = i+1
57
58    if tempIndex != -1:
59        tempDir = sys.argv[tempIndex]
60    else:
61        tempDir = '.'
62
63    return tempDir
64
65
66def vtkRegressionTestImage( renWin ):
67    """vtkRegressionTestImage(renWin) -- produce regression image for window
68
69    This function writes out a regression .png file for a vtkWindow.
70    Does anyone involved in testing care to elaborate?
71    """
72    imageIndex=-1;
73    for i in range(0, len(sys.argv)):
74        if sys.argv[i] == '-V' and i < len(sys.argv)-1:
75            imageIndex = i+1
76
77    if imageIndex != -1:
78        fname = os.path.join(vtkGetDataRoot(), sys.argv[imageIndex])
79
80        rt_w2if = vtk.vtkWindowToImageFilter()
81        rt_w2if.SetInput(renWin)
82
83        if os.path.isfile(fname):
84            pass
85        else:
86            rt_pngw = vtk.vtkPNGWriter()
87            rt_pngw.SetFileName(fname)
88            rt_pngw.SetInputConnection(rt_w2if.GetOutputPort())
89            rt_pngw.Write()
90            rt_pngw = None
91
92        rt_png = vtk.vtkPNGReader()
93        rt_png.SetFileName(fname)
94
95        rt_id = vtk.vtkImageDifference()
96        rt_id.SetInputConnection(rt_w2if.GetOutputPort())
97        rt_id.SetImageConnection(rt_png.GetOutputPort())
98        rt_id.Update()
99
100        if rt_id.GetThresholdedError() <= 10:
101            return 1
102        else:
103            sys.stderr.write('Failed image test: %f\n'
104                             % rt_id.GetThresholdedError())
105            return 0
106    return 2
107