[vtkusers] tube filter with varying radius along spline
John Hunter
jdhunter at ace.bsd.uchicago.edu
Thu Oct 9 10:24:37 EDT 2003
>>>>> "John" == John Hunter <jdhunter at ace.bsd.uchicago.edu> writes:
John> I am trying to use a vtkTubeFilter along a spline curve
John> where the radius of the tube and/or the color of the tube
John> vary with the spline independent variable (t in the example
John> below). I am not sure how to achieve this. Below is a
John> script which I have been using for testing, but all the vary
John> radius and vary color calls have been without effect. I
John> think this is because I do not have the scalar data set
John> properly, but am not sure how to proceed. I have looked at
John> the officeTube example and the streamline example from the
John> VTK textbook, but haven't been able to adapt these to my
John> case. Here is the example, any suggestions will be much
John> appreciated.
OK, figured this one out with a little bit of help from the textbook.
Following the example in Figure 5-17, I learned how to associate the
scalar value t with my poly data, and can control tube thickness and
color along the spline with the following modified script.
import vtk
numberOfInputPoints = 3
aSplineX = vtk.vtkCardinalSpline()
aSplineY = vtk.vtkCardinalSpline()
aSplineZ = vtk.vtkCardinalSpline()
inputPoints = vtk.vtkPoints()
aSplineX.AddPoint(0, 0)
aSplineY.AddPoint(0, 0)
aSplineZ.AddPoint(0, 0)
inputPoints.InsertPoint(0, 0, 0, 0)
aSplineX.AddPoint(1, 1)
aSplineY.AddPoint(1, 1)
aSplineZ.AddPoint(1, 0)
inputPoints.InsertPoint(1, 1, 1, 0)
aSplineX.AddPoint(2, 2)
aSplineY.AddPoint(2, 4)
aSplineZ.AddPoint(2, 0)
inputPoints.InsertPoint(2, 2, 4, 0)
# Generate the polyline for the spline.
points = vtk.vtkPoints()
profileData = vtk.vtkPolyData()
scalars = vtk.vtkFloatArray()
# Number of points on the spline
numberOfOutputPoints = 40
# Interpolate x, y and z by using the three spline filters and
# create new points
for i in range(numberOfOutputPoints):
t = (numberOfInputPoints-1.0)/(numberOfOutputPoints-1.0)*i
points.InsertPoint(i, aSplineX.Evaluate(t), aSplineY.Evaluate(t),
aSplineZ.Evaluate(t))
scalars.InsertTuple1(i,t)
# Create the polyline.
lines = vtk.vtkCellArray()
lines.InsertNextCell(numberOfOutputPoints)
for i in range(numberOfOutputPoints):
lines.InsertCellPoint(i)
profileData.SetPoints(points)
profileData.SetLines(lines)
profileData.GetPointData().SetScalars(scalars)
# Add thickness to the resulting line.
profileTubes = vtk.vtkTubeFilter()
profileTubes.SetNumberOfSides(8)
profileTubes.SetInput(profileData)
profileTubes.SetRadius(.01)
# Vary tube thickness with scalar
profileTubes.SetRadiusFactor(20)
profileTubes.SetVaryRadiusToVaryRadiusByScalar()
profileMapper = vtk.vtkPolyDataMapper()
profileMapper.SetInput(profileTubes.GetOutput())
profileMapper.SetScalarRange(0,t)
#Set this to Off to turn off color variation with scalar
profileMapper.ScalarVisibilityOn()
profile = vtk.vtkActor()
profile.SetMapper(profileMapper)
profile.GetProperty().SetSpecular(.3)
profile.GetProperty().SetSpecularPower(30)
# Now create the RenderWindow, Renderer and Interactor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors
ren.AddActor(profile)
renWin.SetSize(500, 500)
iren.Initialize()
renWin.Render()
iren.Start()
More information about the vtkusers
mailing list