ParaView/IU: Difference between revisions
No edit summary |
|||
Line 36: | Line 36: | ||
</source> | </source> | ||
== Create a helix (Programmable Source) == | ==2. Create a helix (Programmable Source) == | ||
<source lang="python"> | |||
#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()) | |||
</source> | |||
==3. Associating point attributes == | |||
<source lang="python"> | <source lang="python"> |
Revision as of 18:47, 21 March 2012
1. Create a Triangle Using Python
<source lang="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)
</source>
2. Create a helix (Programmable Source)
<source lang="python">
- 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())
</source>
3. Associating point attributes
<source lang="python">
- 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())
</source>