[vtkusers] Simple 2D unstructured grid

Andras Lasso lasso at queensu.ca
Wed Sep 26 11:37:44 EDT 2018


Works well for me in 3D Slicer’s embedded Python 2.7 64 bit, both with latest VTK master and VTK 7.1.0.

Andras

From: vtkusers <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
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/ba06aac2/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 14278 bytes
Desc: image001.png
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180926/ba06aac2/attachment.png>


More information about the vtkusers mailing list