[vtkusers] Simple 2D unstructured grid

Andras Lasso lasso at queensu.ca
Wed Sep 26 11:59:58 EDT 2018


Your script works well in Paraview’s embedded Python interpreter, too.

It seems that something is wrong in your Python/VTK environment. To confirm, you can check if other VTK Python examples<https://lorensen.github.io/VTKExamples/site/> crash, too.

Andras

From: Andrea Gavana <andrea.gavana at gmail.com>
Sent: Wednesday, September 26, 2018 11:53 AM
To: Andras Lasso <lasso at queensu.ca>
Cc: vtkusers at public.kitware.com
Subject: Re: [vtkusers] Simple 2D unstructured grid

Hi Andras,
On Wed, 26 Sep 2018 at 17:37, Andras Lasso <lasso at queensu.ca<mailto:lasso at queensu.ca>> wrote:
Works well for me in 3D Slicer’s embedded Python 2.7 64 bit, both with latest VTK master and VTK 7.1.0.


Thank you for trying it out. I have upgraded VTK to 7.1.1 from Gohlke's site but the program still crashes Python hard. Does anyone know what might be going on?

Andrea.



Andras

From: vtkusers <vtkusers-bounces at public.kitware.com<mailto:vtkusers-bounces at public.kitware.com>> On Behalf Of Andrea Gavana
Sent: Wednesday, September 26, 2018 10:23 AM
To: vtkusers at public.kitware.com<mailto:vtkusers at public.kitware.com>
Subject: [vtkusers] Simple 2D unstructured grid

Hello list,

    I m trying to create a very, very simple 2D unstructured grid but every time I run my script - no matter what modifications I do - Python crashes hard with a Windows message "Python has stopped working...".

A simplified version of my grid is in the picture below (done in Excel, sorry...):

[image.png]

I attach a copy of my script below. Could anyone offer some suggestions on what I am doing wrong? I am using VTK 7.1.0 on Windows 7 64 bit, Python 2.7 64 bit - VTK comes from Christophe Gohlke very useful Python binary wheels.

Thank you in advance for your help.

Andrea.


# -- BEGIN CODE -- #

import numpy
import vtk

import vtk.util.numpy_support as numpy_support

# X, Y coordinates of a simple grid - Z is zero

X = [600.4957421, 600.5, 600.5, 600.4957421, 600.4913048, 600.5,
     600.5, 600.4957421, 600.4913048, 600.4868674, 600.5, 600.5,
     600.4913048, 600.4868674, 600.4824301, 600.5, 600.5, 600.4868674,
     600.4824301, 600.4779928, 600.5, 600.5, 600.4824301, 600.4779928,
     600.4735554, 600.5, 600.5, 600.4779928, 600.4735554]

Y = [2940.5, 2940.5, 2940.404044, 2940.5, 2940.6, 2940.6, 2940.5,
     2940.5, 2940.6, 2940.7, 2940.7, 2940.6, 2940.6, 2940.7, 2940.8,
     2940.8, 2940.7, 2940.7, 2940.8, 2940.9, 2940.9, 2940.8, 2940.8,
     2940.9, 2941, 2941, 2940.9, 2940.9, 2941]

# First face is triangular, everything else is a 4-sided polygon
IDS = [range(4)]
IDS += numpy.split(numpy.arange(4, len(X)), 5)

npoints = len(X)
matrix = numpy.zeros((npoints, 3), numpy.float32)

matrix[:, 0] = X
matrix[:, 1] = Y

# Create the grid points
vtk_pts = vtk.vtkPoints()
vtk_pts.SetData(numpy_support.numpy_to_vtk(matrix, deep=1))

# Create the unstructured grid
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(vtk_pts)

# Allocate space for the cells in the grid
nc = len(IDS)
grid.Allocate(nc)

# Loop through all cells
for i in xrange(nc):
    cell_ids = IDS[i]
    ncoords = len(cell_ids)
    grid.InsertNextCell(vtk.VTK_POLYGON, ncoords, cell_ids)

print grid.GetNumberOfCells(), nc

mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(grid)

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Add the actors to the renderer, set the background and size
ren.AddActor(actor)
ren.SetBackground(0, 0, 0)

# This allows the interactor to initalize itself. It has to be
# called before an event loop.
iren.Initialize()

ren.ResetCamera()
renWin.Render()

# Start the event loop.
iren.Start()


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180926/a00b89ab/attachment.html>


More information about the vtkusers mailing list