[vtkusers] c api and python and vtk - with SWIG

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Wed Jan 28 00:50:45 EST 2004


>>>>> "MA" == Mark Asbach <mark.asbach at post.rwth-aachen.de> writes:

    >> I have a set of C codes that manipulate a data structure, one
    >> component of which is an array of doubles.  I've managed to
    >> wrap the C codes with SWIG so I have access to the structure
    >> with the typical SWIG accessor functions,
    >> i.e. Sp4Array_data_get() What do I have to do to get this data
    >> array into vtk?

    MA> The way I took for my code is to just write the conversion to
    MA> vtk data structures in C, wrap them with SWIG and mix that
    MA> with the vtk python stuff. There is a bug in the current SWIG
    MA> adaptor code of VTK though, and I don't know, if David
    MA> committed my fix to the repository already (David?).

In general and especially when you have Numeric arrays, this is much
easier to do if you use Weave.  I completely forgot about it and never
posted about it, sorry!  My apologies also for the cross-posting.
Here are some details.

  http://www.scipy.org/site_content/weave

Weave is a neat tool that lets you write C++ code straight inside your
Python code.  The first time the Python script is run, the C++ code is
compiled to a module, and stored in a special place (all done
automatically).  The next time the script runs, the script will use
the compiled module.  Weave generates all the standard boiler plate
code to convert the objects, return values etc.  I've not played with
generic SWIG pointers, but it should be possible to use too.

A long while ago I wrote a small component that makes it trivial to
embed VTK C++ code inside a VTK-Python script.  If you use weave from
CVS this is available.  I am not sure if the tarball at the Weave web
site has this part and if it does not please ask on the scipy-dev list
that I've CC'd for clarifications etc.  I've also not tested this
under MSVC.  Only under Linux with g++.  YMMV.

Anyway, here is a trivial example that I used to test with.  I convert
a reasonably large Numeric array to a vtkFloatArray.

# ------------
try:
    from scipy import weave
except ImportError:
    import weave
import vtk
import sys
import Numeric
import time

# change these to suit your needs.
inc_dirs=['/cvs/VTK/','/cvs/VTK/Common']
lib_dirs=['/cvs/VTK/bin']

def simple_test():
    a = vtk.vtkStructuredPoints()

    # Your C++ code
    code="""
    printf("Inline a->ClassName() == %s\n", a->GetClassName());
    printf("Inline a->GetReferenceCount() == %d\n", a->GetReferenceCount());
    """
    # Now execute this.
    weave.inline(code, ['a'], include_dirs=inc_dirs, library_dirs=lib_dirs)
    

def test():
    arr = Numeric.arange(0, 10, 0.0001)
    print "Number of elements in array = ", arr.shape[0]

    v_arr = vtk.vtkFloatArray()
    ts = time.time()
    for i in range(arr.shape[0]):
        v_arr.InsertNextValue(arr[i])
    print "Time taken to do it in pure Python =", time.time() - ts    
    
    v_arr = vtk.vtkFloatArray()

    # Your C++ code
    code = """
    int size = Narr[0];
    for (int i=0; i<size; ++i)
        v_arr->InsertNextValue(arr[i]);
    """
    ts = time.time()
    # Execute it.
    weave.inline(code, ['arr', 'v_arr'], include_dirs=inc_dirs,
                 library_dirs=lib_dirs)
    # Note, if you need special headers for compilation, add them via 
    # an extra keyword arg., headers=["vtkFoo.h"]

    print "Time taken to do it using Weave =", time.time() - ts

    print "Checking data."
    for i in range(v_arr.GetNumberOfTuples()):
        val = (v_arr.GetValue(i) -arr[i] )
        assert (val < 1e-6), "i = %d, val= %f"%(i, val)
    print "OK."

if __name__ == "__main__":    
    simple_test()
    test()

# ------------

The example is certainly trivial, but shows you how it works.  This is
also old code and I am not sure how things have changed recently.  For
more details check the weave docs.  On my machine I get these results:

  Inline a->ClassName() == vtkStructuredPoints
  Inline a->GetReferenceCount() == 2
  Number of elements in array =  100000
  Time taken to do it in pure Python = 0.800973892212
  Time taken to do it using Weave = 0.0328440666199
  Checking data.
  OK.

Which is an ~25 fold speed increase.

Its been a while since I used/wrote this so I'm most probably not the
right person to ask questions.  I just wanted to let you folks know of
this option.  You might be better served asking questions on
scipy-dev.

cheers,
prabhu



More information about the vtkusers mailing list