misc.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Miscellaneous functions and classes that don't fit into specific
  2. categories."""
  3. import sys, os
  4. def calldata_type(type):
  5. """set_call_data_type(type) -- convenience decorator to easily set the CallDataType attribute
  6. for python function used as observer callback.
  7. For example:
  8. import vtkmodules.util.calldata_type
  9. import vtkmodules.util.vtkConstants
  10. import vtkmodules.vtkCommonCore import vtkCommand, vtkLookupTable
  11. @calldata_type(vtkConstants.VTK_STRING)
  12. def onError(caller, event, calldata):
  13. print("caller: %s - event: %s - msg: %s" % (caller.GetClassName(), event, calldata))
  14. lt = vtkLookupTable()
  15. lt.AddObserver(vtkCommand.ErrorEvent, onError)
  16. lt.SetTableRange(2,1)
  17. """
  18. from vtkmodules import vtkCommonCore
  19. supported_call_data_types = ['string0', vtkCommonCore.VTK_STRING,
  20. vtkCommonCore.VTK_OBJECT, vtkCommonCore.VTK_INT,
  21. vtkCommonCore.VTK_LONG, vtkCommonCore.VTK_DOUBLE, vtkCommonCore.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. # the following functions are for the vtk regression testing and examples
  30. def vtkGetDataRoot():
  31. """vtkGetDataRoot() -- return vtk example data directory"""
  32. dataRoot = None
  33. for i, argv in enumerate(sys.argv):
  34. if argv == '-D' and i+1 < len(sys.argv):
  35. dataRoot = sys.argv[i+1]
  36. if dataRoot is None:
  37. dataRoot = os.environ.get('VTK_DATA_ROOT', '../../../../VTKData')
  38. return dataRoot
  39. def vtkGetTempDir():
  40. """vtkGetTempDir() -- return vtk testing temp dir"""
  41. tempDir = None
  42. for i, argv in enumerate(sys.argv):
  43. if argv == '-T' and i+1 < len(sys.argv):
  44. tempDir = sys.argv[i+1]
  45. if tempDir is None:
  46. tempDir = '.'
  47. return tempDir
  48. def vtkRegressionTestImage(renWin):
  49. """vtkRegressionTestImage(renWin) -- produce regression image for window
  50. This function writes out a regression .png file for a vtkWindow.
  51. Does anyone involved in testing care to elaborate?
  52. """
  53. from vtkmodules.vtkRenderingCore import vtkWindowToImageFilter
  54. from vtkmodules.vtkIOImage import vtkPNGReader
  55. from vtkmodules.vtkImagingCore import vtkImageDifference
  56. fname = None
  57. for i, argv in enumerate(sys.argv):
  58. if argv == '-V' and i+1 < len(sys.argv):
  59. fname = os.path.join(vtkGetDataRoot(), sys.argv[i+1])
  60. if fname is None:
  61. return 2
  62. else:
  63. rt_w2if = vtkWindowToImageFilter()
  64. rt_w2if.SetInput(renWin)
  65. if not os.path.isfile(fname):
  66. rt_pngw = vtkPNGWriter()
  67. rt_pngw.SetFileName(fname)
  68. rt_pngw.SetInputConnection(rt_w2if.GetOutputPort())
  69. rt_pngw.Write()
  70. rt_pngw = None
  71. rt_png = vtkPNGReader()
  72. rt_png.SetFileName(fname)
  73. rt_id = vtkImageDifference()
  74. rt_id.SetInputConnection(rt_w2if.GetOutputPort())
  75. rt_id.SetImageConnection(rt_png.GetOutputPort())
  76. rt_id.Update()
  77. if rt_id.GetThresholdedError() <= 10:
  78. return 1
  79. else:
  80. sys.stderr.write('Failed image test: %f\n'
  81. % rt_id.GetThresholdedError())
  82. return 0