ParaView/Simple ParaView 3 Python Filters

From KitwarePublic
Jump to navigationJump to search

Examples of Filters Programmed using the Python Programmable Filter

It would be nice, if you have written a possibly useful pp-filter, if you would add the code to this page. Here are some simple examples.

Tetrahedra Volume (Filter)

<source lang="python">

  1. This filter computes the volume of the tetrahedra in an unstructured mesh:

pdi = self.GetInput() pdo = self.GetOutput() newData = vtk.vtkDoubleArray() newData.SetName("Volume") numTets = pdi.GetNumberOfCells() for i in range(0, numTets):

      cell = pdi.GetCell(i)
      p1 = pdi.GetPoint(cell.GetPointId(0))
      p2 = pdi.GetPoint(cell.GetPointId(1))
      p3 = pdi.GetPoint(cell.GetPointId(2))
      p4 = pdi.GetPoint(cell.GetPointId(3))
      volume = vtk.vtkTetra.ComputeVolume(p1,p2,p3,p4)
      newData.InsertNextValue(volume)

pdo.GetCellData().AddArray(newData) </source>

Tetrahedra Radius (Filter)

<source lang="python">

# This filter computes the radius h of the tetrahedra in an unstructured mesh:
# Adapted by Johan Jansson (jjan@csc.kth.se) 
from math import *
pdi = self.GetInput()
pdo = self.GetOutput()
newData = vtk.vtkDoubleArray()
newData.SetName("h")
numTets = pdi.GetNumberOfCells()
for i in range(0, numTets):
    cell = pdi.GetCell(i)
    p1 = pdi.GetPoint(cell.GetPointId(0))
    p2 = pdi.GetPoint(cell.GetPointId(1))
    p3 = pdi.GetPoint(cell.GetPointId(2))
    p4 = pdi.GetPoint(cell.GetPointId(3))
    c = [0.0, 0.0, 0.0]
    h = vtk.vtkTetra.Circumsphere(p1,p2,p3,p4,c)
    # VTK actually computes the square
    h = sqrt(h)
    newData.InsertNextValue(h)
pdo.GetCellData().AddArray(newData)

</source>

Flip Tetrahedra (Filter)

<source lang="python">

# This filter flips the tetrahedra (useful, if you have different convention of 
# tet orientation than VTK, and wish to use the vtkMeshQuality filter).
pdi = self.GetInput()
pdo = self.GetOutput()
numTets = pdi.GetNumberOfCells()
newcells = vtk.vtkCellArray()
for i in range(0, numTets):
       cell = pdi.GetCell(i)
       i1 = cell.GetPointId(0)
       i2 = cell.GetPointId(1)
       i3 = cell.GetPointId(2)
       i4 = cell.GetPointId(3)
       newcells.InsertNextCell(4)
       newcells.InsertCellPoint(i1)
       newcells.InsertCellPoint(i2)
       newcells.InsertCellPoint(i4)
       newcells.InsertCellPoint(i3)
pdo.SetCells( 10, newcells )

</source>


Helix (Source)

This is intended as the Python script for a 'Programmable Source'. It generates a helix with two sets of scalar data defined along the helix. The scalar data can be visualized using filters such as the 'tube' filter where the scalar value is represented as a color. The scalar called 'First Property' or the scalar called 'Second Property' can be chosen from the 'Display' tab of the 'tube' filter under the 'Color by' option. This lists the available scalars that are defined at points along the helix. <source lang="python">

#This script generates a helix curve.
#This is intended as the script of a 'Programmable Source'
import math

numPts = 80.0 # Points along Helix
length = 8.0 # Length of Helix
rounds = 3.0 # Number of times around 

#Get a vtk.PolyData object for the output
pdo = self.GetPolyDataOutput()

#This will store the points for the Helix
newPts = vtk.vtkPoints()

#Associate scalar values with the points.
#Also set the name for the scalar that will be
#visible in Paraview dropdown lists for setting
#color along the helix.
vals1 = vtk.vtkDoubleArray()
vals1.SetName('First Property')
vals2 = vtk.vtkDoubleArray()
vals2.SetName('Second Property')

for i in range(0, numPts):
   #Generate the Points along the Helix
   x = i*length/numPts
   y = math.sin(i*rounds*2*math.pi/numPts)
   z = math.cos(i*rounds*2*math.pi/numPts)
   #Insert the Points into the vtkPoints object
   #The first parameter indicates the reference.
   #value for the point. Here we add the sequentially.
   #Note that the first point is at index 0 (not 1).
   newPts.InsertPoint(i, x,y,z)

   #Order of Scalars should match order Pts were added
   vals1.InsertNextValue(i)
   vals2.InsertNextValue(math.sin(i*2.0*math.pi/numPts)) 

#Add the points to the vtkPolyData object
#Right now the points are not associated with a line - 
#it is just a set of unconnected points. We need to
#create a 'cell' object that ties points together
#to make, in this case, a curve. This is done below.
#A 'cell' is just an object that tell how points are
#connected to make a 1D, 2D, or 3D object.
pdo.SetPoints(newPts) 

# Add the scalar point data
pdo.GetPointData().SetScalars(vals1) #'First Property' scalar
pdo.GetPointData().AddArray(vals2) #'Second Property' scalar

#Make a vtkPolyLine which holds the info necessary
#to create a curve composed of line segments. This
#really just hold constructor data that will be passed
#to vtkPolyData to add a new line.
aPolyLine = vtk.vtkPolyLine()

#Indicate the number of points along the line
aPolyLine.GetPointIds().SetNumberOfIds(numPts)
for i in range(0,numPts):
   #Add the points to the line. The first value indicates
   #the order of the point on the line. The second value
   #is a reference to a point in a vtkPoints object. Depends
   #on the order that Points were added to vtkPoints object.
   #Note that this will not be associated with actual points
   #until it is added to a vtkPolyData object which holds a
   #vtkPoints object.
   aPolyLine.GetPointIds().SetId(i, i)

#Allocate the number of 'cells' that will be added. We are just
#adding one vtkPolyLine 'cell' to the vtkPolyData object.
pdo.Allocate(1, 1) 

#Add the poly line 'cell' to the vtkPolyData object.
pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds()) 

#The Helix is ready to plot! Click 'Apply'.

</source>