1 | /*========================================================================= |
---|
2 | |
---|
3 | Program: Visualization Toolkit |
---|
4 | Module: $RCSfile: Cone6.cxx,v $ |
---|
5 | |
---|
6 | Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen |
---|
7 | All rights reserved. |
---|
8 | See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
---|
9 | |
---|
10 | This software is distributed WITHOUT ANY WARRANTY; without even |
---|
11 | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
12 | PURPOSE. See the above copyright notice for more information. |
---|
13 | |
---|
14 | =========================================================================*/ |
---|
15 | // |
---|
16 | // This example introduces 3D widgets. 3D widgets take advantage of the |
---|
17 | // event/observer design pattern introduced previously. They typically |
---|
18 | // have a particular representation in the scene which can be interactively |
---|
19 | // selected and manipulated using the mouse and keyboard. As the widgets |
---|
20 | // are manipulated, they in turn invoke events such as StartInteractionEvent, |
---|
21 | // InteractionEvent, and EndInteractionEvent which can be used to manipulate |
---|
22 | // the scene that the widget is embedded in. 3D widgets work in the context |
---|
23 | // of the event loop which was set up in the previous example. |
---|
24 | // |
---|
25 | // Note: there are more 3D widget examples in VTK/Examples/GUI/. |
---|
26 | // |
---|
27 | |
---|
28 | // First include the required header files for the VTK classes we are using. |
---|
29 | #include "vtkConeSource.h" |
---|
30 | #include "vtkPolyDataMapper.h" |
---|
31 | #include "vtkRenderWindow.h" |
---|
32 | #include "vtkRenderWindowInteractor.h" |
---|
33 | #include "vtkCamera.h" |
---|
34 | #include "vtkActor.h" |
---|
35 | #include "vtkRenderer.h" |
---|
36 | #include "vtkCommand.h" |
---|
37 | #include "vtkBoxWidget.h" |
---|
38 | #include "vtkTransform.h" |
---|
39 | #include "vtkInteractorStyleTrackballCamera.h" |
---|
40 | |
---|
41 | // |
---|
42 | // Similar to Cone2.cxx, we define a callback for interaction. |
---|
43 | // |
---|
44 | class vtkMyCallback : public vtkCommand |
---|
45 | { |
---|
46 | public: |
---|
47 | static vtkMyCallback *New() |
---|
48 | { return new vtkMyCallback; } |
---|
49 | void Delete() |
---|
50 | { delete this; } |
---|
51 | virtual void Execute(vtkObject *caller, unsigned long, void*) |
---|
52 | { |
---|
53 | vtkTransform *t = vtkTransform::New(); |
---|
54 | vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller); |
---|
55 | widget->GetTransform(t); |
---|
56 | widget->GetProp3D()->SetUserTransform(t); |
---|
57 | } |
---|
58 | }; |
---|
59 | |
---|
60 | int main( int argc, char *argv[] ) |
---|
61 | { |
---|
62 | // |
---|
63 | // Next we create an instance of vtkConeSource and set some of its |
---|
64 | // properties. The instance of vtkConeSource "cone" is part of a |
---|
65 | // visualization pipeline (it is a source process object); it produces data |
---|
66 | // (output type is vtkPolyData) which other filters may process. |
---|
67 | // |
---|
68 | vtkConeSource *cone = vtkConeSource::New(); |
---|
69 | cone->SetHeight( 3.0 ); |
---|
70 | cone->SetRadius( 1.0 ); |
---|
71 | cone->SetResolution( 10 ); |
---|
72 | |
---|
73 | // |
---|
74 | // In this example we terminate the pipeline with a mapper process object. |
---|
75 | // (Intermediate filters such as vtkShrinkPolyData could be inserted in |
---|
76 | // between the source and the mapper.) We create an instance of |
---|
77 | // vtkPolyDataMapper to map the polygonal data into graphics primitives. We |
---|
78 | // connect the output of the cone souece to the input of this mapper. |
---|
79 | // |
---|
80 | vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); |
---|
81 | coneMapper->SetInput( cone->GetOutput() ); |
---|
82 | |
---|
83 | // |
---|
84 | // Create an actor to represent the cone. The actor orchestrates rendering |
---|
85 | // of the mapper's graphics primitives. An actor also refers to properties |
---|
86 | // via a vtkProperty instance, and includes an internal transformation |
---|
87 | // matrix. We set this actor's mapper to be coneMapper which we created |
---|
88 | // above. |
---|
89 | // |
---|
90 | vtkActor *coneActor = vtkActor::New(); |
---|
91 | coneActor->SetMapper( coneMapper ); |
---|
92 | |
---|
93 | // |
---|
94 | // Create the Renderer and assign actors to it. A renderer is like a |
---|
95 | // viewport. It is part or all of a window on the screen and it is |
---|
96 | // responsible for drawing the actors it has. We also set the background |
---|
97 | // color here. |
---|
98 | // |
---|
99 | vtkRenderer *ren1= vtkRenderer::New(); |
---|
100 | ren1->AddActor( coneActor ); |
---|
101 | ren1->SetBackground( 0.1, 0.2, 0.4 ); |
---|
102 | |
---|
103 | // |
---|
104 | // Finally we create the render window which will show up on the screen. |
---|
105 | // We put our renderer into the render window using AddRenderer. We also |
---|
106 | // set the size to be 300 pixels by 300. |
---|
107 | // |
---|
108 | vtkRenderWindow *renWin = vtkRenderWindow::New(); |
---|
109 | renWin->AddRenderer( ren1 ); |
---|
110 | renWin->SetSize( 300, 300 ); |
---|
111 | |
---|
112 | // |
---|
113 | // The vtkRenderWindowInteractor class watches for events (e.g., keypress, |
---|
114 | // mouse) in the vtkRenderWindow. These events are translated into |
---|
115 | // event invocations that VTK understands (see VTK/Common/vtkCommand.h |
---|
116 | // for all events that VTK processes). Then observers of these VTK |
---|
117 | // events can process them as appropriate. |
---|
118 | vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); |
---|
119 | iren->SetRenderWindow(renWin); |
---|
120 | |
---|
121 | // |
---|
122 | // By default the vtkRenderWindowInteractor instantiates an instance |
---|
123 | // of vtkInteractorStyle. vtkInteractorStyle translates a set of events |
---|
124 | // it observes into operations on the camera, actors, and/or properties |
---|
125 | // in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. |
---|
126 | // Here we specify a particular interactor style. |
---|
127 | vtkInteractorStyleTrackballCamera *style = |
---|
128 | vtkInteractorStyleTrackballCamera::New(); |
---|
129 | iren->SetInteractorStyle(style); |
---|
130 | |
---|
131 | // |
---|
132 | // Here we use a vtkBoxWidget to transform the underlying coneActor (by |
---|
133 | // manipulating its transformation matrix). Many other types of widgets |
---|
134 | // are available for use, see the documentation for more details. |
---|
135 | // |
---|
136 | // The SetInteractor method is how 3D widgets are associated with the render |
---|
137 | // window interactor. Internally, SetInteractor sets up a bunch of callbacks |
---|
138 | // using the Command/Observer mechanism (AddObserver()). The place factor |
---|
139 | // controls the initial size of the widget with respect to the bounding box |
---|
140 | // of the input to the widget. |
---|
141 | vtkBoxWidget *boxWidget = vtkBoxWidget::New(); |
---|
142 | boxWidget->SetInteractor(iren); |
---|
143 | boxWidget->SetPlaceFactor(1.25); |
---|
144 | |
---|
145 | // |
---|
146 | // Place the interactor initially. The input to a 3D widget is used to |
---|
147 | // initially position and scale the widget. The EndInteractionEvent is |
---|
148 | // observed which invokes the SelectPolygons callback. |
---|
149 | // |
---|
150 | boxWidget->SetProp3D(coneActor); |
---|
151 | boxWidget->PlaceWidget(); |
---|
152 | vtkMyCallback *callback = vtkMyCallback::New(); |
---|
153 | boxWidget->AddObserver(vtkCommand::InteractionEvent, callback); |
---|
154 | |
---|
155 | // |
---|
156 | // Normally the user presses the "i" key to bring a 3D widget to life. Here |
---|
157 | // we will manually enable it so it appears with the cone. |
---|
158 | // |
---|
159 | boxWidget->On(); |
---|
160 | |
---|
161 | // |
---|
162 | // Start the event loop. |
---|
163 | // |
---|
164 | iren->Initialize(); |
---|
165 | iren->Start(); |
---|
166 | |
---|
167 | // |
---|
168 | // Free up any objects we created. All instances in VTK are deleted by |
---|
169 | // using the Delete() method. |
---|
170 | // |
---|
171 | cone->Delete(); |
---|
172 | coneMapper->Delete(); |
---|
173 | coneActor->Delete(); |
---|
174 | callback->Delete(); |
---|
175 | boxWidget->Delete(); |
---|
176 | ren1->Delete(); |
---|
177 | renWin->Delete(); |
---|
178 | iren->Delete(); |
---|
179 | style->Delete(); |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|