[vtkusers] vectors array...

fred fredantispam at free.fr
Mon May 22 13:36:35 EDT 2006


Hi all,

I use a modified imv's mayavi to display 3D data.
I also want to display vectors instead of scalars.
So I have to modify it again.

If I understand a little bit what array2vtk() does :

def array2vtk(z):
    """Converts a Numeric Array to a VTK array object directly. The
    resulting array copies the data in the passed Numeric array.  The
    array can therefore be deleted safely.  This works for real arrays.

    XXX what should be done for complex arrays?
    """

    arr_vtk = {'c':vtkConstants.VTK_UNSIGNED_CHAR,
               'b':vtkConstants.VTK_UNSIGNED_CHAR,
               '1':vtkConstants.VTK_CHAR,
               's':vtkConstants.VTK_SHORT,
               'i':vtkConstants.VTK_INT,
               'l':vtkConstants.VTK_LONG,
               'f':vtkConstants.VTK_FLOAT,
               'd':vtkConstants.VTK_DOUBLE,
               'F':vtkConstants.VTK_FLOAT,
               'D':vtkConstants.VTK_DOUBLE}
    # A dummy array used to create others.
    f = vtk.vtkFloatArray()
    # First create an array of the right type by using the typecode.
    tmp = f.CreateDataArray(arr_vtk[z.typecode()])
    tmp.SetReferenceCount(2) # Prevents memory leak.
    zf = Numeric.ravel(z)
    tmp.SetNumberOfTuples(len(zf))
    tmp.SetNumberOfComponents(1)
    tmp.SetVoidArray(zf, len(zf), 1)
    # Now create a new array that is a DeepCopy of tmp.  This is
    # required because tmp does not copy the data from the NumPy array
    # and will point to garbage if the NumPy array is deleted.
    arr = f.CreateDataArray(arr_vtk[z.typecode()])
    arr.SetReferenceCount(2) # Prevents memory leak.
    arr.DeepCopy(tmp)
    return arr

I can't get writting my array2vtk_vect() function :

def array2vtk_vect(x, y, z):
     arr_vtk = {'c':vtkConstants.VTK_UNSIGNED_CHAR,
               'b':vtkConstants.VTK_UNSIGNED_CHAR,
               '1':vtkConstants.VTK_CHAR,
               's':vtkConstants.VTK_SHORT,
               'i':vtkConstants.VTK_INT,
               'l':vtkConstants.VTK_LONG,
               'f':vtkConstants.VTK_FLOAT,
               'd':vtkConstants.VTK_DOUBLE,
               'F':vtkConstants.VTK_FLOAT,
               'D':vtkConstants.VTK_DOUBLE}
    f = vtk.vtkFloatArray()
    tmp = f.CreateDataArray(arr_vtk[z.typecode()])
    tmp.SetReferenceCount(2) # Prevents memory leak.
    xf = Numeric.ravel(x)
    yf = Numeric.ravel(y)
    zf = Numeric.ravel(z)
    tmp.SetNumberOfTuples(len(zf))    # len(xf) = len(yf) = len(zf)
    tmp.SetNumberOfComponents(3)   # 3 for vectors
How can I use SetVoidArray() to do the job ?

I looked for in VTK User's Guide book but found nothing about SetVoidArray
or whatsoever.


Cheers,

-- 
Dr. Fred.



More information about the vtkusers mailing list