ParaView/IU

From KitwarePublic
Jump to navigationJump to search

1. Create a Triangle Using Python

from paraview import vtk

# Create a poly-data instance
pd = vtk.vtkPolyData()

# Set up the containter to save the
# point locations (geometry)
points = vtk.vtkPoints()
pd.SetPoints(points)

# Add the point coordinates
points.SetNumberOfPoints(3)
points.SetPoint(0, 0, 0, 0)
points.SetPoint(1, 2, 1, 0)
points.SetPoint(2, 3, 0, 0)

# We are adding a single triangle with
# 3 points. Create a id-list to refer to
# the point ids that form the triangle.
ids = vtk.vtkIdList()
ids.SetNumberOfIds(3)
ids.SetId(0, 0)
ids.SetId(1, 1)
ids.SetId(2, 2)

# Since this polydata has only 1 cell,
# allocate it.
pd.Allocate(1, 1)

# Insert the cell giving its type and
# the point ids that form the cell.
pd.InsertNextCell(vtk.VTK_POLYGON, ids)

2. Create a helix (Programmable Source)

#This script generates a helix curve.
#This is intended as the script of a 'Programmable Source'
import math
 
numPts = 80 # 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()
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 them sequentially.
   #Note that the first point is at index 0 (not 1).
   newPts.InsertPoint(i, x,y,z)
 
#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 a curve (in this case). This is done below.
#A 'cell' is just an object that tells how points are
#connected to make a 1D, 2D, or 3D object.
pdo.SetPoints(newPts)
 
#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())

3. Associating point attributes

import math
...

#Get a vtk.PolyData object for the output
pdo = self.GetPolyDataOutput()
 
#This will store the points for the Helix
newPts = vtk.vtkPoints()

# This will store point attributes.
pointArray = vtk.vtkIntArray()
pointArray.SetNumberOfComponents(1)
pointArray.SetNumberOfTuples(numPts)
pointArray.SetName("Length")

# Associate the pointArray to point attributes for the output
pdo.GetPointData().AddArray("pointArray")

cumulative_distance = 0
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)
   ...
   pointArray.SetValue(i, cumulative_distance)
   x0, y0, z0 = (x, y, z)
   if i > 0:
         x0, y0, z0 = newPts.GetPoint(i-1)
   cumulative_distance += math.sqrt( (x0-x)*(x0-x) + (y0-y)*(y0-y) + (z0-z)* (z0-z) )
 ...

4. Transform Data (Programmable Filter)

# Example demonstrating a Python script
# used to transform data in a Programmable Filter.
pdi = self.GetPolyDataInput()
pdo =  self.GetPolyDataOutput()
newPoints = vtk.vtkPoints()
numPoints = pdi.GetNumberOfPoints()
for i in range(0, numPoints):
    coord = pdi.GetPoint(i)
    x, y, z = coord[:3]
    x = x * 1
    y = y * 1
    z = 1 + z*0.3
    newPoints.InsertPoint(i, x, y, z)
pdo.SetPoints(newPoints)