[Fwd: Re: [vtkusers] GetVoidPointer() in Python...]

Tuhin Sinha tk.sinha at vanderbilt.edu
Thu Aug 19 16:04:37 EDT 2004


I meant to reply to the list, but managed to reply only to Prabhu on
this... my apologies...
Prabhu,

  I looked into those classes and found that the key piece that is
missing is that vtkDataArray doesn't allow you to specify the void
pointer to the array you want to export to.  This problem is trivial
with vtkImageData when you have vtkImageExport provide you a method to
set the output void pointer (i.e. via
vtkImageExport::SetExportVoidPointer(void*) or
vtkImageExport::Export(void*)).  But I feel like this should also be a
capability in vtkDataArray and not exclusive to vtkImageExport. 
Something like:

void vtkXXXArray::ExportToVoidPointer(void * out_ptr)
{
  if(out_ptr && this->Array) {
    memcpy(out_ptr,
      this->Array,
      this->Size*sizeof(this->Array[0]));
  }
}

In the meantime, I tried the following in Python:

  npts = poly_data.GetNumberOfPoints()

  n_array = Numeric.array((npts*3,Numeric.Float32)

  in_floatarray = poly_data.GetPoints().GetData()

  sizeof_float = 4

  out_floatarray = vtkFloatArray()
  out_floatarray.SetVoidArray(n_array,npts*sizeof_float*3,1)
  out_floatarray.DeepCopy(in_floatarray)

Only to have my plans foiled by the following lines in
vtkFloatArray::DeepCopy():

  this->SaveUserArray = 0;
  this->Array = new float[this->Size];

I guess for my thought process to work, one would have to hack all of
the vtkXXXArray::DeepCopy as follows:

  if(this->SaveUserArray == 1 
     && this->Size > in->GetSize()) {
    memcpy(this->Array,
      (X*)in->GetVoidPointer(0),
      in->GetSize()*sizeof(this->Array[0]));
  } 
  else {
    delete [] this->Array;
    this->SaveUserArray = 0;
    this->Size = in->GetSize()
    this->Array = new X[this->Size];
    memcpy(this->Array,
      (X*)in->GetVoidPointer(0),
      this->Size*sizeof(this->Array[0]));
  }

I think either of these would work as a general solution for getting any
data array in VTK out to external data containers, but I don't know if
this is something the maintainers want or maybe I'm missing some other
critical piece of information.  As it stands I can't see how to do what
I want to do without a slooooow 'for' loop in Python.   I guess I could
pad my arrays and put them in into a vtkImageData object and pump them
out using vtkImageExport... but that seems silly.  If you or anyone has
more pointers for me, please let me know.

Thanks,

Tuhin

On Thu, 2004-08-19 at 13:28, Prabhu Ramachandran wrote: 
> >>>>> "TS" == Tuhin Sinha <tk.sinha at vanderbilt.edu> writes:
> 
>     TS> Hey all,
>     TS>   What's the point of vtkDataArray::GetVoidPointer() in
>     TS>   Python?  How can
>     TS> I use the returned pointer address inside Python to get to the
>     TS> data.  In a similar vein, why doesn't vtkDataArray have
>     TS> something like ExportToVoidPointer(void*).  This would allow
>     TS> easy exporting of vtkDataArrays to Python.  FYI, my end goal
>     TS> is to copy a substantial amount of data from a vtkFloatArray
>     TS> over to a python array without going through a for loop.  Any
>     TS> help is appreciated.
> 
> Look at Wrapping/Python/vtk/util/vtkImage{ImportFrom,ExportTo}Array.py
> 
> cheers,
> prabhu




More information about the vtkusers mailing list