ParaView/IU

From KitwarePublic
Jump to navigationJump to search

1. Create a Triangle Using Python

<source lang="python"> from paraview import vtk

  1. Create a poly-data instance

pd = vtk.vtkPolyData()

  1. Set up the containter to save the
  2. point locations (geometry)

points = vtk.vtkPoints() pd.SetPoints(points)

  1. 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)

  1. We are adding a single triangle with
  2. 3 points. Create a id-list to refer to
  3. 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)

  1. Since this polydata has only 1 cell,
  2. allocate it.

pd.Allocate(1, 1)

  1. Insert the cell giving its type and
  2. the point ids that form the cell.

pd.InsertNextCell(vtk.VTK_POLYGON, ids)

</source>

2. Create a helix (Programmable Source)

<source lang="python">

  1. This script generates a helix curve.
  2. 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

  1. Get a vtk.PolyData object for the output

pdo = self.GetPolyDataOutput()

  1. 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)

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

pdo.SetPoints(newPts)

  1. Make a vtkPolyLine which holds the info necessary
  2. to create a curve composed of line segments. This
  3. really just hold constructor data that will be passed
  4. to vtkPolyData to add a new line.

aPolyLine = vtk.vtkPolyLine()

  1. 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)

  1. Allocate the number of 'cells' that will be added. We are just
  2. adding one vtkPolyLine 'cell' to the vtkPolyData object.

pdo.Allocate(1, 1)

  1. Add the poly line 'cell' to the vtkPolyData object.

pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds())

</source>

3. Associating point attributes

<source lang="python">

import math ...

  1. Get a vtk.PolyData object for the output

pdo = self.GetPolyDataOutput()

  1. This will store the points for the Helix

newPts = vtk.vtkPoints()

  1. This will store point attributes.

pointArray = vtk.vtkIntArray() pointArray.SetNumberOfComponents(1) pointArray.SetNumberOfTuples(numPts) pointArray.SetName("Length")

  1. 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) )
...

</source>

4. Transform Data (Programmable Filter)

<source lang="python">

  1. Example demonstrating a Python script
  2. 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)

</source>