]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkImagePlanes.cxx
*** empty log message ***
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/03 13:35:32 $
6   Version:   $Revision: 1.15 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35
36 #ifdef _USE_VTK_
37 #include "bbvtkImagePlanes.h"
38 #include "bbvtkPackage.h"
39 #include "vtkCellPicker.h"
40 #include "vtkProperty.h"
41
42 #include "vtkMetaImageWriter.h"
43 #include "vtkPNGWriter.h"
44
45 #include "bbstdCast.h"
46 #include <vtkCommand.h>
47
48 #include "vtkImageData.h"
49 //#include "vtkOutlineFilter.h"
50 //#include "vtkPolyDataMapper.h"
51 //#include "vtkActor.h"
52 #include "vtkImagePlaneWidget.h"
53 #include "vtkCellPicker.h"
54 //#include "vtkProperty.h"
55
56 //#include "vtkRenderer.h"
57 //#include "vtkCamera.h"
58
59 #include "vtkPlaneWidget.h"
60
61
62 namespace bbstd
63 {
64
65   //====================================================================
66   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
67                                           bbtk::AtomicBlackBox);
68   //====================================================================
69
70 }
71 using namespace bbstd;
72 namespace bbvtk
73 {
74
75
76   //====================================================================
77   // Add the specialized adaptors to the package
78   typedef vtkImagePlaneWidget* I;
79   typedef vtkInteractorObserver* O;
80
81   BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(vtk,Cast,I,O);
82
83 }
84
85 namespace bbvtk
86 {
87
88   //================================================================
89  class myCallbackPlane : public vtkCommand
90  {
91  public:
92    static myCallbackPlane *New()
93       {
94          return new myCallbackPlane;
95       }
96    virtual void Execute(vtkObject *caller, unsigned long, void*)
97    {
98        currentBlackBox->Process();
99        currentBlackBox->bbSignalOutputModification();
100    } 
101    void SetCurrentBlackBox(ImagePlanes *cBB) {currentBlackBox = cBB;};    
102    void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
103    myCallbackPlane() {};
104
105  private:
106    vtkPlaneWidget *planeWidget;
107    ImagePlanes *currentBlackBox;
108  };
109   //================================================================
110
111
112
113   //================================================================
114
115    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
116    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
117
118    void ImagePlanes::bbUserConstructor() 
119    { 
120      bbSetOutputPlaneX(0);
121      bbSetOutputPlaneY(0);
122      bbSetOutputPlaneZ(0);
123      bbSetOutputImageX(0);
124      bbSetOutputImageY(0);
125      bbSetOutputImageZ(0);
126      bbSetInputIn(0);
127      std::vector<double> vect;
128      vect.push_back(0);
129      vect.push_back(0);
130      bbSetInputWindowLevel (vect);  
131    }
132    
133    void ImagePlanes::bbUserCopyConstructor(bbtk::BlackBox::Pointer) 
134    { 
135      bbSetOutputPlaneX(0);
136      bbSetOutputPlaneY(0);
137      bbSetOutputPlaneZ(0);
138      bbSetOutputImageX(0);
139      bbSetOutputImageY(0);
140      bbSetOutputImageZ(0);
141    }
142
143    void ImagePlanes::Init() 
144    {  
145
146      /// CREATION DES WIDGETS
147      if (bbGetOutputPlaneX() != 0) return;
148        
149      // The shared picker enables us to use 3 planes at one time
150      // and gets the picking order right
151      vtkCellPicker* picker = vtkCellPicker::New();
152      picker->SetTolerance(0.005);
153   
154      // The 3 image plane widgets 
155      vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
156      planeWidgetX->DisplayTextOn();
157      planeWidgetX->SetPicker(picker);
158      planeWidgetX->SetKeyPressActivationValue('x');
159      vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
160      prop1->SetColor(1, 0, 0);
161
162      vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
163      planeWidgetY->DisplayTextOn();
164      planeWidgetY->SetPicker(picker);
165      planeWidgetY->SetKeyPressActivationValue('y');
166      vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
167      prop2->SetColor(1, 1, 0);
168      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
169
170      vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
171      planeWidgetZ->DisplayTextOn();
172      planeWidgetZ->SetPicker(picker);
173      planeWidgetZ->SetKeyPressActivationValue('z');
174      vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
175      prop3->SetColor(0, 0, 1);
176      planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
177
178      bbSetOutputPlaneX(planeWidgetX);
179      bbSetOutputPlaneY(planeWidgetY);
180      bbSetOutputPlaneZ(planeWidgetZ);
181      bbSetOutputImageX(planeWidgetX->GetResliceOutput());
182      bbSetOutputImageY(planeWidgetY->GetResliceOutput());
183      bbSetOutputImageZ(planeWidgetZ->GetResliceOutput());
184
185      picker->UnRegister(NULL);
186      
187      myCallbackPlane *_myCallback = myCallbackPlane::New();
188      _myCallback->SetCurrentBlackBox(this);
189      planeWidgetX->AddObserver(vtkCommand::InteractionEvent,_myCallback);
190      planeWidgetY->AddObserver(vtkCommand::InteractionEvent,_myCallback);
191      planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,_myCallback);  
192    }
193
194 //---------------------------------------------------------------------
195   void ImagePlanes::bbUserDestructor()
196   {
197
198     if (bbGetOutputPlaneX()) bbGetOutputPlaneX()->UnRegister(NULL);
199     if (bbGetOutputPlaneY()) bbGetOutputPlaneY()->UnRegister(NULL);
200     if (bbGetOutputPlaneZ()) bbGetOutputPlaneZ()->UnRegister(NULL);
201   }
202   
203 //---------------------------------------------------------------------  
204    void ImagePlanes::Process()
205    {
206      if (bbGetInputIn()!=0)
207        {
208          // Create the widgets if not already done
209          Init();
210
211          if ( bbGetInputIn() != image)
212            {
213              // Input image has changed : reinitialize planes
214              image = bbGetInputIn();
215
216              int xMin, xMax, yMin, yMax, zMin, zMax;
217              bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
218              
219              // Initial values : center of the volume (in real world, not in pixels!)
220              double xSpacing, ySpacing, zSpacing;
221              bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
222              
223              bbGetOutputPlaneX()->SetInput(bbGetInputIn());
224              bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();  
225              bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
226              
227              bbGetOutputPlaneY()->SetInput(bbGetInputIn());
228              bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
229              bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
230              
231              bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
232              bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
233              bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
234              
235              if (bbGetInputWindowLevel()[0]!=0)
236                {
237                  bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
238                                                      bbGetInputWindowLevel()[1]);
239                }
240              else 
241                {
242                  double *range = image->GetScalarRange();
243                  bbGetOutputPlaneZ()->SetWindowLevel(range[1] - range[0],
244                                                      0.5*(range[1]+range[0]));
245                }
246            }
247          
248          
249          // UPDATE DES SORTIES 
250          bbGetOutputPlaneX()->GetResliceOutput()->Update();
251          bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
252          bbGetOutputPlaneZ()->GetResliceOutput()->Update(); 
253          
254                  
255        }
256    }
257 }//namespace bbtk
258
259 #endif // _USE_VTK_
260