1 | # |
---|
2 | # This example introduces 3D widgets. 3D widgets take advantage of the |
---|
3 | # event/observer design pattern introduced previously. They typically |
---|
4 | # have a particular representation in the scene which can be interactively |
---|
5 | # selected and manipulated using the mouse and keyboard. As the widgets |
---|
6 | # are manipulated, they in turn invoke events such as StartInteractionEvent, |
---|
7 | # InteractionEvent, and EndInteractionEvent which can be used to manipulate |
---|
8 | # the scene that the widget is embedded in. 3D widgets work in the context |
---|
9 | # of the event loop which was set up in the previous example. |
---|
10 | # |
---|
11 | # Note: there are more 3D widget examples in VTK/Examples/GUI/. |
---|
12 | # |
---|
13 | |
---|
14 | # |
---|
15 | # First we include the VTK Tcl packages which will make available |
---|
16 | # all of the VTK commands to Tcl. |
---|
17 | # |
---|
18 | package require vtk |
---|
19 | |
---|
20 | # |
---|
21 | # Next we create an instance of vtkConeSource and set some of its |
---|
22 | # properties. The instance of vtkConeSource "cone" is part of a visualization |
---|
23 | # pipeline (it is a source process object); it produces data (output type is |
---|
24 | # vtkPolyData) which other filters may process. |
---|
25 | # |
---|
26 | vtkConeSource cone |
---|
27 | cone SetHeight 3.0 |
---|
28 | cone SetRadius 1.0 |
---|
29 | cone SetResolution 10 |
---|
30 | |
---|
31 | # |
---|
32 | # In this example we terminate the pipeline with a mapper process object. |
---|
33 | # (Intermediate filters such as vtkShrinkPolyData could be inserted in |
---|
34 | # between the source and the mapper.) We create an instance of |
---|
35 | # vtkPolyDataMapper to map the polygonal data into graphics primitives. We |
---|
36 | # connect the output of the cone souece to the input of this mapper. |
---|
37 | # |
---|
38 | vtkPolyDataMapper coneMapper |
---|
39 | coneMapper SetInput [cone GetOutput] |
---|
40 | |
---|
41 | # |
---|
42 | # Create an actor to represent the cone. The actor orchestrates rendering of |
---|
43 | # the mapper's graphics primitives. An actor also refers to properties via a |
---|
44 | # vtkProperty instance, and includes an internal transformation matrix. We |
---|
45 | # set this actor's mapper to be coneMapper which we created above. |
---|
46 | # |
---|
47 | vtkActor coneActor |
---|
48 | coneActor SetMapper coneMapper |
---|
49 | |
---|
50 | # |
---|
51 | # Create the Renderer and assign actors to it. A renderer is like a |
---|
52 | # viewport. It is part or all of a window on the screen and it is responsible |
---|
53 | # for drawing the actors it has. We also set the background color here. |
---|
54 | # |
---|
55 | vtkRenderer ren1 |
---|
56 | ren1 AddActor coneActor |
---|
57 | ren1 SetBackground 0.1 0.2 0.4 |
---|
58 | |
---|
59 | # |
---|
60 | # Finally we create the render window which will show up on the screen |
---|
61 | # We put our renderer into the render window using AddRenderer. We also |
---|
62 | # set the size to be 300 pixels by 300. |
---|
63 | # |
---|
64 | vtkRenderWindow renWin |
---|
65 | renWin AddRenderer ren1 |
---|
66 | renWin SetSize 300 300 |
---|
67 | |
---|
68 | # |
---|
69 | # The vtkRenderWindowInteractor class watches for events (e.g., keypress, |
---|
70 | # mouse) in the vtkRenderWindow. These events are translated into |
---|
71 | # event invocations that VTK understands (see VTK/Common/vtkCommand.h |
---|
72 | # for all events that VTK processes). Then observers of these VTK |
---|
73 | # events can process them as appropriate. |
---|
74 | vtkRenderWindowInteractor iren |
---|
75 | iren SetRenderWindow renWin |
---|
76 | |
---|
77 | # |
---|
78 | # By default the vtkRenderWindowInteractor instantiates an instance |
---|
79 | # of vtkInteractorStyle. vtkInteractorStyle translates a set of events |
---|
80 | # it observes into operations on the camera, actors, and/or properties |
---|
81 | # in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. |
---|
82 | # Here we specify a particular interactor style. |
---|
83 | vtkInteractorStyleTrackballCamera style |
---|
84 | iren SetInteractorStyle style |
---|
85 | |
---|
86 | # |
---|
87 | # Here we use a vtkBoxWidget to transform the underlying coneActor (by |
---|
88 | # manipulating its transformation matrix). Many other types of widgets |
---|
89 | # are available for use, see the documentation for more details. |
---|
90 | # |
---|
91 | # The SetInteractor method is how 3D widgets are associated with the render |
---|
92 | # window interactor. Internally, SetInteractor sets up a bunch of callbacks |
---|
93 | # using the Command/Observer mechanism (AddObserver()). The place factor |
---|
94 | # controls the initial size of the widget with respect to the bounding box |
---|
95 | # of the input to the widget. |
---|
96 | vtkBoxWidget boxWidget |
---|
97 | boxWidget SetInteractor iren |
---|
98 | boxWidget SetPlaceFactor 1.25 |
---|
99 | |
---|
100 | # |
---|
101 | # Place the interactor initially. The input to a 3D widget is used to |
---|
102 | # initially position and scale the widget. The EndInteractionEvent is |
---|
103 | # observed which invokes the SelectPolygons callback. |
---|
104 | # |
---|
105 | boxWidget SetInput [cone GetOutput] |
---|
106 | boxWidget PlaceWidget |
---|
107 | boxWidget AddObserver InteractionEvent TransformActor |
---|
108 | |
---|
109 | # |
---|
110 | # Normally the user presses the "i" key to bring a 3D widget to life. Here |
---|
111 | # we will manually enable it so it appears with the cone. |
---|
112 | # |
---|
113 | boxWidget On |
---|
114 | |
---|
115 | # |
---|
116 | # We can use the vtkInteract Tcl/Tk interactor at the same time as |
---|
117 | # the box widget. |
---|
118 | # |
---|
119 | iren AddObserver UserEvent {wm deiconify .vtkInteract} |
---|
120 | |
---|
121 | # |
---|
122 | # Initialize starts the event loop. Once the render window appears, mouse |
---|
123 | # in the window to move the camera. If you select the box widget, |
---|
124 | # depending on what is selected, the widget will change shape. As this |
---|
125 | # is happening, it will invoke InteractionEvents on itself. These are |
---|
126 | # caught by the observer which in turn invokes the Tcl proc TransformActor |
---|
127 | # (defined below). If you do not select the box widget, then the events |
---|
128 | # are received by the interactor style, which manipulates the camera as |
---|
129 | # usual. It is possible to have many widgets running simultaneously, and to |
---|
130 | # prioritize the processing of events. |
---|
131 | # |
---|
132 | iren Initialize |
---|
133 | |
---|
134 | # |
---|
135 | # Since we are in the Tcl/Tk environment, we prevent the empty "." |
---|
136 | # window from appearing with the Tk "withdraw" command. |
---|
137 | # |
---|
138 | wm withdraw . |
---|
139 | |
---|
140 | # As the box widget is interacted with, it produces a transformation |
---|
141 | # matrix that is set on the actor. |
---|
142 | vtkTransform t |
---|
143 | proc TransformActor {} { |
---|
144 | boxWidget GetTransform t |
---|
145 | coneActor SetUserTransform t |
---|
146 | } |
---|