vtkVariant.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. """
  2. Utility functions to mimic the template support functions for vtkVariant
  3. """
  4. from vtkmodules import vtkCommonCore
  5. import sys
  6. _variant_type_map = {
  7. 'void' : vtkCommonCore.VTK_VOID,
  8. 'char' : vtkCommonCore.VTK_CHAR,
  9. 'unsigned char' : vtkCommonCore.VTK_UNSIGNED_CHAR,
  10. 'signed char' : vtkCommonCore.VTK_SIGNED_CHAR,
  11. 'short' : vtkCommonCore.VTK_SHORT,
  12. 'unsigned short' : vtkCommonCore.VTK_UNSIGNED_SHORT,
  13. 'int' : vtkCommonCore.VTK_INT,
  14. 'unsigned int' : vtkCommonCore.VTK_UNSIGNED_INT,
  15. 'long' : vtkCommonCore.VTK_LONG,
  16. 'unsigned long' : vtkCommonCore.VTK_UNSIGNED_LONG,
  17. 'long long' : vtkCommonCore.VTK_LONG_LONG,
  18. 'unsigned long long' : vtkCommonCore.VTK_UNSIGNED_LONG_LONG,
  19. 'float' : vtkCommonCore.VTK_FLOAT,
  20. 'double' : vtkCommonCore.VTK_DOUBLE,
  21. 'string' : vtkCommonCore.VTK_STRING,
  22. 'vtkObjectBase' : vtkCommonCore.VTK_OBJECT,
  23. 'vtkObject' : vtkCommonCore.VTK_OBJECT,
  24. }
  25. _variant_method_map = {
  26. vtkCommonCore.VTK_VOID : '',
  27. vtkCommonCore.VTK_CHAR : 'ToChar',
  28. vtkCommonCore.VTK_UNSIGNED_CHAR : 'ToUnsignedChar',
  29. vtkCommonCore.VTK_SIGNED_CHAR : 'ToSignedChar',
  30. vtkCommonCore.VTK_SHORT : 'ToShort',
  31. vtkCommonCore.VTK_UNSIGNED_SHORT : 'ToUnsignedShort',
  32. vtkCommonCore.VTK_INT : 'ToInt',
  33. vtkCommonCore.VTK_UNSIGNED_INT : 'ToUnsignedInt',
  34. vtkCommonCore.VTK_LONG : 'ToLong',
  35. vtkCommonCore.VTK_UNSIGNED_LONG : 'ToUnsignedLong',
  36. vtkCommonCore.VTK_LONG_LONG : 'ToLongLong',
  37. vtkCommonCore.VTK_UNSIGNED_LONG_LONG : 'ToUnsignedLongLong',
  38. vtkCommonCore.VTK_FLOAT : 'ToFloat',
  39. vtkCommonCore.VTK_DOUBLE : 'ToDouble',
  40. vtkCommonCore.VTK_STRING : 'ToString',
  41. vtkCommonCore.VTK_OBJECT : 'ToVTKObject',
  42. }
  43. _variant_check_map = {
  44. vtkCommonCore.VTK_VOID : 'IsValid',
  45. vtkCommonCore.VTK_CHAR : 'IsChar',
  46. vtkCommonCore.VTK_UNSIGNED_CHAR : 'IsUnsignedChar',
  47. vtkCommonCore.VTK_SIGNED_CHAR : 'IsSignedChar',
  48. vtkCommonCore.VTK_SHORT : 'IsShort',
  49. vtkCommonCore.VTK_UNSIGNED_SHORT : 'IsUnsignedShort',
  50. vtkCommonCore.VTK_INT : 'IsInt',
  51. vtkCommonCore.VTK_UNSIGNED_INT : 'IsUnsignedInt',
  52. vtkCommonCore.VTK_LONG : 'IsLong',
  53. vtkCommonCore.VTK_UNSIGNED_LONG : 'IsUnsignedLong',
  54. vtkCommonCore.VTK_LONG_LONG : 'IsLongLong',
  55. vtkCommonCore.VTK_UNSIGNED_LONG_LONG : 'IsUnsignedLongLong',
  56. vtkCommonCore.VTK_FLOAT : 'IsFloat',
  57. vtkCommonCore.VTK_DOUBLE : 'IsDouble',
  58. vtkCommonCore.VTK_STRING : 'IsString',
  59. vtkCommonCore.VTK_OBJECT : 'IsVTKObject',
  60. }
  61. def vtkVariantCreate(v, t):
  62. """
  63. Create a vtkVariant of the specified type, where the type is in the
  64. following format: 'int', 'unsigned int', etc. for numeric types,
  65. and 'string' for strings. You can also use an
  66. integer VTK type constant for the type.
  67. """
  68. if not issubclass(type(t), int):
  69. t = _variant_type_map[t]
  70. return vtkCommonCore.vtkVariant(v, t)
  71. def vtkVariantExtract(v, t=None):
  72. """
  73. Extract the specified value type from the vtkVariant, where the type is
  74. in the following format: 'int', 'unsigned int', etc. for numeric types,
  75. and 'string' for strings. You can also use an
  76. integer VTK type constant for the type. Set the type to 'None" to
  77. extract the value in its native type.
  78. """
  79. v = vtkCommonCore.vtkVariant(v)
  80. if t == None:
  81. t = v.GetType()
  82. elif not issubclass(type(t), int):
  83. t = _variant_type_map[t]
  84. if getattr(v, _variant_check_map[t])():
  85. return getattr(v, _variant_method_map[t])()
  86. else:
  87. return None
  88. def vtkVariantCast(v, t):
  89. """
  90. Cast the vtkVariant to the specified value type, where the type is
  91. in the following format: 'int', 'unsigned int', etc. for numeric types,
  92. and 'string' for strings. You can also use an
  93. integer VTK type constant for the type.
  94. """
  95. if not issubclass(type(t), int):
  96. t = _variant_type_map[t]
  97. v = vtkCommonCore.vtkVariant(v, t)
  98. if v.IsValid():
  99. return getattr(v, _variant_method_map[t])()
  100. else:
  101. return None
  102. def vtkVariantStrictWeakOrder(s1, s2):
  103. """
  104. Compare variants by type first, and then by value. When called from
  105. within a Python 2 interpreter, the return values are -1, 0, 1 like the
  106. cmp() method, for compatibility with the Python 2 list sort() method.
  107. This is in contrast with the Python 3 version of this method (and the
  108. VTK C++ version), which return true or false.
  109. """
  110. s1 = vtkCommonCore.vtkVariant(s1)
  111. s2 = vtkCommonCore.vtkVariant(s2)
  112. t1 = s1.GetType()
  113. t2 = s2.GetType()
  114. # define a cmp(x, y) for Python 3 that returns (x < y)
  115. def vcmp(x, y):
  116. if sys.hexversion >= 0x03000000:
  117. return (x < y)
  118. else:
  119. return cmp(x,y)
  120. # check based on type
  121. if t1 != t2:
  122. return vcmp(t1,t2)
  123. v1 = s1.IsValid()
  124. v2 = s2.IsValid()
  125. # check based on validity
  126. if (not v1) or (not v2):
  127. return vcmp(v1,v2)
  128. # extract and compare the values
  129. r1 = getattr(s1, _variant_method_map[t1])()
  130. r2 = getattr(s2, _variant_method_map[t2])()
  131. # compare vtk objects by classname, then address
  132. if t1 == vtkCommonCore.VTK_OBJECT:
  133. c1 = r1.GetClassName()
  134. c2 = r2.GetClassName()
  135. if c1 != c2:
  136. return vcmp(c1,c2)
  137. else:
  138. return vcmp(r1.__this__,r2.__this__)
  139. return vcmp(r1, r2)
  140. if sys.hexversion >= 0x03000000:
  141. class vtkVariantStrictWeakOrderKey:
  142. """A key method (class, actually) for use with sort()"""
  143. def __init__(self, obj, *args):
  144. self.obj = obj
  145. def __lt__(self, other):
  146. return vtkVariantStrictWeakOrder(self.obj, other)
  147. else:
  148. class vtkVariantStrictWeakOrderKey:
  149. """A key method (class, actually) for use with sort()"""
  150. def __init__(self, obj, *args):
  151. self.obj = obj
  152. def __lt__(self, other):
  153. return vtkVariantStrictWeakOrder(self.obj, other) < 0
  154. def __gt__(self, other):
  155. return vtkVariantStrictWeakOrder(self.obj, other) > 0
  156. def __eq__(self, other):
  157. return vtkVariantStrictWeakOrder(self.obj, other) == 0
  158. def __le__(self, other):
  159. return vtkVariantStrictWeakOrder(self.obj, other) <= 0
  160. def __ge__(self, other):
  161. return vtkVariantStrictWeakOrder(self.obj, other) >= 0
  162. def __ne__(self, other):
  163. return vtkVariantStrictWeakOrder(self.obj, other) != 0
  164. def vtkVariantStrictEquality(s1, s2):
  165. """
  166. Check two variants for strict equality of type and value.
  167. """
  168. s1 = vtkCommonCore.vtkVariant(s1)
  169. s2 = vtkCommonCore.vtkVariant(s2)
  170. t1 = s1.GetType()
  171. t2 = s2.GetType()
  172. # check based on type
  173. if t1 != t2:
  174. return False
  175. v1 = s1.IsValid()
  176. v2 = s2.IsValid()
  177. # check based on validity
  178. if (not v1) and (not v2):
  179. return True
  180. elif v1 != v2:
  181. return False
  182. # extract and compare the values
  183. r1 = getattr(s1, _variant_method_map[t1])()
  184. r2 = getattr(s2, _variant_method_map[t2])()
  185. return (r1 == r2)
  186. def vtkVariantLessThan(s1, s2):
  187. """
  188. Return true if s1 < s2.
  189. """
  190. return (vtkCommonCore.vtkVariant(s1) < vtkCommonCore.vtkVariant(s2))
  191. def vtkVariantEqual(s1, s2):
  192. """
  193. Return true if s1 == s2.
  194. """
  195. return (vtkCommonCore.vtkVariant(s1) == vtkCommonCore.vtkVariant(s2))