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