[vtkusers] Re: What's Wrong With This?!?

Goodwin Lawlor goodwin.lawlor at ucd.ie
Wed Mar 22 12:58:05 EST 2006


Hi Andrea,

I've run out of ideas except to compile a debug version of python and 
vtk and step through the script.

I've run Examples\DataManipulation\Tcl\Arrays.tcl and its ok even when I 
change the number of tuples to 200- its seems to be just a python problem.

I can't see anything wrong with your python scripts below... sorry!

Goodwin

Andrea Gavana wrote:
> Hello Goodwin,
>  
>     thank you very much for your suggestions. This is what I have tried:
>  
> 1) Call SetNumberOfComponents() before SetNumberOfTuples() => VTK Crashes
>  
> 2) Take vtkExtractGrid out of the pipeline => VTK Crashes
>  
> 3) Try using vtkPoints::Allocate() and vtkPoints::SetTuple3(): VTK 
> crashes even before showing the 3D window: the problem is here, as you 
> suggested in your point. I am unable to finish the call to SetTuple3 in 
> a loop if the number of points is greater than 100 or something. I can 
> finish the loop if I choose, i.e., 50 points. I attach the example with 
> SetTuple3 as "prova3.py" (using real simple grid data)
>  
> 4) I get the same runtime error from Windows if I simply use SetData() 
> instead of SetTuple3() and Allocate(), if I choose more than 100 points. 
> If I use 50 points, for example, I get the 3D window with a nice cube. I 
> attach another example as " prova4.py" with SetData() instead of 
> SetTuple3() and Allocate().
>  
> Probably I have made some mistake somewhere for the SetTuple3/Allocate 
> example, but I am quite sure about the SetData() example. It is 
> fantastically difficult to find examples of use of VTK in Python on the 
> net...
>  
> For the samples I used Numpy instead of Numeric because Numpy is much 
> more friendly in creating a 3D grid... I get the error message when I 
> change the values for nx, ny, nz to bigger values.
> 
> Does anyone have some suggestion for me? It's really complicated to find 
> out what the problem is when I get only a windows error message... at 
> least a Python/VTK error messages would be much more useful...
>  
> Thank you very much for your help and suggestions.
>  
> Andrea.
>  
>  
> 
>  
> 
> 
> ------------------------------------------------------------------------
> 
> import numpy as N
> import vtk
> import wx
> from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor
> 
> app = wx.App(0)
> 
> # create regular grid
> xmin, ymin, zmin = -N.pi, -N.pi, 0
> xmax, ymax, zmax =  N.pi, N.pi, 2*N.pi
> 
> nx, ny, nz = 5, 5, 2
> dx, dy, dz = (xmax-xmin)/(nx-1), (ymax-ymin)/(ny-1), (zmax-zmin)/(nz-1)
> 
> ZZ, YY, XX = N.mgrid[zmin:zmax+dz:dz, ymin:ymax+dy:dy, xmin:xmax+dx:dx]
> XX = XX.astype(N.Float32)
> YY = YY.astype(N.Float32)
> ZZ = ZZ.astype(N.Float32)
> 
> # set scalar function f(x,y,z)
> RR = N.sqrt(XX**2 + YY**2 + ZZ**2)
> WW = N.cos(2*RR)/(RR + 0.5)
> 
> xyz = N.zeros( (nx*ny*nz,3), N.Float32 )
> 
> xyz[:,0] = XX.flatten()
> xyz[:,1] = YY.flatten()
> xyz[:,2] = ZZ.flatten()
> 
> # 3-Dcoordinates
> vtk_xyz = vtk.vtkFloatArray()
> vtk_xyz.SetNumberOfTuples(nx*ny*nz)
> vtk_xyz.SetNumberOfComponents(3)
> 
> vtk_pts = vtk.vtkPoints()
> vtk_pts.Allocate(nx*ny*nz, nx*ny*nz)
> vtk_pts.SetDataTypeToFloat()
> 
> count = 0
> 
> for ii in xrange(nx):
>     for jj in xrange(ny):
>         for kk in xrange(nz):
>             vtk_xyz.SetTuple3(count, xyz[count, 0], xyz[count, 1], xyz[count, 2])
>             count = count + 1
> 
> vtk_pts.SetData(vtk_xyz)
> 
> # create and fill vtk array
> vtk_zz = vtk.vtkFloatArray()
> vtk_zz.SetNumberOfTuples(nx*ny*nz)
> vtk_zz.SetNumberOfComponents(1)
> vtk_zz.SetVoidArray(WW.flatten(), nx*ny*nz, 1)
> 
> # create vtk data
> grid = vtk.vtkStructuredGrid()
> grid.SetDimensions(nx, ny, nz)
> grid.SetPoints(vtk_pts)
> grid.GetPointData().SetScalars(vtk_zz)
> 
> # Create Actor
> surfaceMapper = vtk.vtkDataSetMapper()
> surfaceMapper.SetInput(grid)
> 
> surfaceActor = vtk.vtkActor()
> surfaceActor.SetMapper(surfaceMapper)
>                 
> # show
> frame = wx.Frame(None, -1, "BlaBla")
> 
> widget = wxVTKRenderWindowInteractor(frame, -1)
> hiren = vtk.vtkRenderer()
> widget.GetRenderWindow().AddRenderer(hiren)
> 
> hiren.AddActor(surfaceActor)
> 
> widget.AddObserver("ExitEvent", lambda o,e,f=frame: f.Close())
> widget.Enable(1)
> 
> sizer = wx.BoxSizer(wx.VERTICAL)
> sizer.Add(widget, 1, wx.EXPAND)
> frame.SetSizer(sizer)
> sizer.Layout()
> 
> frame.SetSize((800, 600))
> frame.Show()
> 
> app.MainLoop()
> 
> 
> 
> ------------------------------------------------------------------------
> 
> import numpy as N
> import vtk
> import wx
> from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor
> 
> app = wx.App(0)
> 
> # create regular grid
> xmin, ymin, zmin = -N.pi, -N.pi, 0
> xmax, ymax, zmax =  N.pi, N.pi, 2*N.pi
> 
> nx, ny, nz = 5, 5, 2
> dx, dy, dz = (xmax-xmin)/(nx-1), (ymax-ymin)/(ny-1), (zmax-zmin)/(nz-1)
> 
> ZZ, YY, XX = N.mgrid[zmin:zmax+dz:dz, ymin:ymax+dy:dy, xmin:xmax+dx:dx]
> XX = XX.astype(N.Float32)
> YY = YY.astype(N.Float32)
> ZZ = ZZ.astype(N.Float32)
> 
> # set scalar function f(x,y,z)
> RR = N.sqrt(XX**2 + YY**2 + ZZ**2)
> WW = N.cos(2*RR)/(RR + 0.5)
> 
> xyz = N.zeros( (nx*ny*nz,3), N.Float32 )
> 
> xyz[:,0] = XX.flatten()
> xyz[:,1] = YY.flatten()
> xyz[:,2] = ZZ.flatten()
> 
> # 3-Dcoordinates
> vtk_xyz = vtk.vtkFloatArray()
> vtk_xyz.SetNumberOfTuples(nx*ny*nz)
> vtk_xyz.SetNumberOfComponents(3)
> vtk_xyz.SetVoidArray(xyz.flatten(), 3*nx*ny*nz, 1)
> 
> vtk_pts = vtk.vtkPoints()
> vtk_pts.Allocate(nx*ny*nz, nx*ny*nz)
> vtk_pts.SetDataTypeToFloat()
> vtk_pts.SetData(vtk_xyz)
> 
> # create and fill vtk array
> vtk_zz = vtk.vtkFloatArray()
> vtk_zz.SetNumberOfTuples(nx*ny*nz)
> vtk_zz.SetNumberOfComponents(1)
> vtk_zz.SetVoidArray(WW.flatten(), nx*ny*nz, 1)
> 
> # create vtk data
> grid = vtk.vtkStructuredGrid()
> grid.SetDimensions(nx, ny, nz)
> grid.SetPoints(vtk_pts)
> grid.GetPointData().SetScalars(vtk_zz)
> 
> # Create Actor
> surfaceMapper = vtk.vtkDataSetMapper()
> surfaceMapper.SetInput(grid)
> 
> surfaceActor = vtk.vtkActor()
> surfaceActor.SetMapper(surfaceMapper)
>                 
> # show
> frame = wx.Frame(None, -1, "BlaBla")
> 
> widget = wxVTKRenderWindowInteractor(frame, -1)
> hiren = vtk.vtkRenderer()
> widget.GetRenderWindow().AddRenderer(hiren)
> 
> hiren.AddActor(surfaceActor)
> 
> widget.AddObserver("ExitEvent", lambda o,e,f=frame: f.Close())
> widget.Enable(1)
> 
> sizer = wx.BoxSizer(wx.VERTICAL)
> sizer.Add(widget, 1, wx.EXPAND)
> frame.SetSizer(sizer)
> sizer.Layout()
> 
> frame.SetSize((800, 600))
> frame.Show()
> 
> app.MainLoop()
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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