3 #include "vtkKWSlicer.h"
5 #include "vtkCornerAnnotation.h"
6 #include "vtkImageData.h"
7 #include "vtkImageViewer2.h"
8 #include "vtkKWApplication.h"
9 #include "vtkKWFrame.h"
10 #include "vtkKWFrameWithLabel.h"
11 #include "vtkKWMenu.h"
12 #include "vtkKWMenuButton.h"
13 #include "vtkKWMenuButtonWithSpinButtons.h"
14 #include "vtkKWMenuButtonWithSpinButtonsWithLabel.h"
15 #include "vtkKWNotebook.h"
16 #include "vtkKWRenderWidget.h"
17 #include "vtkKWScale.h"
18 #include "vtkKWSimpleAnimationWidget.h"
19 #include "vtkKWWindow.h"
20 #include "vtkKWWindowLevelPresetSelector.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkRenderWindow.h"
23 #include "vtkRenderWindowInteractor.h"
24 #include "vtkXMLImageDataReader.h"
26 //#include "vtkKWWidgetsPaths.h"
27 #include "vtkToolkits.h"
29 #include <vtksys/SystemTools.hxx>
34 //----------------------------------------------------------------------------
35 vtkStandardNewMacro( vtkKWSlicer );
36 vtkCxxRevisionMacro(vtkKWSlicer, "$Revision: 1.1 $");
38 //----------------------------------------------------------------------------
39 vtkKWSlicer::vtkKWSlicer()
41 this->RenderWidget = NULL;
42 this->ImageViewer = NULL;
43 this->SliceScale = NULL;
44 this->WindowLevelPresetSelector = NULL;
45 this->AnimationWidget = NULL;
48 //----------------------------------------------------------------------------
49 vtkKWSlicer::~vtkKWSlicer()
53 this->SliceScale->Delete();
55 if (this->ImageViewer)
57 this->ImageViewer->Delete();
59 if (this->RenderWidget)
61 this->RenderWidget->Delete();
63 if (this->WindowLevelPresetSelector)
65 this->WindowLevelPresetSelector->Delete();
67 if (this->AnimationWidget)
69 this->AnimationWidget->Delete();
73 //----------------------------------------------------------------------------
74 void vtkKWSlicer::CreateWidget()
76 // Check if already created
78 if (this->IsCreated())
80 vtkErrorMacro("class already created");
84 // Call the superclass to create the whole widget
86 this->Superclass::CreateWidget();
88 vtkKWApplication *app = this->GetApplication();
90 // Add a render widget, attach it to the view frame, and pack
92 if (!this->RenderWidget)
94 this->RenderWidget = vtkKWRenderWidget::New();
96 this->RenderWidget->SetParent(this); //->GetViewFrame());
97 this->RenderWidget->Create();
98 this->RenderWidget->CornerAnnotationVisibilityOn();
100 app->Script("pack %s -expand y -fill both -anchor c -expand y",
101 this->RenderWidget->GetWidgetName());
103 // Create a volume reader
105 vtkXMLImageDataReader *reader = vtkXMLImageDataReader::New();
107 char data_path[2048];
108 sprintf(data_path, "%s/Data/head100x100x47.vti", KWWidgets_EXAMPLES_DIR);
109 if (!vtksys::SystemTools::FileExists(data_path))
112 "%s/..%s/Examples/Data/head100x100x47.vti",
113 app->GetInstallationDirectory(), KWWidgets_INSTALL_DATA_DIR);
115 reader->SetFileName(data_path);
117 // Create an image viewer
118 // Use the render window and renderer of the renderwidget
120 if (!this->ImageViewer)
122 this->ImageViewer = vtkImageViewer2::New();
124 this->ImageViewer->SetRenderWindow(this->RenderWidget->GetRenderWindow());
125 this->ImageViewer->SetRenderer(this->RenderWidget->GetRenderer());
126 // this->ImageViewer->SetInput(reader->GetOutput());
127 this->ImageViewer->SetupInteractor(
128 this->RenderWidget->GetRenderWindow()->GetInteractor());
130 // Reset the window/level and the camera
133 double *range = reader->GetOutput()->GetScalarRange();
134 this->ImageViewer->SetColorWindow(range[1] - range[0]);
135 this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
137 this->RenderWidget->ResetCamera();
139 // The corner annotation has the ability to parse "tags" and fill
140 // them with information gathered from other objects.
141 // For example, let's display the slice and window/level in one corner
142 // by connecting the corner annotation to our image actor and
145 vtkCornerAnnotation *ca = this->RenderWidget->GetCornerAnnotation();
146 ca->SetImageActor(this->ImageViewer->GetImageActor());
147 ca->SetWindowLevel(this->ImageViewer->GetWindowLevel());
148 ca->SetText(2, "<slice>");
149 ca->SetText(3, "<window>\n<level>");
151 // Create a scale to control the slice
153 if (!this->SliceScale)
155 this->SliceScale = vtkKWScale::New();
157 this->SliceScale->SetParent(this); //->GetViewPanelFrame());
158 this->SliceScale->Create();
159 this->SliceScale->SetCommand(this, "SetSliceFromScaleCallback");
161 app->Script("pack %s -side top -expand n -fill x -padx 2 -pady 2",
162 this->SliceScale->GetWidgetName());
164 // Create a menu button to control the orientation
166 vtkKWMenuButtonWithSpinButtonsWithLabel *orientation_menubutton =
167 vtkKWMenuButtonWithSpinButtonsWithLabel::New();
169 orientation_menubutton->SetParent(this); //->GetMainPanelFrame());
170 orientation_menubutton->Create();
171 orientation_menubutton->SetLabelText("Orientation:");
172 orientation_menubutton->SetPadX(2);
173 orientation_menubutton->SetPadY(2);
174 orientation_menubutton->SetBorderWidth(2);
175 orientation_menubutton->SetReliefToGroove();
177 app->Script("pack %s -side top -anchor nw -expand n -fill x",
178 orientation_menubutton->GetWidgetName());
180 vtkKWMenuButton *mb = orientation_menubutton->GetWidget()->GetWidget();
181 vtkKWMenu *menu = mb->GetMenu();
183 menu->AddRadioButton("X-Y", this, "SetSliceOrientationToXYCallback");
184 menu->AddRadioButton("X-Z", this, "SetSliceOrientationToXZCallback");
185 menu->AddRadioButton("Y-Z", this, "SetSliceOrientationToYZCallback");
189 // Create a window/level preset selector
191 vtkKWFrameWithLabel *wl_frame = vtkKWFrameWithLabel::New();
192 wl_frame->SetParent(this); //->GetMainPanelFrame());
194 wl_frame->SetLabelText("Window/Level Presets");
196 app->Script("pack %s -side top -anchor nw -expand n -fill x -pady 2",
197 wl_frame->GetWidgetName());
199 if (!this->WindowLevelPresetSelector)
201 this->WindowLevelPresetSelector = vtkKWWindowLevelPresetSelector::New();
203 this->WindowLevelPresetSelector->SetParent(wl_frame->GetFrame());
204 this->WindowLevelPresetSelector->Create();
205 this->WindowLevelPresetSelector->ThumbnailColumnVisibilityOn();
206 this->WindowLevelPresetSelector->SetPresetAddCommand(
207 this, "WindowLevelPresetAddCallback");
208 this->WindowLevelPresetSelector->SetPresetApplyCommand(
209 this, "WindowLevelPresetApplyCallback");
210 this->WindowLevelPresetSelector->SetPresetUpdateCommand(
211 this, "WindowLevelPresetUpdateCallback");
212 this->WindowLevelPresetSelector->SetPresetHasChangedCommand(
213 this, "WindowLevelPresetHasChangedCallback");
215 app->Script("pack %s -side top -anchor nw -expand n -fill x",
216 this->WindowLevelPresetSelector->GetWidgetName());
218 // Create a simple animation widget
220 vtkKWFrameWithLabel *animation_frame = vtkKWFrameWithLabel::New();
221 animation_frame->SetParent(this); //->GetMainPanelFrame());
222 animation_frame->Create();
223 animation_frame->SetLabelText("Movie Creator");
225 app->Script("pack %s -side top -anchor nw -expand n -fill x -pady 2",
226 animation_frame->GetWidgetName());
228 if (!this->AnimationWidget)
230 this->AnimationWidget = vtkKWSimpleAnimationWidget::New();
232 this->AnimationWidget->SetParent(animation_frame->GetFrame());
233 this->AnimationWidget->Create();
234 this->AnimationWidget->SetRenderWidget(this->RenderWidget);
235 this->AnimationWidget->SetAnimationTypeToSlice();
236 this->AnimationWidget->SetSliceSetCommand(this, "SetSliceCallback");
237 this->AnimationWidget->SetSliceGetCommand(this, "GetSliceCallback");
239 app->Script("pack %s -side top -anchor nw -expand n -fill x",
240 this->AnimationWidget->GetWidgetName());
242 this->UpdateSliceRanges();
244 // Deallocate local objects
247 orientation_menubutton->Delete();
249 animation_frame->Delete();
253 //----------------------------------------------------------------------------
254 void vtkKWSlicer::SetImage(vtkImageData* image)
256 vtkImageData* i = vtkImageData::New();
257 i->ShallowCopy(image);
258 this->ImageViewer->SetInput(i);
259 double *range = i->GetScalarRange();
260 this->ImageViewer->SetColorWindow(range[1] - range[0]);
261 this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
262 this->RenderWidget->ResetCamera();
264 this->UpdateSliceRanges();
266 //----------------------------------------------------------------------------
268 //----------------------------------------------------------------------------
269 void vtkKWSlicer::SetSliceFromScaleCallback(double value)
271 this->ImageViewer->SetSlice((int)value);
274 //----------------------------------------------------------------------------
275 void vtkKWSlicer::SetSliceCallback(int slice)
277 this->ImageViewer->SetSlice(slice);
280 //----------------------------------------------------------------------------
281 int vtkKWSlicer::GetSliceCallback()
283 return this->ImageViewer->GetSlice();
286 //----------------------------------------------------------------------------
287 int vtkKWSlicer::GetSliceMinCallback()
289 return this->ImageViewer->GetSliceMin();
292 //----------------------------------------------------------------------------
293 int vtkKWSlicer::GetSliceMaxCallback()
295 return this->ImageViewer->GetSliceMax();
298 //----------------------------------------------------------------------------
299 void vtkKWSlicer::UpdateSliceRanges()
301 this->SliceScale->SetRange(
302 this->ImageViewer->GetSliceMin(), this->ImageViewer->GetSliceMax());
303 this->SliceScale->SetValue(this->ImageViewer->GetSlice());
305 this->AnimationWidget->SetSliceRange(
306 this->ImageViewer->GetSliceMin(), this->ImageViewer->GetSliceMax());
309 //----------------------------------------------------------------------------
310 void vtkKWSlicer::SetSliceOrientationToXYCallback()
312 this->ImageViewer->SetSliceOrientationToXY();
313 this->UpdateSliceRanges();
316 //----------------------------------------------------------------------------
317 void vtkKWSlicer::SetSliceOrientationToXZCallback()
319 this->ImageViewer->SetSliceOrientationToXZ();
320 this->UpdateSliceRanges();
323 //----------------------------------------------------------------------------
324 void vtkKWSlicer::SetSliceOrientationToYZCallback()
326 this->ImageViewer->SetSliceOrientationToYZ();
327 this->UpdateSliceRanges();
330 //----------------------------------------------------------------------------
331 void vtkKWSlicer::WindowLevelPresetApplyCallback(int id)
333 if (this->WindowLevelPresetSelector->HasPreset(id))
335 this->ImageViewer->SetColorWindow(
336 this->WindowLevelPresetSelector->GetPresetWindow(id));
337 this->ImageViewer->SetColorLevel(
338 this->WindowLevelPresetSelector->GetPresetLevel(id));
339 this->ImageViewer->Render();
342 //----------------------------------------------------------------------------
343 int vtkKWSlicer::WindowLevelPresetAddCallback()
345 int id = this->WindowLevelPresetSelector->AddPreset();
346 this->WindowLevelPresetUpdateCallback(id);
347 this->WindowLevelPresetSelector->SelectPreset(id);
351 //----------------------------------------------------------------------------
352 void vtkKWSlicer::WindowLevelPresetUpdateCallback(int id)
354 this->WindowLevelPresetSelector->SetPresetWindow(
355 id, this->ImageViewer->GetColorWindow());
356 this->WindowLevelPresetSelector->SetPresetLevel(
357 id, this->ImageViewer->GetColorLevel());
358 this->WindowLevelPresetHasChangedCallback(id);
361 //----------------------------------------------------------------------------
362 void vtkKWSlicer::WindowLevelPresetHasChangedCallback(int id)
364 this->WindowLevelPresetSelector->
365 BuildPresetThumbnailAndScreenshotFromRenderWindow(
366 id, this->RenderWidget->GetRenderWindow());
371 #endif // USE_KWWIDGETS