[vtkusers] passing pointers and SWIG

W.Burakiewicz at cwi.nl W.Burakiewicz at cwi.nl
Sat Sep 10 10:54:55 EDT 2005


Hello,
I am writing an application using VTK 4.4.2 with python interface.
I would like to perform some operations on array data (e.g. find Frenet
frames for point trajectories)in C.
I am trying to use SWIG to wrap my C functions. However I encountered two
problems while passing array pointers returned
by VTK from python to C.

here is some example code:

-- test.py --
from VTK import *
import arrop
n = 10

a = VTKFloatArray()
a.SetNumberOfComponents(1)
a.SetNumberOfTuples(n)
a_ptr = a.GetVoidPointer(0)
print a_ptr

avg = arrop.float_arravg(a_ptr,n)

-- arrop.i --
%module arrop
%{
float float_arravg(void* arr_vp, int n);
%}
float float_arravg(void* arr_vp, int n);

-- arrop.c --
float float_arravg(void* arr_vp, int n){
    float *arr = (float*)arr_vp;
    float sum = 0.0;
    int i;
    fprintf(stderr,"float_arravg: arr_vp=%p\n",arr_vp);
    for(i=0; i<n; i++)
       sum+=arr[i];
    return sum/(float)n;
}

1. While using SWIG 1.3.24 my python code does not work. I got a message
like:
"C/C++ pointer expected, found 'str(_08165660_void_p)' instead"
That is because after version 1.3.22 SWIG represents pointers in python as
objects not as strings.

Question 1: How to pass VTK pointer to SWIG wrapped function with new SWIG?

2. While using SWIG 1.3.22 I get a Segmentation Fault. There is a problem
with endianess of VTK and SWIG:
the output is:

_08165660_void_p                 <---- this a string representation of a
pointer in python
float_arravg: arr_vp=0x60561608  <---- this is the same pointer printed
from C
Segmentation fault

The pointers have their bytes swapped. But why? I have read about some VTK
fix for SWIG and endianiness but I don't know how to find it.
(I am using little endian machine.)
Question 2: Why does VTK or SWIG swap bytes in pointer representation? How
can I prevent this behaviour?

Thank you for your help,
Wojtek








More information about the vtkusers mailing list