1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#!/usr/bin/env python
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
# Now create the RenderWindow, Renderer and Interactor
#
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
math = vtk.vtkMath()
numberOfInputPoints = 30
aSplineX = vtk.vtkCardinalSpline()
aSplineY = vtk.vtkCardinalSpline()
aSplineZ = vtk.vtkCardinalSpline()
# generate random points
inputPoints = vtk.vtkPoints()
i = 0
while i < numberOfInputPoints:
x = math.Random(0, 1)
y = math.Random(0, 1)
z = math.Random(0, 1)
aSplineX.AddPoint(i, x)
aSplineY.AddPoint(i, y)
aSplineZ.AddPoint(i, z)
inputPoints.InsertPoint(i, x, y, z)
i += 1
inputData = vtk.vtkPolyData()
inputData.SetPoints(inputPoints)
balls = vtk.vtkSphereSource()
balls.SetRadius(.01)
balls.SetPhiResolution(10)
balls.SetThetaResolution(10)
glyphPoints = vtk.vtkGlyph3D()
glyphPoints.SetInputData(inputData)
glyphPoints.SetSourceConnection(balls.GetOutputPort())
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputConnection(glyphPoints.GetOutputPort())
glyph = vtk.vtkActor()
glyph.SetMapper(glyphMapper)
glyph.GetProperty().SetDiffuseColor(1, 0.4, 0.4)
glyph.GetProperty().SetSpecular(.3)
glyph.GetProperty().SetSpecularPower(30)
ren1.AddActor(glyph)
# create a polyline
points = vtk.vtkPoints()
profileData = vtk.vtkPolyData()
numberOfOutputPoints = 400
offset = 1.0
def fit ():
points.Reset()
i = 0
while i < numberOfOutputPoints:
t = (numberOfInputPoints - offset) / (numberOfOutputPoints - 1) * i
points.InsertPoint(i, aSplineX.Evaluate(t), aSplineY.Evaluate(t), aSplineZ.Evaluate(t))
i += 1
profileData.Modified()
fit()
lines = vtk.vtkCellArray()
lines.InsertNextCell(numberOfOutputPoints)
i = 0
while i < numberOfOutputPoints:
lines.InsertCellPoint(i)
i += 1
profileData.SetPoints(points)
profileData.SetLines(lines)
profileTubes = vtk.vtkTubeFilter()
profileTubes.SetNumberOfSides(8)
profileTubes.SetInputData(profileData)
profileTubes.SetRadius(.005)
profileMapper = vtk.vtkPolyDataMapper()
profileMapper.SetInputConnection(profileTubes.GetOutputPort())
profile = vtk.vtkActor()
profile.SetMapper(profileMapper)
profile.GetProperty().SetDiffuseColor(1, 1, 0.6)
profile.GetProperty().SetSpecular(.3)
profile.GetProperty().SetSpecularPower(30)
ren1.AddActor(profile)
ren1.ResetCamera()
ren1.GetActiveCamera().Dolly(1.5)
ren1.ResetCameraClippingRange()
renWin.SetSize(400, 400)
# render the image
#
iren.Initialize()
def opened (aSplineX, aSplineY, aSplineZ):
offset = 1.0
aSplineX.ClosedOff()
aSplineY.ClosedOff()
aSplineZ.ClosedOff()
fit()
renWin.Render()
def varyLeft (aSplineX, aSplineY, aSplineZ):
left = -1
while left <= 1:
aSplineX.SetLeftValue(left)
aSplineY.SetLeftValue(left)
aSplineZ.SetLeftValue(left)
fit()
renWin.Render()
left += 0.05
def varyRight (aSplineX, aSplineY, aSplineZ):
right = -1
while right <= 1:
aSplineX.SetRightValue(right)
aSplineY.SetRightValue(right)
aSplineZ.SetRightValue(right)
fit()
renWin.Render()
right += 0.05
def constraint (value, aSplineX, aSplineY, aSplineZ):
aSplineX.SetLeftConstraint(value)
aSplineY.SetLeftConstraint(value)
aSplineZ.SetLeftConstraint(value)
aSplineX.SetRightConstraint(value)
aSplineY.SetRightConstraint(value)
aSplineZ.SetRightConstraint(value)
def closed (aSplineX, aSplineY, aSplineZ):
offset = 0.0
aSplineX.ClosedOn()
aSplineY.ClosedOn()
aSplineZ.ClosedOn()
fit()
renWin.Render()
#iren.Start()
|