michi1510 / vtkInterface

Python interface for VTK

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vtkInterface Overview

vtkInterface is a VTK helper module that takes a different approach on interfacing with VTK through numpy and direct array access. This module simplifies mesh creation and plotting by adding functionality to existing VTK objects.

This module can be used for scientific plotting for presentations and research papers as well as a supporting module for other mesh dependent Python modules.

Documentation

Refer to the main documentation page for detailed installation and usage details.

Installation

If you have a working copy of VTK 7.0 or newer, installation is simply:

pip install vtkInterface

You can also visit PyPi or GitHub to download the source.

See the Installation for more details.

Quick Examples

Loading and Plotting a Mesh from File

Loading a mesh is trivial

import vtkInterface
mesh = vtkInterface.PolyData('airplane.ply')
mesh.Plot(color='orange')

image

In fact, the code to generate the previous screenshot was created with:

mesh.Plot(screenshot='airplane.png', color='orange')

The points and faces from the mesh are directly accessible as a numpy array:

>>> print(mesh.points)
[[ 896.99401855   48.76010132   82.26560211]
 [ 906.59301758   48.76010132   80.74520111]
 [ 907.53900146   55.49020004   83.65809631]
 ..., 
 [ 806.66497803  627.36297607    5.11482   ]
 [ 806.66497803  654.43200684    7.51997995]
 [ 806.66497803  681.5369873     9.48744011]]
>>> print(mesh.GetNumpyFaces())
[[   0    1    2]
 [   0    2    3]
 [   4    5    1]
 ..., 
 [1324 1333 1323]
 [1325 1216 1334]
 [1325 1334 1324]]

Creating a Structured Surface

This example creates a simple surface grid and plots the resulting grid and its curvature:

import vtkInterface

# Make data
import numpy as np

x = np.arange(-10, 10, 0.25)
y = np.arange(-10, 10, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

# create and plot structured grid
grid = vtkInterface.StructuredGrid(x, y, z)
grid.Plot()  # basic plot

# Plot mean curvature
grid.PlotCurvature()

image

Generating a structured grid is a one liner in this module, and the points from the resulting surface are also a numpy array:

>>> grid.points
[[-10.         -10.           0.99998766]
 [ -9.75       -10.           0.98546793]
 [ -9.5        -10.           0.9413954 ]
 ..., 
 [  9.25         9.75         0.76645876]
 [  9.5          9.75         0.86571785]
 [  9.75         9.75         0.93985707]]

Creating a GIF Movie

This example shows the versatility of the plotting object by generating a moving gif:

import vtkInterface
import numpy as np

x = np.arange(-10, 10, 0.25)
y = np.arange(-10, 10, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

# Create and structured surface
grid = vtkInterface.StructuredGrid(x, y, z)

# Make copy of points
pts = grid.points.copy()

# Start a plotter object and set the scalars to the Z height
plobj = vtkInterface.PlotClass()
plobj.AddMesh(grid, scalars=z.ravel())
plobj.Plot(autoclose=False)

# Open a gif
plobj.OpenGif('wave.gif')

# Update Z and write a frame for each updated position
nframe = 15
for phase in np.linspace(0, 2*np.pi, nframe + 1)[:nframe]:
    z = np.sin(r + phase)
    pts[:, -1] = z.ravel()
    plobj.UpdateCoordinates(pts)
    plobj.UpdateScalars(z.ravel())

    plobj.WriteFrame()

# Close movie and delete object
plobj.Close()

image

About

Python interface for VTK

License:MIT License


Languages

Language:Python 100.0%