[vtkusers] strange python numpy problem

Bryn Lloyd blloyd at student.ethz.ch
Tue Oct 16 07:52:07 EDT 2007


Hi Eugen,

I think the mistake you made is here:

a=numpy.array([[0.1],[0.2],[0.3],[0.4],[0.5],[0.6],[0.7],[0.8]],dtype=numpy.float32);
print content of array
...

...
a = vtk.vtkActor();
a.SetMapper(mapper);


Both variables have the same name. I tried it out - calling the actor
'act' instead of 'a' did the job.
If you move the "print content of array" down below the "a =
vtk.vtkActor()" line, you will not get correct output.


Cheers
Bryn






Eugen Wintersberger wrote:
> Hi there
>  I've just started with VTK and want to use it together with python for
> writting scripts for data visualization. In python numpy arrays are my 
> most favorable structures for holding numerical data, therefore, I would
> like copy data directly from a numpy array to a vtkFloatArray for
> instance without using loops.
> You will find two scripts attached to this mail:
> test1.py which fills a vtkFloatArray from a numpy array by means of a   
>          loop
> test2.py which fills a vtkFloatArray using the SetVoidArray method
>          of the vtkFloatArray class.
> After filling in the data I print the data stored in the vtkFloatArray
> and get the following output:
> 
> ~/src/test/python/vtk $ python test1.py                                                                                                                      
> 0.10000000149                                                                                                                                                
> 0.20000000298                                                                                                                                                
> 0.300000011921                                                                                                                                               
> 0.40000000596                                                                                                                                                
> 0.5                                                                                                                                                          
> 0.600000023842                                                                                                                                               
> 0.699999988079                                                                                                                                               
> 0.800000011921   
> 
> and  
> 
> ~/src/test/python/vtk $ python test2.py                                                                                                                      
> 0.10000000149                                                                                                                                                
> 0.20000000298                                                                                                                                                
> 0.300000011921                                                                                                                                               
> 0.40000000596                                                                                                                                                
> 0.5                                                                                                                                                          
> 0.600000023842                                                                                                                                               
> 0.699999988079                                                                                                                                               
> 0.800000011921      
> 
> So it seems that both procedures load the data correctly into the 
> vtkFloatArray. However, the resulting 3D image looks much more different
> as you can see from the two attached screenshots of test1.jpg and
> test2.jpg (the screenshots belong to the output of the scripts test1.py
> and test2.py respectively). 
> 
> I user VTK 5.0.2 on an Ubuntu AMD64 (native 64Bit) installation. 
> 
> Has anyone an idea what I am doing wrong or if there is a problem in the
> VTK/python interface? If this numpy arrays are not supported it would be
> an interesting feature to add to VTKs python interface.
> 
> best regrads  
>   Eugen Wintersberger
> 
>  
> 
> 
> 
> ------------------------------------------------------------------------
> 
> #!/bin/env python
> 
> import vtk
> import numpy
> 
> #create a vtkDataArray holding the data values on the positions
> a = numpy.array([[0.1],[0.2],[0.3],[0.4],[0.5],[0.6],[0.7],[0.8]],dtype=numpy.float32);
> data = vtk.vtkFloatArray();
> data.SetNumberOfComponents(1);
> data.SetNumberOfTuples(8);
> #data.SetVoidArray(a[:,:],a.shape[0],1);
> for i in range(a.shape[0]):
> 	data.InsertTuple1(i,a[i,0]);
> for i in range(a.shape[0]):
> 	print data.GetTuple1(i);
> 
> 
> #create the polygonal data field describing a cube
> points = vtk.vtkPoints();
> points.InsertPoint(0,-0.5,0.5,-0.5);
> points.InsertPoint(1, 0.5,0.5,-0.5);
> points.InsertPoint(2,-0.5,0.5, 0.5);
> points.InsertPoint(3, 0.5,0.5, 0.5);
> points.InsertPoint(4,-0.5,-0.5,-0.5);
> points.InsertPoint(5, 0.5,-0.5,-0.5);
> points.InsertPoint(6,-0.5,-0.5, 0.5);
> points.InsertPoint(7, 0.5,-0.5, 0.5);
> 
> pdata = vtk.vtkPolyData();
> pdata.SetPoints(points);
> pdata.GetPointData().SetScalars(data);
> 
> 
> #create the sphere which should be used to anotate the
> #corners of the cube
> sp = vtk.vtkSphereSource();
> sp.SetPhiResolution(50);
> sp.SetThetaResolution(50);
> 	
> #need representation of the data with Glyphs
> glyph = vtk.vtkGlyph3D();
> glyph.SetInput(pdata);
> glyph.SetSourceConnection(sp.GetOutputPort());
> 
> mapper = vtk.vtkPolyDataMapper();
> mapper.SetInputConnection(glyph.GetOutputPort());
> 
> #start here with rendering
> a = vtk.vtkActor();
> a.SetMapper(mapper);
> 
> #add an actor with a coordinate frame
> tprop = vtk.vtkTextProperty();
> tprop.SetColor(0,0,0);
> tprop.ShadowOn();
> 
> ren = vtk.vtkRenderer();
> ren.SetBackground(0.0,0.0,0.0);
> ren.AddActor(a);
> 
> cube = vtk.vtkCubeAxesActor2D();
> cube.SetInput(pdata);
> cube.SetCamera(ren.GetActiveCamera());
> cube.SetFlyModeToOuterEdges();
> cube.SetNumberOfLabels(5);
> ren.AddActor(cube);
> 
> renwin = vtk.vtkRenderWindow();
> renwin.AddRenderer(ren);
> renwin.SetSize(300,300);
> 
> renwinia = vtk.vtkRenderWindowInteractor()
> renwinia.SetRenderWindow(renwin);
> 
> renwinia.Initialize();
> renwin.Render();
> renwinia.Start();
> 	
> 
> 	
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> #!/bin/env python
> 
> import vtk
> import numpy
> 
> #create a vtkDataArray holding the data values on the positions
> a = numpy.array([[0.1],[0.2],[0.3],[0.4],[0.5],[0.6],[0.7],[0.8]],dtype=numpy.float32);
> data = vtk.vtkFloatArray();
> data.SetNumberOfComponents(1);
> data.SetNumberOfTuples(8);
> data.SetVoidArray(a[:,:],a.shape[0],1);
> #for i in range(a.shape[0]):
> #	data.InsertTuple1(i,a[i,0]);
> for i in range(a.shape[0]):
> 	print data.GetTuple1(i);
> 
> 
> #create the polygonal data field describing a cube
> points = vtk.vtkPoints();
> points.InsertPoint(0,-0.5,0.5,-0.5);
> points.InsertPoint(1, 0.5,0.5,-0.5);
> points.InsertPoint(2,-0.5,0.5, 0.5);
> points.InsertPoint(3, 0.5,0.5, 0.5);
> points.InsertPoint(4,-0.5,-0.5,-0.5);
> points.InsertPoint(5, 0.5,-0.5,-0.5);
> points.InsertPoint(6,-0.5,-0.5, 0.5);
> points.InsertPoint(7, 0.5,-0.5, 0.5);
> 
> pdata = vtk.vtkPolyData();
> pdata.SetPoints(points);
> pdata.GetPointData().SetScalars(data);
> 
> 
> #create the sphere which should be used to anotate the
> #corners of the cube
> sp = vtk.vtkSphereSource();
> sp.SetPhiResolution(50);
> sp.SetThetaResolution(50);
> 	
> #need representation of the data with Glyphs
> glyph = vtk.vtkGlyph3D();
> glyph.SetInput(pdata);
> glyph.SetSourceConnection(sp.GetOutputPort());
> 
> mapper = vtk.vtkPolyDataMapper();
> mapper.SetInputConnection(glyph.GetOutputPort());
> 
> #start here with rendering
> a = vtk.vtkActor();
> a.SetMapper(mapper);
> 
> #add an actor with a coordinate frame
> tprop = vtk.vtkTextProperty();
> tprop.SetColor(0,0,0);
> tprop.ShadowOn();
> 
> ren = vtk.vtkRenderer();
> ren.SetBackground(0.0,0.0,0.0);
> ren.AddActor(a);
> 
> cube = vtk.vtkCubeAxesActor2D();
> cube.SetInput(pdata);
> cube.SetCamera(ren.GetActiveCamera());
> cube.SetFlyModeToOuterEdges();
> cube.SetNumberOfLabels(5);
> ren.AddActor(cube);
> 
> renwin = vtk.vtkRenderWindow();
> renwin.AddRenderer(ren);
> renwin.SetSize(300,300);
> 
> renwinia = vtk.vtkRenderWindowInteractor()
> renwinia.SetRenderWindow(renwin);
> 
> renwinia.Initialize();
> renwin.Render();
> renwinia.Start();
> 	
> 
> 	
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list