The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings MMD
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

a19ea140a5935ca6ed5d36b44ad45104dc626e19
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import vtk
6 '''
7 There are two alternative ways to apply the transform.
8  1) Use vtkTransformPolyDataFilter to create a new transformed polydata.
9     This method is useful if the transformed polydata is needed
10       later in the pipeline
11     To do this, set USER_MATRIX = True
12  2) Apply the transform directly to the actor using vtkProp3D's SetUserMatrix.
13     No new data is produced.
14     To do this, set USER_MATRIX = False
15 '''
16 USER_MATRIX = True
19 def main():
20     colors = vtk.vtkNamedColors()
22     # Set the background color.
23     colors.SetColor("BkgColor", [26, 51, 77, 255])
26     # Create an arrow.
27     arrowSource = vtk.vtkArrowSource()
29     # Generate a random start and end point
30     startPoint = [0] * 3
31     endPoint = [0] * 3
32     rng = vtk.vtkMinimalStandardRandomSequence()
33     rng.SetSeed(8775070)  # For testing.
34     for i in range(0, 3):
35         rng.Next()
36         startPoint[i] = rng.GetRangeValue(-10, 10)
37         rng.Next()
38         endPoint[i] = rng.GetRangeValue(-10, 10)
40     # Compute a basis
41     normalizedX = [0] * 3
42     normalizedY = [0] * 3
43     normalizedZ = [0] * 3
45     # The X axis is a vector from start to end
46     vtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)
47     length = vtk.vtkMath.Norm(normalizedX)
48     vtk.vtkMath.Normalize(normalizedX)
50     # The Z axis is an arbitrary vector cross X
51     arbitrary = [0] * 3
52     for i in range(0, 3):
53         rng.Next()
54         arbitrary[i] = rng.GetRangeValue(-10, 10)
55     vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)
56     vtk.vtkMath.Normalize(normalizedZ)
58     # The Y axis is Z cross X
59     vtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)
60     matrix = vtk.vtkMatrix4x4()
62     # Create the direction cosine matrix
63     matrix.Identity()
64     for i in range(0, 3):
65         matrix.SetElement(i, 0, normalizedX[i])
66         matrix.SetElement(i, 1, normalizedY[i])
67         matrix.SetElement(i, 2, normalizedZ[i])
69     # Apply the transforms
70     transform = vtk.vtkTransform()
71     transform.Translate(startPoint)
72     transform.Concatenate(matrix)
73     transform.Scale(length, length, length)
75     # Transform the polydata
76     transformPD = vtk.vtkTransformPolyDataFilter()
77     transformPD.SetTransform(transform)
78     transformPD.SetInputConnection(arrowSource.GetOutputPort())
80     # Create a mapper and actor for the arrow
81     mapper = vtk.vtkPolyDataMapper()
82     actor = vtk.vtkActor()
83     if USER_MATRIX:
84         mapper.SetInputConnection(arrowSource.GetOutputPort())
85         actor.SetUserMatrix(transform.GetMatrix())
86     else:
87         mapper.SetInputConnection(transformPD.GetOutputPort())
88     actor.SetMapper(mapper)
89     actor.GetProperty().SetColor(colors.GetColor3d("Cyan"))
91     # Create spheres for start and end point
92     sphereStartSource = vtk.vtkSphereSource()
93     sphereStartSource.SetCenter(startPoint)
94     sphereStartSource.SetRadius(0.8)
95     sphereStartMapper = vtk.vtkPolyDataMapper()
96     sphereStartMapper.SetInputConnection(sphereStartSource.GetOutputPort())
97     sphereStart = vtk.vtkActor()
98     sphereStart.SetMapper(sphereStartMapper)
99     sphereStart.GetProperty().SetColor(colors.GetColor3d("Yellow"))
101     sphereEndSource = vtk.vtkSphereSource()
102     sphereEndSource.SetCenter(endPoint)
103     sphereEndSource.SetRadius(0.8)
104     sphereEndMapper = vtk.vtkPolyDataMapper()
105     sphereEndMapper.SetInputConnection(sphereEndSource.GetOutputPort())
106     sphereEnd = vtk.vtkActor()
107     sphereEnd.SetMapper(sphereEndMapper)
108     sphereEnd.GetProperty().SetColor(colors.GetColor3d("Magenta"))
110     # Create a renderer, render window, and interactor
111     renderer = vtk.vtkRenderer()
112     renderWindow = vtk.vtkRenderWindow()
113     renderWindow.SetWindowName("Oriented Arrow")
114     renderWindow.AddRenderer(renderer)
115     renderWindowInteractor = vtk.vtkRenderWindowInteractor()
116     renderWindowInteractor.SetRenderWindow(renderWindow)
118     # Add the actor to the scene
119     renderer.AddActor(actor)
120     renderer.AddActor(sphereStart)
121     renderer.AddActor(sphereEnd)
122     renderer.SetBackground(colors.GetColor3d("BkgColor"))
124     # Render and interact
125     renderWindow.Render()
126     renderWindowInteractor.Start()
129 if __name__ == '__main__':
130     main()