File: misc.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (133 lines) | stat: -rw-r--r-- 4,415 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Miscellaneous functions and classes that don't fit into specific
categories."""

import sys, os
from functools import wraps
import warnings

def deprecated(version, message):
    """
    Decorator to mark functions as deprecated.
    When the decorated function is called, a DeprecationWarning is issued with the provided message.

    Example
    -------
    >>> @deprecated(version=1.2, message="Use 'new_function' instead.")
    ... def old_function():
    ...     pass

    >>> old_function()
    DeprecationWarning: Function 'old_function' is deprecated since 1.2. Use 'new_function' instead.

    Note you can filter warning messages, see: https://docs.python.org/3/library/warnings.html#describing-warning-filters
    """
    def decorator(func):
        warn = f"Function '{func.__name__}' is deprecated since version {version}. " + message
        @wraps(func)
        def wrapped(*args, **kwargs):
            warnings.warn(warn, DeprecationWarning)
            return func(*args, **kwargs)
        return wrapped
    return decorator

def calldata_type(type):
    """set_call_data_type(type) -- convenience decorator to easily set the CallDataType attribute
    for python function used as observer callback.
    For example:

    import vtkmodules.util.calldata_type
    import vtkmodules.util.vtkConstants
    import vtkmodules.vtkCommonCore import vtkCommand, vtkLookupTable

    @calldata_type(vtkConstants.VTK_STRING)
    def onError(caller, event, calldata):
        print("caller: %s - event: %s - msg: %s" % (caller.GetClassName(), event, calldata))

    lt = vtkLookupTable()
    lt.AddObserver(vtkCommand.ErrorEvent, onError)
    lt.SetTableRange(2,1)
    """
    from vtkmodules import vtkCommonCore
    supported_call_data_types = ['string0', vtkCommonCore.VTK_STRING,
            vtkCommonCore.VTK_OBJECT, vtkCommonCore.VTK_INT,
            vtkCommonCore.VTK_LONG, vtkCommonCore.VTK_DOUBLE, vtkCommonCore.VTK_FLOAT]

    if type not in supported_call_data_types:
        raise TypeError("'%s' is not a supported VTK call data type. Supported types are: %s" % (type, supported_call_data_types))

    def wrap(f):
        f.CallDataType = type
        return f

    return wrap

#----------------------------------------------------------------------
# the following functions are for the vtk regression testing and examples

def vtkGetDataRoot():
    """vtkGetDataRoot() -- return vtk example data directory"""
    dataRoot = None
    for i, argv in enumerate(sys.argv):
        if argv == '-D' and i+1 < len(sys.argv):
            dataRoot = sys.argv[i+1]

    if dataRoot is None:
        dataRoot = os.environ.get('VTK_DATA_ROOT', '../../../../VTKData')

    return dataRoot

def vtkGetTempDir():
    """vtkGetTempDir() -- return vtk testing temp dir"""
    tempDir = None
    for i, argv in enumerate(sys.argv):
        if argv == '-T' and i+1 < len(sys.argv):
            tempDir = sys.argv[i+1]

    if tempDir is None:
        tempDir = '.'

    return tempDir

def vtkRegressionTestImage(renWin):
    """vtkRegressionTestImage(renWin) -- produce regression image for window

    This function writes out a regression .png file for a vtkWindow.
    Does anyone involved in testing care to elaborate?
    """
    from vtkmodules.vtkRenderingCore import vtkWindowToImageFilter
    from vtkmodules.vtkIOImage import vtkPNGReader
    from vtkmodules.vtkImagingCore import vtkImageDifference

    fname = None
    for i, argv in enumerate(sys.argv):
        if argv == '-V' and i+1 < len(sys.argv):
            fname = os.path.join(vtkGetDataRoot(), sys.argv[i+1])

    if fname is None:
        return 2

    else:
        rt_w2if = vtkWindowToImageFilter()
        rt_w2if.SetInput(renWin)

        if not os.path.isfile(fname):
            rt_pngw = vtkPNGWriter()
            rt_pngw.SetFileName(fname)
            rt_pngw.SetInputConnection(rt_w2if.GetOutputPort())
            rt_pngw.Write()
            rt_pngw = None

        rt_png = vtkPNGReader()
        rt_png.SetFileName(fname)

        rt_id = vtkImageDifference()
        rt_id.SetInputConnection(rt_w2if.GetOutputPort())
        rt_id.SetImageConnection(rt_png.GetOutputPort())
        rt_id.Update()

        if rt_id.GetThresholdedError() <= 10:
            return 1
        else:
            sys.stderr.write('Failed image test: %f\n'
                             % rt_id.GetThresholdedError())
            return 0