Commit 0464fbf6 authored by David Gobbi's avatar David Gobbi Committed by Marcus D. Hanwell
Browse files

ENH: vtkVariantStrictEquality(), vtkVariantStrictWeakOrder() for python

parent 59db98f7
......@@ -23,46 +23,50 @@ _variant_type_map = {
'double' : vtkConstants.VTK_DOUBLE,
'string' : vtkConstants.VTK_STRING,
'unicode string' : vtkConstants.VTK_UNICODE_STRING,
'vtkObjectBase' : vtkConstants.VTK_OBJECT,
'vtkObject' : vtkConstants.VTK_OBJECT,
}
_variant_method_map = {
'char' : 'ToChar',
'unsigned char' : 'ToUnsignedChar',
'signed char' : 'ToSignedChar',
'short' : 'ToShort',
'unsigned short' : 'ToUnsignedShort',
'int' : 'ToInt',
'unsigned int' : 'ToUnsignedInt',
'long' : 'ToLong',
'unsigned long' : 'ToUnsignedLong',
'long long' : 'ToLongLong',
'unsigned long long' : 'ToUnsignedLongLong',
'__int64' : 'To__Int64',
'unsigned __int64' : 'ToUnsigned__Int64',
'float' : 'ToFloat',
'double' : 'ToDouble',
'string' : 'ToString',
'unicode string' : 'ToUnicodeString',
vtk.VTK_CHAR : 'ToChar',
vtk.VTK_UNSIGNED_CHAR : 'ToUnsignedChar',
vtk.VTK_SIGNED_CHAR : 'ToSignedChar',
vtk.VTK_SHORT : 'ToShort',
vtk.VTK_UNSIGNED_SHORT : 'ToUnsignedShort',
vtk.VTK_INT : 'ToInt',
vtk.VTK_UNSIGNED_INT : 'ToUnsignedInt',
vtk.VTK_LONG : 'ToLong',
vtk.VTK_UNSIGNED_LONG : 'ToUnsignedLong',
vtk.VTK_LONG_LONG : 'ToLongLong',
vtk.VTK_UNSIGNED_LONG_LONG : 'ToUnsignedLongLong',
vtk.VTK___INT64 : 'To__Int64',
vtk.VTK_UNSIGNED___INT64 : 'ToUnsigned__Int64',
vtk.VTK_FLOAT : 'ToFloat',
vtk.VTK_DOUBLE : 'ToDouble',
vtk.VTK_STRING : 'ToString',
vtk.VTK_UNICODE_STRING : 'ToUnicodeString',
vtk.VTK_OBJECT : 'ToVTKObject',
}
_variant_check_map = {
'char' : 'IsChar',
'unsigned char' : 'IsUnsignedChar',
'signed char' : 'IsSignedChar',
'short' : 'IsShort',
'unsigned short' : 'IsUnsignedShort',
'int' : 'IsInt',
'unsigned int' : 'IsUnsignedInt',
'long' : 'IsLong',
'unsigned long' : 'IsUnsignedLong',
'long long' : 'IsLongLong',
'unsigned long long' : 'IsUnsignedLongLong',
'__int64' : 'Is__Int64',
'unsigned __int64' : 'IsUnsigned__Int64',
'float' : 'IsFloat',
'double' : 'IsDouble',
'string' : 'IsString',
'unicode string' : 'IsUnicodeString',
vtk.VTK_CHAR : 'IsChar',
vtk.VTK_UNSIGNED_CHAR : 'IsUnsignedChar',
vtk.VTK_SIGNED_CHAR : 'IsSignedChar',
vtk.VTK_SHORT : 'IsShort',
vtk.VTK_UNSIGNED_SHORT : 'IsUnsignedShort',
vtk.VTK_INT : 'IsInt',
vtk.VTK_UNSIGNED_INT : 'IsUnsignedInt',
vtk.VTK_LONG : 'IsLong',
vtk.VTK_UNSIGNED_LONG : 'IsUnsignedLong',
vtk.VTK_LONG_LONG : 'IsLongLong',
vtk.VTK_UNSIGNED_LONG_LONG : 'IsUnsignedLongLong',
vtk.VTK___INT64 : 'Is__Int64',
vtk.VTK_UNSIGNED___INT64 : 'IsUnsigned__Int64',
vtk.VTK_FLOAT : 'IsFloat',
vtk.VTK_DOUBLE : 'IsDouble',
vtk.VTK_STRING : 'IsString',
vtk.VTK_UNICODE_STRING : 'IsUnicodeString',
vtk.VTK_OBJECT : 'IsVTKObject',
}
......@@ -70,45 +74,122 @@ def vtkVariantCreate(v, t):
"""
Create a vtkVariant of the specified type, where the type is in the
following format: 'int', 'unsigned int', etc. for numeric types,
'string', and 'unicode string'.
and 'string' or 'unicode string' for strings. You can also use an
integer VTK type constant for the type.
"""
try:
if not issubclass(type(t), int):
i = _variant_type_map[t]
except KeyError:
raise ValueError, "second parameter must be 'int', 'string', etc."
return vtk.vtkVariant(v, i)
def vtkVariantExtract(v, t=None):
"""
Extract the specified value type from the vtkVariant, where the type is
int the following format: 'int', 'unsigned int', etc. for numerics,
'string', and 'unicode string'.
in the following format: 'int', 'unsigned int', etc. for numeric types,
and 'string' or 'unicode string' for strings. You can also use an
integer VTK type constant for the type. Set the type to 'None" to
extract the value in its native type.
"""
if t == None:
if v.IsVTKObject():
raise ValueError, "use ToVTKObject() instead"
t = v.GetTypeAsString()
try:
i = _variant_type_map[t]
except KeyError:
raise ValueError, "second parameter must be 'int', 'string', etc."
t = v.GetType()
elif not issubclass(type(t), int):
t = _variant_type_map[t]
if getattr(v, _variant_check_map[t])():
return getattr(v, _variant_method_map[t])()
else:
return None
def vtkVariantCast(v, t):
"""
Cast the vtkVariant to the specified value type, where the type is
int the following format: 'int', 'unsigned int', etc. for numerics,
'string', and 'unicode string'.
in the following format: 'int', 'unsigned int', etc. for numeric types,
and 'string' or 'unicode string' for strings. You can also use an
integer VTK type constant for the type.
"""
try:
i = _variant_type_map[t]
except KeyError:
raise ValueError, "second parameter must be 'int', 'string', etc."
u = vtk.vtkVariant(v, i)
if u.IsValid():
return getattr(u, _variant_method_map[t])()
if not issubclass(type(t), int):
t = _variant_type_map[t]
v = vtk.vtkVariant(v, i)
if v.IsValid():
return getattr(v, _variant_method_map[t])()
else:
return None
def vtkVariantStrictWeakOrder(s1, s2):
"""
Compare variants by type first, and then by value. The return values
are -1, 0, 1 like the python cmp() method, for compatibility with the
python list sort() method. This is in contrast with the C++ version,
which returns true or false.
"""
t1 = s1.GetType()
t2 = s2.GetType()
# check based on type
if t1 != t2:
return cmp(t1,t2)
v1 = s1.IsValid()
v2 = s2.IsValid()
# check based on validity
if (not v1) and (not v2):
return 0
elif v1 != v2:
return cmp(v1,v2)
# extract and compare the values
r1 = getattr(s1, _variant_method_map[t1])()
r2 = getattr(s2, _variant_method_map[t2])()
# compare vtk objects by classname
if t1 == vtk.VTK_OBJECT:
return cmp(r1.GetClassName(), r2.GetClassName())
return cmp(r1, r2)
def vtkVariantStrictEquality(s1, s2):
"""
Check two variants for strict equality of type and value.
"""
t1 = s1.GetType()
t2 = s2.GetType()
# check based on type
if t1 != t2:
return False
v1 = s1.IsValid()
v2 = s2.IsValid()
# check based on validity
if (not v1) and (not v2):
return True
elif v1 != v2:
return False
# extract and compare the values
r1 = getattr(s1, _variant_method_map[t1])()
r2 = getattr(s2, _variant_method_map[t2])()
return (r1 == r2)
def vtkVariantLessThan(s1, s2):
"""
Return true if s1 < s2. This isn't very useful in Python.
"""
return (s1 < s2)
def vtkVariantEqual(s1, s2):
"""
Return true if s1 == s2. This isn't very useful in Python.
"""
return (s1 == s2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment