Difference between revisions of "VTK/Examples/Python/RotationAroundLine"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
(Miss)
 
Line 6: Line 6:
 
<source lang="python">
 
<source lang="python">
 
import vtk
 
import vtk
 
+
 
# create two cones, rotate one of them.
 
# create two cones, rotate one of them.
 
+
 
# create a rendering window and renderer
 
# create a rendering window and renderer
 
ren = vtk.vtkRenderer()
 
ren = vtk.vtkRenderer()
Line 16: Line 16:
 
HEIGHT=480
 
HEIGHT=480
 
renWin.SetSize(WIDTH,HEIGHT)
 
renWin.SetSize(WIDTH,HEIGHT)
 
+
 
# create a renderwindowinteractor
 
# create a renderwindowinteractor
 
iren = vtk.vtkRenderWindowInteractor()
 
iren = vtk.vtkRenderWindowInteractor()
 
iren.SetRenderWindow(renWin)
 
iren.SetRenderWindow(renWin)
 
+
 
# create cone
 
# create cone
 
source = vtk.vtkConeSource()
 
source = vtk.vtkConeSource()
 
source.SetResolution(60)
 
source.SetResolution(60)
 
source.SetCenter(-2,0,0)
 
source.SetCenter(-2,0,0)
 
+
 
# create a transform that rotates the cone
 
# create a transform that rotates the cone
 
transform = vtk.vtkTransform()
 
transform = vtk.vtkTransform()
Line 33: Line 33:
 
transformFilter.SetInputConnection(source.GetOutputPort())
 
transformFilter.SetInputConnection(source.GetOutputPort())
 
transformFilter.Update()
 
transformFilter.Update()
 
+
 
# mapper for original cone
 
# mapper for original cone
 
coneMapper1 = vtk.vtkPolyDataMapper()
 
coneMapper1 = vtk.vtkPolyDataMapper()
coneMapper1.SetInput(source.GetOutput())
+
if vtk.VTK_MAJOR_VERSION <= 5:
 
+
    coneMapper1.SetInput(source.GetOutput())
 +
else:
 +
    coneMapper1.SetInputConnection(source.GetOutputPort())
 +
   
 +
 
# another mapper for the rotated cone
 
# another mapper for the rotated cone
 
coneMapper2 = vtk.vtkPolyDataMapper()
 
coneMapper2 = vtk.vtkPolyDataMapper()
coneMapper2.SetInput(transformFilter.GetOutput())
+
if vtk.VTK_MAJOR_VERSION <= 5:
 
+
    coneMapper2.SetInput(transformFilter.GetOutput())
 +
else:
 +
    coneMapper2.SetInputConnection(transformFilter.GetOutputPort())
 +
 
# actor for original cone
 
# actor for original cone
 
actor1 = vtk.vtkActor()
 
actor1 = vtk.vtkActor()
 
actor1.SetMapper(coneMapper1)
 
actor1.SetMapper(coneMapper1)
 
+
 
# actor for rotated cone
 
# actor for rotated cone
 
actor2 = vtk.vtkActor()
 
actor2 = vtk.vtkActor()
 
actor2.SetMapper(coneMapper2)
 
actor2.SetMapper(coneMapper2)
 
+
 
+
 
# color the original cone red
 
# color the original cone red
 
actor1.GetProperty().SetColor(1,0,0) # (R,G,B)
 
actor1.GetProperty().SetColor(1,0,0) # (R,G,B)
 
# color rotated cone blue
 
# color rotated cone blue
 
actor2.GetProperty().SetColor(0,0,1) # (R,G,B)
 
actor2.GetProperty().SetColor(0,0,1) # (R,G,B)
 
+
 
# assign actor to the renderer
 
# assign actor to the renderer
 
ren.AddActor(actor1)
 
ren.AddActor(actor1)
 
ren.AddActor(actor2)
 
ren.AddActor(actor2)
 
+
 
# enable user interface interactor
 
# enable user interface interactor
 
iren.Initialize()
 
iren.Initialize()

Latest revision as of 21:25, 28 May 2012

NOTE: this example shows both the original and the rotated object using different colors. Not exactly equivalent to the C++ example with the same name.

Rotationaroundline.png

RotationAroundLine.py

import vtk
 
# create two cones, rotate one of them.
 
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
WIDTH=640
HEIGHT=480
renWin.SetSize(WIDTH,HEIGHT)
 
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
 
# create cone
source = vtk.vtkConeSource()
source.SetResolution(60)
source.SetCenter(-2,0,0)
 
# create a transform that rotates the cone
transform = vtk.vtkTransform()
transform.RotateWXYZ(45,0,1,0)
transformFilter=vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputConnection(source.GetOutputPort())
transformFilter.Update()
 
# mapper for original cone
coneMapper1 = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    coneMapper1.SetInput(source.GetOutput())
else:
    coneMapper1.SetInputConnection(source.GetOutputPort())
    
 
# another mapper for the rotated cone
coneMapper2 = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    coneMapper2.SetInput(transformFilter.GetOutput())
else:
    coneMapper2.SetInputConnection(transformFilter.GetOutputPort())
 
# actor for original cone
actor1 = vtk.vtkActor()
actor1.SetMapper(coneMapper1)
 
# actor for rotated cone
actor2 = vtk.vtkActor()
actor2.SetMapper(coneMapper2)
 
 
# color the original cone red
actor1.GetProperty().SetColor(1,0,0) # (R,G,B)
# color rotated cone blue
actor2.GetProperty().SetColor(0,0,1) # (R,G,B)
 
# assign actor to the renderer
ren.AddActor(actor1)
ren.AddActor(actor2)
 
# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()