]> Creatis software - crea.git/blob - src/creaVtkBasicSlicer.cxx
#3180 crea Feature New Normal Future - Set wx-config for wxWidgets 2.8
[crea.git] / src / creaVtkBasicSlicer.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */                                                                         
27
28 #ifdef USE_VTK
29
30 #include <creaVtkBasicSlicer.h>
31
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderWindowInteractor.h"
34
35 #include "vtkOutlineFilter.h"
36 #include "vtkPolyDataMapper.h"
37 #include "vtkActor.h"
38 #include "vtkImagePlaneWidget.h"
39 #include "vtkCellPicker.h"
40 #include "vtkProperty.h"
41 #include "vtkRenderer.h"
42 #include "vtkCamera.h"
43
44 #include "vtkInteractorStyleTrackballCamera.h"
45
46 namespace crea
47 {
48   
49   void VtkBasicSlicer( vtkImageData* I )
50   {
51
52     int xMin, xMax, yMin, yMax, zMin, zMax;
53   I->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
54
55   double sx, sy, sz;
56   I->GetSpacing(sx, sy, sz);
57
58   double ox, oy, oz;
59   I->GetOrigin(ox,oy,oz);
60
61   
62   // An outline is shown for context.
63   
64   vtkOutlineFilter* outline = vtkOutlineFilter::New();
65   outline->SetInput(I);
66   
67   vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper::New();
68   outlineMapper->SetInputConnection(outline->GetOutputPort());
69   
70   vtkActor* outlineActor = vtkActor::New();
71   outlineActor->SetMapper(outlineMapper);
72   
73
74   // The shared picker enables us to use 3 planes at one time
75   // and gets the picking order right
76   vtkCellPicker* picker = vtkCellPicker::New();
77   picker->SetTolerance(0.005);
78   
79   // The 3 image plane widgets are used to probe the dataset.
80   vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
81   planeWidgetX->DisplayTextOn();
82   planeWidgetX->SetInput(I);
83   planeWidgetX->SetPlaneOrientationToXAxes();
84   planeWidgetX->SetSlicePosition((xMax+xMin)/2.);
85   planeWidgetX->SetPicker(picker);
86   planeWidgetX->SetKeyPressActivationValue('x');
87   vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
88   prop1->SetColor(1, 0, 0);
89
90   vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
91   planeWidgetY->DisplayTextOn();
92   planeWidgetY->SetInput(I);
93   planeWidgetY->SetPlaneOrientationToYAxes();
94   planeWidgetY->SetSlicePosition((yMax+yMin)/2.);
95   planeWidgetY->SetPicker(picker);
96   planeWidgetY->SetKeyPressActivationValue('y');
97   vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
98   prop2->SetColor(1, 1, 0);
99   planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
100
101   // for the z-slice, turn off texture interpolation:
102   // interpolation is now nearest neighbour, to demonstrate
103   // cross-hair cursor snapping to pixel centers
104   vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
105   planeWidgetZ->DisplayTextOn();
106   planeWidgetZ->SetInput(I);
107   planeWidgetZ->SetPlaneOrientationToZAxes();
108   planeWidgetZ->SetSlicePosition((zMax+zMin)/2.);
109   planeWidgetZ->SetPicker(picker);
110   planeWidgetZ->SetKeyPressActivationValue('z');
111   vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
112   prop3->SetColor(0, 0, 1);
113   planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
114
115   planeWidgetZ->SetWindowLevel(512,256);
116
117   // Create the RenderWindow and Renderer
118   vtkRenderer* ren = vtkRenderer::New();
119   vtkRenderWindow* renWin = vtkRenderWindow::New();
120   renWin->AddRenderer(ren);
121   
122   // Add the outline actor to the renderer, set the background color and size
123   ren->AddActor(outlineActor);
124   renWin->SetSize(600, 600);
125   ren->SetBackground(0.1, 0.1, 0.2);
126   
127   // an interactor
128   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
129   iren->SetInteractorStyle( vtkInteractorStyleTrackballCamera::New() );
130   iren->SetRenderWindow(renWin);
131
132   // Set the interactor for the widgets
133   planeWidgetX->SetInteractor(iren);
134   planeWidgetX->On();
135   planeWidgetY->SetInteractor(iren);
136   planeWidgetY->On();
137   planeWidgetZ->SetInteractor(iren);
138   planeWidgetZ->On();
139
140   // Create an initial interesting view
141   vtkCamera* cam1 = ren->GetActiveCamera();
142   cam1->SetFocalPoint(0, 0, 0);
143   cam1->SetPosition(0, 0, -1500);
144   //cam1->OrthogonalizeViewUp();
145   cam1->Elevation(110);
146   cam1->SetViewUp(0, 0, -1);
147   cam1->Azimuth(45);
148   ren->ResetCameraClippingRange();
149
150   // Render it
151   //  render_widget->Render();
152
153   iren->Initialize();
154   renWin->Render();
155   iren->Start();
156
157   ren->Delete();  
158   renWin->Delete();
159   iren->Delete();
160   planeWidgetX->Delete();
161   planeWidgetY->Delete();
162   planeWidgetZ->Delete();
163   outline->Delete();
164   outlineMapper->Delete();
165   outlineActor->Delete();
166   picker->Delete();
167   }
168
169 }
170 #endif // USE_VTK