]> Creatis software - bbtk.git/blob - packages/kw/src/vtkKWSlicer.cxx
0c9cd4777b7d160192ce19e4d7aff8384150b621
[bbtk.git] / packages / kw / src / vtkKWSlicer.cxx
1 #ifdef USE_KWWIDGETS
2
3 #include "vtkKWSlicer.h"
4
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"
25
26 //#include "vtkKWWidgetsPaths.h"
27 #include "vtkToolkits.h"
28
29 #include <vtksys/SystemTools.hxx>
30
31 namespace bbkw
32 {
33
34 //----------------------------------------------------------------------------
35 vtkStandardNewMacro( vtkKWSlicer );
36 vtkCxxRevisionMacro(vtkKWSlicer, "$Revision: 1.1 $");
37
38 //----------------------------------------------------------------------------
39 vtkKWSlicer::vtkKWSlicer()
40 {
41   this->RenderWidget = NULL;
42   this->ImageViewer = NULL;
43   this->SliceScale = NULL;
44   this->WindowLevelPresetSelector = NULL;
45   this->AnimationWidget = NULL;
46 }
47
48 //----------------------------------------------------------------------------
49 vtkKWSlicer::~vtkKWSlicer()
50 {
51   if (this->SliceScale)
52     {
53     this->SliceScale->Delete();
54     }
55   if (this->ImageViewer)
56     {
57     this->ImageViewer->Delete();
58     }
59   if (this->RenderWidget)
60     {
61     this->RenderWidget->Delete();
62     }
63   if (this->WindowLevelPresetSelector)
64     {
65     this->WindowLevelPresetSelector->Delete();
66     }
67   if (this->AnimationWidget)
68     {
69     this->AnimationWidget->Delete();
70     }
71 }
72
73 //----------------------------------------------------------------------------
74 void vtkKWSlicer::CreateWidget()
75 {
76   // Check if already created
77
78   if (this->IsCreated())
79     {
80     vtkErrorMacro("class already created");
81     return;
82     }
83
84   // Call the superclass to create the whole widget
85
86   this->Superclass::CreateWidget();
87
88   vtkKWApplication *app = this->GetApplication();
89
90   // Add a render widget, attach it to the view frame, and pack
91
92   if (!this->RenderWidget)
93     {
94     this->RenderWidget = vtkKWRenderWidget::New();
95     }
96   this->RenderWidget->SetParent(this); //->GetViewFrame());
97   this->RenderWidget->Create();
98   this->RenderWidget->CornerAnnotationVisibilityOn();
99
100   app->Script("pack %s -expand y -fill both -anchor c -expand y", 
101               this->RenderWidget->GetWidgetName());
102
103   // Create a volume reader
104   /*
105   vtkXMLImageDataReader *reader = vtkXMLImageDataReader::New();
106
107   char data_path[2048];
108   sprintf(data_path, "%s/Data/head100x100x47.vti", KWWidgets_EXAMPLES_DIR);
109   if (!vtksys::SystemTools::FileExists(data_path))
110     {
111     sprintf(data_path,
112             "%s/..%s/Examples/Data/head100x100x47.vti",
113             app->GetInstallationDirectory(), KWWidgets_INSTALL_DATA_DIR);
114     }
115   reader->SetFileName(data_path);
116   */
117   // Create an image viewer
118   // Use the render window and renderer of the renderwidget
119
120   if (!this->ImageViewer)
121     {
122     this->ImageViewer = vtkImageViewer2::New();
123     }
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());
129
130   // Reset the window/level and the camera
131   /*
132   reader->Update();
133   double *range = reader->GetOutput()->GetScalarRange();
134   this->ImageViewer->SetColorWindow(range[1] - range[0]);
135   this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
136
137   this->RenderWidget->ResetCamera();
138   */
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
143   // image mapper
144
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>");
150
151   // Create a scale to control the slice
152
153   if (!this->SliceScale)
154     {
155     this->SliceScale = vtkKWScale::New();
156     }
157   this->SliceScale->SetParent(this); //->GetViewPanelFrame());
158   this->SliceScale->Create();
159   this->SliceScale->SetCommand(this, "SetSliceFromScaleCallback");
160
161   app->Script("pack %s -side top -expand n -fill x -padx 2 -pady 2",
162               this->SliceScale->GetWidgetName());
163
164   // Create a menu button to control the orientation
165
166   vtkKWMenuButtonWithSpinButtonsWithLabel *orientation_menubutton =
167     vtkKWMenuButtonWithSpinButtonsWithLabel::New();
168
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();
176
177   app->Script("pack %s -side top -anchor nw -expand n -fill x",
178               orientation_menubutton->GetWidgetName());
179
180   vtkKWMenuButton *mb = orientation_menubutton->GetWidget()->GetWidget();
181   vtkKWMenu *menu = mb->GetMenu();
182
183   menu->AddRadioButton("X-Y", this, "SetSliceOrientationToXYCallback");
184   menu->AddRadioButton("X-Z", this, "SetSliceOrientationToXZCallback");
185   menu->AddRadioButton("Y-Z", this, "SetSliceOrientationToYZCallback");
186
187   mb->SetValue("X-Y");
188
189   // Create a window/level preset selector
190
191   vtkKWFrameWithLabel *wl_frame = vtkKWFrameWithLabel::New();
192   wl_frame->SetParent(this); //->GetMainPanelFrame());
193   wl_frame->Create();
194   wl_frame->SetLabelText("Window/Level Presets");
195
196   app->Script("pack %s -side top -anchor nw -expand n -fill x -pady 2",
197               wl_frame->GetWidgetName());
198
199   if (!this->WindowLevelPresetSelector)
200     {
201     this->WindowLevelPresetSelector = vtkKWWindowLevelPresetSelector::New();
202     }
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");
214
215   app->Script("pack %s -side top -anchor nw -expand n -fill x",
216               this->WindowLevelPresetSelector->GetWidgetName());
217
218   // Create a simple animation widget
219
220   vtkKWFrameWithLabel *animation_frame = vtkKWFrameWithLabel::New();
221   animation_frame->SetParent(this); //->GetMainPanelFrame());
222   animation_frame->Create();
223   animation_frame->SetLabelText("Movie Creator");
224
225   app->Script("pack %s -side top -anchor nw -expand n -fill x -pady 2",
226               animation_frame->GetWidgetName());
227
228   if (!this->AnimationWidget)
229     {
230     this->AnimationWidget = vtkKWSimpleAnimationWidget::New();
231     }
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");
238
239   app->Script("pack %s -side top -anchor nw -expand n -fill x",
240               this->AnimationWidget->GetWidgetName());
241
242   this->UpdateSliceRanges();
243
244   // Deallocate local objects
245
246   //  reader->Delete();
247   orientation_menubutton->Delete();
248   wl_frame->Delete();
249   animation_frame->Delete();
250 }
251
252
253 //----------------------------------------------------------------------------
254 void vtkKWSlicer::SetImage(vtkImageData* image)
255 {
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(); 
263   i->Delete();
264   this->UpdateSliceRanges();
265 }
266 //----------------------------------------------------------------------------
267
268 //----------------------------------------------------------------------------
269 void vtkKWSlicer::SetSliceFromScaleCallback(double value)
270 {
271   this->ImageViewer->SetSlice((int)value);
272 }
273
274 //----------------------------------------------------------------------------
275 void vtkKWSlicer::SetSliceCallback(int slice)
276 {
277   this->ImageViewer->SetSlice(slice);
278 }
279
280 //----------------------------------------------------------------------------
281 int vtkKWSlicer::GetSliceCallback()
282 {
283   return this->ImageViewer->GetSlice();
284 }
285
286 //----------------------------------------------------------------------------
287 int vtkKWSlicer::GetSliceMinCallback()
288 {
289   return this->ImageViewer->GetSliceMin();
290 }
291
292 //----------------------------------------------------------------------------
293 int vtkKWSlicer::GetSliceMaxCallback()
294 {
295   return this->ImageViewer->GetSliceMax();
296 }
297
298 //----------------------------------------------------------------------------
299 void vtkKWSlicer::UpdateSliceRanges()
300 {
301   this->SliceScale->SetRange(
302     this->ImageViewer->GetSliceMin(), this->ImageViewer->GetSliceMax());
303   this->SliceScale->SetValue(this->ImageViewer->GetSlice());
304
305   this->AnimationWidget->SetSliceRange(
306     this->ImageViewer->GetSliceMin(), this->ImageViewer->GetSliceMax());
307 }
308
309 //----------------------------------------------------------------------------
310 void vtkKWSlicer::SetSliceOrientationToXYCallback()
311 {
312   this->ImageViewer->SetSliceOrientationToXY();
313   this->UpdateSliceRanges();
314 }
315
316 //----------------------------------------------------------------------------
317 void vtkKWSlicer::SetSliceOrientationToXZCallback()
318 {
319   this->ImageViewer->SetSliceOrientationToXZ();
320   this->UpdateSliceRanges();
321 }
322
323 //----------------------------------------------------------------------------
324 void vtkKWSlicer::SetSliceOrientationToYZCallback()
325 {
326   this->ImageViewer->SetSliceOrientationToYZ();
327   this->UpdateSliceRanges();
328 }
329
330 //----------------------------------------------------------------------------
331 void vtkKWSlicer::WindowLevelPresetApplyCallback(int id)
332 {
333   if (this->WindowLevelPresetSelector->HasPreset(id))
334     {
335     this->ImageViewer->SetColorWindow(
336       this->WindowLevelPresetSelector->GetPresetWindow(id));
337     this->ImageViewer->SetColorLevel(
338       this->WindowLevelPresetSelector->GetPresetLevel(id));
339     this->ImageViewer->Render();
340     }
341 }
342 //----------------------------------------------------------------------------
343 int vtkKWSlicer::WindowLevelPresetAddCallback()
344 {
345   int id = this->WindowLevelPresetSelector->AddPreset();
346   this->WindowLevelPresetUpdateCallback(id);
347   this->WindowLevelPresetSelector->SelectPreset(id);
348   return id;
349 }
350
351 //----------------------------------------------------------------------------
352 void vtkKWSlicer::WindowLevelPresetUpdateCallback(int id)
353 {
354   this->WindowLevelPresetSelector->SetPresetWindow(
355     id, this->ImageViewer->GetColorWindow());
356   this->WindowLevelPresetSelector->SetPresetLevel(
357     id, this->ImageViewer->GetColorLevel());
358   this->WindowLevelPresetHasChangedCallback(id);
359 }
360
361 //----------------------------------------------------------------------------
362 void vtkKWSlicer::WindowLevelPresetHasChangedCallback(int id)
363 {
364   this->WindowLevelPresetSelector->
365     BuildPresetThumbnailAndScreenshotFromRenderWindow(
366       id, this->RenderWidget->GetRenderWindow());
367 }
368
369
370 } // namespace kw
371 #endif // USE_KWWIDGETS